2
0

flatload.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  1. /****************************************************************************/
  2. /*
  3. * QEMU bFLT binary loader. Based on linux/fs/binfmt_flat.c
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * Copyright (C) 2006 CodeSourcery.
  19. * Copyright (C) 2000-2003 David McCullough <davidm@snapgear.com>
  20. * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
  21. * Copyright (C) 2002 SnapGear, by Paul Dale <pauli@snapgear.com>
  22. * Copyright (C) 2000, 2001 Lineo, by David McCullough <davidm@lineo.com>
  23. * based heavily on:
  24. *
  25. * linux/fs/binfmt_aout.c:
  26. * Copyright (C) 1991, 1992, 1996 Linus Torvalds
  27. * linux/fs/binfmt_flat.c for 2.0 kernel
  28. * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
  29. * JAN/99 -- coded full program relocation (gerg@snapgear.com)
  30. */
  31. /****************************************************************************/
  32. #include "qemu/osdep.h"
  33. #include "qemu.h"
  34. #include "exec/page-protection.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. 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. static abi_ulong
  94. calc_reloc(abi_ulong r, struct lib_info *p, int curid, int internalp)
  95. {
  96. abi_ulong addr;
  97. int id;
  98. abi_ulong start_brk;
  99. abi_ulong start_data;
  100. abi_ulong text_len;
  101. abi_ulong start_code;
  102. id = 0;
  103. start_brk = p[id].start_brk;
  104. start_data = p[id].start_data;
  105. start_code = p[id].start_code;
  106. text_len = p[id].text_len;
  107. if (!flat_reloc_valid(r, start_brk - start_data + text_len)) {
  108. fprintf(stderr, "BINFMT_FLAT: reloc outside program 0x%x "
  109. "(0 - 0x%x/0x%x)\n",
  110. (int) r,(int)(start_brk-start_code),(int)text_len);
  111. goto failed;
  112. }
  113. if (r < text_len) /* In text segment */
  114. addr = r + start_code;
  115. else /* In data segment */
  116. addr = r - text_len + start_data;
  117. /* Range checked already above so doing the range tests is redundant...*/
  118. return(addr);
  119. failed:
  120. abort();
  121. return RELOC_FAILED;
  122. }
  123. /****************************************************************************/
  124. /* ??? This does not handle endianness correctly. */
  125. static void old_reloc(struct lib_info *libinfo, uint32_t rl)
  126. {
  127. #ifdef DEBUG
  128. const char *segment[] = { "TEXT", "DATA", "BSS", "*UNKNOWN*" };
  129. #endif
  130. uint32_t *ptr;
  131. uint32_t offset;
  132. int reloc_type;
  133. offset = rl & 0x3fffffff;
  134. reloc_type = rl >> 30;
  135. /* ??? How to handle this? */
  136. #if defined(CONFIG_COLDFIRE)
  137. ptr = (uint32_t *) ((unsigned long) libinfo->start_code + offset);
  138. #else
  139. ptr = (uint32_t *) ((unsigned long) libinfo->start_data + offset);
  140. #endif
  141. #ifdef DEBUG
  142. fprintf(stderr, "Relocation of variable at DATASEG+%x "
  143. "(address %p, currently %x) into segment %s\n",
  144. offset, ptr, (int)*ptr, segment[reloc_type]);
  145. #endif
  146. switch (reloc_type) {
  147. case OLD_FLAT_RELOC_TYPE_TEXT:
  148. *ptr += libinfo->start_code;
  149. break;
  150. case OLD_FLAT_RELOC_TYPE_DATA:
  151. *ptr += libinfo->start_data;
  152. break;
  153. case OLD_FLAT_RELOC_TYPE_BSS:
  154. *ptr += libinfo->end_data;
  155. break;
  156. default:
  157. fprintf(stderr, "BINFMT_FLAT: Unknown relocation type=%x\n",
  158. reloc_type);
  159. break;
  160. }
  161. DBG_FLT("Relocation became %x\n", (int)*ptr);
  162. }
  163. /****************************************************************************/
  164. static int load_flat_file(struct linux_binprm * bprm,
  165. struct lib_info *libinfo, int id, abi_ulong *extra_stack)
  166. {
  167. struct flat_hdr * hdr;
  168. abi_ulong textpos = 0, datapos = 0;
  169. abi_long result;
  170. abi_ulong realdatastart = 0;
  171. abi_ulong text_len, data_len, bss_len, stack_len, flags;
  172. abi_ulong extra;
  173. abi_ulong reloc = 0, rp;
  174. int i, rev, relocs = 0;
  175. abi_ulong fpos;
  176. abi_ulong start_code;
  177. abi_ulong indx_len;
  178. hdr = ((struct flat_hdr *) bprm->buf); /* exec-header */
  179. text_len = ntohl(hdr->data_start);
  180. data_len = ntohl(hdr->data_end) - ntohl(hdr->data_start);
  181. bss_len = ntohl(hdr->bss_end) - ntohl(hdr->data_end);
  182. stack_len = ntohl(hdr->stack_size);
  183. if (extra_stack) {
  184. stack_len += *extra_stack;
  185. *extra_stack = stack_len;
  186. }
  187. relocs = ntohl(hdr->reloc_count);
  188. flags = ntohl(hdr->flags);
  189. rev = ntohl(hdr->rev);
  190. DBG_FLT("BINFMT_FLAT: Loading file: %s\n", bprm->filename);
  191. if (rev != FLAT_VERSION && rev != OLD_FLAT_VERSION) {
  192. fprintf(stderr, "BINFMT_FLAT: bad magic/rev (0x%x, need 0x%x)\n",
  193. rev, (int) FLAT_VERSION);
  194. return -ENOEXEC;
  195. }
  196. /* Don't allow old format executables to use shared libraries */
  197. if (rev == OLD_FLAT_VERSION && id != 0) {
  198. fprintf(stderr, "BINFMT_FLAT: shared libraries are not available\n");
  199. return -ENOEXEC;
  200. }
  201. /*
  202. * fix up the flags for the older format, there were all kinds
  203. * of endian hacks, this only works for the simple cases
  204. */
  205. if (rev == OLD_FLAT_VERSION && flat_old_ram_flag(flags))
  206. flags = FLAT_FLAG_RAM;
  207. if (flags & (FLAT_FLAG_GZIP|FLAT_FLAG_GZDATA)) {
  208. fprintf(stderr, "ZFLAT executables are not supported\n");
  209. return -ENOEXEC;
  210. }
  211. /*
  212. * calculate the extra space we need to map in
  213. */
  214. extra = relocs * sizeof(abi_ulong);
  215. if (extra < bss_len + stack_len)
  216. extra = bss_len + stack_len;
  217. /* Add space for library base pointers. Make sure this does not
  218. misalign the doesn't misalign the data segment. */
  219. indx_len = MAX_SHARED_LIBS * sizeof(abi_ulong);
  220. indx_len = (indx_len + 15) & ~(abi_ulong)15;
  221. /*
  222. * Allocate the address space.
  223. */
  224. probe_guest_base(bprm->filename, 0,
  225. text_len + data_len + extra + indx_len - 1);
  226. /*
  227. * there are a couple of cases here, the separate code/data
  228. * case, and then the fully copied to RAM case which lumps
  229. * it all together.
  230. */
  231. if ((flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP)) == 0) {
  232. /*
  233. * this should give us a ROM ptr, but if it doesn't we don't
  234. * really care
  235. */
  236. DBG_FLT("BINFMT_FLAT: ROM mapping of file (we hope)\n");
  237. textpos = target_mmap(0, text_len, PROT_READ|PROT_EXEC,
  238. MAP_PRIVATE, bprm->src.fd, 0);
  239. if (textpos == -1) {
  240. fprintf(stderr, "Unable to mmap process text\n");
  241. return -1;
  242. }
  243. realdatastart = target_mmap(0, data_len + extra + indx_len,
  244. PROT_READ|PROT_WRITE|PROT_EXEC,
  245. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  246. if (realdatastart == -1) {
  247. fprintf(stderr, "Unable to allocate RAM for process data\n");
  248. return realdatastart;
  249. }
  250. datapos = realdatastart + indx_len;
  251. DBG_FLT("BINFMT_FLAT: Allocated data+bss+stack (%d bytes): %x\n",
  252. (int)(data_len + bss_len + stack_len), (int)datapos);
  253. fpos = ntohl(hdr->data_start);
  254. result = target_pread(bprm->src.fd, datapos,
  255. data_len + (relocs * sizeof(abi_ulong)),
  256. fpos);
  257. if (result < 0) {
  258. fprintf(stderr, "Unable to read data+bss\n");
  259. return result;
  260. }
  261. reloc = datapos + (ntohl(hdr->reloc_start) - text_len);
  262. } else {
  263. textpos = target_mmap(0, text_len + data_len + extra + indx_len,
  264. PROT_READ | PROT_EXEC | PROT_WRITE,
  265. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  266. if (textpos == -1 ) {
  267. fprintf(stderr, "Unable to allocate RAM for process text/data\n");
  268. return -1;
  269. }
  270. realdatastart = textpos + ntohl(hdr->data_start);
  271. datapos = realdatastart + indx_len;
  272. reloc = (textpos + ntohl(hdr->reloc_start) + indx_len);
  273. result = target_pread(bprm->src.fd, textpos,
  274. text_len, 0);
  275. if (result >= 0) {
  276. result = target_pread(bprm->src.fd, datapos,
  277. data_len + (relocs * sizeof(abi_ulong)),
  278. ntohl(hdr->data_start));
  279. }
  280. if (result < 0) {
  281. fprintf(stderr, "Unable to read code+data+bss\n");
  282. return result;
  283. }
  284. }
  285. DBG_FLT("Mapping is 0x%x, Entry point is 0x%x, data_start is 0x%x\n",
  286. (int)textpos, 0x00ffffff&ntohl(hdr->entry),
  287. ntohl(hdr->data_start));
  288. /* The main program needs a little extra setup in the task structure */
  289. start_code = textpos + sizeof (struct flat_hdr);
  290. DBG_FLT("%s %s: TEXT=%x-%x DATA=%x-%x BSS=%x-%x\n",
  291. id ? "Lib" : "Load", bprm->filename,
  292. (int) start_code, (int) (textpos + text_len),
  293. (int) datapos,
  294. (int) (datapos + data_len),
  295. (int) (datapos + data_len),
  296. (int) (((datapos + data_len + bss_len) + 3) & ~3));
  297. text_len -= sizeof(struct flat_hdr); /* the real code len */
  298. /* Store the current module values into the global library structure */
  299. libinfo[id].start_code = start_code;
  300. libinfo[id].start_data = datapos;
  301. libinfo[id].end_data = datapos + data_len;
  302. libinfo[id].start_brk = datapos + data_len + bss_len;
  303. libinfo[id].text_len = text_len;
  304. libinfo[id].loaded = 1;
  305. libinfo[id].entry = (0x00ffffff & ntohl(hdr->entry)) + textpos;
  306. libinfo[id].build_date = ntohl(hdr->build_date);
  307. /*
  308. * We just load the allocations into some temporary memory to
  309. * help simplify all this mumbo jumbo
  310. *
  311. * We've got two different sections of relocation entries.
  312. * The first is the GOT which resides at the beginning of the data segment
  313. * and is terminated with a -1. This one can be relocated in place.
  314. * The second is the extra relocation entries tacked after the image's
  315. * data segment. These require a little more processing as the entry is
  316. * really an offset into the image which contains an offset into the
  317. * image.
  318. */
  319. if (flags & FLAT_FLAG_GOTPIC) {
  320. rp = datapos;
  321. while (1) {
  322. abi_ulong addr;
  323. if (get_user_ual(addr, rp))
  324. return -EFAULT;
  325. if (addr == -1)
  326. break;
  327. if (addr) {
  328. addr = calc_reloc(addr, libinfo, id, 0);
  329. if (addr == RELOC_FAILED)
  330. return -ENOEXEC;
  331. if (put_user_ual(addr, rp))
  332. return -EFAULT;
  333. }
  334. rp += sizeof(abi_ulong);
  335. }
  336. }
  337. /*
  338. * Now run through the relocation entries.
  339. * We've got to be careful here as C++ produces relocatable zero
  340. * entries in the constructor and destructor tables which are then
  341. * tested for being not zero (which will always occur unless we're
  342. * based from address zero). This causes an endless loop as __start
  343. * is at zero. The solution used is to not relocate zero addresses.
  344. * This has the negative side effect of not allowing a global data
  345. * reference to be statically initialised to _stext (I've moved
  346. * __start to address 4 so that is okay).
  347. */
  348. if (rev > OLD_FLAT_VERSION) {
  349. abi_ulong persistent = 0;
  350. for (i = 0; i < relocs; i++) {
  351. abi_ulong addr, relval;
  352. /* Get the address of the pointer to be
  353. relocated (of course, the address has to be
  354. relocated first). */
  355. if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
  356. return -EFAULT;
  357. relval = ntohl(relval);
  358. if (flat_set_persistent(relval, &persistent))
  359. continue;
  360. addr = flat_get_relocate_addr(relval);
  361. rp = calc_reloc(addr, libinfo, id, 1);
  362. if (rp == RELOC_FAILED)
  363. return -ENOEXEC;
  364. /* Get the pointer's value. */
  365. if (get_user_ual(addr, rp))
  366. return -EFAULT;
  367. addr = flat_get_addr_from_rp(addr, relval, flags, &persistent);
  368. if (addr != 0) {
  369. /*
  370. * Do the relocation. PIC relocs in the data section are
  371. * already in target order
  372. */
  373. if ((flags & FLAT_FLAG_GOTPIC) == 0)
  374. addr = ntohl(addr);
  375. addr = calc_reloc(addr, libinfo, id, 0);
  376. if (addr == RELOC_FAILED)
  377. return -ENOEXEC;
  378. /* Write back the relocated pointer. */
  379. if (flat_put_addr_at_rp(rp, addr, relval))
  380. return -EFAULT;
  381. }
  382. }
  383. } else {
  384. for (i = 0; i < relocs; i++) {
  385. abi_ulong relval;
  386. if (get_user_ual(relval, reloc + i * sizeof(abi_ulong)))
  387. return -EFAULT;
  388. old_reloc(&libinfo[0], relval);
  389. }
  390. }
  391. /* zero the BSS. */
  392. memset(g2h_untagged(datapos + data_len), 0, bss_len);
  393. return 0;
  394. }
  395. /****************************************************************************/
  396. int load_flt_binary(struct linux_binprm *bprm, struct image_info *info)
  397. {
  398. struct lib_info libinfo[MAX_SHARED_LIBS];
  399. abi_ulong p;
  400. abi_ulong stack_len;
  401. abi_ulong start_addr;
  402. abi_ulong sp;
  403. int res;
  404. int i, j;
  405. memset(libinfo, 0, sizeof(libinfo));
  406. /*
  407. * We have to add the size of our arguments to our stack size
  408. * otherwise it's too easy for users to create stack overflows
  409. * by passing in a huge argument list. And yes, we have to be
  410. * pedantic and include space for the argv/envp array as it may have
  411. * a lot of entries.
  412. */
  413. stack_len = 0;
  414. for (i = 0; i < bprm->argc; ++i) {
  415. /* the argv strings */
  416. stack_len += strlen(bprm->argv[i]);
  417. }
  418. for (i = 0; i < bprm->envc; ++i) {
  419. /* the envp strings */
  420. stack_len += strlen(bprm->envp[i]);
  421. }
  422. stack_len += (bprm->argc + 1) * 4; /* the argv array */
  423. stack_len += (bprm->envc + 1) * 4; /* the envp array */
  424. mmap_lock();
  425. res = load_flat_file(bprm, libinfo, 0, &stack_len);
  426. mmap_unlock();
  427. if (is_error(res)) {
  428. return res;
  429. }
  430. /* Update data segment pointers for all libraries */
  431. for (i=0; i<MAX_SHARED_LIBS; i++) {
  432. if (libinfo[i].loaded) {
  433. abi_ulong seg;
  434. seg = libinfo[i].start_data;
  435. for (j=0; j<MAX_SHARED_LIBS; j++) {
  436. seg -= 4;
  437. /* FIXME - handle put_user() failures */
  438. if (put_user_ual(libinfo[j].loaded
  439. ? libinfo[j].start_data
  440. : UNLOADED_LIB,
  441. seg))
  442. return -EFAULT;
  443. }
  444. }
  445. }
  446. p = ((libinfo[0].start_brk + stack_len + 3) & ~3) - 4;
  447. DBG_FLT("p=%x\n", (int)p);
  448. /* Copy argv/envp. */
  449. p = copy_strings(p, bprm->envc, bprm->envp);
  450. p = copy_strings(p, bprm->argc, bprm->argv);
  451. /* Align stack. */
  452. sp = p & ~(abi_ulong)(sizeof(abi_ulong) - 1);
  453. /* Enforce final stack alignment of 16 bytes. This is sufficient
  454. for all current targets, and excess alignment is harmless. */
  455. stack_len = bprm->envc + bprm->argc + 2;
  456. stack_len += flat_argvp_envp_on_stack() ? 2 : 0; /* argv, argp */
  457. stack_len += 1; /* argc */
  458. stack_len *= sizeof(abi_ulong);
  459. sp -= (sp - stack_len) & 15;
  460. sp = loader_build_argptr(bprm->envc, bprm->argc, sp, p,
  461. flat_argvp_envp_on_stack());
  462. /* Fake some return addresses to ensure the call chain will
  463. * initialise library in order for us. We are required to call
  464. * lib 1 first, then 2, ... and finally the main program (id 0).
  465. */
  466. start_addr = libinfo[0].entry;
  467. /* Stash our initial stack pointer into the mm structure */
  468. info->start_code = libinfo[0].start_code;
  469. info->end_code = libinfo[0].start_code + libinfo[0].text_len;
  470. info->start_data = libinfo[0].start_data;
  471. info->end_data = libinfo[0].end_data;
  472. info->brk = libinfo[0].start_brk;
  473. info->start_stack = sp;
  474. info->stack_limit = libinfo[0].start_brk;
  475. info->entry = start_addr;
  476. info->code_offset = info->start_code;
  477. info->data_offset = info->start_data - libinfo[0].text_len;
  478. DBG_FLT("start_thread(entry=0x%x, start_stack=0x%x)\n",
  479. (int)info->entry, (int)info->start_stack);
  480. return 0;
  481. }