rtld.h revision 346156
140939Sdes/*-
240939Sdes * Copyright 1996, 1997, 1998, 1999, 2000 John D. Polstra.
340939Sdes * All rights reserved.
440939Sdes *
540939Sdes * Redistribution and use in source and binary forms, with or without
640939Sdes * modification, are permitted provided that the following conditions
740939Sdes * are met:
840939Sdes * 1. Redistributions of source code must retain the above copyright
940939Sdes *    notice, this list of conditions and the following disclaimer.
1040939Sdes * 2. Redistributions in binary form must reproduce the above copyright
1140939Sdes *    notice, this list of conditions and the following disclaimer in the
1240939Sdes *    documentation and/or other materials provided with the distribution.
1340939Sdes *
1440939Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1540939Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1640939Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1740939Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1840939Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1940939Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2040939Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2140939Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2240939Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2340939Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2440939Sdes *
2540939Sdes * $FreeBSD: stable/11/libexec/rtld-elf/rtld.h 346156 2019-04-12 15:15:27Z kib $
2640939Sdes */
2740939Sdes
2850476Speter#ifndef RTLD_H /* { */
2940939Sdes#define RTLD_H 1
3040939Sdes
3141862Sdes#include <machine/elf.h>
3240939Sdes#include <sys/types.h>
3355557Sdes#include <sys/queue.h>
3462981Sdes
3540939Sdes#include <elf-hints.h>
3640939Sdes#include <link.h>
3740939Sdes#include <stdarg.h>
3840939Sdes#include <setjmp.h>
3960924Sdes#include <stddef.h>
4041862Sdes
4141862Sdes#include "rtld_lock.h"
4240939Sdes#include "rtld_machdep.h"
4340939Sdes
4440939Sdes#define NEW(type)	((type *) xmalloc(sizeof(type)))
4540939Sdes#define CNEW(type)	((type *) xcalloc(1, sizeof(type)))
4640939Sdes
4740939Sdes/* We might as well do booleans like C++. */
4840975Sdestypedef unsigned char bool;
4940939Sdes#define false	0
5040939Sdes#define true	1
5140939Sdes
5240939Sdesextern size_t tls_last_offset;
5340939Sdesextern size_t tls_last_size;
5440939Sdesextern size_t tls_static_space;
5560737Sumeextern int tls_dtv_generation;
5660737Sumeextern int tls_max_index;
5760737Sume
5860737Sumeextern int npagesizes;
5940975Sdesextern size_t *pagesizes;
6040939Sdes
6140939Sdesextern int main_argc;
6262981Sdesextern char **main_argv;
6362981Sdesextern char **environ;
6440939Sdes
6562981Sdesstruct stat;
6640939Sdesstruct Struct_Obj_Entry;
6740939Sdes
6840939Sdes/* Lists of shared objects */
6940939Sdestypedef struct Struct_Objlist_Entry {
7040939Sdes    STAILQ_ENTRY(Struct_Objlist_Entry) link;
7160924Sdes    struct Struct_Obj_Entry *obj;
7240975Sdes} Objlist_Entry;
7340939Sdes
7460924Sdestypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
7560924Sdes
7660924Sdes/* Types of init and fini functions */
7740939Sdestypedef void (*InitFunc)(void);
7840939Sdestypedef void (*InitArrFunc)(int, char **, char **);
7940939Sdes
8040939Sdes/* Lists of shared object dependencies */
8140939Sdestypedef struct Struct_Needed_Entry {
8240939Sdes    struct Struct_Needed_Entry *next;
8340939Sdes    struct Struct_Obj_Entry *obj;
8440939Sdes    unsigned long name;		/* Offset of name in string table */
8560924Sdes} Needed_Entry;
8660924Sdes
8760924Sdestypedef struct Struct_Name_Entry {
8840939Sdes    STAILQ_ENTRY(Struct_Name_Entry) link;
8940939Sdes    char   name[1];
9040939Sdes} Name_Entry;
9140939Sdes
9240939Sdes/* Lock object */
9340939Sdestypedef struct Struct_LockInfo {
9440939Sdes    void *context;		/* Client context for creating locks */
9540939Sdes    void *thelock;		/* The one big lock */
9641862Sdes    /* Debugging aids. */
9741862Sdes    volatile int rcount;	/* Number of readers holding lock */
9840975Sdes    volatile int wcount;	/* Number of writers holding lock */
9940975Sdes    /* Methods */
10040975Sdes    void *(*lock_create)(void *context);
10141862Sdes    void (*rlock_acquire)(void *lock);
10240975Sdes    void (*wlock_acquire)(void *lock);
10340975Sdes    void (*rlock_release)(void *lock);
10440975Sdes    void (*wlock_release)(void *lock);
10540975Sdes    void (*lock_destroy)(void *lock);
10640975Sdes    void (*context_destroy)(void *context);
10740975Sdes} LockInfo;
10841862Sdes
10940975Sdestypedef struct Struct_Ver_Entry {
11040975Sdes	Elf_Word     hash;
11140975Sdes	unsigned int flags;
11241862Sdes	const char  *name;
11340975Sdes	const char  *file;
11440975Sdes} Ver_Entry;
11541862Sdes
11640975Sdestypedef struct Struct_Sym_Match_Result {
11740975Sdes    const Elf_Sym *sym_out;
11840975Sdes    const Elf_Sym *vsymp;
11941862Sdes    int vcount;
12040975Sdes} Sym_Match_Result;
12140975Sdes
12241862Sdes#define VER_INFO_HIDDEN	0x01
12340975Sdes
12440975Sdes/*
12541862Sdes * Shared object descriptor.
12640975Sdes *
12740975Sdes * Items marked with "(%)" are dynamically allocated, and must be freed
12840975Sdes * when the structure is destroyed.
12940975Sdes *
13040975Sdes * CAUTION: It appears that the JDK port peeks into these structures.
13140975Sdes * It looks at "next" and "mapbase" at least.  Don't add new members
13240975Sdes * near the front, until this can be straightened out.
13341862Sdes */
13440975Sdestypedef struct Struct_Obj_Entry {
13540975Sdes    /*
13640975Sdes     * These two items have to be set right for compatibility with the
13741862Sdes     * original ElfKit crt1.o.
13840975Sdes     */
13940975Sdes    Elf_Size magic;		/* Magic number (sanity check) */
14041862Sdes    Elf_Size version;		/* Version number of struct format */
14140975Sdes
14240975Sdes    TAILQ_ENTRY(Struct_Obj_Entry) next;
14340975Sdes    char *path;			/* Pathname of underlying file (%) */
14441862Sdes    char *origin_path;		/* Directory path of origin file */
14540975Sdes    int refcount;		/* DAG references */
14640975Sdes    int holdcount;		/* Count of transient references */
14741862Sdes    int dl_refcount;		/* Number of times loaded by dlopen */
14840975Sdes
14960924Sdes    /* These items are computed by map_object() or by digest_phdr(). */
15040939Sdes    caddr_t mapbase;		/* Base address of mapped region */
15140939Sdes    size_t mapsize;		/* Size of mapped region in bytes */
15240939Sdes    size_t textsize;		/* Size of text segment in bytes */
15341862Sdes    Elf_Addr vaddrbase;		/* Base address in shared object file */
15441862Sdes    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
15541862Sdes    const Elf_Dyn *dynamic;	/* Dynamic section */
15660924Sdes    caddr_t entry;		/* Entry point */
15741862Sdes    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
15841862Sdes    size_t phsize;		/* Size of program header in bytes */
15941862Sdes    const char *interp;		/* Pathname of the interpreter, if any */
16041862Sdes    Elf_Word stack_flags;
16141862Sdes
16260924Sdes    /* TLS information */
16341862Sdes    int tlsindex;		/* Index in DTV for this module */
16460928Sdes    void *tlsinit;		/* Base address of TLS init block */
16541862Sdes    size_t tlsinitsize;		/* Size of TLS init block for this module */
16641862Sdes    size_t tlssize;		/* Size of TLS block for this module */
16741862Sdes    size_t tlsoffset;		/* Offset of static TLS block for this module */
16840939Sdes    size_t tlsalign;		/* Alignment of static TLS block */
16940939Sdes
17040939Sdes    caddr_t relro_page;
17140939Sdes    size_t relro_size;
17240939Sdes
17340939Sdes    /* Items from the dynamic section. */
17460737Sume    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
17540939Sdes    const Elf_Rel *rel;		/* Relocation entries */
17660737Sume    unsigned long relsize;	/* Size in bytes of relocation info */
17760737Sume    const Elf_Rela *rela;	/* Relocation entries with addend */
17860737Sume    unsigned long relasize;	/* Size in bytes of addend relocation info */
17940939Sdes    const Elf_Rel *pltrel;	/* PLT relocation entries */
18062964Sdes    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
18141862Sdes    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
18241862Sdes    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
18341862Sdes    const Elf_Sym *symtab;	/* Symbol table */
18440939Sdes    const char *strtab;		/* String table */
18560737Sume    unsigned long strsize;	/* Size in bytes of string table */
18660737Sume#ifdef __mips__
18760737Sume    Elf_Word local_gotno;	/* Number of local GOT entries */
18860737Sume    Elf_Word symtabno;		/* Number of dynamic symbols */
18960737Sume    Elf_Word gotsym;		/* First dynamic symbol in GOT */
19060737Sume#endif
19160737Sume#ifdef __powerpc64__
19260737Sume    Elf_Addr glink;		/* GLINK PLT call stub section */
19340939Sdes#endif
19440939Sdes
19540939Sdes    const Elf_Verneed *verneed; /* Required versions. */
19641862Sdes    Elf_Word verneednum;	/* Number of entries in verneed table */
19741862Sdes    const Elf_Verdef  *verdef;	/* Provided versions. */
19841862Sdes    Elf_Word verdefnum;		/* Number of entries in verdef table */
19940939Sdes    const Elf_Versym *versyms;  /* Symbol versions table */
20062981Sdes
20160737Sume    const Elf_Hashelt *buckets;	/* Hash table buckets array */
20262981Sdes    unsigned long nbuckets;	/* Number of buckets */
20360737Sume    const Elf_Hashelt *chains;	/* Hash table chain array */
20462981Sdes    unsigned long nchains;	/* Number of entries in chain array */
20560737Sume
20660737Sume    Elf32_Word nbuckets_gnu;		/* Number of GNU hash buckets*/
20760737Sume    Elf32_Word symndx_gnu;		/* 1st accessible symbol on dynsym table */
20840939Sdes    Elf32_Word maskwords_bm_gnu;  	/* Bloom filter words - 1 (bitmask) */
20962911Sume    Elf32_Word shift2_gnu;		/* Bloom filter shift count */
21062981Sdes    Elf32_Word dynsymcount;		/* Total entries in dynsym table */
21140939Sdes    Elf_Addr *bloom_gnu;		/* Bloom filter used by GNU hash func */
21240939Sdes    const Elf_Hashelt *buckets_gnu;	/* GNU hash table bucket array */
21340939Sdes    const Elf_Hashelt *chain_zero_gnu;	/* GNU hash table value array (Zeroed) */
21440939Sdes
21540939Sdes    char *rpath;		/* Search path specified in object */
21640939Sdes    char *runpath;		/* Search path with different priority */
21741989Sdes    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
21841989Sdes    Needed_Entry *needed_filtees;
21955557Sdes    Needed_Entry *needed_aux_filtees;
22055557Sdes
22155557Sdes    STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we
22255557Sdes					       know about. */
22355557Sdes    Ver_Entry *vertab;		/* Versions required /defined by this object */
22455557Sdes    int vernum;			/* Number of entries in vertab */
22555557Sdes
22655557Sdes    Elf_Addr init;		/* Initialization function to call */
22755557Sdes    Elf_Addr fini;		/* Termination function to call */
22855557Sdes    Elf_Addr preinit_array;	/* Pre-initialization array of functions */
22955557Sdes    Elf_Addr init_array;	/* Initialization array of functions */
23055557Sdes    Elf_Addr fini_array;	/* Termination array of functions */
23155557Sdes    int preinit_array_num;	/* Number of entries in preinit_array */
23255557Sdes    int init_array_num; 	/* Number of entries in init_array */
23355557Sdes    int fini_array_num; 	/* Number of entries in fini_array */
23455557Sdes
23555557Sdes    int32_t osrel;		/* OSREL note value */
23655557Sdes
23755557Sdes    bool mainprog : 1;		/* True if this is the main program */
23855557Sdes    bool rtld : 1;		/* True if this is the dynamic linker */
23955557Sdes    bool relocated : 1;		/* True if processed by relocate_objects() */
24055557Sdes    bool ver_checked : 1;	/* True if processed by rtld_verify_object_versions */
24155557Sdes    bool textrel : 1;		/* True if there are relocations to text seg */
24255557Sdes    bool symbolic : 1;		/* True if generated with "-Bsymbolic" */
24355557Sdes    bool bind_now : 1;		/* True if all relocations should be made first */
24455557Sdes    bool traced : 1;		/* Already printed in ldd trace output */
24555557Sdes    bool jmpslots_done : 1;	/* Already have relocated the jump slots */
24655557Sdes    bool init_done : 1;		/* Already have added object to init list */
24755557Sdes    bool tls_done : 1;		/* Already allocated offset for static TLS */
24855557Sdes    bool phdr_alloc : 1;	/* Phdr is allocated and needs to be freed. */
24955557Sdes    bool z_origin : 1;		/* Process rpath and soname tokens */
25055557Sdes    bool z_nodelete : 1;	/* Do not unload the object and dependencies */
25155557Sdes    bool z_noopen : 1;		/* Do not load on dlopen */
25255557Sdes    bool z_loadfltr : 1;	/* Immediately load filtees */
25355557Sdes    bool z_interpose : 1;	/* Interpose all objects but main */
25455557Sdes    bool z_nodeflib : 1;	/* Don't search default library path */
25555557Sdes    bool z_global : 1;		/* Make the object global */
25655557Sdes    bool static_tls : 1;	/* Needs static TLS allocation */
25755557Sdes    bool static_tls_copied : 1;	/* Needs static TLS copying */
25855557Sdes    bool ref_nodel : 1;		/* Refcount increased to prevent dlclose */
25955557Sdes    bool init_scanned: 1;	/* Object is already on init list. */
26055557Sdes    bool on_fini_list: 1;	/* Object is already on fini list. */
26155557Sdes    bool dag_inited : 1;	/* Object has its DAG initialized. */
26255557Sdes    bool filtees_loaded : 1;	/* Filtees loaded */
26355557Sdes    bool irelative : 1;		/* Object has R_MACHDEP_IRELATIVE relocs */
26455557Sdes    bool gnu_ifunc : 1;		/* Object has references to STT_GNU_IFUNC */
26563334Sdes    bool non_plt_gnu_ifunc : 1;	/* Object has non-plt IFUNC references */
26655557Sdes    bool ifuncs_resolved : 1;	/* Object ifuncs were already resolved */
26755557Sdes    bool crt_no_init : 1;	/* Object' crt does not call _init/_fini */
26855557Sdes    bool valid_hash_sysv : 1;	/* A valid System V hash hash tag is available */
26955557Sdes    bool valid_hash_gnu : 1;	/* A valid GNU hash tag is available */
27055557Sdes    bool dlopened : 1;		/* dlopen()-ed (vs. load statically) */
27155557Sdes    bool marker : 1;		/* marker on the global obj list */
27255557Sdes    bool unholdfree : 1;	/* unmap upon last unhold */
27355557Sdes    bool doomed : 1;		/* Object cannot be referenced */
27455557Sdes
27555557Sdes    struct link_map linkmap;	/* For GDB and dlinfo() */
27655557Sdes    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
27763334Sdes    Objlist dagmembers;		/* DAG has these members (%) */
27855557Sdes    dev_t dev;			/* Object's filesystem's device */
27955557Sdes    ino_t ino;			/* Object's inode number */
28055557Sdes    void *priv;			/* Platform-dependent */
28155557Sdes} Obj_Entry;
28255557Sdes
28355557Sdes#define RTLD_MAGIC	0xd550b87a
28455557Sdes#define RTLD_VERSION	1
28555557Sdes
28655557SdesTAILQ_HEAD(obj_entry_q, Struct_Obj_Entry);
28755557Sdes
28855557Sdes#define RTLD_STATIC_TLS_EXTRA	128
28955557Sdes
29055557Sdes/* Flags to be passed into symlook_ family of functions. */
29155557Sdes#define SYMLOOK_IN_PLT	0x01	/* Lookup for PLT symbol */
29255557Sdes#define SYMLOOK_DLSYM	0x02	/* Return newest versioned symbol. Used by
29355557Sdes				   dlsym. */
29455557Sdes#define	SYMLOOK_EARLY	0x04	/* Symlook is done during initialization. */
29555557Sdes#define	SYMLOOK_IFUNC	0x08	/* Allow IFUNC processing in
29662964Sdes				   reloc_non_plt(). */
29755557Sdes
29855557Sdes/* Flags for load_object(). */
29955557Sdes#define	RTLD_LO_NOLOAD	0x01	/* dlopen() specified RTLD_NOLOAD. */
30055557Sdes#define	RTLD_LO_DLOPEN	0x02	/* Load_object() called from dlopen(). */
30162981Sdes#define	RTLD_LO_TRACE	0x04	/* Only tracing. */
30262981Sdes#define	RTLD_LO_NODELETE 0x08	/* Loaded object cannot be closed. */
30362981Sdes#define	RTLD_LO_FILTEES 0x10	/* Loading filtee. */
30462981Sdes#define	RTLD_LO_EARLY	0x20	/* Do not call ctors, postpone it to the
30562981Sdes				   initialization during the image start. */
30662981Sdes
30762981Sdes/*
30862981Sdes * Symbol cache entry used during relocation to avoid multiple lookups
30962981Sdes * of the same symbol.
31062981Sdes */
31162981Sdestypedef struct Struct_SymCache {
31262981Sdes    const Elf_Sym *sym;		/* Symbol table entry */
31362981Sdes    const Obj_Entry *obj;	/* Shared object which defines it */
31462981Sdes} SymCache;
31562981Sdes
31662981Sdes/*
31762981Sdes * This structure provides a reentrant way to keep a list of objects and
31862981Sdes * check which ones have already been processed in some way.
31962981Sdes */
32062981Sdestypedef struct Struct_DoneList {
32162981Sdes    const Obj_Entry **objs;		/* Array of object pointers */
32241989Sdes    unsigned int num_alloc;		/* Allocated size of the array */
32341989Sdes    unsigned int num_used;		/* Number of array slots used */
32441989Sdes} DoneList;
32541989Sdes
32641989Sdesstruct Struct_RtldLockState {
32741989Sdes	int lockstate;
32841989Sdes	sigjmp_buf env;
32941989Sdes};
33041989Sdes
33141989Sdesstruct fill_search_info_args {
33241989Sdes	int request;
33341989Sdes	unsigned int flags;
33441989Sdes	struct dl_serinfo *serinfo;
33541989Sdes	struct dl_serpath *serpath;
33641989Sdes	char *strspace;
33741989Sdes};
33841989Sdes
33941989Sdes/*
34041989Sdes * The pack of arguments and results for the symbol lookup functions.
34141989Sdes */
34241989Sdestypedef struct Struct_SymLook {
34341989Sdes    const char *name;
34441989Sdes    unsigned long hash;
34541989Sdes    uint32_t hash_gnu;
34641989Sdes    const Ver_Entry *ventry;
34741989Sdes    int flags;
34841989Sdes    const Obj_Entry *defobj_out;
34941989Sdes    const Elf_Sym *sym_out;
35041989Sdes    struct Struct_RtldLockState *lockstate;
35141989Sdes} SymLook;
35241989Sdes
35341989Sdesvoid _rtld_error(const char *, ...) __printflike(1, 2) __exported;
35441989Sdesvoid rtld_die(void) __dead2;
35541989Sdesconst char *rtld_strerror(int);
35641989SdesObj_Entry *map_object(int, const char *, const struct stat *);
35741989Sdesvoid *xcalloc(size_t, size_t);
35841989Sdesvoid *xmalloc(size_t);
35941989Sdeschar *xstrdup(const char *);
36041989Sdesvoid *malloc_aligned(size_t size, size_t align);
36141989Sdesvoid free_aligned(void *ptr);
362extern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
363extern Elf_Sym sym_zero;	/* For resolving undefined weak refs. */
364extern bool ld_bind_not;
365
366void dump_relocations(Obj_Entry *);
367void dump_obj_relocations(Obj_Entry *);
368void dump_Elf_Rel(Obj_Entry *, const Elf_Rel *, u_long);
369void dump_Elf_Rela(Obj_Entry *, const Elf_Rela *, u_long);
370
371/*
372 * Function declarations.
373 */
374unsigned long elf_hash(const char *);
375const Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
376  const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *);
377void lockdflt_init(void);
378void digest_notes(Obj_Entry *, Elf_Addr, Elf_Addr);
379Obj_Entry *globallist_curr(const Obj_Entry *obj);
380Obj_Entry *globallist_next(const Obj_Entry *obj);
381void obj_free(Obj_Entry *);
382Obj_Entry *obj_new(void);
383void _rtld_bind_start(void);
384void *rtld_resolve_ifunc(const Obj_Entry *obj, const Elf_Sym *def);
385void symlook_init(SymLook *, const char *);
386int symlook_obj(SymLook *, const Obj_Entry *);
387void *tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset);
388void *allocate_tls(Obj_Entry *, void *, size_t, size_t);
389void free_tls(void *, size_t, size_t);
390void *allocate_module_tls(int index);
391bool allocate_tls_offset(Obj_Entry *obj);
392void free_tls_offset(Obj_Entry *obj);
393const Ver_Entry *fetch_ventry(const Obj_Entry *obj, unsigned long);
394int convert_prot(int elfflags);
395
396/*
397 * MD function declarations.
398 */
399int do_copy_relocations(Obj_Entry *);
400int reloc_non_plt(Obj_Entry *, Obj_Entry *, int flags,
401    struct Struct_RtldLockState *);
402int reloc_plt(Obj_Entry *);
403int reloc_jmpslots(Obj_Entry *, int flags, struct Struct_RtldLockState *);
404int reloc_iresolve(Obj_Entry *, struct Struct_RtldLockState *);
405int reloc_gnu_ifunc(Obj_Entry *, int flags, struct Struct_RtldLockState *);
406void ifunc_init(Elf_Auxinfo[__min_size(AT_COUNT)]);
407void pre_init(void);
408void init_pltgot(Obj_Entry *);
409void allocate_initial_tls(Obj_Entry *);
410
411#endif /* } */
412