2
0

flatload.c 25 KB

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