2
0

flatload.c 25 KB

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