2
0

flatload.c 25 KB

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