Statistics
| Branch: | Revision:

root / linux-user / flatload.c @ 72cf2d4f

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