Statistics
| Branch: | Revision:

root / linux-user / flatload.c @ d084469c

History | View | Annotate | Download (25.5 kB)

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