linux-user: Split mmap prototypes into user-mmap.h
[qemu.git] / linux-user / flatload.c
blob99550061db8d15fc04ce4dde5a17a4d751acd675
1 /****************************************************************************/
2 /*
3 * QEMU bFLT binary loader. Based on linux/fs/binfmt_flat.c
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
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:
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)
32 /* ??? ZFLAT and shared library support is currently disabled. */
34 /****************************************************************************/
36 #include "qemu/osdep.h"
38 #include "qemu.h"
39 #include "loader.h"
40 #include "user-mmap.h"
41 #include "flat.h"
42 #include "target_flat.h"
44 //#define DEBUG
46 #ifdef DEBUG
47 #define DBG_FLT(...) printf(__VA_ARGS__)
48 #else
49 #define DBG_FLT(...)
50 #endif
52 #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */
53 #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */
55 struct lib_info {
56 abi_ulong start_code; /* Start of text segment */
57 abi_ulong start_data; /* Start of data segment */
58 abi_ulong end_data; /* Start of bss section */
59 abi_ulong start_brk; /* End of data segment */
60 abi_ulong text_len; /* Length of text segment */
61 abi_ulong entry; /* Start address for this module */
62 abi_ulong build_date; /* When this one was compiled */
63 short loaded; /* Has this library been loaded? */
66 #ifdef CONFIG_BINFMT_SHARED_FLAT
67 static int load_flat_shared_library(int id, struct lib_info *p);
68 #endif
70 struct linux_binprm;
72 /****************************************************************************/
74 * create_flat_tables() parses the env- and arg-strings in new user
75 * memory and creates the pointer tables from them, and puts their
76 * addresses on the "stack", returning the new stack pointer value.
79 /* Push a block of strings onto the guest stack. */
80 static abi_ulong copy_strings(abi_ulong p, int n, char **s)
82 int len;
84 while (n-- > 0) {
85 len = strlen(s[n]) + 1;
86 p -= len;
87 memcpy_to_target(p, s[n], len);
90 return p;
93 static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
94 abi_ulong offset)
96 void *buf;
97 int ret;
99 buf = lock_user(VERIFY_WRITE, ptr, len, 0);
100 if (!buf) {
101 return -EFAULT;
103 ret = pread(fd, buf, len, offset);
104 if (ret < 0) {
105 ret = -errno;
107 unlock_user(buf, ptr, len);
108 return ret;
110 /****************************************************************************/
112 #ifdef CONFIG_BINFMT_ZFLAT
114 #include <linux/zlib.h>
116 #define LBUFSIZE 4000
118 /* gzip flag byte */
119 #define ASCII_FLAG 0x01 /* bit 0 set: file probably ASCII text */
120 #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
121 #define EXTRA_FIELD 0x04 /* bit 2 set: extra field present */
122 #define ORIG_NAME 0x08 /* bit 3 set: original file name present */
123 #define COMMENT 0x10 /* bit 4 set: file comment present */
124 #define ENCRYPTED 0x20 /* bit 5 set: file is encrypted */
125 #define RESERVED 0xC0 /* bit 6,7: reserved */
127 static int decompress_exec(
128 struct linux_binprm *bprm,
129 unsigned long offset,
130 char *dst,
131 long len,
132 int fd)
134 unsigned char *buf;
135 z_stream strm;
136 loff_t fpos;
137 int ret, retval;
139 DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);
141 memset(&strm, 0, sizeof(strm));
142 strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
143 if (strm.workspace == NULL) {
144 DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
145 return -ENOMEM;
147 buf = kmalloc(LBUFSIZE, GFP_KERNEL);
148 if (buf == NULL) {
149 DBG_FLT("binfmt_flat: no memory for read buffer\n");
150 retval = -ENOMEM;
151 goto out_free;
154 /* Read in first chunk of data and parse gzip header. */
155 fpos = offset;
156 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
158 strm.next_in = buf;
159 strm.avail_in = ret;
160 strm.total_in = 0;
162 retval = -ENOEXEC;
164 /* Check minimum size -- gzip header */
165 if (ret < 10) {
166 DBG_FLT("binfmt_flat: file too small?\n");
167 goto out_free_buf;
170 /* Check gzip magic number */
171 if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
172 DBG_FLT("binfmt_flat: unknown compression magic?\n");
173 goto out_free_buf;
176 /* Check gzip method */
177 if (buf[2] != 8) {
178 DBG_FLT("binfmt_flat: unknown compression method?\n");
179 goto out_free_buf;
181 /* Check gzip flags */
182 if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
183 (buf[3] & RESERVED)) {
184 DBG_FLT("binfmt_flat: unknown flags?\n");
185 goto out_free_buf;
188 ret = 10;
189 if (buf[3] & EXTRA_FIELD) {
190 ret += 2 + buf[10] + (buf[11] << 8);
191 if (unlikely(LBUFSIZE == ret)) {
192 DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
193 goto out_free_buf;
196 if (buf[3] & ORIG_NAME) {
197 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
199 if (unlikely(LBUFSIZE == ret)) {
200 DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
201 goto out_free_buf;
204 if (buf[3] & COMMENT) {
205 for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
207 if (unlikely(LBUFSIZE == ret)) {
208 DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
209 goto out_free_buf;
213 strm.next_in += ret;
214 strm.avail_in -= ret;
216 strm.next_out = dst;
217 strm.avail_out = len;
218 strm.total_out = 0;
220 if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
221 DBG_FLT("binfmt_flat: zlib init failed?\n");
222 goto out_free_buf;
225 while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
226 ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
227 if (ret <= 0)
228 break;
229 if (is_error(ret)) {
230 break;
232 len -= ret;
234 strm.next_in = buf;
235 strm.avail_in = ret;
236 strm.total_in = 0;
239 if (ret < 0) {
240 DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
241 ret, strm.msg);
242 goto out_zlib;
245 retval = 0;
246 out_zlib:
247 zlib_inflateEnd(&strm);
248 out_free_buf:
249 kfree(buf);
250 out_free:
251 kfree(strm.workspace);
252 out:
253 return retval;
256 #endif /* CONFIG_BINFMT_ZFLAT */
258 /****************************************************************************/
260 static abi_ulong
261 calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
263 abi_ulong addr;
264 int id;
265 abi_ulong start_brk;
266 abi_ulong start_data;
267 abi_ulong text_len;
268 abi_ulong start_code;
270 #ifdef CONFIG_BINFMT_SHARED_FLAT
271 #error needs checking
272 if (r == 0)
273 id = curid; /* Relocs of 0 are always self referring */
274 else {
275 id = (r >> 24) & 0xff; /* Find ID for this reloc */
276 r &= 0x00ffffff; /* Trim ID off here */
278 if (id >= MAX_SHARED_LIBS) {
279 fprintf(stderr, "BINFMT_FLAT: reference 0x%x to shared library %d\n",
280 (unsigned) r, id);
281 goto failed;
283 if (curid != id) {
284 if (internalp) {
285 fprintf(stderr, "BINFMT_FLAT: reloc address 0x%x not "
286 "in same module (%d != %d)\n",
287 (unsigned) r, curid, id);
288 goto failed;
289 } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
290 fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
291 goto failed;
293 /* Check versioning information (i.e. time stamps) */
294 if (p[id].build_date && p[curid].build_date
295 && p[curid].build_date < p[id].build_date) {
296 fprintf(stderr, "BINFMT_FLAT: library %d is younger than %d\n",
297 id, curid);
298 goto failed;
301 #else
302 id = 0;
303 #endif
305 start_brk = p[id].start_brk;
306 start_data = p[id].start_data;
307 start_code = p[id].start_code;
308 text_len = p[id].text_len;
310 if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
311 fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
312 "(0 - 0x%x/0x%x)\n",
313 (int) r,(int)(start_brk-start_code),(int)text_len);
314 goto failed;
317 if (r < text_len) /* In text segment */
318 addr = r + start_code;
319 else /* In data segment */
320 addr = r - text_len + start_data;
322 /* Range checked already above so doing the range tests is redundant...*/
323 return(addr);
325 failed:
326 abort();
327 return RELOC_FAILED;
330 /****************************************************************************/
332 /* ??? This does not handle endianness correctly. */
333 static void old_reloc(struct lib_info *libinfo, uint32_t rl)
335 #ifdef DEBUG
336 const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
337 #endif
338 uint32_t *ptr;
339 uint32_t offset;
340 int reloc_type;
342 offset = rl & 0x3fffffff;
343 reloc_type = rl >> 30;
344 /* ??? How to handle this? */
345 #if defined(CONFIG_COLDFIRE)
346 ptr = (uint32_t *) ((unsigned long) libinfo->start_code + offset);
347 #else
348 ptr = (uint32_t *) ((unsigned long) libinfo->start_data + offset);
349 #endif
351 #ifdef DEBUG
352 fprintf(stderr, "Relocation of variable at DATASEG+%x "
353 "(address %p, currently %x) into segment %s\n",
354 offset, ptr, (int)*ptr, segment[reloc_type]);
355 #endif
357 switch (reloc_type) {
358 case OLD_FLAT_RELOC_TYPE_TEXT:
359 *ptr += libinfo->start_code;
360 break;
361 case OLD_FLAT_RELOC_TYPE_DATA:
362 *ptr += libinfo->start_data;
363 break;
364 case OLD_FLAT_RELOC_TYPE_BSS:
365 *ptr += libinfo->end_data;
366 break;
367 default:
368 fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
369 reloc_type);
370 break;
372 DBG_FLT("Relocation became %x\n", (int)*ptr);
375 /****************************************************************************/
377 static int load_flat_file(struct linux_binprm * bprm,
378 struct lib_info *libinfo, int id, abi_ulong *extra_stack)
380 struct flat_hdr * hdr;
381 abi_ulong textpos = 0, datapos = 0;
382 abi_long result;
383 abi_ulong realdatastart = 0;
384 abi_ulong text_len, data_len, bss_len, stack_len, flags;
385 abi_ulong extra;
386 abi_ulong reloc = 0, rp;
387 int i, rev, relocs = 0;
388 abi_ulong fpos;
389 abi_ulong start_code;
390 abi_ulong indx_len;
392 hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
394 text_len = ntohl(hdr->data_start);
395 data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
396 bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
397 stack_len = ntohl(hdr->stack_size);
398 if (extra_stack) {
399 stack_len += *extra_stack;
400 *extra_stack = stack_len;
402 relocs = ntohl(hdr->reloc_count);
403 flags = ntohl(hdr->flags);
404 rev = ntohl(hdr->rev);
406 DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
408 if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
409 fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
410 rev, (int) FLAT_VERSION);
411 return -ENOEXEC;
414 /* Don't allow old format executables to use shared libraries */
415 if (rev == OLD_FLAT_VERSION && id != 0) {
416 fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
417 return -ENOEXEC;
421 * fix up the flags for the older format, there were all kinds
422 * of endian hacks, this only works for the simple cases
424 if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
425 flags = FLAT_FLAG_RAM;
427 #ifndef CONFIG_BINFMT_ZFLAT
428 if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
429 fprintf(stderr, "Support for ZFLAT executables is not enabled\n");
430 return -ENOEXEC;
432 #endif
435 * calculate the extra space we need to map in
437 extra = relocs * sizeof(abi_ulong);
438 if (extra < bss_len + stack_len)
439 extra = bss_len + stack_len;
441 /* Add space for library base pointers. Make sure this does not
442 misalign the doesn't misalign the data segment. */
443 indx_len = MAX_SHARED_LIBS * sizeof(abi_ulong);
444 indx_len = (indx_len + 15) & ~(abi_ulong)15;
447 * Allocate the address space.
449 probe_guest_base(bprm->filename, 0,
450 text_len + data_len + extra + indx_len);
453 * there are a couple of cases here, the separate code/data
454 * case, and then the fully copied to RAM case which lumps
455 * it all together.
457 if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
459 * this should give us a ROM ptr, but if it doesn't we don't
460 * really care
462 DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
464 textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
465 MAP_PRIVATE, bprm->fd, 0);
466 if (textpos == -1) {
467 fprintf(stderr, "Unable to mmap process text\n");
468 return -1;
471 realdatastart = target_mmap(0, data_len + extra + indx_len,
472 PROT_READ|PROT_WRITE|PROT_EXEC,
473 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
475 if (realdatastart == -1) {
476 fprintf(stderr, "Unable to allocate RAM for process data\n");
477 return realdatastart;
479 datapos = realdatastart + indx_len;
481 DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
482 (int)(data_len + bss_len + stack_len), (int)datapos);
484 fpos = ntohl(hdr->data_start);
485 #ifdef CONFIG_BINFMT_ZFLAT
486 if (flags & FLAT_FLAG_GZDATA) {
487 result = decompress_exec(bprm, fpos, (char *) datapos,
488 data_len + (relocs * sizeof(abi_ulong)))
489 } else
490 #endif
492 result = target_pread(bprm->fd, datapos,
493 data_len + (relocs * sizeof(abi_ulong)),
494 fpos);
496 if (result < 0) {
497 fprintf(stderr, "Unable to read data+bss\n");
498 return result;
501 reloc = datapos + (ntohl(hdr->reloc_start) - text_len);
503 } else {
505 textpos = target_mmap(0, text_len + data_len + extra + indx_len,
506 PROT_READ | PROT_EXEC | PROT_WRITE,
507 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
508 if (textpos == -1 ) {
509 fprintf(stderr, "Unable to allocate RAM for process text/data\n");
510 return -1;
513 realdatastart = textpos + ntohl(hdr->data_start);
514 datapos = realdatastart + indx_len;
515 reloc = (textpos + ntohl(hdr->reloc_start) + indx_len);
517 #ifdef CONFIG_BINFMT_ZFLAT
518 #error code needs checking
520 * load it all in and treat it like a RAM load from now on
522 if (flags & FLAT_FLAG_GZIP) {
523 result = decompress_exec(bprm, sizeof (struct flat_hdr),
524 (((char *) textpos) + sizeof (struct flat_hdr)),
525 (text_len + data_len + (relocs * sizeof(unsigned long))
526 - sizeof (struct flat_hdr)),
528 memmove((void *) datapos, (void *) realdatastart,
529 data_len + (relocs * sizeof(unsigned long)));
530 } else if (flags & FLAT_FLAG_GZDATA) {
531 fpos = 0;
532 result = bprm->file->f_op->read(bprm->file,
533 (char *) textpos, text_len, &fpos);
534 if (!is_error(result)) {
535 result = decompress_exec(bprm, text_len, (char *) datapos,
536 data_len + (relocs * sizeof(unsigned long)), 0);
539 else
540 #endif
542 result = target_pread(bprm->fd, textpos,
543 text_len, 0);
544 if (result >= 0) {
545 result = target_pread(bprm->fd, datapos,
546 data_len + (relocs * sizeof(abi_ulong)),
547 ntohl(hdr->data_start));
550 if (result < 0) {
551 fprintf(stderr, "Unable to read code+data+bss\n");
552 return result;
556 DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
557 (int)textpos, 0x00ffffff&ntohl(hdr->entry),
558 ntohl(hdr->data_start));
560 /* The main program needs a little extra setup in the task structure */
561 start_code = textpos + sizeof (struct flat_hdr);
563 DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
564 id ? "Lib" : "Load", bprm->filename,
565 (int) start_code, (int) (textpos + text_len),
566 (int) datapos,
567 (int) (datapos + data_len),
568 (int) (datapos + data_len),
569 (int) (((datapos + data_len + bss_len) + 3) & ~3));
571 text_len -= sizeof(struct flat_hdr); /* the real code len */
573 /* Store the current module values into the global library structure */
574 libinfo[id].start_code = start_code;
575 libinfo[id].start_data = datapos;
576 libinfo[id].end_data = datapos + data_len;
577 libinfo[id].start_brk = datapos + data_len + bss_len;
578 libinfo[id].text_len = text_len;
579 libinfo[id].loaded = 1;
580 libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
581 libinfo[id].build_date = ntohl(hdr->build_date);
584 * We just load the allocations into some temporary memory to
585 * help simplify all this mumbo jumbo
587 * We've got two different sections of relocation entries.
588 * The first is the GOT which resides at the beginning of the data segment
589 * and is terminated with a -1. This one can be relocated in place.
590 * The second is the extra relocation entries tacked after the image's
591 * data segment. These require a little more processing as the entry is
592 * really an offset into the image which contains an offset into the
593 * image.
595 if (flags & FLAT_FLAG_GOTPIC) {
596 rp = datapos;
597 while (1) {
598 abi_ulong addr;
599 if (get_user_ual(addr, rp))
600 return -EFAULT;
601 if (addr == -1)
602 break;
603 if (addr) {
604 addr = calc_reloc(addr, libinfo, id, 0);
605 if (addr == RELOC_FAILED)
606 return -ENOEXEC;
607 if (put_user_ual(addr, rp))
608 return -EFAULT;
610 rp += sizeof(abi_ulong);
615 * Now run through the relocation entries.
616 * We've got to be careful here as C++ produces relocatable zero
617 * entries in the constructor and destructor tables which are then
618 * tested for being not zero (which will always occur unless we're
619 * based from address zero). This causes an endless loop as __start
620 * is at zero. The solution used is to not relocate zero addresses.
621 * This has the negative side effect of not allowing a global data
622 * reference to be statically initialised to _stext (I've moved
623 * __start to address 4 so that is okay).
625 if (rev > OLD_FLAT_VERSION) {
626 abi_ulong persistent = 0;
627 for (i = 0; i < relocs; i++) {
628 abi_ulong addr, relval;
630 /* Get the address of the pointer to be
631 relocated (of course, the address has to be
632 relocated first). */
633 if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
634 return -EFAULT;
635 relval = ntohl(relval);
636 if (flat_set_persistent(relval, &persistent))
637 continue;
638 addr = flat_get_relocate_addr(relval);
639 rp = calc_reloc(addr, libinfo, id, 1);
640 if (rp == RELOC_FAILED)
641 return -ENOEXEC;
643 /* Get the pointer's value. */
644 if (get_user_ual(addr, rp))
645 return -EFAULT;
646 addr = flat_get_addr_from_rp(addr, relval, flags, &persistent);
647 if (addr != 0) {
649 * Do the relocation. PIC relocs in the data section are
650 * already in target order
652 if ((flags & FLAT_FLAG_GOTPIC) == 0)
653 addr = ntohl(addr);
654 addr = calc_reloc(addr, libinfo, id, 0);
655 if (addr == RELOC_FAILED)
656 return -ENOEXEC;
658 /* Write back the relocated pointer. */
659 if (flat_put_addr_at_rp(rp, addr, relval))
660 return -EFAULT;
663 } else {
664 for (i = 0; i < relocs; i++) {
665 abi_ulong relval;
666 if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
667 return -EFAULT;
668 old_reloc(&libinfo[0], relval);
672 /* zero the BSS. */
673 memset(g2h_untagged(datapos + data_len), 0, bss_len);
675 return 0;
679 /****************************************************************************/
680 #ifdef CONFIG_BINFMT_SHARED_FLAT
683 * Load a shared library into memory. The library gets its own data
684 * segment (including bss) but not argv/argc/environ.
687 static int load_flat_shared_library(int id, struct lib_info *libs)
689 struct linux_binprm bprm;
690 int res;
691 char buf[16];
693 /* Create the file name */
694 sprintf(buf, "/lib/lib%d.so", id);
696 /* Open the file up */
697 bprm.filename = buf;
698 bprm.file = open_exec(bprm.filename);
699 res = PTR_ERR(bprm.file);
700 if (IS_ERR(bprm.file))
701 return res;
703 res = prepare_binprm(&bprm);
705 if (!is_error(res)) {
706 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;
713 return(res);
716 #endif /* CONFIG_BINFMT_SHARED_FLAT */
718 int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
720 struct lib_info libinfo[MAX_SHARED_LIBS];
721 abi_ulong p;
722 abi_ulong stack_len;
723 abi_ulong start_addr;
724 abi_ulong sp;
725 int res;
726 int i, j;
728 memset(libinfo, 0, sizeof(libinfo));
730 * We have to add the size of our arguments to our stack size
731 * otherwise it's too easy for users to create stack overflows
732 * by passing in a huge argument list. And yes, we have to be
733 * pedantic and include space for the argv/envp array as it may have
734 * a lot of entries.
736 stack_len = 0;
737 for (i = 0; i < bprm->argc; ++i) {
738 /* the argv strings */
739 stack_len += strlen(bprm->argv[i]);
741 for (i = 0; i < bprm->envc; ++i) {
742 /* the envp strings */
743 stack_len += strlen(bprm->envp[i]);
745 stack_len += (bprm->argc + 1) * 4; /* the argv array */
746 stack_len += (bprm->envc + 1) * 4; /* the envp array */
749 res = load_flat_file(bprm, libinfo, 0, &stack_len);
750 if (is_error(res)) {
751 return res;
754 /* Update data segment pointers for all libraries */
755 for (i=0; i<MAX_SHARED_LIBS; i++) {
756 if (libinfo[i].loaded) {
757 abi_ulong p;
758 p = libinfo[i].start_data;
759 for (j=0; j<MAX_SHARED_LIBS; j++) {
760 p -= 4;
761 /* FIXME - handle put_user() failures */
762 if (put_user_ual(libinfo[j].loaded
763 ? libinfo[j].start_data
764 : UNLOADED_LIB,
766 return -EFAULT;
771 p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4;
772 DBG_FLT("p=%x\n", (int)p);
774 /* Copy argv/envp. */
775 p = copy_strings(p, bprm->envc, bprm->envp);
776 p = copy_strings(p, bprm->argc, bprm->argv);
777 /* Align stack. */
778 sp = p & ~(abi_ulong)(sizeof(abi_ulong) - 1);
779 /* Enforce final stack alignment of 16 bytes. This is sufficient
780 for all current targets, and excess alignment is harmless. */
781 stack_len = bprm->envc + bprm->argc + 2;
782 stack_len += flat_argvp_envp_on_stack() ? 2 : 0; /* arvg, argp */
783 stack_len += 1; /* argc */
784 stack_len *= sizeof(abi_ulong);
785 sp -= (sp - stack_len) & 15;
786 sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p,
787 flat_argvp_envp_on_stack());
789 /* Fake some return addresses to ensure the call chain will
790 * initialise library in order for us. We are required to call
791 * lib 1 first, then 2, ... and finally the main program (id 0).
793 start_addr = libinfo[0].entry;
795 #ifdef CONFIG_BINFMT_SHARED_FLAT
796 #error here
797 for (i = MAX_SHARED_LIBS-1; i>0; i--) {
798 if (libinfo[i].loaded) {
799 /* Push previous first to call address */
800 --sp;
801 if (put_user_ual(start_addr, sp))
802 return -EFAULT;
803 start_addr = libinfo[i].entry;
806 #endif
808 /* Stash our initial stack pointer into the mm structure */
809 info->start_code = libinfo[0].start_code;
810 info->end_code = libinfo[0].start_code = libinfo[0].text_len;
811 info->start_data = libinfo[0].start_data;
812 info->end_data = libinfo[0].end_data;
813 info->start_brk = libinfo[0].start_brk;
814 info->start_stack = sp;
815 info->stack_limit = libinfo[0].start_brk;
816 info->entry = start_addr;
817 info->code_offset = info->start_code;
818 info->data_offset = info->start_data - libinfo[0].text_len;
820 DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
821 (int)info->entry, (int)info->start_stack);
823 return 0;