428
|
1 /* Dump an executable image.
|
|
2 Copyright (C) 1985, 1986, 1987, 1988 Free Software Foundation, Inc.
|
|
3
|
|
4 This file is part of XEmacs.
|
|
5
|
|
6 XEmacs is free software; you can redistribute it and/or modify it
|
|
7 under the terms of the GNU General Public License as published by the
|
|
8 Free Software Foundation; either version 2, or (at your option) any
|
|
9 later version.
|
|
10
|
|
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with XEmacs; see the file COPYING. If not, write to
|
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
|
20
|
|
21 /* Synched up with: FSF 20.2. */
|
|
22
|
771
|
23 /* This file has been ... uhhhhh ... Mule-ized. Yeah.
|
|
24
|
|
25 (Everything here is external format. That's potentially dangerous,
|
|
26 but in practice it'll be OK.) --ben */
|
|
27
|
428
|
28 /* Originally based on the COFF unexec.c by Spencer W. Thomas.
|
|
29 *
|
|
30 * Subsequently hacked on by
|
|
31 * Bill Mann <Bill_Man@praxisint.com>
|
|
32 * Andrew Vignaux <Andrew.Vignaux@comp.vuw.ac.nz>
|
|
33 * Mike Sperber <sperber@informatik.uni-tuebingen.de>
|
|
34 *
|
|
35 * Synopsis:
|
|
36 * unexec (new_name, a_name, data_start, bss_start, entry_address)
|
|
37 * char *new_name, *a_name;
|
|
38 * unsigned data_start, bss_start, entry_address;
|
|
39 *
|
|
40 * Takes a snapshot of the program and makes an a.out format file in the
|
|
41 * file named by the string argument new_name.
|
|
42 * If a_name is non-NULL, the symbol table will be taken from the given file.
|
|
43 * On some machines, an existing a_name file is required.
|
|
44 *
|
|
45 * data_start and entry_address are ignored.
|
|
46 *
|
|
47 * bss_start indicates how much of the data segment is to be saved in the
|
|
48 * a.out file and restored when the program is executed. It gives the lowest
|
|
49 * unsaved address, and is rounded up to a page boundary. The default when 0
|
|
50 * is given assumes that the entire data segment is to be stored, including
|
|
51 * the previous data and bss as well as any additional storage allocated with
|
|
52 * sbrk(2).
|
|
53 *
|
|
54 */
|
|
55
|
|
56 #ifndef emacs
|
|
57 #define PERROR(arg) perror (arg); return -1
|
|
58 #else
|
|
59 #include <config.h>
|
|
60 #define PERROR(file) report_error (file, new)
|
|
61 #endif
|
|
62
|
|
63 #include <a.out.h>
|
|
64 /* Define getpagesize () if the system does not.
|
|
65 Note that this may depend on symbols defined in a.out.h
|
|
66 */
|
|
67 #include "getpagesize.h"
|
|
68
|
|
69 #include <sys/types.h>
|
|
70 #include <stdio.h>
|
|
71 #include <sys/stat.h>
|
|
72 #include <errno.h>
|
|
73 #include <unistd.h>
|
|
74 #include <fcntl.h>
|
|
75
|
|
76 extern char *start_of_text (void); /* Start of text */
|
|
77 extern char *start_of_data (void); /* Start of initialized data */
|
|
78
|
|
79 extern int _data;
|
|
80 extern int _text;
|
|
81
|
|
82 #include <filehdr.h>
|
|
83 #include <aouthdr.h>
|
|
84 #include <scnhdr.h>
|
|
85 #include <syms.h>
|
|
86
|
|
87 static struct filehdr f_hdr; /* File header */
|
|
88 static struct aouthdr f_ohdr; /* Optional file header (a.out) */
|
|
89 static long bias; /* Bias to add for growth */
|
|
90 static long lnnoptr; /* Pointer to line-number info within file */
|
|
91
|
|
92 static long text_scnptr;
|
|
93 static long data_scnptr;
|
|
94 #define ALIGN(val, pwr) (((val) + ((1L<<(pwr))-1)) & ~((1L<<(pwr))-1))
|
|
95 static long load_scnptr;
|
|
96 static long orig_load_scnptr;
|
|
97 static long orig_data_scnptr;
|
|
98 static int unrelocate_symbols (int, int, char *, char *);
|
|
99
|
|
100 #ifndef MAX_SECTIONS
|
|
101 #define MAX_SECTIONS 10
|
|
102 #endif
|
|
103
|
|
104 static int adjust_lnnoptrs (int, int, char *);
|
|
105
|
|
106 static int pagemask;
|
|
107
|
|
108 /* Correct an int which is the bit pattern of a pointer to a byte
|
|
109 into an int which is the number of a byte.
|
|
110 This is a no-op on ordinary machines, but not on all. */
|
|
111
|
|
112 #ifndef ADDR_CORRECT /* Let m-*.h files override this definition */
|
|
113 #define ADDR_CORRECT(x) ((char *)(x) - (char*)0)
|
|
114 #endif
|
|
115
|
|
116 #ifdef emacs
|
|
117 #include "lisp.h"
|
|
118
|
|
119 static void
|
|
120 report_error (char *file, int fd)
|
|
121 {
|
|
122 if (fd)
|
|
123 close (fd);
|
563
|
124 report_error_with_errno (Qio_error, "Cannot unexec",
|
|
125 build_string (file));
|
428
|
126 }
|
|
127 #endif /* emacs */
|
|
128
|
|
129 #define ERROR0(msg) report_error_1 (new, msg, 0, 0); return -1
|
|
130 #define ERROR1(msg,x) report_error_1 (new, msg, x, 0); return -1
|
|
131 #define ERROR2(msg,x,y) report_error_1 (new, msg, x, y); return -1
|
|
132
|
|
133 static void
|
|
134 report_error_1 (int fd, char *msg, int a1, int a2)
|
|
135 {
|
|
136 close (fd);
|
|
137 #ifdef emacs
|
563
|
138 signal_ferror (Qio_error, msg, a1, a2);
|
428
|
139 #else
|
|
140 fprintf (stderr, msg, a1, a2);
|
|
141 fprintf (stderr, "\n");
|
|
142 #endif
|
|
143 }
|
|
144
|
|
145 static int make_hdr (int, int, unsigned, unsigned, unsigned, char *, char *);
|
|
146 static void mark_x (char *);
|
|
147 static int copy_text_and_data (int);
|
|
148 static int copy_sym (int, int, char *, char *);
|
|
149 static void write_segment (int, char *, char *);
|
|
150
|
|
151 /* ****************************************************************
|
|
152 * unexec
|
|
153 *
|
|
154 * driving logic.
|
|
155 */
|
|
156 int unexec (char *new_name, char *a_name,
|
|
157 uintptr_t data_start,
|
|
158 uintptr_t bss_start,
|
|
159 uintptr_t entry_address)
|
|
160 {
|
|
161 int new = -1, a_out = -1;
|
|
162
|
|
163 if (a_name && (a_out = open (a_name, O_RDONLY)) < 0)
|
|
164 {
|
|
165 PERROR (a_name);
|
|
166 }
|
|
167 if ((new = creat (new_name, 0666)) < 0)
|
|
168 {
|
|
169 PERROR (new_name);
|
|
170 }
|
|
171 if (make_hdr (new, a_out,
|
|
172 data_start, bss_start,
|
|
173 entry_address,
|
|
174 a_name, new_name) < 0
|
|
175 || copy_text_and_data (new) < 0
|
|
176 || copy_sym (new, a_out, a_name, new_name) < 0
|
|
177 || adjust_lnnoptrs (new, a_out, new_name) < 0
|
|
178 || unrelocate_symbols (new, a_out, a_name, new_name) < 0)
|
|
179 {
|
|
180 close (new);
|
|
181 return -1;
|
|
182 }
|
|
183
|
|
184 close (new);
|
|
185 if (a_out >= 0)
|
|
186 close (a_out);
|
|
187 mark_x (new_name);
|
|
188 return 0;
|
|
189 }
|
|
190
|
|
191 /* ****************************************************************
|
|
192 * make_hdr
|
|
193 *
|
|
194 * Make the header in the new a.out from the header in core.
|
|
195 * Modify the text and data sizes.
|
|
196 */
|
|
197 static int
|
|
198 make_hdr (int new, int a_out,
|
|
199 unsigned data_start, unsigned bss_start,
|
|
200 unsigned entry_address,
|
|
201 char *a_name, char *new_name)
|
|
202 {
|
|
203 int scns;
|
|
204 unsigned int bss_end;
|
|
205
|
|
206 struct scnhdr section[MAX_SECTIONS];
|
|
207 struct scnhdr * f_thdr; /* Text section header */
|
|
208 struct scnhdr * f_dhdr; /* Data section header */
|
|
209 struct scnhdr * f_bhdr; /* Bss section header */
|
|
210 struct scnhdr * f_lhdr; /* Loader section header */
|
|
211 struct scnhdr * f_tchdr; /* Typechk section header */
|
|
212 struct scnhdr * f_dbhdr; /* Debug section header */
|
|
213 struct scnhdr * f_xhdr; /* Except section header */
|
|
214
|
|
215 load_scnptr = orig_load_scnptr = lnnoptr = 0;
|
|
216 pagemask = getpagesize () - 1;
|
|
217
|
|
218 /* Adjust text/data boundary. */
|
|
219 data_start = (long) start_of_data ();
|
|
220 data_start = ADDR_CORRECT (data_start);
|
|
221
|
|
222 data_start = data_start & ~pagemask; /* (Down) to page boundary. */
|
|
223
|
|
224 bss_end = ADDR_CORRECT (sbrk (0)) + pagemask;
|
|
225 bss_end &= ~ pagemask;
|
|
226 /* Adjust data/bss boundary. */
|
|
227 if (bss_start != 0)
|
|
228 {
|
|
229 bss_start = (ADDR_CORRECT (bss_start) + pagemask);
|
|
230 /* (Up) to page bdry. */
|
|
231 bss_start &= ~ pagemask;
|
|
232 if (bss_start > bss_end)
|
|
233 {
|
|
234 ERROR1 ("unexec: Specified bss_start (%u) is past end of program",
|
|
235 bss_start);
|
|
236 }
|
|
237 }
|
|
238 else
|
|
239 bss_start = bss_end;
|
|
240
|
|
241 if (data_start > bss_start) /* Can't have negative data size. */
|
|
242 {
|
|
243 ERROR2 ("unexec: data_start (%u) can't be greater than bss_start (%u)",
|
|
244 data_start, bss_start);
|
|
245 }
|
|
246
|
|
247 /* Salvage as much info from the existing file as possible */
|
|
248 f_thdr = NULL; f_dhdr = NULL; f_bhdr = NULL;
|
|
249 f_lhdr = NULL; f_tchdr = NULL; f_dbhdr = NULL; f_xhdr = NULL;
|
|
250 if (a_out >= 0)
|
|
251 {
|
|
252 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
|
|
253 {
|
|
254 PERROR (a_name);
|
|
255 }
|
|
256 if (f_hdr.f_opthdr > 0)
|
|
257 {
|
|
258 if (read (a_out, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
|
|
259 {
|
|
260 PERROR (a_name);
|
|
261 }
|
|
262 }
|
|
263 if (f_hdr.f_nscns > MAX_SECTIONS)
|
|
264 {
|
|
265 ERROR0 ("unexec: too many section headers -- increase MAX_SECTIONS");
|
|
266 }
|
|
267 /* Loop through section headers */
|
|
268 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
|
|
269 struct scnhdr *s = §ion[scns];
|
|
270 if (read (a_out, s, sizeof (*s)) != sizeof (*s))
|
|
271 {
|
|
272 PERROR (a_name);
|
|
273 }
|
|
274
|
|
275 #define CHECK_SCNHDR(ptr, name, flags) \
|
|
276 if (strcmp(s->s_name, name) == 0) { \
|
|
277 if (s->s_flags != flags) { \
|
|
278 fprintf(stderr, "unexec: %lx flags where %x expected in %s section.\n", \
|
|
279 (unsigned long)s->s_flags, flags, name); \
|
|
280 } \
|
|
281 if (ptr) { \
|
|
282 fprintf(stderr, "unexec: duplicate section header for section %s.\n", \
|
|
283 name); \
|
|
284 } \
|
|
285 ptr = s; \
|
|
286 }
|
|
287 CHECK_SCNHDR(f_thdr, _TEXT, STYP_TEXT);
|
|
288 CHECK_SCNHDR(f_dhdr, _DATA, STYP_DATA);
|
|
289 CHECK_SCNHDR(f_bhdr, _BSS, STYP_BSS);
|
|
290 CHECK_SCNHDR(f_lhdr, _LOADER, STYP_LOADER);
|
|
291 CHECK_SCNHDR(f_dbhdr, _DEBUG, STYP_DEBUG);
|
|
292 CHECK_SCNHDR(f_tchdr, _TYPCHK, STYP_TYPCHK);
|
|
293 CHECK_SCNHDR(f_xhdr, _EXCEPT, STYP_EXCEPT);
|
|
294 }
|
|
295
|
|
296 if (f_thdr == 0)
|
|
297 {
|
|
298 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _TEXT);
|
|
299 }
|
|
300 if (f_dhdr == 0)
|
|
301 {
|
|
302 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _DATA);
|
|
303 }
|
|
304 if (f_bhdr == 0)
|
|
305 {
|
|
306 ERROR1 ("unexec: couldn't find \"%s\" section", (int) _BSS);
|
|
307 }
|
|
308 }
|
|
309 else
|
|
310 {
|
|
311 ERROR0 ("can't build a COFF file from scratch yet");
|
|
312 }
|
|
313 orig_data_scnptr = f_dhdr->s_scnptr;
|
|
314 orig_load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
|
|
315
|
|
316 /* Now we alter the contents of all the f_*hdr variables
|
|
317 to correspond to what we want to dump. */
|
|
318
|
|
319 /* Indicate that the reloc information is no longer valid for ld (bind);
|
|
320 we only update it enough to fake out the exec-time loader. */
|
|
321 f_hdr.f_flags |= (F_RELFLG | F_EXEC);
|
|
322
|
|
323 f_ohdr.dsize = bss_start - f_ohdr.data_start;
|
|
324 f_ohdr.bsize = bss_end - bss_start;
|
|
325
|
|
326 f_dhdr->s_size = f_ohdr.dsize;
|
|
327 f_bhdr->s_size = f_ohdr.bsize;
|
|
328 f_bhdr->s_paddr = f_ohdr.data_start + f_ohdr.dsize;
|
|
329 f_bhdr->s_vaddr = f_ohdr.data_start + f_ohdr.dsize;
|
|
330
|
|
331 /* fix scnptr's */
|
|
332 {
|
|
333 ulong ptr = section[0].s_scnptr;
|
|
334
|
|
335 bias = -1;
|
|
336 for (scns = 0; scns < f_hdr.f_nscns; scns++)
|
|
337 {
|
|
338 struct scnhdr *s = §ion[scns];
|
|
339
|
|
340 if (s->s_flags & STYP_PAD) /* .pad sections omitted in AIX 4.1 */
|
|
341 {
|
|
342 /*
|
|
343 * the text_start should probably be o_algntext but that doesn't
|
|
344 * seem to change
|
|
345 */
|
|
346 if (f_ohdr.text_start != 0) /* && scns != 0 */
|
|
347 {
|
|
348 s->s_size = 512 - (ptr % 512);
|
|
349 if (s->s_size == 512)
|
|
350 s->s_size = 0;
|
|
351 }
|
|
352 s->s_scnptr = ptr;
|
|
353 }
|
|
354 else if (s->s_flags & STYP_DATA)
|
|
355 s->s_scnptr = ptr;
|
|
356 else if (!(s->s_flags & (STYP_TEXT | STYP_BSS)))
|
|
357 {
|
|
358 if (bias == -1) /* if first section after bss */
|
|
359 bias = ptr - s->s_scnptr;
|
|
360
|
|
361 s->s_scnptr += bias;
|
|
362 ptr = s->s_scnptr;
|
|
363 }
|
|
364
|
|
365 ptr = ptr + s->s_size;
|
|
366 }
|
|
367 }
|
|
368
|
|
369 /* fix other pointers */
|
|
370 for (scns = 0; scns < f_hdr.f_nscns; scns++)
|
|
371 {
|
|
372 struct scnhdr *s = §ion[scns];
|
|
373
|
|
374 if (s->s_relptr != 0)
|
|
375 {
|
|
376 s->s_relptr += bias;
|
|
377 }
|
|
378 if (s->s_lnnoptr != 0)
|
|
379 {
|
|
380 if (lnnoptr == 0) lnnoptr = s->s_lnnoptr;
|
|
381 s->s_lnnoptr += bias;
|
|
382 }
|
|
383 }
|
|
384
|
|
385 if (f_hdr.f_symptr > 0L)
|
|
386 {
|
|
387 f_hdr.f_symptr += bias;
|
|
388 }
|
|
389
|
|
390 text_scnptr = f_thdr->s_scnptr;
|
|
391 data_scnptr = f_dhdr->s_scnptr;
|
|
392 load_scnptr = f_lhdr ? f_lhdr->s_scnptr : 0;
|
|
393
|
|
394 if (write (new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
|
|
395 {
|
|
396 PERROR (new_name);
|
|
397 }
|
|
398
|
|
399 if (f_hdr.f_opthdr > 0)
|
|
400 {
|
|
401 if (write (new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr))
|
|
402 {
|
|
403 PERROR (new_name);
|
|
404 }
|
|
405 }
|
|
406
|
|
407 for (scns = 0; scns < f_hdr.f_nscns; scns++) {
|
|
408 struct scnhdr *s = §ion[scns];
|
|
409 if (write (new, s, sizeof (*s)) != sizeof (*s))
|
|
410 {
|
|
411 PERROR (new_name);
|
|
412 }
|
|
413 }
|
|
414
|
|
415 return (0);
|
|
416 }
|
|
417
|
|
418 /* ****************************************************************
|
|
419
|
|
420 *
|
|
421 * Copy the text and data segments from memory to the new a.out
|
|
422 */
|
|
423 static int
|
|
424 copy_text_and_data (int new)
|
|
425 {
|
|
426 char *end;
|
|
427 char *ptr;
|
|
428
|
|
429 lseek (new, (long) text_scnptr, SEEK_SET);
|
|
430 ptr = start_of_text () + text_scnptr;
|
|
431 end = ptr + f_ohdr.tsize;
|
|
432 write_segment (new, ptr, end);
|
|
433
|
|
434 lseek (new, (long) data_scnptr, SEEK_SET);
|
|
435 ptr = (char *) f_ohdr.data_start;
|
|
436 end = ptr + f_ohdr.dsize;
|
|
437 write_segment (new, ptr, end);
|
|
438
|
|
439 return 0;
|
|
440 }
|
|
441
|
|
442 #define UnexBlockSz (1<<12) /* read/write block size */
|
|
443 static void
|
|
444 write_segment (int new, char *ptr, char *end)
|
|
445 {
|
|
446 int i, nwrite, ret;
|
|
447 char buf[80];
|
|
448 char zeros[UnexBlockSz];
|
|
449
|
|
450 for (i = 0; ptr < end;)
|
|
451 {
|
|
452 /* distance to next block. */
|
|
453 nwrite = (((int) ptr + UnexBlockSz) & -UnexBlockSz) - (int) ptr;
|
|
454 /* But not beyond specified end. */
|
|
455 if (nwrite > end - ptr) nwrite = end - ptr;
|
|
456 ret = write (new, ptr, nwrite);
|
|
457 /* If write gets a page fault, it means we reached
|
|
458 a gap between the old text segment and the old data segment.
|
|
459 This gap has probably been remapped into part of the text segment.
|
|
460 So write zeros for it. */
|
|
461 if (ret == -1 && errno == EFAULT)
|
|
462 {
|
|
463 memset (zeros, 0, nwrite);
|
|
464 write (new, zeros, nwrite);
|
|
465 }
|
|
466 else if (nwrite != ret)
|
|
467 {
|
|
468 sprintf (buf,
|
|
469 "unexec write failure: addr 0x%lx, fileno %d, size 0x%x, wrote 0x%x, errno %d",
|
|
470 (unsigned long)ptr, new, nwrite, ret, errno);
|
|
471 PERROR (buf);
|
|
472 }
|
|
473 i += nwrite;
|
|
474 ptr += nwrite;
|
|
475 }
|
|
476 }
|
|
477
|
|
478 /* ****************************************************************
|
|
479 * copy_sym
|
|
480 *
|
|
481 * Copy the relocation information and symbol table from the a.out to the new
|
|
482 */
|
|
483 static int
|
|
484 copy_sym (int new, int a_out, char *a_name, char *new_name)
|
|
485 {
|
|
486 char page[UnexBlockSz];
|
|
487 int n;
|
|
488
|
|
489 if (a_out < 0)
|
|
490 return 0;
|
|
491
|
|
492 if (orig_load_scnptr == 0L)
|
|
493 return 0;
|
|
494
|
|
495 if (lnnoptr && lnnoptr < orig_load_scnptr) /* if there is line number info */
|
|
496 lseek (a_out, lnnoptr, SEEK_SET); /* start copying from there */
|
|
497 else
|
|
498 lseek (a_out, orig_load_scnptr, SEEK_SET); /* Position a.out to symtab. */
|
|
499
|
647
|
500 while ((n = read (a_out, page, sizeof (page))) > 0)
|
428
|
501 {
|
|
502 if (write (new, page, n) != n)
|
|
503 {
|
|
504 PERROR (new_name);
|
|
505 }
|
|
506 }
|
|
507 if (n < 0)
|
|
508 {
|
|
509 PERROR (a_name);
|
|
510 }
|
|
511 return 0;
|
|
512 }
|
|
513
|
|
514 /* ****************************************************************
|
|
515 * mark_x
|
|
516 *
|
|
517 * After successfully building the new a.out, mark it executable
|
|
518 */
|
|
519 static void
|
|
520 mark_x (char *name)
|
|
521 {
|
|
522 struct stat sbuf;
|
|
523 int um;
|
|
524 int new = 0; /* for PERROR */
|
|
525
|
|
526 um = umask (777);
|
|
527 umask (um);
|
|
528 if (stat (name, &sbuf) == -1)
|
|
529 {
|
|
530 PERROR (name);
|
|
531 }
|
|
532 sbuf.st_mode |= 0111 & ~um;
|
|
533 if (chmod (name, sbuf.st_mode) == -1)
|
|
534 PERROR (name);
|
|
535 }
|
|
536
|
|
537 static int
|
|
538 adjust_lnnoptrs (int writedesc, int readdesc, char *new_name)
|
|
539 {
|
|
540 int nsyms;
|
|
541 int naux;
|
|
542 int new;
|
|
543 struct syment symentry;
|
|
544 union auxent auxentry;
|
|
545
|
|
546 if (!lnnoptr || !f_hdr.f_symptr)
|
|
547 return 0;
|
|
548
|
|
549 if ((new = open (new_name, O_RDWR)) < 0)
|
|
550 {
|
|
551 PERROR (new_name);
|
|
552 return -1;
|
|
553 }
|
|
554
|
|
555 lseek (new, f_hdr.f_symptr, SEEK_SET);
|
|
556 for (nsyms = 0; nsyms < f_hdr.f_nsyms; nsyms++)
|
|
557 {
|
|
558 read (new, &symentry, SYMESZ);
|
|
559 if (symentry.n_sclass == C_BINCL || symentry.n_sclass == C_EINCL)
|
|
560 {
|
|
561 symentry.n_value += bias;
|
|
562 lseek (new, -SYMESZ, SEEK_CUR);
|
|
563 write (new, &symentry, SYMESZ);
|
|
564 }
|
|
565
|
|
566 for (naux = symentry.n_numaux; naux-- != 0; )
|
|
567 {
|
|
568 read (new, &auxentry, AUXESZ);
|
|
569 nsyms++;
|
|
570 if (naux != 0 /* skip csect auxentry (last entry) */
|
|
571 && (symentry.n_sclass == C_EXT || symentry.n_sclass == C_HIDEXT))
|
|
572 {
|
|
573 auxentry.x_sym.x_fcnary.x_fcn.x_lnnoptr += bias;
|
|
574 lseek (new, -AUXESZ, SEEK_CUR);
|
|
575 write (new, &auxentry, AUXESZ);
|
|
576 }
|
|
577 }
|
|
578 }
|
|
579 close (new);
|
|
580
|
|
581 return 0;
|
|
582 }
|
|
583
|
|
584 static int
|
|
585 unrelocate_symbols (int new, int a_out, char *a_name, char *new_name)
|
|
586 {
|
|
587 int i;
|
|
588 LDHDR ldhdr;
|
|
589 LDREL ldrel;
|
|
590 ulong t_reloc = (ulong) &_text - f_ohdr.text_start;
|
|
591 #ifndef ALIGN_DATA_RELOC
|
|
592 ulong d_reloc = (ulong) &_data - f_ohdr.data_start;
|
|
593 #else
|
|
594 /* This worked (and was needed) before AIX 4.2.
|
|
595 I have no idea why. -- Mike */
|
|
596 ulong d_reloc = (ulong) &_data - ALIGN(f_ohdr.data_start, 2);
|
|
597 #endif
|
|
598 int * p;
|
|
599
|
|
600 if (load_scnptr == 0)
|
|
601 return 0;
|
|
602
|
|
603 lseek (a_out, orig_load_scnptr, SEEK_SET);
|
|
604 if (read (a_out, &ldhdr, sizeof (ldhdr)) != sizeof (ldhdr))
|
|
605 {
|
|
606 PERROR (new_name);
|
|
607 }
|
|
608
|
|
609 #define SYMNDX_TEXT 0
|
|
610 #define SYMNDX_DATA 1
|
|
611 #define SYMNDX_BSS 2
|
|
612
|
|
613 for (i = 0; i < ldhdr.l_nreloc; i++)
|
|
614 {
|
|
615 lseek (a_out,
|
|
616 orig_load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
|
|
617 SEEK_SET);
|
|
618
|
|
619 if (read (a_out, &ldrel, LDRELSZ) != LDRELSZ)
|
|
620 {
|
|
621 PERROR (a_name);
|
|
622 }
|
|
623
|
|
624 /* move the BSS loader symbols to the DATA segment */
|
|
625 if (ldrel.l_symndx == SYMNDX_BSS)
|
|
626 {
|
|
627 ldrel.l_symndx = SYMNDX_DATA;
|
|
628
|
|
629 lseek (new,
|
|
630 load_scnptr + LDHDRSZ + LDSYMSZ*ldhdr.l_nsyms + LDRELSZ*i,
|
|
631 SEEK_SET);
|
|
632
|
|
633 if (write (new, &ldrel, LDRELSZ) != LDRELSZ)
|
|
634 {
|
|
635 PERROR (new_name);
|
|
636 }
|
|
637 }
|
|
638
|
|
639 if (ldrel.l_rsecnm == f_ohdr.o_sndata)
|
|
640 {
|
|
641 int orig_int;
|
|
642
|
|
643 lseek (a_out,
|
|
644 orig_data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
|
|
645 SEEK_SET);
|
|
646
|
|
647 if (read (a_out, (void *) &orig_int, sizeof (orig_int))
|
|
648 != sizeof (orig_int))
|
|
649 {
|
|
650 PERROR (a_name);
|
|
651 }
|
|
652
|
|
653 p = (int *) (ldrel.l_vaddr + d_reloc);
|
|
654
|
|
655 switch (ldrel.l_symndx) {
|
|
656 case SYMNDX_TEXT:
|
|
657 orig_int = * p - t_reloc;
|
|
658 break;
|
|
659
|
|
660 case SYMNDX_DATA:
|
|
661 case SYMNDX_BSS:
|
|
662 orig_int = * p - d_reloc;
|
|
663 break;
|
|
664 }
|
|
665
|
|
666 if (orig_int != * p)
|
|
667 {
|
|
668 lseek (new,
|
|
669 data_scnptr + (ldrel.l_vaddr - f_ohdr.data_start),
|
|
670 SEEK_SET);
|
|
671 if (write (new, (void *) &orig_int, sizeof (orig_int))
|
|
672 != sizeof (orig_int))
|
|
673 {
|
|
674 PERROR (new_name);
|
|
675 }
|
|
676 }
|
|
677 }
|
|
678 }
|
|
679 return 0;
|
|
680 }
|