db_rtld.c 5.7 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_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. void _DkDebugAddMap (struct link_map * map)
  37. {
  38. const ElfW(Ehdr) * ehdr = (void *) map->l_map_start;
  39. int shdrsz = sizeof(ElfW(Shdr)) * ehdr->e_shnum;
  40. ElfW(Shdr) * shdr = NULL;
  41. ElfW(Phdr) * phdr = (void *) (map->l_map_start + ehdr->e_phoff);
  42. const ElfW(Phdr) * ph;
  43. int fd = ocall_open(map->l_name, O_RDONLY, 0);
  44. if (fd < 0)
  45. return;
  46. for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
  47. if (ph->p_type == PT_LOAD &&
  48. ehdr->e_shoff >= ph->p_offset &&
  49. ehdr->e_shoff < ph->p_offset + ph->p_filesz) {
  50. shdr = (void *) map->l_addr + ph->p_vaddr +
  51. (ehdr->e_shoff - ph->p_offset);
  52. break;
  53. }
  54. if (!shdr) {
  55. shdr = __alloca(shdrsz);
  56. unsigned long s = ALLOC_ALIGNDOWN(ehdr->e_shoff);
  57. unsigned long e = ALLOC_ALIGNUP(ehdr->e_shoff + shdrsz);
  58. void * umem;
  59. ocall_map_untrusted(fd, s, e - s, PROT_READ, &umem);
  60. memcpy(shdr, umem + ehdr->e_shoff - s, shdrsz);
  61. ocall_unmap_untrusted(umem, e - s);
  62. }
  63. ElfW(Shdr) * shdrend = (void *) shdr + shdrsz;
  64. int shstroff = shdr[ehdr->e_shstrndx].sh_offset;
  65. size_t shstrsz = shdr[ehdr->e_shstrndx].sh_size;
  66. const char * shstrtab = NULL;
  67. for (ph = phdr; ph < &phdr[ehdr->e_phnum]; ++ph)
  68. if (ph->p_type == PT_LOAD &&
  69. shstroff >= ph->p_offset &&
  70. shstroff < ph->p_offset + ph->p_filesz) {
  71. shstrtab = (void *) map->l_addr + ph->p_vaddr +
  72. (shstroff - ph->p_offset);
  73. break;
  74. }
  75. if (!shstrtab) {
  76. shstrtab = __alloca(shstrsz);
  77. unsigned long s = ALLOC_ALIGNDOWN(shstroff);
  78. unsigned long e = ALLOC_ALIGNUP(shstroff + shstrsz);
  79. void * umem;
  80. ocall_map_untrusted(fd, s, e - s, PROT_READ, &umem);
  81. memcpy((void *) shstrtab, umem + shstroff - s, shstrsz);
  82. ocall_unmap_untrusted(umem, e - s);
  83. }
  84. ocall_close(fd);
  85. ElfW(Addr) text_addr = 0;
  86. for (ElfW(Shdr) * s = shdr ; s < shdrend ; s++)
  87. if (strcmp_static(shstrtab + s->sh_name, ".text")) {
  88. text_addr = map->l_addr + s->sh_addr;
  89. break;
  90. }
  91. if (!text_addr)
  92. return;
  93. #define BUFFER_LENGTH 4096
  94. char buffer[BUFFER_LENGTH], * ptr = buffer;
  95. snprintf(ptr, BUFFER_LENGTH - (ptr - buffer),
  96. "add-symbol-file %s 0x%016llx -readnow", map->l_name, text_addr);
  97. ptr += strlen(ptr);
  98. for (ElfW(Shdr) * s = shdr ; s < shdrend ; s++) {
  99. if (!s->sh_name || !s->sh_addr)
  100. continue;
  101. if (strcmp_static(shstrtab + s->sh_name, ".text"))
  102. continue;
  103. if (s->sh_type == SHT_NULL)
  104. continue;
  105. if (strpartcmp_static(shstrtab + s->sh_name, ".debug_"))
  106. continue;
  107. snprintf(ptr, BUFFER_LENGTH - (ptr - buffer),
  108. " -s %s 0x%016llx", shstrtab + s->sh_name,
  109. map->l_addr + s->sh_addr);
  110. ptr += strlen(ptr);
  111. }
  112. ocall_load_debug(buffer);
  113. }
  114. void _DkDebugDelMap (struct link_map * map)
  115. {
  116. char buffer[BUFFER_LENGTH];
  117. snprintf(buffer, BUFFER_LENGTH, "remove-symbol-file %s", map->l_name);
  118. ocall_load_debug(buffer);
  119. }
  120. void setup_elf_hash (struct link_map *map);
  121. extern void * section_text, * section_rodata, * section_dynamic,
  122. * section_data, * section_bss;
  123. void setup_pal_map (struct link_map * pal_map)
  124. {
  125. const ElfW(Ehdr) * header = (void *) pal_map->l_addr;
  126. pal_map->l_real_ld = pal_map->l_ld = (void *) elf_machine_dynamic();
  127. pal_map->l_type = OBJECT_RTLD;
  128. pal_map->l_entry = header->e_entry;
  129. pal_map->l_phdr = (void *) (pal_map->l_addr + header->e_phoff);
  130. pal_map->l_phnum = header->e_phnum;
  131. setup_elf_hash(pal_map);
  132. char buffer[BUFFER_LENGTH];
  133. snprintf(buffer, BUFFER_LENGTH,
  134. "add-symbol-file %s 0x%016llx -readnow -s .rodata 0x%016llx "
  135. "-s .dynamic 0x%016llx -s .data 0x%016llx -s .bss 0x%016llx",
  136. pal_map->l_name,
  137. &section_text, &section_rodata, &section_dynamic,
  138. &section_data, &section_bss);
  139. ocall_load_debug(buffer);
  140. pal_map->l_prev = pal_map->l_next = NULL;
  141. loaded_maps = pal_map;
  142. }
  143. ElfW(Addr) resolve_rtld (const char * sym_name)
  144. {
  145. /* We are not using this, because in Linux we can rely on
  146. rtld_map to directly lookup symbols */
  147. return 0;
  148. }