Mercurial > hg > xemacs-beta
comparison src/unexcw.c @ 647:b39c14581166
[xemacs-hg @ 2001-08-13 04:45:47 by ben]
removal of unsigned, size_t, etc.
author | ben |
---|---|
date | Mon, 13 Aug 2001 04:46:48 +0000 |
parents | 023b83f4e54b |
children | 943eaba38521 |
comparison
equal
deleted
inserted
replaced
646:00c54252fe4f | 647:b39c14581166 |
---|---|
36 } while (0) | 36 } while (0) |
37 | 37 |
38 #if !defined (HAVE_A_OUT_H) && !defined (WIN32_NATIVE) | 38 #if !defined (HAVE_A_OUT_H) && !defined (WIN32_NATIVE) |
39 unexec (char *, char *, void *, void *, void *) | 39 unexec (char *, char *, void *, void *, void *) |
40 { | 40 { |
41 PERROR("cannot unexec() a.out.h not installed"); | 41 PERROR ("cannot unexec() a.out.h not installed"); |
42 } | 42 } |
43 #else | 43 #else |
44 | 44 |
45 #ifdef MINGW | 45 #ifdef MINGW |
46 #include <../../include/a.out.h> | 46 #include <../../include/a.out.h> |
47 #else | 47 #else |
48 #include <a.out.h> | 48 #include <a.out.h> |
49 #endif | 49 #endif |
50 | 50 |
51 #define ALLOC_UNIT 0xFFFF | 51 #define ALLOC_UNIT 0xFFFF |
52 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT)) | 52 #define ALLOC_MASK ~((unsigned long) (ALLOC_UNIT)) |
53 #define ALIGN_ALLOC(addr) \ | 53 #define ALIGN_ALLOC(addr) \ |
54 ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK) | 54 ((((unsigned long) addr) + ALLOC_UNIT) & ALLOC_MASK) |
55 /* Note that all sections must be aligned on a 0x1000 boundary so | 55 /* Note that all sections must be aligned on a 0x1000 boundary so |
56 this is the minimum size that our dummy bss can be. */ | 56 this is the minimum size that our dummy bss can be. */ |
57 #ifndef NO_DEBUG | 57 #ifndef NO_DEBUG |
58 #define BSS_PAD_SIZE 0x1000 | 58 #define BSS_PAD_SIZE 0x1000 |
59 #else | 59 #else |
65 #define UNINIT_PTR ((void *) 0xF0A0F0A0) | 65 #define UNINIT_PTR ((void *) 0xF0A0F0A0) |
66 #define UNINIT_LONG (0xF0A0F0A0L) | 66 #define UNINIT_LONG (0xF0A0F0A0L) |
67 | 67 |
68 static void get_section_info (int a_out, char* a_name); | 68 static void get_section_info (int a_out, char* a_name); |
69 static void copy_executable_and_dump_data_section (int a_out, int a_new); | 69 static void copy_executable_and_dump_data_section (int a_out, int a_new); |
70 static void dup_file_area(int a_out, int a_new, long size); | 70 static void dup_file_area (int a_out, int a_new, long size); |
71 #if 0 | 71 #if 0 |
72 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval); | 72 static void write_int_to_bss (int a_out, int a_new, void* va, void* newval); |
73 #endif | 73 #endif |
74 | 74 |
75 /* Cached info about the .data section in the executable. */ | 75 /* Cached info about the .data section in the executable. */ |
76 void* data_start_va = UNINIT_PTR; | 76 void *data_start_va = UNINIT_PTR; |
77 unsigned long data_size = UNINIT_LONG; | 77 long data_size = UNINIT_LONG; |
78 | 78 |
79 /* Cached info about the .bss section in the executable. */ | 79 /* Cached info about the .bss section in the executable. */ |
80 void* bss_start = UNINIT_PTR; | 80 void *bss_start = UNINIT_PTR; |
81 unsigned long bss_size = UNINIT_LONG; | 81 long bss_size = UNINIT_LONG; |
82 int sections_reversed = 0; | 82 int sections_reversed = 0; |
83 FILHDR f_hdr; | 83 FILHDR f_hdr; |
84 PEAOUTHDR f_ohdr; | 84 PEAOUTHDR f_ohdr; |
85 SCNHDR f_data, f_bss, f_text, f_nextdata; | 85 SCNHDR f_data, f_bss, f_text, f_nextdata; |
86 | 86 |
87 #define CHECK_AOUT_POS(a) \ | 87 #define CHECK_AOUT_POS(a) \ |
88 if (lseek(a_out, 0, SEEK_CUR) != a) \ | 88 do { \ |
89 { \ | 89 if (lseek (a_out, 0, SEEK_CUR) != a) \ |
90 printf("we are at %lx, should be at %lx\n", \ | 90 { \ |
91 lseek(a_out, 0, SEEK_CUR), a); \ | 91 printf ("we are at %lx, should be at %lx\n", \ |
92 exit(-1); \ | 92 lseek (a_out, 0, SEEK_CUR), a); \ |
93 } | 93 exit (-1); \ |
94 } \ | |
95 } while (0) | |
94 | 96 |
95 /* Dump out .data and .bss sections into a new executable. */ | 97 /* Dump out .data and .bss sections into a new executable. */ |
96 int | 98 int |
97 unexec (char *out_name, char *in_name, uintptr_t start_data, | 99 unexec (char *out_name, char *in_name, uintptr_t start_data, |
98 uintptr_t d1, uintptr_t d2) | 100 uintptr_t d1, uintptr_t d2) |
127 /* Get the interesting section info, like start and size of .bss... */ | 129 /* Get the interesting section info, like start and size of .bss... */ |
128 get_section_info (a_out, a_name); | 130 get_section_info (a_out, a_name); |
129 | 131 |
130 copy_executable_and_dump_data_section (a_out, a_new); | 132 copy_executable_and_dump_data_section (a_out, a_new); |
131 | 133 |
132 close(a_out); | 134 close (a_out); |
133 close(a_new); | 135 close (a_new); |
134 return 0; | 136 return 0; |
135 } | 137 } |
136 | 138 |
137 /* Flip through the executable and cache the info necessary for dumping. */ | 139 /* Flip through the executable and cache the info necessary for dumping. */ |
138 static void | 140 static void |
144 | 146 |
145 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | 147 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) |
146 PERROR (a_name); | 148 PERROR (a_name); |
147 | 149 |
148 if (f_hdr.e_magic != DOSMAGIC) | 150 if (f_hdr.e_magic != DOSMAGIC) |
149 PERROR("unknown exe header"); | 151 PERROR ("unknown exe header"); |
150 | 152 |
151 /* Check the NT header signature ... */ | 153 /* Check the NT header signature ... */ |
152 if (f_hdr.nt_signature != NT_SIGNATURE) | 154 if (f_hdr.nt_signature != NT_SIGNATURE) |
153 PERROR("invalid nt header"); | 155 PERROR ("invalid nt header"); |
154 | 156 |
155 /* Flip through the sections for .data and .bss ... */ | 157 /* Flip through the sections for .data and .bss ... */ |
156 if (f_hdr.f_opthdr > 0) | 158 if (f_hdr.f_opthdr > 0) |
157 { | 159 { |
158 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ) | 160 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ) |
170 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) | 172 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) |
171 || (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data"))) | 173 || (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data"))) |
172 PERROR ("no .bss / .data section"); | 174 PERROR ("no .bss / .data section"); |
173 | 175 |
174 /* check for reversed .bss and .data */ | 176 /* check for reversed .bss and .data */ |
175 if (!strcmp(f_bss.s_name, ".data")) | 177 if (!strcmp (f_bss.s_name, ".data")) |
176 { | 178 { |
177 printf(".data and .bss reversed\n"); | 179 printf (".data and .bss reversed\n"); |
178 sections_reversed = 1; | 180 sections_reversed = 1; |
179 memcpy(&f_data, &f_bss, sizeof(f_bss)); | 181 memcpy (&f_data, &f_bss, sizeof (f_bss)); |
180 } | 182 } |
181 | 183 |
182 /* The .data section. */ | 184 /* The .data section. */ |
183 if (!sections_reversed) | 185 if (!sections_reversed) |
184 { | 186 { |
195 | 197 |
196 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr); | 198 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr); |
197 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start); | 199 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start); |
198 | 200 |
199 /* must keep bss data that we want to be blank as blank */ | 201 /* must keep bss data that we want to be blank as blank */ |
200 printf("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize); | 202 printf ("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize); |
201 | 203 |
202 /* The .data section. */ | 204 /* The .data section. */ |
203 data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr); | 205 data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr); |
204 | 206 |
205 /* We want to only write Emacs data back to the executable, | 207 /* We want to only write Emacs data back to the executable, |
206 not any of the library data (if library data is included, | 208 not any of the library data (if library data is included, |
207 then a dumped Emacs won't run on system versions other | 209 then a dumped Emacs won't run on system versions other |
208 than the one Emacs was dumped on). */ | 210 than the one Emacs was dumped on). */ |
209 data_size = (unsigned long)my_edata - (unsigned long)data_start_va; | 211 data_size = (unsigned long)my_edata - (unsigned long)data_start_va; |
210 printf("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize); | 212 printf ("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize); |
211 | 213 |
212 /* The following data section - often .idata */ | 214 /* The following data section - often .idata */ |
213 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata) | 215 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata) |
214 && | 216 && strcmp (&f_nextdata.s_name[2], "data")) |
215 strcmp (&f_nextdata.s_name[2], "data")) | |
216 PERROR ("no other data section"); | 217 PERROR ("no other data section"); |
217 } | 218 } |
218 | 219 |
219 /* The dump routines. */ | 220 /* The dump routines. */ |
220 | 221 |
221 static void | 222 static void |
222 copy_executable_and_dump_data_section (int a_out, int a_new) | 223 copy_executable_and_dump_data_section (int a_out, int a_new) |
223 { | 224 { |
224 long size=0; | 225 long size = 0; |
225 unsigned long new_data_size, new_bss_size, | 226 /* NOTE: Some of these were previously declared as unsigned long, |
226 bss_padding, file_sz_change, data_padding=0, | 227 but the ones changed to long represent file sizes or pointers, |
227 f_data_s_vaddr = f_data.s_vaddr, | 228 which can't reasonably get above 2G. (A 2G executable???) |
228 f_data_s_scnptr = f_data.s_scnptr, | 229 Furthermore, some were even being compared as in if (x < 0) ... */ |
229 f_bss_s_vaddr = f_bss.s_vaddr, | 230 long new_data_size, new_bss_size, bss_padding, file_sz_change; |
230 f_nextdata_s_scnptr = f_nextdata.s_scnptr; | 231 long data_padding = 0; |
232 long f_data_s_scnptr = f_data.s_scnptr; | |
233 long f_nextdata_s_scnptr = f_nextdata.s_scnptr; | |
234 unsigned long f_data_s_vaddr = f_data.s_vaddr; | |
235 unsigned long f_bss_s_vaddr = f_bss.s_vaddr; | |
231 | 236 |
232 int i; | 237 int i; |
233 void* empty_space; | 238 void* empty_space; |
234 extern int static_heap_dumped; | 239 extern int static_heap_dumped; |
235 SCNHDR section; | 240 SCNHDR section; |
266 } | 271 } |
267 | 272 |
268 if ((new_bss_size - bss_size) < BSS_PAD_SIZE) | 273 if ((new_bss_size - bss_size) < BSS_PAD_SIZE) |
269 PERROR (".bss free space too small"); | 274 PERROR (".bss free space too small"); |
270 | 275 |
271 file_sz_change=(new_bss_size + data_padding) - BSS_PAD_SIZE; | 276 file_sz_change = (new_bss_size + data_padding) - BSS_PAD_SIZE; |
272 new_data_size=f_ohdr.dsize + file_sz_change; | 277 new_data_size = f_ohdr.dsize + file_sz_change; |
273 | 278 |
274 if (!sections_reversed) | 279 if (!sections_reversed) |
275 f_data.s_vaddr = f_bss.s_vaddr; | 280 f_data.s_vaddr = f_bss.s_vaddr; |
276 f_data.s_paddr += file_sz_change; | 281 f_data.s_paddr += file_sz_change; |
277 #if 0 | 282 #if 0 |
278 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize) | 283 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize) |
279 printf("section size doesn't tally with dsize %lx != %lx\n", | 284 printf ("section size doesn't tally with dsize %lx != %lx\n", |
280 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize); | 285 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize); |
281 #endif | 286 #endif |
282 f_data.s_size += file_sz_change; | 287 f_data.s_size += file_sz_change; |
283 lseek (a_new, 0, SEEK_SET); | 288 lseek (a_new, 0, SEEK_SET); |
284 /* write file header */ | 289 /* write file header */ |
285 f_hdr.f_symptr += file_sz_change; | 290 f_hdr.f_symptr += file_sz_change; |
286 #ifdef NO_DEBUG | 291 #ifdef NO_DEBUG |
287 f_hdr.f_nscns--; | 292 f_hdr.f_nscns--; |
288 #endif | 293 #endif |
289 | 294 |
290 printf("writing file header\n"); | 295 printf ("writing file header\n"); |
291 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr)) | 296 if (write (a_new, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) |
292 PERROR("failed to write file header"); | 297 PERROR ("failed to write file header"); |
293 /* write optional header fixing dsize & bsize*/ | 298 /* write optional header fixing dsize & bsize*/ |
294 printf("writing optional header\n"); | 299 printf ("writing optional header\n"); |
295 printf("new data size is %lx, >= %lx\n", new_data_size, | 300 printf ("new data size is %lx, >= %lx\n", new_data_size, |
296 f_ohdr.dsize + f_ohdr.bsize); | 301 f_ohdr.dsize + f_ohdr.bsize); |
297 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize ) | 302 if (new_data_size < (long) (f_ohdr.dsize + f_ohdr.bsize)) |
298 printf("warning: new data size is < approx\n"); | 303 printf ("warning: new data size is < approx\n"); |
299 f_ohdr.dsize=new_data_size; | 304 f_ohdr.dsize=new_data_size; |
300 f_ohdr.bsize=BSS_PAD_SIZE; | 305 f_ohdr.bsize=BSS_PAD_SIZE; |
301 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr)) | 306 if (write (a_new, &f_ohdr, sizeof (f_ohdr)) != sizeof (f_ohdr)) |
302 PERROR("failed to write optional header"); | 307 PERROR ("failed to write optional header"); |
303 /* write text as is */ | 308 /* write text as is */ |
304 printf("writing text header (unchanged)\n"); | 309 printf ("writing text header (unchanged)\n"); |
305 | 310 |
306 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text)) | 311 if (write (a_new, &f_text, sizeof (f_text)) != sizeof (f_text)) |
307 PERROR("failed to write text header"); | 312 PERROR ("failed to write text header"); |
308 #ifndef NO_DEBUG | 313 #ifndef NO_DEBUG |
309 /* Write small bss section. */ | 314 /* Write small bss section. */ |
310 if (!sections_reversed) | 315 if (!sections_reversed) |
311 { | 316 { |
312 f_bss.s_size = BSS_PAD_SIZE; | 317 f_bss.s_size = BSS_PAD_SIZE; |
313 f_bss.s_paddr = BSS_PAD_SIZE; | 318 f_bss.s_paddr = BSS_PAD_SIZE; |
314 f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE; | 319 f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE; |
315 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 320 if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss)) |
316 PERROR("failed to write bss header"); | 321 PERROR ("failed to write bss header"); |
317 } | 322 } |
318 #endif | 323 #endif |
319 /* write new data header */ | 324 /* write new data header */ |
320 printf("writing .data header\n"); | 325 printf ("writing .data header\n"); |
321 | 326 |
322 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data)) | 327 if (write (a_new, &f_data, sizeof (f_data)) != sizeof (f_data)) |
323 PERROR("failed to write data header"); | 328 PERROR ("failed to write data header"); |
324 #ifndef NO_DEBUG | 329 #ifndef NO_DEBUG |
325 /* Write small bss section. */ | 330 /* Write small bss section. */ |
326 if (sections_reversed) | 331 if (sections_reversed) |
327 { | 332 { |
328 f_bss.s_size = BSS_PAD_SIZE; | 333 f_bss.s_size = BSS_PAD_SIZE; |
329 f_bss.s_paddr = BSS_PAD_SIZE; | 334 f_bss.s_paddr = BSS_PAD_SIZE; |
330 f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE; | 335 f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE; |
331 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 336 if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss)) |
332 PERROR("failed to write bss header"); | 337 PERROR ("failed to write bss header"); |
333 } | 338 } |
334 #endif | 339 #endif |
335 printf("writing following data header\n"); | 340 printf ("writing following data header\n"); |
336 f_nextdata.s_scnptr += file_sz_change; | 341 f_nextdata.s_scnptr += file_sz_change; |
337 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change; | 342 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change; |
338 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change; | 343 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change; |
339 if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata)) | 344 if (write (a_new, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)) |
340 PERROR("failed to write nextdata header"); | 345 PERROR ("failed to write nextdata header"); |
341 | 346 |
342 /* copy other section headers adjusting the file offset */ | 347 /* copy other section headers adjusting the file offset */ |
343 for (i=0; i<(f_hdr.f_nscns-3); i++) | 348 for (i=0; i<(f_hdr.f_nscns-3); i++) |
344 { | 349 { |
345 if (read (a_out, §ion, sizeof (section)) != sizeof (section)) | 350 if (read (a_out, §ion, sizeof (section)) != sizeof (section)) |
347 | 352 |
348 section.s_scnptr += file_sz_change; | 353 section.s_scnptr += file_sz_change; |
349 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change; | 354 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change; |
350 if (section.s_relptr != 0) section.s_relptr += file_sz_change; | 355 if (section.s_relptr != 0) section.s_relptr += file_sz_change; |
351 | 356 |
352 if (write(a_new, §ion, sizeof(section)) != sizeof(section)) | 357 if (write (a_new, §ion, sizeof (section)) != sizeof (section)) |
353 PERROR("failed to write data header"); | 358 PERROR ("failed to write data header"); |
354 } | 359 } |
355 #ifdef NO_DEBUG | 360 #ifdef NO_DEBUG |
356 /* dump bss to maintain offsets */ | 361 /* dump bss to maintain offsets */ |
357 memset(&f_bss, 0, sizeof(f_bss)); | 362 memset (&f_bss, 0, sizeof (f_bss)); |
358 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 363 if (write (a_new, &f_bss, sizeof (f_bss)) != sizeof (f_bss)) |
359 PERROR("failed to write bss header"); | 364 PERROR ("failed to write bss header"); |
360 #endif | 365 #endif |
361 size=lseek(a_new, 0, SEEK_CUR); | 366 size = lseek (a_new, 0, SEEK_CUR); |
362 CHECK_AOUT_POS(size); | 367 CHECK_AOUT_POS (size); |
363 | 368 |
364 /* copy eveything else until start of data */ | 369 /* copy eveything else until start of data */ |
365 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR); | 370 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR); |
366 | 371 |
367 printf ("copying executable up to data section ... %lx bytes\n", | 372 printf ("copying executable up to data section ... %lx bytes\n", |
368 size); | 373 size); |
369 dup_file_area(a_out, a_new, size); | 374 dup_file_area (a_out, a_new, size); |
370 | 375 |
371 CHECK_AOUT_POS(f_data_s_scnptr); | 376 CHECK_AOUT_POS (f_data_s_scnptr); |
372 | 377 |
373 if (!sections_reversed) | 378 if (!sections_reversed) |
374 { | 379 { |
375 /* dump bss + padding between sections, sans small bss pad */ | 380 /* dump bss + padding between sections, sans small bss pad */ |
376 printf ("dumping .bss into executable... %lx bytes\n", bss_size); | 381 printf ("dumping .bss into executable... %lx bytes\n", bss_size); |
377 if (write(a_new, bss_start, bss_size) != (int)bss_size) | 382 if (write (a_new, bss_start, bss_size) != bss_size) |
378 { | 383 { |
379 PERROR("failed to write bss section"); | 384 PERROR ("failed to write bss section"); |
380 } | 385 } |
381 | 386 |
382 /* pad, needs to be zero */ | 387 /* pad, needs to be zero */ |
383 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; | 388 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; |
384 if (bss_padding < 0) | 389 if (bss_padding < 0) |
385 PERROR("padded .bss too small"); | 390 PERROR ("padded .bss too small"); |
386 printf ("padding .bss ... %lx bytes\n", bss_padding); | 391 printf ("padding .bss ... %lx bytes\n", bss_padding); |
387 empty_space = malloc(bss_padding); | 392 empty_space = malloc (bss_padding); |
388 memset(empty_space, 0, bss_padding); | 393 memset (empty_space, 0, bss_padding); |
389 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) | 394 if (write (a_new, empty_space, bss_padding) != bss_padding) |
390 PERROR("failed to write bss section"); | 395 PERROR ("failed to write bss section"); |
391 free(empty_space); | 396 free (empty_space); |
392 } | 397 } |
393 | 398 |
394 /* tell dumped version not to free pure heap */ | 399 /* tell dumped version not to free pure heap */ |
395 static_heap_dumped = 1; | 400 static_heap_dumped = 1; |
396 /* Get a pointer to the raw data in our address space. */ | 401 /* Get a pointer to the raw data in our address space. */ |
397 printf ("dumping .data section... %lx bytes\n", data_size); | 402 printf ("dumping .data section... %lx bytes\n", data_size); |
398 if (write(a_new, data_start_va, data_size) != (int)data_size) | 403 if (write (a_new, data_start_va, data_size) != data_size) |
399 PERROR("failed to write data section"); | 404 PERROR ("failed to write data section"); |
400 /* were going to use free again ... */ | 405 /* were going to use free again ... */ |
401 static_heap_dumped = 0; | 406 static_heap_dumped = 0; |
402 | 407 |
403 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET); | 408 size = lseek (a_out, f_data_s_scnptr + data_size, SEEK_SET); |
404 | 409 |
405 if (!sections_reversed) | 410 if (!sections_reversed) |
406 { | 411 { |
407 size = f_nextdata_s_scnptr - size; | 412 size = f_nextdata_s_scnptr - size; |
408 dup_file_area(a_out, a_new, size); | 413 dup_file_area (a_out, a_new, size); |
409 } | 414 } |
410 else | 415 else |
411 { | 416 { |
412 /* need to pad to bss with data in file */ | 417 /* need to pad to bss with data in file */ |
413 printf ("padding .data ... %lx bytes\n", data_padding); | 418 printf ("padding .data ... %lx bytes\n", data_padding); |
414 size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size; | 419 size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size; |
415 dup_file_area(a_out, a_new, size); | 420 dup_file_area (a_out, a_new, size); |
416 | 421 |
417 /* dump bss + padding between sections */ | 422 /* dump bss + padding between sections */ |
418 printf ("dumping .bss into executable... %lx bytes\n", bss_size); | 423 printf ("dumping .bss into executable... %lx bytes\n", bss_size); |
419 if (write(a_new, bss_start, bss_size) != (int)bss_size) | 424 if (write (a_new, bss_start, bss_size) != bss_size) |
420 PERROR("failed to write bss section"); | 425 PERROR ("failed to write bss section"); |
421 | 426 |
422 /* pad, needs to be zero */ | 427 /* pad, needs to be zero */ |
423 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; | 428 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; |
424 if (bss_padding < 0) | 429 if (bss_padding < 0) |
425 PERROR("padded .bss too small"); | 430 PERROR ("padded .bss too small"); |
426 printf ("padding .bss ... %lx bytes\n", bss_padding); | 431 printf ("padding .bss ... %lx bytes\n", bss_padding); |
427 empty_space = malloc(bss_padding); | 432 empty_space = malloc (bss_padding); |
428 memset(empty_space, 0, bss_padding); | 433 memset (empty_space, 0, bss_padding); |
429 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) | 434 if (write (a_new, empty_space, bss_padding) != bss_padding) |
430 PERROR("failed to write bss section"); | 435 PERROR ("failed to write bss section"); |
431 free(empty_space); | 436 free (empty_space); |
432 if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr) | 437 if (lseek (a_new, 0, SEEK_CUR) != (long) f_nextdata.s_scnptr) |
433 { | 438 { |
434 printf("at %lx should be at %lx\n", | 439 printf ("at %lx should be at %lx\n", |
435 lseek(a_new, 0, SEEK_CUR), | 440 lseek (a_new, 0, SEEK_CUR), |
436 f_nextdata.s_scnptr); | 441 f_nextdata.s_scnptr); |
437 PERROR("file positioning error\n"); | 442 PERROR ("file positioning error\n"); |
438 } | 443 } |
439 lseek(a_out, f_nextdata_s_scnptr, SEEK_SET); | 444 lseek (a_out, f_nextdata_s_scnptr, SEEK_SET); |
440 } | 445 } |
441 | 446 |
442 CHECK_AOUT_POS(f_nextdata_s_scnptr); | 447 CHECK_AOUT_POS (f_nextdata_s_scnptr); |
443 | 448 |
444 /* now dump - nextdata don't need to do this cygwin ds is in .data! */ | 449 /* now dump - nextdata don't need to do this cygwin ds is in .data! */ |
445 printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size); | 450 printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size); |
446 | 451 |
447 dup_file_area(a_out,a_new,f_nextdata.s_size); | 452 dup_file_area (a_out,a_new,f_nextdata.s_size); |
448 | 453 |
449 /* write rest of file */ | 454 /* write rest of file */ |
450 printf ("writing rest of file\n"); | 455 printf ("writing rest of file\n"); |
451 size = lseek(a_out, 0, SEEK_END); | 456 size = lseek (a_out, 0, SEEK_END); |
452 size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining in a_out */ | 457 size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining in a_out */ |
453 lseek(a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET); | 458 lseek (a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET); |
454 | 459 |
455 dup_file_area(a_out, a_new, size); | 460 dup_file_area (a_out, a_new, size); |
456 } | 461 } |
457 | 462 |
458 /* | 463 /* |
459 * copy from aout to anew | 464 * copy from aout to anew |
460 */ | 465 */ |
461 static void dup_file_area(int a_out, int a_new, long size) | 466 static void |
467 dup_file_area (int a_out, int a_new, long size) | |
462 { | 468 { |
463 char page[BUFSIZ]; | 469 char page[BUFSIZ]; |
464 long n; | 470 long n; |
465 for (; size > 0; size -= sizeof (page)) | 471 for (; size > 0; size -= sizeof (page)) |
466 { | 472 { |
467 n = size > sizeof (page) ? sizeof (page) : size; | 473 n = size > (long) sizeof (page) ? sizeof (page) : size; |
468 if (read (a_out, page, n) != n || write (a_new, page, n) != n) | 474 if (read (a_out, page, n) != n || write (a_new, page, n) != n) |
469 PERROR ("dump_out()"); | 475 PERROR ("dump_out()"); |
470 } | 476 } |
471 } | 477 } |
472 | 478 |
473 #if 0 | 479 #if 0 |
474 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval) | 480 static void |
481 write_int_to_bss (int a_out, int a_new, void* va, void* newval) | |
475 { | 482 { |
476 int cpos; | 483 int cpos; |
477 | 484 |
478 cpos = lseek(a_new, 0, SEEK_CUR); | 485 cpos = lseek (a_new, 0, SEEK_CUR); |
479 if (va < bss_start || va > bss_start + f_data.s_size) | 486 if (va < bss_start || va > bss_start + f_data.s_size) |
480 PERROR("address not in data space\n"); | 487 PERROR ("address not in data space\n"); |
481 lseek(a_new, f_data.s_scnptr + ((unsigned long)va - | 488 lseek (a_new, f_data.s_scnptr + ((unsigned long)va - |
482 (unsigned long)bss_start), SEEK_SET); | 489 (unsigned long)bss_start), SEEK_SET); |
483 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int)) | 490 if (write (a_new, newval, sizeof (int)) != (int) sizeof (int)) |
484 PERROR("failed to write int value"); | 491 PERROR ("failed to write int value"); |
485 lseek(a_new, cpos, SEEK_SET); | 492 lseek (a_new, cpos, SEEK_SET); |
486 } | 493 } |
487 #endif | 494 #endif |
488 | 495 |
489 #endif /* HAVE_A_OUT_H */ | 496 #endif /* HAVE_A_OUT_H */ |