134192Sjdp/*- 255687Sjdp * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra. 334192Sjdp * All rights reserved. 434192Sjdp * 534192Sjdp * Redistribution and use in source and binary forms, with or without 634192Sjdp * modification, are permitted provided that the following conditions 734192Sjdp * are met: 834192Sjdp * 1. Redistributions of source code must retain the above copyright 934192Sjdp * notice, this list of conditions and the following disclaimer. 1034192Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1134192Sjdp * notice, this list of conditions and the following disclaimer in the 1234192Sjdp * documentation and/or other materials provided with the distribution. 1334192Sjdp * 1434192Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1534192Sjdp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1634192Sjdp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1734192Sjdp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1834192Sjdp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1934192Sjdp * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2034192Sjdp * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2134192Sjdp * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2234192Sjdp * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2334192Sjdp * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2434192Sjdp * 2550476Speter * $FreeBSD$ 2634192Sjdp */ 2734192Sjdp 2834192Sjdp#ifndef RTLD_H /* { */ 2934192Sjdp#define RTLD_H 1 3034192Sjdp 3176224Sobrien#include <machine/elf.h> 3234192Sjdp#include <sys/types.h> 3350608Sjdp#include <sys/queue.h> 3434192Sjdp 3576224Sobrien#include <elf-hints.h> 3635529Sdfr#include <link.h> 37225152Skib#include <stdarg.h> 38216695Skib#include <setjmp.h> 3934192Sjdp#include <stddef.h> 4034192Sjdp 41115396Skan#include "rtld_lock.h" 4245501Sjdp#include "rtld_machdep.h" 4345501Sjdp 44127250Speter#ifdef COMPAT_32BIT 45127250Speter#undef STANDARD_LIBRARY_PATH 46127250Speter#undef _PATH_ELF_HINTS 47127250Speter#define _PATH_ELF_HINTS "/var/run/ld-elf32.so.hints" 48127250Speter/* For running 32 bit binaries */ 49127250Speter#define STANDARD_LIBRARY_PATH "/lib32:/usr/lib32" 50127250Speter#define LD_ "LD_32_" 51127250Speter#endif 52127250Speter 5334192Sjdp#ifndef STANDARD_LIBRARY_PATH 54119013Sgordon#define STANDARD_LIBRARY_PATH "/lib:/usr/lib" 5534192Sjdp#endif 56127250Speter#ifndef LD_ 57127250Speter#define LD_ "LD_" 58127250Speter#endif 5934192Sjdp 6034192Sjdp#define NEW(type) ((type *) xmalloc(sizeof(type))) 61233307Skib#define CNEW(type) ((type *) xcalloc(1, sizeof(type))) 6234192Sjdp 6334192Sjdp/* We might as well do booleans like C++. */ 6434192Sjdptypedef unsigned char bool; 6534192Sjdp#define false 0 6634192Sjdp#define true 1 6734192Sjdp 68133063Sdfrextern size_t tls_last_offset; 69133063Sdfrextern size_t tls_last_size; 70133063Sdfrextern size_t tls_static_space; 71133063Sdfrextern int tls_dtv_generation; 72133063Sdfrextern int tls_max_index; 73133063Sdfr 74232831Skibextern int main_argc; 75232831Skibextern char **main_argv; 76232831Skibextern char **environ; 77232831Skib 7850609Sjdpstruct stat; 7934192Sjdpstruct Struct_Obj_Entry; 8034192Sjdp 8155687Sjdp/* Lists of shared objects */ 8250608Sjdptypedef struct Struct_Objlist_Entry { 8360938Sjake STAILQ_ENTRY(Struct_Objlist_Entry) link; 8450608Sjdp struct Struct_Obj_Entry *obj; 8550608Sjdp} Objlist_Entry; 8650608Sjdp 8760938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist; 8850608Sjdp 8963870Sjdp/* Types of init and fini functions */ 9055687Sjdptypedef void (*InitFunc)(void); 91232831Skibtypedef void (*InitArrFunc)(int, char **, char **); 9255687Sjdp 9355687Sjdp/* Lists of shared object dependencies */ 9434192Sjdptypedef struct Struct_Needed_Entry { 9534192Sjdp struct Struct_Needed_Entry *next; 9634192Sjdp struct Struct_Obj_Entry *obj; 9734192Sjdp unsigned long name; /* Offset of name in string table */ 9834192Sjdp} Needed_Entry; 9934192Sjdp 100153515Skantypedef struct Struct_Name_Entry { 101153515Skan STAILQ_ENTRY(Struct_Name_Entry) link; 102153515Skan char name[1]; 103153515Skan} Name_Entry; 104153515Skan 10562801Sjdp/* Lock object */ 10662801Sjdptypedef struct Struct_LockInfo { 10762801Sjdp void *context; /* Client context for creating locks */ 10862801Sjdp void *thelock; /* The one big lock */ 10962801Sjdp /* Debugging aids. */ 11062801Sjdp volatile int rcount; /* Number of readers holding lock */ 11162801Sjdp volatile int wcount; /* Number of writers holding lock */ 11262801Sjdp /* Methods */ 11362801Sjdp void *(*lock_create)(void *context); 11462801Sjdp void (*rlock_acquire)(void *lock); 11562801Sjdp void (*wlock_acquire)(void *lock); 11662801Sjdp void (*rlock_release)(void *lock); 11762801Sjdp void (*wlock_release)(void *lock); 11862801Sjdp void (*lock_destroy)(void *lock); 11962801Sjdp void (*context_destroy)(void *context); 12062801Sjdp} LockInfo; 12162801Sjdp 122153515Skantypedef struct Struct_Ver_Entry { 123153515Skan Elf_Word hash; 124153515Skan unsigned int flags; 125153515Skan const char *name; 126153515Skan const char *file; 127153515Skan} Ver_Entry; 128153515Skan 129234840Skibtypedef struct Struct_Sym_Match_Result { 130234840Skib const Elf_Sym *sym_out; 131234840Skib const Elf_Sym *vsymp; 132234840Skib int vcount; 133234840Skib} Sym_Match_Result; 134234840Skib 135153515Skan#define VER_INFO_HIDDEN 0x01 136153515Skan 13734192Sjdp/* 13834192Sjdp * Shared object descriptor. 13934192Sjdp * 14034192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed 14134192Sjdp * when the structure is destroyed. 14250977Sjdp * 14350977Sjdp * CAUTION: It appears that the JDK port peeks into these structures. 14450977Sjdp * It looks at "next" and "mapbase" at least. Don't add new members 14550977Sjdp * near the front, until this can be straightened out. 14634192Sjdp */ 14734192Sjdptypedef struct Struct_Obj_Entry { 14834192Sjdp /* 14934192Sjdp * These two items have to be set right for compatibility with the 15034192Sjdp * original ElfKit crt1.o. 15134192Sjdp */ 152153504Smarcel Elf_Size magic; /* Magic number (sanity check) */ 153153504Smarcel Elf_Size version; /* Version number of struct format */ 15434192Sjdp 15534192Sjdp struct Struct_Obj_Entry *next; 15634192Sjdp char *path; /* Pathname of underlying file (%) */ 157116511Smdodd char *origin_path; /* Directory path of origin file */ 15834192Sjdp int refcount; 15934192Sjdp int dl_refcount; /* Number of times loaded by dlopen */ 16034192Sjdp 16134192Sjdp /* These items are computed by map_object() or by digest_phdr(). */ 16234192Sjdp caddr_t mapbase; /* Base address of mapped region */ 16334192Sjdp size_t mapsize; /* Size of mapped region in bytes */ 16434192Sjdp size_t textsize; /* Size of text segment in bytes */ 16538467Sjb Elf_Addr vaddrbase; /* Base address in shared object file */ 16634192Sjdp caddr_t relocbase; /* Relocation constant = mapbase - vaddrbase */ 16738467Sjb const Elf_Dyn *dynamic; /* Dynamic section */ 16834192Sjdp caddr_t entry; /* Entry point */ 16938467Sjb const Elf_Phdr *phdr; /* Program header if it is mapped, else NULL */ 17034192Sjdp size_t phsize; /* Size of program header in bytes */ 17150610Sjdp const char *interp; /* Pathname of the interpreter, if any */ 172217153Skib Elf_Word stack_flags; 17334192Sjdp 174133063Sdfr /* TLS information */ 175133063Sdfr int tlsindex; /* Index in DTV for this module */ 176133063Sdfr void *tlsinit; /* Base address of TLS init block */ 177133063Sdfr size_t tlsinitsize; /* Size of TLS init block for this module */ 178133063Sdfr size_t tlssize; /* Size of TLS block for this module */ 179133063Sdfr size_t tlsoffset; /* Offset of static TLS block for this module */ 180133063Sdfr size_t tlsalign; /* Alignment of static TLS block */ 181133063Sdfr 182230784Skib caddr_t relro_page; 183230784Skib size_t relro_size; 184230784Skib 18534192Sjdp /* Items from the dynamic section. */ 18645501Sjdp Elf_Addr *pltgot; /* PLT or GOT, depending on architecture */ 18738467Sjb const Elf_Rel *rel; /* Relocation entries */ 18834192Sjdp unsigned long relsize; /* Size in bytes of relocation info */ 18938816Sdfr const Elf_Rela *rela; /* Relocation entries with addend */ 19038816Sdfr unsigned long relasize; /* Size in bytes of addend relocation info */ 19138467Sjb const Elf_Rel *pltrel; /* PLT relocation entries */ 19234192Sjdp unsigned long pltrelsize; /* Size in bytes of PLT relocation info */ 19338816Sdfr const Elf_Rela *pltrela; /* PLT relocation entries with addend */ 19438816Sdfr unsigned long pltrelasize; /* Size in bytes of PLT addend reloc info */ 19538467Sjb const Elf_Sym *symtab; /* Symbol table */ 19634192Sjdp const char *strtab; /* String table */ 19734192Sjdp unsigned long strsize; /* Size in bytes of string table */ 198177924Simp#ifdef __mips__ 199177924Simp Elf_Word local_gotno; /* Number of local GOT entries */ 200177924Simp Elf_Word symtabno; /* Number of dynamic symbols */ 201177924Simp Elf_Word gotsym; /* First dynamic symbol in GOT */ 202177924Simp#endif 20334192Sjdp 204153515Skan const Elf_Verneed *verneed; /* Required versions. */ 205153515Skan Elf_Word verneednum; /* Number of entries in verneed table */ 206153515Skan const Elf_Verdef *verdef; /* Provided versions. */ 207153515Skan Elf_Word verdefnum; /* Number of entries in verdef table */ 208153515Skan const Elf_Versym *versyms; /* Symbol versions table */ 209153515Skan 21085004Sdfr const Elf_Hashelt *buckets; /* Hash table buckets array */ 21134192Sjdp unsigned long nbuckets; /* Number of buckets */ 21285004Sdfr const Elf_Hashelt *chains; /* Hash table chain array */ 213234841Skib unsigned long nchains; /* Number of entries in chain array */ 21434192Sjdp 215234841Skib Elf32_Word nbuckets_gnu; /* Number of GNU hash buckets*/ 216234841Skib Elf32_Word symndx_gnu; /* 1st accessible symbol on dynsym table */ 217234841Skib Elf32_Word maskwords_bm_gnu; /* Bloom filter words - 1 (bitmask) */ 218234841Skib Elf32_Word shift2_gnu; /* Bloom filter shift count */ 219234841Skib Elf32_Word dynsymcount; /* Total entries in dynsym table */ 220234841Skib Elf_Addr *bloom_gnu; /* Bloom filter used by GNU hash func */ 221234841Skib const Elf_Hashelt *buckets_gnu; /* GNU hash table bucket array */ 222234841Skib const Elf_Hashelt *chain_zero_gnu; /* GNU hash table value array (Zeroed) */ 223234841Skib 224189959Skib char *rpath; /* Search path specified in object */ 225238471Skib char *runpath; /* Search path with different priority */ 22634192Sjdp Needed_Entry *needed; /* Shared objects needed by this one (%) */ 227216695Skib Needed_Entry *needed_filtees; 228216695Skib Needed_Entry *needed_aux_filtees; 22934192Sjdp 230153515Skan STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we 231153515Skan know about. */ 232153515Skan Ver_Entry *vertab; /* Versions required /defined by this object */ 233153515Skan int vernum; /* Number of entries in vertab */ 234153515Skan 23585677Speter Elf_Addr init; /* Initialization function to call */ 23685677Speter Elf_Addr fini; /* Termination function to call */ 237232831Skib Elf_Addr preinit_array; /* Pre-initialization array of functions */ 238232831Skib Elf_Addr init_array; /* Initialization array of functions */ 239232831Skib Elf_Addr fini_array; /* Termination array of functions */ 240232831Skib int preinit_array_num; /* Number of entries in preinit_array */ 241232831Skib int init_array_num; /* Number of entries in init_array */ 242232831Skib int fini_array_num; /* Number of entries in fini_array */ 24334192Sjdp 244232831Skib int32_t osrel; /* OSREL note value */ 245232831Skib 246168312Skan bool mainprog : 1; /* True if this is the main program */ 247168312Skan bool rtld : 1; /* True if this is the dynamic linker */ 248233231Skib bool relocated : 1; /* True if processed by relocate_objects() */ 249233546Skib bool ver_checked : 1; /* True if processed by rtld_verify_object_versions */ 250168312Skan bool textrel : 1; /* True if there are relocations to text seg */ 251168312Skan bool symbolic : 1; /* True if generated with "-Bsymbolic" */ 252168312Skan bool bind_now : 1; /* True if all relocations should be made first */ 253168312Skan bool traced : 1; /* Already printed in ldd trace output */ 254168312Skan bool jmpslots_done : 1; /* Already have relocated the jump slots */ 255168312Skan bool init_done : 1; /* Already have added object to init list */ 256168312Skan bool tls_done : 1; /* Already allocated offset for static TLS */ 257168312Skan bool phdr_alloc : 1; /* Phdr is allocated and needs to be freed. */ 258189959Skib bool z_origin : 1; /* Process rpath and soname tokens */ 259190543Skib bool z_nodelete : 1; /* Do not unload the object and dependencies */ 260199829Skib bool z_noopen : 1; /* Do not load on dlopen */ 261216695Skib bool z_loadfltr : 1; /* Immediately load filtees */ 262256101Skib bool z_interpose : 1; /* Interpose all objects but main */ 263238471Skib bool z_nodeflib : 1; /* Don't search default library path */ 264194531Skan bool ref_nodel : 1; /* Refcount increased to prevent dlclose */ 265194531Skan bool init_scanned: 1; /* Object is already on init list. */ 266194531Skan bool on_fini_list: 1; /* Object is already on fini list. */ 267214728Skib bool dag_inited : 1; /* Object has its DAG initialized. */ 268216695Skib bool filtees_loaded : 1; /* Filtees loaded */ 269228435Skib bool irelative : 1; /* Object has R_MACHDEP_IRELATIVE relocs */ 270228435Skib bool gnu_ifunc : 1; /* Object has references to STT_GNU_IFUNC */ 271271469Skib bool non_plt_gnu_ifunc : 1; /* Object has non-plt IFUNC references */ 272232831Skib bool crt_no_init : 1; /* Object' crt does not call _init/_fini */ 273234841Skib bool valid_hash_sysv : 1; /* A valid System V hash hash tag is available */ 274234841Skib bool valid_hash_gnu : 1; /* A valid GNU hash tag is available */ 27535529Sdfr 276194531Skan struct link_map linkmap; /* For GDB and dlinfo() */ 27750977Sjdp Objlist dldags; /* Object belongs to these dlopened DAGs (%) */ 27850977Sjdp Objlist dagmembers; /* DAG has these members (%) */ 27950977Sjdp dev_t dev; /* Object's filesystem's device */ 28050977Sjdp ino_t ino; /* Object's inode number */ 281229780Suqs void *priv; /* Platform-dependent */ 28234192Sjdp} Obj_Entry; 28334192Sjdp 28434192Sjdp#define RTLD_MAGIC 0xd550b87a 28534192Sjdp#define RTLD_VERSION 1 28634192Sjdp 287192922Sdfr#define RTLD_STATIC_TLS_EXTRA 128 288133063Sdfr 289153515Skan/* Flags to be passed into symlook_ family of functions. */ 290153515Skan#define SYMLOOK_IN_PLT 0x01 /* Lookup for PLT symbol */ 291228375Skib#define SYMLOOK_DLSYM 0x02 /* Return newest versioned symbol. Used by 292153515Skan dlsym. */ 293233231Skib#define SYMLOOK_EARLY 0x04 /* Symlook is done during initialization. */ 294271469Skib#define SYMLOOK_IFUNC 0x08 /* Allow IFUNC processing in 295271469Skib reloc_non_plt(). */ 296153515Skan 297199829Skib/* Flags for load_object(). */ 298199877Skib#define RTLD_LO_NOLOAD 0x01 /* dlopen() specified RTLD_NOLOAD. */ 299199877Skib#define RTLD_LO_DLOPEN 0x02 /* Load_object() called from dlopen(). */ 300199877Skib#define RTLD_LO_TRACE 0x04 /* Only tracing. */ 301216695Skib#define RTLD_LO_NODELETE 0x08 /* Loaded object cannot be closed. */ 302216695Skib#define RTLD_LO_FILTEES 0x10 /* Loading filtee. */ 303233231Skib#define RTLD_LO_EARLY 0x20 /* Do not call ctors, postpone it to the 304233231Skib initialization during the image start. */ 305199829Skib 30676296Sjdp/* 30776296Sjdp * Symbol cache entry used during relocation to avoid multiple lookups 30876296Sjdp * of the same symbol. 30976296Sjdp */ 31076296Sjdptypedef struct Struct_SymCache { 31176296Sjdp const Elf_Sym *sym; /* Symbol table entry */ 31276296Sjdp const Obj_Entry *obj; /* Shared object which defines it */ 31376296Sjdp} SymCache; 31476296Sjdp 315216695Skib/* 316216695Skib * This structure provides a reentrant way to keep a list of objects and 317216695Skib * check which ones have already been processed in some way. 318216695Skib */ 319216695Skibtypedef struct Struct_DoneList { 320216695Skib const Obj_Entry **objs; /* Array of object pointers */ 321216695Skib unsigned int num_alloc; /* Allocated size of the array */ 322216695Skib unsigned int num_used; /* Number of array slots used */ 323216695Skib} DoneList; 324216695Skib 325216695Skibstruct Struct_RtldLockState { 326216695Skib int lockstate; 327218476Skib sigjmp_buf env; 328216695Skib}; 329216695Skib 330238471Skibstruct fill_search_info_args { 331238471Skib int request; 332238471Skib unsigned int flags; 333238471Skib struct dl_serinfo *serinfo; 334238471Skib struct dl_serpath *serpath; 335238471Skib char *strspace; 336238471Skib}; 337238471Skib 338216695Skib/* 339216695Skib * The pack of arguments and results for the symbol lookup functions. 340216695Skib */ 341216695Skibtypedef struct Struct_SymLook { 342216695Skib const char *name; 343216695Skib unsigned long hash; 344234841Skib uint32_t hash_gnu; 345216695Skib const Ver_Entry *ventry; 346216695Skib int flags; 347216695Skib const Obj_Entry *defobj_out; 348216695Skib const Elf_Sym *sym_out; 349216695Skib struct Struct_RtldLockState *lockstate; 350216695Skib} SymLook; 351216695Skib 352233361Skibvoid _rtld_error(const char *, ...) __printflike(1, 2); 353233361Skibconst char *rtld_strerror(int); 354233361SkibObj_Entry *map_object(int, const char *, const struct stat *); 355233361Skibvoid *xcalloc(size_t, size_t); 356233361Skibvoid *xmalloc(size_t); 357233361Skibchar *xstrdup(const char *); 358259290Skibvoid *malloc_aligned(size_t size, size_t align); 359259290Skibvoid free_aligned(void *ptr); 36038816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[]; 361212497Snwhitehornextern Elf_Sym sym_zero; /* For resolving undefined weak refs. */ 36234192Sjdp 363233361Skibvoid dump_relocations(Obj_Entry *); 364233361Skibvoid dump_obj_relocations(Obj_Entry *); 365233361Skibvoid dump_Elf_Rel(Obj_Entry *, const Elf_Rel *, u_long); 366233361Skibvoid dump_Elf_Rela(Obj_Entry *, const Elf_Rela *, u_long); 367116563Smdodd 36838816Sdfr/* 36938816Sdfr * Function declarations. 37038816Sdfr */ 37138816Sdfrunsigned long elf_hash(const char *); 37266056Sjdpconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *, 373216695Skib const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *); 37445501Sjdpvoid init_pltgot(Obj_Entry *); 375116557Smdoddvoid lockdflt_init(void); 376232831Skibvoid digest_notes(Obj_Entry *, Elf_Addr, Elf_Addr); 37750608Sjdpvoid obj_free(Obj_Entry *); 37850608SjdpObj_Entry *obj_new(void); 37945501Sjdpvoid _rtld_bind_start(void); 380228435Skibvoid *rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def); 381216695Skibvoid symlook_init(SymLook *, const char *); 382216695Skibint symlook_obj(SymLook *, const Obj_Entry *); 383133063Sdfrvoid *tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset); 384133063Sdfrvoid *allocate_tls(Obj_Entry *, void *, size_t, size_t); 385133063Sdfrvoid free_tls(void *, size_t, size_t); 386133063Sdfrvoid *allocate_module_tls(int index); 387133063Sdfrbool allocate_tls_offset(Obj_Entry *obj); 388142645Sdfrvoid free_tls_offset(Obj_Entry *obj); 389153515Skanconst Ver_Entry *fetch_ventry(const Obj_Entry *obj, unsigned long); 39038816Sdfr 391116558Smdodd/* 392116558Smdodd * MD function declarations. 393116558Smdodd */ 394116558Smdoddint do_copy_relocations(Obj_Entry *); 395233231Skibint reloc_non_plt(Obj_Entry *, Obj_Entry *, int flags, 396233231Skib struct Struct_RtldLockState *); 397116558Smdoddint reloc_plt(Obj_Entry *); 398233231Skibint reloc_jmpslots(Obj_Entry *, int flags, struct Struct_RtldLockState *); 399228435Skibint reloc_iresolve(Obj_Entry *, struct Struct_RtldLockState *); 400233231Skibint reloc_gnu_ifunc(Obj_Entry *, int flags, struct Struct_RtldLockState *); 401133063Sdfrvoid allocate_initial_tls(Obj_Entry *); 402116558Smdodd 40334192Sjdp#endif /* } */ 404