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