link.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /* Data structure for communication from the run-time dynamic linker for
  2. loaded ELF shared objects.
  3. Copyright (C) 1995-2026 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library 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 GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <https://www.gnu.org/licenses/>. */
  16. #ifndef _PRIVATE_LINK_H
  17. #define _PRIVATE_LINK_H 1
  18. #ifdef _LINK_H
  19. # error this should be impossible
  20. #endif
  21. # ifndef _ISOMAC
  22. /* Get most of the contents from the public header, but we define a
  23. different `struct link_map' type for private use. The la_objopen
  24. prototype uses the type, so we have to declare it separately. */
  25. # define link_map link_map_public
  26. # define la_objopen la_objopen_wrongproto
  27. # endif
  28. #include <elf/link.h>
  29. # ifndef _ISOMAC
  30. #undef link_map
  31. #undef la_objopen
  32. struct link_map;
  33. extern unsigned int la_objopen (struct link_map *__map, Lmid_t __lmid,
  34. uintptr_t *__cookie);
  35. #include <stdint.h>
  36. #include <stddef.h>
  37. #include <linkmap.h>
  38. #include <dl-fileid.h>
  39. #include <dl-lookupcfg.h>
  40. #include <tls.h>
  41. #include <libc-lock.h>
  42. /* Some internal data structures of the dynamic linker used in the
  43. linker map. We only provide forward declarations. */
  44. struct libname_list;
  45. struct r_found_version;
  46. struct r_search_path_elem;
  47. /* Forward declaration. */
  48. struct link_map;
  49. /* Structure to describe a single list of scope elements. The lookup
  50. functions get passed an array of pointers to such structures. */
  51. struct r_scope_elem
  52. {
  53. /* Array of maps for the scope. */
  54. struct link_map **r_list;
  55. /* Number of entries in the scope. */
  56. unsigned int r_nlist;
  57. };
  58. /* Structure to record search path and allocation mechanism. */
  59. struct r_search_path_struct
  60. {
  61. struct r_search_path_elem **dirs;
  62. int malloced;
  63. };
  64. /* Search path information computed by _dl_init_paths. */
  65. extern struct r_search_path_struct __rtld_search_dirs attribute_hidden;
  66. extern struct r_search_path_struct __rtld_env_path_list attribute_hidden;
  67. /* Structure describing a loaded shared object. The `l_next' and `l_prev'
  68. members form a chain of all the shared objects loaded at startup.
  69. These data structures exist in space used by the run-time dynamic linker;
  70. modifying them may have disastrous results.
  71. This data structure might change in future, if necessary. User-level
  72. programs must avoid defining objects of this type. */
  73. struct link_map
  74. {
  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; /* Difference between the address in the ELF
  78. file and the addresses in memory. */
  79. char *l_name; /* Absolute file name object was found in. */
  80. ElfW(Dyn) *l_ld; /* Dynamic section of the shared object. */
  81. struct link_map *l_next, *l_prev; /* Chain of loaded objects. */
  82. /* All following members are internal to the dynamic linker.
  83. They may change without notice. */
  84. /* This is an element which is only ever different from a pointer to
  85. the very same copy of this type for ld.so when it is used in more
  86. than one namespace. */
  87. struct link_map *l_real;
  88. /* Number of the namespace this link map belongs to. */
  89. Lmid_t l_ns;
  90. struct libname_list *l_libname;
  91. /* Indexed pointers to dynamic section.
  92. [0,DT_NUM) are indexed by the processor-independent tags.
  93. [DT_NUM,DT_NUM+DT_THISPROCNUM) are indexed by the tag minus DT_LOPROC.
  94. [DT_NUM+DT_THISPROCNUM,DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM) are
  95. indexed by DT_VERSIONTAGIDX(tagvalue).
  96. [DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM,
  97. DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM) are indexed by
  98. DT_EXTRATAGIDX(tagvalue).
  99. [DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM,
  100. DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM+DT_VALNUM) are
  101. indexed by DT_VALTAGIDX(tagvalue) and
  102. [DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM+DT_VALNUM,
  103. DT_NUM+DT_THISPROCNUM+DT_VERSIONTAGNUM+DT_EXTRANUM+DT_VALNUM+DT_ADDRNUM)
  104. are indexed by DT_ADDRTAGIDX(tagvalue), see <elf.h>. */
  105. ElfW(Dyn) *l_info[DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM
  106. + DT_EXTRANUM + DT_VALNUM + DT_ADDRNUM];
  107. const ElfW(Phdr) *l_phdr; /* Pointer to program header table in core. */
  108. ElfW(Addr) l_entry; /* Entry point location. */
  109. ElfW(Half) l_phnum; /* Number of program header entries. */
  110. ElfW(Half) l_ldnum; /* Number of dynamic segment entries. */
  111. /* Array of DT_NEEDED dependencies and their dependencies, in
  112. dependency order for symbol lookup (with and without
  113. duplicates). There is no entry before the dependencies have
  114. been loaded. */
  115. struct r_scope_elem l_searchlist;
  116. /* We need a special searchlist to process objects marked with
  117. DT_SYMBOLIC. */
  118. struct r_scope_elem l_symbolic_searchlist;
  119. /* Dependent object that first caused this object to be loaded. */
  120. struct link_map *l_loader;
  121. /* Array with version names. */
  122. struct r_found_version *l_versions;
  123. unsigned int l_nversions;
  124. /* Symbol hash table. */
  125. Elf_Symndx l_nbuckets;
  126. Elf32_Word l_gnu_bitmask_idxbits;
  127. Elf32_Word l_gnu_shift;
  128. const ElfW(Addr) *l_gnu_bitmask;
  129. union
  130. {
  131. const Elf32_Word *l_gnu_buckets;
  132. const Elf_Symndx *l_chain;
  133. };
  134. union
  135. {
  136. const Elf32_Word *l_gnu_chain_zero;
  137. const Elf_Symndx *l_buckets;
  138. };
  139. unsigned int l_direct_opencount; /* Reference count for dlopen/dlclose. */
  140. enum /* Where this object came from. */
  141. {
  142. lt_executable, /* The main executable program. */
  143. lt_library, /* Library needed by main executable. */
  144. lt_loaded /* Extra run-time loaded shared object. */
  145. } l_type:2;
  146. unsigned int l_dt_relr_ref:1; /* Nonzero if GLIBC_ABI_DT_RELR is
  147. referenced. */
  148. unsigned int l_relocated:1; /* Nonzero if object's relocations done. */
  149. unsigned int l_init_called:1; /* Nonzero if DT_INIT function called. */
  150. unsigned int l_global:1; /* Nonzero if object in _dl_global_scope. */
  151. unsigned int l_reserved:2; /* Reserved for internal use. */
  152. unsigned int l_main_map:1; /* Nonzero for the map of the main program. */
  153. unsigned int l_visited:1; /* Used internally for map dependency
  154. graph traversal. */
  155. unsigned int l_map_used:1; /* These two bits are used during traversal */
  156. unsigned int l_map_done:1; /* of maps in _dl_close_worker. */
  157. unsigned int l_phdr_allocated:1; /* Nonzero if the data structure pointed
  158. to by `l_phdr' is allocated. */
  159. unsigned int l_soname_added:1; /* Nonzero if the SONAME is for sure in
  160. the l_libname list. */
  161. unsigned int l_faked:1; /* Nonzero if this is a faked descriptor
  162. without associated file. */
  163. unsigned int l_need_tls_init:1; /* Nonzero if GL(dl_init_static_tls)
  164. should be called on this link map
  165. when relocation finishes. */
  166. unsigned int l_auditing:1; /* Nonzero if the DSO is used in auditing. */
  167. unsigned int l_audit_any_plt:1; /* Nonzero if at least one audit module
  168. is interested in the PLT interception.*/
  169. unsigned int l_removed:1; /* Non-zero if the object cannot be used anymore
  170. since it is removed. */
  171. unsigned int l_contiguous:1; /* Nonzero if inter-segment holes are
  172. mprotected or if no holes are present at
  173. all. */
  174. unsigned int l_free_initfini:1; /* Nonzero if l_initfini can be
  175. freed, ie. not allocated with
  176. the dummy malloc in ld.so. */
  177. unsigned int l_ld_readonly:1; /* Nonzero if dynamic section is readonly. */
  178. unsigned int l_find_object_processed:1; /* Zero if _dl_find_object_update
  179. needs to process this
  180. lt_library map. */
  181. unsigned int l_tls_in_slotinfo:1; /* TLS slotinfo updated in dlopen. */
  182. /* NODELETE status of the map. Only valid for maps of type
  183. lt_loaded. Lazy binding sets l_nodelete_active directly,
  184. potentially from signal handlers. Initial loading of an
  185. DF_1_NODELETE object set l_nodelete_pending. Relocation may
  186. set l_nodelete_pending as well. l_nodelete_pending maps are
  187. promoted to l_nodelete_active status in the final stages of
  188. dlopen, prior to calling ELF constructors. dlclose only
  189. refuses to unload l_nodelete_active maps, the pending status is
  190. ignored. */
  191. bool l_nodelete_active;
  192. bool l_nodelete_pending;
  193. #include <link_map.h>
  194. /* Collected information about own RPATH directories. */
  195. struct r_search_path_struct l_rpath_dirs;
  196. /* Collected results of relocation while profiling. */
  197. struct reloc_result
  198. {
  199. DL_FIXUP_VALUE_TYPE addr;
  200. struct link_map *bound;
  201. unsigned int boundndx;
  202. uint32_t enterexit;
  203. unsigned int flags;
  204. /* CONCURRENCY NOTE: This is used to guard the concurrent initialization
  205. of the relocation result across multiple threads. See the more
  206. detailed notes in elf/dl-runtime.c. */
  207. unsigned int init;
  208. } *l_reloc_result;
  209. /* Pointer to the version information if available. */
  210. ElfW(Versym) *l_versyms;
  211. /* String specifying the path where this object was found. */
  212. const char *l_origin;
  213. /* Start and finish of memory map for this object. l_map_start
  214. need not be the same as l_addr. */
  215. ElfW(Addr) l_map_start, l_map_end;
  216. /* Default array for 'l_scope'. */
  217. struct r_scope_elem *l_scope_mem[4];
  218. /* Size of array allocated for 'l_scope'. */
  219. size_t l_scope_max;
  220. /* This is an array defining the lookup scope for this link map.
  221. There are initially at most three different scope lists. */
  222. struct r_scope_elem **l_scope;
  223. /* A similar array, this time only with the local scope. This is
  224. used occasionally. */
  225. struct r_scope_elem *l_local_scope[2];
  226. /* This information is kept to check for sure whether a shared
  227. object is the same as one already loaded. */
  228. struct r_file_id l_file_id;
  229. /* Collected information about own RUNPATH directories. */
  230. struct r_search_path_struct l_runpath_dirs;
  231. /* List of object in order of the init and fini calls. */
  232. struct link_map **l_initfini;
  233. /* List of the dependencies introduced through symbol binding. */
  234. struct link_map_reldeps
  235. {
  236. unsigned int act;
  237. struct link_map *list[];
  238. } *l_reldeps;
  239. unsigned int l_reldepsmax;
  240. /* Nonzero if the DSO is used. */
  241. unsigned int l_used;
  242. /* Various flag words. */
  243. ElfW(Word) l_feature_1;
  244. ElfW(Word) l_flags_1;
  245. ElfW(Word) l_flags;
  246. /* Temporarily used in `dl_close'. */
  247. int l_idx;
  248. struct link_map_machine l_mach;
  249. struct
  250. {
  251. const ElfW(Sym) *sym;
  252. int type_class;
  253. struct link_map *value;
  254. const ElfW(Sym) *ret;
  255. } l_lookup_cache;
  256. /* Thread-local storage related info. */
  257. /* Start of the initialization image. */
  258. void *l_tls_initimage;
  259. /* Size of the initialization image. */
  260. size_t l_tls_initimage_size;
  261. /* Size of the TLS block. */
  262. size_t l_tls_blocksize;
  263. /* Alignment requirement of the TLS block. */
  264. size_t l_tls_align;
  265. /* Offset of first byte module alignment. */
  266. size_t l_tls_firstbyte_offset;
  267. #ifndef NO_TLS_OFFSET
  268. # define NO_TLS_OFFSET 0
  269. #endif
  270. #ifndef FORCED_DYNAMIC_TLS_OFFSET
  271. # if NO_TLS_OFFSET == 0
  272. # define FORCED_DYNAMIC_TLS_OFFSET -1
  273. # elif NO_TLS_OFFSET == -1
  274. # define FORCED_DYNAMIC_TLS_OFFSET -2
  275. # else
  276. # error "FORCED_DYNAMIC_TLS_OFFSET is not defined"
  277. # endif
  278. #endif
  279. /* For objects present at startup time: offset in the static TLS block. */
  280. ptrdiff_t l_tls_offset;
  281. /* Index of the module in the dtv array. */
  282. size_t l_tls_modid;
  283. /* Number of thread_local objects constructed by this DSO. This is
  284. atomically accessed and modified and is not always protected by the load
  285. lock. See also: CONCURRENCY NOTES in cxa_thread_atexit_impl.c. */
  286. size_t l_tls_dtor_count;
  287. /* Information used to change permission after the relocations are
  288. done. */
  289. ElfW(Addr) l_relro_addr;
  290. size_t l_relro_size;
  291. unsigned long long int l_serial;
  292. };
  293. #include <dl-relocate-ld.h>
  294. /* Information used by audit modules. For most link maps, this data
  295. immediate follows the link map in memory. For the dynamic linker,
  296. it is allocated separately. See link_map_audit_state in
  297. <ldsodefs.h>. */
  298. struct auditstate
  299. {
  300. uintptr_t cookie;
  301. unsigned int bindflags;
  302. };
  303. /* This is the hidden instance of struct r_debug_extended used by the
  304. dynamic linker. */
  305. extern struct r_debug_extended _r_debug_extended attribute_hidden;
  306. rtld_hidden_proto (_r_debug)
  307. #if __ELF_NATIVE_CLASS == 32
  308. # define symbind symbind32
  309. # define LA_SYMBIND "la_symbind32"
  310. #elif __ELF_NATIVE_CLASS == 64
  311. # define symbind symbind64
  312. # define LA_SYMBIND "la_symbind64"
  313. #else
  314. # error "__ELF_NATIVE_CLASS must be defined"
  315. #endif
  316. extern int __dl_iterate_phdr (int (*callback) (struct dl_phdr_info *info,
  317. size_t size, void *data),
  318. void *data);
  319. hidden_proto (__dl_iterate_phdr)
  320. /* We use this macro to refer to ELF macros independent of the native
  321. wordsize. `ELFW(R_TYPE)' is used in place of `ELF32_R_TYPE' or
  322. `ELF64_R_TYPE'. */
  323. #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type)
  324. # endif /* !_ISOMAC */
  325. #endif /* include/link.h */