db_rtld.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. struct link_map * rtld_map = NULL;
  38. extern void setup_elf_hash (struct link_map *map);
  39. void setup_pal_map (const char * realname, ElfW(Dyn) ** dyn, ElfW(Addr) addr)
  40. {
  41. assert (loaded_libraries == NULL);
  42. const ElfW(Ehdr) * header = (void *) addr;
  43. struct link_map * l = new_elf_object(realname, OBJECT_RTLD);
  44. memcpy(l->l_info, dyn, sizeof(l->l_info));
  45. l->l_real_ld = l->l_ld = (void *) elf_machine_dynamic();
  46. l->l_addr = addr;
  47. l->l_entry = header->e_entry;
  48. l->l_phdr = (void *) (addr + header->e_phoff);
  49. l->l_phnum = header->e_phnum;
  50. l->l_relocated = true;
  51. l->l_lookup_symbol = true;
  52. l->l_soname = "libpal.so";
  53. l->l_text_start = (ElfW(Addr)) &text_start;
  54. l->l_text_end = (ElfW(Addr)) &text_end;
  55. l->l_data_start = (ElfW(Addr)) &data_start;
  56. l->l_data_end = (ElfW(Addr)) &data_end;
  57. setup_elf_hash(l);
  58. void * begin_hole = (void *) ALLOC_ALIGNUP(l->l_text_end);
  59. void * end_hole = (void *) ALLOC_ALIGNDOWN(l->l_data_start);
  60. /* Usually the test segment and data segment of a loaded library has
  61. a gap between them. Need to fill the hole with a empty area */
  62. if (begin_hole < end_hole) {
  63. void * addr = begin_hole;
  64. _DkVirtualMemoryAlloc(&addr, end_hole - begin_hole,
  65. PAL_ALLOC_RESERVE, PAL_PROT_NONE);
  66. }
  67. /* Set up debugging before the debugger is notified for the first time. */
  68. if (l->l_info[DT_DEBUG] != NULL)
  69. l->l_info[DT_DEBUG]->d_un.d_ptr = (ElfW(Addr)) &pal_r_debug;
  70. l->l_prev = l->l_next = NULL;
  71. rtld_map = l;
  72. loaded_libraries = l;
  73. if (!pal_sec_info._r_debug) {
  74. pal_r_debug.r_version = 1;
  75. pal_r_debug.r_brk = (ElfW(Addr)) &pal_dl_debug_state;
  76. pal_r_debug.r_ldbase = addr;
  77. pal_r_debug.r_map = loaded_libraries;
  78. pal_sec_info._r_debug = &pal_r_debug;
  79. pal_sec_info._dl_debug_state = &pal_dl_debug_state;
  80. } else {
  81. pal_sec_info._r_debug->r_state = RT_ADD;
  82. pal_sec_info._dl_debug_state();
  83. if (pal_sec_info._r_debug->r_map) {
  84. l->l_prev = pal_sec_info._r_debug->r_map;
  85. pal_sec_info._r_debug->r_map->l_next = l;
  86. } else {
  87. pal_sec_info._r_debug->r_map = loaded_libraries;
  88. }
  89. pal_sec_info._r_debug->r_state = RT_CONSISTENT;
  90. pal_sec_info._dl_debug_state();
  91. }
  92. }
  93. #if USE_VDSO_GETTIME == 1
  94. void setup_vdso_map (ElfW(Addr) addr)
  95. {
  96. const ElfW(Ehdr) * header = (void *) addr;
  97. struct link_map * l = new_elf_object("vdso", OBJECT_RTLD);
  98. l->l_addr = addr;
  99. l->l_entry = header->e_entry;
  100. l->l_phdr = (void *) (addr + header->e_phoff);
  101. l->l_phnum = header->e_phnum;
  102. l->l_relocated = true;
  103. l->l_soname = "libpal.so";
  104. ElfW(Addr) load_offset = 0;
  105. const ElfW(Phdr) * ph;
  106. for (ph = l->l_phdr; ph < &l->l_phdr[l->l_phnum]; ++ph)
  107. switch (ph->p_type) {
  108. case PT_LOAD:
  109. load_offset = addr + (ElfW(Addr)) ph->p_offset
  110. - (ElfW(Addr)) ph->p_vaddr;
  111. break;
  112. case PT_DYNAMIC:
  113. l->l_real_ld = l->l_ld = (void *) addr + ph->p_offset;
  114. l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
  115. break;
  116. }
  117. ElfW(Dyn) local_dyn[4];
  118. int ndyn = 0;
  119. ElfW(Dyn) * dyn;
  120. for (dyn = l->l_ld ; dyn < &l->l_ld[l->l_ldnum]; ++dyn)
  121. switch(dyn->d_tag) {
  122. case DT_STRTAB:
  123. case DT_SYMTAB:
  124. local_dyn[ndyn] = *dyn;
  125. local_dyn[ndyn].d_un.d_ptr += load_offset;
  126. l->l_info[dyn->d_tag] = &local_dyn[ndyn++];
  127. break;
  128. case DT_HASH: {
  129. ElfW(Word) * h = (ElfW(Word) *) (D_PTR(dyn) + load_offset);
  130. l->l_nbuckets = h[0];
  131. l->l_buckets = &h[2];
  132. l->l_chain = &h[l->l_nbuckets + 2];
  133. break;
  134. }
  135. case DT_VERSYM:
  136. case DT_VERDEF:
  137. local_dyn[ndyn] = *dyn;
  138. local_dyn[ndyn].d_un.d_ptr += load_offset;
  139. l->l_info[VERSYMIDX(dyn->d_tag)] = &local_dyn[ndyn++];
  140. break;
  141. }
  142. #if USE_CLOCK_GETTIME == 1
  143. const char * gettime = "__vdso_clock_gettime";
  144. #else
  145. const char * gettime = "__vdso_gettimeofday";
  146. #endif
  147. uint_fast32_t fast_hash = elf_fast_hash(gettime);
  148. long int hash = elf_hash(gettime);
  149. ElfW(Sym) * sym = NULL;
  150. sym = do_lookup_map(NULL, gettime, fast_hash, hash, l);
  151. if (sym)
  152. #if USE_CLOCK_GETTIME == 1
  153. __vdso_clock_gettime =
  154. #else
  155. __vdso_gettimeofday =
  156. #endif
  157. (void *) (load_offset + sym->st_value);
  158. free(l);
  159. }
  160. #endif
  161. ElfW(Addr) resolve_map_in_rtld (ElfW(Sym) * ref)
  162. {
  163. /* We are not using this, because in Linux we can rely on
  164. rtld_map to directly lookup symbols */
  165. return 0;
  166. }