db_rtld.c 36 KB

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