54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
1 /* unexec() support for Cygwin;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
2 complete rewrite of xemacs Cygwin unexec() code
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
3
|
79759
|
4 Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
|
54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
5
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
6 This file is part of GNU Emacs.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
7
|
94963
|
8 GNU Emacs is free software: you can redistribute it and/or modify
|
54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
9 it under the terms of the GNU General Public License as published by
|
94963
|
10 the Free Software Foundation, either version 3 of the License, or
|
|
11 (at your option) any later version.
|
54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
12
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
13 GNU Emacs is distributed in the hope that it will be useful,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
16 GNU General Public License for more details.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
17
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
18 You should have received a copy of the GNU General Public License
|
94963
|
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
|
54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
20
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
21 #include <config.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
22 #include <lisp.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
23 #include <stdio.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
24 #include <fcntl.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
25 #include <a.out.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
26 #include <unistd.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
27 #include <assert.h>
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
28
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
29 #define DOTEXE ".exe"
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
30
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
31 extern int bss_sbrk_did_unexec;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
32
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
33 /* emacs symbols that indicate where bss and data end for emacs internals */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
34 extern char my_endbss[];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
35 extern char my_edata[];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
36
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
37 /*
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
38 ** header for Windows executable files
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
39 */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
40 typedef struct
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
41 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
42 FILHDR file_header;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
43 PEAOUTHDR file_optional_header;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
44 SCNHDR section_header[32];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
45 } exe_header_t;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
46
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
47 int debug_unexcw = 0;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
48
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
49 /*
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
50 ** Read the header from the executable into memory so we can more easily access it.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
51 */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
52 static exe_header_t *
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
53 read_exe_header (int fd, exe_header_t * exe_header_buffer)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
54 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
55 int i;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
56 int ret;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
57
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
58 assert (fd >= 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
59 assert (exe_header_buffer != 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
60
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
61 ret = lseek (fd, 0L, SEEK_SET);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
62 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
63
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
64 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
65 read (fd, &exe_header_buffer->file_header,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
66 sizeof (exe_header_buffer->file_header));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
67 assert (ret == sizeof (exe_header_buffer->file_header));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
68
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
69 assert (exe_header_buffer->file_header.e_magic == 0x5a4d);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
70 assert (exe_header_buffer->file_header.nt_signature == 0x4550);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
71 assert (exe_header_buffer->file_header.f_magic == 0x014c);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
72 assert (exe_header_buffer->file_header.f_nscns > 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
73 assert (exe_header_buffer->file_header.f_nscns <=
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
74 sizeof (exe_header_buffer->section_header) /
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
75 sizeof (exe_header_buffer->section_header[0]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
76 assert (exe_header_buffer->file_header.f_opthdr > 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
77
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
78 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
79 read (fd, &exe_header_buffer->file_optional_header,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
80 sizeof (exe_header_buffer->file_optional_header));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
81 assert (ret == sizeof (exe_header_buffer->file_optional_header));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
82
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
83 assert (exe_header_buffer->file_optional_header.magic == 0x010b);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
84
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
85 for (i = 0; i < exe_header_buffer->file_header.f_nscns; ++i)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
86 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
87 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
88 read (fd, &exe_header_buffer->section_header[i],
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
89 sizeof (exe_header_buffer->section_header[i]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
90 assert (ret == sizeof (exe_header_buffer->section_header[i]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
91 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
92
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
93 return (exe_header_buffer);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
94 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
95
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
96 /*
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
97 ** Fix the dumped emacs executable:
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
98 **
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
99 ** - copy .data section data of interest from running executable into
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
100 ** output .exe file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
101 **
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
102 ** - convert .bss section into an initialized data section (like
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
103 ** .data) and copy .bss section data of interest from running
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
104 ** executable into output .exe file
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
105 */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
106 static void
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
107 fixup_executable (int fd)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
108 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
109 exe_header_t exe_header_buffer;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
110 exe_header_t *exe_header;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
111 int i;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
112 int ret;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
113 int found_data = 0;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
114 int found_bss = 0;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
115
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
116 exe_header = read_exe_header (fd, &exe_header_buffer);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
117 assert (exe_header != 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
118
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
119 assert (exe_header->file_header.f_nscns > 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
120 for (i = 0; i < exe_header->file_header.f_nscns; ++i)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
121 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
122 unsigned long start_address =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
123 exe_header->section_header[i].s_vaddr +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
124 exe_header->file_optional_header.ImageBase;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
125 unsigned long end_address =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
126 exe_header->section_header[i].s_vaddr +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
127 exe_header->file_optional_header.ImageBase +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
128 exe_header->section_header[i].s_paddr;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
129 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
130 printf ("%8s start 0x%08x end 0x%08x\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
131 exe_header->section_header[i].s_name,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
132 start_address, end_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
133 if (my_edata >= (char *) start_address
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
134 && my_edata < (char *) end_address)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
135 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
136 /* data section */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
137 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
138 lseek (fd, (long) (exe_header->section_header[i].s_scnptr),
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
139 SEEK_SET);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
140 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
141 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
142 write (fd, (char *) start_address,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
143 my_edata - (char *) start_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
144 assert (ret == my_edata - (char *) start_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
145 ++found_data;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
146 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
147 printf (" .data, mem start 0x%08x mem length %d\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
148 start_address, my_edata - (char *) start_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
149 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
150 printf (" .data, file start %d file length %d\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
151 (int) exe_header->section_header[i].s_scnptr,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
152 (int) exe_header->section_header[i].s_paddr);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
153 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
154 else if (my_endbss >= (char *) start_address
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
155 && my_endbss < (char *) end_address)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
156 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
157 /* bss section */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
158 ++found_bss;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
159 if (exe_header->section_header[i].s_flags & 0x00000080)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
160 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
161 /* convert uninitialized data section to initialized data section */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
162 struct stat statbuf;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
163 ret = fstat (fd, &statbuf);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
164 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
165
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
166 exe_header->section_header[i].s_flags &= ~0x00000080;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
167 exe_header->section_header[i].s_flags |= 0x00000040;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
168
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
169 exe_header->section_header[i].s_scnptr =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
170 (statbuf.st_size +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
171 exe_header->file_optional_header.FileAlignment) /
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
172 exe_header->file_optional_header.FileAlignment *
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
173 exe_header->file_optional_header.FileAlignment;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
174
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
175 exe_header->section_header[i].s_size =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
176 (exe_header->section_header[i].s_paddr +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
177 exe_header->file_optional_header.FileAlignment) /
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
178 exe_header->file_optional_header.FileAlignment *
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
179 exe_header->file_optional_header.FileAlignment;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
180
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
181 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
182 lseek (fd,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
183 (long) (exe_header->section_header[i].s_scnptr +
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
184 exe_header->section_header[i].s_size - 1),
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
185 SEEK_SET);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
186 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
187 ret = write (fd, "", 1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
188 assert (ret == 1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
189
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
190 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
191 lseek (fd,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
192 (long) ((char *) &exe_header->section_header[i] -
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
193 (char *) exe_header), SEEK_SET);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
194 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
195 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
196 write (fd, &exe_header->section_header[i],
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
197 sizeof (exe_header->section_header[i]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
198 assert (ret == sizeof (exe_header->section_header[i]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
199 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
200 printf (" seek to %ld, write %d\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
201 (long) ((char *) &exe_header->section_header[i] -
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
202 (char *) exe_header),
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
203 sizeof (exe_header->section_header[i]));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
204 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
205 /* write initialized data section */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
206 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
207 lseek (fd, (long) (exe_header->section_header[i].s_scnptr),
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
208 SEEK_SET);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
209 assert (ret != -1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
210 ret =
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
211 write (fd, (char *) start_address,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
212 my_endbss - (char *) start_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
213 assert (ret == (my_endbss - (char *) start_address));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
214 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
215 printf (" .bss, mem start 0x%08x mem length %d\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
216 start_address, my_endbss - (char *) start_address);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
217 if (debug_unexcw)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
218 printf (" .bss, file start %d file length %d\n",
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
219 (int) exe_header->section_header[i].s_scnptr,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
220 (int) exe_header->section_header[i].s_paddr);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
221 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
222 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
223 assert (found_bss == 1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
224 assert (found_data == 1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
225 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
226
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
227 /*
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
228 ** Windows likes .exe suffixes on executables.
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
229 */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
230 static char *
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
231 add_exe_suffix_if_necessary (const char *name, char *modified)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
232 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
233 int i = strlen (name);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
234 if (i <= (sizeof (DOTEXE) - 1))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
235 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
236 sprintf (modified, "%s%s", name, DOTEXE);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
237 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
238 else if (!strcasecmp (name + i - (sizeof (DOTEXE) - 1), DOTEXE))
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
239 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
240 strcpy (modified, name);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
241 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
242 else
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
243 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
244 sprintf (modified, "%s%s", name, DOTEXE);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
245 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
246 return (modified);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
247 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
248
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
249 int
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
250 unexec (char *outfile, char *infile, unsigned start_data, unsigned d1,
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
251 unsigned d2)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
252 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
253 char infile_buffer[FILENAME_MAX];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
254 char outfile_buffer[FILENAME_MAX];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
255 int fd_in;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
256 int fd_out;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
257 int ret;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
258 int ret2;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
259
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
260 if (bss_sbrk_did_unexec)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
261 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
262 /* can only dump once */
|
73688
|
263 printf ("You can only dump Emacs once on this platform.\n");
|
54844
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
264 return (1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
265 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
266
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
267 report_sheap_usage (1);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
268
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
269 infile = add_exe_suffix_if_necessary (infile, infile_buffer);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
270 outfile = add_exe_suffix_if_necessary (outfile, outfile_buffer);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
271
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
272 fd_in = open (infile, O_RDONLY | O_BINARY);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
273 assert (fd_in >= 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
274 fd_out = open (outfile, O_RDWR | O_TRUNC | O_CREAT | O_BINARY, 0755);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
275 assert (fd_out >= 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
276 for (;;)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
277 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
278 char buffer[4096];
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
279 ret = read (fd_in, buffer, sizeof (buffer));
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
280 if (ret == 0)
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
281 {
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
282 /* eof */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
283 break;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
284 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
285 assert (ret > 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
286 /* data */
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
287 ret2 = write (fd_out, buffer, ret);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
288 assert (ret2 == ret);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
289 }
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
290 ret = close (fd_in);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
291 assert (ret == 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
292
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
293 bss_sbrk_did_unexec = 1;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
294 fixup_executable (fd_out);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
295 bss_sbrk_did_unexec = 0;
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
296
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
297 ret = close (fd_out);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
298 assert (ret == 0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
299
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
300 return (0);
|
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
diff
changeset
|
301 }
|
54858
|
302
|
|
303 /* arch-tag: fc44f6c3-ca0a-45e0-a5a2-58b6101b1e65
|
|
304 (do not change this comment) */
|