2
0

flatload.c 26 KB

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