db_rtld.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * db_rtld.c
  17. *
  18. * This file contains utilities to load ELF binaries into the memory
  19. * and link them against each other.
  20. * The source code in this file is imported and modified from the GNU C
  21. * Library.
  22. */
  23. #include "pal_defs.h"
  24. #include "pal.h"
  25. #include "pal_internal.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "pal_rtld.h"
  29. #include "api.h"
  30. #include <sysdeps/generic/ldsodefs.h>
  31. #include <elf/elf.h>
  32. #include <bits/dlfcn.h>
  33. struct link_map * loaded_libraries = NULL;
  34. struct link_map * exec_map = NULL;
  35. bool run_preload = false;
  36. struct link_map * lookup_symbol (const char *undef_name, ElfW(Sym) **ref);
  37. #ifdef assert
  38. /* This function can be used as a breakpoint to debug assertion */
  39. void __attribute__((noinline)) __assert (void)
  40. {
  41. BREAK();
  42. }
  43. #endif
  44. /* This structure communicates dl state to the debugger. The debugger
  45. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  46. a statically-linked program there is no dynamic section for the debugger
  47. to examine and it looks for this particular symbol name. */
  48. struct r_debug pal_r_debug =
  49. { 1, NULL, (ElfW(Addr)) &pal_dl_debug_state, RT_CONSISTENT, 0 };
  50. extern __typeof(pal_r_debug) _r_debug
  51. __attribute ((alias ("pal_r_debug")));
  52. /* This function exists solely to have a breakpoint set on it by the
  53. debugger. The debugger is supposed to find this function's address by
  54. examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
  55. for this particular symbol name in the PT_INTERP file. */
  56. /* The special symbol name is set as breakpoint in gdb */
  57. void __attribute__((noinline)) pal_dl_debug_state (void)
  58. {
  59. }
  60. extern __typeof(pal_dl_debug_state) _dl_debug_state
  61. __attribute ((alias ("pal_dl_debug_state")));
  62. void __attribute__((noinline)) _dl_debug_state_trigger (void)
  63. {
  64. struct link_map *l = pal_r_debug.r_map;
  65. for ( ; l ; l = l->l_next)
  66. if (!memcmp(l->l_name, "file:", 5))
  67. l->l_name += 5;
  68. pal_dl_debug_state();
  69. }
  70. /* This macro is used as a callback from the ELF_DYNAMIC_RELOCATE code. */
  71. static ElfW(Addr) resolve_map (const char **strtab, ElfW(Sym) ** ref)
  72. {
  73. if (ELFW(ST_BIND) ((*ref)->st_info) != STB_LOCAL) {
  74. struct link_map * l = lookup_symbol((*strtab) + (*ref)->st_name, ref);
  75. if (l) {
  76. *strtab = (const void *) D_PTR (l->l_info[DT_STRTAB]);
  77. return l->l_addr;
  78. }
  79. }
  80. return 0;
  81. }
  82. extern ElfW(Addr) resolve_rtld (const char * sym_name);
  83. #define RESOLVE_RTLD(sym_name) resolve_rtld(sym_name)
  84. #define RESOLVE_MAP(strtab, ref) resolve_map(strtab, ref)
  85. #include "dynamic_link.h"
  86. #include "dl-machine-x86_64.h"
  87. /* Allocate a `struct link_map' for a new object being loaded,
  88. and enter it into the _dl_loaded list. */
  89. struct link_map *
  90. new_elf_object (const char * realname, enum object_type type)
  91. {
  92. struct link_map *new;
  93. new = (struct link_map *) malloc(sizeof (struct link_map));
  94. if (new == NULL)
  95. return NULL;
  96. /* We apparently expect this to be zeroed. */
  97. memset(new, 0, sizeof(struct link_map));
  98. new->l_name = realname ?
  99. remalloc(realname, strlen(realname) + 1) :
  100. NULL;
  101. new->l_type = type;
  102. return new;
  103. }
  104. /* Cache the location of MAP's hash table. */
  105. void setup_elf_hash (struct link_map *map)
  106. {
  107. Elf_Symndx * hash;
  108. if (__builtin_expect (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
  109. + DT_THISPROCNUM + DT_VERSIONTAGNUM
  110. + DT_EXTRANUM + DT_VALNUM] != NULL, 1)) {
  111. Elf32_Word *hash32
  112. = (void *) D_PTR (map->l_info[DT_ADDRTAGIDX (DT_GNU_HASH) + DT_NUM
  113. + DT_THISPROCNUM + DT_VERSIONTAGNUM
  114. + DT_EXTRANUM + DT_VALNUM]);
  115. map->l_nbuckets = *hash32++;
  116. Elf32_Word symbias = *hash32++;
  117. Elf32_Word bitmask_nwords = *hash32++;
  118. /* Must be a power of two. */
  119. assert ((bitmask_nwords & (bitmask_nwords - 1)) == 0);
  120. map->l_gnu_bitmask_idxbits = bitmask_nwords - 1;
  121. map->l_gnu_shift = *hash32++;
  122. map->l_gnu_bitmask = (ElfW(Addr) *) hash32;
  123. hash32 += __ELF_NATIVE_CLASS / 32 * bitmask_nwords;
  124. map->l_gnu_buckets = hash32;
  125. hash32 += map->l_nbuckets;
  126. map->l_gnu_chain_zero = hash32 - symbias;
  127. return;
  128. }
  129. if (!map->l_info[DT_HASH])
  130. return;
  131. hash = (void *) D_PTR (map->l_info[DT_HASH]);
  132. /* Structure of DT_HASH:
  133. The bucket array forms the hast table itself. The entries in the
  134. chain array parallel the symbol table.
  135. [ nbucket ]
  136. [ nchain ]
  137. [ bucket[0] ]
  138. [ ... ]
  139. [ bucket[nbucket-1] ]
  140. [ chain[0] ]
  141. [ ... ]
  142. [ chain[nchain-1] ] */
  143. map->l_nbuckets = *hash++;
  144. hash++;
  145. map->l_buckets = hash;
  146. hash += map->l_nbuckets;
  147. map->l_chain = hash;
  148. }
  149. static void * __heap_base = NULL;
  150. static ElfW(Addr) __get_heap_base (size_t size)
  151. {
  152. if (__heap_base == (void *) -1)
  153. return 0;
  154. if (!__heap_base &&
  155. !(__heap_base = pal_config.heap_base)) {
  156. __heap_base = (void *) -1;
  157. return 0;
  158. }
  159. return (ElfW(Addr)) (__heap_base -= ALLOC_ALIGNUP(size));
  160. }
  161. /* Map in the shared object NAME, actually located in REALNAME, and already
  162. opened on FD */
  163. struct link_map *
  164. map_elf_object_by_handle (PAL_HANDLE handle, enum object_type type,
  165. void * fbp, size_t fbp_len,
  166. bool do_copy_dyn)
  167. {
  168. struct link_map * l = new_elf_object(_DkStreamRealpath(handle), type);
  169. const char * errstring = NULL;
  170. int errval = 0;
  171. int ret;
  172. if (handle == NULL) {
  173. errstring = "cannot stat shared object";
  174. errval = PAL_ERROR_INVAL;
  175. call_lose:
  176. printf("%s (%d)\n", errstring, PAL_STRERROR(errval));
  177. return NULL;
  178. }
  179. /* This is the ELF header. We read it in `open_verify'. */
  180. const ElfW(Ehdr) * header = (void *) fbp;
  181. /* Extract the remaining details we need from the ELF header
  182. and then read in the program header table. */
  183. int e_type = header->e_type;
  184. l->l_entry = header->e_entry;
  185. l->l_phnum = header->e_phnum;
  186. size_t maplength = header->e_phnum * sizeof (ElfW(Phdr));
  187. ElfW(Phdr) * phdr;
  188. if (header->e_phoff + maplength <= (size_t) fbp_len) {
  189. phdr = (void *) (fbp + header->e_phoff);
  190. } else {
  191. phdr = (ElfW(Phdr) *) malloc (maplength);
  192. if ((ret = _DkStreamRead(handle, header->e_phoff, maplength, phdr,
  193. NULL, 0)) < 0) {
  194. errstring = "cannot read file data";
  195. errval = ret;
  196. goto call_lose;
  197. }
  198. }
  199. /* Presumed absent PT_GNU_STACK. */
  200. //uint_fast16_t stack_flags = PF_R|PF_W|PF_X;
  201. /* Scan the program header table, collecting its load commands. */
  202. struct loadcmd {
  203. ElfW(Addr) mapstart, mapend, dataend, allocend;
  204. off_t mapoff;
  205. int prot;
  206. } loadcmds[l->l_phnum], *c;
  207. size_t nloadcmds = 0;
  208. bool has_holes = false;
  209. /* The struct is initialized to zero so this is not necessary:
  210. l->l_ld = 0;
  211. l->l_phdr = 0;
  212. l->l_addr = 0; */
  213. const ElfW(Phdr) * ph;
  214. for (ph = phdr; ph < &phdr[l->l_phnum]; ++ph)
  215. switch (ph->p_type)
  216. {
  217. /* These entries tell us where to find things once the file's
  218. segments are mapped in. We record the addresses it says
  219. verbatim, and later correct for the run-time load address. */
  220. case PT_DYNAMIC:
  221. l->l_ld = (void *) ph->p_vaddr;
  222. l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
  223. break;
  224. case PT_PHDR:
  225. l->l_phdr = (void *) ph->p_vaddr;
  226. break;
  227. case PT_LOAD:
  228. /* A load command tells us to map in part of the file.
  229. We record the load commands and process them all later. */
  230. if (__builtin_expect ((ph->p_align & allocshift) != 0, 0)) {
  231. errstring = "ELF load command alignment not aligned";
  232. errval = ENOMEM;
  233. goto call_lose;
  234. }
  235. if (__builtin_expect (((ph->p_vaddr - ph->p_offset)
  236. & (ph->p_align - 1)) != 0, 0)) {
  237. errstring = "\
  238. ELF load command address/offset not properly aligned";
  239. errval = ENOMEM;
  240. goto call_lose;
  241. }
  242. c = &loadcmds[nloadcmds++];
  243. c->mapstart = ALLOC_ALIGNDOWN(ph->p_vaddr);
  244. c->mapend = ALLOC_ALIGNUP(ph->p_vaddr + ph->p_filesz);
  245. c->dataend = ph->p_vaddr + ph->p_filesz;
  246. c->allocend = ph->p_vaddr + ph->p_memsz;
  247. c->mapoff = ALLOC_ALIGNDOWN(ph->p_offset);
  248. /* Determine whether there is a gap between the last segment
  249. and this one. */
  250. if (nloadcmds > 1 && c[-1].mapend != c->mapstart)
  251. has_holes = true;
  252. /* Optimize a common case. */
  253. #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
  254. c->prot = (PF_TO_PROT
  255. >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
  256. #else
  257. c->prot = 0;
  258. if (ph->p_flags & PF_R)
  259. c->prot |= PROT_READ;
  260. if (ph->p_flags & PF_W)
  261. c->prot |= PROT_WRITE;
  262. if (ph->p_flags & PF_X)
  263. c->prot |= PROT_EXEC;
  264. #endif
  265. break;
  266. case PT_TLS:
  267. if (ph->p_memsz == 0)
  268. /* Nothing to do for an empty segment. */
  269. break;
  270. case PT_GNU_STACK:
  271. //stack_flags = ph->p_flags;
  272. break;
  273. case PT_GNU_RELRO:
  274. l->l_relro_addr = ph->p_vaddr;
  275. l->l_relro_size = ph->p_memsz;
  276. break;
  277. }
  278. if (__builtin_expect (nloadcmds == 0, 0)) {
  279. /* This only happens for a bogus object that will be caught with
  280. another error below. But we don't want to go through the
  281. calculations below using NLOADCMDS - 1. */
  282. errstring = "object file has no loadable segments";
  283. goto call_lose;
  284. }
  285. /* Now process the load commands and map segments into memory. */
  286. c = loadcmds;
  287. /* Length of the sections to be loaded. */
  288. maplength = loadcmds[nloadcmds - 1].allocend - c->mapstart;
  289. #define APPEND_WRITECOPY(prot) ((prot)|PAL_PROT_WRITECOPY)
  290. if (__builtin_expect (e_type, ET_DYN) == ET_DYN) {
  291. /* This is a position-independent shared object. We can let the
  292. kernel map it anywhere it likes, but we must have space for all
  293. the segments in their specified positions relative to the first.
  294. So we map the first segment without MAP_FIXED, but with its
  295. extent increased to cover all the segments. Then we remove
  296. access from excess portion, and there is known sufficient space
  297. there to remap from the later segments.
  298. As a refinement, sometimes we have an address that we would
  299. prefer to map such objects at; but this is only a preference,
  300. the OS can do whatever it likes. */
  301. ElfW(Addr) mappref = __get_heap_base(maplength);
  302. /* Remember which part of the address space this object uses. */
  303. errval = _DkStreamMap(handle, (void **) &mappref,
  304. APPEND_WRITECOPY(c->prot), c->mapoff,
  305. maplength);
  306. if (__builtin_expect (errval < 0, 0)) {
  307. errval = -errval;
  308. map_error:
  309. errstring = "failed to map segment from shared object";
  310. goto call_lose;
  311. }
  312. l->l_map_start = mappref;
  313. l->l_map_end = l->l_map_start + maplength;
  314. l->l_addr = l->l_map_start - c->mapstart;
  315. if (has_holes)
  316. /* Change protection on the excess portion to disallow all access;
  317. the portions we do not remap later will be inaccessible as if
  318. unallocated. Then jump into the normal segment-mapping loop to
  319. handle the portion of the segment past the end of the file
  320. mapping. */
  321. _DkVirtualMemoryProtect((caddr_t) (l->l_addr + c->mapend),
  322. loadcmds[nloadcmds - 1].mapstart - c->mapend,
  323. PAL_PROT_NONE);
  324. goto postmap;
  325. }
  326. /* Remember which part of the address space this object uses. */
  327. l->l_map_start = c->mapstart + l->l_addr;
  328. l->l_map_end = l->l_map_start + maplength;
  329. while (c < &loadcmds[nloadcmds]) {
  330. if (c->mapend > c->mapstart) {
  331. /* Map the segment contents from the file. */
  332. void * mapaddr = (void *) (l->l_addr + c->mapstart);
  333. int rv;
  334. if ((rv = _DkStreamMap(handle, &mapaddr, APPEND_WRITECOPY(c->prot),
  335. c->mapoff, c->mapend - c->mapstart)) < 0) {
  336. goto map_error;
  337. }
  338. }
  339. postmap:
  340. if (c->prot & PROT_EXEC) {
  341. l->l_text_start = l->l_addr + c->mapstart;
  342. l->l_text_end = l->l_addr + c->mapend;
  343. }
  344. if (c->prot & PROT_WRITE) {
  345. l->l_data_start = l->l_addr + c->mapstart;
  346. l->l_data_end = l->l_addr + c->mapend;
  347. }
  348. if (l->l_phdr == 0
  349. && (ElfW(Off)) c->mapoff <= header->e_phoff
  350. && ((size_t) (c->mapend - c->mapstart + c->mapoff)
  351. >= header->e_phoff + header->e_phnum * sizeof (ElfW(Phdr))))
  352. /* Found the program header in this segment. */
  353. l->l_phdr = (void *) (c->mapstart + header->e_phoff - c->mapoff);
  354. if (c->allocend > c->dataend) {
  355. /* Extra zero pages should appear at the end of this segment,
  356. after the data mapped from the file. */
  357. ElfW(Addr) zero, zeroend, zerosec;
  358. zero = l->l_addr + c->dataend;
  359. zeroend = ALLOC_ALIGNUP(l->l_addr + c->allocend);
  360. zerosec = ALLOC_ALIGNUP(zero);
  361. if (zeroend < zerosec)
  362. /* All the extra data is in the last section of the segment.
  363. We can just zero it. */
  364. zerosec = zeroend;
  365. if (zerosec > zero) {
  366. /* Zero the final part of the last section of the segment. */
  367. if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
  368. {
  369. /* Dag nab it. */
  370. if (_DkVirtualMemoryProtect((void *) ALLOC_ALIGNDOWN(zero),
  371. allocsize,
  372. c->prot|PAL_PROT_WRITE) < 0) {
  373. errstring = "cannot change memory protections";
  374. goto call_lose;
  375. }
  376. }
  377. memset ((void *) zero, '\0', zerosec - zero);
  378. if (__builtin_expect ((c->prot & PROT_WRITE) == 0, 0))
  379. _DkVirtualMemoryProtect((void *) ALLOC_ALIGNDOWN(zero),
  380. allocsize, c->prot);
  381. }
  382. if (zeroend > zerosec) {
  383. /* Map the remaining zero pages in from the zero fill FD. */
  384. void * mapat = (void *) zerosec;
  385. errval = _DkVirtualMemoryAlloc(&mapat, zeroend - zerosec,
  386. 0, c->prot);
  387. if (__builtin_expect (errval < 0, 0)) {
  388. errstring = "cannot map zero-fill allocation";
  389. goto call_lose;
  390. }
  391. }
  392. }
  393. ++c;
  394. }
  395. if (l->l_ld == 0) {
  396. if (__builtin_expect (e_type == ET_DYN, 0)) {
  397. errstring = "object file has no dynamic section";
  398. goto call_lose;
  399. }
  400. } else {
  401. l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
  402. }
  403. l->l_real_ld = l->l_ld;
  404. if (do_copy_dyn)
  405. l->l_ld = remalloc(l->l_ld, sizeof(ElfW(Dyn)) * l->l_ldnum);
  406. elf_get_dynamic_info(l->l_ld, l->l_info, l->l_addr);
  407. /* When we profile the SONAME might be needed for something else but
  408. loading. Add it right away. */
  409. if (l->l_info[DT_STRTAB] && l->l_info[DT_SONAME])
  410. l->l_soname = (char *) (D_PTR (l->l_info[DT_STRTAB])
  411. + D_PTR (l->l_info[DT_SONAME]));
  412. if (l->l_phdr == NULL) {
  413. /* The program header is not contained in any of the segments.
  414. We have to allocate memory ourself and copy it over from out
  415. temporary place. */
  416. ElfW(Phdr) * newp = (ElfW(Phdr) *) malloc (header->e_phnum
  417. * sizeof (ElfW(Phdr)));
  418. if (!newp) {
  419. errstring = "cannot allocate memory for program header";
  420. goto call_lose;
  421. }
  422. l->l_phdr = memcpy(newp, phdr,
  423. header->e_phnum * sizeof (ElfW(Phdr)));
  424. } else {
  425. /* Adjust the PT_PHDR value by the runtime load address. */
  426. l->l_phdr = (ElfW(Phdr) *) ((ElfW(Addr)) l->l_phdr + l->l_addr);
  427. }
  428. l->l_entry += l->l_addr;
  429. /* Set up the symbol hash table. */
  430. setup_elf_hash (l);
  431. return l;
  432. }
  433. int check_elf_object (PAL_HANDLE handle)
  434. {
  435. #define ELF_MAGIC_SIZE EI_CLASS
  436. unsigned char buffer[ELF_MAGIC_SIZE];
  437. int len = _DkStreamRead(handle, 0, ELF_MAGIC_SIZE, buffer, NULL, 0);
  438. if (__builtin_expect (len < 0, 0))
  439. return -len;
  440. if (__builtin_expect (len < ELF_MAGIC_SIZE, 0))
  441. return -PAL_ERROR_INVAL;
  442. ElfW(Ehdr) * ehdr = (ElfW(Ehdr) *) buffer;
  443. static const unsigned char expected[EI_CLASS] =
  444. {
  445. [EI_MAG0] = ELFMAG0,
  446. [EI_MAG1] = ELFMAG1,
  447. [EI_MAG2] = ELFMAG2,
  448. [EI_MAG3] = ELFMAG3,
  449. };
  450. /* See whether the ELF header is what we expect. */
  451. if (__builtin_expect(memcmp(ehdr->e_ident, expected, ELF_MAGIC_SIZE) !=
  452. 0, 0))
  453. return -PAL_ERROR_INVAL;
  454. return 0;
  455. }
  456. void free_elf_object (struct link_map * map)
  457. {
  458. /* unmap the exec_map */
  459. _DkVirtualMemoryFree((void *) map->l_map_start,
  460. map->l_map_end - map->l_map_start);
  461. pal_sec_info._r_debug->r_state = RT_DELETE;
  462. pal_sec_info._dl_debug_state();
  463. if (map->l_prev)
  464. map->l_prev->l_next = map->l_next;
  465. if (map->l_next)
  466. map->l_next->l_prev = map->l_prev;
  467. pal_sec_info._r_debug->r_state = RT_CONSISTENT;
  468. pal_sec_info._dl_debug_state();
  469. if (loaded_libraries == map)
  470. loaded_libraries = map->l_next;
  471. free(map);
  472. }
  473. /* Map in the shared object file loaded from URI. */
  474. int load_elf_object (const char * uri, enum object_type type)
  475. {
  476. PAL_HANDLE handle;
  477. /* First we open the file by uri, as the regular file handles */
  478. int ret = _DkStreamOpen(&handle, uri, PAL_ACCESS_RDONLY,
  479. 0, 0, 0);
  480. if (ret < 0)
  481. return ret;
  482. if (type == OBJECT_EXEC) {
  483. struct link_map *map = loaded_libraries, *next;
  484. while (map) {
  485. next = map->l_next;
  486. if (map->l_type == type)
  487. free_elf_object(map);
  488. map = next;
  489. }
  490. }
  491. ret = load_elf_object_by_handle(handle, type);
  492. _DkObjectClose(handle);
  493. return ret;
  494. }
  495. static int relocate_elf_object (struct link_map *l);
  496. int load_elf_object_by_handle (PAL_HANDLE handle, enum object_type type)
  497. {
  498. char fb[FILEBUF_SIZE];
  499. char * errstring;
  500. int ret = 0;
  501. /* Now we will start verify the file as a ELF header. This part of code
  502. is borrow from open_verify() */
  503. ElfW(Ehdr) * ehdr = (ElfW(Ehdr) *) &fb;
  504. ElfW(Phdr) * phdr = NULL;
  505. int phdr_malloced = 0;
  506. int len = _DkStreamRead(handle, 0, FILEBUF_SIZE, &fb, NULL, 0);
  507. if (__builtin_expect (len < sizeof(ElfW(Ehdr)), 0)) {
  508. errstring = "ELF file with a strange size";
  509. goto verify_failed;
  510. }
  511. #define ELF32_CLASS ELFCLASS32
  512. #define ELF64_CLASS ELFCLASS64
  513. static const unsigned char expected[EI_NIDENT] =
  514. {
  515. [EI_MAG0] = ELFMAG0,
  516. [EI_MAG1] = ELFMAG1,
  517. [EI_MAG2] = ELFMAG2,
  518. [EI_MAG3] = ELFMAG3,
  519. [EI_CLASS] = ELFW(CLASS),
  520. [EI_DATA] = byteorder,
  521. [EI_VERSION] = EV_CURRENT,
  522. [EI_OSABI] = 0,
  523. };
  524. #define ELFOSABI_LINUX 3 /* Linux. */
  525. int maplength;
  526. /* See whether the ELF header is what we expect. */
  527. if (__builtin_expect(
  528. memcmp(ehdr->e_ident, expected, EI_OSABI) != 0 || (
  529. ehdr->e_ident[EI_OSABI] != ELFOSABI_SYSV &&
  530. ehdr->e_ident[EI_OSABI] != ELFOSABI_LINUX), 0)) {
  531. errstring = "ELF file with invalid header";
  532. goto verify_failed;
  533. }
  534. /* Chia-Che 11/23/13: Removing other checks, comparing the header
  535. should be enough */
  536. maplength = ehdr->e_phnum * sizeof (ElfW(Phdr));
  537. /* if e_phoff + maplength is smaller than the data read */
  538. if (ehdr->e_phoff + maplength <= (size_t) len) {
  539. phdr = (void *) (&fb + ehdr->e_phoff);
  540. } else {
  541. /* ...otherwise, we have to read again */
  542. phdr = malloc (maplength);
  543. phdr_malloced = 1;
  544. ret = _DkStreamRead(handle, ehdr->e_phoff, maplength, phdr, NULL, 0);
  545. if (ret < 0 || ret != maplength) {
  546. errstring = "cannot read file data";
  547. goto verify_failed;
  548. }
  549. }
  550. pal_sec_info._r_debug->r_state = RT_ADD;
  551. pal_sec_info._dl_debug_state();
  552. struct link_map * map;
  553. if (!(map = map_elf_object_by_handle(handle, type, &fb, len, true))) {
  554. errstring = "unexpected failure";
  555. goto verify_failed;
  556. }
  557. relocate_elf_object(map);
  558. if (map->l_type == OBJECT_EXEC)
  559. exec_map = map;
  560. if (map->l_type == OBJECT_PRELOAD && map->l_entry)
  561. run_preload = true;
  562. struct link_map * prev = NULL, ** pprev = &loaded_libraries,
  563. * next = loaded_libraries;
  564. while (next) {
  565. prev = next;
  566. pprev = &next->l_next;
  567. next = next->l_next;
  568. }
  569. *pprev = map;
  570. map->l_prev = prev;
  571. map->l_next = NULL;
  572. pal_sec_info._r_debug->r_state = RT_CONSISTENT;
  573. pal_sec_info._dl_debug_state();
  574. return 0;
  575. verify_failed:
  576. if (phdr && phdr_malloced)
  577. free(phdr);
  578. printf("%s\n", errstring);
  579. return ret;
  580. }
  581. struct sym_val {
  582. ElfW(Sym) *s;
  583. struct link_map *m;
  584. };
  585. /* This is the hashing function specified by the ELF ABI. In the
  586. first five operations no overflow is possible so we optimized it a
  587. bit. */
  588. unsigned long int elf_hash (const char *name_arg)
  589. {
  590. const unsigned char *name = (const unsigned char *) name_arg;
  591. unsigned long int hash = 0;
  592. if (*name == '\0')
  593. return hash;
  594. hash = *name++;
  595. if (*name == '\0')
  596. return hash;
  597. hash = (hash << 4) + *name++;
  598. if (*name == '\0')
  599. return hash;
  600. hash = (hash << 4) + *name++;
  601. if (*name == '\0')
  602. return hash;
  603. hash = (hash << 4) + *name++;
  604. if (*name == '\0')
  605. return hash;
  606. hash = (hash << 4) + *name++;
  607. while (*name != '\0') {
  608. unsigned long int hi;
  609. hash = (hash << 4) + *name++;
  610. hi = hash & 0xf0000000;
  611. /*
  612. * The algorithm specified in the ELF ABI is as follows:
  613. * if (hi != 0)
  614. * hash ^= hi >> 24;
  615. * hash &= ~hi;
  616. * But the following is equivalent and a lot faster, especially on
  617. * modern processors.
  618. */
  619. hash ^= hi;
  620. hash ^= hi >> 24;
  621. }
  622. return hash;
  623. }
  624. ElfW(Sym) *
  625. do_lookup_map (ElfW(Sym) * ref, const char * undef_name,
  626. const uint_fast32_t hash, unsigned long int elf_hash,
  627. const struct link_map * map)
  628. {
  629. /* These variables are used in the nested function. */
  630. Elf_Symndx symidx;
  631. ElfW(Sym) * sym;
  632. /* The tables for this map. */
  633. ElfW(Sym) * symtab = (void *) D_PTR (map->l_info[DT_SYMTAB]);
  634. const char * strtab = (const void *) D_PTR (map->l_info[DT_STRTAB]);
  635. /* Nested routine to check whether the symbol matches. */
  636. ElfW(Sym) * check_match (ElfW(Sym) *sym)
  637. {
  638. unsigned int stt = ELFW(ST_TYPE) (sym->st_info);
  639. assert (ELF_RTYPE_CLASS_PLT == 1);
  640. if (__builtin_expect ((sym->st_value == 0 /* No value. */
  641. && stt != STT_TLS)
  642. || sym->st_shndx == SHN_UNDEF, 0))
  643. return NULL;
  644. /* Ignore all but STT_NOTYPE, STT_OBJECT, STT_FUNC,
  645. STT_COMMON, STT_TLS, and STT_GNU_IFUNC since these are no
  646. code/data definitions. */
  647. #define ALLOWED_STT \
  648. ((1 << STT_NOTYPE) | (1 << STT_OBJECT) | (1 << STT_FUNC) \
  649. | (1 << STT_COMMON) | (1 << STT_TLS) | (1 << STT_GNU_IFUNC))
  650. if (__builtin_expect (((1 << stt) & ALLOWED_STT) == 0, 0))
  651. return NULL;
  652. if (sym != ref && memcmp(strtab + sym->st_name, undef_name,
  653. strlen(undef_name)))
  654. /* Not the symbol we are looking for. */
  655. return NULL;
  656. /* There cannot be another entry for this symbol so stop here. */
  657. return sym;
  658. }
  659. const ElfW(Addr) * bitmask = map->l_gnu_bitmask;
  660. if (__builtin_expect (bitmask != NULL, 1)) {
  661. ElfW(Addr) bitmask_word = bitmask[(hash / __ELF_NATIVE_CLASS)
  662. & map->l_gnu_bitmask_idxbits];
  663. unsigned int hashbit1 = hash & (__ELF_NATIVE_CLASS - 1);
  664. unsigned int hashbit2 = (hash >> map->l_gnu_shift)
  665. & (__ELF_NATIVE_CLASS - 1);
  666. if (__builtin_expect ((bitmask_word >> hashbit1)
  667. & (bitmask_word >> hashbit2) & 1, 0)) {
  668. Elf32_Word bucket = map->l_gnu_buckets
  669. [hash % map->l_nbuckets];
  670. if (bucket != 0) {
  671. const Elf32_Word *hasharr = &map->l_gnu_chain_zero[bucket];
  672. do
  673. if (((*hasharr ^ hash) >> 1) == 0) {
  674. symidx = hasharr - map->l_gnu_chain_zero;
  675. sym = check_match (&symtab[symidx]);
  676. if (sym != NULL)
  677. return sym;
  678. }
  679. while ((*hasharr++ & 1u) == 0);
  680. }
  681. }
  682. /* No symbol found. */
  683. symidx = SHN_UNDEF;
  684. } else {
  685. /* Use the old SysV-style hash table. Search the appropriate
  686. hash bucket in this object's symbol table for a definition
  687. for the same symbol name. */
  688. for (symidx = map->l_buckets[elf_hash % map->l_nbuckets];
  689. symidx != STN_UNDEF;
  690. symidx = map->l_chain[symidx]) {
  691. sym = check_match (&symtab[symidx]);
  692. if (sym != NULL)
  693. return sym;
  694. }
  695. }
  696. return NULL;
  697. }
  698. /* Inner part of the lookup functions. We return a value > 0 if we
  699. found the symbol, the value 0 if nothing is found and < 0 if
  700. something bad happened. */
  701. static int do_lookup (const char * undef_name, ElfW(Sym) * ref,
  702. struct sym_val * result)
  703. {
  704. const uint_fast32_t fast_hash = elf_fast_hash(undef_name);
  705. const long int hash = elf_hash(undef_name);
  706. ElfW(Sym) * sym;
  707. struct link_map * map = loaded_libraries;
  708. while (map) {
  709. if (!map->l_lookup_symbol) {
  710. map = map->l_next;
  711. continue;
  712. }
  713. sym = do_lookup_map (ref, undef_name, fast_hash, hash, map);
  714. if (sym == NULL)
  715. return 0;
  716. switch (__builtin_expect (ELFW(ST_BIND) (sym->st_info), STB_GLOBAL)) {
  717. case STB_WEAK:
  718. /* Weak definition. Use this value if we don't find another. */
  719. if (!result->s) {
  720. result->s = sym;
  721. result->m = (struct link_map *) map;
  722. }
  723. break;
  724. /* FALLTHROUGH */
  725. case STB_GLOBAL:
  726. case STB_GNU_UNIQUE:
  727. /* success: */
  728. /* Global definition. Just what we need. */
  729. result->s = sym;
  730. result->m = (struct link_map *) map;
  731. return 1;
  732. default:
  733. /* Local symbols are ignored. */
  734. break;
  735. }
  736. map = map->l_next;
  737. }
  738. /* We have not found anything until now. */
  739. return 0;
  740. }
  741. /* Search loaded objects' symbol tables for a definition of the symbol
  742. UNDEF_NAME, perhaps with a requested version for the symbol.
  743. We must never have calls to the audit functions inside this function
  744. or in any function which gets called. If this would happen the audit
  745. code might create a thread which can throw off all the scope locking. */
  746. struct link_map * lookup_symbol (const char * undef_name, ElfW(Sym) ** ref)
  747. {
  748. struct sym_val current_value = { NULL, NULL };
  749. do_lookup(undef_name, *ref, &current_value);
  750. if (__builtin_expect (current_value.s == NULL, 0)) {
  751. *ref = NULL;
  752. return NULL;
  753. }
  754. *ref = current_value.s;
  755. return current_value.m;
  756. }
  757. static int protect_relro (struct link_map * l)
  758. {
  759. ElfW(Addr) start = ALLOC_ALIGNDOWN(l->l_addr + l->l_relro_addr);
  760. ElfW(Addr) end = ALLOC_ALIGNUP(l->l_addr + l->l_relro_addr +
  761. l->l_relro_size);
  762. if (start != end)
  763. _DkVirtualMemoryProtect((void *) start, end - start, PAL_PROT_READ);
  764. return 0;
  765. }
  766. static int relocate_elf_object (struct link_map * l)
  767. {
  768. struct textrels {
  769. ElfW(Addr) start;
  770. ElfW(Addr) len;
  771. int prot;
  772. struct textrels * next;
  773. } * textrels = NULL;
  774. int ret;
  775. const ElfW(Phdr) * ph;
  776. for (ph = l->l_phdr ; ph < &l->l_phdr[l->l_phnum] ; ph++)
  777. if (ph->p_type == PT_LOAD && (ph->p_flags & PF_W) == 0) {
  778. struct textrels * r = malloc(sizeof(struct textrels));
  779. r->start = ALLOC_ALIGNDOWN(ph->p_vaddr) + l->l_addr;
  780. r->len = ALLOC_ALIGNUP(ph->p_vaddr + ph->p_memsz)
  781. - ALLOC_ALIGNDOWN(ph->p_vaddr);
  782. ret = _DkVirtualMemoryProtect((void *) r->start, r->len,
  783. PAL_PROT_READ|PAL_PROT_WRITE);
  784. if (ret < 0)
  785. return ret;
  786. #if (PF_R | PF_W | PF_X) == 7 && (PROT_READ | PROT_WRITE | PROT_EXEC) == 7
  787. r->prot = (PF_TO_PROT
  788. >> ((ph->p_flags & (PF_R | PF_W | PF_X)) * 4)) & 0xf;
  789. #else
  790. r->prot = 0;
  791. if (ph->p_flags & PF_R)
  792. r->prot |= PROT_READ;
  793. if (ph->p_flags & PF_W)
  794. r->prot |= PROT_WRITE;
  795. if (ph->p_flags & PF_X)
  796. r->prot |= PROT_EXEC;
  797. #endif
  798. r->next = textrels;
  799. textrels = r;
  800. }
  801. /* Do the actual relocation of the object's GOT and other data. */
  802. if (l->l_type == OBJECT_EXEC)
  803. ELF_DYNAMIC_SCAN(l->l_info, l->l_addr);
  804. else
  805. ELF_DYNAMIC_RELOCATE(l->l_info, l->l_addr);
  806. while (textrels) {
  807. ret = _DkVirtualMemoryProtect((void *) textrels->start, textrels->len,
  808. textrels->prot);
  809. if (ret < 0)
  810. return ret;
  811. struct textrels * next = textrels->next;
  812. free(textrels);
  813. textrels = next;
  814. }
  815. /* In case we can protect the data now that the relocations are
  816. done, do it. */
  817. if (l->l_type != OBJECT_EXEC && l->l_relro_size != 0)
  818. if ((ret = protect_relro(l)) < 0)
  819. return ret;
  820. if (l->l_type == OBJECT_PRELOAD && pal_config.syscall_sym_name) {
  821. uint_fast32_t fast_hash = elf_fast_hash(pal_config.syscall_sym_name);
  822. long int hash = elf_hash(pal_config.syscall_sym_name);
  823. ElfW(Sym) * sym = NULL;
  824. sym = do_lookup_map(NULL, pal_config.syscall_sym_name, fast_hash,
  825. hash, l);
  826. if (sym) {
  827. pal_config.syscall_sym_addr =
  828. (void *) (l->l_addr + sym->st_value);
  829. }
  830. }
  831. l->l_relocated = true;
  832. return 0;
  833. }
  834. void start_execution (int argc, const char ** argv)
  835. {
  836. /* First we will try to run all the preloaded libraries which come with
  837. entry points */
  838. if (exec_map) {
  839. __pal_control.executable_begin = (void *) exec_map->l_map_start;
  840. __pal_control.executable_end = (void *) exec_map->l_map_end;
  841. }
  842. int ret = 0;
  843. if (!run_preload)
  844. goto NO_PRELOAD;
  845. /* Let's count the number of cookies, first we will have argc & argv */
  846. size_t ncookies = argc + 2; /* 1 for argc, argc + 1 for argv */
  847. /* Then we count envp */
  848. for (const char ** e = pal_config.environments; *e; e++)
  849. ncookies++;
  850. ncookies++; /* for NULL-end */
  851. size_t cookiesz = sizeof(unsigned long int) * ncookies
  852. + sizeof(ElfW(auxv_t)) * 6
  853. + sizeof(void *) * 3 + 16;
  854. unsigned long int * cookies = __alloca(cookiesz);
  855. /* Let's copy the cookies */
  856. cookies[0] = (unsigned long int) argc;
  857. size_t i;
  858. for (i = 0 ; i <= argc ; i++)
  859. cookies[i + 1] = (unsigned long int) argv[i];
  860. size_t cnt = argc + 2;
  861. if (pal_config.environments)
  862. for (i = 0 ; pal_config.environments[i]; i++)
  863. cookies[cnt++] = (unsigned long int) pal_config.environments[i];
  864. cookies[cnt++] = 0;
  865. ElfW(auxv_t) * auxv = (ElfW(auxv_t) *) &cookies[cnt];
  866. auxv[0].a_type = AT_PHDR;
  867. auxv[0].a_un.a_val = exec_map ?
  868. (__typeof(auxv[1].a_un.a_val)) exec_map->l_phdr : 0;
  869. auxv[1].a_type = AT_PHNUM;
  870. auxv[1].a_un.a_val = exec_map ? exec_map->l_phnum : 0;
  871. auxv[2].a_type = AT_PAGESZ;
  872. auxv[2].a_un.a_val = __pal_control.pagesize;
  873. auxv[3].a_type = AT_ENTRY;
  874. auxv[3].a_un.a_val = exec_map ? exec_map->l_entry : 0;
  875. auxv[4].a_type = AT_BASE;
  876. auxv[4].a_un.a_val = exec_map ? exec_map->l_addr : 0;
  877. auxv[5].a_type = AT_NULL;
  878. void * stack = (void *) &auxv[6] + sizeof(uint64_t);
  879. ((uint64_t *) stack)[-1] = 0;
  880. /* the previous cookiesz might be wrong, we have to recalculate it */
  881. cookiesz = (PAL_PTR) &auxv[6] - (PAL_PTR) cookies;
  882. for (struct link_map * l = loaded_libraries ; l ; l = l->l_next) {
  883. if (l->l_type != OBJECT_PRELOAD || !l->l_entry)
  884. continue;
  885. #if defined(__x86_64__)
  886. asm volatile (
  887. "movq %%rsp, 16(%3)\r\n"
  888. "movq %2, %%rsp\r\n"
  889. "leaq .LRET1(%%rip), %%rbx\r\n"
  890. "movq %%rbx, 8(%3)\r\n"
  891. "movq %%rbp, 0(%3)\r\n"
  892. "jmp *%1\r\n"
  893. ".LRET1:\r\n"
  894. "popq %%rsp\r\n"
  895. : "=a" (ret)
  896. : "a"(l->l_entry),
  897. "b"(cookies),
  898. "S"(stack)
  899. : "rcx", "rdx", "r8", "r9", "r10", "r11", "memory");
  900. #else
  901. # error "architecture not supported"
  902. #endif
  903. if (ret < 0)
  904. _DkThreadExit(ret);
  905. }
  906. NO_PRELOAD:
  907. if (exec_map && exec_map->l_entry) {
  908. /* This part is awesome. Don't risk changing it!! */
  909. #if defined(__x86_64__)
  910. ret = ((int (*) (int, const char **, const char **))
  911. exec_map->l_entry) (argc, argv, pal_config.environments);
  912. #else
  913. # error "architecture not supported"
  914. #endif
  915. }
  916. /* If they ever return here, we will be exiting */
  917. _DkProcessExit(ret);
  918. /* Control should not get here */
  919. assert(0);
  920. }