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