xref: /openbmc/qemu/linux-user/flatload.c (revision d0b6b793)
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 /* ??? ZFLAT and shared library support is currently disabled.  */
33e5fe0c52Spbrook 
34e5fe0c52Spbrook /****************************************************************************/
35e5fe0c52Spbrook 
36d39594e9SPeter Maydell #include "qemu/osdep.h"
37e5fe0c52Spbrook 
38e5fe0c52Spbrook #include "qemu.h"
393b249d26SPeter Maydell #include "user-internals.h"
403ad0a769SPeter Maydell #include "loader.h"
415423e6d3SPeter Maydell #include "user-mmap.h"
42e5fe0c52Spbrook #include "flat.h"
4394db8de1SPeter Maydell #include "target_flat.h"
44e5fe0c52Spbrook 
45e5fe0c52Spbrook //#define DEBUG
46e5fe0c52Spbrook 
47e5fe0c52Spbrook #ifdef DEBUG
48001faf32SBlue Swirl #define	DBG_FLT(...)	printf(__VA_ARGS__)
49e5fe0c52Spbrook #else
50001faf32SBlue Swirl #define	DBG_FLT(...)
51e5fe0c52Spbrook #endif
52e5fe0c52Spbrook 
53e5fe0c52Spbrook #define RELOC_FAILED 0xff00ff01		/* Relocation incorrect somewhere */
54e5fe0c52Spbrook #define UNLOADED_LIB 0x7ff000ff		/* Placeholder for unused library */
55e5fe0c52Spbrook 
56e5fe0c52Spbrook struct lib_info {
57992f48a0Sblueswir1     abi_ulong start_code;       /* Start of text segment */
58992f48a0Sblueswir1     abi_ulong start_data;       /* Start of data segment */
59992f48a0Sblueswir1     abi_ulong end_data;         /* Start of bss section */
60992f48a0Sblueswir1     abi_ulong start_brk;        /* End of data segment */
61992f48a0Sblueswir1     abi_ulong text_len;	        /* Length of text segment */
62992f48a0Sblueswir1     abi_ulong entry;	        /* Start address for this module */
63992f48a0Sblueswir1     abi_ulong build_date;       /* When this one was compiled */
64e5fe0c52Spbrook     short loaded;		/* Has this library been loaded? */
65e5fe0c52Spbrook };
66e5fe0c52Spbrook 
67e5fe0c52Spbrook #ifdef CONFIG_BINFMT_SHARED_FLAT
68e5fe0c52Spbrook static int load_flat_shared_library(int id, struct lib_info *p);
69e5fe0c52Spbrook #endif
70e5fe0c52Spbrook 
71e5fe0c52Spbrook struct linux_binprm;
72e5fe0c52Spbrook 
73e5fe0c52Spbrook /****************************************************************************/
74e5fe0c52Spbrook /*
75e5fe0c52Spbrook  * create_flat_tables() parses the env- and arg-strings in new user
76e5fe0c52Spbrook  * memory and creates the pointer tables from them, and puts their
77e5fe0c52Spbrook  * addresses on the "stack", returning the new stack pointer value.
78e5fe0c52Spbrook  */
79e5fe0c52Spbrook 
80e5fe0c52Spbrook /* Push a block of strings onto the guest stack.  */
copy_strings(abi_ulong p,int n,char ** s)81992f48a0Sblueswir1 static abi_ulong copy_strings(abi_ulong p, int n, char **s)
82e5fe0c52Spbrook {
83e5fe0c52Spbrook     int len;
84e5fe0c52Spbrook 
85e5fe0c52Spbrook     while (n-- > 0) {
86e5fe0c52Spbrook         len = strlen(s[n]) + 1;
87e5fe0c52Spbrook         p -= len;
88e5fe0c52Spbrook         memcpy_to_target(p, s[n], len);
89e5fe0c52Spbrook     }
90e5fe0c52Spbrook 
91e5fe0c52Spbrook     return p;
92e5fe0c52Spbrook }
93e5fe0c52Spbrook 
target_pread(int fd,abi_ulong ptr,abi_ulong len,abi_ulong offset)94b1d8e52eSblueswir1 static int target_pread(int fd, abi_ulong ptr, abi_ulong len,
95992f48a0Sblueswir1                         abi_ulong offset)
96e5fe0c52Spbrook {
97e5fe0c52Spbrook     void *buf;
98e5fe0c52Spbrook     int ret;
99e5fe0c52Spbrook 
100579a97f7Sbellard     buf = lock_user(VERIFY_WRITE, ptr, len, 0);
101e5a869edSPeter Maydell     if (!buf) {
102e5a869edSPeter Maydell         return -EFAULT;
103e5a869edSPeter Maydell     }
104e5fe0c52Spbrook     ret = pread(fd, buf, len, offset);
105e5a869edSPeter Maydell     if (ret < 0) {
106e5a869edSPeter Maydell         ret = -errno;
107e5a869edSPeter Maydell     }
108e5fe0c52Spbrook     unlock_user(buf, ptr, len);
109e5fe0c52Spbrook     return ret;
110e5fe0c52Spbrook }
111e5fe0c52Spbrook /****************************************************************************/
112e5fe0c52Spbrook 
113e5fe0c52Spbrook #ifdef CONFIG_BINFMT_ZFLAT
114e5fe0c52Spbrook 
115e5fe0c52Spbrook #include <linux/zlib.h>
116e5fe0c52Spbrook 
117e5fe0c52Spbrook #define LBUFSIZE	4000
118e5fe0c52Spbrook 
119e5fe0c52Spbrook /* gzip flag byte */
120e5fe0c52Spbrook #define ASCII_FLAG   0x01 /* bit 0 set: file probably ASCII text */
121e5fe0c52Spbrook #define CONTINUATION 0x02 /* bit 1 set: continuation of multi-part gzip file */
122e5fe0c52Spbrook #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
123e5fe0c52Spbrook #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
124e5fe0c52Spbrook #define COMMENT      0x10 /* bit 4 set: file comment present */
125e5fe0c52Spbrook #define ENCRYPTED    0x20 /* bit 5 set: file is encrypted */
126e5fe0c52Spbrook #define RESERVED     0xC0 /* bit 6,7:   reserved */
127e5fe0c52Spbrook 
decompress_exec(struct linux_binprm * bprm,unsigned long offset,char * dst,long len,int fd)128e5fe0c52Spbrook static int decompress_exec(
129e5fe0c52Spbrook 	struct linux_binprm *bprm,
130e5fe0c52Spbrook 	unsigned long offset,
131e5fe0c52Spbrook 	char *dst,
132e5fe0c52Spbrook 	long len,
133e5fe0c52Spbrook 	int fd)
134e5fe0c52Spbrook {
135e5fe0c52Spbrook 	unsigned char *buf;
136e5fe0c52Spbrook 	z_stream strm;
137e5fe0c52Spbrook 	loff_t fpos;
138e5fe0c52Spbrook 	int ret, retval;
139e5fe0c52Spbrook 
140e5fe0c52Spbrook 	DBG_FLT("decompress_exec(offset=%x,buf=%x,len=%x)\n",(int)offset, (int)dst, (int)len);
141e5fe0c52Spbrook 
142e5fe0c52Spbrook 	memset(&strm, 0, sizeof(strm));
143e5fe0c52Spbrook 	strm.workspace = kmalloc(zlib_inflate_workspacesize(), GFP_KERNEL);
144e5fe0c52Spbrook 	if (strm.workspace == NULL) {
145e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: no memory for decompress workspace\n");
146e5fe0c52Spbrook 		return -ENOMEM;
147e5fe0c52Spbrook 	}
148e5fe0c52Spbrook 	buf = kmalloc(LBUFSIZE, GFP_KERNEL);
149e5fe0c52Spbrook 	if (buf == NULL) {
150e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: no memory for read buffer\n");
151e5fe0c52Spbrook 		retval = -ENOMEM;
152e5fe0c52Spbrook 		goto out_free;
153e5fe0c52Spbrook 	}
154e5fe0c52Spbrook 
155e5fe0c52Spbrook 	/* Read in first chunk of data and parse gzip header. */
156e5fe0c52Spbrook 	fpos = offset;
157e5fe0c52Spbrook 	ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
158e5fe0c52Spbrook 
159e5fe0c52Spbrook 	strm.next_in = buf;
160e5fe0c52Spbrook 	strm.avail_in = ret;
161e5fe0c52Spbrook 	strm.total_in = 0;
162e5fe0c52Spbrook 
163e5fe0c52Spbrook 	retval = -ENOEXEC;
164e5fe0c52Spbrook 
165e5fe0c52Spbrook 	/* Check minimum size -- gzip header */
166e5fe0c52Spbrook 	if (ret < 10) {
167e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: file too small?\n");
168e5fe0c52Spbrook 		goto out_free_buf;
169e5fe0c52Spbrook 	}
170e5fe0c52Spbrook 
171e5fe0c52Spbrook 	/* Check gzip magic number */
172e5fe0c52Spbrook 	if ((buf[0] != 037) || ((buf[1] != 0213) && (buf[1] != 0236))) {
173e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: unknown compression magic?\n");
174e5fe0c52Spbrook 		goto out_free_buf;
175e5fe0c52Spbrook 	}
176e5fe0c52Spbrook 
177e5fe0c52Spbrook 	/* Check gzip method */
178e5fe0c52Spbrook 	if (buf[2] != 8) {
179e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: unknown compression method?\n");
180e5fe0c52Spbrook 		goto out_free_buf;
181e5fe0c52Spbrook 	}
182e5fe0c52Spbrook 	/* Check gzip flags */
183e5fe0c52Spbrook 	if ((buf[3] & ENCRYPTED) || (buf[3] & CONTINUATION) ||
184e5fe0c52Spbrook 	    (buf[3] & RESERVED)) {
185e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: unknown flags?\n");
186e5fe0c52Spbrook 		goto out_free_buf;
187e5fe0c52Spbrook 	}
188e5fe0c52Spbrook 
189e5fe0c52Spbrook 	ret = 10;
190e5fe0c52Spbrook 	if (buf[3] & EXTRA_FIELD) {
191e5fe0c52Spbrook 		ret += 2 + buf[10] + (buf[11] << 8);
192e5fe0c52Spbrook 		if (unlikely(LBUFSIZE == ret)) {
193e5fe0c52Spbrook 			DBG_FLT("binfmt_flat: buffer overflow (EXTRA)?\n");
194e5fe0c52Spbrook 			goto out_free_buf;
195e5fe0c52Spbrook 		}
196e5fe0c52Spbrook 	}
197e5fe0c52Spbrook 	if (buf[3] & ORIG_NAME) {
198e5fe0c52Spbrook 		for (; ret < LBUFSIZE && (buf[ret] != 0); ret++)
199e5fe0c52Spbrook 			;
200e5fe0c52Spbrook 		if (unlikely(LBUFSIZE == ret)) {
201e5fe0c52Spbrook 			DBG_FLT("binfmt_flat: buffer overflow (ORIG_NAME)?\n");
202e5fe0c52Spbrook 			goto out_free_buf;
203e5fe0c52Spbrook 		}
204e5fe0c52Spbrook 	}
205e5fe0c52Spbrook 	if (buf[3] & COMMENT) {
206e5fe0c52Spbrook 		for (;  ret < LBUFSIZE && (buf[ret] != 0); ret++)
207e5fe0c52Spbrook 			;
208e5fe0c52Spbrook 		if (unlikely(LBUFSIZE == ret)) {
209e5fe0c52Spbrook 			DBG_FLT("binfmt_flat: buffer overflow (COMMENT)?\n");
210e5fe0c52Spbrook 			goto out_free_buf;
211e5fe0c52Spbrook 		}
212e5fe0c52Spbrook 	}
213e5fe0c52Spbrook 
214e5fe0c52Spbrook 	strm.next_in += ret;
215e5fe0c52Spbrook 	strm.avail_in -= ret;
216e5fe0c52Spbrook 
217e5fe0c52Spbrook 	strm.next_out = dst;
218e5fe0c52Spbrook 	strm.avail_out = len;
219e5fe0c52Spbrook 	strm.total_out = 0;
220e5fe0c52Spbrook 
221e5fe0c52Spbrook 	if (zlib_inflateInit2(&strm, -MAX_WBITS) != Z_OK) {
222e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: zlib init failed?\n");
223e5fe0c52Spbrook 		goto out_free_buf;
224e5fe0c52Spbrook 	}
225e5fe0c52Spbrook 
226e5fe0c52Spbrook 	while ((ret = zlib_inflate(&strm, Z_NO_FLUSH)) == Z_OK) {
227e5fe0c52Spbrook 		ret = bprm->file->f_op->read(bprm->file, buf, LBUFSIZE, &fpos);
228e5fe0c52Spbrook 		if (ret <= 0)
229e5fe0c52Spbrook 			break;
23071987b12SPhilippe Mathieu-Daudé                 if (is_error(ret)) {
231e5fe0c52Spbrook 			break;
23271987b12SPhilippe Mathieu-Daudé                 }
233e5fe0c52Spbrook 		len -= ret;
234e5fe0c52Spbrook 
235e5fe0c52Spbrook 		strm.next_in = buf;
236e5fe0c52Spbrook 		strm.avail_in = ret;
237e5fe0c52Spbrook 		strm.total_in = 0;
238e5fe0c52Spbrook 	}
239e5fe0c52Spbrook 
240e5fe0c52Spbrook 	if (ret < 0) {
241e5fe0c52Spbrook 		DBG_FLT("binfmt_flat: decompression failed (%d), %s\n",
242e5fe0c52Spbrook 			ret, strm.msg);
243e5fe0c52Spbrook 		goto out_zlib;
244e5fe0c52Spbrook 	}
245e5fe0c52Spbrook 
246e5fe0c52Spbrook 	retval = 0;
247e5fe0c52Spbrook out_zlib:
248e5fe0c52Spbrook 	zlib_inflateEnd(&strm);
249e5fe0c52Spbrook out_free_buf:
250e5fe0c52Spbrook 	kfree(buf);
251e5fe0c52Spbrook out_free:
252e5fe0c52Spbrook 	kfree(strm.workspace);
253e5fe0c52Spbrook out:
254e5fe0c52Spbrook 	return retval;
255e5fe0c52Spbrook }
256e5fe0c52Spbrook 
257e5fe0c52Spbrook #endif /* CONFIG_BINFMT_ZFLAT */
258e5fe0c52Spbrook 
259e5fe0c52Spbrook /****************************************************************************/
260e5fe0c52Spbrook 
261992f48a0Sblueswir1 static abi_ulong
calc_reloc(abi_ulong r,struct lib_info * p,int curid,int internalp)262992f48a0Sblueswir1 calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
263e5fe0c52Spbrook {
264992f48a0Sblueswir1     abi_ulong addr;
265e5fe0c52Spbrook     int id;
266992f48a0Sblueswir1     abi_ulong start_brk;
267992f48a0Sblueswir1     abi_ulong start_data;
268992f48a0Sblueswir1     abi_ulong text_len;
269992f48a0Sblueswir1     abi_ulong start_code;
270e5fe0c52Spbrook 
271e5fe0c52Spbrook #ifdef CONFIG_BINFMT_SHARED_FLAT
272e5fe0c52Spbrook #error needs checking
273e5fe0c52Spbrook     if (r == 0)
274e5fe0c52Spbrook         id = curid;	/* Relocs of 0 are always self referring */
275e5fe0c52Spbrook     else {
276e5fe0c52Spbrook         id = (r >> 24) & 0xff;	/* Find ID for this reloc */
277e5fe0c52Spbrook         r &= 0x00ffffff;	/* Trim ID off here */
278e5fe0c52Spbrook     }
279e5fe0c52Spbrook     if (id >= MAX_SHARED_LIBS) {
280e5fe0c52Spbrook         fprintf(stderr, "BINFMT_FLAT: reference 0x%x to shared library %d\n",
281e5fe0c52Spbrook                 (unsigned) r, id);
282e5fe0c52Spbrook         goto failed;
283e5fe0c52Spbrook     }
284e5fe0c52Spbrook     if (curid != id) {
285e5fe0c52Spbrook         if (internalp) {
286e5fe0c52Spbrook             fprintf(stderr, "BINFMT_FLAT: reloc address 0x%x not "
287e5fe0c52Spbrook                     "in same module (%d != %d)\n",
288e5fe0c52Spbrook                     (unsigned) r, curid, id);
289e5fe0c52Spbrook             goto failed;
29071987b12SPhilippe Mathieu-Daudé         } else if (!p[id].loaded && is_error(load_flat_shared_library(id, p))) {
291e5fe0c52Spbrook             fprintf(stderr, "BINFMT_FLAT: failed to load library %d\n", id);
292e5fe0c52Spbrook             goto failed;
293e5fe0c52Spbrook         }
294e5fe0c52Spbrook         /* Check versioning information (i.e. time stamps) */
295e5fe0c52Spbrook         if (p[id].build_date && p[curid].build_date
296e5fe0c52Spbrook             && p[curid].build_date < p[id].build_date) {
297e5fe0c52Spbrook             fprintf(stderr, "BINFMT_FLAT: library %d is younger than %d\n",
298e5fe0c52Spbrook                     id, curid);
299e5fe0c52Spbrook             goto failed;
300e5fe0c52Spbrook         }
301e5fe0c52Spbrook     }
302e5fe0c52Spbrook #else
303e5fe0c52Spbrook     id = 0;
304e5fe0c52Spbrook #endif
305e5fe0c52Spbrook 
306e5fe0c52Spbrook     start_brk = p[id].start_brk;
307e5fe0c52Spbrook     start_data = p[id].start_data;
308e5fe0c52Spbrook     start_code = p[id].start_code;
309e5fe0c52Spbrook     text_len = p[id].text_len;
310e5fe0c52Spbrook 
311e5fe0c52Spbrook     if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
312e5fe0c52Spbrook         fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
313e5fe0c52Spbrook                 "(0 - 0x%x/0x%x)\n",
314e5fe0c52Spbrook                (int) r,(int)(start_brk-start_code),(int)text_len);
315e5fe0c52Spbrook         goto failed;
316e5fe0c52Spbrook     }
317e5fe0c52Spbrook 
318e5fe0c52Spbrook     if (r < text_len)			/* In text segment */
319e5fe0c52Spbrook         addr = r + start_code;
320e5fe0c52Spbrook     else					/* In data segment */
321e5fe0c52Spbrook         addr = r - text_len + start_data;
322e5fe0c52Spbrook 
323e5fe0c52Spbrook     /* Range checked already above so doing the range tests is redundant...*/
324e5fe0c52Spbrook     return(addr);
325e5fe0c52Spbrook 
326e5fe0c52Spbrook failed:
327e5fe0c52Spbrook     abort();
328e5fe0c52Spbrook     return RELOC_FAILED;
329e5fe0c52Spbrook }
330e5fe0c52Spbrook 
331e5fe0c52Spbrook /****************************************************************************/
332e5fe0c52Spbrook 
333e5fe0c52Spbrook /* ??? This does not handle endianness correctly.  */
old_reloc(struct lib_info * libinfo,uint32_t rl)334b1d8e52eSblueswir1 static void old_reloc(struct lib_info *libinfo, uint32_t rl)
335e5fe0c52Spbrook {
336e5fe0c52Spbrook #ifdef DEBUG
337564e2fe8SRiccardo Magliocchetti 	const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
338e5fe0c52Spbrook #endif
339e5fe0c52Spbrook 	uint32_t *ptr;
340e5fe0c52Spbrook         uint32_t offset;
341e5fe0c52Spbrook         int reloc_type;
342e5fe0c52Spbrook 
343e5fe0c52Spbrook         offset = rl & 0x3fffffff;
344e5fe0c52Spbrook         reloc_type = rl >> 30;
345e5fe0c52Spbrook         /* ??? How to handle this?  */
346e5fe0c52Spbrook #if defined(CONFIG_COLDFIRE)
347526ccb7aSbalrog 	ptr = (uint32_t *) ((unsigned long) libinfo->start_code + offset);
348e5fe0c52Spbrook #else
349526ccb7aSbalrog 	ptr = (uint32_t *) ((unsigned long) libinfo->start_data + offset);
350e5fe0c52Spbrook #endif
351e5fe0c52Spbrook 
352e5fe0c52Spbrook #ifdef DEBUG
353e5fe0c52Spbrook 	fprintf(stderr, "Relocation of variable at DATASEG+%x "
354e5fe0c52Spbrook 		"(address %p, currently %x) into segment %s\n",
355e5fe0c52Spbrook 		offset, ptr, (int)*ptr, segment[reloc_type]);
356e5fe0c52Spbrook #endif
357e5fe0c52Spbrook 
358e5fe0c52Spbrook 	switch (reloc_type) {
359e5fe0c52Spbrook 	case OLD_FLAT_RELOC_TYPE_TEXT:
360e5fe0c52Spbrook 		*ptr += libinfo->start_code;
361e5fe0c52Spbrook 		break;
362e5fe0c52Spbrook 	case OLD_FLAT_RELOC_TYPE_DATA:
363e5fe0c52Spbrook 		*ptr += libinfo->start_data;
364e5fe0c52Spbrook 		break;
365e5fe0c52Spbrook 	case OLD_FLAT_RELOC_TYPE_BSS:
366e5fe0c52Spbrook 		*ptr += libinfo->end_data;
367e5fe0c52Spbrook 		break;
368e5fe0c52Spbrook 	default:
369e5fe0c52Spbrook 		fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
370e5fe0c52Spbrook                         reloc_type);
371e5fe0c52Spbrook 		break;
372e5fe0c52Spbrook 	}
373e5fe0c52Spbrook 	DBG_FLT("Relocation became %x\n", (int)*ptr);
374e5fe0c52Spbrook }
375e5fe0c52Spbrook 
376e5fe0c52Spbrook /****************************************************************************/
377e5fe0c52Spbrook 
load_flat_file(struct linux_binprm * bprm,struct lib_info * libinfo,int id,abi_ulong * extra_stack)378e5fe0c52Spbrook static int load_flat_file(struct linux_binprm * bprm,
379992f48a0Sblueswir1 		struct lib_info *libinfo, int id, abi_ulong *extra_stack)
380e5fe0c52Spbrook {
381e5fe0c52Spbrook     struct flat_hdr * hdr;
382f562e716SBlue Swirl     abi_ulong textpos = 0, datapos = 0;
383f562e716SBlue Swirl     abi_long result;
384992f48a0Sblueswir1     abi_ulong realdatastart = 0;
385992f48a0Sblueswir1     abi_ulong text_len, data_len, bss_len, stack_len, flags;
386992f48a0Sblueswir1     abi_ulong extra;
387992f48a0Sblueswir1     abi_ulong reloc = 0, rp;
388e5fe0c52Spbrook     int i, rev, relocs = 0;
389992f48a0Sblueswir1     abi_ulong fpos;
390e7730352SJuan Quintela     abi_ulong start_code;
391992f48a0Sblueswir1     abi_ulong indx_len;
392e5fe0c52Spbrook 
393e5fe0c52Spbrook     hdr = ((struct flat_hdr *) bprm->buf);		/* exec-header */
394e5fe0c52Spbrook 
395e5fe0c52Spbrook     text_len  = ntohl(hdr->data_start);
396e5fe0c52Spbrook     data_len  = ntohl(hdr->data_end) - ntohl(hdr->data_start);
397e5fe0c52Spbrook     bss_len   = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
398e5fe0c52Spbrook     stack_len = ntohl(hdr->stack_size);
399e5fe0c52Spbrook     if (extra_stack) {
400e5fe0c52Spbrook         stack_len += *extra_stack;
401e5fe0c52Spbrook         *extra_stack = stack_len;
402e5fe0c52Spbrook     }
403e5fe0c52Spbrook     relocs    = ntohl(hdr->reloc_count);
404e5fe0c52Spbrook     flags     = ntohl(hdr->flags);
405e5fe0c52Spbrook     rev       = ntohl(hdr->rev);
406e5fe0c52Spbrook 
407e5fe0c52Spbrook     DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
408e5fe0c52Spbrook 
409e5fe0c52Spbrook     if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
410e5fe0c52Spbrook         fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
411e5fe0c52Spbrook                 rev, (int) FLAT_VERSION);
412e5fe0c52Spbrook         return -ENOEXEC;
413e5fe0c52Spbrook     }
414e5fe0c52Spbrook 
415e5fe0c52Spbrook     /* Don't allow old format executables to use shared libraries */
416e5fe0c52Spbrook     if (rev == OLD_FLAT_VERSION && id != 0) {
417e5fe0c52Spbrook         fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
418e5fe0c52Spbrook         return -ENOEXEC;
419e5fe0c52Spbrook     }
420e5fe0c52Spbrook 
421e5fe0c52Spbrook     /*
422e5fe0c52Spbrook      * fix up the flags for the older format,  there were all kinds
423e5fe0c52Spbrook      * of endian hacks,  this only works for the simple cases
424e5fe0c52Spbrook      */
425e5fe0c52Spbrook     if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
426e5fe0c52Spbrook         flags = FLAT_FLAG_RAM;
427e5fe0c52Spbrook 
428e5fe0c52Spbrook #ifndef CONFIG_BINFMT_ZFLAT
429e5fe0c52Spbrook     if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
430e5fe0c52Spbrook         fprintf(stderr, "Support for ZFLAT executables is not enabled\n");
431e5fe0c52Spbrook         return -ENOEXEC;
432e5fe0c52Spbrook     }
433e5fe0c52Spbrook #endif
434e5fe0c52Spbrook 
435e5fe0c52Spbrook     /*
436e5fe0c52Spbrook      * calculate the extra space we need to map in
437e5fe0c52Spbrook      */
438992f48a0Sblueswir1     extra = relocs * sizeof(abi_ulong);
439e5fe0c52Spbrook     if (extra < bss_len + stack_len)
440e5fe0c52Spbrook         extra = bss_len + stack_len;
441e5fe0c52Spbrook 
4422a1094cdSpbrook     /* Add space for library base pointers.  Make sure this does not
4432a1094cdSpbrook        misalign the  doesn't misalign the data segment.  */
444992f48a0Sblueswir1     indx_len = MAX_SHARED_LIBS * sizeof(abi_ulong);
445992f48a0Sblueswir1     indx_len = (indx_len + 15) & ~(abi_ulong)15;
4462a1094cdSpbrook 
447e5fe0c52Spbrook     /*
4486f9ff551Szhaolichang      * Allocate the address space.
449ee947430SAlex Bennée      */
450ee947430SAlex Bennée     probe_guest_base(bprm->filename, 0,
451a3a67f54SRichard Henderson                      text_len + data_len + extra + indx_len - 1);
452ee947430SAlex Bennée 
453ee947430SAlex Bennée     /*
454e5fe0c52Spbrook      * there are a couple of cases here,  the separate code/data
455e5fe0c52Spbrook      * case,  and then the fully copied to RAM case which lumps
456e5fe0c52Spbrook      * it all together.
457e5fe0c52Spbrook      */
458e5fe0c52Spbrook     if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
459e5fe0c52Spbrook         /*
460e5fe0c52Spbrook          * this should give us a ROM ptr,  but if it doesn't we don't
461e5fe0c52Spbrook          * really care
462e5fe0c52Spbrook          */
463e5fe0c52Spbrook         DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
464e5fe0c52Spbrook 
465e5fe0c52Spbrook         textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
466*d0b6b793SRichard Henderson                               MAP_PRIVATE, bprm->src.fd, 0);
467e5fe0c52Spbrook         if (textpos == -1) {
468e5fe0c52Spbrook             fprintf(stderr, "Unable to mmap process text\n");
469e5fe0c52Spbrook             return -1;
470e5fe0c52Spbrook         }
471e5fe0c52Spbrook 
4722a1094cdSpbrook         realdatastart = target_mmap(0, data_len + extra + indx_len,
473e5fe0c52Spbrook                                     PROT_READ|PROT_WRITE|PROT_EXEC,
474e5fe0c52Spbrook                                     MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
475e5fe0c52Spbrook 
476e5fe0c52Spbrook         if (realdatastart == -1) {
477e5fe0c52Spbrook             fprintf(stderr, "Unable to allocate RAM for process data\n");
478e5fe0c52Spbrook             return realdatastart;
479e5fe0c52Spbrook         }
4802a1094cdSpbrook         datapos = realdatastart + indx_len;
481e5fe0c52Spbrook 
482e5fe0c52Spbrook         DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
483e5fe0c52Spbrook                         (int)(data_len + bss_len + stack_len), (int)datapos);
484e5fe0c52Spbrook 
485e5fe0c52Spbrook         fpos = ntohl(hdr->data_start);
486e5fe0c52Spbrook #ifdef CONFIG_BINFMT_ZFLAT
487e5fe0c52Spbrook         if (flags & FLAT_FLAG_GZDATA) {
488e5fe0c52Spbrook             result = decompress_exec(bprm, fpos, (char *) datapos,
489992f48a0Sblueswir1                                      data_len + (relocs * sizeof(abi_ulong)))
490e5fe0c52Spbrook         } else
491e5fe0c52Spbrook #endif
492e5fe0c52Spbrook         {
493*d0b6b793SRichard Henderson             result = target_pread(bprm->src.fd, datapos,
494992f48a0Sblueswir1                                   data_len + (relocs * sizeof(abi_ulong)),
495e5fe0c52Spbrook                                   fpos);
496e5fe0c52Spbrook         }
497e5fe0c52Spbrook         if (result < 0) {
498e5fe0c52Spbrook             fprintf(stderr, "Unable to read data+bss\n");
499e5fe0c52Spbrook             return result;
500e5fe0c52Spbrook         }
501e5fe0c52Spbrook 
502e5fe0c52Spbrook         reloc = datapos + (ntohl(hdr->reloc_start) - text_len);
503e5fe0c52Spbrook 
504e5fe0c52Spbrook     } else {
505e5fe0c52Spbrook 
5062a1094cdSpbrook         textpos = target_mmap(0, text_len + data_len + extra + indx_len,
507e5fe0c52Spbrook                               PROT_READ | PROT_EXEC | PROT_WRITE,
508e5fe0c52Spbrook                               MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
509e5fe0c52Spbrook         if (textpos == -1 ) {
510e5fe0c52Spbrook             fprintf(stderr, "Unable to allocate RAM for process text/data\n");
511e5fe0c52Spbrook             return -1;
512e5fe0c52Spbrook         }
513e5fe0c52Spbrook 
514e5fe0c52Spbrook         realdatastart = textpos + ntohl(hdr->data_start);
5152a1094cdSpbrook         datapos = realdatastart + indx_len;
5162a1094cdSpbrook         reloc = (textpos + ntohl(hdr->reloc_start) + indx_len);
517e5fe0c52Spbrook 
518e5fe0c52Spbrook #ifdef CONFIG_BINFMT_ZFLAT
519e5fe0c52Spbrook #error code needs checking
520e5fe0c52Spbrook         /*
521e5fe0c52Spbrook          * load it all in and treat it like a RAM load from now on
522e5fe0c52Spbrook          */
523e5fe0c52Spbrook         if (flags & FLAT_FLAG_GZIP) {
524e5fe0c52Spbrook                 result = decompress_exec(bprm, sizeof (struct flat_hdr),
525e5fe0c52Spbrook                                  (((char *) textpos) + sizeof (struct flat_hdr)),
526e5fe0c52Spbrook                                  (text_len + data_len + (relocs * sizeof(unsigned long))
527e5fe0c52Spbrook                                           - sizeof (struct flat_hdr)),
528e5fe0c52Spbrook                                  0);
529e5fe0c52Spbrook                 memmove((void *) datapos, (void *) realdatastart,
530e5fe0c52Spbrook                                 data_len + (relocs * sizeof(unsigned long)));
531e5fe0c52Spbrook         } else if (flags & FLAT_FLAG_GZDATA) {
532e5fe0c52Spbrook                 fpos = 0;
533e5fe0c52Spbrook                 result = bprm->file->f_op->read(bprm->file,
534e5fe0c52Spbrook                                 (char *) textpos, text_len, &fpos);
53571987b12SPhilippe Mathieu-Daudé                 if (!is_error(result)) {
536e5fe0c52Spbrook                         result = decompress_exec(bprm, text_len, (char *) datapos,
537e5fe0c52Spbrook                                          data_len + (relocs * sizeof(unsigned long)), 0);
538e5fe0c52Spbrook                 }
53971987b12SPhilippe Mathieu-Daudé         }
540e5fe0c52Spbrook         else
541e5fe0c52Spbrook #endif
542e5fe0c52Spbrook         {
543*d0b6b793SRichard Henderson             result = target_pread(bprm->src.fd, textpos,
544e5fe0c52Spbrook                                   text_len, 0);
545e5fe0c52Spbrook             if (result >= 0) {
546*d0b6b793SRichard Henderson                 result = target_pread(bprm->src.fd, datapos,
547992f48a0Sblueswir1                     data_len + (relocs * sizeof(abi_ulong)),
548e5fe0c52Spbrook                     ntohl(hdr->data_start));
549e5fe0c52Spbrook             }
550e5fe0c52Spbrook         }
551e5fe0c52Spbrook         if (result < 0) {
552e5fe0c52Spbrook             fprintf(stderr, "Unable to read code+data+bss\n");
553e5fe0c52Spbrook             return result;
554e5fe0c52Spbrook         }
555e5fe0c52Spbrook     }
556e5fe0c52Spbrook 
557e5fe0c52Spbrook     DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
558e5fe0c52Spbrook             (int)textpos, 0x00ffffff&ntohl(hdr->entry),
559e5fe0c52Spbrook             ntohl(hdr->data_start));
560e5fe0c52Spbrook 
561e5fe0c52Spbrook     /* The main program needs a little extra setup in the task structure */
562e5fe0c52Spbrook     start_code = textpos + sizeof (struct flat_hdr);
563e5fe0c52Spbrook 
564e5fe0c52Spbrook     DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
565e5fe0c52Spbrook             id ? "Lib" : "Load", bprm->filename,
566e7730352SJuan Quintela             (int) start_code, (int) (textpos + text_len),
567e5fe0c52Spbrook             (int) datapos,
568e5fe0c52Spbrook             (int) (datapos + data_len),
569e5fe0c52Spbrook             (int) (datapos + data_len),
570e5fe0c52Spbrook             (int) (((datapos + data_len + bss_len) + 3) & ~3));
571e5fe0c52Spbrook 
572e5fe0c52Spbrook     text_len -= sizeof(struct flat_hdr); /* the real code len */
573e5fe0c52Spbrook 
574e5fe0c52Spbrook     /* Store the current module values into the global library structure */
575e5fe0c52Spbrook     libinfo[id].start_code = start_code;
576e5fe0c52Spbrook     libinfo[id].start_data = datapos;
577e5fe0c52Spbrook     libinfo[id].end_data = datapos + data_len;
578e5fe0c52Spbrook     libinfo[id].start_brk = datapos + data_len + bss_len;
579e5fe0c52Spbrook     libinfo[id].text_len = text_len;
580e5fe0c52Spbrook     libinfo[id].loaded = 1;
581e5fe0c52Spbrook     libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
582e5fe0c52Spbrook     libinfo[id].build_date = ntohl(hdr->build_date);
583e5fe0c52Spbrook 
584e5fe0c52Spbrook     /*
585e5fe0c52Spbrook      * We just load the allocations into some temporary memory to
586e5fe0c52Spbrook      * help simplify all this mumbo jumbo
587e5fe0c52Spbrook      *
588e5fe0c52Spbrook      * We've got two different sections of relocation entries.
589b4916d7bSDong Xu Wang      * The first is the GOT which resides at the beginning of the data segment
590e5fe0c52Spbrook      * and is terminated with a -1.  This one can be relocated in place.
591e5fe0c52Spbrook      * The second is the extra relocation entries tacked after the image's
592e5fe0c52Spbrook      * data segment. These require a little more processing as the entry is
593e5fe0c52Spbrook      * really an offset into the image which contains an offset into the
594e5fe0c52Spbrook      * image.
595e5fe0c52Spbrook      */
596e5fe0c52Spbrook     if (flags & FLAT_FLAG_GOTPIC) {
597e5fe0c52Spbrook         rp = datapos;
598e5fe0c52Spbrook         while (1) {
599992f48a0Sblueswir1             abi_ulong addr;
6002f619698Sbellard             if (get_user_ual(addr, rp))
6012f619698Sbellard                 return -EFAULT;
602e5fe0c52Spbrook             if (addr == -1)
603e5fe0c52Spbrook                 break;
604e5fe0c52Spbrook             if (addr) {
605e5fe0c52Spbrook                 addr = calc_reloc(addr, libinfo, id, 0);
606e5fe0c52Spbrook                 if (addr == RELOC_FAILED)
607e5fe0c52Spbrook                     return -ENOEXEC;
6082f619698Sbellard                 if (put_user_ual(addr, rp))
6092f619698Sbellard                     return -EFAULT;
610e5fe0c52Spbrook             }
611992f48a0Sblueswir1             rp += sizeof(abi_ulong);
612e5fe0c52Spbrook         }
613e5fe0c52Spbrook     }
614e5fe0c52Spbrook 
615e5fe0c52Spbrook     /*
616e5fe0c52Spbrook      * Now run through the relocation entries.
617e5fe0c52Spbrook      * We've got to be careful here as C++ produces relocatable zero
618e5fe0c52Spbrook      * entries in the constructor and destructor tables which are then
619e5fe0c52Spbrook      * tested for being not zero (which will always occur unless we're
620e5fe0c52Spbrook      * based from address zero).  This causes an endless loop as __start
621e5fe0c52Spbrook      * is at zero.  The solution used is to not relocate zero addresses.
622e5fe0c52Spbrook      * This has the negative side effect of not allowing a global data
623e5fe0c52Spbrook      * reference to be statically initialised to _stext (I've moved
624e5fe0c52Spbrook      * __start to address 4 so that is okay).
625e5fe0c52Spbrook      */
626e5fe0c52Spbrook     if (rev > OLD_FLAT_VERSION) {
627c3109ba1SMike Frysinger         abi_ulong persistent = 0;
628e5fe0c52Spbrook         for (i = 0; i < relocs; i++) {
629992f48a0Sblueswir1             abi_ulong addr, relval;
630e5fe0c52Spbrook 
631e5fe0c52Spbrook             /* Get the address of the pointer to be
632e5fe0c52Spbrook                relocated (of course, the address has to be
633e5fe0c52Spbrook                relocated first).  */
6342f619698Sbellard             if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
6352f619698Sbellard                 return -EFAULT;
636c3109ba1SMike Frysinger             relval = ntohl(relval);
637c3109ba1SMike Frysinger             if (flat_set_persistent(relval, &persistent))
638c3109ba1SMike Frysinger                 continue;
639e5fe0c52Spbrook             addr = flat_get_relocate_addr(relval);
640e5fe0c52Spbrook             rp = calc_reloc(addr, libinfo, id, 1);
641e5fe0c52Spbrook             if (rp == RELOC_FAILED)
642e5fe0c52Spbrook                 return -ENOEXEC;
643e5fe0c52Spbrook 
644e5fe0c52Spbrook             /* Get the pointer's value.  */
6452f619698Sbellard             if (get_user_ual(addr, rp))
6462f619698Sbellard                 return -EFAULT;
6479721cf2cSCorey J. Boyle             addr = flat_get_addr_from_rp(addr, relval, flags, &persistent);
648e5fe0c52Spbrook             if (addr != 0) {
649e5fe0c52Spbrook                 /*
650e5fe0c52Spbrook                  * Do the relocation.  PIC relocs in the data section are
651e5fe0c52Spbrook                  * already in target order
652e5fe0c52Spbrook                  */
653e5fe0c52Spbrook                 if ((flags & FLAT_FLAG_GOTPIC) == 0)
654c3109ba1SMike Frysinger                     addr = ntohl(addr);
655e5fe0c52Spbrook                 addr = calc_reloc(addr, libinfo, id, 0);
656e5fe0c52Spbrook                 if (addr == RELOC_FAILED)
657e5fe0c52Spbrook                     return -ENOEXEC;
658e5fe0c52Spbrook 
659e5fe0c52Spbrook                 /* Write back the relocated pointer.  */
660c3109ba1SMike Frysinger                 if (flat_put_addr_at_rp(rp, addr, relval))
6612f619698Sbellard                     return -EFAULT;
662e5fe0c52Spbrook             }
663e5fe0c52Spbrook         }
664e5fe0c52Spbrook     } else {
665e5fe0c52Spbrook         for (i = 0; i < relocs; i++) {
666992f48a0Sblueswir1             abi_ulong relval;
6672f619698Sbellard             if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
6682f619698Sbellard                 return -EFAULT;
669e5fe0c52Spbrook             old_reloc(&libinfo[0], relval);
670e5fe0c52Spbrook         }
671e5fe0c52Spbrook     }
672e5fe0c52Spbrook 
673e5fe0c52Spbrook     /* zero the BSS.  */
6743e8f1628SRichard Henderson     memset(g2h_untagged(datapos + data_len), 0, bss_len);
675e5fe0c52Spbrook 
676e5fe0c52Spbrook     return 0;
677e5fe0c52Spbrook }
678e5fe0c52Spbrook 
679e5fe0c52Spbrook 
680e5fe0c52Spbrook /****************************************************************************/
681e5fe0c52Spbrook #ifdef CONFIG_BINFMT_SHARED_FLAT
682e5fe0c52Spbrook 
683e5fe0c52Spbrook /*
684e5fe0c52Spbrook  * Load a shared library into memory.  The library gets its own data
685e5fe0c52Spbrook  * segment (including bss) but not argv/argc/environ.
686e5fe0c52Spbrook  */
687e5fe0c52Spbrook 
load_flat_shared_library(int id,struct lib_info * libs)688e5fe0c52Spbrook static int load_flat_shared_library(int id, struct lib_info *libs)
689e5fe0c52Spbrook {
690e5fe0c52Spbrook 	struct linux_binprm bprm;
691e5fe0c52Spbrook 	int res;
692e5fe0c52Spbrook 	char buf[16];
693e5fe0c52Spbrook 
694e5fe0c52Spbrook 	/* Create the file name */
695e5fe0c52Spbrook 	sprintf(buf, "/lib/lib%d.so", id);
696e5fe0c52Spbrook 
697e5fe0c52Spbrook 	/* Open the file up */
698e5fe0c52Spbrook 	bprm.filename = buf;
699e5fe0c52Spbrook 	bprm.file = open_exec(bprm.filename);
700e5fe0c52Spbrook 	res = PTR_ERR(bprm.file);
701e5fe0c52Spbrook 	if (IS_ERR(bprm.file))
702e5fe0c52Spbrook 		return res;
703e5fe0c52Spbrook 
704e5fe0c52Spbrook 	res = prepare_binprm(&bprm);
705e5fe0c52Spbrook 
70671987b12SPhilippe Mathieu-Daudé         if (!is_error(res)) {
707e5fe0c52Spbrook 		res = load_flat_file(&bprm, libs, id, NULL);
70871987b12SPhilippe Mathieu-Daudé         }
709e5fe0c52Spbrook 	if (bprm.file) {
710e5fe0c52Spbrook 		allow_write_access(bprm.file);
711e5fe0c52Spbrook 		fput(bprm.file);
712e5fe0c52Spbrook 		bprm.file = NULL;
713e5fe0c52Spbrook 	}
714e5fe0c52Spbrook 	return(res);
715e5fe0c52Spbrook }
716e5fe0c52Spbrook 
717e5fe0c52Spbrook #endif /* CONFIG_BINFMT_SHARED_FLAT */
718e5fe0c52Spbrook 
load_flt_binary(struct linux_binprm * bprm,struct image_info * info)719f0116c54SWill Newton int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
720e5fe0c52Spbrook {
721e5fe0c52Spbrook     struct lib_info libinfo[MAX_SHARED_LIBS];
72259baae9aSStefan Brüns     abi_ulong p;
723992f48a0Sblueswir1     abi_ulong stack_len;
724992f48a0Sblueswir1     abi_ulong start_addr;
725992f48a0Sblueswir1     abi_ulong sp;
726e5fe0c52Spbrook     int res;
727e5fe0c52Spbrook     int i, j;
728e5fe0c52Spbrook 
729e5fe0c52Spbrook     memset(libinfo, 0, sizeof(libinfo));
730e5fe0c52Spbrook     /*
731e5fe0c52Spbrook      * We have to add the size of our arguments to our stack size
732e5fe0c52Spbrook      * otherwise it's too easy for users to create stack overflows
733e5fe0c52Spbrook      * by passing in a huge argument list.  And yes,  we have to be
734e5fe0c52Spbrook      * pedantic and include space for the argv/envp array as it may have
735e5fe0c52Spbrook      * a lot of entries.
736e5fe0c52Spbrook      */
73782a39595SMike Frysinger     stack_len = 0;
73882a39595SMike Frysinger     for (i = 0; i < bprm->argc; ++i) {
73982a39595SMike Frysinger         /* the argv strings */
74082a39595SMike Frysinger         stack_len += strlen(bprm->argv[i]);
74182a39595SMike Frysinger     }
74282a39595SMike Frysinger     for (i = 0; i < bprm->envc; ++i) {
74382a39595SMike Frysinger         /* the envp strings */
74482a39595SMike Frysinger         stack_len += strlen(bprm->envp[i]);
74582a39595SMike Frysinger     }
746e5fe0c52Spbrook     stack_len += (bprm->argc + 1) * 4; /* the argv array */
747e5fe0c52Spbrook     stack_len += (bprm->envc + 1) * 4; /* the envp array */
748e5fe0c52Spbrook 
749e5fe0c52Spbrook 
750e5fe0c52Spbrook     res = load_flat_file(bprm, libinfo, 0, &stack_len);
75171987b12SPhilippe Mathieu-Daudé     if (is_error(res)) {
752e5fe0c52Spbrook             return res;
75371987b12SPhilippe Mathieu-Daudé     }
754e5fe0c52Spbrook 
755e5fe0c52Spbrook     /* Update data segment pointers for all libraries */
756e5fe0c52Spbrook     for (i=0; i<MAX_SHARED_LIBS; i++) {
757e5fe0c52Spbrook         if (libinfo[i].loaded) {
758d5308ea6SLaurent Vivier             abi_ulong seg;
759d5308ea6SLaurent Vivier             seg = libinfo[i].start_data;
760e5fe0c52Spbrook             for (j=0; j<MAX_SHARED_LIBS; j++) {
761d5308ea6SLaurent Vivier                 seg -= 4;
7622f619698Sbellard                 /* FIXME - handle put_user() failures */
7632f619698Sbellard                 if (put_user_ual(libinfo[j].loaded
764e5fe0c52Spbrook                                  ? libinfo[j].start_data
7652f619698Sbellard                                  : UNLOADED_LIB,
766d5308ea6SLaurent Vivier                                  seg))
7672f619698Sbellard                     return -EFAULT;
768e5fe0c52Spbrook             }
769e5fe0c52Spbrook         }
770e5fe0c52Spbrook     }
771e5fe0c52Spbrook 
772e5fe0c52Spbrook     p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4;
773e5fe0c52Spbrook     DBG_FLT("p=%x\n", (int)p);
774e5fe0c52Spbrook 
775e5fe0c52Spbrook     /* Copy argv/envp.  */
776e5fe0c52Spbrook     p = copy_strings(p, bprm->envc, bprm->envp);
777388c4508Spbrook     p = copy_strings(p, bprm->argc, bprm->argv);
778e5fe0c52Spbrook     /* Align stack.  */
779992f48a0Sblueswir1     sp = p & ~(abi_ulong)(sizeof(abi_ulong) - 1);
780b35d7448Spbrook     /* Enforce final stack alignment of 16 bytes.  This is sufficient
781b35d7448Spbrook        for all current targets, and excess alignment is harmless.  */
782b35d7448Spbrook     stack_len = bprm->envc + bprm->argc + 2;
783669dcb60SMichael Tokarev     stack_len += flat_argvp_envp_on_stack() ? 2 : 0; /* argv, argp */
7845c76d652SMax Filippov     stack_len += 1; /* argc */
785992f48a0Sblueswir1     stack_len *= sizeof(abi_ulong);
7865c76d652SMax Filippov     sp -= (sp - stack_len) & 15;
787c3109ba1SMike Frysinger     sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p,
788c3109ba1SMike Frysinger                              flat_argvp_envp_on_stack());
789e5fe0c52Spbrook 
790e5fe0c52Spbrook     /* Fake some return addresses to ensure the call chain will
791e5fe0c52Spbrook      * initialise library in order for us.  We are required to call
792e5fe0c52Spbrook      * lib 1 first, then 2, ... and finally the main program (id 0).
793e5fe0c52Spbrook      */
794e5fe0c52Spbrook     start_addr = libinfo[0].entry;
795e5fe0c52Spbrook 
796e5fe0c52Spbrook #ifdef CONFIG_BINFMT_SHARED_FLAT
797e5fe0c52Spbrook #error here
798e5fe0c52Spbrook     for (i = MAX_SHARED_LIBS-1; i>0; i--) {
799e5fe0c52Spbrook             if (libinfo[i].loaded) {
8006f9ff551Szhaolichang                     /* Push previous first to call address */
8012f619698Sbellard                     --sp;
8022f619698Sbellard                     if (put_user_ual(start_addr, sp))
8032f619698Sbellard                         return -EFAULT;
804e5fe0c52Spbrook                     start_addr = libinfo[i].entry;
805e5fe0c52Spbrook             }
806e5fe0c52Spbrook     }
807e5fe0c52Spbrook #endif
808e5fe0c52Spbrook 
809e5fe0c52Spbrook     /* Stash our initial stack pointer into the mm structure */
810e5fe0c52Spbrook     info->start_code = libinfo[0].start_code;
811734a659aSPeter Maydell     info->end_code = libinfo[0].start_code + libinfo[0].text_len;
812e5fe0c52Spbrook     info->start_data = libinfo[0].start_data;
813e5fe0c52Spbrook     info->end_data = libinfo[0].end_data;
8140662a626SRichard Henderson     info->brk = libinfo[0].start_brk;
815e5fe0c52Spbrook     info->start_stack = sp;
81697374d38SPaul Brook     info->stack_limit = libinfo[0].start_brk;
817e5fe0c52Spbrook     info->entry = start_addr;
818978efd6aSpbrook     info->code_offset = info->start_code;
819978efd6aSpbrook     info->data_offset = info->start_data - libinfo[0].text_len;
820978efd6aSpbrook 
821e5fe0c52Spbrook     DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
822e5fe0c52Spbrook             (int)info->entry, (int)info->start_stack);
823e5fe0c52Spbrook 
824e5fe0c52Spbrook     return 0;
825e5fe0c52Spbrook }
826