annotate src/unexcw.c @ 74811:5186094c35db

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