1 /*        $NetBSD: rtld.h,v 1.150 2025/05/02 23:04:31 riastradh Exp $  */
2 
3 /*
4  * Copyright 1996 John D. Polstra.
5  * Copyright 1996 Matt Thomas <matt@3am-software.com>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *      This product includes software developed by John Polstra.
19  * 4. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #ifndef RTLD_H
35 #define RTLD_H
36 
37 #include <dlfcn.h>
38 #include <signal.h>
39 #include <stdbool.h>
40 #include <stddef.h>
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/queue.h>
44 #include <sys/exec_elf.h>
45 #include <sys/tls.h>
46 #include "rtldenv.h"
47 #include "link.h"
48 
49 #if defined(_RTLD_SOURCE)
50 
51 #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
52 #include "unwind.h"
53 #endif
54 
55 #ifndef   RTLD_DEFAULT_LIBRARY_PATH
56 #define   RTLD_DEFAULT_LIBRARY_PATH     "/usr/lib"
57 #endif
58 #define _PATH_LD_HINTS                            "/etc/ld.so.conf"
59 
60 extern size_t _rtld_pagesz;
61 
62 #define round_down(x)         ((x) & ~(_rtld_pagesz - 1))
63 #define round_up(x) round_down((x) + _rtld_pagesz - 1)
64 
65 #define NEW(type)   ((type *) xmalloc(sizeof(type)))
66 #define CNEW(type)  ((type *) xcalloc(sizeof(type)))
67 
68 /*
69  * Fill in a DoneList with an allocation large enough to hold all of
70  * the currently-loaded objects. Keep this in a macro since it calls
71  * alloca and we want that to occur within the scope of the caller.
72  * Callers must be built with -Wno-stack-protector.
73  */
74 #define _rtld_donelist_init(dlp)                                                \
75     ((dlp)->num_alloc = _rtld_objcount,                                         \
76     (dlp)->objs = alloca((dlp)->num_alloc * sizeof((dlp)->objs[0])),  \
77     assert((dlp)->objs != NULL),                                                \
78     (dlp)->num_used = 0)
79 
80 
81 typedef struct Struct_Elf_Hash {
82           unsigned long sysv;
83           unsigned long gnu;
84 } Elf_Hash;
85 #endif /* _RTLD_SOURCE */
86 
87 /*
88  * C++ has mandated the use of the following keywords for its new boolean
89  * type.  We might as well follow their lead.
90  */
91 struct Struct_Obj_Entry;
92 
93 typedef struct Struct_Objlist_Entry {
94           SIMPLEQ_ENTRY(Struct_Objlist_Entry) link;
95           struct Struct_Obj_Entry *obj;
96 } Objlist_Entry;
97 
98 typedef SIMPLEQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
99 
100 typedef struct Struct_Name_Entry {
101           SIMPLEQ_ENTRY(Struct_Name_Entry) link;
102           char      name[1];
103 } Name_Entry;
104 
105 typedef struct Struct_Needed_Entry {
106           struct Struct_Needed_Entry *next;
107           struct Struct_Obj_Entry *obj;
108           unsigned long   name;         /* Offset of name in string table */
109 } Needed_Entry;
110 
111 typedef struct _rtld_search_path_t {
112           struct _rtld_search_path_t *sp_next;
113           const char     *sp_path;
114           size_t          sp_pathlen;
115 } Search_Path;
116 
117 typedef struct Struct_Ver_Entry {
118           Elf_Word        hash;
119           u_int           flags;
120           const char     *name;
121           const char     *file;
122 } Ver_Entry;
123 
124 /* Ver_Entry.flags */
125 #define VER_INFO_HIDDEN       0x01
126 
127 #define RTLD_MAX_ENTRY 10
128 #define RTLD_MAX_LIBRARY 4
129 #define RTLD_MAX_CTL 2
130 typedef struct _rtld_library_xform_t {
131           struct _rtld_library_xform_t *next;
132           char *name;
133           const char *ctlname;
134           struct {
135                     char *value;
136                     char *library[RTLD_MAX_LIBRARY];
137           } entry[RTLD_MAX_ENTRY];
138 } Library_Xform;
139 
140 /*
141  * Shared object descriptor.
142  *
143  * Items marked with "(%)" are dynamically allocated, and must be freed
144  * when the structure is destroyed.
145  */
146 
147 typedef void (*fptr_t)(void);
148 
149 typedef struct Struct_Obj_Entry {
150           struct Struct_Obj_Entry *next;
151           char           *path;                   /* Pathname of underlying file (%) */
152           int             refcount;
153           int             dl_refcount;  /* Number of times loaded by dlopen */
154 
155           /* These items are computed by map_object() or by digest_phdr(). */
156           caddr_t         mapbase;      /* Base address of mapped region */
157           size_t          mapsize;      /* Size of mapped region in bytes */
158           size_t          textsize;     /* Size of text segment in bytes */
159           Elf_Addr        vaddrbase;    /* Base address in shared object file */
160           caddr_t         relocbase;    /* Reloc const = mapbase - *vaddrbase */
161           Elf_Dyn        *dynamic;      /* Dynamic section */
162           caddr_t         entry;                  /* Entry point */
163           const Elf_Phdr *phdr;                   /* Program header (may be xmalloc'ed) */
164           size_t              phsize;             /* Size of program header in bytes */
165 
166           /* Items from the dynamic section. */
167           Elf_Addr       *pltgot;                 /* PLTGOT table */
168           const Elf_Rel  *rel;                    /* Relocation entries */
169           const Elf_Rel  *rellim;                 /* Limit of Relocation entries */
170           const Elf_Rela *rela;                   /* Relocation entries */
171           const Elf_Rela *relalim;      /* Limit of Relocation entries */
172           const Elf_Relr *relr;                   /* Relative relocations */
173           const Elf_Relr *relrlim;      /* Limit of relative relocations */
174           const Elf_Rel  *pltrel;                 /* PLT relocation entries */
175           const Elf_Rel  *pltrellim;    /* Limit of PLT relocation entries */
176           const Elf_Rela *pltrela;      /* PLT relocation entries */
177           const Elf_Rela *pltrelalim;   /* Limit of PLT relocation entries */
178           const Elf_Sym  *symtab;                 /* Symbol table */
179           const char     *strtab;                 /* String table */
180           unsigned long   strsize;      /* Size in bytes of string table */
181 #if defined(__mips__) || defined(__riscv__)
182           Elf_Word        local_gotno;  /* Number of local GOT entries */
183           Elf_Word        symtabno;     /* Number of dynamic symbols */
184           Elf_Word        gotsym;                 /* First dynamic symbol in GOT */
185 #endif
186 
187           /* SysV Hash fields */
188           const Elf_Symindx *buckets;   /* Hash table buckets array */
189           unsigned long       unused1;  /* Used to be nbuckets */
190           const Elf_Symindx *chains;    /* Hash table chain array */
191           unsigned long   nchains;      /* Number of chains */
192 
193           Search_Path    *rpaths;                 /* Search path specified in object */
194           Needed_Entry   *needed;                 /* Shared objects needed by this (%) */
195 
196           fptr_t              init;               /* Initialization function to call */
197           fptr_t              fini;               /* Termination function to call */
198 
199           u_int32_t mainprog:1,         /* True if this is the main program */
200                     rtld:1,             /* True if this is the dynamic linker */
201                               textrel:1,          /* True if there are relocations to
202                                                    * text seg */
203                               symbolic:1,         /* True if generated with
204                                                    * "-Bsymbolic" */
205                               printed:1,          /* True if ldd has printed it */
206                               isdynamic:1,        /* True if this is a pure PIC object */
207                               mainref:1,          /* True if on _rtld_list_main */
208                               globalref:1,        /* True if on _rtld_list_global */
209                               init_done:1,        /* True if .init has been added */
210                               init_called:1,      /* True if .init function has been
211                                                    * called */
212                               fini_called:1,      /* True if .fini function has been
213                                                    * called */
214                               z_now:1,  /* True if object's symbols should be
215                                                      bound immediately */
216                               z_nodelete:1,       /* True if object should never be
217                                                      unloaded */
218                               z_initfirst:1,      /* True if object's .init/.fini take
219                                                    * priority over others */
220                               z_noopen:1,         /* True if object should never be
221                                                      dlopen'ed */
222                               phdr_loaded:1,      /* Phdr is loaded and doesn't need to
223                                                    * be freed. */
224 #ifdef __alpha__
225                               secureplt:1,        /* True if PLT is read-only format */
226 #endif
227 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
228                               tls_static:1,       /* True if static TLS offset
229                                                    * has been allocated */
230                               tls_dynamic:1,      /* True if any non-static DTV entry
231                                                    * has been allocated */
232 #endif
233                               ref_nodel:1,        /* Refcount increased to prevent dlclose */
234                               sysv_hash:1,        /* SysV Hash available */
235                               gnu_hash:1;         /* GNU Hash available */
236 
237           struct link_map linkmap;      /* for the debugger */
238 
239           /* These items are computed by map_object() or by digest_phdr(). */
240           const char     *interp;       /* Pathname of the interpreter, if any */
241           Objlist         dldags;       /* Object belongs to these dlopened DAGs (%) */
242           Objlist         dagmembers;   /* DAG has these members (%) */
243           dev_t           dev;                    /* Object's filesystem's device */
244           ino_t           ino;                    /* Object's inode number */
245 
246           void                *ehdr;
247 
248           /* SysV Hash fields */
249           uint32_t        nbuckets;     /* Number of buckets */
250           uint32_t        nbuckets_m;   /* Precomputed for fast remainder */
251           uint8_t         nbuckets_s1;
252           uint8_t         nbuckets_s2;
253 
254           /* GNU Hash fields */
255           const uint32_t *buckets_gnu;  /* Hash table buckets array */
256           uint32_t  nbuckets_gnu;       /* Number of GNU hash buckets */
257           uint32_t  nbuckets_m_gnu;     /* Precomputed for fast remainder */
258           uint8_t             nbuckets_s1_gnu;
259           uint8_t             nbuckets_s2_gnu;
260           const uint32_t *chains_gnu;   /* Hash table chain array */
261 #define nchains_gnu nchains             /* Number of symbols, shared with SysV Hash */
262           const Elf_Addr *bloom_gnu;
263           uint32_t  symndx_gnu;         /* First accessible symbol on dynsym table */
264           uint32_t  mask_bm_gnu;        /* Bloom filter words - 1 (bitmask) */
265           uint32_t  shift2_gnu;         /* Bloom filter shift count */
266 
267           size_t              pathlen;  /* Pathname length */
268           SIMPLEQ_HEAD(, Struct_Name_Entry) names; /* List of names for this
269                                                               * object we know about. */
270 
271 #ifdef __powerpc__
272 #ifdef _LP64
273           Elf_Addr  glink;              /* global linkage */
274 #else
275           Elf_Addr       *gotptr;                 /* GOT table (secure-plt only) */
276 #endif
277 #endif
278 
279 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
280           /* Thread Local Storage support for this module */
281           size_t              tlsindex; /* Index in DTV */
282           void                *tlsinit; /* Base address of TLS init block */
283           size_t              tlsinitsize;        /* Size of TLS init block */
284           size_t              tlssize;  /* Size of TLS block */
285           size_t              tlsoffset;          /* Offset in the static TLS block */
286           size_t              tlsalign; /* Needed alignment for static TLS */
287 #endif
288 
289 #ifdef GNU_RELRO
290           /* relocation readonly */
291           void                *relro_page;
292           size_t              relro_size;
293 #endif
294 
295           /* symbol versioning */
296           const Elf_Verneed *verneed;   /* Required versions. */
297           Elf_Word  verneednum;         /* Number of entries in verneed table */
298           const Elf_Verdef  *verdef;    /* Provided versions. */
299           Elf_Word  verdefnum;          /* Number of entries in verdef table */
300           const Elf_Versym *versyms;    /* Symbol versions table */
301 
302           Ver_Entry *vertab;  /* Versions required/defined by this
303                                                    * object */
304           int                 vertabnum;          /* Number of entries in vertab */
305 
306           /* init_array/fini_array */
307           fptr_t              *init_array;        /* start of init array */
308           size_t              init_arraysz;       /* # of entries in it */
309           fptr_t              *fini_array;        /* start of fini array */
310           size_t              fini_arraysz;       /* # of entries in it */
311           /* IRELATIVE relocations */
312           size_t              ifunc_remaining;
313 #if \
314     defined(__aarch64__) || \
315     defined(__arm__) || \
316     defined(__i386__) || \
317     defined(__powerpc__) || \
318     defined(__sparc__) || \
319     defined(__x86_64__)
320 #define IFUNC_NONPLT
321           /* On SPARC, the PLT variant is called JMP_IREL and counted above. */
322           size_t              ifunc_remaining_nonplt;
323 #endif
324           size_t              cxa_refcount;       /* For TLS destructors. */
325 #ifdef __ARM_EABI__
326           void                *exidx_start;
327           size_t              exidx_sz;
328 #endif
329 } Obj_Entry;
330 
331 typedef struct Struct_DoneList {
332           const Obj_Entry **objs;                 /* Array of object pointers */
333           unsigned int num_alloc;                 /* Allocated size of the array */
334           unsigned int num_used;                  /* Number of array slots used */
335 } DoneList;
336 
337 
338 #if defined(_RTLD_SOURCE)
339 
340 extern struct r_debug _rtld_debug;
341 extern Search_Path *_rtld_default_paths;
342 extern Obj_Entry *_rtld_objlist;
343 extern Obj_Entry **_rtld_objtail;
344 extern u_int _rtld_objcount;
345 extern u_int _rtld_objloads;
346 extern const uintptr_t _rtld_compat_obj[];
347 extern Obj_Entry *_rtld_objmain;
348 extern Obj_Entry _rtld_objself;
349 extern Search_Path *_rtld_paths;
350 extern Library_Xform *_rtld_xforms;
351 extern bool _rtld_trust;
352 extern Objlist _rtld_list_global;
353 extern Objlist _rtld_list_main;
354 extern Elf_Sym _rtld_sym_zero;
355 extern u_int _rtld_objgen;
356 
357 #define   RTLD_MODEMASK 0x3
358 
359 /* Flags to be passed into _rtld_symlook_ family of functions. */
360 #define SYMLOOK_IN_PLT        0x01      /* Lookup for PLT symbol */
361 #define SYMLOOK_DLSYM         0x02      /* Return newest versioned symbol.
362                                            Used by dlsym. */
363 
364 /* Flags for _rtld_load_object() and friends. */
365 #define   _RTLD_GLOBAL        0x01      /* Add object to global DAG. */
366 #define   _RTLD_MAIN          0x02
367 #define   _RTLD_NOLOAD        0x04      /* dlopen() specified RTLD_NOLOAD. */
368 #define   _RTLD_DLOPEN        0x08      /* Load_object() called from dlopen(). */
369 
370 /* Preallocation for static TLS model */
371 #define   RTLD_STATIC_TLS_RESERVATION   64
372 
373 /* rtld.c */
374 __dso_public char *dlerror(void);
375 __dso_public void *dlopen(const char *, int);
376 __dso_public void *dlsym(void *, const char *);
377 __dso_public int dlclose(void *);
378 __dso_public int dladdr(const void *, Dl_info *);
379 __dso_public int dlinfo(void *, int, void *);
380 __dso_public int dl_iterate_phdr(int (*)(struct dl_phdr_info *, size_t, void *),
381     void *);
382 
383 __dso_public void *_dlauxinfo(void) __pure;
384 __dso_public void __dl_cxa_refcount(void *addr, ssize_t delta);
385 
386 __dso_public pid_t __locked_fork(int *);
387 
388 #if defined(__ARM_EABI__) && !defined(__ARM_DWARF_EH__)
389 /*
390  * This is used by libgcc to find the start and length of the exception table
391  * associated with a PC.
392  */
393 __dso_public _Unwind_Ptr __gnu_Unwind_Find_exidx(_Unwind_Ptr, int *);
394 #endif
395 
396 /* These aren't exported */
397 void _rtld_error(const char *, ...) __printflike(1,2);
398 void _rtld_die(void) __dead;
399 void *_rtld_objmain_sym(const char *);
400 __dso_public void _rtld_debug_state(void) __noinline;
401 void _rtld_linkmap_add(Obj_Entry *);
402 void _rtld_linkmap_delete(Obj_Entry *);
403 void _rtld_objlist_push_head(Objlist *, Obj_Entry *);
404 void _rtld_objlist_push_tail(Objlist *, Obj_Entry *);
405 Objlist_Entry *_rtld_objlist_find(Objlist *, const Obj_Entry *);
406 void _rtld_ref_dag(Obj_Entry *);
407 
408 void _rtld_shared_enter(void);
409 void _rtld_shared_exit(void);
410 void _rtld_exclusive_enter(sigset_t *);
411 void _rtld_exclusive_exit(sigset_t *);
412 
413 int _rtld_relro(const Obj_Entry *, bool);
414 
415 /* expand.c */
416 size_t _rtld_expand_path(char *, size_t, const char *, const char *,\
417     const char *);
418 
419 /* headers.c */
420 void _rtld_digest_dynamic(const char *, Obj_Entry *);
421 Obj_Entry *_rtld_digest_phdr(const Elf_Phdr *, int, caddr_t);
422 
423 /* load.c */
424 Obj_Entry *_rtld_load_object(const char *, int);
425 int _rtld_load_needed_objects(Obj_Entry *, int);
426 int _rtld_preload(const char *);
427 
428 #define   OBJ_ERR   (Obj_Entry *)(-1)
429 /* path.c */
430 void _rtld_add_paths(const char *, Search_Path **, const char *);
431 void _rtld_process_hints(const char *, Search_Path **, Library_Xform **,
432     const char *);
433 int _rtld_sysctl(const char *, void *, size_t *);
434 
435 /* reloc.c */
436 int _rtld_do_copy_relocations(const Obj_Entry *);
437 int _rtld_relocate_objects(Obj_Entry *, bool);
438 int _rtld_relocate_nonplt_objects(Obj_Entry *);
439 int _rtld_relocate_plt_lazy(Obj_Entry *);
440 int _rtld_relocate_plt_objects(const Obj_Entry *);
441 void _rtld_setup_pltgot(const Obj_Entry *);
442 Elf_Addr _rtld_resolve_ifunc(const Obj_Entry *, const Elf_Sym *);
443 Elf_Addr _rtld_resolve_ifunc2(const Obj_Entry *, Elf_Addr);
444 
445 void _rtld_call_ifunc(Obj_Entry *, sigset_t *, u_int);
446 
447 /* search.c */
448 Obj_Entry *_rtld_load_library(const char *, const Obj_Entry *, int);
449 
450 /* symbol.c */
451 const Elf_Sym *_rtld_symlook_obj(const char *, Elf_Hash *,
452     const Obj_Entry *, u_int, const Ver_Entry *);
453 const Elf_Sym *_rtld_find_symdef(unsigned long, const Obj_Entry *,
454     const Obj_Entry **, u_int);
455 const Elf_Sym *_rtld_find_plt_symdef(unsigned long, const Obj_Entry *,
456     const Obj_Entry **, bool);
457 
458 const Elf_Sym *_rtld_symlook_list(const char *, Elf_Hash *,
459     const Objlist *, const Obj_Entry **, u_int, const Ver_Entry *, DoneList *);
460 const Elf_Sym *_rtld_symlook_default(const char *, Elf_Hash *,
461     const Obj_Entry *, const Obj_Entry **, u_int, const Ver_Entry *);
462 const Elf_Sym *_rtld_symlook_needed(const char *, Elf_Hash *,
463     const Needed_Entry *, const Obj_Entry **, u_int, const Ver_Entry *,
464     DoneList *, DoneList *);
465 
466 /* symver.c */
467 void _rtld_object_add_name(Obj_Entry *, const char *);
468 int _rtld_object_match_name(const Obj_Entry *, const char *);
469 int _rtld_verify_object_versions(Obj_Entry *);
470 
471 static __inline const Ver_Entry *
_rtld_fetch_ventry(const Obj_Entry * obj,unsigned long symnum)472 _rtld_fetch_ventry(const Obj_Entry *obj, unsigned long symnum)
473 {
474           Elf_Half vernum;
475 
476           if (obj->vertab) {
477                     vernum = VER_NDX(obj->versyms[symnum].vs_vers);
478                     if (vernum >= obj->vertabnum) {
479                               _rtld_error("%s: symbol %s has wrong verneed value %d",
480                                   obj->path, &obj->strtab[symnum], vernum);
481                     } else if (obj->vertab[vernum].hash) {
482                               return &obj->vertab[vernum];
483                     }
484           }
485           return NULL;
486 }
487 
488 #if defined(__HAVE_TLS_VARIANT_I) || defined(__HAVE_TLS_VARIANT_II)
489 /* tls.c */
490 void *_rtld_tls_get_addr(void *, size_t, size_t);
491 void _rtld_tls_initial_allocation(void);
492 int _rtld_tls_offset_allocate(Obj_Entry *);
493 void _rtld_tls_offset_free(Obj_Entry *);
494 
495 extern size_t _rtld_tls_dtv_generation;
496 extern size_t _rtld_tls_max_index;
497 
498 __dso_public extern void *__tls_get_addr(void *);
499 #ifdef __i386__
500 __dso_public extern void *___tls_get_addr(void *)
501     __attribute__((__regparm__(1)));
502 #endif
503 #endif
504 
505 /* map_object.c */
506 struct stat;
507 Obj_Entry *_rtld_map_object(const char *, int, const struct stat *);
508 void _rtld_obj_free(Obj_Entry *);
509 Obj_Entry *_rtld_obj_new(void);
510 
511 #ifdef RTLD_LOADER
512 /* function descriptors */
513 #ifdef __HAVE_FUNCTION_DESCRIPTORS
514 Elf_Addr _rtld_function_descriptor_alloc(const Obj_Entry *,
515     const Elf_Sym *, Elf_Addr);
516 const void *_rtld_function_descriptor_function(const void *);
517 
518 Elf_Addr _rtld_call_function_addr(const Obj_Entry *, Elf_Addr);
519 #else
520 static inline Elf_Addr
_rtld_call_function_addr(const Obj_Entry * obj,Elf_Addr addr)521 _rtld_call_function_addr(const Obj_Entry *obj, Elf_Addr addr)
522 {
523           return ((Elf_Addr(*)(void))addr)();
524 }
525 #endif /* __HAVE_FUNCTION_DESCRIPTORS */
526 #endif /* RTLD_LOADER */
527 
528 #endif /* _RTLD_SOURCE */
529 
530 #endif /* RTLD_H */
531