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