annotate src/unexcw.c @ 89927:d3ace4524f9e

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