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