annotate src/unexmips.c @ 22:8fc7fe29b841 r19-15b94

Import from CVS: tag r19-15b94
author cvs
date Mon, 13 Aug 2007 08:50:29 +0200
parents 376386a54a3c
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 /* Unexec for MIPS (including IRIS4D).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Copyright (C) 1988, 1992, 1993, 1994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 /* Synched up with: FSF 19.31. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 #include <varargs.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 #ifdef MACH
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 #include <a.out.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 /* I don't know why this isn't defined. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 #ifndef STYP_INIT
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 #define STYP_INIT 0x80000000
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 /* I don't know why this isn't defined. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 #ifndef _RDATA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 #define _RDATA ".rdata"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 #define STYP_RDATA 0x00000100
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 /* Small ("near") data section. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 #ifndef _SDATA
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #define _SDATA ".sdata"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #define STYP_SDATA 0x00000200
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 /* Small ("near") bss section. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 #ifndef _SBSS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 #define _SBSS ".sbss"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 #define STYP_SBSS 0x00000400
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 /* We don't seem to have a sym.h or syms.h anywhere, so we'll do it the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 hard way. This stinks. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 typedef struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 short magic;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 short vstamp;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 long ilineMax;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 struct { long foo, offset; } offsets[11];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 } HDRR, *pHDRR;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #else /* not MACH */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #include <filehdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #include <aouthdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #include <scnhdr.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 #include <sym.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 #endif /* not MACH */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 #if defined (IRIS_4D) || defined (sony)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 #include "getpagesize.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 static void fatal_unexec ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 static void mark_x ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 #define READ(_fd, _buffer, _size, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 errno = EEOF; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 if (read (_fd, _buffer, _size) != _size) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 fatal_unexec (_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 #define WRITE(_fd, _buffer, _size, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 if (write (_fd, _buffer, _size) != _size) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 fatal_unexec (_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 #define SEEK(_fd, _position, _error_message, _error_arg) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 errno = EEOF; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 if (lseek (_fd, _position, L_SET) != _position) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 fatal_unexec (_error_message, _error_arg);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 extern int errno;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 extern char *strerror ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 #define EEOF -1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 static struct scnhdr *text_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 static struct scnhdr *init_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 static struct scnhdr *finit_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 static struct scnhdr *rdata_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 static struct scnhdr *data_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 static struct scnhdr *lit8_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 static struct scnhdr *lit4_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 static struct scnhdr *sdata_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 static struct scnhdr *sbss_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 static struct scnhdr *bss_section;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 struct headers {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 struct filehdr fhdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 struct aouthdr aout;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 struct scnhdr section[10];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 };
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 /* Define name of label for entry point for the dumped executable. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 #ifndef DEFAULT_ENTRY_ADDRESS
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 #define DEFAULT_ENTRY_ADDRESS __start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 unexec (new_name, a_name, data_start, bss_start, entry_address)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 char *new_name, *a_name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 unsigned data_start, bss_start, entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 int new, old;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 int pagesize, brk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 int newsyms, symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 int nread;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 struct headers hdr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 int i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 int vaddr, scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 #define BUFSIZE 8192
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 char buffer[BUFSIZE];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 old = open (a_name, O_RDONLY, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 if (old < 0) fatal_unexec ("opening %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 new = creat (new_name, 0666);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 if (new < 0) fatal_unexec ("creating %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 hdr = *((struct headers *)TEXT_START);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 #ifdef MIPS2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 if (hdr.fhdr.f_magic != MIPSELMAGIC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 && hdr.fhdr.f_magic != MIPSEBMAGIC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 && hdr.fhdr.f_magic != (MIPSELMAGIC | 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 && hdr.fhdr.f_magic != (MIPSEBMAGIC | 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 fprintf (stderr,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 "unexec: input file magic number is %x, not %x, %x, %x or %x.\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 hdr.fhdr.f_magic,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 MIPSELMAGIC, MIPSEBMAGIC,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 MIPSELMAGIC | 1, MIPSEBMAGIC | 1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 #else /* not MIPS2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 if (hdr.fhdr.f_magic != MIPSELMAGIC
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 && hdr.fhdr.f_magic != MIPSEBMAGIC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 fprintf (stderr, "unexec: input file magic number is %x, not %x or %x.\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 hdr.fhdr.f_magic, MIPSELMAGIC, MIPSEBMAGIC);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 #endif /* not MIPS2 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 if (hdr.fhdr.f_opthdr != sizeof (hdr.aout))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 fprintf (stderr, "unexec: input a.out header is %d bytes, not %d.\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 hdr.fhdr.f_opthdr, sizeof (hdr.aout));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 if (hdr.aout.magic != ZMAGIC)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 fprintf (stderr, "unexec: input file a.out magic number is %o, not %o.\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 hdr.aout.magic, ZMAGIC);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 #define CHECK_SCNHDR(ptr, name, flags) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 ptr = NULL; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 for (i = 0; i < hdr.fhdr.f_nscns && !ptr; i++) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 if (strcmp (hdr.section[i].s_name, name) == 0) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 if (hdr.section[i].s_flags != flags) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 fprintf (stderr, "unexec: %x flags (%x expected) in %s section.\n", \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 hdr.section[i].s_flags, flags, name); \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 ptr = hdr.section + i; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 CHECK_SCNHDR (text_section, _TEXT, STYP_TEXT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 CHECK_SCNHDR (init_section, _INIT, STYP_INIT);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 CHECK_SCNHDR (rdata_section, _RDATA, STYP_RDATA);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 CHECK_SCNHDR (data_section, _DATA, STYP_DATA);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 #ifdef _LIT8
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 CHECK_SCNHDR (lit8_section, _LIT8, STYP_LIT8);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 CHECK_SCNHDR (lit4_section, _LIT4, STYP_LIT4);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 #endif /* _LIT8 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 CHECK_SCNHDR (sdata_section, _SDATA, STYP_SDATA);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 CHECK_SCNHDR (sbss_section, _SBSS, STYP_SBSS);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 CHECK_SCNHDR (bss_section, _BSS, STYP_BSS);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 #if 0 /* Apparently this error check goes off on irix 3.3,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 but it doesn't indicate a real problem. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 if (i != hdr.fhdr.f_nscns)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 fprintf (stderr, "unexec: %d sections found instead of %d.\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 i, hdr.fhdr.f_nscns);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 text_section->s_scnptr = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 pagesize = getpagesize ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 /* Casting to int avoids compiler error on NEWS-OS 5.0.2. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 brk = (((int) (sbrk (0))) + pagesize - 1) & (-pagesize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 hdr.aout.dsize = brk - DATA_START;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 hdr.aout.bsize = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 if (entry_address == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 extern DEFAULT_ENTRY_ADDRESS ();
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 hdr.aout.entry = (unsigned)DEFAULT_ENTRY_ADDRESS;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 hdr.aout.entry = entry_address;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 hdr.aout.bss_start = hdr.aout.data_start + hdr.aout.dsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 rdata_section->s_size = data_start - DATA_START;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 /* Adjust start and virtual addresses of rdata_section, too. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 rdata_section->s_vaddr = DATA_START;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 rdata_section->s_paddr = DATA_START;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 rdata_section->s_scnptr = text_section->s_scnptr + hdr.aout.tsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 data_section->s_vaddr = data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 data_section->s_paddr = data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 data_section->s_size = brk - data_start;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 data_section->s_scnptr = rdata_section->s_scnptr + rdata_section->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 vaddr = data_section->s_vaddr + data_section->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 scnptr = data_section->s_scnptr + data_section->s_size;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 if (lit8_section != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 lit8_section->s_vaddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 lit8_section->s_paddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 lit8_section->s_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 lit8_section->s_scnptr = scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 if (lit4_section != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 lit4_section->s_vaddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 lit4_section->s_paddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 lit4_section->s_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 lit4_section->s_scnptr = scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 if (sdata_section != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 sdata_section->s_vaddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 sdata_section->s_paddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 sdata_section->s_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 sdata_section->s_scnptr = scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 if (sbss_section != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 sbss_section->s_vaddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 sbss_section->s_paddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 sbss_section->s_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 sbss_section->s_scnptr = scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 if (bss_section != NULL)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 bss_section->s_vaddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 bss_section->s_paddr = vaddr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 bss_section->s_size = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 bss_section->s_scnptr = scnptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 WRITE (new, (void *) TEXT_START, hdr.aout.tsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 "writing text section to %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 WRITE (new, (void *) DATA_START, hdr.aout.dsize,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 "writing data section to %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 SEEK (old, hdr.fhdr.f_symptr, "seeking to start of symbols in %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 errno = EEOF;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 nread = read (old, buffer, BUFSIZE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 if (nread < sizeof (HDRR)) fatal_unexec ("reading symbols from %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 newsyms = hdr.aout.tsize + hdr.aout.dsize;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 symrel = newsyms - hdr.fhdr.f_symptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 hdr.fhdr.f_symptr = newsyms;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 #define symhdr ((pHDRR)buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 #ifdef MACH
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 for (i = 0; i < 11; i++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 symhdr->offsets[i].offset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 symhdr->cbLineOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 symhdr->cbDnOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 symhdr->cbPdOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 symhdr->cbSymOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 symhdr->cbOptOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 symhdr->cbAuxOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 symhdr->cbSsOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 symhdr->cbSsExtOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 symhdr->cbFdOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 symhdr->cbRfdOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 symhdr->cbExtOffset += symrel;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 #undef symhdr
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 if (write (new, buffer, nread) != nread)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 fatal_unexec ("writing symbols to %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 nread = read (old, buffer, BUFSIZE);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 if (nread < 0) fatal_unexec ("reading symbols from %s", a_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 #undef BUFSIZE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 } while (nread != 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 SEEK (new, 0, "seeking to start of header in %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 WRITE (new, &hdr, sizeof (hdr),
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 "writing header of %s", new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 close (old);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 close (new);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 mark_x (new_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 * mark_x
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 * After successfully building the new a.out, mark it executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 mark_x (name)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 char *name;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 struct stat sbuf;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 int um = umask (777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 umask (um);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 if (stat (name, &sbuf) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 fatal_unexec ("getting protection on %s", name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 sbuf.st_mode |= 0111 & ~um;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 if (chmod (name, sbuf.st_mode) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 fatal_unexec ("setting protection on %s", name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 static void
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 fatal_unexec (s, va_alist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 va_dcl
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 va_list ap;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 if (errno == EEOF)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 fputs ("unexec: unexpected end of file, ", stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 fprintf (stderr, "unexec: %s, ", strerror (errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 va_start (ap);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 _doprnt (s, ap, stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 fputs (".\n", stderr);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 exit (1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 }