pal_rtld.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. * pal_rtld.h
  17. */
  18. #ifndef PAL_RTLD_H
  19. #define PAL_RTLD_H
  20. #include "pal_internal.h"
  21. #include "pal_error.h"
  22. #include "pal_security.h"
  23. #include "api.h"
  24. #include <sysdeps/generic/ldsodefs.h>
  25. #include <elf/elf.h>
  26. #include <bits/dlfcn.h>
  27. #ifndef DT_THISPROCNUM
  28. # define DT_THISPROCNUM 0
  29. #endif
  30. typedef ElfW(Word) Elf_Symndx;
  31. /* Structure describing a loaded shared object. The `l_next' and `l_prev'
  32. members form a chain of all the shared objects loaded at startup.
  33. These data structures exist in space used by the run-time dynamic linker;
  34. modifying them may have disastrous results.
  35. This data structure might change in future, if necessary. User-level
  36. programs must avoid defining objects of this type. */
  37. /* This is a simplified link_map structure */
  38. struct link_map {
  39. /* These first few members are part of the protocol with the debugger.
  40. This is the same format used in SVR4. */
  41. ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
  42. const char * l_name; /* Absolute file name object was found in. */
  43. ElfW(Dyn) * l_real_ld; /* Dynamic section of the shared object. */
  44. struct link_map * l_next, * l_prev; /* Chain of loaded objects. */
  45. /* All following members are internal to the dynamic linker.
  46. They may change without notice. */
  47. enum object_type l_type;
  48. ElfW(Dyn) * l_ld;
  49. char * l_soname;
  50. bool l_relocated;
  51. bool l_lookup_symbol;
  52. ElfW(Dyn) * l_info[DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM
  53. + DT_EXTRANUM + DT_VALNUM + DT_ADDRNUM];
  54. const ElfW(Phdr) * l_phdr; /* Pointer to program header table in core. */
  55. ElfW(Addr) l_entry; /* Entry point location. */
  56. ElfW(Half) l_phnum; /* Number of program header entries. */
  57. ElfW(Half) l_ldnum; /* Number of dynamic segment entries. */
  58. /* End of the executable part of the mapping. */
  59. ElfW(Addr) l_text_start, l_text_end;
  60. ElfW(Addr) l_data_start, l_data_end;
  61. /* Start and finish of memory map for this object. l_map_start
  62. need not be the same as l_addr. */
  63. ElfW(Addr) l_map_start, l_map_end;
  64. /* Information used to change permission after the relocations are
  65. done. */
  66. ElfW(Addr) l_relro_addr;
  67. size_t l_relro_size;
  68. Elf_Symndx l_nbuckets;
  69. /* For DT_HASH */
  70. const Elf_Symndx *l_buckets;
  71. const Elf_Symndx *l_chain;
  72. /* For DT_GNU_HASH */
  73. Elf32_Word l_gnu_bitmask_idxbits;
  74. Elf32_Word l_gnu_shift;
  75. const ElfW(Addr) * l_gnu_bitmask;
  76. const Elf32_Word * l_gnu_buckets;
  77. const Elf32_Word * l_gnu_chain_zero;
  78. };
  79. struct link_gdb_map {
  80. /* These first few members are part of the protocol with the debugger.
  81. This is the same format used in SVR4. */
  82. ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
  83. const char * l_name; /* Absolute file name object was found in. */
  84. ElfW(Dyn) * l_ld; /* Dynamic section of the shared object. */
  85. struct link_map * l_next, * l_prev; /* Chain of loaded objects. */
  86. };
  87. extern struct link_map * loaded_libraries;
  88. extern struct link_map * rtld_map;
  89. extern struct link_map * exec_map;
  90. /* Rendezvous structure used by the run-time dynamic linker to communicate
  91. details of shared object loading to the debugger. If the executable's
  92. dynamic section has a DT_DEBUG element, the run-time linker sets that
  93. element's value to the address where this structure can be found. */
  94. struct r_debug {
  95. int r_version; /* Version number for this protocol. */
  96. struct link_map * r_map; /* Head of the chain of loaded objects. */
  97. /* This is the address of a function internal to the run-time linker,
  98. that will always be called when the linker begins to map in a
  99. library or unmap it, and again when the mapping change is complete.
  100. The debugger can set a breakpoint at this address if it wants to
  101. notice shared object mapping changes. */
  102. ElfW(Addr) r_brk;
  103. enum {
  104. /* This state value describes the mapping change taking place when
  105. the `r_brk' address is called. */
  106. RT_CONSISTENT, /* Mapping change is complete. */
  107. RT_ADD, /* Beginning to add a new object. */
  108. RT_DELETE /* Beginning to remove an object mapping. */
  109. } r_state;
  110. ElfW(Addr) r_ldbase; /* Base address the linker is loaded at. */
  111. };
  112. void pal_dl_debug_state (void);
  113. /* This structure communicates dl state to the debugger. The debugger
  114. normally finds it via the DT_DEBUG entry in the dynamic section, but in
  115. a statically-linked program there is no dynamic section for the debugger
  116. to examine and it looks for this particular symbol name. */
  117. extern struct r_debug pal_r_debug;
  118. /* Some systems link their relocatable objects for another base address
  119. than 0. We want to know the base address for these such that we can
  120. subtract this address from the segment addresses during mapping.
  121. This results in a more efficient address space usage. Defaults to
  122. zero for almost all systems. */
  123. #ifndef MAP_BASE_ADDR
  124. # define MAP_BASE_ADDR(l) 0
  125. #endif
  126. /* Handle situations where we have a preferred location in memory for
  127. the shared objects. */
  128. #ifdef ELF_PREFERRED_ADDRESS_DATA
  129. ELF_PREFERRED_ADDRESS_DATA;
  130. #endif
  131. #ifndef ELF_PREFERRED_ADDRESS
  132. # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
  133. #endif
  134. #ifndef ELF_FIXED_ADDRESS
  135. # define ELF_FIXED_ADDRESS(loader, mapstart) ((void) 0)
  136. #endif
  137. #ifndef VERSYMIDX
  138. # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
  139. #endif
  140. #ifndef VALIDX
  141. # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
  142. + DT_EXTRANUM + DT_VALTAGIDX (tag))
  143. #endif
  144. #include <endian.h>
  145. #if BYTE_ORDER == BIG_ENDIAN
  146. # define byteorder ELFDATA2MSB
  147. #elif BYTE_ORDER == LITTLE_ENDIAN
  148. # define byteorder ELFDATA2LSB
  149. #else
  150. # error "Unknown BYTE_ORDER " BYTE_ORDER
  151. # define byteorder ELFDATANONE
  152. #endif
  153. #if __WORDSIZE == 32
  154. # define FILEBUF_SIZE 512
  155. #else
  156. # define FILEBUF_SIZE 832
  157. #endif
  158. struct link_map *
  159. new_elf_object (const char * realname, enum object_type type);
  160. void free_elf_object (struct link_map * map);
  161. static inline uint_fast32_t elf_fast_hash (const char *s)
  162. {
  163. uint_fast32_t h = 5381;
  164. for (unsigned char c = *s; c != '\0'; c = *++s)
  165. h = h * 33 + c;
  166. return h & 0xffffffff;
  167. }
  168. unsigned long int elf_hash (const char *name_arg);
  169. ElfW(Sym) *
  170. do_lookup_map (ElfW(Sym) * ref, const char * undef_name,
  171. const uint_fast32_t hash, unsigned long int elf_hash,
  172. const struct link_map * map);
  173. /* for GDB debugging */
  174. void _DkDebugAddMap (struct link_map * map);
  175. void _DkDebugDelMap (struct link_map * map);
  176. #endif /* PAL_RTLD_H */