pal_rtld.h 5.9 KB

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