annotate src/unexcw.c @ 359:8e84bee8ddd0 r21-1-9

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