summaryrefslogtreecommitdiff
path: root/src/unexcw.c
blob: fe2f120a01ff8a7f4d6174a79b7f71a7e70a7aac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* unexec() support for Cygwin;
   complete rewrite of xemacs Cygwin unexec() code

   Copyright (C) 2004-2023 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>
#include "unexec.h"
#include "lisp.h"
#include <stdio.h>
#include <fcntl.h>
#include <a.out.h>
#include <unistd.h>
#include <assert.h>

#define DOTEXE ".exe"

/*
** header for Windows executable files
*/
typedef struct
{
  FILHDR file_header;
  PEAOUTHDR file_optional_header;
  SCNHDR section_header[32];
} exe_header_t;

int debug_unexcw = 0;

/*
** Read the header from the executable into memory so we can more easily access it.
*/
static exe_header_t *
read_exe_header (int fd, exe_header_t * exe_header_buffer)
{
  int i;
  MAYBE_UNUSED int ret;

  assert (fd >= 0);
  assert (exe_header_buffer != 0);

  ret = lseek (fd, 0L, SEEK_SET);
  assert (ret != -1);

  ret =
    read (fd, &exe_header_buffer->file_header,
	  sizeof (exe_header_buffer->file_header));
  assert (ret == sizeof (exe_header_buffer->file_header));

  assert (exe_header_buffer->file_header.e_magic == 0x5a4d);
  assert (exe_header_buffer->file_header.nt_signature == 0x4550);
#ifdef __x86_64__
  assert (exe_header_buffer->file_header.f_magic == 0x8664);
#else
  assert (exe_header_buffer->file_header.f_magic == 0x014c);
#endif
  assert (exe_header_buffer->file_header.f_nscns > 0);
  assert (exe_header_buffer->file_header.f_nscns <=
          ARRAYELTS (exe_header_buffer->section_header));
  assert (exe_header_buffer->file_header.f_opthdr > 0);

  ret =
    read (fd, &exe_header_buffer->file_optional_header,
	  sizeof (exe_header_buffer->file_optional_header));
  assert (ret == sizeof (exe_header_buffer->file_optional_header));

#ifdef __x86_64__
  assert (exe_header_buffer->file_optional_header.magic == 0x020b);
#else
  assert (exe_header_buffer->file_optional_header.magic == 0x010b);
#endif

  for (i = 0; i < exe_header_buffer->file_header.f_nscns; ++i)
    {
      ret =
	read (fd, &exe_header_buffer->section_header[i],
	      sizeof (exe_header_buffer->section_header[i]));
      assert (ret == sizeof (exe_header_buffer->section_header[i]));
    }

  return (exe_header_buffer);
}

