db_rtld.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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 "elf-x86_64.h"
  36. /* This structure communicates dl state to the debugger. The debugger
  37. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  38. a statically-linked program there is no dynamic section for the debugger
  39. to examine and it looks for this particular symbol name. */
  40. struct r_debug pal_r_debug =
  41. { 1, NULL, (ElfW(Addr)) &pal_dl_debug_state, RT_CONSISTENT, 0 };
  42. extern __typeof(pal_r_debug) _r_debug
  43. __attribute ((alias ("pal_r_debug")));
  44. /* This function exists solely to have a breakpoint set on it by the
  45. debugger. The debugger is supposed to find this function's address by
  46. examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
  47. for this particular symbol name in the PT_INTERP file. */
  48. /* The special symbol name is set as breakpoint in gdb */
  49. void __attribute__((noinline)) pal_dl_debug_state (void)
  50. {
  51. if (pal_sec._dl_debug_state)
  52. pal_sec._dl_debug_state();
  53. }
  54. extern __typeof(pal_dl_debug_state) _dl_debug_state
  55. __attribute ((alias ("pal_dl_debug_state")));
  56. void _DkDebugAddMap (struct link_map * map)
  57. {
  58. #ifdef DEBUG
  59. struct r_debug * dbg = pal_sec._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. #endif
  90. }
  91. void _DkDebugDelMap (struct link_map * map)
  92. {
  93. #ifdef DEBUG
  94. struct r_debug * dbg = pal_sec._r_debug ? : &pal_r_debug;
  95. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  96. struct link_map ** prev = &dbg->r_map, * last = NULL,
  97. * tmp = *prev, * found = NULL;
  98. while (tmp) {
  99. if (tmp->l_addr == map->l_addr &&
  100. tmp->l_ld == map->l_ld &&
  101. !memcmp(tmp->l_name, map->l_name, len)) {
  102. found = tmp;
  103. break;
  104. }
  105. last = tmp;
  106. tmp = *(prev = &last->l_next);
  107. }
  108. if (!found)
  109. return;
  110. dbg->r_state = RT_DELETE;
  111. pal_dl_debug_state();
  112. if (last)
  113. last->l_next = tmp->l_next;
  114. else
  115. dbg->r_map = tmp->l_next;
  116. if (tmp->l_next)
  117. tmp->l_next->l_prev = last;
  118. free(tmp);
  119. dbg->r_state = RT_CONSISTENT;
  120. pal_dl_debug_state();
  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
  204. ElfW(Addr) resolve_rtld (const char * sym_name)
  205. {
  206. /* We are not using this, because in Linux we can rely on
  207. rtld_map to directly lookup symbols */
  208. return 0;
  209. }