1 | /* Run-time dynamic linker data structures for loaded ELF shared objects. |
2 | Copyright (C) 1995-2022 Free Software Foundation, Inc. |
3 | This file is part of the GNU C Library. |
4 | |
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 | |
10 | The GNU C Library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #ifndef _LDSODEFS_H |
20 | #define _LDSODEFS_H 1 |
21 | |
22 | #include <features.h> |
23 | |
24 | #include <stdbool.h> |
25 | #define __need_size_t |
26 | #define __need_NULL |
27 | #include <stddef.h> |
28 | #include <string.h> |
29 | #include <stdint.h> |
30 | |
31 | #include <elf.h> |
32 | #include <dlfcn.h> |
33 | #include <fpu_control.h> |
34 | #include <sys/mman.h> |
35 | #include <link.h> |
36 | #include <dl-lookupcfg.h> |
37 | #include <dl-sysdep.h> |
38 | #include <dl-fixup-attribute.h> |
39 | #include <libc-lock.h> |
40 | #include <hp-timing.h> |
41 | #include <tls.h> |
42 | #include <list_t.h> |
43 | |
44 | __BEGIN_DECLS |
45 | |
46 | #define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym)) |
47 | #define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ |
48 | + DT_EXTRANUM + DT_VALTAGIDX (tag)) |
49 | #define ADDRIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \ |
50 | + DT_EXTRANUM + DT_VALNUM + DT_ADDRTAGIDX (tag)) |
51 | |
52 | /* Type of GNU hash which the machine uses. */ |
53 | #ifndef ELF_MACHINE_GNU_HASH_ADDRIDX |
54 | # define ELF_MACHINE_GNU_HASH_ADDRIDX ADDRIDX (DT_GNU_HASH) |
55 | #endif |
56 | |
57 | /* Calculate the index of a symbol in GNU hash. */ |
58 | #ifndef ELF_MACHINE_HASH_SYMIDX |
59 | # define ELF_MACHINE_HASH_SYMIDX(map, hasharr) \ |
60 | ((hasharr) - (map)->l_gnu_chain_zero) |
61 | #endif |
62 | |
63 | /* Setup MIPS xhash. Defined only for MIPS. */ |
64 | #ifndef ELF_MACHINE_XHASH_SETUP |
65 | # define ELF_MACHINE_XHASH_SETUP(hash32, symbias, map) \ |
66 | ((void) (hash32), (void) (symbias), (void) (map)) |
67 | #endif |
68 | |
69 | /* We use this macro to refer to ELF types independent of the native wordsize. |
70 | `ElfW(TYPE)' is used in place of `Elf32_TYPE' or `Elf64_TYPE'. */ |
71 | #define ELFW(type) _ElfW (ELF, __ELF_NATIVE_CLASS, type) |
72 | |
73 | /* Return true if dynamic section in the shared library L should be |
74 | relocated. */ |
75 | |
76 | static inline bool |
77 | dl_relocate_ld (const struct link_map *l) |
78 | { |
79 | /* Don't relocate dynamic section if it is readonly */ |
80 | return !(l->l_ld_readonly || DL_RO_DYN_SECTION); |
81 | } |
82 | |
83 | /* All references to the value of l_info[DT_PLTGOT], |
84 | l_info[DT_STRTAB], l_info[DT_SYMTAB], l_info[DT_RELA], |
85 | l_info[DT_REL], l_info[DT_JMPREL], and l_info[VERSYMIDX (DT_VERSYM)] |
86 | have to be accessed via the D_PTR macro. The macro is needed since for |
87 | most architectures the entry is already relocated - but for some not |
88 | and we need to relocate at access time. */ |
89 | #define D_PTR(map, i) \ |
90 | ((map)->i->d_un.d_ptr + (dl_relocate_ld (map) ? 0 : (map)->l_addr)) |
91 | |
92 | /* Result of the lookup functions and how to retrieve the base address. */ |
93 | typedef struct link_map *lookup_t; |
94 | #define LOOKUP_VALUE(map) map |
95 | #define LOOKUP_VALUE_ADDRESS(map, set) ((set) || (map) ? (map)->l_addr : 0) |
96 | |
97 | /* Calculate the address of symbol REF using the base address from map MAP, |
98 | if non-NULL. Don't check for NULL map if MAP_SET is TRUE. */ |
99 | #define SYMBOL_ADDRESS(map, ref, map_set) \ |
100 | ((ref) == NULL ? 0 \ |
101 | : (__glibc_unlikely ((ref)->st_shndx == SHN_ABS) ? 0 \ |
102 | : LOOKUP_VALUE_ADDRESS (map, map_set)) + (ref)->st_value) |
103 | |
104 | /* Type of a constructor function, in DT_INIT, DT_INIT_ARRAY, |
105 | DT_PREINIT_ARRAY. */ |
106 | typedef void (*dl_init_t) (int, char **, char **); |
107 | |
108 | /* Type of a constructor function, in DT_FINI, DT_FINI_ARRAY. */ |
109 | typedef void (*fini_t) (void); |
110 | |
111 | /* On some architectures a pointer to a function is not just a pointer |
112 | to the actual code of the function but rather an architecture |
113 | specific descriptor. */ |
114 | #ifndef ELF_FUNCTION_PTR_IS_SPECIAL |
115 | # define DL_SYMBOL_ADDRESS(map, ref) \ |
116 | (void *) SYMBOL_ADDRESS (map, ref, false) |
117 | # define DL_LOOKUP_ADDRESS(addr) ((ElfW(Addr)) (addr)) |
118 | # define DL_CALL_DT_INIT(map, start, argc, argv, env) \ |
119 | ((dl_init_t) (start)) (argc, argv, env) |
120 | # define DL_CALL_DT_FINI(map, start) ((fini_t) (start)) () |
121 | #endif |
122 | |
123 | /* On some architectures dladdr can't use st_size of all symbols this way. */ |
124 | #define DL_ADDR_SYM_MATCH(L, SYM, MATCHSYM, ADDR) \ |
125 | ((ADDR) >= (L)->l_addr + (SYM)->st_value \ |
126 | && ((((SYM)->st_shndx == SHN_UNDEF || (SYM)->st_size == 0) \ |
127 | && (ADDR) == (L)->l_addr + (SYM)->st_value) \ |
128 | || (ADDR) < (L)->l_addr + (SYM)->st_value + (SYM)->st_size) \ |
129 | && ((MATCHSYM) == NULL || (MATCHSYM)->st_value < (SYM)->st_value)) |
130 | |
131 | /* According to the ELF gABI no STV_HIDDEN or STV_INTERNAL symbols are |
132 | expected to be present in dynamic symbol tables as they should have |
133 | been either removed or converted to STB_LOCAL binding by the static |
134 | linker. However some GNU binutils versions produce such symbols in |
135 | some cases. To prevent such symbols present in a buggy binary from |
136 | preempting global symbols we filter them out with this predicate. */ |
137 | static __always_inline bool |
138 | dl_symbol_visibility_binds_local_p (const ElfW(Sym) *sym) |
139 | { |
140 | return (ELFW(ST_VISIBILITY) (sym->st_other) == STV_HIDDEN |
141 | || ELFW(ST_VISIBILITY) (sym->st_other) == STV_INTERNAL); |
142 | } |
143 | |
144 | /* Unmap a loaded object, called by _dl_close (). */ |
145 | #ifndef DL_UNMAP_IS_SPECIAL |
146 | # define DL_UNMAP(map) _dl_unmap_segments (map) |
147 | #endif |
148 | |
149 | /* Reloc type classes as returned by elf_machine_type_class(). |
150 | ELF_RTYPE_CLASS_PLT means this reloc should not be satisfied by |
151 | some PLT symbol, ELF_RTYPE_CLASS_COPY means this reloc should not be |
152 | satisfied by any symbol in the executable. Some architectures do |
153 | not support copy relocations. In this case we define the macro to |
154 | zero so that the code for handling them gets automatically optimized |
155 | out. ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA means address of protected |
156 | data defined in the shared library may be external, i.e., due to copy |
157 | relocation. */ |
158 | #define ELF_RTYPE_CLASS_PLT 1 |
159 | #ifndef DL_NO_COPY_RELOCS |
160 | # define ELF_RTYPE_CLASS_COPY 2 |
161 | #else |
162 | # define ELF_RTYPE_CLASS_COPY 0 |
163 | #endif |
164 | /* If DL_EXTERN_PROTECTED_DATA is defined, address of protected data |
165 | defined in the shared library may be external, i.e., due to copy |
166 | relocation. */ |
167 | #ifdef DL_EXTERN_PROTECTED_DATA |
168 | # define ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA 4 |
169 | #else |
170 | # define ELF_RTYPE_CLASS_EXTERN_PROTECTED_DATA 0 |
171 | #endif |
172 | |
173 | /* ELF uses the PF_x macros to specify the segment permissions, mmap |
174 | uses PROT_xxx. In most cases the three macros have the values 1, 2, |
175 | and 3 but not in a matching order. The following macros allows |
176 | converting from the PF_x values to PROT_xxx values. */ |
177 | #define PF_TO_PROT \ |
178 | ((PROT_READ << (PF_R * 4)) \ |
179 | | (PROT_WRITE << (PF_W * 4)) \ |
180 | | (PROT_EXEC << (PF_X * 4)) \ |
181 | | ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) \ |
182 | | ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) \ |
183 | | ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) \ |
184 | | ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4))) |
185 | |
186 | /* The filename itself, or the main program name, if available. */ |
187 | #define DSO_FILENAME(name) ((name)[0] ? (name) \ |
188 | : (rtld_progname ?: "<main program>")) |
189 | |
190 | #define RTLD_PROGNAME (rtld_progname ?: "<program name unknown>") |
191 | |
192 | /* For the version handling we need an array with only names and their |
193 | hash values. */ |
194 | struct r_found_version |
195 | { |
196 | const char *name; |
197 | ElfW(Word) hash; |
198 | |
199 | int hidden; |
200 | const char *filename; |
201 | }; |
202 | |
203 | /* We want to cache information about the searches for shared objects. */ |
204 | |
205 | enum r_dir_status { unknown, nonexisting, existing }; |
206 | |
207 | struct r_search_path_elem |
208 | { |
209 | /* This link is only used in the `all_dirs' member of `r_search_path'. */ |
210 | struct r_search_path_elem *next; |
211 | |
212 | /* Strings saying where the definition came from. */ |
213 | const char *what; |
214 | const char *where; |
215 | |
216 | /* Basename for this search path element. The string must end with |
217 | a slash character. */ |
218 | const char *dirname; |
219 | size_t dirnamelen; |
220 | |
221 | enum r_dir_status status[0]; |
222 | }; |
223 | |
224 | struct r_strlenpair |
225 | { |
226 | const char *str; |
227 | size_t len; |
228 | }; |
229 | |
230 | |
231 | /* A data structure for a simple single linked list of strings. */ |
232 | struct libname_list |
233 | { |
234 | const char *name; /* Name requested (before search). */ |
235 | struct libname_list *next; /* Link to next name for this object. */ |
236 | int dont_free; /* Flag whether this element should be freed |
237 | if the object is not entirely unloaded. */ |
238 | }; |
239 | |
240 | |
241 | /* Bit masks for the objects which valid callers can come from to |
242 | functions with restricted interface. */ |
243 | enum allowmask |
244 | { |
245 | allow_libc = 1, |
246 | allow_libdl = 2, |
247 | allow_libpthread = 4, |
248 | allow_ldso = 8 |
249 | }; |
250 | |
251 | |
252 | /* DSO sort algorithm to use (check dl-sort-maps.c). */ |
253 | enum dso_sort_algorithm |
254 | { |
255 | dso_sort_algorithm_original, |
256 | dso_sort_algorithm_dfs |
257 | }; |
258 | |
259 | struct audit_ifaces |
260 | { |
261 | void (*activity) (uintptr_t *, unsigned int); |
262 | char *(*objsearch) (const char *, uintptr_t *, unsigned int); |
263 | unsigned int (*objopen) (struct link_map *, Lmid_t, uintptr_t *); |
264 | void (*preinit) (uintptr_t *); |
265 | union |
266 | { |
267 | uintptr_t (*symbind32) (Elf32_Sym *, unsigned int, uintptr_t *, |
268 | uintptr_t *, unsigned int *, const char *); |
269 | uintptr_t (*symbind64) (Elf64_Sym *, unsigned int, uintptr_t *, |
270 | uintptr_t *, unsigned int *, const char *); |
271 | }; |
272 | union |
273 | { |
274 | #ifdef ARCH_PLTENTER_MEMBERS |
275 | ARCH_PLTENTER_MEMBERS; |
276 | #endif |
277 | }; |
278 | union |
279 | { |
280 | #ifdef ARCH_PLTEXIT_MEMBERS |
281 | ARCH_PLTEXIT_MEMBERS; |
282 | #endif |
283 | }; |
284 | unsigned int (*objclose) (uintptr_t *); |
285 | |
286 | struct audit_ifaces *next; |
287 | }; |
288 | |
289 | |
290 | /* Test whether given NAME matches any of the names of the given object. */ |
291 | extern int _dl_name_match_p (const char *__name, const struct link_map *__map) |
292 | attribute_hidden; |
293 | |
294 | /* Compute next higher prime number. */ |
295 | extern unsigned long int _dl_higher_prime_number (unsigned long int n) |
296 | attribute_hidden; |
297 | |
298 | /* A stripped down strtoul-like implementation. */ |
299 | uint64_t _dl_strtoul (const char *, char **) attribute_hidden; |
300 | |
301 | /* Function used as argument for `_dl_receive_error' function. The |
302 | arguments are the error code, error string, and the objname the |
303 | error occurred in. */ |
304 | typedef void (*receiver_fct) (int, const char *, const char *); |
305 | |
306 | /* Internal functions of the run-time dynamic linker. |
307 | These can be accessed if you link again the dynamic linker |
308 | as a shared library, as in `-lld' or `/lib/ld.so' explicitly; |
309 | but are not normally of interest to user programs. |
310 | |
311 | The `-ldl' library functions in <dlfcn.h> provide a simple |
312 | user interface to run-time dynamic linking. */ |
313 | |
314 | |
315 | #ifndef SHARED |
316 | # define EXTERN extern |
317 | # define GL(name) _##name |
318 | #else |
319 | # define EXTERN |
320 | # if IS_IN (rtld) |
321 | # define GL(name) _rtld_local._##name |
322 | # else |
323 | # define GL(name) _rtld_global._##name |
324 | # endif |
325 | struct rtld_global |
326 | { |
327 | #endif |
328 | /* Don't change the order of the following elements. 'dl_loaded' |
329 | must remain the first element. Forever. */ |
330 | |
331 | /* Non-shared code has no support for multiple namespaces. */ |
332 | #ifdef SHARED |
333 | # define DL_NNS 16 |
334 | #else |
335 | # define DL_NNS 1 |
336 | #endif |
337 | EXTERN struct link_namespaces |
338 | { |
339 | /* A pointer to the map for the main map. */ |
340 | struct link_map *_ns_loaded; |
341 | /* Number of object in the _dl_loaded list. */ |
342 | unsigned int _ns_nloaded; |
343 | /* Direct pointer to the searchlist of the main object. */ |
344 | struct r_scope_elem *_ns_main_searchlist; |
345 | /* This is zero at program start to signal that the global scope map is |
346 | allocated by rtld. Later it keeps the size of the map. It might be |
347 | reset if in _dl_close if the last global object is removed. */ |
348 | unsigned int _ns_global_scope_alloc; |
349 | |
350 | /* During dlopen, this is the number of objects that still need to |
351 | be added to the global scope map. It has to be taken into |
352 | account when resizing the map, for future map additions after |
353 | recursive dlopen calls from ELF constructors. */ |
354 | unsigned int _ns_global_scope_pending_adds; |
355 | |
356 | /* Once libc.so has been loaded into the namespace, this points to |
357 | its link map. */ |
358 | struct link_map *libc_map; |
359 | |
360 | /* Search table for unique objects. */ |
361 | struct unique_sym_table |
362 | { |
363 | __rtld_lock_define_recursive (, lock) |
364 | struct unique_sym |
365 | { |
366 | uint32_t hashval; |
367 | const char *name; |
368 | const ElfW(Sym) *sym; |
369 | const struct link_map *map; |
370 | } *entries; |
371 | size_t size; |
372 | size_t n_elements; |
373 | void (*free) (void *); |
374 | } _ns_unique_sym_table; |
375 | /* Keep track of changes to each namespace' list. */ |
376 | struct r_debug_extended _ns_debug; |
377 | } _dl_ns[DL_NNS]; |
378 | /* One higher than index of last used namespace. */ |
379 | EXTERN size_t _dl_nns; |
380 | |
381 | /* During the program run we must not modify the global data of |
382 | loaded shared object simultanously in two threads. Therefore we |
383 | protect `_dl_open' and `_dl_close' in dl-close.c. |
384 | |
385 | This must be a recursive lock since the initializer function of |
386 | the loaded object might as well require a call to this function. |
387 | At this time it is not anymore a problem to modify the tables. */ |
388 | __rtld_lock_define_recursive (EXTERN, _dl_load_lock) |
389 | /* This lock is used to keep __dl_iterate_phdr from inspecting the |
390 | list of loaded objects while an object is added to or removed |
391 | from that list. */ |
392 | __rtld_lock_define_recursive (EXTERN, _dl_load_write_lock) |
393 | /* This lock protects global and module specific TLS related data. |
394 | E.g. it is held in dlopen and dlclose when GL(dl_tls_generation), |
395 | GL(dl_tls_max_dtv_idx) or GL(dl_tls_dtv_slotinfo_list) are |
396 | accessed and when TLS related relocations are processed for a |
397 | module. It was introduced to keep pthread_create accessing TLS |
398 | state that is being set up. */ |
399 | __rtld_lock_define_recursive (EXTERN, _dl_load_tls_lock) |
400 | |
401 | /* Incremented whenever something may have been added to dl_loaded. */ |
402 | EXTERN unsigned long long _dl_load_adds; |
403 | |
404 | /* The object to be initialized first. */ |
405 | EXTERN struct link_map *_dl_initfirst; |
406 | |
407 | /* Map of shared object to be profiled. */ |
408 | EXTERN struct link_map *_dl_profile_map; |
409 | |
410 | /* Counters for the number of relocations performed. */ |
411 | EXTERN unsigned long int _dl_num_relocations; |
412 | EXTERN unsigned long int _dl_num_cache_relocations; |
413 | |
414 | /* List of search directories. */ |
415 | EXTERN struct r_search_path_elem *_dl_all_dirs; |
416 | |
417 | /* Structure describing the dynamic linker itself. */ |
418 | EXTERN struct link_map _dl_rtld_map; |
419 | #ifdef SHARED |
420 | /* Used to store the audit information for the link map of the |
421 | dynamic loader. */ |
422 | struct auditstate _dl_rtld_auditstate[DL_NNS]; |
423 | #endif |
424 | |
425 | #if !PTHREAD_IN_LIBC && defined SHARED \ |
426 | && defined __rtld_lock_default_lock_recursive |
427 | EXTERN void (*_dl_rtld_lock_recursive) (void *); |
428 | EXTERN void (*_dl_rtld_unlock_recursive) (void *); |
429 | #endif |
430 | |
431 | /* Get architecture specific definitions. */ |
432 | #define PROCINFO_DECL |
433 | #ifndef PROCINFO_CLASS |
434 | # define PROCINFO_CLASS EXTERN |
435 | #endif |
436 | #include <dl-procruntime.c> |
437 | |
438 | #if !PTHREAD_IN_LIBC |
439 | /* If loading a shared object requires that we make the stack executable |
440 | when it was not, we do it by calling this function. |
441 | It returns an errno code or zero on success. */ |
442 | EXTERN int (*_dl_make_stack_executable_hook) (void **); |
443 | #endif |
444 | |
445 | /* Prevailing state of the stack, PF_X indicating it's executable. */ |
446 | EXTERN ElfW(Word) _dl_stack_flags; |
447 | |
448 | /* Flag signalling whether there are gaps in the module ID allocation. */ |
449 | EXTERN bool _dl_tls_dtv_gaps; |
450 | /* Highest dtv index currently needed. */ |
451 | EXTERN size_t _dl_tls_max_dtv_idx; |
452 | /* Information about the dtv slots. */ |
453 | EXTERN struct dtv_slotinfo_list |
454 | { |
455 | size_t len; |
456 | struct dtv_slotinfo_list *next; |
457 | struct dtv_slotinfo |
458 | { |
459 | size_t gen; |
460 | struct link_map *map; |
461 | } slotinfo[]; |
462 | } *_dl_tls_dtv_slotinfo_list; |
463 | /* Number of modules in the static TLS block. */ |
464 | EXTERN size_t _dl_tls_static_nelem; |
465 | /* Size actually allocated in the static TLS block. */ |
466 | EXTERN size_t _dl_tls_static_used; |
467 | /* Remaining amount of static TLS that may be used for optimizing |
468 | dynamic TLS access (e.g. with TLSDESC). */ |
469 | EXTERN size_t _dl_tls_static_optional; |
470 | |
471 | /* Number of additional entries in the slotinfo array of each slotinfo |
472 | list element. A large number makes it almost certain take we never |
473 | have to iterate beyond the first element in the slotinfo list. */ |
474 | #define TLS_SLOTINFO_SURPLUS (62) |
475 | |
476 | /* Number of additional slots in the dtv allocated. */ |
477 | #define DTV_SURPLUS (14) |
478 | |
479 | /* Initial dtv of the main thread, not allocated with normal malloc. */ |
480 | EXTERN void *_dl_initial_dtv; |
481 | /* Generation counter for the dtv. */ |
482 | EXTERN size_t _dl_tls_generation; |
483 | |
484 | #if !PTHREAD_IN_LIBC |
485 | EXTERN void (*_dl_init_static_tls) (struct link_map *); |
486 | #endif |
487 | |
488 | /* Scopes to free after next THREAD_GSCOPE_WAIT (). */ |
489 | EXTERN struct dl_scope_free_list |
490 | { |
491 | size_t count; |
492 | void *list[50]; |
493 | } *_dl_scope_free_list; |
494 | #if PTHREAD_IN_LIBC |
495 | /* List of active thread stacks, with memory managed by glibc. */ |
496 | EXTERN list_t _dl_stack_used; |
497 | |
498 | /* List of thread stacks that were allocated by the application. */ |
499 | EXTERN list_t _dl_stack_user; |
500 | |
501 | /* List of queued thread stacks. */ |
502 | EXTERN list_t _dl_stack_cache; |
503 | |
504 | /* Total size of all stacks in the cache (sum over stackblock_size). */ |
505 | EXTERN size_t _dl_stack_cache_actsize; |
506 | |
507 | /* We need to record what list operations we are going to do so |
508 | that, in case of an asynchronous interruption due to a fork() |
509 | call, we can correct for the work. */ |
510 | EXTERN uintptr_t _dl_in_flight_stack; |
511 | |
512 | /* Mutex protecting the stack lists. */ |
513 | EXTERN int _dl_stack_cache_lock; |
514 | #else |
515 | /* The total number of thread IDs currently in use, or on the list of |
516 | available thread IDs. */ |
517 | EXTERN int _dl_pthread_num_threads; |
518 | |
519 | /* Array of __pthread structures and its lock. */ |
520 | EXTERN struct __pthread **_dl_pthread_threads; |
521 | __libc_rwlock_define (EXTERN, _dl_pthread_threads_lock) |
522 | #endif |
523 | #ifdef SHARED |
524 | }; |
525 | # define __rtld_global_attribute__ |
526 | # if IS_IN (rtld) |
527 | # ifdef HAVE_SDATA_SECTION |
528 | # define __rtld_local_attribute__ \ |
529 | __attribute__ ((visibility ("hidden"), section (".sdata"))) |
530 | # undef __rtld_global_attribute__ |
531 | # define __rtld_global_attribute__ __attribute__ ((section (".sdata"))) |
532 | # else |
533 | # define __rtld_local_attribute__ __attribute__ ((visibility ("hidden"))) |
534 | # endif |
535 | extern struct rtld_global _rtld_local __rtld_local_attribute__; |
536 | # undef __rtld_local_attribute__ |
537 | # endif |
538 | extern struct rtld_global _rtld_global __rtld_global_attribute__; |
539 | # undef __rtld_global_attribute__ |
540 | #endif |
541 | |
542 | #ifndef SHARED |
543 | # define GLRO(name) _##name |
544 | #else |
545 | # if IS_IN (rtld) |
546 | # define GLRO(name) _rtld_local_ro._##name |
547 | # else |
548 | # define GLRO(name) _rtld_global_ro._##name |
549 | # endif |
550 | struct rtld_global_ro |
551 | { |
552 | #endif |
553 | |
554 | /* If nonzero the appropriate debug information is printed. */ |
555 | EXTERN int _dl_debug_mask; |
556 | #define DL_DEBUG_LIBS (1 << 0) |
557 | #define DL_DEBUG_IMPCALLS (1 << 1) |
558 | #define DL_DEBUG_BINDINGS (1 << 2) |
559 | #define DL_DEBUG_SYMBOLS (1 << 3) |
560 | #define DL_DEBUG_VERSIONS (1 << 4) |
561 | #define DL_DEBUG_RELOC (1 << 5) |
562 | #define DL_DEBUG_FILES (1 << 6) |
563 | #define DL_DEBUG_STATISTICS (1 << 7) |
564 | #define DL_DEBUG_UNUSED (1 << 8) |
565 | #define DL_DEBUG_SCOPES (1 << 9) |
566 | /* These two are used only internally. */ |
567 | #define DL_DEBUG_HELP (1 << 10) |
568 | #define DL_DEBUG_PRELINK (1 << 11) |
569 | |
570 | /* OS version. */ |
571 | EXTERN unsigned int _dl_osversion; |
572 | /* Platform name. */ |
573 | EXTERN const char *_dl_platform; |
574 | EXTERN size_t _dl_platformlen; |
575 | |
576 | /* Cached value of `getpagesize ()'. */ |
577 | EXTERN size_t _dl_pagesize; |
578 | |
579 | /* Cached value of `sysconf (_SC_MINSIGSTKSZ)'. */ |
580 | EXTERN size_t _dl_minsigstacksize; |
581 | |
582 | /* Do we read from ld.so.cache? */ |
583 | EXTERN int _dl_inhibit_cache; |
584 | |
585 | /* Copy of the content of `_dl_main_searchlist' at startup time. */ |
586 | EXTERN struct r_scope_elem _dl_initial_searchlist; |
587 | |
588 | /* CLK_TCK as reported by the kernel. */ |
589 | EXTERN int _dl_clktck; |
590 | |
591 | /* If nonzero print warnings messages. */ |
592 | EXTERN int _dl_verbose; |
593 | |
594 | /* File descriptor to write debug messages to. */ |
595 | EXTERN int _dl_debug_fd; |
596 | |
597 | /* Do we do lazy relocations? */ |
598 | EXTERN int _dl_lazy; |
599 | |
600 | /* Nonzero if runtime lookups should not update the .got/.plt. */ |
601 | EXTERN int _dl_bind_not; |
602 | |
603 | /* Nonzero if references should be treated as weak during runtime |
604 | linking. */ |
605 | EXTERN int _dl_dynamic_weak; |
606 | |
607 | /* Default floating-point control word. */ |
608 | EXTERN fpu_control_t _dl_fpu_control; |
609 | |
610 | /* Expected cache ID. */ |
611 | EXTERN int _dl_correct_cache_id; |
612 | |
613 | /* Mask for hardware capabilities that are available. */ |
614 | EXTERN uint64_t _dl_hwcap; |
615 | |
616 | #if !HAVE_TUNABLES |
617 | /* Mask for important hardware capabilities we honour. */ |
618 | EXTERN uint64_t _dl_hwcap_mask; |
619 | #endif |
620 | |
621 | #ifdef HAVE_AUX_VECTOR |
622 | /* Pointer to the auxv list supplied to the program at startup. */ |
623 | EXTERN ElfW(auxv_t) *_dl_auxv; |
624 | #endif |
625 | |
626 | /* Get architecture specific definitions. */ |
627 | #include <dl-procinfo.c> |
628 | |
629 | /* Names of shared object for which the RPATH should be ignored. */ |
630 | EXTERN const char *_dl_inhibit_rpath; |
631 | |
632 | /* Location of the binary. */ |
633 | EXTERN const char *_dl_origin_path; |
634 | |
635 | /* -1 if the dynamic linker should honor library load bias, |
636 | 0 if not, -2 use the default (honor biases for normal |
637 | binaries, don't honor for PIEs). */ |
638 | EXTERN ElfW(Addr) _dl_use_load_bias; |
639 | |
640 | /* Size of the static TLS block. */ |
641 | EXTERN size_t _dl_tls_static_size; |
642 | |
643 | /* Alignment requirement of the static TLS block. */ |
644 | EXTERN size_t _dl_tls_static_align; |
645 | |
646 | /* Size of surplus space in the static TLS area for dynamically |
647 | loaded modules with IE-model TLS or for TLSDESC optimization. |
648 | See comments in elf/dl-tls.c where it is initialized. */ |
649 | EXTERN size_t _dl_tls_static_surplus; |
650 | |
651 | /* Name of the shared object to be profiled (if any). */ |
652 | EXTERN const char *_dl_profile; |
653 | /* Filename of the output file. */ |
654 | EXTERN const char *_dl_profile_output; |
655 | /* Name of the object we want to trace the prelinking. */ |
656 | EXTERN const char *_dl_trace_prelink; |
657 | /* Map of shared object to be prelink traced. */ |
658 | EXTERN struct link_map *_dl_trace_prelink_map; |
659 | |
660 | /* All search directories defined at startup. This is assigned a |
661 | non-NULL pointer by the ld.so startup code (after initialization |
662 | to NULL), so this can also serve as an indicator whether a copy |
663 | of ld.so is initialized and active. See the rtld_active function |
664 | below. */ |
665 | EXTERN struct r_search_path_elem *_dl_init_all_dirs; |
666 | |
667 | #ifdef NEED_DL_SYSINFO |
668 | /* Syscall handling improvements. This is very specific to x86. */ |
669 | EXTERN uintptr_t _dl_sysinfo; |
670 | #endif |
671 | |
672 | #ifdef NEED_DL_SYSINFO_DSO |
673 | /* The vsyscall page is a virtual DSO pre-mapped by the kernel. |
674 | This points to its ELF header. */ |
675 | EXTERN const ElfW(Ehdr) *_dl_sysinfo_dso; |
676 | |
677 | /* At startup time we set up the normal DSO data structure for it, |
678 | and this points to it. */ |
679 | EXTERN struct link_map *_dl_sysinfo_map; |
680 | |
681 | # define PROCINFO_DECL |
682 | # ifndef PROCINFO_CLASS |
683 | # define PROCINFO_CLASS EXTERN |
684 | # endif |
685 | # include <dl-vdso-setup.c> |
686 | #endif |
687 | |
688 | /* Mask for more hardware capabilities that are available on some |
689 | platforms. */ |
690 | EXTERN uint64_t _dl_hwcap2; |
691 | |
692 | EXTERN enum dso_sort_algorithm _dl_dso_sort_algo; |
693 | |
694 | #ifdef SHARED |
695 | /* We add a function table to _rtld_global which is then used to |
696 | call the function instead of going through the PLT. The result |
697 | is that we can avoid exporting the functions and we do not jump |
698 | PLT relocations in libc.so. */ |
699 | void (*_dl_debug_printf) (const char *, ...) |
700 | __attribute__ ((__format__ (__printf__, 1, 2))); |
701 | void (*_dl_mcount) (ElfW(Addr) frompc, ElfW(Addr) selfpc); |
702 | lookup_t (*_dl_lookup_symbol_x) (const char *, struct link_map *, |
703 | const ElfW(Sym) **, struct r_scope_elem *[], |
704 | const struct r_found_version *, int, int, |
705 | struct link_map *); |
706 | void *(*_dl_open) (const char *file, int mode, const void *caller_dlopen, |
707 | Lmid_t nsid, int argc, char *argv[], char *env[]); |
708 | void (*_dl_close) (void *map); |
709 | /* libdl in a secondary namespace (after dlopen) must use |
710 | _dl_catch_error from the main namespace, so it has to be |
711 | exported in some way. */ |
712 | int (*_dl_catch_error) (const char **objname, const char **errstring, |
713 | bool *mallocedp, void (*operate) (void *), |
714 | void *args); |
715 | /* libdl in a secondary namespace must use free from the base |
716 | namespace. */ |
717 | void (*_dl_error_free) (void *); |
718 | void *(*_dl_tls_get_addr_soft) (struct link_map *); |
719 | |
720 | /* Called from __libc_shared to deallocate malloc'ed memory. */ |
721 | void (*_dl_libc_freeres) (void); |
722 | |
723 | /* Implementation of _dl_find_object. The public entry point is in |
724 | libc, and this is patched by __rtld_static_init to support static |
725 | dlopen. */ |
726 | int (*_dl_find_object) (void *, struct dl_find_object *); |
727 | |
728 | #ifdef HAVE_DL_DISCOVER_OSVERSION |
729 | int (*_dl_discover_osversion) (void); |
730 | #endif |
731 | |
732 | /* Dynamic linker operations used after static dlopen. */ |
733 | const struct dlfcn_hook *_dl_dlfcn_hook; |
734 | |
735 | /* List of auditing interfaces. */ |
736 | struct audit_ifaces *_dl_audit; |
737 | unsigned int _dl_naudit; |
738 | }; |
739 | # define __rtld_global_attribute__ |
740 | # if IS_IN (rtld) |
741 | # define __rtld_local_attribute__ __attribute__ ((visibility ("hidden"))) |
742 | extern struct rtld_global_ro _rtld_local_ro |
743 | attribute_relro __rtld_local_attribute__; |
744 | extern struct rtld_global_ro _rtld_global_ro |
745 | attribute_relro __rtld_global_attribute__; |
746 | # undef __rtld_local_attribute__ |
747 | # else |
748 | /* We cheat a bit here. We declare the variable as as const even |
749 | though it is at startup. */ |
750 | extern const struct rtld_global_ro _rtld_global_ro |
751 | attribute_relro __rtld_global_attribute__; |
752 | # endif |
753 | # undef __rtld_global_attribute__ |
754 | #endif |
755 | #undef EXTERN |
756 | |
757 | #ifndef SHARED |
758 | /* dl-support.c defines these and initializes them early on. */ |
759 | extern const ElfW(Phdr) *_dl_phdr; |
760 | extern size_t _dl_phnum; |
761 | #endif |
762 | |
763 | #if PTHREAD_IN_LIBC |
764 | /* This function changes the permissions of all stacks (not just those |
765 | of the main stack). */ |
766 | int _dl_make_stacks_executable (void **stack_endp) attribute_hidden; |
767 | #else |
768 | /* This is the initial value of GL(dl_make_stack_executable_hook). |
769 | A threads library can change it. The ld.so implementation changes |
770 | the permissions of the main stack only. */ |
771 | extern int _dl_make_stack_executable (void **stack_endp); |
772 | rtld_hidden_proto (_dl_make_stack_executable) |
773 | #endif |
774 | |
775 | /* Variable pointing to the end of the stack (or close to it). This value |
776 | must be constant over the runtime of the application. Some programs |
777 | might use the variable which results in copy relocations on some |
778 | platforms. But this does not matter, ld.so can always use the local |
779 | copy. */ |
780 | extern void *__libc_stack_end |
781 | #ifndef LIBC_STACK_END_NOT_RELRO |
782 | attribute_relro |
783 | #endif |
784 | ; |
785 | rtld_hidden_proto (__libc_stack_end) |
786 | |
787 | /* Parameters passed to the dynamic linker. */ |
788 | extern int _dl_argc attribute_hidden attribute_relro; |
789 | extern char **_dl_argv |
790 | #ifndef DL_ARGV_NOT_RELRO |
791 | attribute_relro |
792 | #endif |
793 | ; |
794 | rtld_hidden_proto (_dl_argv) |
795 | #if IS_IN (rtld) |
796 | extern unsigned int _dl_skip_args attribute_hidden |
797 | # ifndef DL_ARGV_NOT_RELRO |
798 | attribute_relro |
799 | # endif |
800 | ; |
801 | extern unsigned int _dl_skip_args_internal attribute_hidden |
802 | # ifndef DL_ARGV_NOT_RELRO |
803 | attribute_relro |
804 | # endif |
805 | ; |
806 | #endif |
807 | #define rtld_progname _dl_argv[0] |
808 | |
809 | /* Flag set at startup and cleared when the last initializer has run. */ |
810 | extern int _dl_starting_up; |
811 | weak_extern (_dl_starting_up) |
812 | rtld_hidden_proto (_dl_starting_up) |
813 | |
814 | /* Random data provided by the kernel. */ |
815 | extern void *_dl_random attribute_hidden attribute_relro; |
816 | |
817 | /* Write message on the debug file descriptor. The parameters are |
818 | interpreted as for a `printf' call. All the lines start with a |
819 | tag showing the PID. */ |
820 | extern void _dl_debug_printf (const char *fmt, ...) |
821 | __attribute__ ((__format__ (__printf__, 1, 2))) attribute_hidden; |
822 | |
823 | /* Write message on the debug file descriptor. The parameters are |
824 | interpreted as for a `printf' call. All the lines buf the first |
825 | start with a tag showing the PID. */ |
826 | extern void _dl_debug_printf_c (const char *fmt, ...) |
827 | __attribute__ ((__format__ (__printf__, 1, 2))) attribute_hidden; |
828 | |
829 | |
830 | /* Write a message on the specified descriptor FD. The parameters are |
831 | interpreted as for a `printf' call. */ |
832 | #if IS_IN (rtld) || !defined (SHARED) |
833 | extern void _dl_dprintf (int fd, const char *fmt, ...) |
834 | __attribute__ ((__format__ (__printf__, 2, 3))) |
835 | attribute_hidden; |
836 | #else |
837 | __attribute__ ((always_inline, __format__ (__printf__, 2, 3))) |
838 | static inline void |
839 | _dl_dprintf (int fd, const char *fmt, ...) |
840 | { |
841 | /* Use local declaration to avoid includign <stdio.h>. */ |
842 | extern int __dprintf(int fd, const char *format, ...) attribute_hidden; |
843 | __dprintf (fd, fmt, __builtin_va_arg_pack ()); |
844 | } |
845 | #endif |
846 | |
847 | /* Write LENGTH bytes at BUFFER to FD, like write. Returns the number |
848 | of bytes written on success, or a negative error constant on |
849 | failure. */ |
850 | ssize_t _dl_write (int fd, const void *buffer, size_t length) |
851 | attribute_hidden; |
852 | |
853 | /* Write a message on the specified descriptor standard output. The |
854 | parameters are interpreted as for a `printf' call. */ |
855 | void _dl_printf (const char *fmt, ...) |
856 | attribute_hidden __attribute__ ((__format__ (__printf__, 1, 2))); |
857 | |
858 | /* Write a message on the specified descriptor standard error. The |
859 | parameters are interpreted as for a `printf' call. */ |
860 | void _dl_error_printf (const char *fmt, ...) |
861 | attribute_hidden __attribute__ ((__format__ (__printf__, 1, 2))); |
862 | |
863 | /* Write a message on the specified descriptor standard error and exit |
864 | the program. The parameters are interpreted as for a `printf' call. */ |
865 | void _dl_fatal_printf (const char *fmt, ...) |
866 | __attribute__ ((__format__ (__printf__, 1, 2), __noreturn__)); |
867 | rtld_hidden_proto (_dl_fatal_printf) |
868 | |
869 | /* An exception raised by the _dl_signal_error function family and |
870 | caught by _dl_catch_error function family. Exceptions themselves |
871 | are copied as part of the raise operation, but the strings are |
872 | not. */ |
873 | struct dl_exception |
874 | { |
875 | const char *objname; |
876 | const char *errstring; |
877 | |
878 | /* This buffer typically stores both objname and errstring |
879 | above. */ |
880 | char *message_buffer; |
881 | }; |
882 | |
883 | /* Creates a new exception. This calls malloc; if allocation fails, |
884 | dummy values are inserted. OBJECT is the name of the problematical |
885 | shared object, or null if its a general problem. ERRSTRING is a |
886 | string describing the specific problem. */ |
887 | void _dl_exception_create (struct dl_exception *, const char *object, |
888 | const char *errstring) |
889 | __attribute__ ((nonnull (1, 3))); |
890 | rtld_hidden_proto (_dl_exception_create) |
891 | |
892 | /* Used internally to implement dlerror message freeing. See |
893 | include/dlfcn.h and dlfcn/dlerror.c. */ |
894 | void _dl_error_free (void *ptr) attribute_hidden; |
895 | |
896 | /* Like _dl_exception_create, but create errstring from a format |
897 | string FMT. Currently, only "%s" and "%%" are supported as format |
898 | directives. */ |
899 | void _dl_exception_create_format (struct dl_exception *, const char *objname, |
900 | const char *fmt, ...) |
901 | __attribute__ ((nonnull (1, 3), format (printf, 3, 4))); |
902 | rtld_hidden_proto (_dl_exception_create_format) |
903 | |
904 | /* Deallocate the exception, freeing allocated buffers (if |
905 | possible). */ |
906 | void _dl_exception_free (struct dl_exception *) |
907 | __attribute__ ((nonnull (1))); |
908 | rtld_hidden_proto (_dl_exception_free) |
909 | |
910 | /* This function is called by all the internal dynamic linker |
911 | functions when they encounter an error. ERRCODE is either an |
912 | `errno' code or zero; it specifies the return value of |
913 | _dl_catch_error. OCCASION is included in the error message if the |
914 | process is terminated immediately. */ |
915 | void _dl_signal_exception (int errcode, struct dl_exception *, |
916 | const char *occasion) |
917 | __attribute__ ((__noreturn__)); |
918 | libc_hidden_proto (_dl_signal_exception) |
919 | |
920 | /* Like _dl_signal_exception, but creates the exception first. */ |
921 | extern void _dl_signal_error (int errcode, const char *object, |
922 | const char *occasion, const char *errstring) |
923 | __attribute__ ((__noreturn__)); |
924 | libc_hidden_proto (_dl_signal_error) |
925 | |
926 | /* Like _dl_signal_exception, but may return when called in the |
927 | context of _dl_receive_error. This is only used during ld.so |
928 | bootstrap. In static and profiled builds, this is equivalent to |
929 | _dl_signal_exception. */ |
930 | #if IS_IN (rtld) |
931 | extern void _dl_signal_cexception (int errcode, struct dl_exception *, |
932 | const char *occasion) attribute_hidden; |
933 | #else |
934 | __attribute__ ((always_inline)) |
935 | static inline void |
936 | _dl_signal_cexception (int errcode, struct dl_exception *exception, |
937 | const char *occasion) |
938 | { |
939 | _dl_signal_exception (errcode, exception, occasion); |
940 | } |
941 | #endif |
942 | |
943 | /* See _dl_signal_cexception above. */ |
944 | #if IS_IN (rtld) |
945 | extern void _dl_signal_cerror (int errcode, const char *object, |
946 | const char *occasion, const char *errstring) |
947 | attribute_hidden; |
948 | #else |
949 | __attribute__ ((always_inline)) |
950 | static inline void |
951 | _dl_signal_cerror (int errcode, const char *object, |
952 | const char *occasion, const char *errstring) |
953 | { |
954 | _dl_signal_error (errcode, object, occasion, errstring); |
955 | } |
956 | #endif |
957 | |
958 | /* Call OPERATE, receiving errors from `dl_signal_cerror'. Unlike |
959 | `_dl_catch_error' the operation is resumed after the OPERATE |
960 | function returns. |
961 | ARGS is passed as argument to OPERATE. */ |
962 | extern void _dl_receive_error (receiver_fct fct, void (*operate) (void *), |
963 | void *args) attribute_hidden; |
964 | |
965 | /* Call OPERATE, catching errors from `_dl_signal_error' and related |
966 | functions. If there is no error, *ERRSTRING is set to null. If |
967 | there is an error, *ERRSTRING is set to a string constructed from |
968 | the strings passed to _dl_signal_error, and the error code passed |
969 | is the return value and *OBJNAME is set to the object name which |
970 | experienced the problems. ERRSTRING if nonzero points to a |
971 | malloc'ed string which the caller has to free after use. ARGS is |
972 | passed as argument to OPERATE. MALLOCEDP is set to true only if |
973 | the returned string is allocated using the libc's malloc. */ |
974 | extern int _dl_catch_error (const char **objname, const char **errstring, |
975 | bool *mallocedp, void (*operate) (void *), |
976 | void *args); |
977 | libc_hidden_proto (_dl_catch_error) |
978 | |
979 | /* Used for initializing GLRO (_dl_catch_error). */ |
980 | extern __typeof__ (_dl_catch_error) _rtld_catch_error attribute_hidden; |
981 | |
982 | /* Call OPERATE (ARGS). If no error occurs, set *EXCEPTION to zero. |
983 | Otherwise, store a copy of the raised exception in *EXCEPTION, |
984 | which has to be freed by _dl_exception_free. As a special case, if |
985 | EXCEPTION is null, call OPERATE (ARGS) with exception handling |
986 | disabled (so that exceptions are fatal). */ |
987 | int _dl_catch_exception (struct dl_exception *exception, |
988 | void (*operate) (void *), void *args); |
989 | libc_hidden_proto (_dl_catch_exception) |
990 | |
991 | /* Open the shared object NAME and map in its segments. |
992 | LOADER's DT_RPATH is used in searching for NAME. |
993 | If the object is already opened, returns its existing map. */ |
994 | extern struct link_map *_dl_map_object (struct link_map *loader, |
995 | const char *name, |
996 | int type, int trace_mode, int mode, |
997 | Lmid_t nsid) attribute_hidden; |
998 | |
999 | /* Call _dl_map_object on the dependencies of MAP, and set up |
1000 | MAP->l_searchlist. PRELOADS points to a vector of NPRELOADS previously |
1001 | loaded objects that will be inserted into MAP->l_searchlist after MAP |
1002 | but before its dependencies. */ |
1003 | extern void _dl_map_object_deps (struct link_map *map, |
1004 | struct link_map **preloads, |
1005 | unsigned int npreloads, int trace_mode, |
1006 | int open_mode) |
1007 | attribute_hidden; |
1008 | |
1009 | /* Cache the locations of MAP's hash table. */ |
1010 | extern void _dl_setup_hash (struct link_map *map) attribute_hidden; |
1011 | |
1012 | |
1013 | /* Collect the directories in the search path for LOADER's dependencies. |
1014 | The data structure is defined in <dlfcn.h>. If COUNTING is true, |
1015 | SI->dls_cnt and SI->dls_size are set; if false, those must be as set |
1016 | by a previous call with COUNTING set, and SI must point to SI->dls_size |
1017 | bytes to be used in filling in the result. */ |
1018 | extern void _dl_rtld_di_serinfo (struct link_map *loader, |
1019 | Dl_serinfo *si, bool counting); |
1020 | |
1021 | /* Process PT_GNU_PROPERTY program header PH in module L after |
1022 | PT_LOAD segments are mapped from file FD. */ |
1023 | void _dl_process_pt_gnu_property (struct link_map *l, int fd, |
1024 | const ElfW(Phdr) *ph); |
1025 | |
1026 | |
1027 | /* Search loaded objects' symbol tables for a definition of the symbol |
1028 | referred to by UNDEF. *SYM is the symbol table entry containing the |
1029 | reference; it is replaced with the defining symbol, and the base load |
1030 | address of the defining object is returned. SYMBOL_SCOPE is a |
1031 | null-terminated list of object scopes to search; each object's |
1032 | l_searchlist (i.e. the segment of the dependency tree starting at that |
1033 | object) is searched in turn. REFERENCE_NAME should name the object |
1034 | containing the reference; it is used in error messages. |
1035 | TYPE_CLASS describes the type of symbol we are looking for. */ |
1036 | enum |
1037 | { |
1038 | /* If necessary add dependency between user and provider object. */ |
1039 | DL_LOOKUP_ADD_DEPENDENCY = 1, |
1040 | /* Return most recent version instead of default version for |
1041 | unversioned lookup. */ |
1042 | DL_LOOKUP_RETURN_NEWEST = 2, |
1043 | /* Set if dl_lookup* called with GSCOPE lock held. */ |
1044 | DL_LOOKUP_GSCOPE_LOCK = 4, |
1045 | /* Set if dl_lookup is called for non-lazy relocation processing |
1046 | from _dl_relocate_object in elf/dl-reloc.c. */ |
1047 | DL_LOOKUP_FOR_RELOCATE = 8, |
1048 | }; |
1049 | |
1050 | /* Lookup versioned symbol. */ |
1051 | extern lookup_t _dl_lookup_symbol_x (const char *undef, |
1052 | struct link_map *undef_map, |
1053 | const ElfW(Sym) **sym, |
1054 | struct r_scope_elem *symbol_scope[], |
1055 | const struct r_found_version *version, |
1056 | int type_class, int flags, |
1057 | struct link_map *skip_map) |
1058 | attribute_hidden; |
1059 | |
1060 | |
1061 | /* Restricted version of _dl_lookup_symbol_x. Searches MAP (and only |
1062 | MAP) for the symbol UNDEF_NAME, with GNU hash NEW_HASH (computed |
1063 | with dl_new_hash), symbol version VERSION, and symbol version hash |
1064 | VERSION_HASH (computed with _dl_elf_hash). Returns a pointer to |
1065 | the symbol table entry in MAP on success, or NULL on failure. MAP |
1066 | must have symbol versioning information, or otherwise the result is |
1067 | undefined. */ |
1068 | const ElfW(Sym) *_dl_lookup_direct (struct link_map *map, |
1069 | const char *undef_name, |
1070 | uint32_t new_hash, |
1071 | const char *version, |
1072 | uint32_t version_hash) attribute_hidden; |
1073 | |
1074 | /* Add the new link_map NEW to the end of the namespace list. */ |
1075 | extern void _dl_add_to_namespace_list (struct link_map *new, Lmid_t nsid) |
1076 | attribute_hidden; |
1077 | |
1078 | /* Allocate a `struct link_map' for a new object being loaded. */ |
1079 | extern struct link_map *_dl_new_object (char *realname, const char *libname, |
1080 | int type, struct link_map *loader, |
1081 | int mode, Lmid_t nsid) |
1082 | attribute_hidden; |
1083 | |
1084 | /* Relocate the given object (if it hasn't already been). |
1085 | SCOPE is passed to _dl_lookup_symbol in symbol lookups. |
1086 | If RTLD_LAZY is set in RELOC-MODE, don't relocate its PLT. */ |
1087 | extern void _dl_relocate_object (struct link_map *map, |
1088 | struct r_scope_elem *scope[], |
1089 | int reloc_mode, int consider_profiling) |
1090 | attribute_hidden; |
1091 | |
1092 | /* Protect PT_GNU_RELRO area. */ |
1093 | extern void _dl_protect_relro (struct link_map *map) attribute_hidden; |
1094 | |
1095 | /* Call _dl_signal_error with a message about an unhandled reloc type. |
1096 | TYPE is the result of ELFW(R_TYPE) (r_info), i.e. an R_<CPU>_* value. |
1097 | PLT is nonzero if this was a PLT reloc; it just affects the message. */ |
1098 | extern void _dl_reloc_bad_type (struct link_map *map, |
1099 | unsigned int type, int plt) |
1100 | attribute_hidden __attribute__ ((__noreturn__)); |
1101 | |
1102 | /* Resolve conflicts if prelinking. */ |
1103 | extern void _dl_resolve_conflicts (struct link_map *l, |
1104 | ElfW(Rela) *conflict, |
1105 | ElfW(Rela) *conflictend) |
1106 | attribute_hidden; |
1107 | |
1108 | /* Check the version dependencies of all objects available through |
1109 | MAP. If VERBOSE print some more diagnostics. */ |
1110 | extern int _dl_check_all_versions (struct link_map *map, int verbose, |
1111 | int trace_mode) attribute_hidden; |
1112 | |
1113 | /* Check the version dependencies for MAP. If VERBOSE print some more |
1114 | diagnostics. */ |
1115 | extern int _dl_check_map_versions (struct link_map *map, int verbose, |
1116 | int trace_mode) attribute_hidden; |
1117 | |
1118 | /* Initialize the object in SCOPE by calling the constructors with |
1119 | ARGC, ARGV, and ENV as the parameters. */ |
1120 | extern void _dl_init (struct link_map *main_map, int argc, char **argv, |
1121 | char **env) attribute_hidden; |
1122 | |
1123 | /* Call the finalizer functions of all shared objects whose |
1124 | initializer functions have completed. */ |
1125 | extern void _dl_fini (void) attribute_hidden; |
1126 | |
1127 | /* Invoke the DT_FINI_ARRAY and DT_FINI destructors for MAP, which |
1128 | must be a struct link_map *. Can be used as an argument to |
1129 | _dl_catch_exception. */ |
1130 | void _dl_call_fini (void *map) attribute_hidden; |
1131 | |
1132 | /* Sort array MAPS according to dependencies of the contained objects. |
1133 | If FORCE_FIRST, MAPS[0] keeps its place even if the dependencies |
1134 | say otherwise. */ |
1135 | extern void _dl_sort_maps (struct link_map **maps, unsigned int nmaps, |
1136 | bool force_first, bool for_fini) attribute_hidden; |
1137 | |
1138 | /* The dynamic linker calls this function before and having changing |
1139 | any shared object mappings. The `r_state' member of `struct r_debug' |
1140 | says what change is taking place. This function's address is |
1141 | the value of the `r_brk' member. */ |
1142 | extern void _dl_debug_state (void); |
1143 | rtld_hidden_proto (_dl_debug_state) |
1144 | |
1145 | /* Initialize `struct r_debug_extended' for the namespace NS. LDBASE |
1146 | is the run-time load address of the dynamic linker, to be put in the |
1147 | `r_ldbase' member. Return the address of the structure. */ |
1148 | extern struct r_debug *_dl_debug_initialize (ElfW(Addr) ldbase, Lmid_t ns) |
1149 | attribute_hidden; |
1150 | |
1151 | /* Update the `r_map' member and return the address of `struct r_debug' |
1152 | of the namespace NS. */ |
1153 | extern struct r_debug *_dl_debug_update (Lmid_t ns) attribute_hidden; |
1154 | |
1155 | /* Initialize the basic data structure for the search paths. SOURCE |
1156 | is either "LD_LIBRARY_PATH" or "--library-path". |
1157 | GLIBC_HWCAPS_PREPEND adds additional glibc-hwcaps subdirectories to |
1158 | search. GLIBC_HWCAPS_MASK is used to filter the built-in |
1159 | subdirectories if not NULL. */ |
1160 | extern void _dl_init_paths (const char *library_path, const char *source, |
1161 | const char *glibc_hwcaps_prepend, |
1162 | const char *glibc_hwcaps_mask) |
1163 | attribute_hidden; |
1164 | |
1165 | /* Gather the information needed to install the profiling tables and start |
1166 | the timers. */ |
1167 | extern void _dl_start_profile (void) attribute_hidden; |
1168 | |
1169 | /* The actual functions used to keep book on the calls. */ |
1170 | extern void _dl_mcount (ElfW(Addr) frompc, ElfW(Addr) selfpc); |
1171 | rtld_hidden_proto (_dl_mcount) |
1172 | |
1173 | /* This function is simply a wrapper around the _dl_mcount function |
1174 | which does not require a FROMPC parameter since this is the |
1175 | calling function. */ |
1176 | extern void _dl_mcount_wrapper (void *selfpc); |
1177 | |
1178 | /* Show the members of the auxiliary array passed up from the kernel. */ |
1179 | extern void _dl_show_auxv (void) attribute_hidden; |
1180 | |
1181 | /* Return all environment variables starting with `LD_', one after the |
1182 | other. */ |
1183 | extern char *_dl_next_ld_env_entry (char ***position) attribute_hidden; |
1184 | |
1185 | /* Return an array with the names of the important hardware |
1186 | capabilities. PREPEND is a colon-separated list of glibc-hwcaps |
1187 | directories to search first. MASK is a colon-separated list used |
1188 | to filter the built-in glibc-hwcaps subdirectories. The length of |
1189 | the array is written to *SZ, and the maximum of all strings length |
1190 | is written to *MAX_CAPSTRLEN. */ |
1191 | const struct r_strlenpair *_dl_important_hwcaps (const char *prepend, |
1192 | const char *mask, |
1193 | size_t *sz, |
1194 | size_t *max_capstrlen) |
1195 | attribute_hidden; |
1196 | |
1197 | /* Look up NAME in ld.so.cache and return the file name stored there, |
1198 | or null if none is found. Caller must free returned string. */ |
1199 | extern char *_dl_load_cache_lookup (const char *name) attribute_hidden; |
1200 | |
1201 | /* If the system does not support MAP_COPY we cannot leave the file open |
1202 | all the time since this would create problems when the file is replaced. |
1203 | Therefore we provide this function to close the file and open it again |
1204 | once needed. */ |
1205 | extern void _dl_unload_cache (void) attribute_hidden; |
1206 | |
1207 | /* System-dependent function to read a file's whole contents in the |
1208 | most convenient manner available. *SIZEP gets the size of the |
1209 | file. On error MAP_FAILED is returned. */ |
1210 | extern void *_dl_sysdep_read_whole_file (const char *file, size_t *sizep, |
1211 | int prot) attribute_hidden; |
1212 | |
1213 | /* System-specific function to do initial startup for the dynamic linker. |
1214 | After this, file access calls and getenv must work. This is responsible |
1215 | for setting __libc_enable_secure if we need to be secure (e.g. setuid), |
1216 | and for setting _dl_argc and _dl_argv, and then calling _dl_main. */ |
1217 | extern ElfW(Addr) _dl_sysdep_start (void **start_argptr, |
1218 | void (*dl_main) (const ElfW(Phdr) *phdr, |
1219 | ElfW(Word) phnum, |
1220 | ElfW(Addr) *user_entry, |
1221 | ElfW(auxv_t) *auxv)) |
1222 | attribute_hidden; |
1223 | |
1224 | extern void _dl_sysdep_start_cleanup (void) attribute_hidden; |
1225 | |
1226 | |
1227 | /* Determine next available module ID and set the L l_tls_modid. */ |
1228 | extern void _dl_assign_tls_modid (struct link_map *l) attribute_hidden; |
1229 | |
1230 | /* Count the modules with TLS segments. */ |
1231 | extern size_t _dl_count_modids (void) attribute_hidden; |
1232 | |
1233 | /* Calculate offset of the TLS blocks in the static TLS block. */ |
1234 | extern void _dl_determine_tlsoffset (void) attribute_hidden; |
1235 | |
1236 | /* Calculate the size of the static TLS surplus, when the given |
1237 | number of audit modules are loaded. */ |
1238 | void _dl_tls_static_surplus_init (size_t naudit) attribute_hidden; |
1239 | |
1240 | /* This function is called very early from dl_main to set up TLS and |
1241 | other thread-related data structures. */ |
1242 | void __tls_pre_init_tp (void) attribute_hidden; |
1243 | |
1244 | /* This function is called after processor-specific initialization of |
1245 | the TCB and thread pointer via TLS_INIT_TP, to complete very early |
1246 | initialization of the thread library. */ |
1247 | void __tls_init_tp (void) attribute_hidden; |
1248 | |
1249 | #ifndef SHARED |
1250 | /* Set up the TCB for statically linked applications. This is called |
1251 | early during startup because we always use TLS (for errno and the |
1252 | stack protector, among other things). */ |
1253 | void __libc_setup_tls (void); |
1254 | |
1255 | # if ENABLE_STATIC_PIE |
1256 | /* Relocate static executable with PIE. */ |
1257 | extern void _dl_relocate_static_pie (void) attribute_hidden; |
1258 | |
1259 | /* Get a pointer to _dl_main_map. */ |
1260 | extern struct link_map * _dl_get_dl_main_map (void) |
1261 | __attribute__ ((visibility ("hidden" ))); |
1262 | # else |
1263 | # define _dl_relocate_static_pie() |
1264 | # endif |
1265 | #endif |
1266 | |
1267 | /* Perform early memory allocation, avoding a TCB dependency. |
1268 | Terminate the process if allocation fails. May attempt to use |
1269 | brk. */ |
1270 | void *_dl_early_allocate (size_t size) attribute_hidden; |
1271 | |
1272 | /* Initialize the DSO sort algorithm to use. */ |
1273 | #if !HAVE_TUNABLES |
1274 | static inline void |
1275 | __always_inline |
1276 | _dl_sort_maps_init (void) |
1277 | { |
1278 | /* This is optimized out if tunables are not enabled. */ |
1279 | } |
1280 | #else |
1281 | extern void _dl_sort_maps_init (void) attribute_hidden; |
1282 | #endif |
1283 | |
1284 | /* Initialization of libpthread for statically linked applications. |
1285 | If libpthread is not linked in, this is an empty function. */ |
1286 | void __pthread_initialize_minimal (void) weak_function; |
1287 | |
1288 | /* Allocate memory for static TLS block (unless MEM is nonzero) and dtv. */ |
1289 | extern void *_dl_allocate_tls (void *mem); |
1290 | rtld_hidden_proto (_dl_allocate_tls) |
1291 | |
1292 | /* Get size and alignment requirements of the static TLS block. */ |
1293 | extern void _dl_get_tls_static_info (size_t *sizep, size_t *alignp); |
1294 | |
1295 | extern void _dl_allocate_static_tls (struct link_map *map) attribute_hidden; |
1296 | |
1297 | /* These are internal entry points to the two halves of _dl_allocate_tls, |
1298 | only used within rtld.c itself at startup time. */ |
1299 | extern void *_dl_allocate_tls_storage (void) attribute_hidden; |
1300 | extern void *_dl_allocate_tls_init (void *, bool); |
1301 | rtld_hidden_proto (_dl_allocate_tls_init) |
1302 | |
1303 | /* Deallocate memory allocated with _dl_allocate_tls. */ |
1304 | extern void _dl_deallocate_tls (void *tcb, bool dealloc_tcb); |
1305 | rtld_hidden_proto (_dl_deallocate_tls) |
1306 | |
1307 | extern void _dl_nothread_init_static_tls (struct link_map *) attribute_hidden; |
1308 | |
1309 | /* Find origin of the executable. */ |
1310 | extern const char *_dl_get_origin (void) attribute_hidden; |
1311 | |
1312 | /* Count DSTs. */ |
1313 | extern size_t _dl_dst_count (const char *name) attribute_hidden; |
1314 | |
1315 | /* Substitute DST values. */ |
1316 | extern char *_dl_dst_substitute (struct link_map *l, const char *name, |
1317 | char *result) attribute_hidden; |
1318 | |
1319 | /* Open the shared object NAME, relocate it, and run its initializer if it |
1320 | hasn't already been run. MODE is as for `dlopen' (see <dlfcn.h>). If |
1321 | the object is already opened, returns its existing map. */ |
1322 | extern void *_dl_open (const char *name, int mode, const void *caller, |
1323 | Lmid_t nsid, int argc, char *argv[], char *env[]) |
1324 | attribute_hidden; |
1325 | |
1326 | /* Free or queue for freeing scope OLD. If other threads might be |
1327 | in the middle of _dl_fixup, _dl_profile_fixup or dl*sym using the |
1328 | old scope, OLD can't be freed until no thread is using it. */ |
1329 | extern int _dl_scope_free (void *) attribute_hidden; |
1330 | |
1331 | |
1332 | /* Add module to slot information data. If DO_ADD is false, only the |
1333 | required memory is allocated. Must be called with GL |
1334 | (dl_load_tls_lock) acquired. If the function has already been called |
1335 | for the link map L with !do_add, then this function will not raise |
1336 | an exception, otherwise it is possible that it encounters a memory |
1337 | allocation failure. */ |
1338 | extern void _dl_add_to_slotinfo (struct link_map *l, bool do_add) |
1339 | attribute_hidden; |
1340 | |
1341 | /* Update slot information data for at least the generation of the |
1342 | module with the given index. */ |
1343 | extern struct link_map *_dl_update_slotinfo (unsigned long int req_modid) |
1344 | attribute_hidden; |
1345 | |
1346 | /* Look up the module's TLS block as for __tls_get_addr, |
1347 | but never touch anything. Return null if it's not allocated yet. */ |
1348 | extern void *_dl_tls_get_addr_soft (struct link_map *l) attribute_hidden; |
1349 | |
1350 | extern int _dl_addr_inside_object (struct link_map *l, const ElfW(Addr) addr) |
1351 | attribute_hidden; |
1352 | |
1353 | /* Show show of an object. */ |
1354 | extern void _dl_show_scope (struct link_map *new, int from) |
1355 | attribute_hidden; |
1356 | |
1357 | extern struct link_map *_dl_find_dso_for_object (const ElfW(Addr) addr); |
1358 | rtld_hidden_proto (_dl_find_dso_for_object) |
1359 | |
1360 | /* Initialization which is normally done by the dynamic linker. */ |
1361 | extern void _dl_non_dynamic_init (void) |
1362 | attribute_hidden; |
1363 | |
1364 | /* Used by static binaries to check the auxiliary vector. */ |
1365 | extern void _dl_aux_init (ElfW(auxv_t) *av) |
1366 | attribute_hidden; |
1367 | |
1368 | /* Initialize the static TLS space for the link map in all existing |
1369 | threads. */ |
1370 | #if PTHREAD_IN_LIBC |
1371 | void _dl_init_static_tls (struct link_map *map) attribute_hidden; |
1372 | #endif |
1373 | static inline void |
1374 | dl_init_static_tls (struct link_map *map) |
1375 | { |
1376 | #if PTHREAD_IN_LIBC |
1377 | /* The stack list is available to ld.so, so the initialization can |
1378 | be handled within ld.so directly. */ |
1379 | _dl_init_static_tls (map); |
1380 | #else |
1381 | GL (dl_init_static_tls) (map); |
1382 | #endif |
1383 | } |
1384 | |
1385 | #ifndef SHARED |
1386 | /* Called before relocating ld.so during static dlopen. This can be |
1387 | used to partly initialize the dormant ld.so copy in the static |
1388 | dlopen namespace. */ |
1389 | void __rtld_static_init (struct link_map *map) attribute_hidden; |
1390 | #endif |
1391 | |
1392 | /* Return true if the ld.so copy in this namespace is actually active |
1393 | and working. If false, the dl_open/dlfcn hooks have to be used to |
1394 | call into the outer dynamic linker (which happens after static |
1395 | dlopen). */ |
1396 | #ifdef SHARED |
1397 | static inline bool |
1398 | rtld_active (void) |
1399 | { |
1400 | /* The default-initialized variable does not have a non-zero |
1401 | dl_init_all_dirs member, so this allows us to recognize an |
1402 | initialized and active ld.so copy. */ |
1403 | return GLRO(dl_init_all_dirs) != NULL; |
1404 | } |
1405 | |
1406 | static inline struct auditstate * |
1407 | link_map_audit_state (struct link_map *l, size_t index) |
1408 | { |
1409 | if (l == &GL (dl_rtld_map)) |
1410 | /* The auditstate array is stored separately. */ |
1411 | return &GL (dl_rtld_auditstate) [index]; |
1412 | else |
1413 | { |
1414 | /* The auditstate array follows the link map in memory. */ |
1415 | struct auditstate *base = (struct auditstate *) (l + 1); |
1416 | return &base[index]; |
1417 | } |
1418 | } |
1419 | |
1420 | /* Call the la_objsearch from the audit modules from the link map L. If |
1421 | ORIGNAME is non NULL, it is updated with the revious name prior calling |
1422 | la_objsearch. */ |
1423 | const char *_dl_audit_objsearch (const char *name, struct link_map *l, |
1424 | unsigned int code) |
1425 | attribute_hidden; |
1426 | |
1427 | /* Call the la_activity from the audit modules from the link map L and issues |
1428 | the ACTION argument. */ |
1429 | void _dl_audit_activity_map (struct link_map *l, int action) |
1430 | attribute_hidden; |
1431 | |
1432 | /* Call the la_activity from the audit modules from the link map from the |
1433 | namespace NSID and issues the ACTION argument. */ |
1434 | void _dl_audit_activity_nsid (Lmid_t nsid, int action) |
1435 | attribute_hidden; |
1436 | |
1437 | /* Call the la_objopen from the audit modules for the link_map L on the |
1438 | namespace identification NSID. */ |
1439 | void _dl_audit_objopen (struct link_map *l, Lmid_t nsid) |
1440 | attribute_hidden; |
1441 | |
1442 | /* Call the la_objclose from the audit modules for the link_map L. */ |
1443 | void _dl_audit_objclose (struct link_map *l) |
1444 | attribute_hidden; |
1445 | |
1446 | /* Call the la_preinit from the audit modules for the link_map L. */ |
1447 | void _dl_audit_preinit (struct link_map *l); |
1448 | |
1449 | /* Call the la_symbind{32,64} from the audit modules for the link_map L. If |
1450 | RELOC_RESULT is NULL it assumes the symbol to be bind-now and will set |
1451 | the flags with LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT prior calling |
1452 | la_symbind{32,64}. */ |
1453 | void _dl_audit_symbind (struct link_map *l, struct reloc_result *reloc_result, |
1454 | const ElfW(Sym) *defsym, DL_FIXUP_VALUE_TYPE *value, |
1455 | lookup_t result) |
1456 | attribute_hidden; |
1457 | /* Same as _dl_audit_symbind, but also sets LA_SYMB_DLSYM flag. */ |
1458 | void _dl_audit_symbind_alt (struct link_map *l, const ElfW(Sym) *ref, |
1459 | void **value, lookup_t result); |
1460 | rtld_hidden_proto (_dl_audit_symbind_alt) |
1461 | void _dl_audit_pltenter (struct link_map *l, struct reloc_result *reloc_result, |
1462 | DL_FIXUP_VALUE_TYPE *value, void *regs, |
1463 | long int *framesize) |
1464 | attribute_hidden; |
1465 | void DL_ARCH_FIXUP_ATTRIBUTE _dl_audit_pltexit (struct link_map *l, |
1466 | ElfW(Word) reloc_arg, |
1467 | const void *inregs, |
1468 | void *outregs) |
1469 | attribute_hidden; |
1470 | #endif /* SHARED */ |
1471 | |
1472 | #if PTHREAD_IN_LIBC && defined SHARED |
1473 | /* Recursive locking implementation for use within the dynamic loader. |
1474 | Used to define the __rtld_lock_lock_recursive and |
1475 | __rtld_lock_unlock_recursive via <libc-lock.h>. Initialized to a |
1476 | no-op dummy implementation early. Similar |
1477 | to GL (dl_rtld_lock_recursive) and GL (dl_rtld_unlock_recursive) |
1478 | in !PTHREAD_IN_LIBC builds. */ |
1479 | extern int (*___rtld_mutex_lock) (pthread_mutex_t *) attribute_hidden; |
1480 | extern int (*___rtld_mutex_unlock) (pthread_mutex_t *lock) attribute_hidden; |
1481 | |
1482 | /* Called after libc has been loaded, but before RELRO is activated. |
1483 | Used to initialize the function pointers to the actual |
1484 | implementations. */ |
1485 | void __rtld_mutex_init (void) attribute_hidden; |
1486 | #else /* !PTHREAD_IN_LIBC */ |
1487 | static inline void |
1488 | __rtld_mutex_init (void) |
1489 | { |
1490 | /* The initialization happens later (!PTHREAD_IN_LIBC) or is not |
1491 | needed at all (!SHARED). */ |
1492 | } |
1493 | #endif /* !PTHREAD_IN_LIBC */ |
1494 | |
1495 | /* Implementation of GL (dl_libc_freeres). */ |
1496 | void __rtld_libc_freeres (void) attribute_hidden; |
1497 | |
1498 | void __thread_gscope_wait (void) attribute_hidden; |
1499 | # define THREAD_GSCOPE_WAIT() __thread_gscope_wait () |
1500 | |
1501 | __END_DECLS |
1502 | |
1503 | #endif /* ldsodefs.h */ |
1504 | |