db_rtld.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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._dl_debug_state)
  53. pal_sec._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. #ifdef DEBUG
  60. struct r_debug * dbg = pal_sec._r_debug ? : &pal_r_debug;
  61. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  62. struct link_map ** prev = &dbg->r_map, * last = NULL,
  63. * tmp = *prev;
  64. while (tmp) {
  65. if (tmp->l_addr == map->l_addr &&
  66. tmp->l_ld == map->l_ld &&
  67. !memcmp(tmp->l_name, map->l_name, len))
  68. return;
  69. last = tmp;
  70. tmp = *(prev = &last->l_next);
  71. }
  72. struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
  73. if (!m)
  74. return;
  75. if (len) {
  76. m->l_name = (char *) m + sizeof(struct link_gdb_map);
  77. memcpy((void *) m->l_name, map->l_name, len);
  78. } else {
  79. m->l_name = NULL;
  80. }
  81. m->l_addr = map->l_addr;
  82. m->l_ld = map->l_real_ld;
  83. dbg->r_state = RT_ADD;
  84. pal_dl_debug_state();
  85. *prev = (struct link_map *) m;
  86. m->l_prev = last;
  87. m->l_next = NULL;
  88. dbg->r_state = RT_CONSISTENT;
  89. pal_dl_debug_state();
  90. #endif
  91. }
  92. void _DkDebugDelMap (struct link_map * map)
  93. {
  94. #ifdef DEBUG
  95. struct r_debug * dbg = pal_sec._r_debug ? : &pal_r_debug;
  96. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  97. struct link_map ** prev = &dbg->r_map, * last = NULL,
  98. * tmp = *prev, * found = NULL;
  99. while (tmp) {
  100. if (tmp->l_addr == map->l_addr &&
  101. tmp->l_ld == map->l_ld &&
  102. !memcmp(tmp->l_name, map->l_name, len)) {
  103. found = tmp;
  104. break;
  105. }
  106. last = tmp;
  107. tmp = *(prev = &last->l_next);
  108. }
  109. if (!found)
  110. return;
  111. dbg->r_state = RT_DELETE;
  112. pal_dl_debug_state();
  113. if (last)
  114. last->l_next = tmp->l_next;
  115. else
  116. dbg->r_map = tmp->l_next;
  117. if (tmp->l_next)
  118. tmp->l_next->l_prev = last;
  119. free(tmp);
  120. dbg->r_state = RT_CONSISTENT;
  121. pal_dl_debug_state();
  122. #endif
  123. }
  124. extern void setup_elf_hash (struct link_map *map);
  125. void setup_pal_map (struct link_map * pal_map)
  126. {
  127. const ElfW(Ehdr) * header = (void *) pal_map->l_addr;
  128. pal_map->l_real_ld = pal_map->l_ld = (void *) elf_machine_dynamic();
  129. pal_map->l_type = OBJECT_RTLD;
  130. pal_map->l_entry = header->e_entry;
  131. pal_map->l_phdr = (void *) (pal_map->l_addr + header->e_phoff);
  132. pal_map->l_phnum = header->e_phnum;
  133. setup_elf_hash(pal_map);
  134. _DkDebugAddMap(pal_map);
  135. pal_map->l_prev = pal_map->l_next = NULL;
  136. loaded_maps = pal_map;
  137. }
  138. #if USE_VDSO_GETTIME == 1
  139. void setup_vdso_map (ElfW(Addr) addr)
  140. {
  141. const ElfW(Ehdr) * header = (void *) addr;
  142. struct link_map vdso_map;
  143. memset(&vdso_map, 0, sizeof(struct link_map));
  144. vdso_map.l_name = "vdso";
  145. vdso_map.l_type = OBJECT_RTLD;
  146. vdso_map.l_addr = addr;
  147. vdso_map.l_entry = header->e_entry;
  148. vdso_map.l_phdr = (void *) (addr + header->e_phoff);
  149. vdso_map.l_phnum = header->e_phnum;
  150. ElfW(Addr) load_offset = 0;
  151. const ElfW(Phdr) * ph;
  152. for (ph = vdso_map.l_phdr; ph < &vdso_map.l_phdr[vdso_map.l_phnum]; ++ph)
  153. switch (ph->p_type) {
  154. case PT_LOAD:
  155. load_offset = addr + (ElfW(Addr)) ph->p_offset
  156. - (ElfW(Addr)) ph->p_vaddr;
  157. break;
  158. case PT_DYNAMIC:
  159. vdso_map.l_real_ld = vdso_map.l_ld = (void *) addr + ph->p_offset;
  160. vdso_map.l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
  161. break;
  162. }
  163. ElfW(Dyn) local_dyn[4];
  164. int ndyn = 0;
  165. ElfW(Dyn) * dyn;
  166. for (dyn = vdso_map.l_ld ; dyn < &vdso_map.l_ld[vdso_map.l_ldnum]; ++dyn)
  167. switch(dyn->d_tag) {
  168. case DT_STRTAB:
  169. case DT_SYMTAB:
  170. local_dyn[ndyn] = *dyn;
  171. local_dyn[ndyn].d_un.d_ptr += load_offset;
  172. vdso_map.l_info[dyn->d_tag] = &local_dyn[ndyn++];
  173. break;
  174. case DT_HASH: {
  175. ElfW(Word) * h = (ElfW(Word) *) (D_PTR(dyn) + load_offset);
  176. vdso_map.l_nbuckets = h[0];
  177. vdso_map.l_buckets = &h[2];
  178. vdso_map.l_chain = &h[vdso_map.l_nbuckets + 2];
  179. break;
  180. }
  181. case DT_VERSYM:
  182. case DT_VERDEF:
  183. local_dyn[ndyn] = *dyn;
  184. local_dyn[ndyn].d_un.d_ptr += load_offset;
  185. vdso_map.l_info[VERSYMIDX(dyn->d_tag)] = &local_dyn[ndyn++];
  186. break;
  187. }
  188. #if USE_CLOCK_GETTIME == 1
  189. const char * gettime = "__vdso_clock_gettime";
  190. #else
  191. const char * gettime = "__vdso_gettimeofday";
  192. #endif
  193. uint_fast32_t fast_hash = elf_fast_hash(gettime);
  194. long int hash = elf_hash(gettime);
  195. ElfW(Sym) * sym = NULL;
  196. sym = do_lookup_map(NULL, gettime, fast_hash, hash, &vdso_map);
  197. if (sym)
  198. #if USE_CLOCK_GETTIME == 1
  199. linux_state.vdso_clock_gettime = (void *) (load_offset + sym->st_value);
  200. #else
  201. linux_state.vdso_gettimeofday = (void *) (load_offset + sym->st_value);
  202. #endif
  203. }
  204. #endif
  205. ElfW(Addr) resolve_rtld (const char * sym_name)
  206. {
  207. /* We are not using this, because in Linux we can rely on
  208. rtld_map to directly lookup symbols */
  209. return 0;
  210. }