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