db_rtld.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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_freebsd_defs.h"
  23. #include "pal.h"
  24. #include "pal_internal.h"
  25. #include "pal_freebsd.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 <dlfcn.h>
  34. #include "elf-x86_64.h"
  35. /* This structure communicates dl state to the debugger. The debugger
  36. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  37. a statically-linked program there is no dynamic section for the debugger
  38. to examine and it looks for this particular symbol name. */
  39. struct r_debug pal_r_debug =
  40. { 1, NULL, &pal_r_debug_state, RT_CONSISTENT, };
  41. /* This function exists solely to have a breakpoint set on it by the
  42. debugger. The debugger is supposed to find this function's address by
  43. examining the r_brk member of struct r_debug, but GDB 4.15 in fact looks
  44. for this particular symbol name in the PT_INTERP file. */
  45. /* The special symbol name is set as breakpoint in gdb */
  46. void __attribute__((noinline))
  47. pal_r_debug_state (struct r_debug * rd, struct link_gdb_map * map)
  48. {
  49. if (pal_sec.r_debug_state)
  50. pal_sec.r_debug_state(rd, map);
  51. asm volatile ("" ::: "memory");
  52. }
  53. extern __typeof(pal_r_debug_state) r_debug_state
  54. __attribute ((alias ("pal_r_debug_state")));
  55. void _DkDebugAddMap (struct link_map * map)
  56. {
  57. struct r_debug * dbg = pal_sec.r_debug ? : &pal_r_debug;
  58. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  59. struct link_map ** prev = &dbg->r_map, * last = NULL,
  60. * tmp = *prev;
  61. while (tmp) {
  62. if (tmp->l_addr == map->l_addr &&
  63. tmp->l_ld == map->l_ld &&
  64. !memcmp(tmp->l_name, map->l_name, len))
  65. return;
  66. last = tmp;
  67. tmp = *(prev = &last->l_next);
  68. }
  69. struct link_gdb_map * m = malloc(sizeof(struct link_gdb_map) + len);
  70. if (!m)
  71. return;
  72. if (len) {
  73. m->l_name = (char *) m + sizeof(struct link_gdb_map);
  74. memcpy((void *) m->l_name, map->l_name, len);
  75. } else {
  76. m->l_name = NULL;
  77. }
  78. m->l_addr = map->l_addr;
  79. m->l_ld = map->l_real_ld;
  80. dbg->r_state = RT_ADD;
  81. pal_r_debug_state(dbg, m);
  82. *prev = (struct link_map *) m;
  83. m->l_prev = last;
  84. m->l_next = NULL;
  85. dbg->r_state = RT_CONSISTENT;
  86. pal_r_debug_state(dbg, NULL);
  87. }
  88. void _DkDebugDelMap (struct link_map * map)
  89. {
  90. struct r_debug * dbg = pal_sec.r_debug ? : &pal_r_debug;
  91. int len = map->l_name ? strlen(map->l_name) + 1 : 0;
  92. struct link_gdb_map ** prev = (void *) &dbg->r_map;
  93. struct link_gdb_map * last = NULL;
  94. struct link_gdb_map * t = (void *) dbg->r_map, * m = NULL;
  95. while (t) {
  96. if (t->l_addr == map->l_addr &&
  97. t->l_ld == map->l_ld &&
  98. !memcmp(t->l_name, map->l_name, len)) {
  99. m = t;
  100. break;
  101. }
  102. last = t;
  103. prev = (void *) last->l_next;
  104. t = *prev;
  105. }
  106. if (!m)
  107. return;
  108. dbg->r_state = RT_DELETE;
  109. pal_r_debug_state(dbg, m);
  110. if (last)
  111. last->l_next = m->l_next;
  112. else
  113. dbg->r_map = m->l_next;
  114. if (m->l_next)
  115. m->l_next->l_prev = (void *) last;
  116. free(m);
  117. dbg->r_state = RT_CONSISTENT;
  118. pal_r_debug_state(dbg, NULL);
  119. }
  120. extern void setup_elf_hash (struct link_map *map);
  121. void setup_pal_map (struct link_map * pal_map)
  122. {
  123. const ElfW(Ehdr) * header = (void *) pal_map->l_addr;
  124. pal_map->l_real_ld = pal_map->l_ld = (void *) elf_machine_dynamic();
  125. pal_map->l_type = OBJECT_RTLD;
  126. pal_map->l_entry = header->e_entry;
  127. pal_map->l_phdr = (void *) (pal_map->l_addr + header->e_phoff);
  128. pal_map->l_phnum = header->e_phnum;
  129. setup_elf_hash(pal_map);
  130. _DkDebugAddMap(pal_map);
  131. pal_map->l_prev = pal_map->l_next = NULL;
  132. loaded_maps = pal_map;
  133. }