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