annotate src/unexelfsgi.c @ 367:a4f53d9b3154 r21-1-13

Import from CVS: tag r21-1-13
author cvs
date Mon, 13 Aug 2007 11:01:07 +0200
parents 8e84bee8ddd0
children cc15677e0335
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
1 /* Copyright (C) 1985, 1986, 1987, 1988, 1990, 1992, 1999, 2000
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
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
4 This file is part of XEmacs.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
7 under the terms of the GNU General Public License as published by
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
8 the Free Software Foundation; either version 2, or (at your option)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
9 any later version.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
11 GNU Emacs is distributed in the hope that it will be useful, but
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
12 WITHOUT ANY WARRANTY; without even the implied warranty of
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
14 General Public License for more details.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
16 You should have received a copy of the GNU General Public License
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
17 along with GNU Emacs; see the file COPYING. If not, write to the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
18 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
19 Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
21 In other words, you are welcome to use, share and improve this
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
22 program. You are forbidden to forbid anyone else to use, share and
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
23 improve what you give them. Help stamp out software-hoarding! */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 /*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 * unexec.c - Convert a running program into an a.out file.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 * Author: Spencer W. Thomas
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 * Computer Science Dept.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 * University of Utah
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 * Date: Tue Mar 2 1982
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 * Modified heavily since then.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 * Synopsis:
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
36 * void
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
37 * unexec (char *new_name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
38 * char *old_name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
39 * uintptr_t data_start,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
40 * uintptr_t bss_start,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
41 * uintptr_t entry_address)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 *
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
43 * The basic idea is that we start with an ELF file which contains
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
44 * .bss (uninitialized global data) section which is normally not in
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
45 * the file. As we load lisp the variables, which were first set to 0,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
46 * will change their values. We want to save those changed values into
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
47 * another ELF file, which will become a new xemacs image. To do this,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
48 * we need to change several structures in the ELF file.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 *
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
50 * First of all, we need to change the programm header which tells
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
51 * the linker how to load stuff into memory so that data will come
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
52 * from the file and not from the /dev/zero. To do this, we find the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
53 * segment, which is marked as loadable (type PT_LOAD) and which
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
54 * covers the old .bss section. We will next change the filesz and
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
55 * memsz for that segment to extend over the new data section.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 *
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
57 * Next we have to make sure that section header for the stuff which
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
58 * used to be uninitialized is changed to be initialized and to come
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
59 * from the file. To do this, we change the size and the type of the old
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
60 * .bss section (and all other section of the type SHT_NOBITS) to cover the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
61 * new section and to be of type SHT_PROCBITS.
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
62 *
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
63 * We also insert a new SHT_NOBITS section to keep some tools, which expect
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
64 * .bss happy.
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
65 *
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
66 * Finally we need to patch up some references to the section
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
67 * indexes since we change the order and undo the relocation info to
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
68 * be the same as it was "before" because we actually used the data
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
69 * from the memory which were changed by the run-time linker.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
72 #ifndef emacs
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
73 #define fatal(a, b, c) fprintf (stderr, a, b, c), exit (1)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
74 #include <string.h>
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
75 #else
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
76 #include <config.h>
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
77 extern void fatal (const char *, ...);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
78 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 #include <sys/types.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 #include <sys/stat.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #include <memory.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 #include <fcntl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 #include <elf.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 #include <sys/mman.h>
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
89 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
90 #include <sys/elf_mips.h>
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
91 #include <sym.h>
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
92 #endif /* __sony_news && _SYSTYPE_SYSV */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
93 #if __sgi
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
94 #include <syms.h> /* for HDRR declaration */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
95 #endif /* __sgi */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
97 #if __GNU_LIBRARY__ - 0 >= 6
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
98 # include <link.h> /* get ElfW etc */
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
99 #endif
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
100
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
101 #ifndef ElfW
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
102 # ifdef __STDC__
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
103 # define ElfBitsW(bits, type) Elf##bits##_##type
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
104 # else
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
105 # define ElfBitsW(bits, type) Elf/**/bits/**/_/**/type
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
106 # endif
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
107 # ifdef _LP64
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
108 # define ELFSIZE 64
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
109 # else
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
110 # define ELFSIZE 32
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
111 # endif
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
112 /* This macro expands `bits' before invoking ElfBitsW. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
113 # define ElfExpandBitsW(bits, type) ElfBitsW (bits, type)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
114 # define ElfW(type) ElfExpandBitsW (ELFSIZE, type)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
115 #endif
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
116
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
117 #ifndef ELF_BSS_SECTION_NAME
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
118 #define ELF_BSS_SECTION_NAME ".bss"
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
119 #endif
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
120
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 /* Get the address of a particular section or program header entry,
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
122 * accounting for the size of the entries. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 #define OLD_SECTION_H(n) \
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
125 (*(ElfW(Shdr) *) ((byte *) old_section_h + old_file_h->e_shentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 #define NEW_SECTION_H(n) \
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
127 (*(ElfW(Shdr) *) ((byte *) new_section_h + new_file_h->e_shentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 #define OLD_PROGRAM_H(n) \
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
129 (*(ElfW(Phdr) *) ((byte *) old_program_h + old_file_h->e_phentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 #define NEW_PROGRAM_H(n) \
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
131 (*(ElfW(Phdr) *) ((byte *) new_program_h + new_file_h->e_phentsize * (n)))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 #define PATCH_INDEX(n) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 do { \
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
135 if ((int) (n) >= growme_index) \
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 (n)++; } while (0)
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
137
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 typedef unsigned char byte;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 /* Round X up to a multiple of Y. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
142 static ElfW(Addr)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
143 round_up (ElfW(Addr) x, ElfW(Addr) y)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 int rem = x % y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 if (rem == 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 return x;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 return x - rem + y;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 /* Return the index of the section named NAME.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 SECTION_NAMES, FILE_NAME and FILE_H give information
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 about the file we are looking in.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 If we don't find the section NAME, that is a fatal error
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 if NOERROR is 0; we return -1 if NOERROR is nonzero. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 static int
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
159 find_section (char *name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
160 const char *section_names,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
161 char *file_name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
162 ElfW(Ehdr) *old_file_h,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
163 ElfW(Shdr) *old_section_h,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
164 int noerror)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 int idx;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 for (idx = 1; idx < old_file_h->e_shnum; idx++)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 #ifdef DEBUG
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 fprintf (stderr, "Looking for %s - found %s\n", name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 section_names + OLD_SECTION_H (idx).sh_name);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 if (!strcmp (section_names + OLD_SECTION_H (idx).sh_name,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 name))
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
176 return idx;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
179 /* If we're here, we found nothing or return did not work */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
180 if ( ! noerror)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
181 fatal ("Can't find %s in %s.\n", name, file_name);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
182
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
183 return -1;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 /* ****************************************************************
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 * unexec
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 * driving logic.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 * In ELF, this works by replacing the old .bss section with a new
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 * .data section, and inserting an empty .bss immediately afterwards.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 *
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 */
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
195 void
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
196 unexec (char *new_name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
197 char *old_name,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
198 uintptr_t data_start,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
199 uintptr_t bss_start,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
200 uintptr_t entry_address)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 {
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
202 int old_file;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
204 struct stat stat_buf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 caddr_t old_base, new_base;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
207 ElfW(Ehdr) *old_file_h, * new_file_h;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
208 ElfW(Phdr) *old_program_h, * new_program_h;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
209 ElfW(Shdr) *old_section_h, * new_section_h;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
210 ElfW(Shdr) * growme = NULL, * grown = NULL;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
211 ElfW(Addr) old_bss_addr = 0, new_data2_addr = 0;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
213 int growme_index = -1;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
214 int n, nn;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
215 const char *old_section_names;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
216 int old_mdebug_index, old_data_index;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
217 int new_bss_addr, new_data2_size, new_data2_offset, new_file, new_file_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
219 /* Open the old file */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
220 if ( (old_file = open (old_name, O_RDONLY)) < 0 )
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
221 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
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 if (fstat (old_file, &stat_buf) == -1)
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
224 fatal ("Can't fstat (%s): errno %d\n", old_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
226 /* map old file into the address space. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
227 old_base = (caddr_t) mmap ((caddr_t) 0, stat_buf.st_size,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
228 PROT_READ, MAP_SHARED, old_file, 0);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
229 if (old_base == (caddr_t) MAP_FAILED)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
230 fatal ("Can't mmap (%s): errno %d\n", old_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
232 old_file_h = (ElfW(Ehdr) *) old_base;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
233 old_program_h = (ElfW(Phdr) *) ((byte *) old_base + old_file_h->e_phoff);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
234 old_section_h = (ElfW(Shdr) *) ((byte *) old_base + old_file_h->e_shoff);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
235 old_section_names = (const char *) old_base
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
236 + OLD_SECTION_H (old_file_h->e_shstrndx).sh_offset;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
238 /* Find a section which we will grow by looking for the SHT_NOBITS
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
239 * section with ALLOCATE flag and with the biggest address. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
240 for (n = 1; n < old_file_h->e_shnum; n++) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
241 ElfW(Shdr) * sh = & OLD_SECTION_H(n);
167
85ec50267440 Import from CVS: tag r20-3b10
cvs
parents: 165
diff changeset
242
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
243 if ((sh->sh_type == SHT_NOBITS) && (sh->sh_flags & SHF_ALLOC)) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
244 if ( old_bss_addr < sh->sh_addr ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
245 growme = sh;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
246 growme_index = n;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
247 new_data2_addr = old_bss_addr = sh->sh_addr;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
248 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
249 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
250 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
252 if (growme == NULL )
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
253 fatal ("Can't find a section to grow\n", 0, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 old_data_index = find_section (".data", old_section_names,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 old_name, old_file_h, old_section_h, 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
258 new_bss_addr = (ElfW(Addr)) sbrk (0);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
259 new_data2_size = new_bss_addr - old_bss_addr;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 new_data2_offset = OLD_SECTION_H (old_data_index).sh_offset +
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
261 (new_data2_addr - OLD_SECTION_H (old_data_index).sh_addr);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
263 if ( new_bss_addr < old_bss_addr + growme->sh_size )
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
264 fatal (".bss shrank when undumping???\n", 0, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
266 /* Set the output file to the right size and mmap it. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
267 if ( (new_file = open (new_name, O_RDWR | O_CREAT, 0666)) < 0 )
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
268 fatal ("Can't create (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
270 new_file_size = stat_buf.st_size + old_file_h->e_shentsize + new_data2_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 if (ftruncate (new_file, new_file_size))
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
273 fatal ("Can't ftruncate (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
275 new_base = (caddr_t) mmap ((caddr_t) 0, new_file_size,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
276 PROT_READ | PROT_WRITE,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
277 #ifdef UNEXEC_USE_MAP_PRIVATE
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
278 MAP_PRIVATE,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
279 #else
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
280 MAP_SHARED,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
281 #endif
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
282 new_file, 0);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 if (new_base == (caddr_t) -1)
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
285 fatal ("Can't mmap (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
287 new_file_h = (ElfW(Ehdr) *) new_base;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
288 new_program_h = (ElfW(Phdr) *) ((byte *) new_base + old_file_h->e_phoff);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
289 new_section_h = (ElfW(Shdr) *) ((byte *) new_base + old_file_h->e_shoff +
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
290 new_data2_size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 /* Make our new file, program and section headers as copies of the
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
293 * originals. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 memcpy (new_file_h, old_file_h, old_file_h->e_ehsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 memcpy (new_program_h, old_program_h,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 old_file_h->e_phnum * old_file_h->e_phentsize);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 /* Modify the e_shstrndx if necessary. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 PATCH_INDEX (new_file_h->e_shstrndx);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 /* Fix up file header. We'll add one section. Section header is
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
302 * further away now. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
303 new_file_h->e_shoff += new_data2_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 new_file_h->e_shnum += 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
306 /* Fix up a new program header by extending the writable data
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
307 * segment so that the bss area is covered too. Find that segment by
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
308 * looking for one that starts before and ends after the .bss and is
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
309 * PT_LOADable. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
310 for (n = new_file_h->e_phnum - 1; n >= 0; n--) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
311 ElfW(Phdr) * ph = & NEW_PROGRAM_H(n);
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
312 #ifdef DEBUG
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
313 printf ("%d @ %0x + %0x against %0x + %0x",
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
314 n, ph->p_vaddr, ph->p_memsz,growme->sh_addr, growme->sh_size);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 #endif
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
316 if ((ph->p_type == PT_LOAD) &&
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
317 (ph->p_vaddr <= growme->sh_addr) &&
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
318 ((ph->p_vaddr+ph->p_memsz) >= (growme->sh_addr + growme->sh_size))) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
319 /* Make sure that the size includes any padding before the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
320 * old .bss section. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
321 ph->p_memsz = ph->p_filesz = new_bss_addr - ph->p_vaddr;
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
322 #ifdef DEBUG
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
323 puts (" That's the one!");
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
324 #endif
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
325 break;
359
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
326 }
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
327 #ifdef DEBUG
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
328 putchar ('\n');
8e84bee8ddd0 Import from CVS: tag r21-1-9
cvs
parents: 288
diff changeset
329 #endif
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
330 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
332 if (n < 0)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
333 fatal ("Couldn't find segment which covers %s",
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
334 old_section_names + growme->sh_name);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
336 /* Walk through all section headers, insert the new data2 section
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
337 * right before the new bss section. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
338 for (n = 1, nn = 1; n < (int) old_file_h->e_shnum; n++, nn++) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
339 ElfW(Shdr) * nsec = & NEW_SECTION_H(nn);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
340 ElfW(Shdr) * osec = & OLD_SECTION_H(n);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
342 /* If this is the section we want to grow, insert the new data
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
343 * section before it. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
344 if ( osec == growme ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
345 /* Steal the data section header for this data2 section but
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
346 * use the * 'grow' section's alignment. This * will assure
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
347 * that the new section * always be placed in the same spot
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
348 * * as the old section by any other * application. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
349 ElfW(Shdr) * od = &OLD_SECTION_H(old_data_index);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
351 memcpy (nsec, od, new_file_h->e_shentsize);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
353 nsec->sh_addr = new_data2_addr;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
354 nsec->sh_offset = new_data2_offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
355 nsec->sh_size = new_data2_size;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
356 nsec->sh_addralign = osec->sh_addralign;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
357
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
358 /* Copy over what we have in memory now. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
359 memcpy (nsec->sh_offset + new_base, (caddr_t) osec->sh_addr,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 new_data2_size);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 nn++;
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
362 grown = nsec++;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
363 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
364
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
365 memcpy (nsec, osec, old_file_h->e_shentsize);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
366
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
367 if ( osec == growme ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
368 /* The new bss section's size is zero, and its file offset
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
369 * and virtual address should be off by NEW_DATA2_SIZE. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
370 nsec->sh_offset = grown->sh_offset + new_data2_size;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
371 nsec->sh_addr = grown->sh_addr + new_data2_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
373 /* Let the new bss section address alignment be the same as
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
374 * the section address alignment followed the old bss
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
375 * section, so this section will be placed in exactly the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
376 * same place. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
377 nsec->sh_addralign = osec->sh_addralign;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
378 nsec->sh_size = 0;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
379 } else {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
380 /* Any section that was originally placed AFTER the bss
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
381 * section should now be off by NEW_DATA2_SIZE. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
382 if ( round_up (nsec->sh_offset, growme->sh_addralign) >=
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
383 new_data2_offset)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
384 nsec->sh_offset += new_data2_size;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
385 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
386
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
387 /* Any section that was originally placed after the section *
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
388 * header table should now be off by the size of one section
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
389 * header table entry. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
390 if (nsec->sh_offset > new_file_h->e_shoff)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
391 nsec->sh_offset += new_file_h->e_shentsize;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
392
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
393
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 /* If any section hdr refers to the section after the new .data
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
395 * section, make it refer to next one because we have inserted a
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
396 * new section in between. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
397 PATCH_INDEX (nsec->sh_link);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
398
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
399 /* For symbol tables, info is a symbol table index, so don't
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
400 * change it. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
401 if (nsec->sh_type != SHT_SYMTAB && nsec->sh_type != SHT_DYNSYM)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
402 PATCH_INDEX (nsec->sh_info);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
403
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
404 /* Any section which used to be NOBITS will now becomes PROGBITS
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
405 * if it's ALLOC-atable, unless, of cause, it's not the one we
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
406 * decided to grow */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
407 if ( (osec->sh_type == SHT_NOBITS) && (osec->sh_flags & SHF_ALLOC) &&
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
408 (osec != growme ) ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
409 nsec->sh_type = SHT_PROGBITS;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
410 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
411
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
412 /* Now, start to copy the content of sections */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
413 if ( nsec->sh_type != SHT_NULL || nsec->sh_type != SHT_NOBITS ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
414
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
415 /* Write out the sections. .data and .data1 (and data2,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
416 * called ".data" in the strings table) get copied from the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
417 * current process instead of the old file. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
418 caddr_t src = old_base + osec->sh_offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
419 const char * secname = old_section_names + nsec->sh_name;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
420 const char * names[] = {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
421 ".data",".sdata", ".lit4", ".lit8", ".sdata1", ".data1",
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
422 ".sbss", NULL};
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
423 int i;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
424
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
425 for ( i=0; names[i] != NULL; i++ ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
426 if ( ! strcmp (secname, names[i]) ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
427 src = (caddr_t) osec->sh_addr;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
428 break;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
429 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
430 }
163
0132846995bd Import from CVS: tag r20-3b8
cvs
parents: 70
diff changeset
431
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
432 memcpy (nsec->sh_offset + new_base, src, nsec->sh_size);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
433 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
434
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
435 old_mdebug_index = find_section (".mdebug", old_section_names,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
436 old_name, old_file_h, old_section_h, 1);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
437
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
438 #if defined (__sony_news) && defined (_SYSTYPE_SYSV)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
439 if (nsec->sh_type == SHT_MIPS_DEBUG && old_mdebug_index != -1) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
440 int diff = nsec->sh_offset-OLD_SECTION_H(old_mdebug_index).sh_offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
441 HDRR *phdr = (HDRR *)(nsec->sh_offset + new_base);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
443 if (diff) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
444 phdr->cbLineOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
445 phdr->cbDnOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
446 phdr->cbPdOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
447 phdr->cbSymOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
448 phdr->cbOptOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
449 phdr->cbAuxOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
450 phdr->cbSsOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
451 phdr->cbSsExtOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
452 phdr->cbFdOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
453 phdr->cbRfdOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
454 phdr->cbExtOffset += diff;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
455 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
456 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
457 #endif /* __sony_news && _SYSTYPE_SYSV */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
458
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
459 #if __sgi
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
460 /* Adjust the HDRR offsets in .mdebug and copy the line data if
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
461 * it's in its usual 'hole' in the object. Makes the new file
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
462 * debuggable with dbx. patches up two problems: the absolute
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
463 * file offsets in the HDRR record of .mdebug (see
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
464 * /usr/include/syms.h), and the ld bug that gets the line table
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
465 * in a hole in the elf file rather than in the .mdebug section
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
466 * proper.
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
467 *
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
468 * David Anderson. davea@sgi.com Jan 16,1994 */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 #define MDEBUGADJUST(__ct,__fileaddr) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 if (n_phdrr->__ct > 0) \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 { \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 n_phdrr->__fileaddr += movement; \
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
475 if (n == old_mdebug_index) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
476 HDRR * o_phdrr = (HDRR *)((byte *)old_base + osec->sh_offset);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
477 HDRR * n_phdrr = (HDRR *)((byte *)new_base + nsec->sh_offset);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
478 unsigned movement = new_data2_size;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 MDEBUGADJUST (idnMax, cbDnOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 MDEBUGADJUST (ipdMax, cbPdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 MDEBUGADJUST (isymMax, cbSymOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 MDEBUGADJUST (ioptMax, cbOptOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 MDEBUGADJUST (iauxMax, cbAuxOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 MDEBUGADJUST (issMax, cbSsOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 MDEBUGADJUST (issExtMax, cbSsExtOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 MDEBUGADJUST (ifdMax, cbFdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 MDEBUGADJUST (crfd, cbRfdOffset);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 MDEBUGADJUST (iextMax, cbExtOffset);
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
490
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
491 /* The Line Section, being possible off in a hole of the
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
492 * object, requires special handling. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
493 if (n_phdrr->cbLine > 0) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
494 if (o_phdrr->cbLineOffset >
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
495 osec->sh_offset+ osec->sh_size){
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
496 /* line data is in a hole in elf. do special copy
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
497 * and adjust for this ld mistake. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 n_phdrr->cbLineOffset += movement;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 memcpy (n_phdrr->cbLineOffset + new_base,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 o_phdrr->cbLineOffset + old_base, n_phdrr->cbLine);
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
502 } else {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
503 /* somehow line data is in .mdebug as it is supposed
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
504 * to be. */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 MDEBUGADJUST (cbLine, cbLineOffset);
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
506 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
507 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
508 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
509 #endif /* __sgi */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
510 /* If it is the symbol table, its st_shndx field needs to be
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
511 * patched. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
512 if (nsec->sh_type == SHT_SYMTAB || nsec->sh_type == SHT_DYNSYM) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
513 unsigned int num = nsec->sh_size / nsec->sh_entsize;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
514 ElfW(Sym) * sym = (ElfW(Sym) *)(nsec->sh_offset + new_base);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
515 byte *symnames = ((byte *) new_base +
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
516 NEW_SECTION_H (nsec->sh_link).sh_offset);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
517
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
518 for (; num--; sym++) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
519 const char * symnam = (char *) (symnames + sym->st_name);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
520
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
521 /* Update the symbol values of _edata and _end. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
522 if (strcmp (symnam, "_end") == 0
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
523 || strcmp (symnam, "end") == 0
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
524 || strcmp (symnam, "_edata") == 0
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
525 || strcmp (symnam, "edata") == 0)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
526 memcpy (&sym->st_value, &new_bss_addr,sizeof (new_bss_addr));
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
527
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
528
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
529 if ((sym->st_shndx == SHN_UNDEF) || (sym->st_shndx == SHN_ABS)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
530 || (sym->st_shndx == SHN_COMMON)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
531 || (sym->st_shndx >= SHN_LOPROC &&
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
532 sym->st_shndx <= SHN_HIPROC))
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
533 continue;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
534
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
535 PATCH_INDEX (sym->st_shndx);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
536 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
537 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
538 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
540 /* This loop seeks out relocation sections for the data section, so
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
541 * that it can undo relocations performed by the runtime linker. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
542 for (n = new_file_h->e_shnum - 1; n; n--) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
543 ElfW(Shdr) section = NEW_SECTION_H (n);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
544
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
545 if ( section.sh_type == SHT_REL || section.sh_type == SHT_RELA ) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
546 /* This code handles two different size structs, but there
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
547 * should be no harm in that provided that r_offset is
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
548 * always the first member. */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
549 ElfW(Shdr) * info = & NEW_SECTION_H(section.sh_info);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
550 const char * nm = old_section_names + info->sh_name;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
551
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
552 if (!strcmp (nm, ".data") || !strcmp (nm, ".sdata")
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
553 || !strcmp (nm, ".lit4") || !strcmp (nm, ".lit8")
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
554 || !strcmp (nm, ".sdata1") || !strcmp (nm, ".data1")) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
555 ElfW(Addr) offset = info->sh_addr - info->sh_offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
556 caddr_t end, reloc = old_base + section.sh_offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
557
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
558 for (end = reloc + section.sh_size; reloc < end;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
559 reloc += section.sh_entsize) {
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
560 ElfW(Addr) addr = ((ElfW(Rel) *) reloc)->r_offset - offset;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
561 #ifdef __alpha__
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
562 /* The Alpha ELF binutils currently have a bug that
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
563 * sometimes results in relocs that contain all
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
564 * zeroes. Work around this for now... */
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
565 if (((ElfW(Rel) *) reloc)->r_offset == 0)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
566 continue;
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
567 #endif
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
568 memcpy (new_base + addr, old_base + addr,
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
569 sizeof(ElfW(Addr)));
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
570 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
571 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
572 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
573 }
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
574
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
575 #ifdef UNEXEC_USE_MAP_PRIVATE
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
576 if (lseek (new_file, 0, SEEK_SET) == -1)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
577 fatal ("Can't rewind (%s): errno %d\n", new_name, errno);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
578
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
579 if (write (new_file, new_base, new_file_size) != new_file_size)
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
580 fatal ("Can't write (%s): errno %d\n", new_name, errno);
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
581 #endif
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 /* Close the files and make the new file executable. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 if (close (old_file))
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
585 fatal ("Can't close (%s): errno %d\n", old_name, errno);
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 if (close (new_file))
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
588 fatal ("Can't close (%s): errno %d\n", new_name, errno);
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 if (stat (new_name, &stat_buf) == -1)
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
591 fatal ("Can't stat (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 n = umask (777);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 umask (n);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 stat_buf.st_mode |= 0111 & ~n;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
596 if (chmod (new_name, stat_buf.st_mode) == -1)
367
a4f53d9b3154 Import from CVS: tag r21-1-13
cvs
parents: 359
diff changeset
597 fatal ("Can't chmod (%s): errno %d\n", new_name, errno);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
598 }