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