pal_rtld.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * pal_rtld.h
  15. */
  16. #ifndef PAL_RTLD_H
  17. #define PAL_RTLD_H
  18. #include "pal_internal.h"
  19. #include "pal_error.h"
  20. #include "api.h"
  21. #include <sysdeps/generic/ldsodefs.h>
  22. #include <elf/elf.h>
  23. #ifndef DT_THISPROCNUM
  24. # define DT_THISPROCNUM 0
  25. #endif
  26. typedef ElfW(Word) Elf_Symndx;
  27. /* Structure describing a loaded shared object. The `l_next' and `l_prev'
  28. members form a chain of all the shared objects loaded at startup.
  29. These data structures exist in space used by the run-time dynamic linker;
  30. modifying them may have disastrous results.
  31. This data structure might change in future, if necessary. User-level
  32. programs must avoid defining objects of this type. */
  33. /* This is a simplified link_map structure */
  34. struct link_map {
  35. /* These first few members are part of the protocol with the debugger.
  36. This is the same format used in SVR4. */
  37. ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
  38. const char * l_name; /* Absolute file name object was found in. */
  39. ElfW(Dyn) * l_real_ld; /* Dynamic section of the shared object. */
  40. struct link_map * l_next, * l_prev; /* Chain of loaded objects. */
  41. /* All following members are internal to the dynamic linker.
  42. They may change without notice. */
  43. enum object_type l_type;
  44. ElfW(Dyn) * l_ld;
  45. ElfW(Dyn) * l_info[DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM
  46. + DT_EXTRANUM + DT_VALNUM + DT_ADDRNUM];
  47. const ElfW(Phdr) * l_phdr; /* Pointer to program header table in core. */
  48. ElfW(Addr) l_entry; /* Entry point location. */
  49. ElfW(Half) l_phnum; /* Number of program header entries. */
  50. ElfW(Half) l_ldnum; /* Number of dynamic segment entries. */
  51. /* Start and finish of memory map for this object. l_map_start
  52. need not be the same as l_addr. */
  53. ElfW(Addr) l_map_start, l_map_end;
  54. /* Information used to change permission after the relocations are
  55. done. */
  56. ElfW(Addr) l_relro_addr;
  57. int l_relro_size;
  58. Elf_Symndx l_nbuckets;
  59. /* For DT_HASH */
  60. const Elf_Symndx *l_buckets;
  61. const Elf_Symndx *l_chain;
  62. /* For DT_GNU_HASH */
  63. Elf32_Word l_gnu_bitmask_idxbits;
  64. Elf32_Word l_gnu_shift;
  65. const ElfW(Addr) * l_gnu_bitmask;
  66. const Elf32_Word * l_gnu_buckets;
  67. const Elf32_Word * l_gnu_chain_zero;
  68. #if CACHE_LOADED_BINARIES == 1
  69. #define NRELOCS 64
  70. ElfW(Addr) * relocs[NRELOCS];
  71. int nrelocs;
  72. #endif
  73. };
  74. struct link_gdb_map {
  75. /* These first few members are part of the protocol with the debugger.
  76. This is the same format used in SVR4. */
  77. ElfW(Addr) l_addr; /* Base address shared object is loaded at. */
  78. const char * l_name; /* Absolute file name object was found in. */
  79. ElfW(Dyn) * l_ld; /* Dynamic section of the shared object. */
  80. struct link_map * l_next, * l_prev; /* Chain of loaded objects. */
  81. };
  82. extern struct link_map * loaded_maps;
  83. extern struct link_map * rtld_map;
  84. extern struct link_map * exec_map;
  85. /* Some systems link their relocatable objects for another base address
  86. than 0. We want to know the base address for these such that we can
  87. subtract this address from the segment addresses during mapping.
  88. This results in a more efficient address space usage. Defaults to
  89. zero for almost all systems. */
  90. #ifndef MAP_BASE_ADDR
  91. # define MAP_BASE_ADDR(l) 0
  92. #endif
  93. /* Handle situations where we have a preferred location in memory for
  94. the shared objects. */
  95. #ifdef ELF_PREFERRED_ADDRESS_DATA
  96. ELF_PREFERRED_ADDRESS_DATA;
  97. #endif
  98. #ifndef ELF_PREFERRED_ADDRESS
  99. # define ELF_PREFERRED_ADDRESS(loader, maplength, mapstartpref) (mapstartpref)
  100. #endif
  101. #ifndef ELF_FIXED_ADDRESS
  102. # define ELF_FIXED_ADDRESS(loader, mapstart) ((void)0)
  103. #endif
  104. #ifndef VERSYMIDX
  105. # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
  106. #endif
  107. #ifndef VALIDX
  108. # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
  109. + DT_EXTRANUM + DT_VALTAGIDX(tag))
  110. #endif
  111. #include <host_endian.h>
  112. #if __BYTE_ORDER == __BIG_ENDIAN
  113. # define byteorder ELFDATA2MSB
  114. #elif __BYTE_ORDER == __LITTLE_ENDIAN
  115. # define byteorder ELFDATA2LSB
  116. #else
  117. # error "Unknown __BYTE_ORDER " __BYTE_ORDER
  118. # define byteorder ELFDATANONE
  119. #endif
  120. #if __WORDSIZE == 32
  121. # define FILEBUF_SIZE 512
  122. #else
  123. # define FILEBUF_SIZE 832
  124. #endif
  125. struct link_map *
  126. new_elf_object (const char * realname, enum object_type type);
  127. void free_elf_object (struct link_map * map);
  128. static inline uint_fast32_t elf_fast_hash (const char *s)
  129. {
  130. uint_fast32_t h = 5381;
  131. for (unsigned char c = *s; c != '\0'; c = *++s)
  132. h = h * 33 + c;
  133. return h & 0xffffffff;
  134. }
  135. unsigned long int elf_hash (const char *name_arg);
  136. ElfW(Sym) *
  137. do_lookup_map (ElfW(Sym) * ref, const char * undef_name,
  138. const uint_fast32_t hash, unsigned long int elf_hash,
  139. const struct link_map * map);
  140. /* for GDB debugging */
  141. void _DkDebugAddMap (struct link_map * map);
  142. void _DkDebugDelMap (struct link_map * map);
  143. noreturn void start_execution(const char** arguments, const char** environs);
  144. #endif /* PAL_RTLD_H */