/*
** Fix the dumped emacs executable:
**
** - copy .data section data of interest from running executable into
**   output .exe file
**
** - convert .bss section into an initialized data section (like
**   .data) and copy .bss section data of interest from running
**   executable into output .exe file
*/
static void
fixup_executable (int fd)
{
  exe_header_t exe_header_buffer;
  exe_header_t *exe_header;
  int i;
  MAYBE_UNUSED int ret;
  int found_data = 0;
  int found_bss = 0;

  exe_header = read_exe_header (fd, &exe_header_buffer);
  assert (exe_header != 0);

  assert (exe_header->file_header.f_nscns > 0);
  for (i = 0; i < exe_header->file_header.f_nscns; ++i)
    {
      unsigned long start_address =
	exe_header->section_header[i].s_vaddr +
	exe_header->file_optional_header.ImageBase;
      unsigned long end_address =
	exe_header->section_header[i].s_vaddr +
	exe_header->file_optional_header.ImageBase +
	exe_header->section_header[i].s_paddr;
      if (debug_unexcw)
	printf ("%8s start %#lx end %#lx\n",
		exe_header->section_header[i].s_name,
		start_address, end_address);
      if (my_edata >= (char *) start_address
	  && my_edata < (char *) end_address)
	{
	  /* data section */
	  ret =
	    lseek (fd, (long) (exe_header->section_header[i].s_scnptr),
		   SEEK_SET);
	  assert (ret != -1);
	  ret =
	    write (fd, (char *) start_address,
		   my_edata - (char *) start_address);
	  assert (ret == my_edata - (char *) start_address);
	  ++found_data;
	  if (debug_unexcw)
	    printf ("         .data, mem start %#lx mem length %td\n",
		    start_address, my_edata - (char *) start_address);
	  if (debug_unexcw)
	    printf ("         .data, file start %d file length %d\n",
		    (int) exe_header->section_header[i].s_scnptr,
		    (int) exe_header->section_header[i].s_paddr);
	}
      else if (my_endbss >= (char *) start_address
	       && my_endbss < (char *) end_address)
	{
	  /* bss section */
	  ++found_bss;
	  if (exe_header->section_header[i].s_flags & 0x00000080)
	    {
	      /* convert uninitialized data section to initialized data section */
	      struct stat statbuf;
	      ret = fstat (fd, &statbuf);
	      assert (ret != -1);

	      exe_header->section_header[i].s_flags &= ~0x00000080;
	      exe_header->section_header[i].s_flags |= 0x00000040;

	      exe_header->section_header[i].s_scnptr =
		(statbuf.st_size +
		 exe_header->file_optional_header.FileAlignment) /
		exe_header->file_optional_header.FileAlignment *
		exe_header->file_optional_header.FileAlignment;

	      exe_header->section_header[i].s_size =
		(exe_header->section_header[i].s_paddr +
		 exe_header->file_optional_header.FileAlignment) /
		exe_header->file_optional_header.FileAlignment *
		exe_header->file_optional_header.FileAlignment;

              /* Make sure the generated bootstrap binary isn't
               * sparse.  NT doesn't use a file cache for sparse
               * executables, so if we bootstrap Emacs using a sparse
               * bootstrap-emacs.exe, bootstrap takes about twenty
               * times longer than it would otherwise.  */

              ret = posix_fallocate (fd,
                                     ( exe_header->section_header[i].s_scnptr +
                                       exe_header->section_header[i].s_size ),
                                     1);

              assert (ret != -1);

	      ret =
		lseek (fd,
		       (long) (exe_header->section_header[i].s_scnptr +
			       exe_header->section_header[i].s_size - 1),
		       SEEK_SET);
	      assert (ret != -1);
	      ret = write (fd, "", 1);
	      assert (ret == 1);

	      ret =
		lseek (fd,
		       (long) ((char *) &exe_header->section_header[i] -
			       (char *) exe_header), SEEK_SET);
	      assert (ret != -1);
	      ret =
		write (fd, &exe_header->section_header[i],
		       sizeof (exe_header->section_header[i]));
	      assert (ret == sizeof (exe_header->section_header[i]));
	      if (debug_unexcw)
		printf ("         seek to %ld, write %zu\n",
			(long) ((char *) &exe_header->section_header[i] -
				(char *) exe_header),
			sizeof (exe_header->section_header[i]));
	    }
	  /* write initialized data section */
	  ret =
	    lseek (fd, (long) (exe_header->section_header[i].s_scnptr),
		   SEEK_SET);
	  assert (ret != -1);
	  ret =
	    write (fd, (char *) start_address,
		   my_endbss - (char *) start_address);
	  assert (ret == (my_endbss - (char *) start_address));
	  if (debug_unexcw)
	    printf ("         .bss, mem start %#lx mem length %td\n",
		    start_address, my_endbss - (char *) start_address);
	  if (debug_unexcw)
	    printf ("         .bss, file start %d file length %d\n",
		    (int) exe_header->section_header[i].s_scnptr,
		    (int) exe_header->section_header[i].s_paddr);
	}
    }
  assert (found_bss == 1);
  assert (found_data == 1);
}

/*
** Windows likes .exe suffixes on executables.
*/
static char *
add_exe_suffix_if_necessary (const char *name, char *modified)
{
  int i = strlen (name);
  if (i <= (sizeof (DOTEXE) - 1))
    {
      sprintf (modified, "%s%s", name, DOTEXE);
    }
  else if (!strcasecmp (name + i - (sizeof (DOTEXE) - 1), DOTEXE))
    {
      strcpy (modified, name);
    }
  else
    {
      sprintf (modified, "%s%s", name, DOTEXE);
    }
  return (modified);
}

void
unexec (const char *outfile, const char *infile)
{
  char infile_buffer[FILENAME_MAX];
  char outfile_buffer[FILENAME_MAX];
  int fd_in;
  int fd_out;
  int ret;
  MAYBE_UNUSED int ret2;

  infile = add_exe_suffix_if_necessary (infile, infile_buffer);
  outfile = add_exe_suffix_if_necessary (outfile, outfile_buffer);

  fd_in = emacs_open (infile, O_RDONLY, 0);
  assert (fd_in >= 0);
  fd_out = emacs_open (outfile, O_RDWR | O_TRUNC | O_CREAT, 0755);
  assert (fd_out >= 0);
  for (;;)
    {
      char buffer[4096];
      ret = read (fd_in, buffer, sizeof (buffer));
      if (ret == 0)
	{
	  /* eof */
	  break;
	}
      assert (ret > 0);
      /* data */
      ret2 = write (fd_out, buffer, ret);
      assert (ret2 == ret);
    }
  ret = emacs_close (fd_in);
  assert (ret == 0);

  fixup_executable (fd_out);

  ret = emacs_close (fd_out);
  assert (ret == 0);
}