ldsodefs.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <elf/elf.h>
  22. #include <stddef.h>
  23. #include <sys/mman.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) (void*)(LOOKUP_VALUE_ADDRESS(map) + ref->st_value)
  59. #define DL_LOOKUP_ADDRESS(addr) ((ElfW(Addr))(addr))
  60. #define DL_DT_INIT_ADDRESS(map, start) (start)
  61. #define DL_DT_FINI_ADDRESS(map, start) (start)
  62. #endif
  63. #endif
  64. /* On some architectures dladdr can't use st_size of all symbols this way. */
  65. #define DL_ADDR_SYM_MATCH(L, SYM, MATCHSYM, ADDR) \
  66. ((ADDR) >= (L)->l_addr + (SYM)->st_value && \
  67. ((((SYM)->st_shndx == SHN_UNDEF || (SYM)->st_size == 0) && \
  68. (ADDR) == (L)->l_addr + (SYM)->st_value) || \
  69. (ADDR) < (L)->l_addr + (SYM)->st_value + (SYM)->st_size) && \
  70. ((MATCHSYM) == NULL || (MATCHSYM)->st_value < (SYM)->st_value))
  71. /* Unmap a loaded object, called by _dl_close (). */
  72. #ifndef DL_UNMAP_IS_SPECIAL
  73. #define DL_UNMAP(map) __munmap((void*)(map)->l_map_start, (map)->l_map_end - (map)->l_map_start)
  74. #endif
  75. /* By default we do not need special support to initialize DSOs loaded
  76. by statically linked binaries. */
  77. #ifndef DL_STATIC_INIT
  78. #define DL_STATIC_INIT(map)
  79. #endif
  80. /* Reloc type classes as returned by elf_machine_type_class().
  81. ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by
  82. some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be
  83. satisfied by any symbol in the executable. Some architectures do
  84. not support copy relocations. In this case we define the macro to
  85. zero so that the code for handling them gets automatically optimized
  86. out. */
  87. #define ELF_RTYPE_CLASS_PLT 1
  88. #ifndef DL_NO_COPY_RELOCS
  89. #define ELF_RTYPE_CLASS_COPY 2
  90. #else
  91. #define ELF_RTYPE_CLASS_COPY 0
  92. #endif
  93. /* ELF uses the PF_x macros to specify the segment permissions, mmap
  94. uses PROT_xxx. In most cases the three macros have the values 1, 2,
  95. and 3 but not in a matching order. The following macros allows
  96. converting from the PF_x values to PROT_xxx values. */
  97. #define PF_TO_PROT \
  98. ((PROT_READ << (PF_R * 4)) | (PROT_WRITE << (PF_W * 4)) | (PROT_EXEC << (PF_X * 4)) | \
  99. ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) | \
  100. ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) | \
  101. ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) | \
  102. ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
  103. /* For the version handling we need an array with only names and their
  104. hash values. */
  105. struct r_found_version {
  106. const char* name;
  107. ElfW(Word) hash;
  108. int hidden;
  109. const char* filename;
  110. };
  111. /* We want to cache information about the searches for shared objects. */
  112. enum r_dir_status { unknown, nonexisting, existing };
  113. struct r_search_path_elem {
  114. /* This link is only used in the `all_dirs' member of `r_search_path'. */
  115. struct r_search_path_elem* next;
  116. /* Strings saying where the definition came from. */
  117. const char* what;
  118. const char* where;
  119. /* Basename for this search path element. The string must end with
  120. a slash character. */
  121. const char* dirname;
  122. size_t dirnamelen;
  123. enum r_dir_status status[0];
  124. };
  125. struct r_strlenpair {
  126. const char* str;
  127. size_t len;
  128. };
  129. /* A data structure for a simple single linked list of strings. */
  130. struct libname_list {
  131. const char* name; /* Name requested (before search). */
  132. struct libname_list* next; /* Link to next name for this object. */
  133. int dont_free; /* Flag whether this element should be freed
  134. if the object is not entirely unloaded. */
  135. };
  136. /* Bit masks for the objects which valid callers can come from to
  137. functions with restricted interface. */
  138. enum allowmask { allow_libc = 1, allow_libdl = 2, allow_libpthread = 4, allow_ldso = 8 };
  139. /* search loaded objects' symbol tables for a definition of the symbol
  140. referred to by undef. *sym is the symbol table entry containing the
  141. reference; it is replaced with the defining symbol, and the base load
  142. address of the defining object is returned. symbol_scope is a
  143. null-terminated list of object scopes to search; each object's
  144. l_searchlist (i.e. the segment of the dependency tree starting at that
  145. object) is searched in turn. reference_name should name the object
  146. containing the reference; it is used in error messages.
  147. type_class describes the type of symbol we are looking for. */
  148. enum {
  149. /* if necessary add dependency between user and provider object. */
  150. dl_lookup_add_dependency = 1,
  151. /* return most recent version instead of default version for
  152. unversioned lookup. */
  153. dl_lookup_return_newest = 2,
  154. /* set if dl_lookup* called with gscope lock held. */
  155. dl_lookup_gscope_lock = 4,
  156. };
  157. #define DL_LOOKUP_ADD_DEPENDENCY dl_lookup_add_dependency
  158. #define DL_LOOKUP_RETURN_NEWEST dl_lookup_return_newest
  159. #define DL_LOOKUP_GSCOPE_LOCK dl_lookup_gscope_lock
  160. #endif /* ldsodefs.h */