elfload.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. * ELF loading code
  3. *
  4. * Copyright (c) 2013 Stacey D. Son
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "qemu/osdep.h"
  20. #include "qemu.h"
  21. #include "disas/disas.h"
  22. #include "qemu/path.h"
  23. static abi_ulong target_auxents; /* Where the AUX entries are in target */
  24. static size_t target_auxents_sz; /* Size of AUX entries including AT_NULL */
  25. #include "target_arch_reg.h"
  26. #include "target_os_elf.h"
  27. #include "target_os_stack.h"
  28. #include "target_os_thread.h"
  29. #include "target_os_user.h"
  30. abi_ulong target_stksiz;
  31. abi_ulong target_stkbas;
  32. static int elf_core_dump(int signr, CPUArchState *env);
  33. static int load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr,
  34. int fd, abi_ulong rbase, abi_ulong *baddrp);
  35. static inline void memcpy_fromfs(void *to, const void *from, unsigned long n)
  36. {
  37. memcpy(to, from, n);
  38. }
  39. #if HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN
  40. static void bswap_ehdr(struct elfhdr *ehdr)
  41. {
  42. bswap16s(&ehdr->e_type); /* Object file type */
  43. bswap16s(&ehdr->e_machine); /* Architecture */
  44. bswap32s(&ehdr->e_version); /* Object file version */
  45. bswaptls(&ehdr->e_entry); /* Entry point virtual address */
  46. bswaptls(&ehdr->e_phoff); /* Program header table file offset */
  47. bswaptls(&ehdr->e_shoff); /* Section header table file offset */
  48. bswap32s(&ehdr->e_flags); /* Processor-specific flags */
  49. bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
  50. bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
  51. bswap16s(&ehdr->e_phnum); /* Program header table entry count */
  52. bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
  53. bswap16s(&ehdr->e_shnum); /* Section header table entry count */
  54. bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
  55. }
  56. static void bswap_phdr(struct elf_phdr *phdr, int phnum)
  57. {
  58. int i;
  59. for (i = 0; i < phnum; i++, phdr++) {
  60. bswap32s(&phdr->p_type); /* Segment type */
  61. bswap32s(&phdr->p_flags); /* Segment flags */
  62. bswaptls(&phdr->p_offset); /* Segment file offset */
  63. bswaptls(&phdr->p_vaddr); /* Segment virtual address */
  64. bswaptls(&phdr->p_paddr); /* Segment physical address */
  65. bswaptls(&phdr->p_filesz); /* Segment size in file */
  66. bswaptls(&phdr->p_memsz); /* Segment size in memory */
  67. bswaptls(&phdr->p_align); /* Segment alignment */
  68. }
  69. }
  70. static void bswap_shdr(struct elf_shdr *shdr, int shnum)
  71. {
  72. int i;
  73. for (i = 0; i < shnum; i++, shdr++) {
  74. bswap32s(&shdr->sh_name);
  75. bswap32s(&shdr->sh_type);
  76. bswaptls(&shdr->sh_flags);
  77. bswaptls(&shdr->sh_addr);
  78. bswaptls(&shdr->sh_offset);
  79. bswaptls(&shdr->sh_size);
  80. bswap32s(&shdr->sh_link);
  81. bswap32s(&shdr->sh_info);
  82. bswaptls(&shdr->sh_addralign);
  83. bswaptls(&shdr->sh_entsize);
  84. }
  85. }
  86. static void bswap_sym(struct elf_sym *sym)
  87. {
  88. bswap32s(&sym->st_name);
  89. bswaptls(&sym->st_value);
  90. bswaptls(&sym->st_size);
  91. bswap16s(&sym->st_shndx);
  92. }
  93. static void bswap_note(struct elf_note *en)
  94. {
  95. bswap32s(&en->n_namesz);
  96. bswap32s(&en->n_descsz);
  97. bswap32s(&en->n_type);
  98. }
  99. #else
  100. static void bswap_ehdr(struct elfhdr *ehdr) { }
  101. static void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
  102. static void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
  103. static void bswap_sym(struct elf_sym *sym) { }
  104. static void bswap_note(struct elf_note *en) { }
  105. #endif /* HOST_BIG_ENDIAN != TARGET_BIG_ENDIAN */
  106. #include "elfcore.c"
  107. /*
  108. * 'copy_elf_strings()' copies argument/envelope strings from user
  109. * memory to free pages in kernel mem. These are in a format ready
  110. * to be put directly into the top of new user memory.
  111. *
  112. */
  113. static abi_ulong copy_elf_strings(int argc, char **argv, void **page,
  114. abi_ulong p)
  115. {
  116. char *tmp, *tmp1, *pag = NULL;
  117. int len, offset = 0;
  118. if (!p) {
  119. return 0; /* bullet-proofing */
  120. }
  121. while (argc-- > 0) {
  122. tmp = argv[argc];
  123. if (!tmp) {
  124. fprintf(stderr, "VFS: argc is wrong");
  125. exit(-1);
  126. }
  127. tmp1 = tmp;
  128. while (*tmp++) {
  129. continue;
  130. }
  131. len = tmp - tmp1;
  132. if (p < len) { /* this shouldn't happen - 128kB */
  133. return 0;
  134. }
  135. while (len) {
  136. --p; --tmp; --len;
  137. if (--offset < 0) {
  138. offset = p % TARGET_PAGE_SIZE;
  139. pag = page[p / TARGET_PAGE_SIZE];
  140. if (!pag) {
  141. pag = g_try_malloc0(TARGET_PAGE_SIZE);
  142. page[p / TARGET_PAGE_SIZE] = pag;
  143. if (!pag) {
  144. return 0;
  145. }
  146. }
  147. }
  148. if (len == 0 || offset == 0) {
  149. *(pag + offset) = *tmp;
  150. } else {
  151. int bytes_to_copy = (len > offset) ? offset : len;
  152. tmp -= bytes_to_copy;
  153. p -= bytes_to_copy;
  154. offset -= bytes_to_copy;
  155. len -= bytes_to_copy;
  156. memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
  157. }
  158. }
  159. }
  160. return p;
  161. }
  162. static void setup_arg_pages(struct bsd_binprm *bprm, struct image_info *info,
  163. abi_ulong *stackp, abi_ulong *stringp)
  164. {
  165. abi_ulong stack_base, size;
  166. abi_long addr;
  167. /*
  168. * Create enough stack to hold everything. If we don't use it for args,
  169. * we'll use it for something else...
  170. */
  171. size = target_dflssiz;
  172. stack_base = TARGET_USRSTACK - size;
  173. addr = target_mmap(stack_base , size + qemu_host_page_size,
  174. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  175. if (addr == -1) {
  176. perror("stk mmap");
  177. exit(-1);
  178. }
  179. /* we reserve one extra page at the top of the stack as guard */
  180. target_mprotect(addr + size, qemu_host_page_size, PROT_NONE);
  181. target_stksiz = size;
  182. target_stkbas = addr;
  183. if (setup_initial_stack(bprm, stackp, stringp) != 0) {
  184. perror("stk setup");
  185. exit(-1);
  186. }
  187. }
  188. static void set_brk(abi_ulong start, abi_ulong end)
  189. {
  190. /* page-align the start and end addresses... */
  191. start = HOST_PAGE_ALIGN(start);
  192. end = HOST_PAGE_ALIGN(end);
  193. if (end <= start) {
  194. return;
  195. }
  196. if (target_mmap(start, end - start, PROT_READ | PROT_WRITE | PROT_EXEC,
  197. MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0) == -1) {
  198. perror("cannot mmap brk");
  199. exit(-1);
  200. }
  201. }
  202. /*
  203. * We need to explicitly zero any fractional pages after the data
  204. * section (i.e. bss). This would contain the junk from the file that
  205. * should not be in memory.
  206. */
  207. static void padzero(abi_ulong elf_bss, abi_ulong last_bss)
  208. {
  209. abi_ulong nbyte;
  210. if (elf_bss >= last_bss) {
  211. return;
  212. }
  213. /*
  214. * XXX: this is really a hack : if the real host page size is
  215. * smaller than the target page size, some pages after the end
  216. * of the file may not be mapped. A better fix would be to
  217. * patch target_mmap(), but it is more complicated as the file
  218. * size must be known.
  219. */
  220. if (qemu_real_host_page_size() < qemu_host_page_size) {
  221. abi_ulong end_addr, end_addr1;
  222. end_addr1 = REAL_HOST_PAGE_ALIGN(elf_bss);
  223. end_addr = HOST_PAGE_ALIGN(elf_bss);
  224. if (end_addr1 < end_addr) {
  225. mmap((void *)g2h_untagged(end_addr1), end_addr - end_addr1,
  226. PROT_READ | PROT_WRITE | PROT_EXEC,
  227. MAP_FIXED | MAP_PRIVATE | MAP_ANON, -1, 0);
  228. }
  229. }
  230. nbyte = elf_bss & (qemu_host_page_size - 1);
  231. if (nbyte) {
  232. nbyte = qemu_host_page_size - nbyte;
  233. do {
  234. /* FIXME - what to do if put_user() fails? */
  235. put_user_u8(0, elf_bss);
  236. elf_bss++;
  237. } while (--nbyte);
  238. }
  239. }
  240. static abi_ulong load_elf_interp(struct elfhdr *interp_elf_ex,
  241. int interpreter_fd,
  242. abi_ulong *interp_load_addr)
  243. {
  244. struct elf_phdr *elf_phdata = NULL;
  245. abi_ulong rbase;
  246. int retval;
  247. abi_ulong baddr, error;
  248. error = 0;
  249. bswap_ehdr(interp_elf_ex);
  250. /* First of all, some simple consistency checks */
  251. if ((interp_elf_ex->e_type != ET_EXEC && interp_elf_ex->e_type != ET_DYN) ||
  252. !elf_check_arch(interp_elf_ex->e_machine)) {
  253. return ~((abi_ulong)0UL);
  254. }
  255. /* Now read in all of the header information */
  256. if (sizeof(struct elf_phdr) * interp_elf_ex->e_phnum > TARGET_PAGE_SIZE) {
  257. return ~(abi_ulong)0UL;
  258. }
  259. elf_phdata = (struct elf_phdr *) malloc(sizeof(struct elf_phdr) *
  260. interp_elf_ex->e_phnum);
  261. if (!elf_phdata) {
  262. return ~((abi_ulong)0UL);
  263. }
  264. /*
  265. * If the size of this structure has changed, then punt, since
  266. * we will be doing the wrong thing.
  267. */
  268. if (interp_elf_ex->e_phentsize != sizeof(struct elf_phdr)) {
  269. free(elf_phdata);
  270. return ~((abi_ulong)0UL);
  271. }
  272. retval = lseek(interpreter_fd, interp_elf_ex->e_phoff, SEEK_SET);
  273. if (retval >= 0) {
  274. retval = read(interpreter_fd, (char *) elf_phdata,
  275. sizeof(struct elf_phdr) * interp_elf_ex->e_phnum);
  276. }
  277. if (retval < 0) {
  278. perror("load_elf_interp");
  279. exit(-1);
  280. free(elf_phdata);
  281. return retval;
  282. }
  283. bswap_phdr(elf_phdata, interp_elf_ex->e_phnum);
  284. rbase = 0;
  285. if (interp_elf_ex->e_type == ET_DYN) {
  286. /*
  287. * In order to avoid hardcoding the interpreter load
  288. * address in qemu, we allocate a big enough memory zone.
  289. */
  290. rbase = target_mmap(0, INTERP_MAP_SIZE, PROT_NONE,
  291. MAP_PRIVATE | MAP_ANON, -1, 0);
  292. if (rbase == -1) {
  293. perror("mmap");
  294. exit(-1);
  295. }
  296. }
  297. error = load_elf_sections(interp_elf_ex, elf_phdata, interpreter_fd, rbase,
  298. &baddr);
  299. if (error != 0) {
  300. perror("load_elf_sections");
  301. exit(-1);
  302. }
  303. /* Now use mmap to map the library into memory. */
  304. close(interpreter_fd);
  305. free(elf_phdata);
  306. *interp_load_addr = baddr;
  307. return ((abi_ulong) interp_elf_ex->e_entry) + rbase;
  308. }
  309. static int symfind(const void *s0, const void *s1)
  310. {
  311. struct elf_sym *sym = (struct elf_sym *)s1;
  312. __typeof(sym->st_value) addr = *(uint64_t *)s0;
  313. int result = 0;
  314. if (addr < sym->st_value) {
  315. result = -1;
  316. } else if (addr >= sym->st_value + sym->st_size) {
  317. result = 1;
  318. }
  319. return result;
  320. }
  321. static const char *lookup_symbolxx(struct syminfo *s, uint64_t orig_addr)
  322. {
  323. #if ELF_CLASS == ELFCLASS32
  324. struct elf_sym *syms = s->disas_symtab.elf32;
  325. #else
  326. struct elf_sym *syms = s->disas_symtab.elf64;
  327. #endif
  328. /* binary search */
  329. struct elf_sym *sym;
  330. sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
  331. if (sym != NULL) {
  332. return s->disas_strtab + sym->st_name;
  333. }
  334. return "";
  335. }
  336. /* FIXME: This should use elf_ops.h.inc */
  337. static int symcmp(const void *s0, const void *s1)
  338. {
  339. struct elf_sym *sym0 = (struct elf_sym *)s0;
  340. struct elf_sym *sym1 = (struct elf_sym *)s1;
  341. return (sym0->st_value < sym1->st_value) ? -1 :
  342. ((sym0->st_value > sym1->st_value) ? 1 : 0);
  343. }
  344. /* Best attempt to load symbols from this ELF object. */
  345. static void load_symbols(struct elfhdr *hdr, int fd)
  346. {
  347. unsigned int i, nsyms;
  348. struct elf_shdr sechdr, symtab, strtab;
  349. char *strings;
  350. struct syminfo *s;
  351. struct elf_sym *syms, *new_syms;
  352. lseek(fd, hdr->e_shoff, SEEK_SET);
  353. for (i = 0; i < hdr->e_shnum; i++) {
  354. if (read(fd, &sechdr, sizeof(sechdr)) != sizeof(sechdr)) {
  355. return;
  356. }
  357. bswap_shdr(&sechdr, 1);
  358. if (sechdr.sh_type == SHT_SYMTAB) {
  359. symtab = sechdr;
  360. lseek(fd, hdr->e_shoff + sizeof(sechdr) * sechdr.sh_link,
  361. SEEK_SET);
  362. if (read(fd, &strtab, sizeof(strtab)) != sizeof(strtab)) {
  363. return;
  364. }
  365. bswap_shdr(&strtab, 1);
  366. goto found;
  367. }
  368. }
  369. return; /* Shouldn't happen... */
  370. found:
  371. /* Now know where the strtab and symtab are. Snarf them. */
  372. s = malloc(sizeof(*s));
  373. syms = malloc(symtab.sh_size);
  374. if (!syms) {
  375. free(s);
  376. return;
  377. }
  378. s->disas_strtab = strings = malloc(strtab.sh_size);
  379. if (!s->disas_strtab) {
  380. free(s);
  381. free(syms);
  382. return;
  383. }
  384. lseek(fd, symtab.sh_offset, SEEK_SET);
  385. if (read(fd, syms, symtab.sh_size) != symtab.sh_size) {
  386. free(s);
  387. free(syms);
  388. free(strings);
  389. return;
  390. }
  391. nsyms = symtab.sh_size / sizeof(struct elf_sym);
  392. i = 0;
  393. while (i < nsyms) {
  394. bswap_sym(syms + i);
  395. /* Throw away entries which we do not need. */
  396. if (syms[i].st_shndx == SHN_UNDEF ||
  397. syms[i].st_shndx >= SHN_LORESERVE ||
  398. ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
  399. nsyms--;
  400. if (i < nsyms) {
  401. syms[i] = syms[nsyms];
  402. }
  403. continue;
  404. }
  405. #if defined(TARGET_ARM) || defined(TARGET_MIPS)
  406. /* The bottom address bit marks a Thumb or MIPS16 symbol. */
  407. syms[i].st_value &= ~(target_ulong)1;
  408. #endif
  409. i++;
  410. }
  411. /*
  412. * Attempt to free the storage associated with the local symbols
  413. * that we threw away. Whether or not this has any effect on the
  414. * memory allocation depends on the malloc implementation and how
  415. * many symbols we managed to discard.
  416. */
  417. new_syms = realloc(syms, nsyms * sizeof(*syms));
  418. if (new_syms == NULL) {
  419. free(s);
  420. free(syms);
  421. free(strings);
  422. return;
  423. }
  424. syms = new_syms;
  425. qsort(syms, nsyms, sizeof(*syms), symcmp);
  426. lseek(fd, strtab.sh_offset, SEEK_SET);
  427. if (read(fd, strings, strtab.sh_size) != strtab.sh_size) {
  428. free(s);
  429. free(syms);
  430. free(strings);
  431. return;
  432. }
  433. s->disas_num_syms = nsyms;
  434. #if ELF_CLASS == ELFCLASS32
  435. s->disas_symtab.elf32 = syms;
  436. s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
  437. #else
  438. s->disas_symtab.elf64 = syms;
  439. s->lookup_symbol = (lookup_symbol_t)lookup_symbolxx;
  440. #endif
  441. s->next = syminfos;
  442. syminfos = s;
  443. }
  444. /* Check the elf header and see if this a target elf binary. */
  445. int is_target_elf_binary(int fd)
  446. {
  447. uint8_t buf[128];
  448. struct elfhdr elf_ex;
  449. if (lseek(fd, 0L, SEEK_SET) < 0) {
  450. return 0;
  451. }
  452. if (read(fd, buf, sizeof(buf)) < 0) {
  453. return 0;
  454. }
  455. elf_ex = *((struct elfhdr *)buf);
  456. bswap_ehdr(&elf_ex);
  457. if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
  458. (!elf_check_arch(elf_ex.e_machine))) {
  459. return 0;
  460. } else {
  461. return 1;
  462. }
  463. }
  464. static int
  465. load_elf_sections(const struct elfhdr *hdr, struct elf_phdr *phdr, int fd,
  466. abi_ulong rbase, abi_ulong *baddrp)
  467. {
  468. struct elf_phdr *elf_ppnt;
  469. abi_ulong baddr;
  470. int i;
  471. bool first;
  472. /*
  473. * Now we do a little grungy work by mmaping the ELF image into
  474. * the correct location in memory. At this point, we assume that
  475. * the image should be loaded at fixed address, not at a variable
  476. * address.
  477. */
  478. first = true;
  479. for (i = 0, elf_ppnt = phdr; i < hdr->e_phnum; i++, elf_ppnt++) {
  480. int elf_prot = 0;
  481. abi_ulong error;
  482. /* XXX Skip memsz == 0. */
  483. if (elf_ppnt->p_type != PT_LOAD) {
  484. continue;
  485. }
  486. if (elf_ppnt->p_flags & PF_R) {
  487. elf_prot |= PROT_READ;
  488. }
  489. if (elf_ppnt->p_flags & PF_W) {
  490. elf_prot |= PROT_WRITE;
  491. }
  492. if (elf_ppnt->p_flags & PF_X) {
  493. elf_prot |= PROT_EXEC;
  494. }
  495. error = target_mmap(TARGET_ELF_PAGESTART(rbase + elf_ppnt->p_vaddr),
  496. (elf_ppnt->p_filesz +
  497. TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)),
  498. elf_prot,
  499. (MAP_FIXED | MAP_PRIVATE | MAP_DENYWRITE),
  500. fd,
  501. (elf_ppnt->p_offset -
  502. TARGET_ELF_PAGEOFFSET(elf_ppnt->p_vaddr)));
  503. if (error == -1) {
  504. perror("mmap");
  505. exit(-1);
  506. } else if (elf_ppnt->p_memsz != elf_ppnt->p_filesz) {
  507. abi_ulong start_bss, end_bss;
  508. start_bss = rbase + elf_ppnt->p_vaddr + elf_ppnt->p_filesz;
  509. end_bss = rbase + elf_ppnt->p_vaddr + elf_ppnt->p_memsz;
  510. /*
  511. * Calling set_brk effectively mmaps the pages that we need for the
  512. * bss and break sections.
  513. */
  514. set_brk(start_bss, end_bss);
  515. padzero(start_bss, end_bss);
  516. }
  517. if (first) {
  518. baddr = TARGET_ELF_PAGESTART(rbase + elf_ppnt->p_vaddr);
  519. first = false;
  520. }
  521. }
  522. if (baddrp != NULL) {
  523. *baddrp = baddr;
  524. }
  525. return 0;
  526. }
  527. int load_elf_binary(struct bsd_binprm *bprm, struct target_pt_regs *regs,
  528. struct image_info *info)
  529. {
  530. struct elfhdr elf_ex;
  531. struct elfhdr interp_elf_ex;
  532. int interpreter_fd = -1; /* avoid warning */
  533. abi_ulong load_addr;
  534. int i;
  535. struct elf_phdr *elf_ppnt;
  536. struct elf_phdr *elf_phdata;
  537. abi_ulong elf_brk;
  538. int error, retval;
  539. char *elf_interpreter;
  540. abi_ulong baddr, elf_entry, et_dyn_addr, interp_load_addr = 0;
  541. abi_ulong reloc_func_desc = 0;
  542. load_addr = 0;
  543. elf_ex = *((struct elfhdr *) bprm->buf); /* exec-header */
  544. bswap_ehdr(&elf_ex);
  545. /* First of all, some simple consistency checks */
  546. if ((elf_ex.e_type != ET_EXEC && elf_ex.e_type != ET_DYN) ||
  547. (!elf_check_arch(elf_ex.e_machine))) {
  548. return -ENOEXEC;
  549. }
  550. bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
  551. bprm->p = copy_elf_strings(bprm->envc, bprm->envp, bprm->page, bprm->p);
  552. bprm->p = copy_elf_strings(bprm->argc, bprm->argv, bprm->page, bprm->p);
  553. if (!bprm->p) {
  554. retval = -E2BIG;
  555. }
  556. /* Now read in all of the header information */
  557. elf_phdata = (struct elf_phdr *)malloc(elf_ex.e_phentsize * elf_ex.e_phnum);
  558. if (elf_phdata == NULL) {
  559. return -ENOMEM;
  560. }
  561. retval = lseek(bprm->fd, elf_ex.e_phoff, SEEK_SET);
  562. if (retval > 0) {
  563. retval = read(bprm->fd, (char *)elf_phdata,
  564. elf_ex.e_phentsize * elf_ex.e_phnum);
  565. }
  566. if (retval < 0) {
  567. perror("load_elf_binary");
  568. exit(-1);
  569. free(elf_phdata);
  570. return -errno;
  571. }
  572. bswap_phdr(elf_phdata, elf_ex.e_phnum);
  573. elf_ppnt = elf_phdata;
  574. elf_brk = 0;
  575. elf_interpreter = NULL;
  576. for (i = 0; i < elf_ex.e_phnum; i++) {
  577. if (elf_ppnt->p_type == PT_INTERP) {
  578. if (elf_interpreter != NULL) {
  579. free(elf_phdata);
  580. free(elf_interpreter);
  581. close(bprm->fd);
  582. return -EINVAL;
  583. }
  584. elf_interpreter = (char *)malloc(elf_ppnt->p_filesz);
  585. if (elf_interpreter == NULL) {
  586. free(elf_phdata);
  587. close(bprm->fd);
  588. return -ENOMEM;
  589. }
  590. retval = lseek(bprm->fd, elf_ppnt->p_offset, SEEK_SET);
  591. if (retval >= 0) {
  592. retval = read(bprm->fd, elf_interpreter, elf_ppnt->p_filesz);
  593. }
  594. if (retval < 0) {
  595. perror("load_elf_binary2");
  596. exit(-1);
  597. }
  598. if (retval >= 0) {
  599. retval = open(path(elf_interpreter), O_RDONLY);
  600. if (retval >= 0) {
  601. interpreter_fd = retval;
  602. } else {
  603. perror(elf_interpreter);
  604. exit(-1);
  605. /* retval = -errno; */
  606. }
  607. }
  608. if (retval >= 0) {
  609. retval = lseek(interpreter_fd, 0, SEEK_SET);
  610. if (retval >= 0) {
  611. retval = read(interpreter_fd, bprm->buf, 128);
  612. }
  613. }
  614. if (retval >= 0) {
  615. interp_elf_ex = *((struct elfhdr *) bprm->buf);
  616. }
  617. if (retval < 0) {
  618. perror("load_elf_binary3");
  619. exit(-1);
  620. free(elf_phdata);
  621. free(elf_interpreter);
  622. close(bprm->fd);
  623. return retval;
  624. }
  625. }
  626. elf_ppnt++;
  627. }
  628. /* Some simple consistency checks for the interpreter */
  629. if (elf_interpreter) {
  630. if (interp_elf_ex.e_ident[0] != 0x7f ||
  631. strncmp((char *)&interp_elf_ex.e_ident[1], "ELF", 3) != 0) {
  632. free(elf_interpreter);
  633. free(elf_phdata);
  634. close(bprm->fd);
  635. return -ELIBBAD;
  636. }
  637. }
  638. /*
  639. * OK, we are done with that, now set up the arg stuff, and then start this
  640. * sucker up
  641. */
  642. if (!bprm->p) {
  643. free(elf_interpreter);
  644. free(elf_phdata);
  645. close(bprm->fd);
  646. return -E2BIG;
  647. }
  648. /* OK, This is the point of no return */
  649. info->end_data = 0;
  650. info->end_code = 0;
  651. elf_entry = (abi_ulong) elf_ex.e_entry;
  652. /* XXX Join this with PT_INTERP search? */
  653. baddr = 0;
  654. for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
  655. if (elf_ppnt->p_type != PT_LOAD) {
  656. continue;
  657. }
  658. baddr = elf_ppnt->p_vaddr;
  659. break;
  660. }
  661. et_dyn_addr = 0;
  662. if (elf_ex.e_type == ET_DYN && baddr == 0) {
  663. et_dyn_addr = ELF_ET_DYN_LOAD_ADDR;
  664. }
  665. /*
  666. * Do this so that we can load the interpreter, if need be. We will
  667. * change some of these later
  668. */
  669. info->rss = 0;
  670. setup_arg_pages(bprm, info, &bprm->p, &bprm->stringp);
  671. info->start_stack = bprm->p;
  672. info->elf_flags = elf_ex.e_flags;
  673. error = load_elf_sections(&elf_ex, elf_phdata, bprm->fd, et_dyn_addr,
  674. &load_addr);
  675. for (i = 0, elf_ppnt = elf_phdata; i < elf_ex.e_phnum; i++, elf_ppnt++) {
  676. if (elf_ppnt->p_type != PT_LOAD) {
  677. continue;
  678. }
  679. if (elf_ppnt->p_memsz > elf_ppnt->p_filesz)
  680. elf_brk = MAX(elf_brk, et_dyn_addr + elf_ppnt->p_vaddr +
  681. elf_ppnt->p_memsz);
  682. }
  683. if (error != 0) {
  684. perror("load_elf_sections");
  685. exit(-1);
  686. }
  687. if (elf_interpreter) {
  688. elf_entry = load_elf_interp(&interp_elf_ex, interpreter_fd,
  689. &interp_load_addr);
  690. reloc_func_desc = interp_load_addr;
  691. close(interpreter_fd);
  692. free(elf_interpreter);
  693. if (elf_entry == ~((abi_ulong)0UL)) {
  694. printf("Unable to load interpreter\n");
  695. free(elf_phdata);
  696. exit(-1);
  697. return 0;
  698. }
  699. } else {
  700. interp_load_addr = et_dyn_addr;
  701. elf_entry += interp_load_addr;
  702. }
  703. free(elf_phdata);
  704. if (qemu_log_enabled()) {
  705. load_symbols(&elf_ex, bprm->fd);
  706. }
  707. close(bprm->fd);
  708. bprm->p = target_create_elf_tables(bprm->p, bprm->argc, bprm->envc,
  709. bprm->stringp, &elf_ex, load_addr,
  710. et_dyn_addr, interp_load_addr, info);
  711. info->load_addr = reloc_func_desc;
  712. info->brk = elf_brk;
  713. info->start_stack = bprm->p;
  714. info->load_bias = 0;
  715. info->entry = elf_entry;
  716. #ifdef USE_ELF_CORE_DUMP
  717. bprm->core_dump = &elf_core_dump;
  718. #else
  719. bprm->core_dump = NULL;
  720. #endif
  721. return 0;
  722. }
  723. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
  724. {
  725. target_thread_init(regs, infop);
  726. }