annotate src/unexcw.c @ 263:727739f917cb r20-5b30

Import from CVS: tag r20-5b30
author cvs
date Mon, 13 Aug 2007 10:24:41 +0200
parents 677f6a0ee643
children b2472a1930f2
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
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
43 #include <windows.h>
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
44 #include <a.out.h>
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
45
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
46 #define ALLOC_UNIT 0xFFFF
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
47 #define ALLOC_MASK ~((unsigned long)(ALLOC_UNIT))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
48 #define ALIGN_ALLOC(addr) \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
49 ((((unsigned long)addr) + ALLOC_UNIT) & ALLOC_MASK)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
50
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
51 /* To prevent zero-initialized variables from being placed into the bss
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
52 section, use non-zero values to represent an uninitialized state. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
53 #define UNINIT_PTR ((void *) 0xF0A0F0A0)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
54 #define UNINIT_LONG (0xF0A0F0A0L)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
55
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
56 static void get_section_info (int a_out, char* a_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
57 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
58 static void dup_file_area(int a_out, int a_new, long size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
59 #if 0
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
60 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
61 #endif
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
62
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
63 /* Cached info about the .data section in the executable. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
64 void* data_start_va = UNINIT_PTR;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
65 unsigned long data_size = UNINIT_LONG;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
66
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
67 /* Cached info about the .bss section in the executable. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
68 void* bss_start = UNINIT_PTR;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
69 unsigned long bss_size = UNINIT_LONG;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
70 FILHDR f_hdr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
71 PEAOUTHDR f_ohdr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
72 SCNHDR f_data, f_bss, f_text, f_idata;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
73
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
74 #define PERROR(arg) perror(arg);exit(-1)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
75 #define CHECK_AOUT_POS(a) \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
76 if (lseek(a_out, 0, SEEK_CUR) != a) \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
77 { \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
78 printf("we are at %lx, should be at %lx\n", \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
79 lseek(a_out, 0, SEEK_CUR), a); \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
80 exit(-1); \
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
81 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
82
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
83 /* Dump out .data and .bss sections into a new executable. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
84 void unexec (char *out_name, char *in_name, void *start_data,
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
85 void * d1, void * d2)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
86 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
87 /* ugly nt hack - should be in lisp */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
88 int a_new, a_out = -1;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
89 char new_name[MAX_PATH], a_name[MAX_PATH];
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
90 char *ptr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
91
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
92 /* Make sure that the input and output filenames have the
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
93 ".exe" extension...patch them up if they don't. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
94 strcpy (a_name, in_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
95 ptr = a_name + strlen (a_name) - 4;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
96 if (strcmp (ptr, ".exe"))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
97 strcat (a_name, ".exe");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
98
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
99 strcpy (new_name, out_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
100 ptr = new_name + strlen (new_name) - 4;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
101 if (strcmp (ptr, ".exe"))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
102 strcat (new_name, ".exe");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
103
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
104 /* We need to round off our heap to NT's allocation unit (64KB). */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
105 /* round_heap (get_allocation_unit ()); */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
106
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
107 if (a_name && (a_out = open (a_name, O_RDONLY | OPEN_BINARY)) < 0)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
108 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
109 PERROR (a_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
110 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
111
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
112 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
113 CREAT_MODE)) < 0)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
114 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
115 PERROR (new_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
116 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
117
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
118 /* Get the interesting section info, like start and size of .bss... */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
119 get_section_info (a_out, a_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
120
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
121 copy_executable_and_dump_data_section (a_out, a_new);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
122
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
123 close(a_out);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
124 close(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
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
127 /* Flip through the executable and cache the info necessary for dumping. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
128 static void get_section_info (int a_out, char* a_name)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
129 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
130 extern int my_ebss;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
131 /* From lastfile.c */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
132 extern char my_edata[];
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
133
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
134 if (read (a_out, &f_hdr, sizeof (f_hdr)) != sizeof (f_hdr))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
135 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
136 PERROR (a_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
137 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
138
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
139 if (f_hdr.e_magic != DOSMAGIC)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
140 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
141 PERROR("unknown exe header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
142 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
143
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
144 /* Check the NT header signature ... */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
145 if (f_hdr.nt_signature != NT_SIGNATURE)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
146 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
147 PERROR("invalid nt header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
148 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
149
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
150 /* Flip through the sections for .data and .bss ... */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
151 if (f_hdr.f_opthdr > 0)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
152 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
153 if (read (a_out, &f_ohdr, AOUTSZ) != AOUTSZ)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
154 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
155 PERROR (a_name);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
156 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
157 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
158 /* Loop through .data & .bss section headers, copying them in */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
159 lseek (a_out, sizeof (f_hdr) + f_hdr.f_opthdr, 0);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
160
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
161 if (read (a_out, &f_text, sizeof (f_text)) != sizeof (f_text)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
162 &&
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
163 strcmp (f_text.s_name, ".text"))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
164 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
165 PERROR ("no .text section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
166 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
167
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
168 /* The .bss section. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
169 if (read (a_out, &f_bss, sizeof (f_bss)) != sizeof (f_bss)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
170 &&
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
171 strcmp (f_bss.s_name, ".bss"))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
172 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
173 PERROR ("no .bss section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
174 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
175
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
176 bss_start = (void *) ((char*)f_ohdr.ImageBase + f_bss.s_vaddr);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
177 bss_size = (unsigned long)((char*)&my_ebss-(char*)bss_start);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
178
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
179 /* must keep bss data that we want to be blank as blank */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
180 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
181
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
182 /* The .data section. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
183 if (read (a_out, &f_data, sizeof (f_data)) != sizeof (f_data)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
184 &&
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
185 strcmp (f_data.s_name, ".data"))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
186 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
187 PERROR ("no .data section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
188 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
189
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
190 /* The .data section. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
191 data_start_va = (void *) ((char*)f_ohdr.ImageBase + f_data.s_vaddr);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
192
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
193 /* We want to only write Emacs data back to the executable,
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
194 not any of the library data (if library data is included,
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
195 then a dumped Emacs won't run on system versions other
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
196 than the one Emacs was dumped on). */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
197 data_size = (unsigned long)my_edata - (unsigned long)data_start_va;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
198
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
199 /* The .idata section. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
200 if (read (a_out, &f_idata, sizeof (f_idata)) != sizeof (f_idata)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
201 &&
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 251
diff changeset
202 strcmp (f_idata.s_name, ".rdata"))
251
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
203 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
204 PERROR ("no .idata section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
205 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
206 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
207
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
208 /* The dump routines. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
209
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
210 static void
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
211 copy_executable_and_dump_data_section (int a_out, int a_new)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
212 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
213 long size=0;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
214 unsigned long new_data_size, new_bss_size, f_data_s_vaddr,
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
215 file_sz_change, f_data_s_scnptr, bss_padding;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
216 int i;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
217 void* empty_space;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
218 extern int static_heap_dumped;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
219 SCNHDR section;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
220 /* calculate new sizes f_ohdr.dsize is the total initalized data
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
221 size on disk which is f_data.s_size + f_idata.s_size.
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
222 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
223 not be changed. *.s_vaddr is the virtual address of the start
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
224 of the section normalzed from f_ohdr.ImageBase. *.s_paddr
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
225 appears to be the number of bytes in the section actually used
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
226 (whereas *.s_size is aligned).
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
227
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
228 bsize is now 0 since subsumed into .data
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
229 dsize is dsize + (f_data.s_vaddr - f_bss.s_vaddr)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
230 f_data.s_vaddr is f_bss.s_vaddr
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
231 f_data.s_size is new dsize maybe.
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
232 what about s_paddr & s_scnptr? */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
233 /* this is the amount the file increases in size */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
234 new_bss_size=f_data.s_vaddr - f_bss.s_vaddr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
235 file_sz_change=new_bss_size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
236 new_data_size=f_ohdr.dsize + new_bss_size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
237 f_data_s_scnptr = f_data.s_scnptr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
238 f_data_s_vaddr = f_data.s_vaddr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
239 f_data.s_vaddr = f_bss.s_vaddr;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
240 f_data.s_paddr += new_bss_size;
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 251
diff changeset
241 #if 0
251
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
242 if (f_data.s_size + f_idata.s_size != f_ohdr.dsize)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
243 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
244 printf("section size doesn't tally with dsize %lx != %lx\n",
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
245 f_data.s_size + f_idata.s_size, f_ohdr.dsize);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
246 }
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 251
diff changeset
247 #endif
251
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
248 f_data.s_size += new_bss_size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
249 lseek (a_new, 0, SEEK_SET);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
250 /* write file header */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
251 f_hdr.f_symptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
252 f_hdr.f_nscns--;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
253 printf("writing file header\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
254 if (write(a_new, &f_hdr, sizeof(f_hdr)) != sizeof(f_hdr))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
255 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
256 PERROR("failed to write file header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
257 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
258 /* write optional header fixing dsize & bsize*/
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
259 printf("writing optional header\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
260 printf("new data size is %lx, >= %lx\n", new_data_size,
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
261 f_ohdr.dsize + f_ohdr.bsize);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
262 if (new_data_size < f_ohdr.dsize + f_ohdr.bsize )
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
263 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
264 PERROR("new data size is < approx");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
265 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
266 f_ohdr.dsize=new_data_size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
267 f_ohdr.bsize=0;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
268 if (write(a_new, &f_ohdr, sizeof(f_ohdr)) != sizeof(f_ohdr))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
269 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
270 PERROR("failed to write optional header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
271 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
272 /* write text as is */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
273 printf("writing text header (unchanged)\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
274
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
275 if (write(a_new, &f_text, sizeof(f_text)) != sizeof(f_text))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
276 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
277 PERROR("failed to write text header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
278 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
279
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
280 /* write new data header */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
281 printf("writing .data header\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
282
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
283 if (write(a_new, &f_data, sizeof(f_data)) != sizeof(f_data))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
284 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
285 PERROR("failed to write data header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
286 }
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 251
diff changeset
287
251
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
288 printf("writing .idata header\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
289 f_idata.s_scnptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
290 if (f_idata.s_lnnoptr != 0) f_idata.s_lnnoptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
291 if (f_idata.s_relptr != 0) f_idata.s_relptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
292 if (write(a_new, &f_idata, sizeof(f_idata)) != sizeof(f_idata))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
293 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
294 PERROR("failed to write idata header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
295 }
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 251
diff changeset
296
251
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
297 /* copy other section headers adjusting the file offset */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
298 for (i=0; i<(f_hdr.f_nscns-3); i++)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
299 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
300 if (read (a_out, &section, sizeof (section)) != sizeof (section))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
301 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
302 PERROR ("no .data section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
303 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
304
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
305 section.s_scnptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
306 if (section.s_lnnoptr != 0) section.s_lnnoptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
307 if (section.s_relptr != 0) section.s_relptr += file_sz_change;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
308
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
309 if (write(a_new, &section, sizeof(section)) != sizeof(section))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
310 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
311 PERROR("failed to write data header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
312 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
313 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
314
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
315 /* dump bss to maintain offsets */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
316 memset(&f_bss, 0, sizeof(f_bss));
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
317 if (write(a_new, &f_bss, sizeof(f_bss)) != sizeof(f_bss))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
318 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
319 PERROR("failed to write bss header");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
320 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
321
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
322 size=lseek(a_new, 0, SEEK_CUR);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
323 CHECK_AOUT_POS(size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
324
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
325 /* copy eveything else until start of data */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
326 size = f_data_s_scnptr - lseek (a_out, 0, SEEK_CUR);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
327
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
328 printf ("copying executable up to data section ... %lx bytes\n",
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
329 size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
330 dup_file_area(a_out, a_new, size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
331
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
332 CHECK_AOUT_POS(f_data_s_scnptr);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
333
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
334 /* dump bss + padding between sections */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
335 printf ("dumping .bss into executable... %lx bytes\n", bss_size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
336 if (write(a_new, bss_start, bss_size) != (int)bss_size)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
337 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
338 PERROR("failed to write bss section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
339 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
340
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
341 /* pad, needs to be zero */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
342 bss_padding = new_bss_size - bss_size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
343 printf ("padding .bss ... %lx bytes\n", bss_padding);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
344 empty_space = malloc(bss_padding);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
345 memset(empty_space, 0, bss_padding);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
346 if (write(a_new, empty_space, bss_padding) != (int)bss_padding)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
347 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
348 PERROR("failed to write bss section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
349 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
350 free(empty_space);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
351
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
352 /* tell dumped version not to free pure heap */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
353 static_heap_dumped = 1;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
354 /* Get a pointer to the raw data in our address space. */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
355 printf ("dumping .data section... %lx bytes\n", data_size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
356 if (write(a_new, data_start_va, data_size) != (int)data_size)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
357 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
358 PERROR("failed to write data section");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
359 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
360 /* were going to use free again ... */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
361 static_heap_dumped = 0;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
362
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
363 size = lseek(a_out, f_data_s_scnptr + data_size, SEEK_SET);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
364 size = f_idata.s_scnptr - size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
365 dup_file_area(a_out, a_new, size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
366
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
367 // lseek(a_out, f_idata.s_scnptr, SEEK_CUR);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
368 CHECK_AOUT_POS(f_idata.s_scnptr);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
369 /* now dump - idata don't need to do this cygwin ds is in .data! */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
370 printf ("dumping .idata section... %lx bytes\n", f_idata.s_size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
371
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
372 dup_file_area(a_out,a_new,f_idata.s_size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
373
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
374 /* write rest of file */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
375 printf ("writing rest of file\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
376 size = lseek(a_out, 0, SEEK_END);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
377 size = size - (f_idata.s_scnptr + f_idata.s_size); /* length remaining in a_out */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
378 lseek(a_out, f_idata.s_scnptr + f_idata.s_size, SEEK_SET);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
379
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
380 dup_file_area(a_out, a_new, size);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
381 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
382
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
383 /*
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
384 * copy from aout to anew
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
385 */
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
386 static void dup_file_area(int a_out, int a_new, long size)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
387 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
388 char page[BUFSIZ];
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
389 long n;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
390 for (; size > 0; size -= sizeof (page))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
391 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
392 n = size > sizeof (page) ? sizeof (page) : size;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
393 if (read (a_out, page, n) != n || write (a_new, page, n) != n)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
394 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
395 PERROR ("dump_out()");
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 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
399
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
400 #if 0
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
401 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
402 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
403 int cpos;
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
404
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
405 cpos = lseek(a_new, 0, SEEK_CUR);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
406 if (va < bss_start || va > bss_start + f_data.s_size)
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
407 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
408 PERROR("address not in data space\n");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
409 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
410 lseek(a_new, f_data.s_scnptr + ((unsigned long)va -
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
411 (unsigned long)bss_start), SEEK_SET);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
412 if (write(a_new, newval, sizeof(int)) != (int)sizeof(int))
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
413 {
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
414 PERROR("failed to write int value");
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
415 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
416 lseek(a_new, cpos, SEEK_SET);
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
417 }
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
418 #endif
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
419
677f6a0ee643 Import from CVS: tag r20-5b24
cvs
parents:
diff changeset
420 #endif /* HAVE_A_OUT_H */