0
|
1 /* Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
|
|
2
|
|
3 This file is part of XEmacs.
|
|
4
|
|
5 XEmacs is free software; you can redistribute it and/or modify it
|
|
6 under the terms of the GNU General Public License as published by the
|
|
7 Free Software Foundation; either version 2, or (at your option) any
|
|
8 later version.
|
|
9
|
|
10 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
13 for more details.
|
|
14
|
|
15 You should have received a copy of the GNU General Public License
|
|
16 along with XEmacs; see the file COPYING. If not, write to
|
|
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
18 Boston, MA 02111-1307, USA. */
|
|
19
|
|
20 /* Synched up with: FSF 19.31. */
|
|
21
|
|
22 /* Modified by Andrew.Vignaux@comp.vuw.ac.nz to get it to work :-) */
|
|
23
|
|
24 /*
|
|
25 * unexec.c - Convert a running program into an a.out file.
|
|
26 *
|
|
27 * Author: Spencer W. Thomas
|
|
28 * Computer Science Dept.
|
|
29 * University of Utah
|
|
30 * Date: Tue Mar 2 1982
|
|
31 * Modified heavily since then.
|
|
32 *
|
|
33 * Updated for AIX 4.1.3 by Bill_Mann @ PraxisInt.com, Feb 1996
|
|
34 * As of AIX 4.1, text, data, and bss are pre-relocated by the binder in
|
|
35 * such a way that the file can be mapped with code in one segment and
|
|
36 * data/bss in another segment, without reading or copying the file, by
|
|
37 * the AIX exec loader. Padding sections are omitted, nevertheless
|
|
38 * small amounts of 'padding' still occurs between sections in the file.
|
|
39 * As modified, this code handles both 3.2 and 4.1 conventions.
|
|
40 *
|
|
41 * Synopsis:
|
|
42 * unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
43 * char *new_name, *a_name;
|
|
44 * unsigned data_start, bss_start, entry_address;
|
|
45 *
|
|
46 * Takes a snapshot of the program and makes an a.out format file in the
|
|
47 * file named by the string argument new_name.
|
|
48 * If a_name is non-NULL, the symbol table will be taken from the given file.
|
|
49 * On some machines, an existing a_name file is required.
|
|
50 *
|
|
51 * The boundaries within the a.out file may be adjusted with the data_start
|
|
52 * and bss_start arguments. Either or both may be given as 0 for defaults.
|
|
53 *
|
|
54 * Data_start gives the boundary between the text segment and the data
|
|
55 * segment of the program. The text segment can contain shared, read-only
|
|
56 * program code and literal data, while the data segment is always unshared
|
|
57 * and unprotected. Data_start gives the lowest unprotected address.
|
|
58 * The value you specify may be rounded down to a suitable boundary
|
|
59 * as required by the machine you are using.
|
|
60 *
|
|
61 * Specifying zero for data_start means the boundary between text and data
|
|
62 * should not be the same as when the program was loaded.
|
|
63 * If NO_REMAP is defined, the argument data_start is ignored and the
|
|
64 * segment boundaries are never changed.
|
|
65 *
|
|
66 * Bss_start indicates how much of the data segment is to be saved in the
|
|
67 * a.out file and restored when the program is executed. It gives the lowest
|
|
68 * unsaved address, and is rounded up to a page boundary. The default when 0
|
|
69 * is given assumes that the entire data segment is to be stored, including
|
|
70 * the previous data and bss as well as any additional storage allocated with
|
|
71 * break (2).
|
|
72 *
|
|
73 * The new file is set up to start at entry_address.
|
|
74 *
|
|
75 * If you make improvements I'd like to get them too.
|
|
76 * harpo!utah-cs!thomas, thomas@Utah-20
|
|
77 *
|
|
78 */
|
|
79
|
|
80 /* There are several compilation parameters affecting unexec:
|
|
81
|
|
82 * COFF
|
|
83
|
|
84 Define this if your system uses COFF for executables.
|
|
85 Otherwise we assume you use Berkeley format.
|
|
86
|
|
87 * NO_REMAP
|
|
88
|
|
89 Define this if you do not want to try to save Emacs's pure data areas
|
|
90 as part of the text segment.
|
|
91
|
|
92 Saving them as text is good because it allows users to share more.
|
|
93
|
|
94 However, on machines that locate the text area far from the data area,
|
|
95 the boundary cannot feasibly be moved. Such machines require
|
|
96 NO_REMAP.
|
|
97
|
|
98 Also, remapping can cause trouble with the built-in startup routine
|
|
99 /lib/crt0.o, which defines `environ' as an initialized variable.
|
|
100 Dumping `environ' as pure does not work! So, to use remapping,
|
|
101 you must write a startup routine for your machine in Emacs's crt0.c.
|
|
102 If NO_REMAP is defined, Emacs uses the system's crt0.o.
|
|
103
|
|
104 * SECTION_ALIGNMENT
|
|
105
|
|
106 Some machines that use COFF executables require that each section
|
|
107 start on a certain boundary *in the COFF file*. Such machines should
|
|
108 define SECTION_ALIGNMENT to a mask of the low-order bits that must be
|
|
109 zero on such a boundary. This mask is used to control padding between
|
|
110 segments in the COFF file.
|
|
111
|
|
112 If SECTION_ALIGNMENT is not defined, the segments are written
|
|
113 consecutively with no attempt at alignment. This is right for
|
|
114 unmodified system V.
|
|
115
|
|
116 * SEGMENT_MASK
|
|
117
|
|
118 Some machines require that the beginnings and ends of segments
|
|
119 *in core* be on certain boundaries. For most machines, a page
|
|
120 boundary is sufficient. That is the default. When a larger
|
|
121 boundary is needed, define SEGMENT_MASK to a mask of
|
|
122 the bits that must be zero on such a boundary.
|
|
123
|
|
124 * A_TEXT_OFFSET(HDR)
|
|
125
|
|
126 Some machines count the a.out header as part of the size of the text
|
|
127 segment (a_text); they may actually load the header into core as the
|
|
128 first data in the text segment. Some have additional padding between
|
|
129 the header and the real text of the program that is counted in a_text.
|
|
130
|
|
131 For these machines, define A_TEXT_OFFSET(HDR) to examine the header
|
|
132 structure HDR and return the number of bytes to add to `a_text'
|
|
133 before writing it (above and beyond the number of bytes of actual
|
|
134 program text). HDR's standard fields are already correct, except that
|
|
135 this adjustment to the `a_text' field has not yet been made;
|
|
136 thus, the amount of offset can depend on the data in the file.
|
|
137
|
|
138 * A_TEXT_SEEK(HDR)
|
|
139
|
|
140 If defined, this macro specifies the number of bytes to seek into the
|
|
141 a.out file before starting to write the text segment.a
|
|
142
|
|
143 * EXEC_MAGIC
|
|
144
|
|
145 For machines using COFF, this macro, if defined, is a value stored
|
|
146 into the magic number field of the output file.
|
|
147
|
|
148 * ADJUST_EXEC_HEADER
|
|
149
|
|
150 This macro can be used to generate statements to adjust or
|
|
151 initialize nonstandard fields in the file header
|
|
152
|
|
153 * ADDR_CORRECT(ADDR)
|
|
154
|
|
155 Macro to correct an int which is the bit pattern of a pointer to a byte
|
|
156 into an int which is the number of a byte.
|
|
157
|
|
158 This macro has a default definition which is usually right.
|
|
159 This default definition is a no-op on most machines (where a
|
|
160 pointer looks like an int) but not on all machines.
|
|
161
|
|
162 */
|
|
163
|
|
164 #define XCOFF
|
|
165 #define COFF
|
|
166 #define NO_REMAP
|
|
167
|
|
168 #ifndef emacs
|
|
169 #define PERROR(arg) perror (arg); return -1
|
|
170 #else
|
|
171 #include <config.h>
|
|
172 #define PERROR(file) report_error (file, new)
|
|
173 #endif
|
|
174
|
|
175 #include <a.out.h>
|
|
176 /* Define getpagesize () if the system does not.
|
|
177 Note that this may depend on symbols defined in a.out.h
|
|
178 */
|
|
179 #include "getpagesize.h"
|
|
180
|
|
181 #ifndef makedev /* Try to detect types.h already loaded */
|
|
182 #include <sys/types.h>
|
|
183 #endif
|
|
184 #include <stdio.h>
|
|
185 #include <sys/stat.h>
|
|
186 #include <errno.h>
|
|
187
|
219
|
188 extern char *start_of_text (void); /* Start of text */
|
|
189 extern char *start_of_data (void); /* Start of initialized data */
|
0
|
190
|
|
191 extern int _data;
|
|
192 extern int _edata;
|
|
193 extern int _text;
|
|
194 extern int _etext;
|
|
195 extern int _end;
|
|
196 #ifdef COFF
|
|
197 #ifndef USG
|
|
198 #ifndef STRIDE
|
|
199 #ifndef UMAX
|
|
200 #ifndef sun386
|
|
201 /* I have a suspicion that these are turned off on all systems
|
|
202 and can be deleted. Try it in version 19. */
|
|
203 #include <filehdr.h>
|
|
204 #include <aouthdr.h>
|
|
205 #include <scnhdr.h>
|
|
206 #include <syms.h>
|
|
207 #endif /* not sun386 */
|
|
208 #endif /* not UMAX */
|
|
209 #endif /* Not STRIDE */
|
|
210 #endif /* not USG */
|
|
211 static struct filehdr f_hdr; /* File header */
|
|
212 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
|
|
213 long bias; /* Bias to add for growth */
|
|
214 long lnnoptr; /* Pointer to line-number info within file */
|
|
215
|
|
216 static long text_scnptr;
|
|
217 static long data_scnptr;
|
|
218 #ifdef XCOFF
|
|
219 #define ALIGN(val, pwr) (((val) + ((1L<<(pwr))-1)) & ~((1L<<(pwr))-1))
|
|
220 static long load_scnptr;
|
|
221 static long orig_load_scnptr;
|
|
222 static long orig_data_scnptr;
|
219
|
223 static unrelocate_symbols (int, int, char *, char *);
|
0
|
224 #endif
|
|
225 static ulong data_st; /* start of data area written out */
|
|
226
|
|
227 #ifndef MAX_SECTIONS
|
|
228 #define MAX_SECTIONS 10
|
|
229 #endif
|
|
230
|
219
|
231 static adjust_lnnoptrs (int, int, char *);
|
0
|
232 #endif /* COFF */
|
|
233
|
|
234 static int pagemask;
|
|
235
|
|
236 /* Correct an int which is the bit pattern of a pointer to a byte
|
|
237 into an int which is the number of a byte.
|
|
238 This is a no-op on ordinary machines, but not on all. */
|
|
239
|
|
240 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */
|
|
241 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
|
|
242 #endif
|
|
243
|
|
244 #ifdef emacs
|
|
245 #include "lisp.h"
|
|
246
|
|
247 static
|
219
|
248 report_error (char *file, int fd)
|
0
|
249 {
|
|
250 if (fd)
|
|
251 close (fd);
|
|
252 report_file_error ("Cannot unexec", Fcons (build_string (file), Qnil));
|
|
253 }
|
|
254 #endif /* emacs */
|
|
255
|
|
256 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
|
|
257 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
|
|
258 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
|
|
259
|
|
260 static
|
219
|
261 report_error_1 (int fd, char *msg, int a1, int a2)
|
0
|
262 {
|
|
263 close (fd);
|
|
264 #ifdef emacs
|
|
265 error (msg, a1, a2);
|
|
266 #else
|
|
267 fprintf (stderr, msg, a1, a2);
|
|
268 fprintf (stderr, "\n");
|
|
269 #endif
|
|
270 }
|
|
271
|
219
|
272 static int make_hdr (int, int, unsigned, unsigned, unsigned, char *, char *);
|
|
273 static void mark_x (char *);
|
|
274 static int copy_text_and_data (int);
|
|
275 static int copy_sym (int, int, char *, char *);
|
|
276 static write_segment (int, char *, char *);
|
0
|
277
|
|
278 /* ****************************************************************
|
|
279 * unexec
|
|
280 *
|
|
281 * driving logic.
|
|
282 */
|
219
|
283 int unexec (char *new_name, char *a_name,
|
|
284 uintptr_t data_start,
|
|
285 uintptr_t bss_start,
|
|
286 uintptr_t entry_address)
|
0
|
287 {
|
|
288 int new, a_out = -1;
|
|
289
|
|
290 if (a_name && (a_out = open (a_name, 0)) < 0)
|
|
291 {
|
|
292 PERROR (a_name);
|
|
293 }
|
|
294 if ((new = creat (new_name, 0666)) < 0)
|
|
295 {
|
|
296 PERROR (new_name);
|
|
297 }
|
|
298 if (make_hdr (new,a_out,data_start,bss_start,entry_address,a_name,new_name) < 0
|
|
299 || copy_text_and_data (new) < 0
|
|
300 || copy_sym (new, a_out, a_name, new_name) < 0
|
|
301 #ifdef COFF
|
|
302 || adjust_lnnoptrs (new, a_out, new_name) < 0
|
|
303 #endif
|
|
304 #ifdef XCOFF
|
|
305 || unrelocate_symbols (new, a_out, a_name, new_name) < 0
|
|
306 #endif
|
|
307 )
|
|
308 {
|
|
309 close (new);
|
207
|
310 /* unlink (new_name); */ /* Failed, unlink new a.out */
|
0
|
311 return -1;
|
|
312 }
|
|
313
|
|
314 close (new);
|
|
315 if (a_out >= 0)
|
|
316 close (a_out);
|
|
317 mark_x (new_name);
|
|
318 return 0;
|
|
319 }
|
|
320
|
|
321 /* ****************************************************************
|
|
322 * make_hdr
|
|
323 *
|
|
324 * Make the header in the new a.out from the header in core.
|
|
325 * Modify the text and data sizes.
|
|
326 */
|
|
327 static int
|
219
|
328 make_hdr (int new, int a_out, unsigned data_start, unsigned bss_start, unsigned entry_address, char *a_name, char *new_name)
|
0
|
329 {
|
|
330 int scns;
|
|
331 unsigned int bss_end;
|
|
332
|
|
333 struct scnhdr section[MAX_SECTIONS];
|
|
334 struct scnhdr * f_thdr; /* Text section header */
|
|
335 struct scnhdr * f_dhdr; /* Data section header */
|
|
336 struct scnhdr * f_bhdr; /* Bss section header */
|
|
337 struct scnhdr * f_lhdr; /* Loader section header */
|
|
338 struct scnhdr * f_tchdr; /* Typechk section header */
|
|
339 struct scnhdr * f_dbhdr; /* Debug section header */
|
|
340 struct scnhdr * f_xhdr; /* Except section header */
|
|
341
|
|
342 load_scnptr = orig_load_scnptr = lnnoptr = 0;
|
|
343 pagemask = getpagesize () - 1;
|
|
344
|
|
345 /* Adjust text/data boundary. */
|
|
346 #ifdef NO_REMAP
|
|
347 data_start = (long) start_of_data ();
|
|
348 #endif /* NO_REMAP */
|
|
349 data_start = ADDR_CORRECT (data_start);
|
|
350
|
|
351 #ifdef SEGMENT_MASK
|
|
352 data_start = data_start & ~SEGMENT_MASK; /* (Down) to segment boundary. */
|
|
353 #else
|
|
354 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
|
|
355 #endif
|
|
356
|
|
357
|
|
358 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
|
|
359 bss_end &= ~ pagemask;
|
|
360 /* Adjust data/bss boundary. */
|
|
361 if (bss_start != 0)
|
|
362 {
|
|
363 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
|
|
364 /* (Up) to page bdry. */
|
|
365 bss_start &= ~ pagemask;
|
|
366 if (bss_start > bss_end)
|
|
367 {
|
|
368 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
|
|
369 bss_start);
|
|
370 }
|
|
371 }
|
|
372 else
|
|
373 bss_start = bss_end;
|
|
374
|
|
375 if (data_start > bss_start) /* Can't have negative data size. */
|
|
376 {
|
|
377 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
|
|
378 data_start, bss_start);
|
|
379 }
|
|
380
|
|
381 #ifdef COFF
|
|
382 /* Salvage as much info from the existing file as possible */
|
|
383 f_thdr = NULL; f_dhdr = NULL; f_bhdr = NULL;
|
|
384 f_lhdr = NULL; f_tchdr = NULL; f_dbhdr = NULL; f_xhdr = NULL;
|
|
385 if (a_out >= 0)
|
|
386 {
|
|
387 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
|
|
388 {
|
|
389 PERROR (a_name);
|
|
390 }
|
|
391 if (f_hdr.f_opthdr > 0)
|
|
392 {
|
|
393 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
|
|
394 {
|
|
395 PERROR (a_name);
|
|
396 }
|
|
397 }
|
|
398 if (f_hdr.f_nscns > MAX_SECTIONS)
|
|
399 {
|
|
400 ERROR0 ("unexec: too many section headers -- increase MAX_SECTIONS");
|
|
401 }
|
|
402 /* Loop through section headers */
|
|
403 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
|
|
404 struct scnhdr *s = §ion[scns];
|
|
405 if (read (a_out, s, sizeof (*s)) != sizeof (*s))
|
|
406 {
|
|
407 PERROR (a_name);
|
|
408 }
|
|
409
|
|
410 #define CHECK_SCNHDR(ptr, name, flags) \
|
|
411 if (strcmp(s->s_name, name) == 0) { \
|
|
412 if (s->s_flags != flags) { \
|
|
413 fprintf(stderr, "unexec: %lx flags where %x expected in %s section.\n", \
|
|
414 (unsigned long)s->s_flags, flags, name); \
|
|
415 } \
|
|
416 if (ptr) { \
|
|
417 fprintf(stderr, "unexec: duplicate section header for section %s.\n", \
|
|
418 name); \
|
|
419 } \
|
|
420 ptr = s; \
|
|
421 }
|
|
422 CHECK_SCNHDR(f_thdr, _TEXT, STYP_TEXT);
|
|
423 CHECK_SCNHDR(f_dhdr, _DATA, STYP_DATA);
|
|
424 CHECK_SCNHDR(f_bhdr, _BSS, STYP_BSS);
|
|
425 CHECK_SCNHDR(f_lhdr, _LOADER, STYP_LOADER);
|
|
426 CHECK_SCNHDR(f_dbhdr, _DEBUG, STYP_DEBUG);
|
|
427 CHECK_SCNHDR(f_tchdr, _TYPCHK, STYP_TYPCHK);
|
|
428 CHECK_SCNHDR(f_xhdr, _EXCEPT, STYP_EXCEPT);
|
|
429 }
|
|
430
|
|
431 if (f_thdr == 0)
|
|
432 {
|
219
|
433 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _TEXT);
|
0
|
434 }
|
|
435 if (f_dhdr == 0)
|
|
436 {
|
219
|
437 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _DATA);
|
0
|
438 }
|
|
439 if (f_bhdr == 0)
|
|
440 {
|
219
|
441 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _BSS);
|
0
|
442 }
|
|
443 }
|
|
444 else
|
|
445 {
|
|
446 ERROR0 ("can't build a COFF file from scratch yet");
|
|
447 }
|
|
448 orig_data_scnptr = f_dhdr->s_scnptr;
|
|
449 orig_load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
|
|
450
|
|
451 /* Now we alter the contents of all the f_*hdr variables
|
|
452 to correspond to what we want to dump. */
|
|
453
|
|
454 /* Indicate that the reloc information is no longer valid for ld (bind);
|
|
455 we only update it enough to fake out the exec-time loader. */
|
|
456 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
|
|
457
|
|
458 #ifdef EXEC_MAGIC
|
|
459 f_ohdr.magic = EXEC_MAGIC;
|
|
460 #endif
|
|
461 #ifndef NO_REMAP
|
|
462 f_ohdr.tsize = data_start - f_ohdr.text_start;
|
|
463 f_ohdr.text_start = (long) start_of_text ();
|
|
464 #endif
|
|
465 data_st = f_ohdr.data_start ? f_ohdr.data_start : (ulong) &_data;
|
|
466 f_ohdr.dsize = bss_start - data_st;
|
|
467 f_ohdr.bsize = bss_end - bss_start;
|
|
468
|
|
469 f_dhdr->s_size = f_ohdr.dsize;
|
|
470 f_bhdr->s_size = f_ohdr.bsize;
|
|
471 f_bhdr->s_paddr = f_ohdr.data_start + f_ohdr.dsize;
|
|
472 f_bhdr->s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
|
|
473
|
|
474 /* fix scnptr's */
|
|
475 {
|
|
476 ulong ptr = section[0].s_scnptr;
|
|
477
|
|
478 bias = -1;
|
|
479 for (scns = 0; scns < f_hdr.f_nscns; scns++)
|
|
480 {
|
|
481 struct scnhdr *s = §ion[scns];
|
|
482
|
|
483 if (s->s_flags & STYP_PAD) /* .pad sections omitted in AIX 4.1 */
|
|
484 {
|
|
485 /*
|
|
486 * the text_start should probably be o_algntext but that doesn't
|
|
487 * seem to change
|
|
488 */
|
|
489 if (f_ohdr.text_start != 0) /* && scns != 0 */
|
|
490 {
|
|
491 s->s_size = 512 - (ptr % 512);
|
|
492 if (s->s_size == 512)
|
|
493 s->s_size = 0;
|
|
494 }
|
|
495 s->s_scnptr = ptr;
|
|
496 }
|
|
497 else if (s->s_flags & STYP_DATA)
|
|
498 s->s_scnptr = ptr;
|
|
499 else if (!(s->s_flags & (STYP_TEXT | STYP_BSS)))
|
|
500 {
|
|
501 if (bias == -1) /* if first section after bss */
|
|
502 bias = ptr - s->s_scnptr;
|
|
503
|
|
504 s->s_scnptr += bias;
|
|
505 ptr = s->s_scnptr;
|
|
506 }
|
|
507
|
|
508 ptr = ptr + s->s_size;
|
|
509 }
|
|
510 }
|
|
511
|
|
512 /* fix other pointers */
|
|
513 for (scns = 0; scns < f_hdr.f_nscns; scns++)
|
|
514 {
|
|
515 struct scnhdr *s = §ion[scns];
|
|
516
|
|
517 if (s->s_relptr != 0)
|
|
518 {
|
|
519 s->s_relptr += bias;
|
|
520 }
|
|
521 if (s->s_lnnoptr != 0)
|
|
522 {
|
|
523 if (lnnoptr == 0) lnnoptr = s->s_lnnoptr;
|
|
524 s->s_lnnoptr += bias;
|
|
525 }
|
|
526 }
|
|
527
|
|
528 if (f_hdr.f_symptr > 0L)
|
|
529 {
|
|
530 f_hdr.f_symptr += bias;
|
|
531 }
|
|
532
|
|
533 text_scnptr = f_thdr->s_scnptr;
|
|
534 data_scnptr = f_dhdr->s_scnptr;
|
|
535 load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
|
|
536
|
|
537 #ifdef ADJUST_EXEC_HEADER
|
|
538 ADJUST_EXEC_HEADER
|
|
539 #endif /* ADJUST_EXEC_HEADER */
|
|
540
|
|
541 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
|
|
542 {
|
|
543 PERROR (new_name);
|
|
544 }
|
|
545
|
|
546 if (f_hdr.f_opthdr > 0)
|
|
547 {
|
|
548 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
|
|
549 {
|
|
550 PERROR (new_name);
|
|
551 }
|
|
552 }
|
|
553
|
|
554 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
|
|
555 struct scnhdr *s = §ion[scns];
|
|
556 if (write (new, s, sizeof (*s)) != sizeof (*s))
|
|
557 {
|
|
558 PERROR (new_name);
|
|
559 }
|
|
560 }
|
|
561
|
|
562 return (0);
|
|
563
|
|
564 #endif /* COFF */
|
|
565 }
|
|
566
|
|
567 /* ****************************************************************
|
|
568
|
|
569 *
|
|
570 * Copy the text and data segments from memory to the new a.out
|
|
571 */
|
|
572 static int
|
219
|
573 copy_text_and_data (int new)
|
0
|
574 {
|
|
575 char *end;
|
|
576 char *ptr;
|
|
577
|
|
578 lseek (new, (long) text_scnptr, 0);
|
|
579 ptr = start_of_text () + text_scnptr;
|
|
580 end = ptr + f_ohdr.tsize;
|
|
581 write_segment (new, ptr, end);
|
|
582
|
|
583 lseek (new, (long) data_scnptr, 0);
|
|
584 ptr = (char *) data_st;
|
|
585 end = ptr + f_ohdr.dsize;
|
|
586 write_segment (new, ptr, end);
|
|
587
|
|
588 return 0;
|
|
589 }
|
|
590
|
|
591 #define UnexBlockSz (1<<12) /* read/write block size */
|
219
|
592 write_segment (int new, char *ptr, char *end)
|
0
|
593 {
|
|
594 int i, nwrite, ret;
|
|
595 char buf[80];
|
|
596 extern int errno;
|
|
597 char zeros[UnexBlockSz];
|
|
598
|
|
599 for (i = 0; ptr < end;)
|
|
600 {
|
|
601 /* distance to next block. */
|
|
602 nwrite = (((int) ptr + UnexBlockSz) & -UnexBlockSz) - (int) ptr;
|
|
603 /* But not beyond specified end. */
|
|
604 if (nwrite > end - ptr) nwrite = end - ptr;
|
|
605 ret = write (new, ptr, nwrite);
|
|
606 /* If write gets a page fault, it means we reached
|
|
607 a gap between the old text segment and the old data segment.
|
|
608 This gap has probably been remapped into part of the text segment.
|
|
609 So write zeros for it. */
|
|
610 if (ret == -1 && errno == EFAULT)
|
|
611 {
|
282
|
612 memset (zeros, 0, nwrite);
|
0
|
613 write (new, zeros, nwrite);
|
|
614 }
|
|
615 else if (nwrite != ret)
|
|
616 {
|
|
617 sprintf (buf,
|
|
618 "unexec write failure: addr 0x%lx, fileno %d, size 0x%x, wrote 0x%x, errno %d",
|
|
619 (unsigned long)ptr, new, nwrite, ret, errno);
|
|
620 PERROR (buf);
|
|
621 }
|
|
622 i += nwrite;
|
|
623 ptr += nwrite;
|
|
624 }
|
|
625 }
|
|
626
|
|
627 /* ****************************************************************
|
|
628 * copy_sym
|
|
629 *
|
|
630 * Copy the relocation information and symbol table from the a.out to the new
|
|
631 */
|
|
632 static int
|
219
|
633 copy_sym (int new, int a_out, char *a_name, char *new_name)
|
0
|
634 {
|
|
635 char page[UnexBlockSz];
|
|
636 int n;
|
|
637
|
|
638 if (a_out < 0)
|
|
639 return 0;
|
|
640
|
|
641 if (orig_load_scnptr == 0L)
|
|
642 return 0;
|
|
643
|
|
644 if (lnnoptr && lnnoptr < orig_load_scnptr) /* if there is line number info */
|
|
645 lseek (a_out, lnnoptr, 0); /* start copying from there */
|
|
646 else
|
|
647 lseek (a_out, orig_load_scnptr, 0); /* Position a.out to symtab. */
|
|
648
|
|
649 while ((n = read (a_out, page, sizeof page)) > 0)
|
|
650 {
|
|
651 if (write (new, page, n) != n)
|
|
652 {
|
|
653 PERROR (new_name);
|
|
654 }
|
|
655 }
|
|
656 if (n < 0)
|
|
657 {
|
|
658 PERROR (a_name);
|
|
659 }
|
|
660 return 0;
|
|
661 }
|
|
662
|
|
663 /* ****************************************************************
|
|
664 * mark_x
|
|
665 *
|
|
666 * After successfully building the new a.out, mark it executable
|
|
667 */
|
|
668 static void
|
219
|
669 mark_x (char *name)
|
0
|
670 {
|
|
671 struct stat sbuf;
|
|
672 int um;
|
|
673 int new = 0; /* for PERROR */
|
|
674
|
|
675 um = umask (777);
|
|
676 umask (um);
|
|
677 if (stat (name, &sbuf) == -1)
|
|
678 {
|
|
679 PERROR (name);
|
|
680 }
|
|
681 sbuf.st_mode |= 0111 & ~um;
|
|
682 if (chmod (name, sbuf.st_mode) == -1)
|
|
683 PERROR (name);
|
|
684 }
|
|
685
|
|
686 /*
|
|
687 * If the COFF file contains a symbol table and a line number section,
|
|
688 * then any auxiliary entries that have values for x_lnnoptr must
|
|
689 * be adjusted by the amount that the line number section has moved
|
|
690 * in the file (bias computed in make_hdr). The #@$%&* designers of
|
|
691 * the auxiliary entry structures used the absolute file offsets for
|
|
692 * the line number entry rather than an offset from the start of the
|
|
693 * line number section!
|
|
694 *
|
|
695 * When I figure out how to scan through the symbol table and pick out
|
|
696 * the auxiliary entries that need adjustment, this routine will
|
|
697 * be fixed. As it is now, all such entries are wrong and sdb
|
|
698 * will complain. Fred Fish, UniSoft Systems Inc.
|
|
699 *
|
|
700 * I believe this is now fixed correctly. Bill Mann
|
|
701 */
|
|
702
|
|
703 #ifdef COFF
|
|
704
|
|
705 /* This function is probably very slow. Instead of reopening the new
|
|
706 file for input and output it should copy from the old to the new
|
|
707 using the two descriptors already open (WRITEDESC and READDESC).
|
|
708 Instead of reading one small structure at a time it should use
|
|
709 a reasonable size buffer. But I don't have time to work on such
|
|
710 things, so I am installing it as submitted to me. -- RMS. */
|
|
711
|
219
|
712 adjust_lnnoptrs (int writedesc, int readdesc, char *new_name)
|
0
|
713 {
|
|
714 int nsyms;
|
|
715 int naux;
|
|
716 int new;
|
|
717 #ifdef amdahl_uts
|
|
718 SYMENT symentry;
|
|
719 AUXENT auxentry;
|
|
720 #else
|
|
721 struct syment symentry;
|
|
722 union auxent auxentry;
|
|
723 #endif
|
|
724
|
|
725 if (!lnnoptr || !f_hdr.f_symptr)
|
|
726 return 0;
|
|
727
|
|
728 if ((new = open (new_name, 2)) < 0)
|
|
729 {
|
|
730 PERROR (new_name);
|
|
731 return -1;
|
|
732 }
|
|
733
|
|
734 lseek (new, f_hdr.f_symptr, 0);
|
|
735 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
|
|
736 {
|
|
737 read (new, &symentry, SYMESZ);
|
|
738 if (symentry.n_sclass == C_BINCL || symentry.n_sclass == C_EINCL)
|
|
739 {
|
|
740 symentry.n_value += bias;
|
|
741 lseek (new, -SYMESZ, 1);
|
|
742 write (new, &symentry, SYMESZ);
|
|
743 }
|
|
744
|
|
745 for (naux = symentry.n_numaux; naux-- != 0; )
|
|
746 {
|
|
747 read (new, &auxentry, AUXESZ);
|
|
748 nsyms++;
|
|
749 if (naux != 0 /* skip csect auxentry (last entry) */
|
|
750 && (symentry.n_sclass == C_EXT || symentry.n_sclass == C_HIDEXT))
|
|
751 {
|
|
752 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
|
|
753 lseek (new, -AUXESZ, 1);
|
|
754 write (new, &auxentry, AUXESZ);
|
|
755 }
|
|
756 }
|
|
757 }
|
|
758 close (new);
|
|
759 }
|
|
760
|
|
761 #endif /* COFF */
|
|
762
|
|
763 #ifdef XCOFF
|
|
764
|
|
765 /* It is probably a false economy to optimise this routine (it used to
|
|
766 read one LDREL and do do two lseeks per iteration) but the wrath of
|
|
767 RMS (see above :-) would be too much to bear */
|
|
768
|
219
|
769 unrelocate_symbols (int new, int a_out, char *a_name, char *new_name)
|
0
|
770 {
|
|
771 int i;
|
|
772 int l;
|
|
773 LDREL *ldrel;
|
|
774 LDHDR ldhdr;
|
|
775 LDREL ldrel_buf [20];
|
|
776 ulong t_reloc = (ulong) &_text - f_ohdr.text_start;
|
|
777 ulong d_reloc = (ulong) &_data - ALIGN(f_ohdr.data_start, 2);
|
|
778 int * p;
|
|
779
|
|
780 if (load_scnptr == 0)
|
|
781 return 0;
|
|
782
|
|
783 lseek (a_out, orig_load_scnptr, 0);
|
|
784 if (read (a_out, &ldhdr, sizeof (ldhdr)) != sizeof (ldhdr))
|
|
785 {
|
|
786 PERROR (new_name);
|
|
787 }
|
|
788
|
|
789 #define SYMNDX_TEXT 0
|
|
790 #define SYMNDX_DATA 1
|
|
791 #define SYMNDX_BSS 2
|
|
792 l = 0;
|
|
793 for (i = 0; i < ldhdr.l_nreloc; i++, l--, ldrel++)
|
|
794 {
|
|
795 if (l == 0) {
|
|
796 lseek (a_out,
|
|
797 orig_load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
|
|
798 0);
|
|
799
|
|
800 l = ldhdr.l_nreloc - i;
|
|
801 if (l > sizeof (ldrel_buf) / LDRELSZ)
|
|
802 l = sizeof (ldrel_buf) / LDRELSZ;
|
|
803
|
|
804 if (read (a_out, ldrel_buf, l * LDRELSZ) != l * LDRELSZ)
|
|
805 {
|
|
806 PERROR (a_name);
|
|
807 }
|
|
808 ldrel = ldrel_buf;
|
|
809 }
|
|
810
|
|
811 /* move the BSS loader symbols to the DATA segment */
|
|
812 if (ldrel->l_symndx == SYMNDX_BSS)
|
|
813 {
|
|
814 ldrel->l_symndx = SYMNDX_DATA;
|
|
815
|
|
816 lseek (new,
|
|
817 load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
|
|
818 0);
|
|
819
|
|
820 if (write (new, ldrel, LDRELSZ) != LDRELSZ)
|
|
821 {
|
|
822 PERROR (new_name);
|
|
823 }
|
|
824 }
|
|
825
|
|
826 if (ldrel->l_rsecnm == f_ohdr.o_sndata)
|
|
827 {
|
|
828 int orig_int;
|
|
829
|
|
830 lseek (a_out,
|
|
831 orig_data_scnptr + (ldrel->l_vaddr - f_ohdr.data_start), 0);
|
|
832
|
|
833 if (read (a_out, (void *) &orig_int, sizeof (orig_int))
|
|
834 != sizeof (orig_int))
|
|
835 {
|
|
836 PERROR (a_name);
|
|
837 }
|
|
838
|
|
839 p = (int *) (ldrel->l_vaddr + d_reloc);
|
|
840
|
|
841 switch (ldrel->l_symndx) {
|
|
842 case SYMNDX_TEXT:
|
|
843 orig_int = * p - t_reloc;
|
|
844 break;
|
|
845
|
|
846 case SYMNDX_DATA:
|
|
847 case SYMNDX_BSS:
|
|
848 orig_int = * p - d_reloc;
|
|
849 break;
|
|
850 }
|
|
851
|
|
852 if (orig_int != * p)
|
|
853 {
|
|
854 lseek (new,
|
|
855 data_scnptr + (ldrel->l_vaddr - f_ohdr.data_start), 0);
|
|
856 if (write (new, (void *) &orig_int, sizeof (orig_int))
|
|
857 != sizeof (orig_int))
|
|
858 {
|
|
859 PERROR (new_name);
|
|
860 }
|
|
861 }
|
|
862 }
|
|
863 }
|
|
864 }
|
|
865 #endif /* XCOFF */
|