annotate src/unexcw.c @ 430:a5df635868b2 r21-2-23

Import from CVS: tag r21-2-23
author cvs
date Mon, 13 Aug 2007 11:29:08 +0200
parents 3ecd8885ac67
children abe6d1db359e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* unexec for GNU Emacs on Cygwin32.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1994, 1998 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to the Free
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 02111-1307, USA.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 /* This is a complete rewrite, some code snarfed from unexnt.c and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 unexec.c, Andy Piper (andy@xemacs.org) 13-1-98 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 #include <stdlib.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 #include "sysfile.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 #define PERROR(arg) perror(arg);exit(-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 #ifndef HAVE_A_OUT_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 unexec (char *, char *, void *, void *, void *)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 PERROR("cannot unexec() a.out.h not installed");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 #undef CONST
430
a5df635868b2 Import from CVS: tag r21-2-23
cvs
parents: 428
diff changeset
43 #ifndef MAX_PATH
a5df635868b2 Import from CVS: tag r21-2-23
cvs
parents: 428
diff changeset
44 #define MAX_PATH 260
a5df635868b2 Import from CVS: tag r21-2-23
cvs
parents: 428
diff changeset
45 #endif
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 #include <a.out.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 #define ALLOC_UNIT 0xFFFF
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 #define ALIGN_ALLOC(addr) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 /* To prevent zero-initialized variables from being placed into the bss
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 section, use non-zero values to represent an uninitialized state. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 #define UNINIT_PTR ((void *) 0xF0A0F0A0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 #define UNINIT_LONG (0xF0A0F0A0L)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 static void get_section_info (int a_out, char* a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 static void copy_executable_and_dump_data_section (int a_out, int a_new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 static void dup_file_area(int a_out, int a_new, long size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65 /* Cached info about the .data section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 void* data_start_va = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 unsigned long data_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 /* Cached info about the .bss section in the executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 void* bss_start = UNINIT_PTR;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 unsigned long bss_size = UNINIT_LONG;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 int sections_reversed = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 FILHDR f_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 PEAOUTHDR f_ohdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 SCNHDR f_data, f_bss, f_text, f_nextdata;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #define PERROR(arg) perror(arg);exit(-1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #define CHECK_AOUT_POS(a) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 if (lseek(a_out, 0, SEEK_CUR) != a) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 { \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 printf("we are at %lx, should be at %lx\n", \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 lseek(a_out, 0, SEEK_CUR), a); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 exit(-1); \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 /* Dump out .data and .bss sections into a new executable. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87 void unexec (char *out_name, char *in_name, void *start_data,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 void * d1, void * d2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 /* ugly nt hack - should be in lisp */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 int a_new, a_out = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 char new_name[MAX_PATH], a_name[MAX_PATH];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93 char *ptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 /* Make sure that the input and output filenames have the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 ".exe" extension...patch them up if they don't. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 strcpy (a_name, in_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 ptr = a_name + strlen (a_name) - 4;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 if (strcmp (ptr, ".exe"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 strcat (a_name, ".exe");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 strcpy (new_name, out_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 ptr = new_name + strlen (new_name) - 4;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 if (strcmp (ptr, ".exe"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 strcat (new_name, ".exe");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 /* We need to round off our heap to NT's allocation unit (64KB). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 /* round_heap (get_allocation_unit ()); */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 if ((a_new = open (new_name, O_WRONLY | O_TRUNC | O_CREAT | OPEN_BINARY,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 CREAT_MODE)) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 PERROR (new_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 /* Get the interesting section info, like start and size of .bss... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122 get_section_info (a_out, a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 copy_executable_and_dump_data_section (a_out, a_new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 close(a_out);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 close(a_new);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 /* Flip through the executable and cache the info necessary for dumping. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 static void get_section_info (int a_out, char* a_name)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 extern int my_ebss;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 /* From lastfile.c */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 extern char my_edata[];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142 if (f_hdr.e_magic != DOSMAGIC)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 PERROR("unknown exe header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 /* Check the NT header signature ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 if (f_hdr.nt_signature != NT_SIGNATURE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 PERROR("invalid nt header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 /* Flip through the sections for .data and .bss ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 if (f_hdr.f_opthdr > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 PERROR (a_name);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 /* Loop through .data & .bss section headers, copying them in.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 With newer lds these are reversed so we have to cope with both */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163 lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 strcmp (f_text.s_name, ".text"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 PERROR ("no .text section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 /* The .bss section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 (strcmp (f_bss.s_name, ".bss") && strcmp (f_bss.s_name, ".data")))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 PERROR ("no .bss / .data section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 /* check for reversed .bss and .data */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 if (!strcmp(f_bss.s_name, ".data"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 printf(".data and .bss reversed\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 sections_reversed = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 memcpy(&f_data, &f_bss, sizeof(f_bss));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 /* The .data section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 if (!sections_reversed)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 strcmp (f_data.s_name, ".data"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 PERROR ("no .data section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 ||
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 strcmp (f_bss.s_name, ".bss"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 PERROR ("no .bss section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 /* must keep bss data that we want to be blank as blank */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 printf("found bss - keeping %lx of %lx bytes\n", bss_size, f_ohdr.bsize);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 /* The .data section. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 /* We want to only write Emacs data back to the executable,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 not any of the library data (if library data is included,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219 then a dumped Emacs won't run on system versions other
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 than the one Emacs was dumped on). */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 data_size = (unsigned long)my_edata - (unsigned long)data_start_va;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 printf("found data - keeping %lx of %lx bytes\n", data_size, f_ohdr.dsize);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 /* The following data section - often .idata */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 if (read (a_out, &f_nextdata, sizeof (f_nextdata)) != sizeof (f_nextdata)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 &&
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 strcmp (&f_nextdata.s_name[2], "data"))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 PERROR ("no other data section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 /* The dump routines. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 copy_executable_and_dump_data_section (int a_out, int a_new)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 long size=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 unsigned long new_data_size, new_bss_size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 bss_padding, file_sz_change, data_padding=0,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 f_data_s_vaddr = f_data.s_vaddr,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 f_data_s_scnptr = f_data.s_scnptr,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243 f_bss_s_vaddr = f_bss.s_vaddr,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 f_nextdata_s_scnptr = f_nextdata.s_scnptr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 int i;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 void* empty_space;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 extern int static_heap_dumped;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 SCNHDR section;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 /* calculate new sizes f_ohdr.dsize is the total initialized data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 size on disk which is f_data.s_size + f_idata.s_size.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 f_ohdr.data_start is the base addres of all data and so should
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 not be changed. *.s_vaddr is the virtual address of the start
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 of the section normalzed from f_ohdr.ImageBase. *.s_paddr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 appears to be the number of bytes in the section actually used
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 (whereas *.s_size is aligned).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 bsize is now 0 since subsumed into .data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 f_data.s_vaddr is f_bss.s_vaddr
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 f_data.s_size is new dsize maybe.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 what about s_paddr & s_scnptr? */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 /* this is the amount the file increases in size */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 if (!sections_reversed)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 new_bss_size = f_data.s_vaddr - f_bss.s_vaddr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 data_padding = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 new_bss_size = f_nextdata.s_vaddr - f_bss.s_vaddr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 data_padding = (f_bss.s_vaddr - f_data.s_vaddr) - f_data.s_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 file_sz_change=new_bss_size + data_padding;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 new_data_size=f_ohdr.dsize + file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 if (!sections_reversed)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 f_data.s_vaddr = f_bss.s_vaddr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 f_data.s_paddr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 if (f_data.s_size + f_nextdata.s_size != f_ohdr.dsize)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 printf("section size doesn't tally with dsize %lx != %lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 f_data.s_size + f_nextdata.s_size, f_ohdr.dsize);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 f_data.s_size += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 lseek (a_new, 0, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 /* write file header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 f_hdr.f_symptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 f_hdr.f_nscns--;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 printf("writing file header\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 PERROR("failed to write file header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 /* write optional header fixing dsize & bsize*/
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 printf("writing optional header\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 printf("new data size is %lx, >= %lx\n", new_data_size,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 f_ohdr.dsize + f_ohdr.bsize);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 PERROR("new data size is < approx");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 f_ohdr.dsize=new_data_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 f_ohdr.bsize=0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 PERROR("failed to write optional header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 /* write text as is */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 printf("writing text header (unchanged)\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 PERROR("failed to write text header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 /* write new data header */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 printf("writing .data header\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 PERROR("failed to write data header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 printf("writing following data header\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 f_nextdata.s_scnptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 if (f_nextdata.s_lnnoptr != 0) f_nextdata.s_lnnoptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 if (f_nextdata.s_relptr != 0) f_nextdata.s_relptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 if (write(a_new, &f_nextdata, sizeof(f_nextdata)) != sizeof(f_nextdata))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 PERROR("failed to write nextdata header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 /* copy other section headers adjusting the file offset */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 for (i=0; i<(f_hdr.f_nscns-3); i++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 if (read (a_out, &section, sizeof (section)) != sizeof (section))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 PERROR ("no .data section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 section.s_scnptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 if (section.s_relptr != 0) section.s_relptr += file_sz_change;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 if (write(a_new, &section, sizeof(section)) != sizeof(section))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354 PERROR("failed to write data header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 /* dump bss to maintain offsets */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 memset(&f_bss, 0, sizeof(f_bss));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 PERROR("failed to write bss header");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 size=lseek(a_new, 0, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 CHECK_AOUT_POS(size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 /* copy eveything else until start of data */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 printf ("copying executable up to data section ... %lx bytes\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 dup_file_area(a_out, a_new, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 CHECK_AOUT_POS(f_data_s_scnptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 if (!sections_reversed)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 /* dump bss + padding between sections */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 if (write(a_new, bss_start, bss_size) != (int)bss_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 PERROR("failed to write bss section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 /* pad, needs to be zero */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 bss_padding = new_bss_size - bss_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 printf ("padding .bss ... %lx bytes\n", bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 empty_space = malloc(bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 memset(empty_space, 0, bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 PERROR("failed to write bss section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 free(empty_space);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 /* tell dumped version not to free pure heap */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 static_heap_dumped = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 /* Get a pointer to the raw data in our address space. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 printf ("dumping .data section... %lx bytes\n", data_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 if (write(a_new, data_start_va, data_size) != (int)data_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404 PERROR("failed to write data section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 /* were going to use free again ... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 static_heap_dumped = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 if (!sections_reversed)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 size = f_nextdata_s_scnptr - size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 dup_file_area(a_out, a_new, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
417 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 /* need to bad to bss with data in file */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 printf ("padding .data ... %lx bytes\n", data_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
420 size = (f_bss_s_vaddr - f_data_s_vaddr) - data_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
421 dup_file_area(a_out, a_new, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 /* dump bss + padding between sections */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 if (write(a_new, bss_start, bss_size) != (int)bss_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 PERROR("failed to write bss section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 /* pad, needs to be zero */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 bss_padding = new_bss_size - bss_size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 printf ("padding .bss ... %lx bytes\n", bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433 empty_space = malloc(bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 memset(empty_space, 0, bss_padding);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437 PERROR("failed to write bss section");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 free(empty_space);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 if (lseek(a_new, 0, SEEK_CUR) != f_nextdata.s_scnptr)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 printf("at %lx should be at %lx\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 lseek(a_new, 0, SEEK_CUR),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 f_nextdata.s_scnptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445 PERROR("file positioning error\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
446 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 lseek(a_out, f_nextdata_s_scnptr, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 CHECK_AOUT_POS(f_nextdata_s_scnptr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
452 /* now dump - nextdata don't need to do this cygwin ds is in .data! */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 printf ("dumping following data section... %lx bytes\n", f_nextdata.s_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455 dup_file_area(a_out,a_new,f_nextdata.s_size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 /* write rest of file */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 printf ("writing rest of file\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459 size = lseek(a_out, 0, SEEK_END);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
460 size = size - (f_nextdata_s_scnptr + f_nextdata.s_size); /* length remaining in a_out */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 lseek(a_out, f_nextdata_s_scnptr + f_nextdata.s_size, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 dup_file_area(a_out, a_new, size);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 /*
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 * copy from aout to anew
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
468 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469 static void dup_file_area(int a_out, int a_new, long size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471 char page[BUFSIZ];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 long n;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473 for (; size > 0; size -= sizeof (page))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475 n = size > sizeof (page) ? sizeof (page) : size;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 if (read (a_out, page, n) != n || write (a_new, page, n) != n)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 PERROR ("dump_out()");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 #if 0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 static void write_int_to_bss(int a_out, int a_new, void* va, void* newval)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 int cpos;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 cpos = lseek(a_new, 0, SEEK_CUR);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489 if (va < bss_start || va > bss_start + f_data.s_size)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 PERROR("address not in data space\n");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 lseek(a_new, f_data.s_scnptr + ((unsigned long)va -
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 (unsigned long)bss_start), SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 PERROR("failed to write int value");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 lseek(a_new, cpos, SEEK_SET);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 #endif /* HAVE_A_OUT_H */