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