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