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