XEmacs 21.2.46 "Urania".
[chise/xemacs-chise.git.1] / src / unexcw.c
1 /* unexec for GNU Emacs on Cygwin32.
2    Copyright (C) 1994, 1998 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 the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20
21 */
22
23 /* This is a complete rewrite, some code snarfed from unexnt.c and
24    unexec.c, Andy Piper (andy@xemacs.org) 13-1-98 */
25
26 #include <config.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
35 #define DONT_ENCAPSULATE /* filenames are external in unex*.c */
36 #include "sysfile.h"
37
38 #define PERROR(arg) perror(arg);exit(-1) 
39
40 #ifndef HAVE_A_OUT_H
41 unexec (char *, char *, void *, void *, void *)
42 {
43   PERROR("cannot unexec() a.out.h not installed");
44 }
45 #else
46
47 #ifndef MAX_PATH
48 #define MAX_PATH 260
49 #endif
50 #include <a.out.h>
51
52 #define ALLOC_UNIT 0xFFFF
53 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
54 #define ALIGN_ALLOC(addr) \
55 ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
56 /* Note that all sections must be aligned on a 0x1000 boundary so
57    this is the minimum size that our dummy bss can be. */
58 #ifndef NO_DEBUG
59 #define BSS_PAD_SIZE    0x1000
60 #else
61 #define BSS_PAD_SIZE    0
62 #endif
63
64 /* To prevent zero-initialized variables from being placed into the bss
65    section, use non-zero values to represent an uninitialized state.  */
66 #define UNINIT_PTR ((void *) 0xF0A0F0A0)
67 #define UNINIT_LONG (0xF0A0F0A0L)
68
69 static void get_section_info (int a_out, char* a_name);
70 static void copy_executable_and_dump_data_section (int a_out, int a_new);
71 static void dup_file_area(int a_out, int a_new, long size);
72 #if 0
73 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval);
74 #endif
75
76 /* Cached info about the .data section in the executable.  */
77 void* data_start_va = UNINIT_PTR;
78 unsigned long  data_size = UNINIT_LONG;
79
80 /* Cached info about the .bss section in the executable.  */
81 void* bss_start = UNINIT_PTR;
82 unsigned long  bss_size = UNINIT_LONG;
83 int sections_reversed = 0;
84 FILHDR f_hdr;
85 PEAOUTHDR f_ohdr;
86 SCNHDR f_data, f_bss, f_text, f_nextdata;
87
88 #define PERROR(arg) perror(arg);exit(-1) 
89 #define CHECK_AOUT_POS(a) \
90 if (lseek(a_out, 0, SEEK_CUR) != a) \
91 { \
92   printf("we are at %lx, should be at %lx\n", \
93          lseek(a_out, 0, SEEK_CUR), a); \
94   exit(-1); \
95 }
96
97 /* Dump out .data and .bss sections into a new executable.  */
98 int
99 unexec (char *out_name, char *in_name, uintptr_t start_data, 
100         uintptr_t d1, uintptr_t d2)
101 {
102   /* ugly nt hack - should be in lisp */
103   int a_new, a_out = -1;
104   char new_name[MAX_PATH], a_name[MAX_PATH];
105   char *ptr;
106   
107   /* Make sure that the input and output filenames have the
108      ".exe" extension...patch them up if they don't.  */
109   strcpy (a_name, in_name);
110   ptr = a_name + strlen (a_name) - 4;
111   if (strcmp (ptr, ".exe"))
112     strcat (a_name, ".exe");
113
114   strcpy (new_name, out_name);
115   ptr = new_name + strlen (new_name) - 4;
116   if (strcmp (ptr, ".exe"))
117     strcat (new_name, ".exe");
118
119   /* We need to round off our heap to NT's allocation unit (64KB).  */
120   /* round_heap (get_allocation_unit ()); */
121
122   if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0)
123     {
124       PERROR (a_name);
125     }
126
127   if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
128                      0755)) < 0)
129     {
130       PERROR (new_name);
131     }
132
133   /* Get the interesting section info, like start and size of .bss...  */
134   get_section_info (a_out, a_name);
135
136   copy_executable_and_dump_data_section (a_out, a_new);
137
138   close(a_out);
139   close(a_new);
140   return 0;
141 }
142
143 /* Flip through the executable and cache the info necessary for dumping.  */
144 static void get_section_info (int a_out, char* a_name)
145 {
146   extern char my_ebss[];
147   /* From lastfile.c  */
148   extern char my_edata[];
149
150   if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
151     {
152       PERROR (a_name);
153     }
154
155   if (f_hdr.e_magic != DOSMAGIC) 
156     {
157       PERROR("unknown exe header");
158     }
159
160   /* Check the NT header signature ...  */
161   if (f_hdr.nt_signature != NT_SIGNATURE) 
162     {
163       PERROR("invalid nt header");
164     }
165
166   /* Flip through the sections for .data and .bss ...  */
167   if (f_hdr.f_opthdr > 0)
168     {
169       if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
170         {
171           PERROR (a_name);
172         }
173     }
174   /* Loop through .data & .bss section headers, copying them in.
175      With newer lds these are reversed so we have to cope with both */
176   lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
177
178   if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
179       ||
180       strcmp (f_text.s_name, ".text"))
181     {
182       PERROR ("no .text section");
183     }
184
185   /* The .bss section.  */
186   if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
187       ||
188       (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
189     {
190       PERROR ("no .bss / .data section");
191     }
192
193   /* check for reversed .bss and .data */
194   if (!strcmp(f_bss.s_name, ".data"))
195     {
196       printf(".data and .bss reversed\n");
197       sections_reversed = 1;
198       memcpy(&f_data, &f_bss, sizeof(f_bss));
199     }
200
201   /* The .data section.  */
202   if (!sections_reversed)
203     {
204       if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
205           ||
206           strcmp (f_data.s_name, ".data"))
207         {
208           PERROR ("no .data section");
209         }
210     }
211   else
212     {
213       if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
214           ||
215           strcmp (f_bss.s_name, ".bss"))
216         {
217           PERROR ("no .bss section");
218         }
219     }
220   
221   bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
222   bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start);
223   
224   /* must keep bss data that we want to be blank as blank */
225   printf("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize);
226
227   /* The .data section.  */
228   data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr);
229
230   /* We want to only write Emacs data back to the executable,
231      not any of the library data (if library data is included,
232      then a dumped Emacs won't run on system versions other
233      than the one Emacs was dumped on).  */
234   data_size = (unsigned long)my_edata - (unsigned long)data_start_va;
235   printf("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize);
236
237   /* The following data section - often .idata */
238   if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)
239       &&
240       strcmp (&f_nextdata.s_name[2], "data"))
241     {
242       PERROR ("no other data section");
243     }
244 }
245
246 /* The dump routines.  */
247
248 static void
249 copy_executable_and_dump_data_section (int a_out, int a_new)
250 {
251   long size=0;
252   unsigned long new_data_size, new_bss_size, 
253     bss_padding, file_sz_change, data_padding=0,
254     f_data_s_vaddr = f_data.s_vaddr,
255     f_data_s_scnptr = f_data.s_scnptr,
256     f_bss_s_vaddr = f_bss.s_vaddr, 
257     f_nextdata_s_scnptr = f_nextdata.s_scnptr;
258
259   int i;
260   void* empty_space;
261   extern int static_heap_dumped;
262   SCNHDR section;
263   /* calculate new sizes:
264
265      f_ohdr.dsize is the total initialized data size on disk which is
266      f_data.s_size + f_idata.s_size.
267
268      f_ohdr.data_start is the base addres of all data and so should
269      not be changed.
270      
271      *.s_vaddr is the virtual address of the start of the section
272      *normalized from f_ohdr.ImageBase.
273
274      *.s_paddr appears to be the number of bytes in the section
275      *actually used (whereas *.s_size is aligned).
276
277      bsize is now 0 since subsumed into .data
278      dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
279      f_data.s_vaddr is f_bss.s_vaddr
280      f_data.s_size is new dsize maybe.
281      what about s_paddr & s_scnptr?  */
282
283   /* this is the amount the file increases in size */
284   if (!sections_reversed)
285     {
286       new_bss_size = f_data.s_vaddr - f_bss.s_vaddr;
287       data_padding = 0;
288     }
289   else
290     {
291       new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr;
292       data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size;
293     }
294
295   if ((new_bss_size - bss_size) < BSS_PAD_SIZE)
296     { 
297       PERROR (".bss free space too small");
298     }
299
300   file_sz_change=(new_bss_size + data_padding) - BSS_PAD_SIZE;
301   new_data_size=f_ohdr.dsize + file_sz_change;
302
303   if (!sections_reversed)
304     {
305       f_data.s_vaddr = f_bss.s_vaddr;
306     }
307   f_data.s_paddr += file_sz_change;
308 #if 0 
309   if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize)
310     {
311       printf("section size doesn't tally with dsize %lx != %lx\n", 
312              f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
313     }
314 #endif
315   f_data.s_size += file_sz_change;
316   lseek (a_new, 0, SEEK_SET);
317   /* write file header */
318   f_hdr.f_symptr += file_sz_change;
319 #ifdef NO_DEBUG
320   f_hdr.f_nscns--;
321 #endif
322
323   printf("writing file header\n");
324   if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
325     {
326       PERROR("failed to write file header");
327     }
328   /* write optional header fixing dsize & bsize*/
329   printf("writing optional header\n");
330   printf("new data size is %lx, >= %lx\n", new_data_size,
331          f_ohdr.dsize + f_ohdr.bsize);
332   if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
333     {
334       printf("warning: new data size is < approx\n");
335     }
336   f_ohdr.dsize=new_data_size;
337   f_ohdr.bsize=BSS_PAD_SIZE;
338   if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
339     {
340       PERROR("failed to write optional header");
341     }
342   /* write text as is */
343   printf("writing text header (unchanged)\n");
344
345   if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
346     {
347       PERROR("failed to write text header");
348     }
349 #ifndef NO_DEBUG
350   /* Write small bss section. */
351   if (!sections_reversed)
352     {
353       f_bss.s_size = BSS_PAD_SIZE;
354       f_bss.s_paddr = BSS_PAD_SIZE;
355       f_bss.s_vaddr = f_data.s_vaddr - BSS_PAD_SIZE;
356       if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
357         {
358           PERROR("failed to write bss header");
359         }
360     }
361 #endif
362   /* write new data header */
363   printf("writing .data header\n");
364
365   if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
366     {
367       PERROR("failed to write data header");
368     }
369 #ifndef NO_DEBUG
370   /* Write small bss section. */
371   if (sections_reversed)
372     {
373       f_bss.s_size = BSS_PAD_SIZE;
374       f_bss.s_paddr = BSS_PAD_SIZE;
375       f_bss.s_vaddr = f_nextdata.s_vaddr - BSS_PAD_SIZE;
376       if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
377         {
378           PERROR("failed to write bss header");
379         }
380     }
381 #endif
382   printf("writing following data header\n");
383   f_nextdata.s_scnptr += file_sz_change;
384   if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change;
385   if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change;
386   if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata))
387     {
388       PERROR("failed to write nextdata header");
389     }
390
391   /* copy other section headers adjusting the file offset */
392   for (i=0; i<(f_hdr.f_nscns-3); i++)
393     {
394       if (read (a_out, &section, sizeof (section)) != sizeof (section))
395         {
396           PERROR ("no .data section");
397         }
398       
399       section.s_scnptr += file_sz_change;
400       if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
401       if (section.s_relptr != 0) section.s_relptr += file_sz_change;
402
403       if (write(a_new, &section, sizeof(section)) != sizeof(section))
404         {
405           PERROR("failed to write data header");
406         }
407     }
408 #ifdef NO_DEBUG
409   /* dump bss to maintain offsets */
410   memset(&f_bss, 0, sizeof(f_bss));
411   if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
412     {
413       PERROR("failed to write bss header");
414     }
415 #endif
416   size=lseek(a_new, 0, SEEK_CUR);
417   CHECK_AOUT_POS(size);
418
419   /* copy eveything else until start of data */
420   size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
421
422   printf ("copying executable up to data section ... %lx bytes\n", 
423           size);
424   dup_file_area(a_out, a_new, size);
425
426   CHECK_AOUT_POS(f_data_s_scnptr);
427
428   if (!sections_reversed)
429     {
430       /* dump bss + padding between sections, sans small bss pad */
431       printf ("dumping .bss into executable... %lx bytes\n", bss_size);
432       if (write(a_new, bss_start, bss_size) != (int)bss_size)
433         {
434           PERROR("failed to write bss section");
435         }
436       
437       /* pad, needs to be zero */
438       bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
439       if (bss_padding < 0)
440         {
441           PERROR("padded .bss too small");
442         }
443       printf ("padding .bss ... %lx bytes\n", bss_padding);
444       empty_space = malloc(bss_padding);
445       memset(empty_space, 0, bss_padding);
446       if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
447         {
448           PERROR("failed to write bss section");
449         }
450       free(empty_space);
451     }
452
453   /* tell dumped version not to free pure heap */
454   static_heap_dumped = 1;
455   /* Get a pointer to the raw data in our address space.  */
456   printf ("dumping .data section... %lx bytes\n", data_size);
457   if (write(a_new, data_start_va, data_size) != (int)data_size)
458     {
459       PERROR("failed to write data section");
460     }
461   /* were going to use free again ... */
462   static_heap_dumped = 0;
463   
464   size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET);
465
466   if (!sections_reversed)
467     {
468       size = f_nextdata_s_scnptr - size;
469       dup_file_area(a_out, a_new, size);
470     }
471   else
472     {
473       /* need to pad to bss with data in file */
474       printf ("padding .data ... %lx bytes\n", data_padding);
475       size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size;
476       dup_file_area(a_out, a_new, size);
477
478       /* dump bss + padding between sections */
479       printf ("dumping .bss into executable... %lx bytes\n", bss_size);
480       if (write(a_new, bss_start, bss_size) != (int)bss_size)
481         {
482           PERROR("failed to write bss section");
483         }
484       
485       /* pad, needs to be zero */
486       bss_padding = (new_bss_size - bss_size) - BSS_PAD_SIZE;
487       if (bss_padding < 0)
488         {
489           PERROR("padded .bss too small");
490         }
491       printf ("padding .bss ... %lx bytes\n", bss_padding);
492       empty_space = malloc(bss_padding);
493       memset(empty_space, 0, bss_padding);
494       if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
495         {
496           PERROR("failed to write bss section");
497         }
498       free(empty_space);
499       if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr)
500         {
501           printf("at %lx should be at %lx\n", 
502                  lseek(a_new, 0, SEEK_CUR),
503                  f_nextdata.s_scnptr);
504           PERROR("file positioning error\n");
505         }
506       lseek(a_out, f_nextdata_s_scnptr, SEEK_SET);
507     }
508
509   CHECK_AOUT_POS(f_nextdata_s_scnptr);
510
511   /* now dump - nextdata don't need to do this cygwin ds is in .data! */
512   printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size);
513
514   dup_file_area(a_out,a_new,f_nextdata.s_size);
515
516   /* write rest of file */
517   printf ("writing rest of file\n");
518   size = lseek(a_out, 0, SEEK_END);
519   size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining in a_out */
520   lseek(a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET);
521
522   dup_file_area(a_out, a_new, size);
523 }
524
525 /*
526  * copy from aout to anew
527  */
528 static void dup_file_area(int a_out, int a_new, long size)
529 {
530   char page[BUFSIZ];
531   long n;
532   for (; size > 0; size -= sizeof (page))
533     {
534       n = size > sizeof (page) ? sizeof (page) : size;
535       if (read (a_out, page, n) != n || write (a_new, page, n) != n)
536         {
537           PERROR ("dump_out()");
538         }
539     }
540 }
541
542 #if 0
543 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval)
544 {
545   int cpos;
546
547   cpos = lseek(a_new, 0, SEEK_CUR);
548   if (va < bss_start || va > bss_start + f_data.s_size)
549     {
550       PERROR("address not in data space\n");
551     }
552   lseek(a_new, f_data.s_scnptr + ((unsigned long)va - 
553                                   (unsigned long)bss_start), SEEK_SET);
554   if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
555     {
556       PERROR("failed to write int value");
557     }
558   lseek(a_new, cpos, SEEK_SET);
559 }
560 #endif
561
562 #endif /* HAVE_A_OUT_H */