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