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