db_rtld.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_freebsd_defs.h"
  25. #include "pal.h"
  26. #include "pal_internal.h"
  27. #include "pal_freebsd.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, &pal_r_debug_state, RT_CONSISTENT, };
  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))
  51. pal_r_debug_state (struct r_debug * rd, struct link_gdb_map * map)
  52. {
  53. if (pal_sec.r_debug_state)
  54. pal_sec.r_debug_state(rd, map);
  55. asm volatile ("" ::: "memory");
  56. }
  57. extern __typeof(pal_r_debug_state) r_debug_state
  58. __attribute ((alias ("pal_r_debug_state")));
  59. void _DkDebugAddMap (struct link_map * map)
  60. {
  61. struct r_debug * dbg = pal_sec.r_debug ? : &pal_r_debug;
  62. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  63. struct link_map ** prev = &dbg->r_map, * last = NULL,
  64. * tmp = *prev;
  65. while (tmp) {
  66. if (tmp->l_addr == map->l_addr &&
  67. tmp->l_ld == map->l_ld &&
  68. !memcmp(tmp->l_name, map->l_name, len))
  69. return;
  70. last = tmp;
  71. tmp = *(prev = &last->l_next);
  72. }
  73. struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
  74. if (!m)
  75. return;
  76. if (len) {
  77. m->l_name = (char *) m + sizeof(struct link_gdb_map);
  78. memcpy((void *) m->l_name, map->l_name, len);
  79. } else {
  80. m->l_name = NULL;
  81. }
  82. m->l_addr = map->l_addr;
  83. m->l_ld = map->l_real_ld;
  84. dbg->r_state = RT_ADD;
  85. pal_r_debug_state(dbg, m);
  86. *prev = (struct link_map *) m;
  87. m->l_prev = last;
  88. m->l_next = NULL;
  89. dbg->r_state = RT_CONSISTENT;
  90. pal_r_debug_state(dbg, NULL);
  91. }
  92. void _DkDebugDelMap (struct link_map * map)
  93. {
  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_gdb_map ** prev = (void *) &dbg->r_map;
  97. struct link_gdb_map * last = NULL;
  98. struct link_gdb_map * t = (void *) dbg->r_map, * m = NULL;
  99. while (t) {
  100. if (t->l_addr == map->l_addr &&
  101. t->l_ld == map->l_ld &&
  102. !memcmp(t->l_name, map->l_name, len)) {
  103. m = t;
  104. break;
  105. }
  106. last = t;
  107. prev = (void *) last->l_next;
  108. t = *prev;
  109. }
  110. if (!m)
  111. return;
  112. dbg->r_state = RT_DELETE;
  113. pal_r_debug_state(dbg, m);
  114. if (last)
  115. last->l_next = m->l_next;
  116. else
  117. dbg->r_map = m->l_next;
  118. if (m->l_next)
  119. m->l_next->l_prev = (void *) last;
  120. free(m);
  121. dbg->r_state = RT_CONSISTENT;
  122. pal_r_debug_state(dbg, NULL);
  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. ElfW(Addr) resolve_rtld (const char * sym_name)
  139. {
  140. /* We are not using this, because in Linux we can rely on
  141. rtld_map to directly lookup symbols */
  142. return 0;
  143. }