db_rtld.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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_linux_defs.h"
  25. #include "pal.h"
  26. #include "pal_internal.h"
  27. #include "pal_linux.h"
  28. #include "pal_debug.h"
  29. #include "pal_error.h"
  30. #include "pal_security.h"
  31. #include "pal_rtld.h"
  32. #include "api.h"
  33. #include <sysdeps/generic/ldsodefs.h>
  34. #include <elf/elf.h>
  35. #include <bits/dlfcn.h>
  36. #include "elf-x86_64.h"
  37. /* This structure communicates dl state to the debugger. The debugger
  38. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  39. a statically-linked program there is no dynamic section for the debugger
  40. to examine and it looks for this particular symbol name. */
  41. struct r_debug pal_r_debug =
  42. { 1, NULL, (ElfW(Addr)) &pal_dl_debug_state, RT_CONSISTENT, 0 };
  43. extern __typeof(pal_r_debug) _r_debug
  44. __attribute ((alias ("pal_r_debug")));
  45. /* This function exists solely to have a breakpoint set on it by the
  46. debugger. The debugger is supposed to find this function's address by
  47. examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
  48. for this particular symbol name in the PT_INTERP file. */
  49. /* The special symbol name is set as breakpoint in gdb */
  50. void __attribute__((noinline)) pal_dl_debug_state (void)
  51. {
  52. if (pal_sec_info._dl_debug_state)
  53. pal_sec_info._dl_debug_state();
  54. }
  55. extern __typeof(pal_dl_debug_state) _dl_debug_state
  56. __attribute ((alias ("pal_dl_debug_state")));
  57. void _DkDebugAddMap (struct link_map * map)
  58. {
  59. struct r_debug * dbg = pal_sec_info._r_debug ? : &pal_r_debug;
  60. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  61. struct link_map ** prev = &dbg->r_map, * last = NULL,
  62. * tmp = *prev;
  63. while (tmp) {
  64. if (tmp->l_addr == map->l_addr &&
  65. tmp->l_ld == map->l_ld &&
  66. !memcmp(tmp->l_name, map->l_name, len))
  67. return;
  68. last = tmp;
  69. tmp = *(prev = &last->l_next);
  70. }
  71. struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
  72. if (!m)
  73. return;
  74. if (len) {
  75. m->l_name = (char *) m + sizeof(struct link_gdb_map);
  76. memcpy((void *) m->l_name, map->l_name, len);
  77. } else {
  78. m->l_name = NULL;
  79. }
  80. m->l_addr = map->l_addr;
  81. m->l_ld = map->l_real_ld;
  82. dbg->r_state = RT_ADD;
  83. pal_dl_debug_state();
  84. *prev = (struct link_map *) m;
  85. m->l_prev = last;
  86. m->l_next = NULL;
  87. dbg->r_state = RT_CONSISTENT;
  88. pal_dl_debug_state();
  89. }
  90. void _DkDebugDelMap (struct link_map * map)
  91. {
  92. struct r_debug * dbg = pal_sec_info._r_debug ? : &pal_r_debug;
  93. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  94. struct link_map ** prev = &dbg->r_map, * last = NULL,
  95. * tmp = *prev, * found = NULL;
  96. while (tmp) {
  97. if (tmp->l_addr == map->l_addr &&
  98. tmp->l_ld == map->l_ld &&
  99. !memcmp(tmp->l_name, map->l_name, len)) {
  100. found = tmp;
  101. break;
  102. }
  103. last = tmp;
  104. tmp = *(prev = &last->l_next);
  105. }
  106. if (!found)
  107. return;
  108. dbg->r_state = RT_DELETE;
  109. pal_dl_debug_state();
  110. if (last)
  111. last->l_next = tmp->l_next;
  112. else
  113. dbg->r_map = tmp->l_next;
  114. if (tmp->l_next)
  115. tmp->l_next->l_prev = last;
  116. free(tmp);
  117. dbg->r_state = RT_CONSISTENT;
  118. pal_dl_debug_state();
  119. }
  120. extern void setup_elf_hash (struct link_map *map);
  121. void setup_pal_map (const char * realname, ElfW(Dyn) ** dyn, ElfW(Addr) addr)
  122. {
  123. assert (loaded_libraries == NULL);
  124. const ElfW(Ehdr) * header = (void *) addr;
  125. struct link_map * l = new_elf_object(realname, OBJECT_RTLD);
  126. memcpy(l->l_info, dyn, sizeof(l->l_info));
  127. l->l_real_ld = l->l_ld = (void *) elf_machine_dynamic();
  128. l->l_addr = addr;
  129. l->l_entry = header->e_entry;
  130. l->l_phdr = (void *) (addr + header->e_phoff);
  131. l->l_phnum = header->e_phnum;
  132. l->l_relocated = true;
  133. l->l_lookup_symbol = true;
  134. l->l_soname = "libpal.so";
  135. l->l_text_start = (ElfW(Addr)) &text_start;
  136. l->l_text_end = (ElfW(Addr)) &text_end;
  137. l->l_data_start = (ElfW(Addr)) &data_start;
  138. l->l_data_end = (ElfW(Addr)) &data_end;
  139. setup_elf_hash(l);
  140. void * begin_hole = (void *) ALLOC_ALIGNUP(l->l_text_end);
  141. void * end_hole = (void *) ALLOC_ALIGNDOWN(l->l_data_start);
  142. /* Usually the test segment and data segment of a loaded library has
  143. a gap between them. Need to fill the hole with a empty area */
  144. if (begin_hole < end_hole) {
  145. void * addr = begin_hole;
  146. _DkVirtualMemoryAlloc(&addr, end_hole - begin_hole,
  147. PAL_ALLOC_RESERVE, PAL_PROT_NONE);
  148. }
  149. /* Set up debugging before the debugger is notified for the first time. */
  150. if (l->l_info[DT_DEBUG] != NULL)
  151. l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) &pal_r_debug;
  152. l->l_prev = l->l_next = NULL;
  153. loaded_libraries = l;
  154. _DkDebugAddMap(l);
  155. }
  156. #if USE_VDSO_GETTIME == 1
  157. void setup_vdso_map (ElfW(Addr) addr)
  158. {
  159. const ElfW(Ehdr) * header = (void *) addr;
  160. struct link_map * l = new_elf_object("vdso", OBJECT_RTLD);
  161. l->l_addr = addr;
  162. l->l_entry = header->e_entry;
  163. l->l_phdr = (void *) (addr + header->e_phoff);
  164. l->l_phnum = header->e_phnum;
  165. l->l_relocated = true;
  166. l->l_soname = "libpal.so";
  167. ElfW(Addr) load_offset = 0;
  168. const ElfW(Phdr) * ph;
  169. for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
  170. switch (ph->p_type) {
  171. case PT_LOAD:
  172. load_offset = addr + (ElfW(Addr)) ph->p_offset
  173. - (ElfW(Addr)) ph->p_vaddr;
  174. break;
  175. case PT_DYNAMIC:
  176. l->l_real_ld = l->l_ld = (void *) addr + ph->p_offset;
  177. l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
  178. break;
  179. }
  180. ElfW(Dyn) local_dyn[4];
  181. int ndyn = 0;
  182. ElfW(Dyn) * dyn;
  183. for (dyn = l->l_ld ; dyn < &l->l_ld[l->l_ldnum]; ++dyn)
  184. switch(dyn->d_tag) {
  185. case DT_STRTAB:
  186. case DT_SYMTAB:
  187. local_dyn[ndyn] = *dyn;
  188. local_dyn[ndyn].d_un.d_ptr += load_offset;
  189. l->l_info[dyn->d_tag] = &local_dyn[ndyn++];
  190. break;
  191. case DT_HASH: {
  192. ElfW(Word) * h = (ElfW(Word) *) (D_PTR(dyn) + load_offset);
  193. l->l_nbuckets = h[0];
  194. l->l_buckets = &h[2];
  195. l->l_chain = &h[l->l_nbuckets + 2];
  196. break;
  197. }
  198. case DT_VERSYM:
  199. case DT_VERDEF:
  200. local_dyn[ndyn] = *dyn;
  201. local_dyn[ndyn].d_un.d_ptr += load_offset;
  202. l->l_info[VERSYMIDX(dyn->d_tag)] = &local_dyn[ndyn++];
  203. break;
  204. }
  205. #if USE_CLOCK_GETTIME == 1
  206. const char * gettime = "__vdso_clock_gettime";
  207. #else
  208. const char * gettime = "__vdso_gettimeofday";
  209. #endif
  210. uint_fast32_t fast_hash = elf_fast_hash(gettime);
  211. long int hash = elf_hash(gettime);
  212. ElfW(Sym) * sym = NULL;
  213. sym = do_lookup_map(NULL, gettime, fast_hash, hash, l);
  214. if (sym)
  215. #if USE_CLOCK_GETTIME == 1
  216. __vdso_clock_gettime =
  217. #else
  218. __vdso_gettimeofday =
  219. #endif
  220. (void *) (load_offset + sym->st_value);
  221. free(l);
  222. }
  223. #endif
  224. ElfW(Addr) resolve_rtld (const char * sym_name)
  225. {
  226. /* We are not using this, because in Linux we can rely on
  227. rtld_map to directly lookup symbols */
  228. return 0;
  229. }