1e5fe0c52Spbrook /****************************************************************************/
2e5fe0c52Spbrook /*
3e5fe0c52Spbrook * QEMU bFLT binary loader. Based on linux/fs/binfmt_flat.c
4e5fe0c52Spbrook *
5e5fe0c52Spbrook * This program is free software; you can redistribute it and/or modify
6e5fe0c52Spbrook * it under the terms of the GNU General Public License as published by
7e5fe0c52Spbrook * the Free Software Foundation; either version 2 of the License, or
8e5fe0c52Spbrook * (at your option) any later version.
9e5fe0c52Spbrook *
10e5fe0c52Spbrook * This program is distributed in the hope that it will be useful,
11e5fe0c52Spbrook * but WITHOUT ANY WARRANTY; without even the implied warranty of
12e5fe0c52Spbrook * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13e5fe0c52Spbrook * GNU General Public License for more details.
14e5fe0c52Spbrook *
15e5fe0c52Spbrook * You should have received a copy of the GNU General Public License
168167ee88SBlue Swirl * along with this program; if not, see <http://www.gnu.org/licenses/>.
17e5fe0c52Spbrook *
18e5fe0c52Spbrook * Copyright (C) 2006 CodeSourcery.
19e5fe0c52Spbrook * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
20e5fe0c52Spbrook * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
21e5fe0c52Spbrook * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
22e5fe0c52Spbrook * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
23e5fe0c52Spbrook * based heavily on:
24e5fe0c52Spbrook *
25e5fe0c52Spbrook * linux/fs/binfmt_aout.c:
26e5fe0c52Spbrook * Copyright (C) 1991, 1992, 1996 Linus Torvalds
27e5fe0c52Spbrook * linux/fs/binfmt_flat.c for 2.0 kernel
28e5fe0c52Spbrook * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
29e5fe0c52Spbrook * JAN/99 -- coded full program relocation (gerg@snapgear.com)
30e5fe0c52Spbrook */
31e5fe0c52Spbrook
32e5fe0c52Spbrook /****************************************************************************/
33e5fe0c52Spbrook
34d39594e9SPeter Maydell #include "qemu/osdep.h"
35e5fe0c52Spbrook
36e5fe0c52Spbrook #include "qemu.h"
373b249d26SPeter Maydell #include "user-internals.h"
383ad0a769SPeter Maydell #include "loader.h"
395423e6d3SPeter Maydell #include "user-mmap.h"
40e5fe0c52Spbrook #include "flat.h"
4194db8de1SPeter Maydell #include "target_flat.h"
42e5fe0c52Spbrook
43e5fe0c52Spbrook //#define DEBUG
44e5fe0c52Spbrook
45e5fe0c52Spbrook #ifdef DEBUG
46001faf32SBlue Swirl #define DBG_FLT(...) printf(__VA_ARGS__)
47e5fe0c52Spbrook #else
48001faf32SBlue Swirl #define DBG_FLT(...)
49e5fe0c52Spbrook #endif
50e5fe0c52Spbrook
51e5fe0c52Spbrook #define RELOC_FAILED 0xff00ff01 /* Relocation incorrect somewhere */
52e5fe0c52Spbrook #define UNLOADED_LIB 0x7ff000ff /* Placeholder for unused library */
53e5fe0c52Spbrook
54e5fe0c52Spbrook struct lib_info {
55992f48a0Sblueswir1 abi_ulong start_code; /* Start of text segment */
56992f48a0Sblueswir1 abi_ulong start_data; /* Start of data segment */
57992f48a0Sblueswir1 abi_ulong end_data; /* Start of bss section */
58992f48a0Sblueswir1 abi_ulong start_brk; /* End of data segment */
59992f48a0Sblueswir1 abi_ulong text_len; /* Length of text segment */
60992f48a0Sblueswir1 abi_ulong entry; /* Start address for this module */
61992f48a0Sblueswir1 abi_ulong build_date; /* When this one was compiled */
62e5fe0c52Spbrook short loaded; /* Has this library been loaded? */
63e5fe0c52Spbrook };
64e5fe0c52Spbrook
65e5fe0c52Spbrook struct linux_binprm;
66e5fe0c52Spbrook
67e5fe0c52Spbrook /****************************************************************************/
68e5fe0c52Spbrook /*
69e5fe0c52Spbrook * create_flat_tables() parses the env- and arg-strings in new user
70e5fe0c52Spbrook * memory and creates the pointer tables from them, and puts their
71e5fe0c52Spbrook * addresses on the "stack", returning the new stack pointer value.
72e5fe0c52Spbrook */
73e5fe0c52Spbrook
74e5fe0c52Spbrook /* Push a block of strings onto the guest stack. */
copy_strings(abi_ulong p,int n,char ** s)75992f48a0Sblueswir1 static abi_ulong copy_strings(abi_ulong p, int n, char **s)
76e5fe0c52Spbrook {
77e5fe0c52Spbrook int len;
78e5fe0c52Spbrook
79e5fe0c52Spbrook while (n-- > 0) {
80e5fe0c52Spbrook len = strlen(s[n]) + 1;
81e5fe0c52Spbrook p -= len;
82e5fe0c52Spbrook memcpy_to_target(p, s[n], len);
83e5fe0c52Spbrook }
84e5fe0c52Spbrook
85e5fe0c52Spbrook return p;
86e5fe0c52Spbrook }
87e5fe0c52Spbrook
target_pread(int fd,abi_ulong ptr,abi_ulong len,abi_ulong offset)88b1d8e52eSblueswir1 static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
89992f48a0Sblueswir1 abi_ulong offset)
90e5fe0c52Spbrook {
91e5fe0c52Spbrook void *buf;
92e5fe0c52Spbrook int ret;
93e5fe0c52Spbrook
94579a97f7Sbellard buf = lock_user(VERIFY_WRITE, ptr, len, 0);
95e5a869edSPeter Maydell if (!buf) {
96e5a869edSPeter Maydell return -EFAULT;
97e5a869edSPeter Maydell }
98e5fe0c52Spbrook ret = pread(fd, buf, len, offset);
99e5a869edSPeter Maydell if (ret < 0) {
100e5a869edSPeter Maydell ret = -errno;
101e5a869edSPeter Maydell }
102e5fe0c52Spbrook unlock_user(buf, ptr, len);
103e5fe0c52Spbrook return ret;
104e5fe0c52Spbrook }
105e5fe0c52Spbrook
106e5fe0c52Spbrook /****************************************************************************/
107e5fe0c52Spbrook
108992f48a0Sblueswir1 static abi_ulong
calc_reloc(abi_ulong r,struct lib_info * p,int curid,int internalp)109992f48a0Sblueswir1 calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
110e5fe0c52Spbrook {
111992f48a0Sblueswir1 abi_ulong addr;
112e5fe0c52Spbrook int id;
113992f48a0Sblueswir1 abi_ulong start_brk;
114992f48a0Sblueswir1 abi_ulong start_data;
115992f48a0Sblueswir1 abi_ulong text_len;
116992f48a0Sblueswir1 abi_ulong start_code;
117e5fe0c52Spbrook
118e5fe0c52Spbrook id = 0;
119e5fe0c52Spbrook
120e5fe0c52Spbrook start_brk = p[id].start_brk;
121e5fe0c52Spbrook start_data = p[id].start_data;
122e5fe0c52Spbrook start_code = p[id].start_code;
123e5fe0c52Spbrook text_len = p[id].text_len;
124e5fe0c52Spbrook
125e5fe0c52Spbrook if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
126e5fe0c52Spbrook fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
127e5fe0c52Spbrook "(0 - 0x%x/0x%x)\n",
128e5fe0c52Spbrook (int) r,(int)(start_brk-start_code),(int)text_len);
129e5fe0c52Spbrook goto failed;
130e5fe0c52Spbrook }
131e5fe0c52Spbrook
132e5fe0c52Spbrook if (r < text_len) /* In text segment */
133e5fe0c52Spbrook addr = r + start_code;
134e5fe0c52Spbrook else /* In data segment */
135e5fe0c52Spbrook addr = r - text_len + start_data;
136e5fe0c52Spbrook
137e5fe0c52Spbrook /* Range checked already above so doing the range tests is redundant...*/
138e5fe0c52Spbrook return(addr);
139e5fe0c52Spbrook
140e5fe0c52Spbrook failed:
141e5fe0c52Spbrook abort();
142e5fe0c52Spbrook return RELOC_FAILED;
143e5fe0c52Spbrook }
144e5fe0c52Spbrook
145e5fe0c52Spbrook /****************************************************************************/
146e5fe0c52Spbrook
147e5fe0c52Spbrook /* ??? This does not handle endianness correctly. */
old_reloc(struct lib_info * libinfo,uint32_t rl)148b1d8e52eSblueswir1 static void old_reloc(struct lib_info *libinfo, uint32_t rl)
149e5fe0c52Spbrook {
150e5fe0c52Spbrook #ifdef DEBUG
151564e2fe8SRiccardo Magliocchetti const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
152e5fe0c52Spbrook #endif
153e5fe0c52Spbrook uint32_t *ptr;
154e5fe0c52Spbrook uint32_t offset;
155e5fe0c52Spbrook int reloc_type;
156e5fe0c52Spbrook
157e5fe0c52Spbrook offset = rl & 0x3fffffff;
158e5fe0c52Spbrook reloc_type = rl >> 30;
159e5fe0c52Spbrook /* ??? How to handle this? */
160e5fe0c52Spbrook #if defined(CONFIG_COLDFIRE)
161526ccb7aSbalrog ptr = (uint32_t *) ((unsigned long) libinfo->start_code + offset);
162e5fe0c52Spbrook #else
163526ccb7aSbalrog ptr = (uint32_t *) ((unsigned long) libinfo->start_data + offset);
164e5fe0c52Spbrook #endif
165e5fe0c52Spbrook
166e5fe0c52Spbrook #ifdef DEBUG
167e5fe0c52Spbrook fprintf(stderr, "Relocation of variable at DATASEG+%x "
168e5fe0c52Spbrook "(address %p, currently %x) into segment %s\n",
169e5fe0c52Spbrook offset, ptr, (int)*ptr, segment[reloc_type]);
170e5fe0c52Spbrook #endif
171e5fe0c52Spbrook
172e5fe0c52Spbrook switch (reloc_type) {
173e5fe0c52Spbrook case OLD_FLAT_RELOC_TYPE_TEXT:
174e5fe0c52Spbrook *ptr += libinfo->start_code;
175e5fe0c52Spbrook break;
176e5fe0c52Spbrook case OLD_FLAT_RELOC_TYPE_DATA:
177e5fe0c52Spbrook *ptr += libinfo->start_data;
178e5fe0c52Spbrook break;
179e5fe0c52Spbrook case OLD_FLAT_RELOC_TYPE_BSS:
180e5fe0c52Spbrook *ptr += libinfo->end_data;
181e5fe0c52Spbrook break;
182e5fe0c52Spbrook default:
183e5fe0c52Spbrook fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
184e5fe0c52Spbrook reloc_type);
185e5fe0c52Spbrook break;
186e5fe0c52Spbrook }
187e5fe0c52Spbrook DBG_FLT("Relocation became %x\n", (int)*ptr);
188e5fe0c52Spbrook }
189e5fe0c52Spbrook
190e5fe0c52Spbrook /****************************************************************************/
191e5fe0c52Spbrook
load_flat_file(struct linux_binprm * bprm,struct lib_info * libinfo,int id,abi_ulong * extra_stack)192e5fe0c52Spbrook static int load_flat_file(struct linux_binprm * bprm,
193992f48a0Sblueswir1 struct lib_info *libinfo, int id, abi_ulong *extra_stack)
194e5fe0c52Spbrook {
195e5fe0c52Spbrook struct flat_hdr * hdr;
196f562e716SBlue Swirl abi_ulong textpos = 0, datapos = 0;
197f562e716SBlue Swirl abi_long result;
198992f48a0Sblueswir1 abi_ulong realdatastart = 0;
199992f48a0Sblueswir1 abi_ulong text_len, data_len, bss_len, stack_len, flags;
200992f48a0Sblueswir1 abi_ulong extra;
201992f48a0Sblueswir1 abi_ulong reloc = 0, rp;
202e5fe0c52Spbrook int i, rev, relocs = 0;
203992f48a0Sblueswir1 abi_ulong fpos;
204e7730352SJuan Quintela abi_ulong start_code;
205992f48a0Sblueswir1 abi_ulong indx_len;
206e5fe0c52Spbrook
207e5fe0c52Spbrook hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
208e5fe0c52Spbrook
209e5fe0c52Spbrook text_len = ntohl(hdr->data_start);
210e5fe0c52Spbrook data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
211e5fe0c52Spbrook bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
212e5fe0c52Spbrook stack_len = ntohl(hdr->stack_size);
213e5fe0c52Spbrook if (extra_stack) {
214e5fe0c52Spbrook stack_len += *extra_stack;
215e5fe0c52Spbrook *extra_stack = stack_len;
216e5fe0c52Spbrook }
217e5fe0c52Spbrook relocs = ntohl(hdr->reloc_count);
218e5fe0c52Spbrook flags = ntohl(hdr->flags);
219e5fe0c52Spbrook rev = ntohl(hdr->rev);
220e5fe0c52Spbrook
221e5fe0c52Spbrook DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
222e5fe0c52Spbrook
223e5fe0c52Spbrook if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
224e5fe0c52Spbrook fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
225e5fe0c52Spbrook rev, (int) FLAT_VERSION);
226e5fe0c52Spbrook return -ENOEXEC;
227e5fe0c52Spbrook }
228e5fe0c52Spbrook
229e5fe0c52Spbrook /* Don't allow old format executables to use shared libraries */
230e5fe0c52Spbrook if (rev == OLD_FLAT_VERSION && id != 0) {
231e5fe0c52Spbrook fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
232e5fe0c52Spbrook return -ENOEXEC;
233e5fe0c52Spbrook }
234e5fe0c52Spbrook
235e5fe0c52Spbrook /*
236e5fe0c52Spbrook * fix up the flags for the older format, there were all kinds
237e5fe0c52Spbrook * of endian hacks, this only works for the simple cases
238e5fe0c52Spbrook */
239e5fe0c52Spbrook if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
240e5fe0c52Spbrook flags = FLAT_FLAG_RAM;
241e5fe0c52Spbrook
242e5fe0c52Spbrook if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
243a6819c1bSPeter Maydell fprintf(stderr, "ZFLAT executables are not supported\n");
244e5fe0c52Spbrook return -ENOEXEC;
245e5fe0c52Spbrook }
246e5fe0c52Spbrook
247e5fe0c52Spbrook /*
248e5fe0c52Spbrook * calculate the extra space we need to map in
249e5fe0c52Spbrook */
250992f48a0Sblueswir1 extra = relocs * sizeof(abi_ulong);
251e5fe0c52Spbrook if (extra < bss_len + stack_len)
252e5fe0c52Spbrook extra = bss_len + stack_len;
253e5fe0c52Spbrook
2542a1094cdSpbrook /* Add space for library base pointers. Make sure this does not
2552a1094cdSpbrook misalign the doesn't misalign the data segment. */
256992f48a0Sblueswir1 indx_len = MAX_SHARED_LIBS * sizeof(abi_ulong);
257992f48a0Sblueswir1 indx_len = (indx_len + 15) & ~(abi_ulong)15;
2582a1094cdSpbrook
259e5fe0c52Spbrook /*
2606f9ff551Szhaolichang * Allocate the address space.
261ee947430SAlex Bennée */
262ee947430SAlex Bennée probe_guest_base(bprm->filename, 0,
263a3a67f54SRichard Henderson text_len + data_len + extra + indx_len - 1);
264ee947430SAlex Bennée
265ee947430SAlex Bennée /*
266e5fe0c52Spbrook * there are a couple of cases here, the separate code/data
267e5fe0c52Spbrook * case, and then the fully copied to RAM case which lumps
268e5fe0c52Spbrook * it all together.
269e5fe0c52Spbrook */
270e5fe0c52Spbrook if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
271e5fe0c52Spbrook /*
272e5fe0c52Spbrook * this should give us a ROM ptr, but if it doesn't we don't
273e5fe0c52Spbrook * really care
274e5fe0c52Spbrook */
275e5fe0c52Spbrook DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
276e5fe0c52Spbrook
277e5fe0c52Spbrook textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
278d0b6b793SRichard Henderson MAP_PRIVATE, bprm->src.fd, 0);
279e5fe0c52Spbrook if (textpos == -1) {
280e5fe0c52Spbrook fprintf(stderr, "Unable to mmap process text\n");
281e5fe0c52Spbrook return -1;
282e5fe0c52Spbrook }
283e5fe0c52Spbrook
2842a1094cdSpbrook realdatastart = target_mmap(0, data_len + extra + indx_len,
285e5fe0c52Spbrook PROT_READ|PROT_WRITE|PROT_EXEC,
286e5fe0c52Spbrook MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
287e5fe0c52Spbrook
288e5fe0c52Spbrook if (realdatastart == -1) {
289e5fe0c52Spbrook fprintf(stderr, "Unable to allocate RAM for process data\n");
290e5fe0c52Spbrook return realdatastart;
291e5fe0c52Spbrook }
2922a1094cdSpbrook datapos = realdatastart + indx_len;
293e5fe0c52Spbrook
294e5fe0c52Spbrook DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
295e5fe0c52Spbrook (int)(data_len + bss_len + stack_len), (int)datapos);
296e5fe0c52Spbrook
297e5fe0c52Spbrook fpos = ntohl(hdr->data_start);
298d0b6b793SRichard Henderson result = target_pread(bprm->src.fd, datapos,
299992f48a0Sblueswir1 data_len + (relocs * sizeof(abi_ulong)),
300e5fe0c52Spbrook fpos);
301e5fe0c52Spbrook if (result < 0) {
302e5fe0c52Spbrook fprintf(stderr, "Unable to read data+bss\n");
303e5fe0c52Spbrook return result;
304e5fe0c52Spbrook }
305e5fe0c52Spbrook
306e5fe0c52Spbrook reloc = datapos + (ntohl(hdr->reloc_start) - text_len);
307e5fe0c52Spbrook
308e5fe0c52Spbrook } else {
309e5fe0c52Spbrook
3102a1094cdSpbrook textpos = target_mmap(0, text_len + data_len + extra + indx_len,
311e5fe0c52Spbrook PROT_READ | PROT_EXEC | PROT_WRITE,
312e5fe0c52Spbrook MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
313e5fe0c52Spbrook if (textpos == -1 ) {
314e5fe0c52Spbrook fprintf(stderr, "Unable to allocate RAM for process text/data\n");
315e5fe0c52Spbrook return -1;
316e5fe0c52Spbrook }
317e5fe0c52Spbrook
318e5fe0c52Spbrook realdatastart = textpos + ntohl(hdr->data_start);
3192a1094cdSpbrook datapos = realdatastart + indx_len;
3202a1094cdSpbrook reloc = (textpos + ntohl(hdr->reloc_start) + indx_len);
321e5fe0c52Spbrook
322d0b6b793SRichard Henderson result = target_pread(bprm->src.fd, textpos,
323e5fe0c52Spbrook text_len, 0);
324e5fe0c52Spbrook if (result >= 0) {
325d0b6b793SRichard Henderson result = target_pread(bprm->src.fd, datapos,
326992f48a0Sblueswir1 data_len + (relocs * sizeof(abi_ulong)),
327e5fe0c52Spbrook ntohl(hdr->data_start));
328e5fe0c52Spbrook }
329e5fe0c52Spbrook if (result < 0) {
330e5fe0c52Spbrook fprintf(stderr, "Unable to read code+data+bss\n");
331e5fe0c52Spbrook return result;
332e5fe0c52Spbrook }
333e5fe0c52Spbrook }
334e5fe0c52Spbrook
335e5fe0c52Spbrook DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
336e5fe0c52Spbrook (int)textpos, 0x00ffffff&ntohl(hdr->entry),
337e5fe0c52Spbrook ntohl(hdr->data_start));
338e5fe0c52Spbrook
339e5fe0c52Spbrook /* The main program needs a little extra setup in the task structure */
340e5fe0c52Spbrook start_code = textpos + sizeof (struct flat_hdr);
341e5fe0c52Spbrook
342e5fe0c52Spbrook DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
343e5fe0c52Spbrook id ? "Lib" : "Load", bprm->filename,
344e7730352SJuan Quintela (int) start_code, (int) (textpos + text_len),
345e5fe0c52Spbrook (int) datapos,
346e5fe0c52Spbrook (int) (datapos + data_len),
347e5fe0c52Spbrook (int) (datapos + data_len),
348e5fe0c52Spbrook (int) (((datapos + data_len + bss_len) + 3) & ~3));
349e5fe0c52Spbrook
350e5fe0c52Spbrook text_len -= sizeof(struct flat_hdr); /* the real code len */
351e5fe0c52Spbrook
352e5fe0c52Spbrook /* Store the current module values into the global library structure */
353e5fe0c52Spbrook libinfo[id].start_code = start_code;
354e5fe0c52Spbrook libinfo[id].start_data = datapos;
355e5fe0c52Spbrook libinfo[id].end_data = datapos + data_len;
356e5fe0c52Spbrook libinfo[id].start_brk = datapos + data_len + bss_len;
357e5fe0c52Spbrook libinfo[id].text_len = text_len;
358e5fe0c52Spbrook libinfo[id].loaded = 1;
359e5fe0c52Spbrook libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
360e5fe0c52Spbrook libinfo[id].build_date = ntohl(hdr->build_date);
361e5fe0c52Spbrook
362e5fe0c52Spbrook /*
363e5fe0c52Spbrook * We just load the allocations into some temporary memory to
364e5fe0c52Spbrook * help simplify all this mumbo jumbo
365e5fe0c52Spbrook *
366e5fe0c52Spbrook * We've got two different sections of relocation entries.
367b4916d7bSDong Xu Wang * The first is the GOT which resides at the beginning of the data segment
368e5fe0c52Spbrook * and is terminated with a -1. This one can be relocated in place.
369e5fe0c52Spbrook * The second is the extra relocation entries tacked after the image's
370e5fe0c52Spbrook * data segment. These require a little more processing as the entry is
371e5fe0c52Spbrook * really an offset into the image which contains an offset into the
372e5fe0c52Spbrook * image.
373e5fe0c52Spbrook */
374e5fe0c52Spbrook if (flags & FLAT_FLAG_GOTPIC) {
375e5fe0c52Spbrook rp = datapos;
376e5fe0c52Spbrook while (1) {
377992f48a0Sblueswir1 abi_ulong addr;
3782f619698Sbellard if (get_user_ual(addr, rp))
3792f619698Sbellard return -EFAULT;
380e5fe0c52Spbrook if (addr == -1)
381e5fe0c52Spbrook break;
382e5fe0c52Spbrook if (addr) {
383e5fe0c52Spbrook addr = calc_reloc(addr, libinfo, id, 0);
384e5fe0c52Spbrook if (addr == RELOC_FAILED)
385e5fe0c52Spbrook return -ENOEXEC;
3862f619698Sbellard if (put_user_ual(addr, rp))
3872f619698Sbellard return -EFAULT;
388e5fe0c52Spbrook }
389992f48a0Sblueswir1 rp += sizeof(abi_ulong);
390e5fe0c52Spbrook }
391e5fe0c52Spbrook }
392e5fe0c52Spbrook
393e5fe0c52Spbrook /*
394e5fe0c52Spbrook * Now run through the relocation entries.
395e5fe0c52Spbrook * We've got to be careful here as C++ produces relocatable zero
396e5fe0c52Spbrook * entries in the constructor and destructor tables which are then
397e5fe0c52Spbrook * tested for being not zero (which will always occur unless we're
398e5fe0c52Spbrook * based from address zero). This causes an endless loop as __start
399e5fe0c52Spbrook * is at zero. The solution used is to not relocate zero addresses.
400e5fe0c52Spbrook * This has the negative side effect of not allowing a global data
401e5fe0c52Spbrook * reference to be statically initialised to _stext (I've moved
402e5fe0c52Spbrook * __start to address 4 so that is okay).
403e5fe0c52Spbrook */
404e5fe0c52Spbrook if (rev > OLD_FLAT_VERSION) {
405c3109ba1SMike Frysinger abi_ulong persistent = 0;
406e5fe0c52Spbrook for (i = 0; i < relocs; i++) {
407992f48a0Sblueswir1 abi_ulong addr, relval;
408e5fe0c52Spbrook
409e5fe0c52Spbrook /* Get the address of the pointer to be
410e5fe0c52Spbrook relocated (of course, the address has to be
411e5fe0c52Spbrook relocated first). */
4122f619698Sbellard if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
4132f619698Sbellard return -EFAULT;
414c3109ba1SMike Frysinger relval = ntohl(relval);
415c3109ba1SMike Frysinger if (flat_set_persistent(relval, &persistent))
416c3109ba1SMike Frysinger continue;
417e5fe0c52Spbrook addr = flat_get_relocate_addr(relval);
418e5fe0c52Spbrook rp = calc_reloc(addr, libinfo, id, 1);
419e5fe0c52Spbrook if (rp == RELOC_FAILED)
420e5fe0c52Spbrook return -ENOEXEC;
421e5fe0c52Spbrook
422e5fe0c52Spbrook /* Get the pointer's value. */
4232f619698Sbellard if (get_user_ual(addr, rp))
4242f619698Sbellard return -EFAULT;
4259721cf2cSCorey J. Boyle addr = flat_get_addr_from_rp(addr, relval, flags, &persistent);
426e5fe0c52Spbrook if (addr != 0) {
427e5fe0c52Spbrook /*
428e5fe0c52Spbrook * Do the relocation. PIC relocs in the data section are
429e5fe0c52Spbrook * already in target order
430e5fe0c52Spbrook */
431e5fe0c52Spbrook if ((flags & FLAT_FLAG_GOTPIC) == 0)
432c3109ba1SMike Frysinger addr = ntohl(addr);
433e5fe0c52Spbrook addr = calc_reloc(addr, libinfo, id, 0);
434e5fe0c52Spbrook if (addr == RELOC_FAILED)
435e5fe0c52Spbrook return -ENOEXEC;
436e5fe0c52Spbrook
437e5fe0c52Spbrook /* Write back the relocated pointer. */
438c3109ba1SMike Frysinger if (flat_put_addr_at_rp(rp, addr, relval))
4392f619698Sbellard return -EFAULT;
440e5fe0c52Spbrook }
441e5fe0c52Spbrook }
442e5fe0c52Spbrook } else {
443e5fe0c52Spbrook for (i = 0; i < relocs; i++) {
444992f48a0Sblueswir1 abi_ulong relval;
4452f619698Sbellard if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
4462f619698Sbellard return -EFAULT;
447e5fe0c52Spbrook old_reloc(&libinfo[0], relval);
448e5fe0c52Spbrook }
449e5fe0c52Spbrook }
450e5fe0c52Spbrook
451e5fe0c52Spbrook /* zero the BSS. */
4523e8f1628SRichard Henderson memset(g2h_untagged(datapos + data_len), 0, bss_len);
453e5fe0c52Spbrook
454e5fe0c52Spbrook return 0;
455e5fe0c52Spbrook }
456e5fe0c52Spbrook
457e5fe0c52Spbrook
458e5fe0c52Spbrook /****************************************************************************/
load_flt_binary(struct linux_binprm * bprm,struct image_info * info)459f0116c54SWill Newton int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
460e5fe0c52Spbrook {
461e5fe0c52Spbrook struct lib_info libinfo[MAX_SHARED_LIBS];
46259baae9aSStefan Brüns abi_ulong p;
463992f48a0Sblueswir1 abi_ulong stack_len;
464992f48a0Sblueswir1 abi_ulong start_addr;
465992f48a0Sblueswir1 abi_ulong sp;
466e5fe0c52Spbrook int res;
467e5fe0c52Spbrook int i, j;
468e5fe0c52Spbrook
469e5fe0c52Spbrook memset(libinfo, 0, sizeof(libinfo));
470e5fe0c52Spbrook /*
471e5fe0c52Spbrook * We have to add the size of our arguments to our stack size
472e5fe0c52Spbrook * otherwise it's too easy for users to create stack overflows
473e5fe0c52Spbrook * by passing in a huge argument list. And yes, we have to be
474e5fe0c52Spbrook * pedantic and include space for the argv/envp array as it may have
475e5fe0c52Spbrook * a lot of entries.
476e5fe0c52Spbrook */
47782a39595SMike Frysinger stack_len = 0;
47882a39595SMike Frysinger for (i = 0; i < bprm->argc; ++i) {
47982a39595SMike Frysinger /* the argv strings */
48082a39595SMike Frysinger stack_len += strlen(bprm->argv[i]);
48182a39595SMike Frysinger }
48282a39595SMike Frysinger for (i = 0; i < bprm->envc; ++i) {
48382a39595SMike Frysinger /* the envp strings */
48482a39595SMike Frysinger stack_len += strlen(bprm->envp[i]);
48582a39595SMike Frysinger }
486e5fe0c52Spbrook stack_len += (bprm->argc + 1) * 4; /* the argv array */
487e5fe0c52Spbrook stack_len += (bprm->envc + 1) * 4; /* the envp array */
488e5fe0c52Spbrook
489e5fe0c52Spbrook
490*18046fbeSPhilippe Mathieu-Daudé mmap_lock();
491e5fe0c52Spbrook res = load_flat_file(bprm, libinfo, 0, &stack_len);
492*18046fbeSPhilippe Mathieu-Daudé mmap_unlock();
493*18046fbeSPhilippe Mathieu-Daudé
49471987b12SPhilippe Mathieu-Daudé if (is_error(res)) {
495e5fe0c52Spbrook return res;
49671987b12SPhilippe Mathieu-Daudé }
497e5fe0c52Spbrook
498e5fe0c52Spbrook /* Update data segment pointers for all libraries */
499e5fe0c52Spbrook for (i=0; i<MAX_SHARED_LIBS; i++) {
500e5fe0c52Spbrook if (libinfo[i].loaded) {
501d5308ea6SLaurent Vivier abi_ulong seg;
502d5308ea6SLaurent Vivier seg = libinfo[i].start_data;
503e5fe0c52Spbrook for (j=0; j<MAX_SHARED_LIBS; j++) {
504d5308ea6SLaurent Vivier seg -= 4;
5052f619698Sbellard /* FIXME - handle put_user() failures */
5062f619698Sbellard if (put_user_ual(libinfo[j].loaded
507e5fe0c52Spbrook ? libinfo[j].start_data
5082f619698Sbellard : UNLOADED_LIB,
509d5308ea6SLaurent Vivier seg))
5102f619698Sbellard return -EFAULT;
511e5fe0c52Spbrook }
512e5fe0c52Spbrook }
513e5fe0c52Spbrook }
514e5fe0c52Spbrook
515e5fe0c52Spbrook p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4;
516e5fe0c52Spbrook DBG_FLT("p=%x\n", (int)p);
517e5fe0c52Spbrook
518e5fe0c52Spbrook /* Copy argv/envp. */
519e5fe0c52Spbrook p = copy_strings(p, bprm->envc, bprm->envp);
520388c4508Spbrook p = copy_strings(p, bprm->argc, bprm->argv);
521e5fe0c52Spbrook /* Align stack. */
522992f48a0Sblueswir1 sp = p & ~(abi_ulong)(sizeof(abi_ulong) - 1);
523b35d7448Spbrook /* Enforce final stack alignment of 16 bytes. This is sufficient
524b35d7448Spbrook for all current targets, and excess alignment is harmless. */
525b35d7448Spbrook stack_len = bprm->envc + bprm->argc + 2;
526669dcb60SMichael Tokarev stack_len += flat_argvp_envp_on_stack() ? 2 : 0; /* argv, argp */
5275c76d652SMax Filippov stack_len += 1; /* argc */
528992f48a0Sblueswir1 stack_len *= sizeof(abi_ulong);
5295c76d652SMax Filippov sp -= (sp - stack_len) & 15;
530c3109ba1SMike Frysinger sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p,
531c3109ba1SMike Frysinger flat_argvp_envp_on_stack());
532e5fe0c52Spbrook
533e5fe0c52Spbrook /* Fake some return addresses to ensure the call chain will
534e5fe0c52Spbrook * initialise library in order for us. We are required to call
535e5fe0c52Spbrook * lib 1 first, then 2, ... and finally the main program (id 0).
536e5fe0c52Spbrook */
537e5fe0c52Spbrook start_addr = libinfo[0].entry;
538e5fe0c52Spbrook
539e5fe0c52Spbrook /* Stash our initial stack pointer into the mm structure */
540e5fe0c52Spbrook info->start_code = libinfo[0].start_code;
541734a659aSPeter Maydell info->end_code = libinfo[0].start_code + libinfo[0].text_len;
542e5fe0c52Spbrook info->start_data = libinfo[0].start_data;
543e5fe0c52Spbrook info->end_data = libinfo[0].end_data;
5440662a626SRichard Henderson info->brk = libinfo[0].start_brk;
545e5fe0c52Spbrook info->start_stack = sp;
54697374d38SPaul Brook info->stack_limit = libinfo[0].start_brk;
547e5fe0c52Spbrook info->entry = start_addr;
548978efd6aSpbrook info->code_offset = info->start_code;
549978efd6aSpbrook info->data_offset = info->start_data - libinfo[0].text_len;
550978efd6aSpbrook
551e5fe0c52Spbrook DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
552e5fe0c52Spbrook (int)info->entry, (int)info->start_stack);
553e5fe0c52Spbrook
554e5fe0c52Spbrook return 0;
555e5fe0c52Spbrook }
556