root / linux-user / flatload.c @ a4bb6c3e
History | View | Annotate | Download (25 kB)
1 | e5fe0c52 | pbrook | /****************************************************************************/
|
---|---|---|---|
2 | e5fe0c52 | pbrook | /*
|
3 | e5fe0c52 | pbrook | * QEMU bFLT binary loader. Based on linux/fs/binfmt_flat.c
|
4 | e5fe0c52 | pbrook | *
|
5 | e5fe0c52 | pbrook | * This program is free software; you can redistribute it and/or modify
|
6 | e5fe0c52 | pbrook | * it under the terms of the GNU General Public License as published by
|
7 | e5fe0c52 | pbrook | * the Free Software Foundation; either version 2 of the License, or
|
8 | e5fe0c52 | pbrook | * (at your option) any later version.
|
9 | e5fe0c52 | pbrook | *
|
10 | e5fe0c52 | pbrook | * This program is distributed in the hope that it will be useful,
|
11 | e5fe0c52 | pbrook | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 | e5fe0c52 | pbrook | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 | e5fe0c52 | pbrook | * GNU General Public License for more details.
|
14 | e5fe0c52 | pbrook | *
|
15 | e5fe0c52 | pbrook | * You should have received a copy of the GNU General Public License
|
16 | e5fe0c52 | pbrook | * along with this program; if not, write to the Free Software
|
17 | e5fe0c52 | pbrook | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
18 | e5fe0c52 | pbrook | *
|
19 | e5fe0c52 | pbrook | * Copyright (C) 2006 CodeSourcery.
|
20 | e5fe0c52 | pbrook | * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
|
21 | e5fe0c52 | pbrook | * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
|
22 | e5fe0c52 | pbrook | * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
|
23 | e5fe0c52 | pbrook | * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
|
24 | e5fe0c52 | pbrook | * based heavily on:
|
25 | e5fe0c52 | pbrook | *
|
26 | e5fe0c52 | pbrook | * linux/fs/binfmt_aout.c:
|
27 | e5fe0c52 | pbrook | * Copyright (C) 1991, 1992, 1996 Linus Torvalds
|
28 | e5fe0c52 | pbrook | * linux/fs/binfmt_flat.c for 2.0 kernel
|
29 | e5fe0c52 | pbrook | * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
|
30 | e5fe0c52 | pbrook | * JAN/99 -- coded full program relocation (gerg@snapgear.com)
|
31 | e5fe0c52 | pbrook | */
|
32 | e5fe0c52 | pbrook | |
33 | e5fe0c52 | pbrook | /* ??? ZFLAT and shared library support is currently disabled. */
|
34 | e5fe0c52 | pbrook | |
35 | e5fe0c52 | pbrook | /****************************************************************************/
|
36 | e5fe0c52 | pbrook | |
37 | e5fe0c52 | pbrook | #include <stdio.h> |
38 | e5fe0c52 | pbrook | #include <stdlib.h> |
39 | e5fe0c52 | pbrook | #include <errno.h> |
40 | e5fe0c52 | pbrook | #include <sys/mman.h> |
41 | e5fe0c52 | pbrook | #include <unistd.h> |
42 | e5fe0c52 | pbrook | |
43 | e5fe0c52 | pbrook | #include "qemu.h" |
44 | e5fe0c52 | pbrook | #include "flat.h" |
45 | e5fe0c52 | pbrook | |
46 | e5fe0c52 | pbrook | //#define DEBUG
|
47 | e5fe0c52 | pbrook | |
48 | e5fe0c52 | pbrook | #ifdef DEBUG
|
49 | e5fe0c52 | pbrook | #define DBG_FLT(a...) printf(a)
|
50 | e5fe0c52 | pbrook | #else
|
51 | e5fe0c52 | pbrook | #define DBG_FLT(a...)
|
52 | e5fe0c52 | pbrook | #endif
|
53 | e5fe0c52 | pbrook | |
54 | e5fe0c52 | pbrook | #define flat_reloc_valid(reloc, size) ((reloc) <= (size))
|
55 | e5fe0c52 | pbrook | #define flat_old_ram_flag(flag) (flag)
|
56 | e5fe0c52 | pbrook | #ifdef TARGET_WORDS_BIGENDIAN
|
57 | e5fe0c52 | pbrook | #define flat_get_relocate_addr(relval) (relval)
|
58 | e5fe0c52 | pbrook | #else
|
59 | e5fe0c52 | pbrook | #define flat_get_relocate_addr(relval) bswap32(relval)
|
60 | e5fe0c52 | pbrook | #endif
|
61 | e5fe0c52 | pbrook | |
62 | e5fe0c52 | pbrook | #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */ |
63 | e5fe0c52 | pbrook | #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */ |
64 | e5fe0c52 | pbrook | |
65 | e5fe0c52 | pbrook | struct lib_info {
|
66 | e5fe0c52 | pbrook | target_ulong start_code; /* Start of text segment */
|
67 | e5fe0c52 | pbrook | target_ulong start_data; /* Start of data segment */
|
68 | e5fe0c52 | pbrook | target_ulong end_data; /* Start of bss section */
|
69 | e5fe0c52 | pbrook | target_ulong start_brk; /* End of data segment */
|
70 | e5fe0c52 | pbrook | target_ulong text_len; /* Length of text segment */
|
71 | e5fe0c52 | pbrook | target_ulong entry; /* Start address for this module */
|
72 | e5fe0c52 | pbrook | target_ulong build_date; /* When this one was compiled */
|
73 | e5fe0c52 | pbrook | short loaded; /* Has this library been loaded? */ |
74 | e5fe0c52 | pbrook | }; |
75 | e5fe0c52 | pbrook | |
76 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_SHARED_FLAT
|
77 | e5fe0c52 | pbrook | static int load_flat_shared_library(int id, struct lib_info *p); |
78 | e5fe0c52 | pbrook | #endif
|
79 | e5fe0c52 | pbrook | |
80 | e5fe0c52 | pbrook | struct linux_binprm;
|
81 | e5fe0c52 | pbrook | |
82 | e5fe0c52 | pbrook | #define ntohl(x) be32_to_cpu(x)
|
83 | e5fe0c52 | pbrook | |
84 | e5fe0c52 | pbrook | /****************************************************************************/
|
85 | e5fe0c52 | pbrook | /*
|
86 | e5fe0c52 | pbrook | * create_flat_tables() parses the env- and arg-strings in new user
|
87 | e5fe0c52 | pbrook | * memory and creates the pointer tables from them, and puts their
|
88 | e5fe0c52 | pbrook | * addresses on the "stack", returning the new stack pointer value.
|
89 | e5fe0c52 | pbrook | */
|
90 | e5fe0c52 | pbrook | |
91 | e5fe0c52 | pbrook | /* Push a block of strings onto the guest stack. */
|
92 | e5fe0c52 | pbrook | static target_ulong copy_strings(target_ulong p, int n, char **s) |
93 | e5fe0c52 | pbrook | { |
94 | e5fe0c52 | pbrook | int len;
|
95 | e5fe0c52 | pbrook | |
96 | e5fe0c52 | pbrook | while (n-- > 0) { |
97 | e5fe0c52 | pbrook | len = strlen(s[n]) + 1;
|
98 | e5fe0c52 | pbrook | p -= len; |
99 | e5fe0c52 | pbrook | memcpy_to_target(p, s[n], len); |
100 | e5fe0c52 | pbrook | } |
101 | e5fe0c52 | pbrook | |
102 | e5fe0c52 | pbrook | return p;
|
103 | e5fe0c52 | pbrook | } |
104 | e5fe0c52 | pbrook | |
105 | e5fe0c52 | pbrook | int target_pread(int fd, target_ulong ptr, target_ulong len, |
106 | e5fe0c52 | pbrook | target_ulong offset) |
107 | e5fe0c52 | pbrook | { |
108 | e5fe0c52 | pbrook | void *buf;
|
109 | e5fe0c52 | pbrook | int ret;
|
110 | e5fe0c52 | pbrook | |
111 | e5fe0c52 | pbrook | buf = lock_user(ptr, len, 0);
|
112 | e5fe0c52 | pbrook | ret = pread(fd, buf, len, offset); |
113 | e5fe0c52 | pbrook | unlock_user(buf, ptr, len); |
114 | e5fe0c52 | pbrook | return ret;
|
115 | e5fe0c52 | pbrook | } |
116 | e5fe0c52 | pbrook | /****************************************************************************/
|
117 | e5fe0c52 | pbrook | |
118 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_ZFLAT
|
119 | e5fe0c52 | pbrook | |
120 | e5fe0c52 | pbrook | #include <linux/zlib.h> |
121 | e5fe0c52 | pbrook | |
122 | e5fe0c52 | pbrook | #define LBUFSIZE 4000 |
123 | e5fe0c52 | pbrook | |
124 | e5fe0c52 | pbrook | /* gzip flag byte */
|
125 | e5fe0c52 | pbrook | #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */ |
126 | e5fe0c52 | pbrook | #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */ |
127 | e5fe0c52 | pbrook | #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */ |
128 | e5fe0c52 | pbrook | #define ORIG_NAME 0x08 /* bit 3 set: original file name present */ |
129 | e5fe0c52 | pbrook | #define COMMENT 0x10 /* bit 4 set: file comment present */ |
130 | e5fe0c52 | pbrook | #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */ |
131 | e5fe0c52 | pbrook | #define RESERVED 0xC0 /* bit 6,7: reserved */ |
132 | e5fe0c52 | pbrook | |
133 | e5fe0c52 | pbrook | static int decompress_exec( |
134 | e5fe0c52 | pbrook | struct linux_binprm *bprm,
|
135 | e5fe0c52 | pbrook | unsigned long offset, |
136 | e5fe0c52 | pbrook | char *dst,
|
137 | e5fe0c52 | pbrook | long len,
|
138 | e5fe0c52 | pbrook | int fd)
|
139 | e5fe0c52 | pbrook | { |
140 | e5fe0c52 | pbrook | unsigned char *buf; |
141 | e5fe0c52 | pbrook | z_stream strm; |
142 | e5fe0c52 | pbrook | loff_t fpos; |
143 | e5fe0c52 | pbrook | int ret, retval;
|
144 | e5fe0c52 | pbrook | |
145 | e5fe0c52 | pbrook | DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len); |
146 | e5fe0c52 | pbrook | |
147 | e5fe0c52 | pbrook | memset(&strm, 0, sizeof(strm)); |
148 | e5fe0c52 | pbrook | strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL); |
149 | e5fe0c52 | pbrook | if (strm.workspace == NULL) { |
150 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
|
151 | e5fe0c52 | pbrook | return -ENOMEM;
|
152 | e5fe0c52 | pbrook | } |
153 | e5fe0c52 | pbrook | buf = kmalloc(LBUFSIZE, GFP_KERNEL); |
154 | e5fe0c52 | pbrook | if (buf == NULL) { |
155 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: no memory for read buffer\n");
|
156 | e5fe0c52 | pbrook | retval = -ENOMEM; |
157 | e5fe0c52 | pbrook | goto out_free;
|
158 | e5fe0c52 | pbrook | } |
159 | e5fe0c52 | pbrook | |
160 | e5fe0c52 | pbrook | /* Read in first chunk of data and parse gzip header. */
|
161 | e5fe0c52 | pbrook | fpos = offset; |
162 | e5fe0c52 | pbrook | ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos); |
163 | e5fe0c52 | pbrook | |
164 | e5fe0c52 | pbrook | strm.next_in = buf; |
165 | e5fe0c52 | pbrook | strm.avail_in = ret; |
166 | e5fe0c52 | pbrook | strm.total_in = 0;
|
167 | e5fe0c52 | pbrook | |
168 | e5fe0c52 | pbrook | retval = -ENOEXEC; |
169 | e5fe0c52 | pbrook | |
170 | e5fe0c52 | pbrook | /* Check minimum size -- gzip header */
|
171 | e5fe0c52 | pbrook | if (ret < 10) { |
172 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: file too small?\n");
|
173 | e5fe0c52 | pbrook | goto out_free_buf;
|
174 | e5fe0c52 | pbrook | } |
175 | e5fe0c52 | pbrook | |
176 | e5fe0c52 | pbrook | /* Check gzip magic number */
|
177 | e5fe0c52 | pbrook | if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) { |
178 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: unknown compression magic?\n");
|
179 | e5fe0c52 | pbrook | goto out_free_buf;
|
180 | e5fe0c52 | pbrook | } |
181 | e5fe0c52 | pbrook | |
182 | e5fe0c52 | pbrook | /* Check gzip method */
|
183 | e5fe0c52 | pbrook | if (buf[2] != 8) { |
184 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: unknown compression method?\n");
|
185 | e5fe0c52 | pbrook | goto out_free_buf;
|
186 | e5fe0c52 | pbrook | } |
187 | e5fe0c52 | pbrook | /* Check gzip flags */
|
188 | e5fe0c52 | pbrook | if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) || |
189 | e5fe0c52 | pbrook | (buf[3] & RESERVED)) {
|
190 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: unknown flags?\n");
|
191 | e5fe0c52 | pbrook | goto out_free_buf;
|
192 | e5fe0c52 | pbrook | } |
193 | e5fe0c52 | pbrook | |
194 | e5fe0c52 | pbrook | ret = 10;
|
195 | e5fe0c52 | pbrook | if (buf[3] & EXTRA_FIELD) { |
196 | e5fe0c52 | pbrook | ret += 2 + buf[10] + (buf[11] << 8); |
197 | e5fe0c52 | pbrook | if (unlikely(LBUFSIZE == ret)) {
|
198 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
|
199 | e5fe0c52 | pbrook | goto out_free_buf;
|
200 | e5fe0c52 | pbrook | } |
201 | e5fe0c52 | pbrook | } |
202 | e5fe0c52 | pbrook | if (buf[3] & ORIG_NAME) { |
203 | e5fe0c52 | pbrook | for (; ret < LBUFSIZE && (buf[ret] != 0); ret++) |
204 | e5fe0c52 | pbrook | ; |
205 | e5fe0c52 | pbrook | if (unlikely(LBUFSIZE == ret)) {
|
206 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
|
207 | e5fe0c52 | pbrook | goto out_free_buf;
|
208 | e5fe0c52 | pbrook | } |
209 | e5fe0c52 | pbrook | } |
210 | e5fe0c52 | pbrook | if (buf[3] & COMMENT) { |
211 | e5fe0c52 | pbrook | for (; ret < LBUFSIZE && (buf[ret] != 0); ret++) |
212 | e5fe0c52 | pbrook | ; |
213 | e5fe0c52 | pbrook | if (unlikely(LBUFSIZE == ret)) {
|
214 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
|
215 | e5fe0c52 | pbrook | goto out_free_buf;
|
216 | e5fe0c52 | pbrook | } |
217 | e5fe0c52 | pbrook | } |
218 | e5fe0c52 | pbrook | |
219 | e5fe0c52 | pbrook | strm.next_in += ret; |
220 | e5fe0c52 | pbrook | strm.avail_in -= ret; |
221 | e5fe0c52 | pbrook | |
222 | e5fe0c52 | pbrook | strm.next_out = dst; |
223 | e5fe0c52 | pbrook | strm.avail_out = len; |
224 | e5fe0c52 | pbrook | strm.total_out = 0;
|
225 | e5fe0c52 | pbrook | |
226 | e5fe0c52 | pbrook | if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
|
227 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: zlib init failed?\n");
|
228 | e5fe0c52 | pbrook | goto out_free_buf;
|
229 | e5fe0c52 | pbrook | } |
230 | e5fe0c52 | pbrook | |
231 | e5fe0c52 | pbrook | while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
|
232 | e5fe0c52 | pbrook | ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos); |
233 | e5fe0c52 | pbrook | if (ret <= 0) |
234 | e5fe0c52 | pbrook | break;
|
235 | e5fe0c52 | pbrook | if (ret >= (unsigned long) -4096) |
236 | e5fe0c52 | pbrook | break;
|
237 | e5fe0c52 | pbrook | len -= ret; |
238 | e5fe0c52 | pbrook | |
239 | e5fe0c52 | pbrook | strm.next_in = buf; |
240 | e5fe0c52 | pbrook | strm.avail_in = ret; |
241 | e5fe0c52 | pbrook | strm.total_in = 0;
|
242 | e5fe0c52 | pbrook | } |
243 | e5fe0c52 | pbrook | |
244 | e5fe0c52 | pbrook | if (ret < 0) { |
245 | e5fe0c52 | pbrook | DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
|
246 | e5fe0c52 | pbrook | ret, strm.msg); |
247 | e5fe0c52 | pbrook | goto out_zlib;
|
248 | e5fe0c52 | pbrook | } |
249 | e5fe0c52 | pbrook | |
250 | e5fe0c52 | pbrook | retval = 0;
|
251 | e5fe0c52 | pbrook | out_zlib:
|
252 | e5fe0c52 | pbrook | zlib_inflateEnd(&strm); |
253 | e5fe0c52 | pbrook | out_free_buf:
|
254 | e5fe0c52 | pbrook | kfree(buf); |
255 | e5fe0c52 | pbrook | out_free:
|
256 | e5fe0c52 | pbrook | kfree(strm.workspace); |
257 | e5fe0c52 | pbrook | out:
|
258 | e5fe0c52 | pbrook | return retval;
|
259 | e5fe0c52 | pbrook | } |
260 | e5fe0c52 | pbrook | |
261 | e5fe0c52 | pbrook | #endif /* CONFIG_BINFMT_ZFLAT */ |
262 | e5fe0c52 | pbrook | |
263 | e5fe0c52 | pbrook | /****************************************************************************/
|
264 | e5fe0c52 | pbrook | |
265 | e5fe0c52 | pbrook | static target_ulong
|
266 | e5fe0c52 | pbrook | calc_reloc(target_ulong r, struct lib_info *p, int curid, int internalp) |
267 | e5fe0c52 | pbrook | { |
268 | e5fe0c52 | pbrook | target_ulong addr; |
269 | e5fe0c52 | pbrook | int id;
|
270 | e5fe0c52 | pbrook | target_ulong start_brk; |
271 | e5fe0c52 | pbrook | target_ulong start_data; |
272 | e5fe0c52 | pbrook | target_ulong text_len; |
273 | e5fe0c52 | pbrook | target_ulong start_code; |
274 | e5fe0c52 | pbrook | |
275 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_SHARED_FLAT
|
276 | e5fe0c52 | pbrook | #error needs checking
|
277 | e5fe0c52 | pbrook | if (r == 0) |
278 | e5fe0c52 | pbrook | id = curid; /* Relocs of 0 are always self referring */
|
279 | e5fe0c52 | pbrook | else {
|
280 | e5fe0c52 | pbrook | id = (r >> 24) & 0xff; /* Find ID for this reloc */ |
281 | e5fe0c52 | pbrook | r &= 0x00ffffff; /* Trim ID off here */ |
282 | e5fe0c52 | pbrook | } |
283 | e5fe0c52 | pbrook | if (id >= MAX_SHARED_LIBS) {
|
284 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: reference 0x%x to shared library %d\n",
|
285 | e5fe0c52 | pbrook | (unsigned) r, id);
|
286 | e5fe0c52 | pbrook | goto failed;
|
287 | e5fe0c52 | pbrook | } |
288 | e5fe0c52 | pbrook | if (curid != id) {
|
289 | e5fe0c52 | pbrook | if (internalp) {
|
290 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: reloc address 0x%x not "
|
291 | e5fe0c52 | pbrook | "in same module (%d != %d)\n",
|
292 | e5fe0c52 | pbrook | (unsigned) r, curid, id);
|
293 | e5fe0c52 | pbrook | goto failed;
|
294 | e5fe0c52 | pbrook | } else if ( ! p[id].loaded && |
295 | e5fe0c52 | pbrook | load_flat_shared_library(id, p) > (unsigned long) -4096) { |
296 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
|
297 | e5fe0c52 | pbrook | goto failed;
|
298 | e5fe0c52 | pbrook | } |
299 | e5fe0c52 | pbrook | /* Check versioning information (i.e. time stamps) */
|
300 | e5fe0c52 | pbrook | if (p[id].build_date && p[curid].build_date
|
301 | e5fe0c52 | pbrook | && p[curid].build_date < p[id].build_date) { |
302 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: library %d is younger than %d\n",
|
303 | e5fe0c52 | pbrook | id, curid); |
304 | e5fe0c52 | pbrook | goto failed;
|
305 | e5fe0c52 | pbrook | } |
306 | e5fe0c52 | pbrook | } |
307 | e5fe0c52 | pbrook | #else
|
308 | e5fe0c52 | pbrook | id = 0;
|
309 | e5fe0c52 | pbrook | #endif
|
310 | e5fe0c52 | pbrook | |
311 | e5fe0c52 | pbrook | start_brk = p[id].start_brk; |
312 | e5fe0c52 | pbrook | start_data = p[id].start_data; |
313 | e5fe0c52 | pbrook | start_code = p[id].start_code; |
314 | e5fe0c52 | pbrook | text_len = p[id].text_len; |
315 | e5fe0c52 | pbrook | |
316 | e5fe0c52 | pbrook | if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
|
317 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
|
318 | e5fe0c52 | pbrook | "(0 - 0x%x/0x%x)\n",
|
319 | e5fe0c52 | pbrook | (int) r,(int)(start_brk-start_code),(int)text_len); |
320 | e5fe0c52 | pbrook | goto failed;
|
321 | e5fe0c52 | pbrook | } |
322 | e5fe0c52 | pbrook | |
323 | e5fe0c52 | pbrook | if (r < text_len) /* In text segment */ |
324 | e5fe0c52 | pbrook | addr = r + start_code; |
325 | e5fe0c52 | pbrook | else /* In data segment */ |
326 | e5fe0c52 | pbrook | addr = r - text_len + start_data; |
327 | e5fe0c52 | pbrook | |
328 | e5fe0c52 | pbrook | /* Range checked already above so doing the range tests is redundant...*/
|
329 | e5fe0c52 | pbrook | return(addr);
|
330 | e5fe0c52 | pbrook | |
331 | e5fe0c52 | pbrook | failed:
|
332 | e5fe0c52 | pbrook | abort(); |
333 | e5fe0c52 | pbrook | return RELOC_FAILED;
|
334 | e5fe0c52 | pbrook | } |
335 | e5fe0c52 | pbrook | |
336 | e5fe0c52 | pbrook | /****************************************************************************/
|
337 | e5fe0c52 | pbrook | |
338 | e5fe0c52 | pbrook | /* ??? This does not handle endianness correctly. */
|
339 | e5fe0c52 | pbrook | void old_reloc(struct lib_info *libinfo, uint32_t rl) |
340 | e5fe0c52 | pbrook | { |
341 | e5fe0c52 | pbrook | #ifdef DEBUG
|
342 | e5fe0c52 | pbrook | char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" }; |
343 | e5fe0c52 | pbrook | #endif
|
344 | e5fe0c52 | pbrook | uint32_t *ptr; |
345 | e5fe0c52 | pbrook | uint32_t offset; |
346 | e5fe0c52 | pbrook | int reloc_type;
|
347 | e5fe0c52 | pbrook | |
348 | e5fe0c52 | pbrook | offset = rl & 0x3fffffff;
|
349 | e5fe0c52 | pbrook | reloc_type = rl >> 30;
|
350 | e5fe0c52 | pbrook | /* ??? How to handle this? */
|
351 | e5fe0c52 | pbrook | #if defined(CONFIG_COLDFIRE)
|
352 | e5fe0c52 | pbrook | ptr = (uint32_t *) (libinfo->start_code + offset); |
353 | e5fe0c52 | pbrook | #else
|
354 | e5fe0c52 | pbrook | ptr = (uint32_t *) (libinfo->start_data + offset); |
355 | e5fe0c52 | pbrook | #endif
|
356 | e5fe0c52 | pbrook | |
357 | e5fe0c52 | pbrook | #ifdef DEBUG
|
358 | e5fe0c52 | pbrook | fprintf(stderr, "Relocation of variable at DATASEG+%x "
|
359 | e5fe0c52 | pbrook | "(address %p, currently %x) into segment %s\n",
|
360 | e5fe0c52 | pbrook | offset, ptr, (int)*ptr, segment[reloc_type]);
|
361 | e5fe0c52 | pbrook | #endif
|
362 | 5fafdf24 | ths | |
363 | e5fe0c52 | pbrook | switch (reloc_type) {
|
364 | e5fe0c52 | pbrook | case OLD_FLAT_RELOC_TYPE_TEXT:
|
365 | e5fe0c52 | pbrook | *ptr += libinfo->start_code; |
366 | e5fe0c52 | pbrook | break;
|
367 | e5fe0c52 | pbrook | case OLD_FLAT_RELOC_TYPE_DATA:
|
368 | e5fe0c52 | pbrook | *ptr += libinfo->start_data; |
369 | e5fe0c52 | pbrook | break;
|
370 | e5fe0c52 | pbrook | case OLD_FLAT_RELOC_TYPE_BSS:
|
371 | e5fe0c52 | pbrook | *ptr += libinfo->end_data; |
372 | e5fe0c52 | pbrook | break;
|
373 | e5fe0c52 | pbrook | default:
|
374 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
|
375 | e5fe0c52 | pbrook | reloc_type); |
376 | e5fe0c52 | pbrook | break;
|
377 | e5fe0c52 | pbrook | } |
378 | e5fe0c52 | pbrook | DBG_FLT("Relocation became %x\n", (int)*ptr); |
379 | 3b46e624 | ths | } |
380 | e5fe0c52 | pbrook | |
381 | e5fe0c52 | pbrook | /****************************************************************************/
|
382 | e5fe0c52 | pbrook | |
383 | e5fe0c52 | pbrook | static int load_flat_file(struct linux_binprm * bprm, |
384 | e5fe0c52 | pbrook | struct lib_info *libinfo, int id, target_ulong *extra_stack) |
385 | e5fe0c52 | pbrook | { |
386 | e5fe0c52 | pbrook | struct flat_hdr * hdr;
|
387 | e5fe0c52 | pbrook | target_ulong textpos = 0, datapos = 0, result; |
388 | e5fe0c52 | pbrook | target_ulong realdatastart = 0;
|
389 | e5fe0c52 | pbrook | target_ulong text_len, data_len, bss_len, stack_len, flags; |
390 | e5fe0c52 | pbrook | target_ulong memp = 0; /* for finding the brk area */ |
391 | e5fe0c52 | pbrook | target_ulong extra; |
392 | e5fe0c52 | pbrook | target_ulong reloc = 0, rp;
|
393 | e5fe0c52 | pbrook | int i, rev, relocs = 0; |
394 | e5fe0c52 | pbrook | target_ulong fpos; |
395 | e5fe0c52 | pbrook | target_ulong start_code, end_code; |
396 | 2a1094cd | pbrook | target_ulong indx_len; |
397 | e5fe0c52 | pbrook | |
398 | e5fe0c52 | pbrook | hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */ |
399 | e5fe0c52 | pbrook | |
400 | e5fe0c52 | pbrook | text_len = ntohl(hdr->data_start); |
401 | e5fe0c52 | pbrook | data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start); |
402 | e5fe0c52 | pbrook | bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end); |
403 | e5fe0c52 | pbrook | stack_len = ntohl(hdr->stack_size); |
404 | e5fe0c52 | pbrook | if (extra_stack) {
|
405 | e5fe0c52 | pbrook | stack_len += *extra_stack; |
406 | e5fe0c52 | pbrook | *extra_stack = stack_len; |
407 | e5fe0c52 | pbrook | } |
408 | e5fe0c52 | pbrook | relocs = ntohl(hdr->reloc_count); |
409 | e5fe0c52 | pbrook | flags = ntohl(hdr->flags); |
410 | e5fe0c52 | pbrook | rev = ntohl(hdr->rev); |
411 | e5fe0c52 | pbrook | |
412 | e5fe0c52 | pbrook | DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
|
413 | e5fe0c52 | pbrook | |
414 | e5fe0c52 | pbrook | if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
|
415 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
|
416 | e5fe0c52 | pbrook | rev, (int) FLAT_VERSION);
|
417 | e5fe0c52 | pbrook | return -ENOEXEC;
|
418 | e5fe0c52 | pbrook | } |
419 | 3b46e624 | ths | |
420 | e5fe0c52 | pbrook | /* Don't allow old format executables to use shared libraries */
|
421 | e5fe0c52 | pbrook | if (rev == OLD_FLAT_VERSION && id != 0) { |
422 | e5fe0c52 | pbrook | fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
|
423 | e5fe0c52 | pbrook | return -ENOEXEC;
|
424 | e5fe0c52 | pbrook | } |
425 | e5fe0c52 | pbrook | |
426 | e5fe0c52 | pbrook | /*
|
427 | e5fe0c52 | pbrook | * fix up the flags for the older format, there were all kinds
|
428 | e5fe0c52 | pbrook | * of endian hacks, this only works for the simple cases
|
429 | e5fe0c52 | pbrook | */
|
430 | e5fe0c52 | pbrook | if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
|
431 | e5fe0c52 | pbrook | flags = FLAT_FLAG_RAM; |
432 | e5fe0c52 | pbrook | |
433 | e5fe0c52 | pbrook | #ifndef CONFIG_BINFMT_ZFLAT
|
434 | e5fe0c52 | pbrook | if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
|
435 | e5fe0c52 | pbrook | fprintf(stderr, "Support for ZFLAT executables is not enabled\n");
|
436 | e5fe0c52 | pbrook | return -ENOEXEC;
|
437 | e5fe0c52 | pbrook | } |
438 | e5fe0c52 | pbrook | #endif
|
439 | e5fe0c52 | pbrook | |
440 | e5fe0c52 | pbrook | /*
|
441 | e5fe0c52 | pbrook | * calculate the extra space we need to map in
|
442 | e5fe0c52 | pbrook | */
|
443 | e5fe0c52 | pbrook | extra = relocs * sizeof(target_ulong);
|
444 | e5fe0c52 | pbrook | if (extra < bss_len + stack_len)
|
445 | e5fe0c52 | pbrook | extra = bss_len + stack_len; |
446 | e5fe0c52 | pbrook | |
447 | 2a1094cd | pbrook | /* Add space for library base pointers. Make sure this does not
|
448 | 2a1094cd | pbrook | misalign the doesn't misalign the data segment. */
|
449 | 2a1094cd | pbrook | indx_len = MAX_SHARED_LIBS * sizeof(target_ulong);
|
450 | 2a1094cd | pbrook | indx_len = (indx_len + 15) & ~(target_ulong)15; |
451 | 2a1094cd | pbrook | |
452 | e5fe0c52 | pbrook | /*
|
453 | e5fe0c52 | pbrook | * there are a couple of cases here, the separate code/data
|
454 | e5fe0c52 | pbrook | * case, and then the fully copied to RAM case which lumps
|
455 | e5fe0c52 | pbrook | * it all together.
|
456 | e5fe0c52 | pbrook | */
|
457 | e5fe0c52 | pbrook | if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) { |
458 | e5fe0c52 | pbrook | /*
|
459 | e5fe0c52 | pbrook | * this should give us a ROM ptr, but if it doesn't we don't
|
460 | e5fe0c52 | pbrook | * really care
|
461 | e5fe0c52 | pbrook | */
|
462 | e5fe0c52 | pbrook | DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
|
463 | e5fe0c52 | pbrook | |
464 | e5fe0c52 | pbrook | textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
|
465 | e5fe0c52 | pbrook | MAP_PRIVATE, bprm->fd, 0);
|
466 | 5fafdf24 | ths | if (textpos == -1) { |
467 | e5fe0c52 | pbrook | fprintf(stderr, "Unable to mmap process text\n");
|
468 | e5fe0c52 | pbrook | return -1; |
469 | e5fe0c52 | pbrook | } |
470 | e5fe0c52 | pbrook | |
471 | 2a1094cd | pbrook | realdatastart = target_mmap(0, data_len + extra + indx_len,
|
472 | e5fe0c52 | pbrook | PROT_READ|PROT_WRITE|PROT_EXEC, |
473 | e5fe0c52 | pbrook | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
474 | e5fe0c52 | pbrook | |
475 | e5fe0c52 | pbrook | if (realdatastart == -1) { |
476 | e5fe0c52 | pbrook | fprintf(stderr, "Unable to allocate RAM for process data\n");
|
477 | e5fe0c52 | pbrook | return realdatastart;
|
478 | e5fe0c52 | pbrook | } |
479 | 2a1094cd | pbrook | datapos = realdatastart + indx_len; |
480 | e5fe0c52 | pbrook | |
481 | e5fe0c52 | pbrook | DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
|
482 | e5fe0c52 | pbrook | (int)(data_len + bss_len + stack_len), (int)datapos); |
483 | e5fe0c52 | pbrook | |
484 | e5fe0c52 | pbrook | fpos = ntohl(hdr->data_start); |
485 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_ZFLAT
|
486 | e5fe0c52 | pbrook | if (flags & FLAT_FLAG_GZDATA) {
|
487 | 5fafdf24 | ths | result = decompress_exec(bprm, fpos, (char *) datapos,
|
488 | e5fe0c52 | pbrook | data_len + (relocs * sizeof(target_ulong)))
|
489 | e5fe0c52 | pbrook | } else
|
490 | e5fe0c52 | pbrook | #endif
|
491 | e5fe0c52 | pbrook | { |
492 | e5fe0c52 | pbrook | result = target_pread(bprm->fd, datapos, |
493 | e5fe0c52 | pbrook | data_len + (relocs * sizeof(target_ulong)),
|
494 | e5fe0c52 | pbrook | fpos); |
495 | e5fe0c52 | pbrook | } |
496 | e5fe0c52 | pbrook | if (result < 0) { |
497 | e5fe0c52 | pbrook | fprintf(stderr, "Unable to read data+bss\n");
|
498 | e5fe0c52 | pbrook | return result;
|
499 | e5fe0c52 | pbrook | } |
500 | e5fe0c52 | pbrook | |
501 | e5fe0c52 | pbrook | reloc = datapos + (ntohl(hdr->reloc_start) - text_len); |
502 | e5fe0c52 | pbrook | memp = realdatastart; |
503 | e5fe0c52 | pbrook | |
504 | e5fe0c52 | pbrook | } else {
|
505 | e5fe0c52 | pbrook | |
506 | 2a1094cd | pbrook | textpos = target_mmap(0, text_len + data_len + extra + indx_len,
|
507 | e5fe0c52 | pbrook | PROT_READ | PROT_EXEC | PROT_WRITE, |
508 | e5fe0c52 | pbrook | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
509 | e5fe0c52 | pbrook | if (textpos == -1 ) { |
510 | e5fe0c52 | pbrook | fprintf(stderr, "Unable to allocate RAM for process text/data\n");
|
511 | e5fe0c52 | pbrook | return -1; |
512 | e5fe0c52 | pbrook | } |
513 | e5fe0c52 | pbrook | |
514 | e5fe0c52 | pbrook | realdatastart = textpos + ntohl(hdr->data_start); |
515 | 2a1094cd | pbrook | datapos = realdatastart + indx_len; |
516 | 2a1094cd | pbrook | reloc = (textpos + ntohl(hdr->reloc_start) + indx_len); |
517 | e5fe0c52 | pbrook | memp = textpos; |
518 | e5fe0c52 | pbrook | |
519 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_ZFLAT
|
520 | e5fe0c52 | pbrook | #error code needs checking
|
521 | e5fe0c52 | pbrook | /*
|
522 | e5fe0c52 | pbrook | * load it all in and treat it like a RAM load from now on
|
523 | e5fe0c52 | pbrook | */
|
524 | e5fe0c52 | pbrook | if (flags & FLAT_FLAG_GZIP) {
|
525 | e5fe0c52 | pbrook | result = decompress_exec(bprm, sizeof (struct flat_hdr), |
526 | e5fe0c52 | pbrook | (((char *) textpos) + sizeof (struct flat_hdr)), |
527 | e5fe0c52 | pbrook | (text_len + data_len + (relocs * sizeof(unsigned long)) |
528 | e5fe0c52 | pbrook | - sizeof (struct flat_hdr)), |
529 | e5fe0c52 | pbrook | 0);
|
530 | e5fe0c52 | pbrook | memmove((void *) datapos, (void *) realdatastart, |
531 | e5fe0c52 | pbrook | data_len + (relocs * sizeof(unsigned long))); |
532 | e5fe0c52 | pbrook | } else if (flags & FLAT_FLAG_GZDATA) { |
533 | e5fe0c52 | pbrook | fpos = 0;
|
534 | e5fe0c52 | pbrook | result = bprm->file->f_op->read(bprm->file, |
535 | e5fe0c52 | pbrook | (char *) textpos, text_len, &fpos);
|
536 | e5fe0c52 | pbrook | if (result < (unsigned long) -4096) |
537 | e5fe0c52 | pbrook | result = decompress_exec(bprm, text_len, (char *) datapos,
|
538 | e5fe0c52 | pbrook | data_len + (relocs * sizeof(unsigned long)), 0); |
539 | e5fe0c52 | pbrook | } |
540 | e5fe0c52 | pbrook | else
|
541 | e5fe0c52 | pbrook | #endif
|
542 | e5fe0c52 | pbrook | { |
543 | e5fe0c52 | pbrook | result = target_pread(bprm->fd, textpos, |
544 | e5fe0c52 | pbrook | text_len, 0);
|
545 | e5fe0c52 | pbrook | if (result >= 0) { |
546 | e5fe0c52 | pbrook | result = target_pread(bprm->fd, datapos, |
547 | e5fe0c52 | pbrook | data_len + (relocs * sizeof(target_ulong)),
|
548 | e5fe0c52 | pbrook | ntohl(hdr->data_start)); |
549 | e5fe0c52 | pbrook | } |
550 | e5fe0c52 | pbrook | } |
551 | e5fe0c52 | pbrook | if (result < 0) { |
552 | e5fe0c52 | pbrook | fprintf(stderr, "Unable to read code+data+bss\n");
|
553 | e5fe0c52 | pbrook | return result;
|
554 | e5fe0c52 | pbrook | } |
555 | e5fe0c52 | pbrook | } |
556 | e5fe0c52 | pbrook | |
557 | e5fe0c52 | pbrook | DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
|
558 | e5fe0c52 | pbrook | (int)textpos, 0x00ffffff&ntohl(hdr->entry), |
559 | e5fe0c52 | pbrook | ntohl(hdr->data_start)); |
560 | e5fe0c52 | pbrook | |
561 | e5fe0c52 | pbrook | /* The main program needs a little extra setup in the task structure */
|
562 | e5fe0c52 | pbrook | start_code = textpos + sizeof (struct flat_hdr); |
563 | e5fe0c52 | pbrook | end_code = textpos + text_len; |
564 | e5fe0c52 | pbrook | |
565 | e5fe0c52 | pbrook | DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
|
566 | e5fe0c52 | pbrook | id ? "Lib" : "Load", bprm->filename, |
567 | e5fe0c52 | pbrook | (int) start_code, (int) end_code, |
568 | e5fe0c52 | pbrook | (int) datapos,
|
569 | e5fe0c52 | pbrook | (int) (datapos + data_len),
|
570 | e5fe0c52 | pbrook | (int) (datapos + data_len),
|
571 | e5fe0c52 | pbrook | (int) (((datapos + data_len + bss_len) + 3) & ~3)); |
572 | e5fe0c52 | pbrook | |
573 | e5fe0c52 | pbrook | text_len -= sizeof(struct flat_hdr); /* the real code len */ |
574 | e5fe0c52 | pbrook | |
575 | e5fe0c52 | pbrook | /* Store the current module values into the global library structure */
|
576 | e5fe0c52 | pbrook | libinfo[id].start_code = start_code; |
577 | e5fe0c52 | pbrook | libinfo[id].start_data = datapos; |
578 | e5fe0c52 | pbrook | libinfo[id].end_data = datapos + data_len; |
579 | e5fe0c52 | pbrook | libinfo[id].start_brk = datapos + data_len + bss_len; |
580 | e5fe0c52 | pbrook | libinfo[id].text_len = text_len; |
581 | e5fe0c52 | pbrook | libinfo[id].loaded = 1;
|
582 | e5fe0c52 | pbrook | libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
|
583 | e5fe0c52 | pbrook | libinfo[id].build_date = ntohl(hdr->build_date); |
584 | 3b46e624 | ths | |
585 | e5fe0c52 | pbrook | /*
|
586 | e5fe0c52 | pbrook | * We just load the allocations into some temporary memory to
|
587 | e5fe0c52 | pbrook | * help simplify all this mumbo jumbo
|
588 | e5fe0c52 | pbrook | *
|
589 | e5fe0c52 | pbrook | * We've got two different sections of relocation entries.
|
590 | e5fe0c52 | pbrook | * The first is the GOT which resides at the begining of the data segment
|
591 | e5fe0c52 | pbrook | * and is terminated with a -1. This one can be relocated in place.
|
592 | e5fe0c52 | pbrook | * The second is the extra relocation entries tacked after the image's
|
593 | e5fe0c52 | pbrook | * data segment. These require a little more processing as the entry is
|
594 | e5fe0c52 | pbrook | * really an offset into the image which contains an offset into the
|
595 | e5fe0c52 | pbrook | * image.
|
596 | e5fe0c52 | pbrook | */
|
597 | e5fe0c52 | pbrook | if (flags & FLAT_FLAG_GOTPIC) {
|
598 | e5fe0c52 | pbrook | rp = datapos; |
599 | e5fe0c52 | pbrook | while (1) { |
600 | e5fe0c52 | pbrook | target_ulong addr; |
601 | e5fe0c52 | pbrook | addr = tgetl(rp); |
602 | e5fe0c52 | pbrook | if (addr == -1) |
603 | e5fe0c52 | pbrook | break;
|
604 | e5fe0c52 | pbrook | if (addr) {
|
605 | e5fe0c52 | pbrook | addr = calc_reloc(addr, libinfo, id, 0);
|
606 | e5fe0c52 | pbrook | if (addr == RELOC_FAILED)
|
607 | e5fe0c52 | pbrook | return -ENOEXEC;
|
608 | e5fe0c52 | pbrook | tputl(rp, addr); |
609 | e5fe0c52 | pbrook | } |
610 | e5fe0c52 | pbrook | rp += sizeof(target_ulong);
|
611 | e5fe0c52 | pbrook | } |
612 | e5fe0c52 | pbrook | } |
613 | e5fe0c52 | pbrook | |
614 | e5fe0c52 | pbrook | /*
|
615 | e5fe0c52 | pbrook | * Now run through the relocation entries.
|
616 | e5fe0c52 | pbrook | * We've got to be careful here as C++ produces relocatable zero
|
617 | e5fe0c52 | pbrook | * entries in the constructor and destructor tables which are then
|
618 | e5fe0c52 | pbrook | * tested for being not zero (which will always occur unless we're
|
619 | e5fe0c52 | pbrook | * based from address zero). This causes an endless loop as __start
|
620 | e5fe0c52 | pbrook | * is at zero. The solution used is to not relocate zero addresses.
|
621 | e5fe0c52 | pbrook | * This has the negative side effect of not allowing a global data
|
622 | e5fe0c52 | pbrook | * reference to be statically initialised to _stext (I've moved
|
623 | e5fe0c52 | pbrook | * __start to address 4 so that is okay).
|
624 | e5fe0c52 | pbrook | */
|
625 | e5fe0c52 | pbrook | if (rev > OLD_FLAT_VERSION) {
|
626 | e5fe0c52 | pbrook | for (i = 0; i < relocs; i++) { |
627 | e5fe0c52 | pbrook | target_ulong addr, relval; |
628 | e5fe0c52 | pbrook | |
629 | e5fe0c52 | pbrook | /* Get the address of the pointer to be
|
630 | e5fe0c52 | pbrook | relocated (of course, the address has to be
|
631 | e5fe0c52 | pbrook | relocated first). */
|
632 | e5fe0c52 | pbrook | relval = tgetl(reloc + i * sizeof (target_ulong));
|
633 | e5fe0c52 | pbrook | addr = flat_get_relocate_addr(relval); |
634 | e5fe0c52 | pbrook | rp = calc_reloc(addr, libinfo, id, 1);
|
635 | e5fe0c52 | pbrook | if (rp == RELOC_FAILED)
|
636 | e5fe0c52 | pbrook | return -ENOEXEC;
|
637 | e5fe0c52 | pbrook | |
638 | e5fe0c52 | pbrook | /* Get the pointer's value. */
|
639 | e5fe0c52 | pbrook | addr = tgetl(rp); |
640 | e5fe0c52 | pbrook | if (addr != 0) { |
641 | e5fe0c52 | pbrook | /*
|
642 | e5fe0c52 | pbrook | * Do the relocation. PIC relocs in the data section are
|
643 | e5fe0c52 | pbrook | * already in target order
|
644 | e5fe0c52 | pbrook | */
|
645 | e5fe0c52 | pbrook | |
646 | e5fe0c52 | pbrook | #ifndef TARGET_WORDS_BIGENDIAN
|
647 | e5fe0c52 | pbrook | if ((flags & FLAT_FLAG_GOTPIC) == 0) |
648 | e5fe0c52 | pbrook | addr = bswap32(addr); |
649 | e5fe0c52 | pbrook | #endif
|
650 | e5fe0c52 | pbrook | addr = calc_reloc(addr, libinfo, id, 0);
|
651 | e5fe0c52 | pbrook | if (addr == RELOC_FAILED)
|
652 | e5fe0c52 | pbrook | return -ENOEXEC;
|
653 | e5fe0c52 | pbrook | |
654 | e5fe0c52 | pbrook | /* Write back the relocated pointer. */
|
655 | e5fe0c52 | pbrook | tputl(rp, addr); |
656 | e5fe0c52 | pbrook | } |
657 | e5fe0c52 | pbrook | } |
658 | e5fe0c52 | pbrook | } else {
|
659 | e5fe0c52 | pbrook | for (i = 0; i < relocs; i++) { |
660 | e5fe0c52 | pbrook | target_ulong relval; |
661 | e5fe0c52 | pbrook | relval = tgetl(reloc + i * sizeof (target_ulong));
|
662 | e5fe0c52 | pbrook | old_reloc(&libinfo[0], relval);
|
663 | e5fe0c52 | pbrook | } |
664 | e5fe0c52 | pbrook | } |
665 | 3b46e624 | ths | |
666 | e5fe0c52 | pbrook | /* zero the BSS. */
|
667 | e5fe0c52 | pbrook | memset((void*)(datapos + data_len), 0, bss_len); |
668 | e5fe0c52 | pbrook | |
669 | e5fe0c52 | pbrook | return 0; |
670 | e5fe0c52 | pbrook | } |
671 | e5fe0c52 | pbrook | |
672 | e5fe0c52 | pbrook | |
673 | e5fe0c52 | pbrook | /****************************************************************************/
|
674 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_SHARED_FLAT
|
675 | e5fe0c52 | pbrook | |
676 | e5fe0c52 | pbrook | /*
|
677 | e5fe0c52 | pbrook | * Load a shared library into memory. The library gets its own data
|
678 | e5fe0c52 | pbrook | * segment (including bss) but not argv/argc/environ.
|
679 | e5fe0c52 | pbrook | */
|
680 | e5fe0c52 | pbrook | |
681 | e5fe0c52 | pbrook | static int load_flat_shared_library(int id, struct lib_info *libs) |
682 | e5fe0c52 | pbrook | { |
683 | e5fe0c52 | pbrook | struct linux_binprm bprm;
|
684 | e5fe0c52 | pbrook | int res;
|
685 | e5fe0c52 | pbrook | char buf[16]; |
686 | e5fe0c52 | pbrook | |
687 | e5fe0c52 | pbrook | /* Create the file name */
|
688 | e5fe0c52 | pbrook | sprintf(buf, "/lib/lib%d.so", id);
|
689 | e5fe0c52 | pbrook | |
690 | e5fe0c52 | pbrook | /* Open the file up */
|
691 | e5fe0c52 | pbrook | bprm.filename = buf; |
692 | e5fe0c52 | pbrook | bprm.file = open_exec(bprm.filename); |
693 | e5fe0c52 | pbrook | res = PTR_ERR(bprm.file); |
694 | e5fe0c52 | pbrook | if (IS_ERR(bprm.file))
|
695 | e5fe0c52 | pbrook | return res;
|
696 | e5fe0c52 | pbrook | |
697 | e5fe0c52 | pbrook | res = prepare_binprm(&bprm); |
698 | e5fe0c52 | pbrook | |
699 | e5fe0c52 | pbrook | if (res <= (unsigned long)-4096) |
700 | e5fe0c52 | pbrook | res = load_flat_file(&bprm, libs, id, NULL);
|
701 | e5fe0c52 | pbrook | if (bprm.file) {
|
702 | e5fe0c52 | pbrook | allow_write_access(bprm.file); |
703 | e5fe0c52 | pbrook | fput(bprm.file); |
704 | e5fe0c52 | pbrook | bprm.file = NULL;
|
705 | e5fe0c52 | pbrook | } |
706 | e5fe0c52 | pbrook | return(res);
|
707 | e5fe0c52 | pbrook | } |
708 | e5fe0c52 | pbrook | |
709 | e5fe0c52 | pbrook | #endif /* CONFIG_BINFMT_SHARED_FLAT */ |
710 | e5fe0c52 | pbrook | |
711 | e5fe0c52 | pbrook | int load_flt_binary(struct linux_binprm * bprm, struct target_pt_regs * regs, |
712 | e5fe0c52 | pbrook | struct image_info * info)
|
713 | e5fe0c52 | pbrook | { |
714 | e5fe0c52 | pbrook | struct lib_info libinfo[MAX_SHARED_LIBS];
|
715 | e5fe0c52 | pbrook | target_ulong p = bprm->p; |
716 | e5fe0c52 | pbrook | target_ulong stack_len; |
717 | e5fe0c52 | pbrook | target_ulong start_addr; |
718 | e5fe0c52 | pbrook | target_ulong sp; |
719 | e5fe0c52 | pbrook | int res;
|
720 | e5fe0c52 | pbrook | int i, j;
|
721 | e5fe0c52 | pbrook | |
722 | e5fe0c52 | pbrook | memset(libinfo, 0, sizeof(libinfo)); |
723 | e5fe0c52 | pbrook | /*
|
724 | e5fe0c52 | pbrook | * We have to add the size of our arguments to our stack size
|
725 | e5fe0c52 | pbrook | * otherwise it's too easy for users to create stack overflows
|
726 | e5fe0c52 | pbrook | * by passing in a huge argument list. And yes, we have to be
|
727 | e5fe0c52 | pbrook | * pedantic and include space for the argv/envp array as it may have
|
728 | e5fe0c52 | pbrook | * a lot of entries.
|
729 | e5fe0c52 | pbrook | */
|
730 | e5fe0c52 | pbrook | #define TOP_OF_ARGS (TARGET_PAGE_SIZE * MAX_ARG_PAGES - sizeof(void *)) |
731 | e5fe0c52 | pbrook | stack_len = TOP_OF_ARGS - bprm->p; /* the strings */
|
732 | e5fe0c52 | pbrook | stack_len += (bprm->argc + 1) * 4; /* the argv array */ |
733 | e5fe0c52 | pbrook | stack_len += (bprm->envc + 1) * 4; /* the envp array */ |
734 | e5fe0c52 | pbrook | |
735 | 3b46e624 | ths | |
736 | e5fe0c52 | pbrook | res = load_flat_file(bprm, libinfo, 0, &stack_len);
|
737 | e5fe0c52 | pbrook | if (res > (unsigned long)-4096) |
738 | e5fe0c52 | pbrook | return res;
|
739 | 3b46e624 | ths | |
740 | e5fe0c52 | pbrook | /* Update data segment pointers for all libraries */
|
741 | e5fe0c52 | pbrook | for (i=0; i<MAX_SHARED_LIBS; i++) { |
742 | e5fe0c52 | pbrook | if (libinfo[i].loaded) {
|
743 | e5fe0c52 | pbrook | target_ulong p; |
744 | e5fe0c52 | pbrook | p = libinfo[i].start_data; |
745 | e5fe0c52 | pbrook | for (j=0; j<MAX_SHARED_LIBS; j++) { |
746 | e5fe0c52 | pbrook | p -= 4;
|
747 | e5fe0c52 | pbrook | tput32(p, libinfo[j].loaded |
748 | e5fe0c52 | pbrook | ? libinfo[j].start_data |
749 | e5fe0c52 | pbrook | : UNLOADED_LIB); |
750 | e5fe0c52 | pbrook | } |
751 | e5fe0c52 | pbrook | } |
752 | e5fe0c52 | pbrook | } |
753 | e5fe0c52 | pbrook | |
754 | e5fe0c52 | pbrook | p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4; |
755 | e5fe0c52 | pbrook | DBG_FLT("p=%x\n", (int)p); |
756 | e5fe0c52 | pbrook | |
757 | e5fe0c52 | pbrook | /* Copy argv/envp. */
|
758 | e5fe0c52 | pbrook | p = copy_strings(p, bprm->envc, bprm->envp); |
759 | 388c4508 | pbrook | p = copy_strings(p, bprm->argc, bprm->argv); |
760 | e5fe0c52 | pbrook | /* Align stack. */
|
761 | e5fe0c52 | pbrook | sp = p & ~(target_ulong)(sizeof(target_ulong) - 1); |
762 | b35d7448 | pbrook | /* Enforce final stack alignment of 16 bytes. This is sufficient
|
763 | b35d7448 | pbrook | for all current targets, and excess alignment is harmless. */
|
764 | b35d7448 | pbrook | stack_len = bprm->envc + bprm->argc + 2;
|
765 | b35d7448 | pbrook | stack_len += 3; /* argc, arvg, argp */ |
766 | b35d7448 | pbrook | stack_len *= sizeof(target_ulong);
|
767 | b35d7448 | pbrook | if ((sp + stack_len) & 15) |
768 | b35d7448 | pbrook | sp -= 16 - ((sp + stack_len) & 15); |
769 | e5fe0c52 | pbrook | sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p, 1);
|
770 | 3b46e624 | ths | |
771 | e5fe0c52 | pbrook | /* Fake some return addresses to ensure the call chain will
|
772 | e5fe0c52 | pbrook | * initialise library in order for us. We are required to call
|
773 | e5fe0c52 | pbrook | * lib 1 first, then 2, ... and finally the main program (id 0).
|
774 | e5fe0c52 | pbrook | */
|
775 | e5fe0c52 | pbrook | start_addr = libinfo[0].entry;
|
776 | e5fe0c52 | pbrook | |
777 | e5fe0c52 | pbrook | #ifdef CONFIG_BINFMT_SHARED_FLAT
|
778 | e5fe0c52 | pbrook | #error here
|
779 | e5fe0c52 | pbrook | for (i = MAX_SHARED_LIBS-1; i>0; i--) { |
780 | e5fe0c52 | pbrook | if (libinfo[i].loaded) {
|
781 | e5fe0c52 | pbrook | /* Push previos first to call address */
|
782 | e5fe0c52 | pbrook | --sp; put_user(start_addr, sp); |
783 | e5fe0c52 | pbrook | start_addr = libinfo[i].entry; |
784 | e5fe0c52 | pbrook | } |
785 | e5fe0c52 | pbrook | } |
786 | e5fe0c52 | pbrook | #endif
|
787 | 3b46e624 | ths | |
788 | e5fe0c52 | pbrook | /* Stash our initial stack pointer into the mm structure */
|
789 | e5fe0c52 | pbrook | info->start_code = libinfo[0].start_code;
|
790 | e5fe0c52 | pbrook | info->end_code = libinfo[0].start_code = libinfo[0].text_len; |
791 | e5fe0c52 | pbrook | info->start_data = libinfo[0].start_data;
|
792 | e5fe0c52 | pbrook | info->end_data = libinfo[0].end_data;
|
793 | e5fe0c52 | pbrook | info->start_brk = libinfo[0].start_brk;
|
794 | e5fe0c52 | pbrook | info->start_stack = sp; |
795 | e5fe0c52 | pbrook | info->entry = start_addr; |
796 | 978efd6a | pbrook | info->code_offset = info->start_code; |
797 | 978efd6a | pbrook | info->data_offset = info->start_data - libinfo[0].text_len;
|
798 | 978efd6a | pbrook | |
799 | e5fe0c52 | pbrook | DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
|
800 | e5fe0c52 | pbrook | (int)info->entry, (int)info->start_stack); |
801 | 3b46e624 | ths | |
802 | e5fe0c52 | pbrook | return 0; |
803 | e5fe0c52 | pbrook | } |