ldsodefs.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* Run-time dynamic linker data structures for loaded ELF shared objects.
  2. Copyright (C) 1995-2009, 2010 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #ifndef _LDSODEFS_H
  17. #define _LDSODEFS_H 1
  18. #include <stdbool.h>
  19. #define __need_size_t
  20. #define __need_NULL
  21. #include <stddef.h>
  22. #include <sys/mman.h>
  23. #include <elf/elf.h>
  24. /* We use this macro to refer to ELF types independent of the native wordsize.
  25. `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
  26. #define ElfW(type) _ElfW (Elf, __ELF_NATIVE_CLASS, type)
  27. #define _ElfW(e,w,t) _ElfW_1 (e, w, _##t)
  28. #define _ElfW_1(e,w,t) e##w##t
  29. /* This symbol refers to the "dynamic structure" in the `.dynamic' section
  30. of whatever module refers to `_DYNAMIC'. So, to find its own
  31. `struct r_debug', a program could do:
  32. for (dyn = _DYNAMIC; dyn->d_tag != DT_NULL; ++dyn)
  33. if (dyn->d_tag == DT_DEBUG)
  34. r_debug = (struct r_debug *) dyn->d_un.d_ptr;
  35. */
  36. extern ElfW(Dyn) _DYNAMIC[];
  37. /* We use this macro to refer to ELF types independent of the native wordsize.
  38. `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */
  39. #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type)
  40. /* We don't like the link_map form ld.so. This macro will be redefined */
  41. #define D_PTR(sym) (sym)->d_un.d_ptr
  42. #if 0 /* Remove this part for now */
  43. /* All references to the value of l_info[DT_PLTGOT],
  44. l_info[DT_STRTAB], l_info[DT_SYMTAB], l_info[DT_RELA],
  45. l_info[DT_REL], l_info[DT_JMPREL], and l_info[VERSYMIDX (DT_VERSYM)]
  46. have to be accessed via the D_PTR macro. The macro is needed since for
  47. most architectures the entry is already relocated - but for some not
  48. and we need to relocate at access time. */
  49. #ifdef DL_RO_DYN_SECTION
  50. # define D_PTR(map, i) ((map)->i->d_un.d_ptr + (map)->l_addr)
  51. #else
  52. # define D_PTR(map, i) (map)->i->d_un.d_ptr
  53. #endif
  54. /* On some architectures a pointer to a function is not just a pointer
  55. to the actual code of the function but rather an architecture
  56. specific descriptor. */
  57. #ifndef ELF_FUNCTION_PTR_IS_SPECIAL
  58. # define DL_SYMBOL_ADDRESS(map, ref) \
  59. (void *) (LOOKUP_VALUE_ADDRESS (map) + ref->st_value)
  60. # define DL_LOOKUP_ADDRESS(addr) ((ElfW(Addr)) (addr))
  61. # define DL_DT_INIT_ADDRESS(map, start) (start)
  62. # define DL_DT_FINI_ADDRESS(map, start) (start)
  63. #endif
  64. #endif
  65. /* On some architectures dladdr can't use st_size of all symbols this way. */
  66. #define DL_ADDR_SYM_MATCH(L, SYM, MATCHSYM, ADDR) \
  67. ((ADDR) >= (L)->l_addr + (SYM)->st_value \
  68. && ((((SYM)->st_shndx == SHN_UNDEF || (SYM)->st_size == 0) \
  69. && (ADDR) == (L)->l_addr + (SYM)->st_value) \
  70. || (ADDR) < (L)->l_addr + (SYM)->st_value + (SYM)->st_size) \
  71. && ((MATCHSYM) == NULL || (MATCHSYM)->st_value < (SYM)->st_value))
  72. /* Unmap a loaded object, called by _dl_close (). */
  73. #ifndef DL_UNMAP_IS_SPECIAL
  74. # define DL_UNMAP(map) \
  75. __munmap ((void *) (map)->l_map_start, \
  76. (map)->l_map_end - (map)->l_map_start)
  77. #endif
  78. /* By default we do not need special support to initialize DSOs loaded
  79. by statically linked binaries. */
  80. #ifndef DL_STATIC_INIT
  81. # define DL_STATIC_INIT(map)
  82. #endif
  83. /* Reloc type classes as returned by elf_machine_type_class().
  84. ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by
  85. some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be
  86. satisfied by any symbol in the executable. Some architectures do
  87. not support copy relocations. In this case we define the macro to
  88. zero so that the code for handling them gets automatically optimized
  89. out. */
  90. #define ELF_RTYPE_CLASS_PLT 1
  91. #ifndef DL_NO_COPY_RELOCS
  92. # define ELF_RTYPE_CLASS_COPY 2
  93. #else
  94. # define ELF_RTYPE_CLASS_COPY 0
  95. #endif
  96. /* ELF uses the PF_x macros to specify the segment permissions, mmap
  97. uses PROT_xxx. In most cases the three macros have the values 1, 2,
  98. and 3 but not in a matching order. The following macros allows
  99. converting from the PF_x values to PROT_xxx values. */
  100. #define PF_TO_PROT \
  101. ((PROT_READ << (PF_R * 4)) \
  102. | (PROT_WRITE << (PF_W * 4)) \
  103. | (PROT_EXEC << (PF_X * 4)) \
  104. | ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) \
  105. | ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) \
  106. | ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) \
  107. | ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
  108. /* For the version handling we need an array with only names and their
  109. hash values. */
  110. struct r_found_version
  111. {
  112. const char *name;
  113. ElfW(Word) hash;
  114. int hidden;
  115. const char *filename;
  116. };
  117. /* We want to cache information about the searches for shared objects. */
  118. enum r_dir_status { unknown, nonexisting, existing };
  119. struct r_search_path_elem
  120. {
  121. /* This link is only used in the `all_dirs' member of `r_search_path'. */
  122. struct r_search_path_elem *next;
  123. /* Strings saying where the definition came from. */
  124. const char *what;
  125. const char *where;
  126. /* Basename for this search path element. The string must end with
  127. a slash character. */
  128. const char *dirname;
  129. size_t dirnamelen;
  130. enum r_dir_status status[0];
  131. };
  132. struct r_strlenpair
  133. {
  134. const char *str;
  135. size_t len;
  136. };
  137. /* A data structure for a simple single linked list of strings. */
  138. struct libname_list
  139. {
  140. const char *name; /* Name requested (before search). */
  141. struct libname_list *next; /* Link to next name for this object. */
  142. int dont_free; /* Flag whether this element should be freed
  143. if the object is not entirely unloaded. */
  144. };
  145. /* Bit masks for the objects which valid callers can come from to
  146. functions with restricted interface. */
  147. enum allowmask
  148. {
  149. allow_libc = 1,
  150. allow_libdl = 2,
  151. allow_libpthread = 4,
  152. allow_ldso = 8
  153. };
  154. /* search loaded objects' symbol tables for a definition of the symbol
  155. referred to by undef. *sym is the symbol table entry containing the
  156. reference; it is replaced with the defining symbol, and the base load
  157. address of the defining object is returned. symbol_scope is a
  158. null-terminated list of object scopes to search; each object's
  159. l_searchlist (i.e. the segment of the dependency tree starting at that
  160. object) is searched in turn. reference_name should name the object
  161. containing the reference; it is used in error messages.
  162. type_class describes the type of symbol we are looking for. */
  163. enum
  164. {
  165. /* if necessary add dependency between user and provider object. */
  166. dl_lookup_add_dependency = 1,
  167. /* return most recent version instead of default version for
  168. unversioned lookup. */
  169. dl_lookup_return_newest = 2,
  170. /* set if dl_lookup* called with gscope lock held. */
  171. dl_lookup_gscope_lock = 4,
  172. };
  173. #define DL_LOOKUP_ADD_DEPENDENCY dl_lookup_add_dependency
  174. #define DL_LOOKUP_RETURN_NEWEST dl_lookup_return_newest
  175. #define DL_LOOKUP_GSCOPE_LOCK dl_lookup_gscope_lock
  176. #endif /* ldsodefs.h */