Mercurial > hg > xemacs-beta
comparison src/unexcw.c @ 558:ed498ef2108b
[xemacs-hg @ 2001-05-23 09:59:33 by ben]
xemacs.mak: call `ver' to get the exact os version and put it in the
installation; suggestion from adrian.
behavior-defs.el: Add scroll-in-place, jka-compr, efs, fix up some things.
pop.c: Remove BROKEN_CYGWIN.
etc\sample.init.el: Rewrite to be much more careful about loading features --
now it decays gracefully even in the complete absence of packages.
Also avoid doing obnoxious things when loading efs.
configure.in: add some support for eventually turning on file coding by
default. Fix numerous places where AC_MSG_WARN had quotes
around its arg, which is bad. Replace with []. Same for
AC_MSG_ERROR.
s\cygwin32.h, s\mingw32.h: remove support for way old beta versions of cygwin.
don't put -Wno-sign-compare in the system switches; this
isn't a system issue. define BROKEN_SIGIO for cygwin to
get C-g support.
device-msw.c: signal an error rather than crash with an unavailable network
printer (from Mike Alexander).
event-msw.c: cleanup headers. fix (hopefully) an error with data corruption
when sending to a network connection.
fileio.c: Fix evil code that attempts
to handle the ~user prefix by (a) always assuming we're referencing
ourselves and not even verifying the user -- hence any file with
a tilde as its first char is invalid! (b) if there wasn't a slash
following the filename, the pointer was set *past* the end of
file and we started reading from uninitialized memory. Now we
simply treat these as files, always.
optionally for 21.4 (doc fix):
lread.c: cambia de pas_de_lache_ici -- al minimo usa la palabra certa.
frame.c: fix warnings.
emacs.c, nt.c, ntproc.c, process-nt.c, realpath.c, unexnt.c: rename MAX_PATH
to standard PATH_MAX.
process-nt.c, realpath.c: cleanup headers.
process-unix.c, sysdep.c, systime.h, syswindows.h: kill BROKEN_CYGWIN and
support for way old beta versions of cygwin.
sysfile.h: use _MAX_PATH (Windows) preferentially for PATH_MAX if defined.
include io.h on Cygwin (we need get_osfhandle()). include
sys/fcntl.h always, since we were including it in various
header files anyway.
unexcw.c: fix up style to conform to standard. remove duplicate definition
of PERROR.
buffer.c: comment change.
database.c, debug.h, device-tty.c, dired-msw.c, glyphs-msw.c: header
cleanups (remove places that directly include a system
header file, because we have our own layer to do this more cleanly
and portably); indentation fixes.
author | ben |
---|---|
date | Wed, 23 May 2001 09:59:48 +0000 |
parents | 666d73d6ac56 |
children | 023b83f4e54b |
comparison
equal
deleted
inserted
replaced
557:f486da5f1a3b | 558:ed498ef2108b |
---|---|
23 /* This is a complete rewrite, some code snarfed from unexnt.c and | 23 /* This is a complete rewrite, some code snarfed from unexnt.c and |
24 unexec.c, Andy Piper (andy@xemacs.org) 13-1-98 */ | 24 unexec.c, Andy Piper (andy@xemacs.org) 13-1-98 */ |
25 | 25 |
26 #include <config.h> | 26 #include <config.h> |
27 #include "lisp.h" | 27 #include "lisp.h" |
28 | |
29 #include <stdio.h> | |
30 #include <stdlib.h> | |
31 #include <unistd.h> | |
32 #include <fcntl.h> | |
33 #include <string.h> | |
34 | 28 |
35 #define DONT_ENCAPSULATE /* filenames are external in unex*.c */ | 29 #define DONT_ENCAPSULATE /* filenames are external in unex*.c */ |
36 #include "sysfile.h" | 30 #include "sysfile.h" |
37 | 31 |
38 #define PERROR(arg) \ | 32 #define PERROR(arg) \ |
45 unexec (char *, char *, void *, void *, void *) | 39 unexec (char *, char *, void *, void *, void *) |
46 { | 40 { |
47 PERROR("cannot unexec() a.out.h not installed"); | 41 PERROR("cannot unexec() a.out.h not installed"); |
48 } | 42 } |
49 #else | 43 #else |
50 | |
51 #ifndef MAX_PATH | |
52 #define MAX_PATH 260 | |
53 #endif | |
54 | 44 |
55 #ifdef MINGW | 45 #ifdef MINGW |
56 #include <../../include/a.out.h> | 46 #include <../../include/a.out.h> |
57 #else | 47 #else |
58 #include <a.out.h> | 48 #include <a.out.h> |
92 int sections_reversed = 0; | 82 int sections_reversed = 0; |
93 FILHDR f_hdr; | 83 FILHDR f_hdr; |
94 PEAOUTHDR f_ohdr; | 84 PEAOUTHDR f_ohdr; |
95 SCNHDR f_data, f_bss, f_text, f_nextdata; | 85 SCNHDR f_data, f_bss, f_text, f_nextdata; |
96 | 86 |
97 #define PERROR(arg) perror(arg);exit(-1) | 87 #define CHECK_AOUT_POS(a) \ |
98 #define CHECK_AOUT_POS(a) \ | 88 if (lseek(a_out, 0, SEEK_CUR) != a) \ |
99 if (lseek(a_out, 0, SEEK_CUR) != a) \ | 89 { \ |
100 { \ | 90 printf("we are at %lx, should be at %lx\n", \ |
101 printf("we are at %lx, should be at %lx\n", \ | 91 lseek(a_out, 0, SEEK_CUR), a); \ |
102 lseek(a_out, 0, SEEK_CUR), a); \ | 92 exit(-1); \ |
103 exit(-1); \ | |
104 } | 93 } |
105 | 94 |
106 /* Dump out .data and .bss sections into a new executable. */ | 95 /* Dump out .data and .bss sections into a new executable. */ |
107 int | 96 int |
108 unexec (char *out_name, char *in_name, uintptr_t start_data, | 97 unexec (char *out_name, char *in_name, uintptr_t start_data, |
109 uintptr_t d1, uintptr_t d2) | 98 uintptr_t d1, uintptr_t d2) |
110 { | 99 { |
111 /* ugly nt hack - should be in lisp */ | 100 /* ugly nt hack - should be in lisp */ |
112 int a_new, a_out = -1; | 101 int a_new, a_out = -1; |
113 char new_name[MAX_PATH], a_name[MAX_PATH]; | 102 char new_name[PATH_MAX], a_name[PATH_MAX]; |
114 char *ptr; | 103 char *ptr; |
115 | 104 |
116 /* Make sure that the input and output filenames have the | 105 /* Make sure that the input and output filenames have the |
117 ".exe" extension...patch them up if they don't. */ | 106 ".exe" extension...patch them up if they don't. */ |
118 strcpy (a_name, in_name); | 107 strcpy (a_name, in_name); |
127 | 116 |
128 /* We need to round off our heap to NT's allocation unit (64KB). */ | 117 /* We need to round off our heap to NT's allocation unit (64KB). */ |
129 /* round_heap (get_allocation_unit ()); */ | 118 /* round_heap (get_allocation_unit ()); */ |
130 | 119 |
131 if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0) | 120 if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0) |
132 { | 121 PERROR (a_name); |
133 PERROR (a_name); | |
134 } | |
135 | 122 |
136 if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY, | 123 if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY, |
137 0755)) < 0) | 124 0755)) < 0) |
138 { | 125 PERROR (new_name); |
139 PERROR (new_name); | |
140 } | |
141 | 126 |
142 /* Get the interesting section info, like start and size of .bss... */ | 127 /* Get the interesting section info, like start and size of .bss... */ |
143 get_section_info (a_out, a_name); | 128 get_section_info (a_out, a_name); |
144 | 129 |
145 copy_executable_and_dump_data_section (a_out, a_new); | 130 copy_executable_and_dump_data_section (a_out, a_new); |
148 close(a_new); | 133 close(a_new); |
149 return 0; | 134 return 0; |
150 } | 135 } |
151 | 136 |
152 /* Flip through the executable and cache the info necessary for dumping. */ | 137 /* Flip through the executable and cache the info necessary for dumping. */ |
153 static void get_section_info (int a_out, char* a_name) | 138 static void |
139 get_section_info (int a_out, char* a_name) | |
154 { | 140 { |
155 extern char my_ebss[]; | 141 extern char my_ebss[]; |
156 /* From lastfile.c */ | 142 /* From lastfile.c */ |
157 extern char my_edata[]; | 143 extern char my_edata[]; |
158 | 144 |
159 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) | 145 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr)) |
160 { | 146 PERROR (a_name); |
161 PERROR (a_name); | |
162 } | |
163 | 147 |
164 if (f_hdr.e_magic != DOSMAGIC) | 148 if (f_hdr.e_magic != DOSMAGIC) |
165 { | 149 PERROR("unknown exe header"); |
166 PERROR("unknown exe header"); | |
167 } | |
168 | 150 |
169 /* Check the NT header signature ... */ | 151 /* Check the NT header signature ... */ |
170 if (f_hdr.nt_signature != NT_SIGNATURE) | 152 if (f_hdr.nt_signature != NT_SIGNATURE) |
171 { | 153 PERROR("invalid nt header"); |
172 PERROR("invalid nt header"); | |
173 } | |
174 | 154 |
175 /* Flip through the sections for .data and .bss ... */ | 155 /* Flip through the sections for .data and .bss ... */ |
176 if (f_hdr.f_opthdr > 0) | 156 if (f_hdr.f_opthdr > 0) |
177 { | 157 { |
178 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ) | 158 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ) |
179 { | 159 PERROR (a_name); |
180 PERROR (a_name); | |
181 } | |
182 } | 160 } |
183 /* Loop through .data & .bss section headers, copying them in. | 161 /* Loop through .data & .bss section headers, copying them in. |
184 With newer lds these are reversed so we have to cope with both */ | 162 With newer lds these are reversed so we have to cope with both */ |
185 lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0); | 163 lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0); |
186 | 164 |
187 if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text) | 165 if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text) |
188 || | 166 || strcmp (f_text.s_name, ".text")) |
189 strcmp (f_text.s_name, ".text")) | 167 PERROR ("no .text section"); |
190 { | |
191 PERROR ("no .text section"); | |
192 } | |
193 | 168 |
194 /* The .bss section. */ | 169 /* The .bss section. */ |
195 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) | 170 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) |
196 || | 171 || (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data"))) |
197 (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data"))) | 172 PERROR ("no .bss / .data section"); |
198 { | |
199 PERROR ("no .bss / .data section"); | |
200 } | |
201 | 173 |
202 /* check for reversed .bss and .data */ | 174 /* check for reversed .bss and .data */ |
203 if (!strcmp(f_bss.s_name, ".data")) | 175 if (!strcmp(f_bss.s_name, ".data")) |
204 { | 176 { |
205 printf(".data and .bss reversed\n"); | 177 printf(".data and .bss reversed\n"); |
209 | 181 |
210 /* The .data section. */ | 182 /* The .data section. */ |
211 if (!sections_reversed) | 183 if (!sections_reversed) |
212 { | 184 { |
213 if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data) | 185 if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data) |
214 || | 186 || strcmp (f_data.s_name, ".data")) |
215 strcmp (f_data.s_name, ".data")) | 187 PERROR ("no .data section"); |
216 { | |
217 PERROR ("no .data section"); | |
218 } | |
219 } | 188 } |
220 else | 189 else |
221 { | 190 { |
222 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) | 191 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss) |
223 || | 192 || strcmp (f_bss.s_name, ".bss")) |
224 strcmp (f_bss.s_name, ".bss")) | 193 PERROR ("no .bss section"); |
225 { | |
226 PERROR ("no .bss section"); | |
227 } | |
228 } | 194 } |
229 | 195 |
230 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr); | 196 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr); |
231 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start); | 197 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start); |
232 | 198 |
245 | 211 |
246 /* The following data section - often .idata */ | 212 /* The following data section - often .idata */ |
247 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata) | 213 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata) |
248 && | 214 && |
249 strcmp (&f_nextdata.s_name[2], "data")) | 215 strcmp (&f_nextdata.s_name[2], "data")) |
250 { | 216 PERROR ("no other data section"); |
251 PERROR ("no other data section"); | |
252 } | |
253 } | 217 } |
254 | 218 |
255 /* The dump routines. */ | 219 /* The dump routines. */ |
256 | 220 |
257 static void | 221 static void |
258 copy_executable_and_dump_data_section (int a_out, int a_new) | 222 copy_executable_and_dump_data_section (int a_out, int a_new) |
259 { | 223 { |
260 long size=0; | 224 long size=0; |
261 unsigned long new_data_size, new_bss_size, | 225 unsigned long new_data_size, new_bss_size, |
262 bss_padding, file_sz_change, data_padding=0, | 226 bss_padding, file_sz_change, data_padding=0, |
263 f_data_s_vaddr = f_data.s_vaddr, | 227 f_data_s_vaddr = f_data.s_vaddr, |
264 f_data_s_scnptr = f_data.s_scnptr, | 228 f_data_s_scnptr = f_data.s_scnptr, |
265 f_bss_s_vaddr = f_bss.s_vaddr, | 229 f_bss_s_vaddr = f_bss.s_vaddr, |
266 f_nextdata_s_scnptr = f_nextdata.s_scnptr; | 230 f_nextdata_s_scnptr = f_nextdata.s_scnptr; |
267 | 231 |
268 int i; | 232 int i; |
269 void* empty_space; | 233 void* empty_space; |
270 extern int static_heap_dumped; | 234 extern int static_heap_dumped; |
271 SCNHDR section; | 235 SCNHDR section; |
272 /* calculate new sizes: | 236 /* calculate new sizes: |
273 | 237 |
274 f_ohdr.dsize is the total initialized data size on disk which is | 238 f_ohdr.dsize is the total initialized data size on disk which is |
275 f_data.s_size + f_idata.s_size. | 239 f_data.s_size + f_idata.s_size. |
276 | 240 |
277 f_ohdr.data_start is the base addres of all data and so should | 241 f_ohdr.data_start is the base addres of all data and so should |
278 not be changed. | 242 not be changed. |
279 | 243 |
280 *.s_vaddr is the virtual address of the start of the section | 244 *.s_vaddr is the virtual address of the start of the section |
281 *normalized from f_ohdr.ImageBase. | 245 *normalized from f_ohdr.ImageBase. |
282 | 246 |
283 *.s_paddr appears to be the number of bytes in the section | 247 *.s_paddr appears to be the number of bytes in the section |
284 *actually used (whereas *.s_size is aligned). | 248 *actually used (whereas *.s_size is aligned). |
285 | 249 |
286 bsize is now 0 since subsumed into .data | 250 bsize is now 0 since subsumed into .data |
287 dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr) | 251 dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr) |
288 f_data.s_vaddr is f_bss.s_vaddr | 252 f_data.s_vaddr is f_bss.s_vaddr |
289 f_data.s_size is new dsize maybe. | 253 f_data.s_size is new dsize maybe. |
290 what about s_paddr & s_scnptr? */ | 254 what about s_paddr & s_scnptr? */ |
300 new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr; | 264 new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr; |
301 data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size; | 265 data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size; |
302 } | 266 } |
303 | 267 |
304 if ((new_bss_size - bss_size) < BSS_PAD_SIZE) | 268 if ((new_bss_size - bss_size) < BSS_PAD_SIZE) |
305 { | 269 PERROR (".bss free space too small"); |
306 PERROR (".bss free space too small"); | |
307 } | |
308 | 270 |
309 file_sz_change=(new_bss_size + data_padding) - BSS_PAD_SIZE; | 271 file_sz_change=(new_bss_size + data_padding) - BSS_PAD_SIZE; |
310 new_data_size=f_ohdr.dsize + file_sz_change; | 272 new_data_size=f_ohdr.dsize + file_sz_change; |
311 | 273 |
312 if (!sections_reversed) | 274 if (!sections_reversed) |
313 { | 275 f_data.s_vaddr = f_bss.s_vaddr; |
314 f_data.s_vaddr = f_bss.s_vaddr; | |
315 } | |
316 f_data.s_paddr += file_sz_change; | 276 f_data.s_paddr += file_sz_change; |
317 #if 0 | 277 #if 0 |
318 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize) | 278 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize) |
319 { | 279 printf("section size doesn't tally with dsize %lx != %lx\n", |
320 printf("section size doesn't tally with dsize %lx != %lx\n", | 280 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize); |
321 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize); | |
322 } | |
323 #endif | 281 #endif |
324 f_data.s_size += file_sz_change; | 282 f_data.s_size += file_sz_change; |
325 lseek (a_new, 0, SEEK_SET); | 283 lseek (a_new, 0, SEEK_SET); |
326 /* write file header */ | 284 /* write file header */ |
327 f_hdr.f_symptr += file_sz_change; | 285 f_hdr.f_symptr += file_sz_change; |
329 f_hdr.f_nscns--; | 287 f_hdr.f_nscns--; |
330 #endif | 288 #endif |
331 | 289 |
332 printf("writing file header\n"); | 290 printf("writing file header\n"); |
333 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr)) | 291 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr)) |
334 { | 292 PERROR("failed to write file header"); |
335 PERROR("failed to write file header"); | |
336 } | |
337 /* write optional header fixing dsize & bsize*/ | 293 /* write optional header fixing dsize & bsize*/ |
338 printf("writing optional header\n"); | 294 printf("writing optional header\n"); |
339 printf("new data size is %lx, >= %lx\n", new_data_size, | 295 printf("new data size is %lx, >= %lx\n", new_data_size, |
340 f_ohdr.dsize + f_ohdr.bsize); | 296 f_ohdr.dsize + f_ohdr.bsize); |
341 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize ) | 297 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize ) |
342 { | 298 printf("warning: new data size is < approx\n"); |
343 printf("warning: new data size is < approx\n"); | |
344 } | |
345 f_ohdr.dsize=new_data_size; | 299 f_ohdr.dsize=new_data_size; |
346 f_ohdr.bsize=BSS_PAD_SIZE; | 300 f_ohdr.bsize=BSS_PAD_SIZE; |
347 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr)) | 301 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr)) |
348 { | 302 PERROR("failed to write optional header"); |
349 PERROR("failed to write optional header"); | |
350 } | |
351 /* write text as is */ | 303 /* write text as is */ |
352 printf("writing text header (unchanged)\n"); | 304 printf("writing text header (unchanged)\n"); |
353 | 305 |
354 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text)) | 306 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text)) |
355 { | 307 PERROR("failed to write text header"); |
356 PERROR("failed to write text header"); | |
357 } | |
358 #ifndef NO_DEBUG | 308 #ifndef NO_DEBUG |
359 /* Write small bss section. */ | 309 /* Write small bss section. */ |
360 if (!sections_reversed) | 310 if (!sections_reversed) |
361 { | 311 { |
362 f_bss.s_size = BSS_PAD_SIZE; | 312 f_bss.s_size = BSS_PAD_SIZE; |
363 f_bss.s_paddr = BSS_PAD_SIZE; | 313 f_bss.s_paddr = BSS_PAD_SIZE; |
364 f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE; | 314 f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE; |
365 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 315 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) |
366 { | 316 PERROR("failed to write bss header"); |
367 PERROR("failed to write bss header"); | |
368 } | |
369 } | 317 } |
370 #endif | 318 #endif |
371 /* write new data header */ | 319 /* write new data header */ |
372 printf("writing .data header\n"); | 320 printf("writing .data header\n"); |
373 | 321 |
374 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data)) | 322 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data)) |
375 { | 323 PERROR("failed to write data header"); |
376 PERROR("failed to write data header"); | |
377 } | |
378 #ifndef NO_DEBUG | 324 #ifndef NO_DEBUG |
379 /* Write small bss section. */ | 325 /* Write small bss section. */ |
380 if (sections_reversed) | 326 if (sections_reversed) |
381 { | 327 { |
382 f_bss.s_size = BSS_PAD_SIZE; | 328 f_bss.s_size = BSS_PAD_SIZE; |
383 f_bss.s_paddr = BSS_PAD_SIZE; | 329 f_bss.s_paddr = BSS_PAD_SIZE; |
384 f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE; | 330 f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE; |
385 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 331 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) |
386 { | 332 PERROR("failed to write bss header"); |
387 PERROR("failed to write bss header"); | |
388 } | |
389 } | 333 } |
390 #endif | 334 #endif |
391 printf("writing following data header\n"); | 335 printf("writing following data header\n"); |
392 f_nextdata.s_scnptr += file_sz_change; | 336 f_nextdata.s_scnptr += file_sz_change; |
393 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change; | 337 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change; |
394 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change; | 338 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change; |
395 if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata)) | 339 if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata)) |
396 { | 340 PERROR("failed to write nextdata header"); |
397 PERROR("failed to write nextdata header"); | |
398 } | |
399 | 341 |
400 /* copy other section headers adjusting the file offset */ | 342 /* copy other section headers adjusting the file offset */ |
401 for (i=0; i<(f_hdr.f_nscns-3); i++) | 343 for (i=0; i<(f_hdr.f_nscns-3); i++) |
402 { | 344 { |
403 if (read (a_out, §ion, sizeof (section)) != sizeof (section)) | 345 if (read (a_out, §ion, sizeof (section)) != sizeof (section)) |
404 { | 346 PERROR ("no .data section"); |
405 PERROR ("no .data section"); | |
406 } | |
407 | 347 |
408 section.s_scnptr += file_sz_change; | 348 section.s_scnptr += file_sz_change; |
409 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change; | 349 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change; |
410 if (section.s_relptr != 0) section.s_relptr += file_sz_change; | 350 if (section.s_relptr != 0) section.s_relptr += file_sz_change; |
411 | 351 |
412 if (write(a_new, §ion, sizeof(section)) != sizeof(section)) | 352 if (write(a_new, §ion, sizeof(section)) != sizeof(section)) |
413 { | 353 PERROR("failed to write data header"); |
414 PERROR("failed to write data header"); | |
415 } | |
416 } | 354 } |
417 #ifdef NO_DEBUG | 355 #ifdef NO_DEBUG |
418 /* dump bss to maintain offsets */ | 356 /* dump bss to maintain offsets */ |
419 memset(&f_bss, 0, sizeof(f_bss)); | 357 memset(&f_bss, 0, sizeof(f_bss)); |
420 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) | 358 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss)) |
421 { | 359 PERROR("failed to write bss header"); |
422 PERROR("failed to write bss header"); | |
423 } | |
424 #endif | 360 #endif |
425 size=lseek(a_new, 0, SEEK_CUR); | 361 size=lseek(a_new, 0, SEEK_CUR); |
426 CHECK_AOUT_POS(size); | 362 CHECK_AOUT_POS(size); |
427 | 363 |
428 /* copy eveything else until start of data */ | 364 /* copy eveything else until start of data */ |
444 } | 380 } |
445 | 381 |
446 /* pad, needs to be zero */ | 382 /* pad, needs to be zero */ |
447 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; | 383 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; |
448 if (bss_padding < 0) | 384 if (bss_padding < 0) |
449 { | 385 PERROR("padded .bss too small"); |
450 PERROR("padded .bss too small"); | |
451 } | |
452 printf ("padding .bss ... %lx bytes\n", bss_padding); | 386 printf ("padding .bss ... %lx bytes\n", bss_padding); |
453 empty_space = malloc(bss_padding); | 387 empty_space = malloc(bss_padding); |
454 memset(empty_space, 0, bss_padding); | 388 memset(empty_space, 0, bss_padding); |
455 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) | 389 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) |
456 { | 390 PERROR("failed to write bss section"); |
457 PERROR("failed to write bss section"); | |
458 } | |
459 free(empty_space); | 391 free(empty_space); |
460 } | 392 } |
461 | 393 |
462 /* tell dumped version not to free pure heap */ | 394 /* tell dumped version not to free pure heap */ |
463 static_heap_dumped = 1; | 395 static_heap_dumped = 1; |
464 /* Get a pointer to the raw data in our address space. */ | 396 /* Get a pointer to the raw data in our address space. */ |
465 printf ("dumping .data section... %lx bytes\n", data_size); | 397 printf ("dumping .data section... %lx bytes\n", data_size); |
466 if (write(a_new, data_start_va, data_size) != (int)data_size) | 398 if (write(a_new, data_start_va, data_size) != (int)data_size) |
467 { | 399 PERROR("failed to write data section"); |
468 PERROR("failed to write data section"); | |
469 } | |
470 /* were going to use free again ... */ | 400 /* were going to use free again ... */ |
471 static_heap_dumped = 0; | 401 static_heap_dumped = 0; |
472 | 402 |
473 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET); | 403 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET); |
474 | 404 |
485 dup_file_area(a_out, a_new, size); | 415 dup_file_area(a_out, a_new, size); |
486 | 416 |
487 /* dump bss + padding between sections */ | 417 /* dump bss + padding between sections */ |
488 printf ("dumping .bss into executable... %lx bytes\n", bss_size); | 418 printf ("dumping .bss into executable... %lx bytes\n", bss_size); |
489 if (write(a_new, bss_start, bss_size) != (int)bss_size) | 419 if (write(a_new, bss_start, bss_size) != (int)bss_size) |
490 { | 420 PERROR("failed to write bss section"); |
491 PERROR("failed to write bss section"); | |
492 } | |
493 | 421 |
494 /* pad, needs to be zero */ | 422 /* pad, needs to be zero */ |
495 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; | 423 bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE; |
496 if (bss_padding < 0) | 424 if (bss_padding < 0) |
497 { | 425 PERROR("padded .bss too small"); |
498 PERROR("padded .bss too small"); | |
499 } | |
500 printf ("padding .bss ... %lx bytes\n", bss_padding); | 426 printf ("padding .bss ... %lx bytes\n", bss_padding); |
501 empty_space = malloc(bss_padding); | 427 empty_space = malloc(bss_padding); |
502 memset(empty_space, 0, bss_padding); | 428 memset(empty_space, 0, bss_padding); |
503 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) | 429 if (write(a_new, empty_space, bss_padding) != (int)bss_padding) |
504 { | 430 PERROR("failed to write bss section"); |
505 PERROR("failed to write bss section"); | |
506 } | |
507 free(empty_space); | 431 free(empty_space); |
508 if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr) | 432 if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr) |
509 { | 433 { |
510 printf("at %lx should be at %lx\n", | 434 printf("at %lx should be at %lx\n", |
511 lseek(a_new, 0, SEEK_CUR), | 435 lseek(a_new, 0, SEEK_CUR), |
540 long n; | 464 long n; |
541 for (; size > 0; size -= sizeof (page)) | 465 for (; size > 0; size -= sizeof (page)) |
542 { | 466 { |
543 n = size > sizeof (page) ? sizeof (page) : size; | 467 n = size > sizeof (page) ? sizeof (page) : size; |
544 if (read (a_out, page, n) != n || write (a_new, page, n) != n) | 468 if (read (a_out, page, n) != n || write (a_new, page, n) != n) |
545 { | 469 PERROR ("dump_out()"); |
546 PERROR ("dump_out()"); | |
547 } | |
548 } | 470 } |
549 } | 471 } |
550 | 472 |
551 #if 0 | 473 #if 0 |
552 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval) | 474 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval) |
553 { | 475 { |
554 int cpos; | 476 int cpos; |
555 | 477 |
556 cpos = lseek(a_new, 0, SEEK_CUR); | 478 cpos = lseek(a_new, 0, SEEK_CUR); |
557 if (va < bss_start || va > bss_start + f_data.s_size) | 479 if (va < bss_start || va > bss_start + f_data.s_size) |
558 { | 480 PERROR("address not in data space\n"); |
559 PERROR("address not in data space\n"); | |
560 } | |
561 lseek(a_new, f_data.s_scnptr + ((unsigned long)va - | 481 lseek(a_new, f_data.s_scnptr + ((unsigned long)va - |
562 (unsigned long)bss_start), SEEK_SET); | 482 (unsigned long)bss_start), SEEK_SET); |
563 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int)) | 483 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int)) |
564 { | 484 PERROR("failed to write int value"); |
565 PERROR("failed to write int value"); | |
566 } | |
567 lseek(a_new, cpos, SEEK_SET); | 485 lseek(a_new, cpos, SEEK_SET); |
568 } | 486 } |
569 #endif | 487 #endif |
570 | 488 |
571 #endif /* HAVE_A_OUT_H */ | 489 #endif /* HAVE_A_OUT_H */ |