annotate src/unexelfsgi.c @ 394:7d59cb494b73 r21-2-12

Import from CVS: tag r21-2-12
author cvs
date Mon, 13 Aug 2007 11:11:37 +0200
parents cc15677e0335
children 74fd4e045ea6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
4 This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
7 under the terms of the GNU General Public License as published by the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
9 later version.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
14 for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
16 You should have received a copy of the GNU General Public License
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
19 Boston, MA 02111-1307, USA. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
21 /* Synched up with: FSF 19.31. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 * unexec.c - Convert a running program into an a.out file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 * Author: Spencer W. Thomas
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 * Computer Science Dept.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 * University of Utah
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 * Date: Tue Mar 2 1982
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 * Modified heavily since then.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 * Synopsis:
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
34 * unexec (new_name, a_name, data_start, bss_start, entry_address)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
35 * char *new_name, *a_name;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
36 * unsigned data_start, bss_start, entry_address;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
37 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
38 * Takes a snapshot of the program and makes an a.out format file in the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
39 * file named by the string argument new_name.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
40 * If a_name is non-NULL, the symbol table will be taken from the given file.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
41 * On some machines, an existing a_name file is required.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
42 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
43 * The boundaries within the a.out file may be adjusted with the data_start
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
44 * and bss_start arguments. Either or both may be given as 0 for defaults.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
45 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
46 * Data_start gives the boundary between the text segment and the data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
47 * segment of the program. The text segment can contain shared, read-only
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
48 * program code and literal data, while the data segment is always unshared
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
49 * and unprotected. Data_start gives the lowest unprotected address.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
50 * The value you specify may be rounded down to a suitable boundary
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
51 * as required by the machine you are using.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
52 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
53 * Specifying zero for data_start means the boundary between text and data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
54 * should not be the same as when the program was loaded.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
55 * If NO_REMAP is defined, the argument data_start is ignored and the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
56 * segment boundaries are never changed.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 *
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
58 * Bss_start indicates how much of the data segment is to be saved in the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
59 * a.out file and restored when the program is executed. It gives the lowest
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
60 * unsaved address, and is rounded up to a page boundary. The default when 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
61 * is given assumes that the entire data segment is to be stored, including
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
62 * the previous data and bss as well as any additional storage allocated with
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
63 * break (2).
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
64 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
65 * The new file is set up to start at entry_address.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
66 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
67 * If you make improvements I'd like to get them too.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
68 * harpo!utah-cs!thomas, thomas@Utah-20
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
69 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
70 */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
71
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
72 /* Even more heavily modified by james@bigtex.cactus.org of Dell Computer Co.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
73 * ELF support added.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
74 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
75 * Basic theory: the data space of the running process needs to be
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
76 * dumped to the output file. Normally we would just enlarge the size
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
77 * of .data, scooting everything down. But we can't do that in ELF,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
78 * because there is often something between the .data space and the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
79 * .bss space.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
80 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
81 * In the temacs dump below, notice that the Global Offset Table
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
82 * (.got) and the Dynamic link data (.dynamic) come between .data1 and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
83 * .bss. It does not work to overlap .data with these fields.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 *
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
85 * The solution is to create a new .data segment. This segment is
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
86 * filled with data from the current process. Since the contents of
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
87 * various sections refer to sections by index, the new .data segment
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
88 * is made the last in the table to avoid changing any existing index.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
89
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
90 * This is an example of how the section headers are changed. "Addr"
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
91 * is a process virtual address. "Offset" is a file offset.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
92
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
93 raid:/nfs/raid/src/dist-18.56/src> dump -h temacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
94
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
95 temacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
96
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
97 **** SECTION HEADER TABLE ****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
98 [No] Type Flags Addr Offset Size Name
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
99 Link Info Adralgn Entsize
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
100
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
101 [1] 1 2 0x80480d4 0xd4 0x13 .interp
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
102 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
103
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
104 [2] 5 2 0x80480e8 0xe8 0x388 .hash
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
105 3 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
106
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
107 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
108 4 1 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
109
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
110 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
111 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
112
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
113 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
114 3 7 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
115
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
116 [6] 1 6 0x8049348 0x1348 0x3 .init
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
117 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
118
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
119 [7] 1 6 0x804934c 0x134c 0x680 .plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
120 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
121
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
122 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
123 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
124
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
125 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
126 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
127
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
128 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
129 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
130
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
131 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
132 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
133
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
134 [12] 1 3 0x8088330 0x3f330 0x20afc .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
135 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
136
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
137 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
138 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
139
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
140 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
141 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
142
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
143 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
144 4 0 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
145
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
146 [16] 8 3 0x80a98f4 0x608f4 0x449c .bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
147 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
148
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
149 [17] 2 0 0 0x608f4 0x9b90 .symtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
150 18 371 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
151
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
152 [18] 3 0 0 0x6a484 0x8526 .strtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
153 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
154
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
155 [19] 3 0 0 0x729aa 0x93 .shstrtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
156 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
157
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
158 [20] 1 0 0 0x72a3d 0x68b7 .comment
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
159 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
160
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
161 raid:/nfs/raid/src/dist-18.56/src> dump -h xemacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
162
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
163 xemacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
164
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
165 **** SECTION HEADER TABLE ****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
166 [No] Type Flags Addr Offset Size Name
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
167 Link Info Adralgn Entsize
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
168
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
169 [1] 1 2 0x80480d4 0xd4 0x13 .interp
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
170 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
171
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
172 [2] 5 2 0x80480e8 0xe8 0x388 .hash
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
173 3 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
174
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
175 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
176 4 1 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
177
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
178 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
179 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
180
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
181 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
182 3 7 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
183
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
184 [6] 1 6 0x8049348 0x1348 0x3 .init
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
185 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
186
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
187 [7] 1 6 0x804934c 0x134c 0x680 .plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
188 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
189
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
190 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
191 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
192
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
193 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
194 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
195
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
196 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
197 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
198
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
199 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
200 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
201
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
202 [12] 1 3 0x8088330 0x3f330 0x20afc .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
203 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
204
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
205 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
206 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
207
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
208 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
209 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
210
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
211 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
212 4 0 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
213
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
214 [16] 8 3 0x80c6800 0x7d800 0 .bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
215 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
216
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
217 [17] 2 0 0 0x7d800 0x9b90 .symtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
218 18 371 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
219
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
220 [18] 3 0 0 0x87390 0x8526 .strtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
221 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
222
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
223 [19] 3 0 0 0x8f8b6 0x93 .shstrtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
224 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
225
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
226 [20] 1 0 0 0x8f949 0x68b7 .comment
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
227 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
228
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
229 [21] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
230 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
231
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
232 * This is an example of how the file header is changed. "Shoff" is
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
233 * the section header offset within the file. Since that table is
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
234 * after the new .data section, it is moved. "Shnum" is the number of
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
235 * sections, which we increment.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 *
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
237 * "Phoff" is the file offset to the program header. "Phentsize" and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
238 * "Shentsz" are the program and section header entries sizes respectively.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
239 * These can be larger than the apparent struct sizes.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
240
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
241 raid:/nfs/raid/src/dist-18.56/src> dump -f temacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
242
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
243 temacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
244
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
245 **** ELF HEADER ****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
246 Class Data Type Machine Version
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
247 Entry Phoff Shoff Flags Ehsize
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
248 Phentsize Phnum Shentsz Shnum Shstrndx
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
249
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
250 1 1 2 3 1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
251 0x80499cc 0x34 0x792f4 0 0x34
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
252 0x20 5 0x28 21 19
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
253
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
254 raid:/nfs/raid/src/dist-18.56/src> dump -f xemacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
255
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
256 xemacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
257
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
258 **** ELF HEADER ****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
259 Class Data Type Machine Version
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
260 Entry Phoff Shoff Flags Ehsize
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
261 Phentsize Phnum Shentsz Shnum Shstrndx
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
262
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
263 1 1 2 3 1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
264 0x80499cc 0x34 0x96200 0 0x34
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
265 0x20 5 0x28 22 19
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
266
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
267 * These are the program headers. "Offset" is the file offset to the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
268 * segment. "Vaddr" is the memory load address. "Filesz" is the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
269 * segment size as it appears in the file, and "Memsz" is the size in
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
270 * memory. Below, the third segment is the code and the fourth is the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
271 * data: the difference between Filesz and Memsz is .bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
272
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
273 raid:/nfs/raid/src/dist-18.56/src> dump -o temacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
274
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
275 temacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
276 ***** PROGRAM EXECUTION HEADER *****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
277 Type Offset Vaddr Paddr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
278 Filesz Memsz Flags Align
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
279
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
280 6 0x34 0x8048034 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
281 0xa0 0xa0 5 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
282
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
283 3 0xd4 0 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
284 0x13 0 4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
285
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
286 1 0x34 0x8048034 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
287 0x3f2f9 0x3f2f9 5 0x1000
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
288
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
289 1 0x3f330 0x8088330 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
290 0x215c4 0x25a60 7 0x1000
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
291
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
292 2 0x60874 0x80a9874 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
293 0x80 0 7 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
294
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
295 raid:/nfs/raid/src/dist-18.56/src> dump -o xemacs
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
296
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
297 xemacs:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
298 ***** PROGRAM EXECUTION HEADER *****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
299 Type Offset Vaddr Paddr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
300 Filesz Memsz Flags Align
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
301
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
302 6 0x34 0x8048034 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
303 0xa0 0xa0 5 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
304
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
305 3 0xd4 0 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
306 0x13 0 4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
307
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
308 1 0x34 0x8048034 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
309 0x3f2f9 0x3f2f9 5 0x1000
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
310
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
311 1 0x3f330 0x8088330 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
312 0x3e4d0 0x3e4d0 7 0x1000
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
313
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
314 2 0x60874 0x80a9874 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
315 0x80 0 7 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
316
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
317
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
320 /* Modified by wtien@urbana.mcd.mot.com of Motorola Inc.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
321 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
322 * The above mechanism does not work if the unexeced ELF file is being
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
323 * re-layout by other applications (such as `strip'). All the applications
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
324 * that re-layout the internal of ELF will layout all sections in ascending
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
325 * order of their file offsets. After the re-layout, the data2 section will
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
326 * still be the LAST section in the section header vector, but its file offset
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
327 * is now being pushed far away down, and causes part of it not to be mapped
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
328 * in (ie. not covered by the load segment entry in PHDR vector), therefore
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
329 * causes the new binary to fail.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
330 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
331 * The solution is to modify the unexec algorithm to insert the new data2
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
332 * section header right before the new bss section header, so their file
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
333 * offsets will be in the ascending order. Since some of the section's (all
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
334 * sections AFTER the bss section) indexes are now changed, we also need to
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
335 * modify some fields to make them point to the right sections. This is done
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
336 * by macro PATCH_INDEX. All the fields that need to be patched are:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
337 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
338 * 1. ELF header e_shstrndx field.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
339 * 2. section header sh_link and sh_info field.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
340 * 3. symbol table entry st_shndx field.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
341 *
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
342 * The above example now should look like:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
343
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
344 **** SECTION HEADER TABLE ****
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
345 [No] Type Flags Addr Offset Size Name
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
346 Link Info Adralgn Entsize
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
347
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
348 [1] 1 2 0x80480d4 0xd4 0x13 .interp
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
349 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
350
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
351 [2] 5 2 0x80480e8 0xe8 0x388 .hash
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
352 3 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
353
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
354 [3] 11 2 0x8048470 0x470 0x7f0 .dynsym
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
355 4 1 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
356
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
357 [4] 3 2 0x8048c60 0xc60 0x3ad .dynstr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
358 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
359
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
360 [5] 9 2 0x8049010 0x1010 0x338 .rel.plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
361 3 7 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
362
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
363 [6] 1 6 0x8049348 0x1348 0x3 .init
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
364 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
365
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
366 [7] 1 6 0x804934c 0x134c 0x680 .plt
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
367 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
368
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
369 [8] 1 6 0x80499cc 0x19cc 0x3c56f .text
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
370 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
372 [9] 1 6 0x8085f3c 0x3df3c 0x3 .fini
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
373 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
374
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
375 [10] 1 2 0x8085f40 0x3df40 0x69c .rodata
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
376 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
377
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
378 [11] 1 2 0x80865dc 0x3e5dc 0xd51 .rodata1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
379 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
380
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
381 [12] 1 3 0x8088330 0x3f330 0x20afc .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
382 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
383
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
384 [13] 1 3 0x80a8e2c 0x5fe2c 0x89d .data1
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
385 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
386
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
387 [14] 1 3 0x80a96cc 0x606cc 0x1a8 .got
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
388 0 0 0x4 0x4
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
389
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
390 [15] 6 3 0x80a9874 0x60874 0x80 .dynamic
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
391 4 0 0x4 0x8
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
392
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
393 [16] 1 3 0x80a98f4 0x608f4 0x1cf0c .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
394 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
395
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
396 [17] 8 3 0x80c6800 0x7d800 0 .bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
397 0 0 0x4 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
398
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
399 [18] 2 0 0 0x7d800 0x9b90 .symtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
400 19 371 0x4 0x10
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
401
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
402 [19] 3 0 0 0x87390 0x8526 .strtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
403 0 0 0x1 0
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
405 [20] 3 0 0 0x8f8b6 0x93 .shstrtab
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
406 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
407
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
408 [21] 1 0 0 0x8f949 0x68b7 .comment
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
409 0 0 0x1 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
410
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
411 */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
412
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
413 /* More mods, by Jack Repenning <jackr@sgi.com>, Fri Aug 11 15:45:52 1995
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
414
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
415 Same algorithm as immediately above. However, the detailed
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
416 calculations of the various locations needed significant
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
417 overhaul.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
418
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
419 At the point of the old .bss, the file offsets and the memory
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
420 addresses do distinct, slightly snaky things:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
421
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
422 offset of .bss is meaningless and unpredictable
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
423 addr of .bss is meaningful
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
424 alignment of .bss is important to addr, so there may be a small
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
425 gap in address range before start of bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
426 offset of next section is rounded up modulo 0x1000
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
427 the hole so-introduced is zero-filled, so it can be mapped in as
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
428 the first partial-page of bss (the rest of the bss is mapped from
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
429 /dev/zero)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
430 I suppose you could view this not as a hole, but as the beginning
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
431 of the bss, actually present in the file. But you should not
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
432 push that worldview too far, as the linker still knows that the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
433 "offset" claimed for the bss is unused, and seems not always
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
434 careful about setting it.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
435
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
436 We are doing all our tricks at this same rather complicated
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
437 location (isn't life fun?):
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
438
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
439 insert a new data section to contain now-initialized old bss and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
440 heap
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
441 define a zero-length bss just so there is one
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
442
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
443 The offset of the new data section is dictated by its current
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
444 address (which, of course, we want also to be its addr): the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
445 loader maps in the whole file region containing old data, rodata,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
446 got, and new data as a single mapped segment, starting at the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
447 address of the first chunk; the rest have to be laid out in the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
448 file such that the map into the right spots. That is:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
449
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
450 offset(newdata) ==
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
451 addrInRunningMemory(newdata)-aIRM(olddata)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
452 + offset(oldData)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
453
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
454 This would not necessarily match the oldbss offset, even if it
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
455 were carefully calculated! We must compute this.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
456
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
457 The linker that built temacs has also already arranged that
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
458 olddata is properly page-aligned (not necessarily beginning on a
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
459 page, but rather that a page's worth of the low bits of addr and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
460 offset match). We preserve this.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
461
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
462 addr(bss) is alignment-constrained from the end of the new data.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
463 Since we base endof(newdata) on sbrk(), we have a page boundary
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
464 (in both offset and addr) and meet any alignment constraint,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
465 needing no alignment adjustment of this location and no
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
466 mini-hole. Or, if you like, we've allowed sbrk() to "compute"
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
467 the mini-hole size for us.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
468
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
469 That puts newbss beginning on a page boundary, both in offset and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
470 addr. (offset(bss) is still meaningless, but what the heck,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
471 we'll fix it up.)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
472
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
473 Since newbss has zero length, and its offset (however
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
474 meaningless) is page aligned, we place the next section exactly
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
475 there, with no hole needed to restore page alignment.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
476
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
477 So, the shift for all sections beyond the playing field is:
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
478
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
479 new_bss_addr - roundup(old_bss_addr,0x1000)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
480
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
481 */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
482 /* Still more mods... Olivier Galibert 19971705
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
483 - support for .sbss section (automagically changed to data without
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
484 name change)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
485 - support for 64bits ABI (will need a bunch of fixes in the rest
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
486 of the code before it works
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
487 */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
488
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 #include <memory.h>
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
493 #include <string.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 #include <elf.h>
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
498 #include <sym.h> /* for HDRR declaration */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 #include <sys/mman.h>
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
500 #include <config.h>
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
501 #include "lisp.h"
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
503 /* in 64bits mode, use 64bits elf */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
504 #ifdef _ABI64
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
505 typedef Elf64_Shdr l_Elf_Shdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
506 typedef Elf64_Phdr l_Elf_Phdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
507 typedef Elf64_Ehdr l_Elf_Ehdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
508 typedef Elf64_Addr l_Elf_Addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
509 typedef Elf64_Word l_Elf_Word;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
510 typedef Elf64_Off l_Elf_Off;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
511 typedef Elf64_Sym l_Elf_Sym;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
512 #else
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
513 typedef Elf32_Shdr l_Elf_Shdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
514 typedef Elf32_Phdr l_Elf_Phdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
515 typedef Elf32_Ehdr l_Elf_Ehdr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
516 typedef Elf32_Addr l_Elf_Addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
517 typedef Elf32_Word l_Elf_Word;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
518 typedef Elf32_Off l_Elf_Off;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
519 typedef Elf32_Sym l_Elf_Sym;
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
520 #endif
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
521
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
522
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 /* Get the address of a particular section or program header entry,
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
524 * accounting for the size of the entries.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
525 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 #define OLD_SECTION_H(n) \
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
528 (*(l_Elf_Shdr *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 #define NEW_SECTION_H(n) \
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
530 (*(l_Elf_Shdr *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 #define OLD_PROGRAM_H(n) \
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
532 (*(l_Elf_Phdr *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 #define NEW_PROGRAM_H(n) \
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
534 (*(l_Elf_Phdr *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 #define PATCH_INDEX(n) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 do { \
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
538 if ((n) >= old_bss_index) \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (n)++; } while (0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 typedef unsigned char byte;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 /* Round X up to a multiple of Y. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
544 static int
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
545 round_up (int x, int y)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 int rem = x % y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 if (rem == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 return x;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 return x - rem + y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 /* Return the index of the section named NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 SECTION_NAMES, FILE_NAME and FILE_H give information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 about the file we are looking in.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 If we don't find the section NAME, that is a fatal error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 if NOERROR is 0; we return -1 if NOERROR is nonzero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 static int
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
561 find_section (char *name,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
562 char *section_names,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
563 char *file_name,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
564 l_Elf_Ehdr *old_file_h,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
565 l_Elf_Shdr *old_section_h,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
566 int noerror)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 int idx;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
570 for (idx = 1; idx < old_file_h->e_shnum; idx++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
572 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 fprintf (stderr, "Looking for %s - found %s\n", name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 section_names + OLD_SECTION_H (idx).sh_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 if (!strcmp (section_names + OLD_SECTION_H (idx).sh_name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
577 name))
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
578 break;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
579 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
580 if (idx == old_file_h->e_shnum)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
581 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
582 if (noerror)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
583 return -1;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
584 else
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
585 fatal ("Can't find .bss in %s.\n", file_name);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
587
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
588 return idx;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 * unexec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 * driving logic.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 * In ELF, this works by replacing the old .bss section with a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 * .data section, and inserting an empty .bss immediately afterwards.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 */
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
600 int
394
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
601 unexec (char *new_name,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
602 char *old_name,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
603 uintptr_t data_start,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
604 uintptr_t bss_start,
7d59cb494b73 Import from CVS: tag r21-2-12
cvs
parents: 371
diff changeset
605 uintptr_t entry_address)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 {
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
607 extern uintptr_t bss_end;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
608 int new_file, old_file, new_file_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
610 /* Pointers to the base of the image of the two files. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 caddr_t old_base, new_base;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
613 /* Pointers to the file, program and section headers for the old and new
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
614 files. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
615 l_Elf_Ehdr *old_file_h, *new_file_h;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
616 l_Elf_Phdr *old_program_h, *new_program_h;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
617 l_Elf_Shdr *old_section_h, *new_section_h;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
618
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
619 /* Point to the section name table in the old file. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
620 char *old_section_names;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
622 l_Elf_Addr old_bss_addr, new_bss_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
623 l_Elf_Addr old_base_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
624 l_Elf_Word old_bss_size, new_data2_size;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
625 l_Elf_Off new_data2_offset, new_base_offset;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
626 l_Elf_Addr new_data2_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
627 l_Elf_Addr new_offsets_shift;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
628
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
629 int n, nn, old_bss_index, old_data_index;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
630 int old_mdebug_index, old_sbss_index;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
631 struct stat stat_buf;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
632
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
633 /* Open the old file & map it into the address space. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
634
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
635 old_file = open (old_name, O_RDONLY);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
636
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
637 if (old_file < 0)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
638 fatal ("Can't open %s for reading: errno %d\n", old_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 if (fstat (old_file, &stat_buf) == -1)
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
641 fatal ("Can't fstat(%s): errno %d\n", old_name, errno);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
642
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
643 old_base = mmap (0, stat_buf.st_size, PROT_READ, MAP_SHARED, old_file, 0);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
644
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
645 if (old_base == (caddr_t) -1)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
646 fatal ("Can't mmap(%s): errno %d\n", old_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
648 #ifdef DEBUG
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
649 fprintf (stderr, "mmap(%s, %x) -> %x\n", old_name, stat_buf.st_size,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
650 old_base);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
651 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
652
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
653 /* Get pointers to headers & section names. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
654
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
655 old_file_h = (l_Elf_Ehdr *) old_base;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
656 old_program_h = (l_Elf_Phdr *) ((byte *) old_base + old_file_h->e_phoff);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
657 old_section_h = (l_Elf_Shdr *) ((byte *) old_base + old_file_h->e_shoff);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
658 old_section_names
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
659 = (char *) old_base + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
661 /* Find the mdebug section, if any. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
662
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
663 old_mdebug_index = find_section (".mdebug", old_section_names,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
664 old_name, old_file_h, old_section_h, 1);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
665
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
666 /* Find the .sbss section, if any. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
667
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
668 old_sbss_index = find_section (".sbss", old_section_names,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
669 old_name, old_file_h, old_section_h, 1);
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
670
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
671 if (old_sbss_index != -1 && (OLD_SECTION_H (old_sbss_index).sh_type == SHT_PROGBITS))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
672 old_sbss_index = -1;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
673
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
674 /* Find the old .bss section. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
676 old_bss_index = find_section (".bss", old_section_names,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
677 old_name, old_file_h, old_section_h, 0);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
678
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
679 /* Find the old .data section. Figure out parameters of
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
680 the new data2 and bss sections. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682 old_data_index = find_section (".data", old_section_names,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 old_name, old_file_h, old_section_h, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
685 old_bss_addr = OLD_SECTION_H (old_bss_index).sh_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
686 old_bss_size = OLD_SECTION_H (old_bss_index).sh_size;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
687 old_base_addr = old_sbss_index == -1 ? old_bss_addr : OLD_SECTION_H (old_sbss_index).sh_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
688 #if defined(emacs) || !defined(DEBUG)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
689 bss_end = (uintptr_t) sbrk (0);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
690 new_bss_addr = (l_Elf_Addr) bss_end;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
691 #else
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
692 new_bss_addr = old_bss_addr + old_bss_size + 0x1234;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
693 #endif
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
694 new_data2_addr = old_bss_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
695 new_data2_size = new_bss_addr - old_bss_addr;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 new_data2_offset = OLD_SECTION_H (old_data_index).sh_offset +
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
697 (new_data2_addr - OLD_SECTION_H (old_data_index).sh_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
698 new_base_offset = OLD_SECTION_H (old_data_index).sh_offset +
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
699 (old_base_addr - OLD_SECTION_H (old_data_index).sh_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
700 new_offsets_shift = new_bss_addr -
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
701 ((old_base_addr & ~0xfff) + ((old_base_addr & 0xfff) ? 0x1000 : 0));
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
702
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
703 #ifdef DEBUG
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
704 fprintf (stderr, "old_bss_index %d\n", old_bss_index);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
705 fprintf (stderr, "old_bss_addr %x\n", old_bss_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
706 fprintf (stderr, "old_bss_size %x\n", old_bss_size);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
707 fprintf (stderr, "old_base_addr %x\n", old_base_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
708 fprintf (stderr, "new_bss_addr %x\n", new_bss_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
709 fprintf (stderr, "new_data2_addr %x\n", new_data2_addr);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
710 fprintf (stderr, "new_data2_size %x\n", new_data2_size);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
711 fprintf (stderr, "new_data2_offset %x\n", new_data2_offset);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
712 fprintf (stderr, "new_offsets_shift %x\n", new_offsets_shift);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
713 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
715 if ((unsigned) new_bss_addr < (unsigned) old_bss_addr + old_bss_size)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
716 fatal (".bss shrank when undumping???\n");
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
717
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
718 /* Set the output file to the right size and mmap it. Set
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
719 pointers to various interesting objects. stat_buf still has
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
720 old_file data. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
721
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
722 new_file = open (new_name, O_RDWR | O_CREAT, 0666);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
723 if (new_file < 0)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
724 fatal ("Can't creat (%s): errno %d\n", new_name, errno);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
725
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
726 new_file_size = stat_buf.st_size /* old file size */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
727 + old_file_h->e_shentsize /* one new section header */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
728 + new_offsets_shift; /* trailing section shift */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730 if (ftruncate (new_file, new_file_size))
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
731 fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
733 new_base = mmap (0, new_file_size, PROT_READ | PROT_WRITE, MAP_SHARED,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
734 new_file, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 if (new_base == (caddr_t) -1)
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
737 fatal ("Can't mmap (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
739 new_file_h = (l_Elf_Ehdr *) new_base;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
740 new_program_h = (l_Elf_Phdr *) ((byte *) new_base + old_file_h->e_phoff);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
741 new_section_h
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
742 = (l_Elf_Shdr *) ((byte *) new_base + old_file_h->e_shoff
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
743 + new_offsets_shift);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 /* Make our new file, program and section headers as copies of the
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
746 originals. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
747
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 memcpy (new_program_h, old_program_h,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 old_file_h->e_phnum * old_file_h->e_phentsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 /* Modify the e_shstrndx if necessary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 PATCH_INDEX (new_file_h->e_shstrndx);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 /* Fix up file header. We'll add one section. Section header is
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
756 further away now. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
757
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
758 new_file_h->e_shoff += new_offsets_shift;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 new_file_h->e_shnum += 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
761
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
762 #ifdef DEBUG
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
763 fprintf (stderr, "Old section offset %x\n", old_file_h->e_shoff);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
764 fprintf (stderr, "Old section count %d\n", old_file_h->e_shnum);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
765 fprintf (stderr, "New section offset %x\n", new_file_h->e_shoff);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
766 fprintf (stderr, "New section count %d\n", new_file_h->e_shnum);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 #endif
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
768
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
769 /* Fix up a new program header. Extend the writable data segment so
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
770 that the bss area is covered too. Find that segment by looking
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
771 for a segment that ends just before the .bss area. Make sure
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
772 that no segments are above the new .data2. Put a loop at the end
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
773 to adjust the offset and address of any segment that is above
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
774 data2, just in case we decide to allow this later. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
775
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
776 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
777 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
778 /* Compute maximum of all requirements for alignment of section. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
779 int alignment = (NEW_PROGRAM_H (n)).p_align;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
780 if ((OLD_SECTION_H (old_bss_index)).sh_addralign > alignment)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
781 alignment = OLD_SECTION_H (old_bss_index).sh_addralign;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
782
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
783 /* Supposedly this condition is okay for the SGI. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
784 #if 0
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
785 if (NEW_PROGRAM_H (n).p_vaddr + NEW_PROGRAM_H (n).p_filesz > old_base_addr)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
786 fatal ("Program segment above .bss in %s\n", old_name);
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
787 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
789 if (NEW_PROGRAM_H (n).p_type == PT_LOAD
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
790 && (round_up ((NEW_PROGRAM_H (n)).p_vaddr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
791 + (NEW_PROGRAM_H (n)).p_filesz,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
792 alignment)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
793 == round_up (old_base_addr, alignment)))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
794 break;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
795 }
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
796 if (n < 0)
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
797 fatal ("Couldn't find segment next to %s in %s\n",
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
798 old_sbss_index == -1 ? ".sbss" : ".bss", old_name);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
799
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
800 NEW_PROGRAM_H (n).p_filesz += new_offsets_shift;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
801 NEW_PROGRAM_H (n).p_memsz = NEW_PROGRAM_H (n).p_filesz;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
802
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
803 #if 1 /* Maybe allow section after data2 - does this ever happen? */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
804 for (n = new_file_h->e_phnum - 1; n >= 0; n--)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
805 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
806 if (NEW_PROGRAM_H (n).p_vaddr
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
807 && NEW_PROGRAM_H (n).p_vaddr >= new_data2_addr)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
808 NEW_PROGRAM_H (n).p_vaddr += new_offsets_shift - old_bss_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
810 if (NEW_PROGRAM_H (n).p_offset >= new_data2_offset)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
811 NEW_PROGRAM_H (n).p_offset += new_offsets_shift;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
812 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
813 #endif
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
814
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
815 /* Fix up section headers based on new .data2 section. Any section
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
816 whose offset or virtual address is after the new .data2 section
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
817 gets its value adjusted. .bss size becomes zero and new address
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
818 is set. data2 section header gets added by copying the existing
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
819 .data header and modifying the offset, address and size. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
820 for (old_data_index = 1; old_data_index < old_file_h->e_shnum;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
821 old_data_index++)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
822 if (!strcmp (old_section_names + OLD_SECTION_H (old_data_index).sh_name,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
823 ".data"))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
824 break;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
825 if (old_data_index == old_file_h->e_shnum)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
826 fatal ("Can't find .data in %s.\n", old_name);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
827
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
828 /* Walk through all section headers, insert the new data2 section right
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
829 before the new bss section. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
830 for (n = 1, nn = 1; n < old_file_h->e_shnum; n++, nn++)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
831 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
832 caddr_t src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
833
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
834 /* XEmacs change: */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
835 if (n < old_bss_index)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
836 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
837 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n),
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
838 old_file_h->e_shentsize);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
839
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
840 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
841 else if (n == old_bss_index)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
842 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
843
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
844 /* If it is bss section, insert the new data2 section before it. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
845 /* Steal the data section header for this data2 section. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
846 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (old_data_index),
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
847 new_file_h->e_shentsize);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
848
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
849 NEW_SECTION_H (nn).sh_addr = new_data2_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
850 NEW_SECTION_H (nn).sh_offset = new_data2_offset;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
851 NEW_SECTION_H (nn).sh_size = new_data2_size;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
852 /* Use the bss section's alignment. This will assure that the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
853 new data2 section always be placed in the same spot as the old
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
854 bss section by any other application. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
855 NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (n).sh_addralign;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
856
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
857 /* Now copy over what we have in the memory now. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
858 memcpy (NEW_SECTION_H (nn).sh_offset + new_base,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
859 (caddr_t) OLD_SECTION_H (n).sh_addr,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
860 new_data2_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
861 nn++;
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
862 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n),
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
863 old_file_h->e_shentsize);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
864
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
865 /* The new bss section's size is zero, and its file offset and virtual
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
866 address should be off by NEW_OFFSETS_SHIFT. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
867 NEW_SECTION_H (nn).sh_offset += new_offsets_shift;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
868 NEW_SECTION_H (nn).sh_addr = new_bss_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
869 /* Let the new bss section address alignment be the same as the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
870 section address alignment followed the old bss section, so
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
871 this section will be placed in exactly the same place. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
872 NEW_SECTION_H (nn).sh_addralign = OLD_SECTION_H (n).sh_addralign;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
873 NEW_SECTION_H (nn).sh_size = 0;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
874 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
875 else /* n > old_bss_index */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
876 memcpy (&NEW_SECTION_H (nn), &OLD_SECTION_H (n),
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
877 old_file_h->e_shentsize);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
878
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
879 /* Any section that was original placed AFTER the bss
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
880 section must now be adjusted by NEW_OFFSETS_SHIFT. */
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
881
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
882 if (NEW_SECTION_H (nn).sh_offset >= new_base_offset)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
883 NEW_SECTION_H (nn).sh_offset += new_offsets_shift;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
884
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
885 /* If any section hdr refers to the section after the new .data
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
886 section, make it refer to next one because we have inserted
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
887 a new section in between. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
888
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
889 PATCH_INDEX (NEW_SECTION_H (nn).sh_link);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
890 /* For symbol tables, info is a symbol table index,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
891 so don't change it. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
892 if (NEW_SECTION_H (nn).sh_type != SHT_SYMTAB
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
893 && NEW_SECTION_H (nn).sh_type != SHT_DYNSYM)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
894 PATCH_INDEX (NEW_SECTION_H (nn).sh_info);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
895
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
896 /* Fix the type and alignment for the .sbss section */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
897 if ((old_sbss_index != -1) && !strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".sbss"))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
898 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
899 NEW_SECTION_H (nn).sh_type = SHT_PROGBITS;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
900 NEW_SECTION_H (nn).sh_offset = round_up (NEW_SECTION_H (nn).sh_offset,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
901 NEW_SECTION_H (nn).sh_addralign);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
902 }
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
903
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
904 /* Now, start to copy the content of sections. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
905 if (NEW_SECTION_H (nn).sh_type == SHT_NULL
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
906 || NEW_SECTION_H (nn).sh_type == SHT_NOBITS)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
907 continue;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
908
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
909 /* Write out the sections. .data, .data1 and .sbss (and data2, called
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
910 ".data" in the strings table) get copied from the current process
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
911 instead of the old file. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
912 if (!strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".data")
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
913 || !strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".data1")
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
914 || !strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".got")
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
915 || !strcmp (old_section_names + NEW_SECTION_H (nn).sh_name, ".sbss"))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
916 src = (caddr_t) OLD_SECTION_H (n).sh_addr;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
917 else
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
918 src = old_base + OLD_SECTION_H (n).sh_offset;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
919
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
920 memcpy (NEW_SECTION_H (nn).sh_offset + new_base, src,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
921 NEW_SECTION_H (nn).sh_size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
922
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
923 /* Adjust the HDRR offsets in .mdebug and copy the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
924 line data if it's in its usual 'hole' in the object.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
925 Makes the new file debuggable with dbx.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
926 patches up two problems: the absolute file offsets
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
927 in the HDRR record of .mdebug (see /usr/include/syms.h), and
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
928 the ld bug that gets the line table in a hole in the
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
929 elf file rather than in the .mdebug section proper.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
930 David Anderson. davea@sgi.com Jan 16,1994. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
931 if (n == old_mdebug_index)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
932 {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 #define MDEBUGADJUST(__ct,__fileaddr) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 if (n_phdrr->__ct > 0) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936 n_phdrr->__fileaddr += movement; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
939 HDRR * o_phdrr = (HDRR *)((byte *)old_base + OLD_SECTION_H (n).sh_offset);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
940 HDRR * n_phdrr = (HDRR *)((byte *)new_base + NEW_SECTION_H (nn).sh_offset);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
941 unsigned movement = new_offsets_shift;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 MDEBUGADJUST (idnMax, cbDnOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 MDEBUGADJUST (ipdMax, cbPdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 MDEBUGADJUST (isymMax, cbSymOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 MDEBUGADJUST (ioptMax, cbOptOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 MDEBUGADJUST (iauxMax, cbAuxOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 MDEBUGADJUST (issMax, cbSsOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 MDEBUGADJUST (issExtMax, cbSsExtOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 MDEBUGADJUST (ifdMax, cbFdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 MDEBUGADJUST (crfd, cbRfdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 MDEBUGADJUST (iextMax, cbExtOffset);
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
953 /* The Line Section, being possible off in a hole of the object,
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
954 requires special handling. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
955 if (n_phdrr->cbLine > 0)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
956 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
957 if (o_phdrr->cbLineOffset > (OLD_SECTION_H (n).sh_offset
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
958 + OLD_SECTION_H (n).sh_size))
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
959 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
960 /* line data is in a hole in elf. do special copy and adjust
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
961 for this ld mistake.
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
962 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 n_phdrr->cbLineOffset += movement;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 memcpy (n_phdrr->cbLineOffset + new_base,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
967 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
968 else
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
969 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
970 /* somehow line data is in .mdebug as it is supposed to be. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 MDEBUGADJUST (cbLine, cbLineOffset);
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
972 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
973 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
974 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
976 /* If it is the symbol table, its st_shndx field needs to be patched. */
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
977 if (NEW_SECTION_H (nn).sh_type == SHT_SYMTAB
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
978 || NEW_SECTION_H (nn).sh_type == SHT_DYNSYM)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
979 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
980 l_Elf_Shdr *spt = &NEW_SECTION_H (nn);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
981 unsigned int num = spt->sh_size / spt->sh_entsize;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
982 l_Elf_Sym * sym = (l_Elf_Sym *) (NEW_SECTION_H (nn).sh_offset
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
983 + new_base);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
984 for (; num--; sym++)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
985 {
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
986 if (sym->st_shndx == SHN_UNDEF
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
987 || sym->st_shndx == SHN_ABS
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
988 || sym->st_shndx == SHN_COMMON)
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
989 continue;
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
990
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
991 PATCH_INDEX (sym->st_shndx);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
992 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
993 }
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
994 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
996 /* Close the files and make the new file executable. */
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
997
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 if (close (old_file))
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
999 fatal ("Can't close (%s): errno %d\n", old_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001 if (close (new_file))
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1002 fatal ("Can't close (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 if (stat (new_name, &stat_buf) == -1)
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1005 fatal ("Can't stat (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1006
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1007 n = umask (777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008 umask (n);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 stat_buf.st_mode |= 0111 & ~n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 if (chmod (new_name, stat_buf.st_mode) == -1)
371
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1011 fatal ("Can't chmod (%s): errno %d\n", new_name, errno);
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1012
cc15677e0335 Import from CVS: tag r21-2b1
cvs
parents: 367
diff changeset
1013 return 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 }