/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14*15* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR16* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES17* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.18* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,19* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT20* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,21* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY22* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT23* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF24* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.25*/2627#ifndef RTLD_H /* { */28#define RTLD_H 12930#include <machine/elf.h>31#include <sys/types.h>32#include <sys/queue.h>3334#include <elf-hints.h>35#include <link.h>36#include <stdarg.h>37#include <stdbool.h>38#include <setjmp.h>39#include <stddef.h>4041#include "rtld_lock.h"42#include "rtld_machdep.h"4344#define NEW(type) ((type *) xmalloc(sizeof(type)))45#define CNEW(type) ((type *) xcalloc(1, sizeof(type)))4647extern size_t tls_last_offset;48extern size_t tls_last_size;49extern size_t tls_static_space;50extern Elf_Addr tls_dtv_generation;51extern int tls_max_index;52extern size_t ld_static_tls_extra;5354extern int npagesizes;55extern size_t *pagesizes;56extern size_t page_size;5758extern int main_argc;59extern char **main_argv;60extern char **environ;6162struct stat;63struct Struct_Obj_Entry;6465/* Lists of shared objects */66typedef struct Struct_Objlist_Entry {67STAILQ_ENTRY(Struct_Objlist_Entry) link;68struct Struct_Obj_Entry *obj;69} Objlist_Entry;7071typedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;7273/* Types of init and fini functions */74typedef void (*InitFunc)(void);75typedef void (*InitArrFunc)(int, char **, char **);7677/* Lists of shared object dependencies */78typedef struct Struct_Needed_Entry {79struct Struct_Needed_Entry *next;80struct Struct_Obj_Entry *obj;81unsigned long name; /* Offset of name in string table */82} Needed_Entry;8384typedef struct Struct_Name_Entry {85STAILQ_ENTRY(Struct_Name_Entry) link;86char name[1];87} Name_Entry;8889/* Lock object */90typedef struct Struct_LockInfo {91void *context; /* Client context for creating locks */92void *thelock; /* The one big lock */93/* Debugging aids. */94volatile int rcount; /* Number of readers holding lock */95volatile int wcount; /* Number of writers holding lock */96/* Methods */97void *(*lock_create)(void *context);98void (*rlock_acquire)(void *lock);99void (*wlock_acquire)(void *lock);100void (*rlock_release)(void *lock);101void (*wlock_release)(void *lock);102void (*lock_destroy)(void *lock);103void (*context_destroy)(void *context);104} LockInfo;105106typedef struct Struct_Ver_Entry {107Elf_Word hash;108unsigned int flags;109const char *name;110const char *file;111} Ver_Entry;112113typedef struct Struct_Sym_Match_Result {114const Elf_Sym *sym_out;115const Elf_Sym *vsymp;116int vcount;117} Sym_Match_Result;118119#define VER_INFO_HIDDEN 0x01120121/*122* Shared object descriptor.123*124* Items marked with "(%)" are dynamically allocated, and must be freed125* when the structure is destroyed.126*127* CAUTION: It appears that the JDK port peeks into these structures.128* It looks at "next" and "mapbase" at least. Don't add new members129* near the front, until this can be straightened out.130*/131typedef struct Struct_Obj_Entry {132/*133* These two items have to be set right for compatibility with the134* original ElfKit crt1.o.135*/136Elf_Size magic; /* Magic number (sanity check) */137Elf_Size version; /* Version number of struct format */138139TAILQ_ENTRY(Struct_Obj_Entry) next;140char *path; /* Pathname of underlying file (%) */141char *origin_path; /* Directory path of origin file */142int refcount; /* DAG references */143int holdcount; /* Count of transient references */144int dl_refcount; /* Number of times loaded by dlopen */145146/* These items are computed by map_object() or by digest_phdr(). */147caddr_t mapbase; /* Base address of mapped region */148size_t mapsize; /* Size of mapped region in bytes */149Elf_Addr vaddrbase; /* Base address in shared object file */150caddr_t relocbase; /* Relocation constant = mapbase - vaddrbase */151const Elf_Dyn *dynamic; /* Dynamic section */152caddr_t entry; /* Entry point */153const Elf_Phdr *phdr; /* Program header if it is mapped, else NULL */154size_t phsize; /* Size of program header in bytes */155const char *interp; /* Pathname of the interpreter, if any */156Elf_Word stack_flags;157158/* TLS information */159int tlsindex; /* Index in DTV for this module */160void *tlsinit; /* Base address of TLS init block */161size_t tlsinitsize; /* Size of TLS init block for this module */162size_t tlssize; /* Size of TLS block for this module */163size_t tlsoffset; /* Offset of static TLS block for this module */164size_t tlsalign; /* Alignment of static TLS block */165size_t tlspoffset; /* p_offset of the static TLS block */166167/* Items from the dynamic section. */168Elf_Addr *pltgot; /* PLT or GOT, depending on architecture */169const Elf_Rel *rel; /* Relocation entries */170unsigned long relsize; /* Size in bytes of relocation info */171const Elf_Rela *rela; /* Relocation entries with addend */172unsigned long relasize; /* Size in bytes of addend relocation info */173const Elf_Relr *relr; /* RELR relocation entries */174unsigned long relrsize; /* Size in bytes of RELR relocations */175const Elf_Rel *pltrel; /* PLT relocation entries */176unsigned long pltrelsize; /* Size in bytes of PLT relocation info */177const Elf_Rela *pltrela; /* PLT relocation entries with addend */178unsigned long pltrelasize; /* Size in bytes of PLT addend reloc info */179const Elf_Sym *symtab; /* Symbol table */180const char *strtab; /* String table */181unsigned long strsize; /* Size in bytes of string table */182183const Elf_Verneed *verneed; /* Required versions. */184Elf_Word verneednum; /* Number of entries in verneed table */185const Elf_Verdef *verdef; /* Provided versions. */186Elf_Word verdefnum; /* Number of entries in verdef table */187const Elf_Versym *versyms; /* Symbol versions table */188189const Elf_Hashelt *buckets; /* Hash table buckets array */190unsigned long nbuckets; /* Number of buckets */191const Elf_Hashelt *chains; /* Hash table chain array */192unsigned long nchains; /* Number of entries in chain array */193194Elf32_Word nbuckets_gnu; /* Number of GNU hash buckets*/195Elf32_Word symndx_gnu; /* 1st accessible symbol on dynsym table */196Elf32_Word maskwords_bm_gnu; /* Bloom filter words - 1 (bitmask) */197Elf32_Word shift2_gnu; /* Bloom filter shift count */198Elf32_Word dynsymcount; /* Total entries in dynsym table */199const Elf_Addr *bloom_gnu; /* Bloom filter used by GNU hash func */200const Elf_Hashelt *buckets_gnu; /* GNU hash table bucket array */201const Elf_Hashelt *chain_zero_gnu; /* GNU hash table value array (Zeroed) */202203const char *rpath; /* Search path specified in object */204const char *runpath; /* Search path with different priority */205Needed_Entry *needed; /* Shared objects needed by this one (%) */206Needed_Entry *needed_filtees;207Needed_Entry *needed_aux_filtees;208209STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we210know about. */211Ver_Entry *vertab; /* Versions required /defined by this object */212int vernum; /* Number of entries in vertab */213214Elf_Addr init; /* Initialization function to call */215Elf_Addr fini; /* Termination function to call */216Elf_Addr preinit_array; /* Pre-initialization array of functions */217Elf_Addr init_array; /* Initialization array of functions */218Elf_Addr fini_array; /* Termination array of functions */219int preinit_array_num; /* Number of entries in preinit_array */220int init_array_num; /* Number of entries in init_array */221int fini_array_num; /* Number of entries in fini_array */222223int32_t osrel; /* OSREL note value */224uint32_t fctl0; /* FEATURE_CONTROL note desc[0] value */225226bool mainprog : 1; /* True if this is the main program */227bool rtld : 1; /* True if this is the dynamic linker */228bool relocated : 1; /* True if processed by relocate_objects() */229bool ver_checked : 1; /* True if processed by rtld_verify_object_versions */230bool textrel : 1; /* True if there are relocations to text seg */231bool symbolic : 1; /* True if generated with "-Bsymbolic" */232bool deepbind : 1; /* True if loaded with RTLD_DEEPBIND" */233bool bind_now : 1; /* True if all relocations should be made first */234bool traced : 1; /* Already printed in ldd trace output */235bool jmpslots_done : 1; /* Already have relocated the jump slots */236bool init_done : 1; /* Already have added object to init list */237bool tls_static : 1; /* Already allocated offset for static TLS */238bool tls_dynamic : 1; /* A non-static DTV entry has been allocated */239bool phdr_alloc : 1; /* Phdr is allocated and needs to be freed. */240bool z_origin : 1; /* Process rpath and soname tokens */241bool z_nodelete : 1; /* Do not unload the object and dependencies */242bool z_noopen : 1; /* Do not load on dlopen */243bool z_loadfltr : 1; /* Immediately load filtees */244bool z_interpose : 1; /* Interpose all objects but main */245bool z_nodeflib : 1; /* Don't search default library path */246bool z_global : 1; /* Make the object global */247bool z_pie : 1; /* Object proclaimed itself PIE executable */248bool z_initfirst : 1; /* Proceed initializers before other objects */249bool static_tls : 1; /* Needs static TLS allocation */250bool static_tls_copied : 1; /* Needs static TLS copying */251bool ref_nodel : 1; /* Refcount increased to prevent dlclose */252bool init_scanned: 1; /* Object is already on init list. */253bool on_fini_list: 1; /* Object is already on fini list. */254bool dag_inited : 1; /* Object has its DAG initialized. */255bool filtees_loaded : 1; /* Filtees loaded */256bool filtees_loading : 1; /* In process of filtees loading */257bool irelative : 1; /* Object has R_MACHDEP_IRELATIVE relocs */258bool irelative_nonplt : 1; /* Object has R_MACHDEP_IRELATIVE non-plt relocs */259bool gnu_ifunc : 1; /* Object has references to STT_GNU_IFUNC */260bool non_plt_gnu_ifunc : 1; /* Object has non-plt IFUNC references */261bool ifuncs_resolved : 1; /* Object ifuncs were already resolved */262bool crt_no_init : 1; /* Object' crt does not call _init/_fini */263bool valid_hash_sysv : 1; /* A valid System V hash hash tag is available */264bool valid_hash_gnu : 1; /* A valid GNU hash tag is available */265bool dlopened : 1; /* dlopen()-ed (vs. load statically) */266bool marker : 1; /* marker on the global obj list */267bool unholdfree : 1; /* unmap upon last unhold */268bool doomed : 1; /* Object cannot be referenced */269270MD_OBJ_ENTRY271272struct link_map linkmap; /* For GDB and dlinfo() */273Objlist dldags; /* Object belongs to these dlopened DAGs (%) */274Objlist dagmembers; /* DAG has these members (%) */275dev_t dev; /* Object's filesystem's device */276ino_t ino; /* Object's inode number */277void *priv; /* Platform-dependent */278} Obj_Entry;279280#define RTLD_MAGIC 0xd550b87a281#define RTLD_VERSION 1282283TAILQ_HEAD(obj_entry_q, Struct_Obj_Entry);284285#define RTLD_STATIC_TLS_EXTRA 128286287/* Flags to be passed into symlook_ family of functions. */288#define SYMLOOK_IN_PLT 0x01 /* Lookup for PLT symbol */289#define SYMLOOK_DLSYM 0x02 /* Return newest versioned symbol. Used by290dlsym. */291#define SYMLOOK_EARLY 0x04 /* Symlook is done during initialization. */292#define SYMLOOK_IFUNC 0x08 /* Allow IFUNC processing in293reloc_non_plt(). */294295/* Flags for load_object(). */296#define RTLD_LO_NOLOAD 0x01 /* dlopen() specified RTLD_NOLOAD. */297#define RTLD_LO_DLOPEN 0x02 /* Load_object() called from dlopen(). */298#define RTLD_LO_TRACE 0x04 /* Only tracing. */299#define RTLD_LO_NODELETE 0x08 /* Loaded object cannot be closed. */300#define RTLD_LO_FILTEES 0x10 /* Loading filtee. */301#define RTLD_LO_EARLY 0x20 /* Do not call ctors, postpone it to the302initialization during the image start. */303#define RTLD_LO_IGNSTLS 0x40 /* Do not allocate static TLS */304#define RTLD_LO_DEEPBIND 0x80 /* Force symbolic for this object */305306/*307* Symbol cache entry used during relocation to avoid multiple lookups308* of the same symbol.309*/310typedef struct Struct_SymCache {311const Elf_Sym *sym; /* Symbol table entry */312const Obj_Entry *obj; /* Shared object which defines it */313} SymCache;314315/*316* This structure provides a reentrant way to keep a list of objects and317* check which ones have already been processed in some way.318*/319typedef struct Struct_DoneList {320const Obj_Entry **objs; /* Array of object pointers */321unsigned int num_alloc; /* Allocated size of the array */322unsigned int num_used; /* Number of array slots used */323} DoneList;324325struct Struct_RtldLockState {326int lockstate;327sigjmp_buf env;328};329330struct fill_search_info_args {331int request;332unsigned int flags;333struct dl_serinfo *serinfo;334struct dl_serpath *serpath;335char *strspace;336};337338/*339* The pack of arguments and results for the symbol lookup functions.340*/341typedef struct Struct_SymLook {342const char *name;343unsigned long hash;344uint32_t hash_gnu;345const Ver_Entry *ventry;346int flags;347const Obj_Entry *defobj_out;348const Elf_Sym *sym_out;349struct Struct_RtldLockState *lockstate;350} SymLook;351352enum {353LD_BIND_NOW = 0,354LD_PRELOAD,355LD_LIBMAP,356LD_LIBRARY_PATH,357LD_LIBRARY_PATH_FDS,358LD_LIBMAP_DISABLE,359LD_BIND_NOT,360LD_DEBUG,361LD_ELF_HINTS_PATH,362LD_LOADFLTR,363LD_LIBRARY_PATH_RPATH,364LD_PRELOAD_FDS,365LD_DYNAMIC_WEAK,366LD_TRACE_LOADED_OBJECTS,367LD_UTRACE,368LD_DUMP_REL_PRE,369LD_DUMP_REL_POST,370LD_TRACE_LOADED_OBJECTS_PROGNAME,371LD_TRACE_LOADED_OBJECTS_FMT1,372LD_TRACE_LOADED_OBJECTS_FMT2,373LD_TRACE_LOADED_OBJECTS_ALL,374LD_SHOW_AUXV,375LD_STATIC_TLS_EXTRA,376LD_NO_DL_ITERATE_PHDR_AFTER_FORK,377};378379void _rtld_error(const char *, ...) __printflike(1, 2) __exported;380void rtld_die(void) __dead2;381const char *rtld_strerror(int);382Obj_Entry *map_object(int, const char *, const struct stat *, bool);383void *xcalloc(size_t, size_t);384void *xmalloc(size_t);385char *xstrdup(const char *);386void *xmalloc_aligned(size_t size, size_t align, size_t offset);387extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];388extern Elf_Sym sym_zero; /* For resolving undefined weak refs. */389extern bool ld_bind_not;390extern bool ld_fast_sigblock;391392void dump_relocations(Obj_Entry *);393void dump_obj_relocations(Obj_Entry *);394void dump_Elf_Rel(Obj_Entry *, const Elf_Rel *, u_long);395void dump_Elf_Rela(Obj_Entry *, const Elf_Rela *, u_long);396397/*398* Function declarations.399*/400const char *ld_get_env_var(int idx);401uintptr_t rtld_round_page(uintptr_t);402uintptr_t rtld_trunc_page(uintptr_t);403Elf32_Word elf_hash(const char *);404const Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,405const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *);406void lockdflt_init(void);407void digest_notes(Obj_Entry *, Elf_Addr, Elf_Addr);408Obj_Entry *globallist_curr(const Obj_Entry *obj);409Obj_Entry *globallist_next(const Obj_Entry *obj);410void obj_free(Obj_Entry *);411Obj_Entry *obj_new(void);412Obj_Entry *obj_from_addr(const void *);413void _rtld_bind_start(void);414void *rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def);415void symlook_init(SymLook *, const char *);416int symlook_obj(SymLook *, const Obj_Entry *);417void *tls_get_addr_common(struct tcb *tcb, int index, size_t offset);418void *allocate_tls(Obj_Entry *, void *, size_t, size_t);419void free_tls(void *, size_t, size_t);420void *allocate_module_tls(struct tcb *tcb, int index);421bool allocate_tls_offset(Obj_Entry *obj);422void free_tls_offset(Obj_Entry *obj);423const Ver_Entry *fetch_ventry(const Obj_Entry *obj, unsigned long);424int convert_prot(int elfflags);425bool check_elf_headers(const Elf_Ehdr *hdr, const char *path);426427/*428* MD function declarations.429*/430int do_copy_relocations(Obj_Entry *);431int reloc_non_plt(Obj_Entry *, Obj_Entry *, int flags,432struct Struct_RtldLockState *);433int reloc_plt(Obj_Entry *, int flags, struct Struct_RtldLockState *);434int reloc_jmpslots(Obj_Entry *, int flags, struct Struct_RtldLockState *);435int reloc_iresolve(Obj_Entry *, struct Struct_RtldLockState *);436int reloc_iresolve_nonplt(Obj_Entry *, struct Struct_RtldLockState *);437int reloc_gnu_ifunc(Obj_Entry *, int flags, struct Struct_RtldLockState *);438void ifunc_init(Elf_Auxinfo *[__min_size(AT_COUNT)]);439void init_pltgot(Obj_Entry *);440void allocate_initial_tls(Obj_Entry *);441442#endif /* } */443444445