db_rtld.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* Copyright (C) 2014 Stony Brook University
  2. This file is part of Graphene Library OS.
  3. Graphene Library OS is free software: you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. Graphene Library OS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /*
  14. * db_rtld.c
  15. *
  16. * This file contains utilities to load ELF binaries into the memory
  17. * and link them against each other.
  18. * The source code in this file is imported and modified from the GNU C
  19. * Library.
  20. */
  21. #include "pal_defs.h"
  22. #include "pal_linux_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_linux.h"
  26. #include "pal_debug.h"
  27. #include "pal_error.h"
  28. #include "pal_security.h"
  29. #include "pal_rtld.h"
  30. #include "api.h"
  31. #include <sysdeps/generic/ldsodefs.h>
  32. #include <elf/elf.h>
  33. #include "elf-x86_64.h"
  34. /* This structure communicates dl state to the debugger. The debugger
  35. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  36. a statically-linked program there is no dynamic section for the debugger
  37. to examine and it looks for this particular symbol name. */
  38. struct r_debug pal_r_debug =
  39. { 1, NULL, (ElfW(Addr)) &pal_dl_debug_state, RT_CONSISTENT, 0 };
  40. /* This function exists solely to have a breakpoint set on it by the
  41. debugger. The debugger is supposed to find this function's address by
  42. examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
  43. for this particular symbol name in the PT_INTERP file. */
  44. /* The special symbol name is set as breakpoint in gdb */
  45. void __attribute__((noinline)) pal_dl_debug_state (void)
  46. {
  47. if (pal_sec._dl_debug_state)
  48. pal_sec._dl_debug_state();
  49. }
  50. extern __typeof(pal_dl_debug_state) _dl_debug_state
  51. __attribute ((alias ("pal_dl_debug_state")));
  52. void _DkDebugAddMap (struct link_map * map)
  53. {
  54. #ifdef DEBUG
  55. struct r_debug * dbg = pal_sec._r_debug ? : &pal_r_debug;
  56. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  57. struct link_map ** prev = &dbg->r_map, * last = NULL,
  58. * tmp = *prev;
  59. while (tmp) {
  60. if (tmp->l_addr == map->l_addr &&
  61. tmp->l_ld == map->l_ld &&
  62. !memcmp(tmp->l_name, map->l_name, len))
  63. return;
  64. last = tmp;
  65. tmp = *(prev = &last->l_next);
  66. }
  67. struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
  68. if (!m)
  69. return;
  70. if (len) {
  71. m->l_name = (char *) m + sizeof(struct link_gdb_map);
  72. memcpy((void *) m->l_name, map->l_name, len);
  73. } else {
  74. m->l_name = NULL;
  75. }
  76. m->l_addr = map->l_addr;
  77. m->l_ld = map->l_real_ld;
  78. dbg->r_state = RT_ADD;
  79. pal_dl_debug_state();
  80. *prev = (struct link_map *) m;
  81. m->l_prev = last;
  82. m->l_next = NULL;
  83. dbg->r_state = RT_CONSISTENT;
  84. pal_dl_debug_state();
  85. #else
  86. __UNUSED(map);
  87. #endif
  88. }
  89. void _DkDebugDelMap (struct link_map * map)
  90. {
  91. #ifdef DEBUG
  92. struct r_debug * dbg = pal_sec._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. #else
  120. __UNUSED(map);
  121. #endif
  122. }
  123. extern void setup_elf_hash (struct link_map *map);
  124. void setup_pal_map (struct link_map * pal_map)
  125. {
  126. const ElfW(Ehdr) * header = (void *) pal_map->l_addr;
  127. pal_map->l_real_ld = pal_map->l_ld = (void *) elf_machine_dynamic();
  128. pal_map->l_type = OBJECT_RTLD;
  129. pal_map->l_entry = header->e_entry;
  130. pal_map->l_phdr = (void *) (pal_map->l_addr + header->e_phoff);
  131. pal_map->l_phnum = header->e_phnum;
  132. setup_elf_hash(pal_map);
  133. _DkDebugAddMap(pal_map);
  134. pal_map->l_prev = pal_map->l_next = NULL;
  135. loaded_maps = pal_map;
  136. }
  137. #if USE_VDSO_GETTIME == 1
  138. void setup_vdso_map (ElfW(Addr) addr)
  139. {
  140. const ElfW(Ehdr) * header = (void *) addr;
  141. struct link_map vdso_map;
  142. memset(&vdso_map, 0, sizeof(struct link_map));
  143. vdso_map.l_name = "vdso";
  144. vdso_map.l_type = OBJECT_RTLD;
  145. vdso_map.l_addr = addr;
  146. vdso_map.l_entry = header->e_entry;
  147. vdso_map.l_phdr = (void *) (addr + header->e_phoff);
  148. vdso_map.l_phnum = header->e_phnum;
  149. ElfW(Addr) load_offset = 0;
  150. const ElfW(Phdr) * ph;
  151. for (ph = vdso_map.l_phdr; ph < &vdso_map.l_phdr[vdso_map.l_phnum]; ++ph)
  152. switch (ph->p_type) {
  153. case PT_LOAD:
  154. load_offset = addr + (ElfW(Addr)) ph->p_offset
  155. - (ElfW(Addr)) ph->p_vaddr;
  156. break;
  157. case PT_DYNAMIC:
  158. vdso_map.l_real_ld = vdso_map.l_ld = (void *) addr + ph->p_offset;
  159. vdso_map.l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
  160. break;
  161. }
  162. ElfW(Dyn) local_dyn[4];
  163. int ndyn = 0;
  164. ElfW(Dyn) * dyn;
  165. for (dyn = vdso_map.l_ld ; dyn < &vdso_map.l_ld[vdso_map.l_ldnum]; ++dyn)
  166. switch(dyn->d_tag) {
  167. case DT_STRTAB:
  168. case DT_SYMTAB:
  169. local_dyn[ndyn] = *dyn;
  170. local_dyn[ndyn].d_un.d_ptr += load_offset;
  171. vdso_map.l_info[dyn->d_tag] = &local_dyn[ndyn++];
  172. break;
  173. case DT_HASH: {
  174. ElfW(Word) * h = (ElfW(Word) *) (D_PTR(dyn) + load_offset);
  175. vdso_map.l_nbuckets = h[0];
  176. vdso_map.l_buckets = &h[2];
  177. vdso_map.l_chain = &h[vdso_map.l_nbuckets + 2];
  178. break;
  179. }
  180. case DT_VERSYM:
  181. case DT_VERDEF:
  182. local_dyn[ndyn] = *dyn;
  183. local_dyn[ndyn].d_un.d_ptr += load_offset;
  184. vdso_map.l_info[VERSYMIDX(dyn->d_tag)] = &local_dyn[ndyn++];
  185. break;
  186. }
  187. #if USE_CLOCK_GETTIME == 1
  188. const char * gettime = "__vdso_clock_gettime";
  189. #else
  190. const char * gettime = "__vdso_gettimeofday";
  191. #endif
  192. uint_fast32_t fast_hash = elf_fast_hash(gettime);
  193. long int hash = elf_hash(gettime);
  194. ElfW(Sym) * sym = NULL;
  195. sym = do_lookup_map(NULL, gettime, fast_hash, hash, &vdso_map);
  196. if (sym)
  197. #if USE_CLOCK_GETTIME == 1
  198. linux_state.vdso_clock_gettime = (void *) (load_offset + sym->st_value);
  199. #else
  200. linux_state.vdso_gettimeofday = (void *) (load_offset + sym->st_value);
  201. #endif
  202. }
  203. #endif