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