rtld.h revision 216695
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: head/libexec/rtld-elf/rtld.h 216695 2010-12-25 08:51:20Z kib $
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>
37216695Skib#include <setjmp.h>
3834192Sjdp#include <stddef.h>
3934192Sjdp
40115396Skan#include "rtld_lock.h"
4145501Sjdp#include "rtld_machdep.h"
4245501Sjdp
43127250Speter#ifdef COMPAT_32BIT
44127250Speter#undef STANDARD_LIBRARY_PATH
45127250Speter#undef _PATH_ELF_HINTS
46127250Speter#define	_PATH_ELF_HINTS		"/var/run/ld-elf32.so.hints"
47127250Speter/* For running 32 bit binaries  */
48127250Speter#define	STANDARD_LIBRARY_PATH	"/lib32:/usr/lib32"
49127250Speter#define LD_ "LD_32_"
50127250Speter#endif
51127250Speter
5234192Sjdp#ifndef STANDARD_LIBRARY_PATH
53119013Sgordon#define STANDARD_LIBRARY_PATH	"/lib:/usr/lib"
5434192Sjdp#endif
55127250Speter#ifndef LD_
56127250Speter#define LD_ "LD_"
57127250Speter#endif
5834192Sjdp
5934192Sjdp#define NEW(type)	((type *) xmalloc(sizeof(type)))
6034192Sjdp#define CNEW(type)	((type *) xcalloc(sizeof(type)))
6134192Sjdp
6234192Sjdp/* We might as well do booleans like C++. */
6334192Sjdptypedef unsigned char bool;
6434192Sjdp#define false	0
6534192Sjdp#define true	1
6634192Sjdp
67133063Sdfrextern size_t tls_last_offset;
68133063Sdfrextern size_t tls_last_size;
69133063Sdfrextern size_t tls_static_space;
70133063Sdfrextern int tls_dtv_generation;
71133063Sdfrextern int tls_max_index;
72133063Sdfr
7350609Sjdpstruct stat;
7434192Sjdpstruct Struct_Obj_Entry;
7534192Sjdp
7655687Sjdp/* Lists of shared objects */
7750608Sjdptypedef struct Struct_Objlist_Entry {
7860938Sjake    STAILQ_ENTRY(Struct_Objlist_Entry) link;
7950608Sjdp    struct Struct_Obj_Entry *obj;
8050608Sjdp} Objlist_Entry;
8150608Sjdp
8260938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
8350608Sjdp
8463870Sjdp/* Types of init and fini functions */
8555687Sjdptypedef void (*InitFunc)(void);
8655687Sjdp
8755687Sjdp/* Lists of shared object dependencies */
8834192Sjdptypedef struct Struct_Needed_Entry {
8934192Sjdp    struct Struct_Needed_Entry *next;
9034192Sjdp    struct Struct_Obj_Entry *obj;
9134192Sjdp    unsigned long name;		/* Offset of name in string table */
9234192Sjdp} Needed_Entry;
9334192Sjdp
94153515Skantypedef struct Struct_Name_Entry {
95153515Skan    STAILQ_ENTRY(Struct_Name_Entry) link;
96153515Skan    char   name[1];
97153515Skan} Name_Entry;
98153515Skan
9962801Sjdp/* Lock object */
10062801Sjdptypedef struct Struct_LockInfo {
10162801Sjdp    void *context;		/* Client context for creating locks */
10262801Sjdp    void *thelock;		/* The one big lock */
10362801Sjdp    /* Debugging aids. */
10462801Sjdp    volatile int rcount;	/* Number of readers holding lock */
10562801Sjdp    volatile int wcount;	/* Number of writers holding lock */
10662801Sjdp    /* Methods */
10762801Sjdp    void *(*lock_create)(void *context);
10862801Sjdp    void (*rlock_acquire)(void *lock);
10962801Sjdp    void (*wlock_acquire)(void *lock);
11062801Sjdp    void (*rlock_release)(void *lock);
11162801Sjdp    void (*wlock_release)(void *lock);
11262801Sjdp    void (*lock_destroy)(void *lock);
11362801Sjdp    void (*context_destroy)(void *context);
11462801Sjdp} LockInfo;
11562801Sjdp
116153515Skantypedef struct Struct_Ver_Entry {
117153515Skan	Elf_Word     hash;
118153515Skan	unsigned int flags;
119153515Skan	const char  *name;
120153515Skan	const char  *file;
121153515Skan} Ver_Entry;
122153515Skan
123153515Skan#define VER_INFO_HIDDEN	0x01
124153515Skan
12534192Sjdp/*
12634192Sjdp * Shared object descriptor.
12734192Sjdp *
12834192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
12934192Sjdp * when the structure is destroyed.
13050977Sjdp *
13150977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
13250977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
13350977Sjdp * near the front, until this can be straightened out.
13434192Sjdp */
13534192Sjdptypedef struct Struct_Obj_Entry {
13634192Sjdp    /*
13734192Sjdp     * These two items have to be set right for compatibility with the
13834192Sjdp     * original ElfKit crt1.o.
13934192Sjdp     */
140153504Smarcel    Elf_Size magic;		/* Magic number (sanity check) */
141153504Smarcel    Elf_Size version;		/* Version number of struct format */
14234192Sjdp
14334192Sjdp    struct Struct_Obj_Entry *next;
14434192Sjdp    char *path;			/* Pathname of underlying file (%) */
145116511Smdodd    char *origin_path;		/* Directory path of origin file */
14634192Sjdp    int refcount;
14734192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
14834192Sjdp
14934192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
15034192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
15134192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
15234192Sjdp    size_t textsize;		/* Size of text segment in bytes */
15338467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
15434192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
15538467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
15634192Sjdp    caddr_t entry;		/* Entry point */
15738467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
15834192Sjdp    size_t phsize;		/* Size of program header in bytes */
15950610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
16034192Sjdp
161133063Sdfr    /* TLS information */
162133063Sdfr    int tlsindex;		/* Index in DTV for this module */
163133063Sdfr    void *tlsinit;		/* Base address of TLS init block */
164133063Sdfr    size_t tlsinitsize;		/* Size of TLS init block for this module */
165133063Sdfr    size_t tlssize;		/* Size of TLS block for this module */
166133063Sdfr    size_t tlsoffset;		/* Offset of static TLS block for this module */
167133063Sdfr    size_t tlsalign;		/* Alignment of static TLS block */
168133063Sdfr
16934192Sjdp    /* Items from the dynamic section. */
17045501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
17138467Sjb    const Elf_Rel *rel;		/* Relocation entries */
17234192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
17338816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
17438816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
17538467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
17634192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
17738816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
17838816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
17938467Sjb    const Elf_Sym *symtab;	/* Symbol table */
18034192Sjdp    const char *strtab;		/* String table */
18134192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
182177924Simp#ifdef __mips__
183177924Simp    Elf_Word local_gotno;	/* Number of local GOT entries */
184177924Simp    Elf_Word symtabno;		/* Number of dynamic symbols */
185177924Simp    Elf_Word gotsym;		/* First dynamic symbol in GOT */
186177924Simp#endif
18734192Sjdp
188153515Skan    const Elf_Verneed *verneed; /* Required versions. */
189153515Skan    Elf_Word verneednum;	/* Number of entries in verneed table */
190153515Skan    const Elf_Verdef  *verdef;	/* Provided versions. */
191153515Skan    Elf_Word verdefnum;		/* Number of entries in verdef table */
192153515Skan    const Elf_Versym *versyms;  /* Symbol versions table */
193153515Skan
19485004Sdfr    const Elf_Hashelt *buckets;	/* Hash table buckets array */
19534192Sjdp    unsigned long nbuckets;	/* Number of buckets */
19685004Sdfr    const Elf_Hashelt *chains;	/* Hash table chain array */
19734192Sjdp    unsigned long nchains;	/* Number of chains */
19834192Sjdp
199189959Skib    char *rpath;		/* Search path specified in object */
20034192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
201216695Skib    Needed_Entry *needed_filtees;
202216695Skib    Needed_Entry *needed_aux_filtees;
20334192Sjdp
204153515Skan    STAILQ_HEAD(, Struct_Name_Entry) names; /* List of names for this object we
205153515Skan					       know about. */
206153515Skan    Ver_Entry *vertab;		/* Versions required /defined by this object */
207153515Skan    int vernum;			/* Number of entries in vertab */
208153515Skan
20985677Speter    Elf_Addr init;		/* Initialization function to call */
21085677Speter    Elf_Addr fini;		/* Termination function to call */
21134192Sjdp
212168312Skan    bool mainprog : 1;		/* True if this is the main program */
213168312Skan    bool rtld : 1;		/* True if this is the dynamic linker */
214168312Skan    bool textrel : 1;		/* True if there are relocations to text seg */
215168312Skan    bool symbolic : 1;		/* True if generated with "-Bsymbolic" */
216168312Skan    bool bind_now : 1;		/* True if all relocations should be made first */
217168312Skan    bool traced : 1;		/* Already printed in ldd trace output */
218168312Skan    bool jmpslots_done : 1;	/* Already have relocated the jump slots */
219168312Skan    bool init_done : 1;		/* Already have added object to init list */
220168312Skan    bool tls_done : 1;		/* Already allocated offset for static TLS */
221168312Skan    bool phdr_alloc : 1;	/* Phdr is allocated and needs to be freed. */
222189959Skib    bool z_origin : 1;		/* Process rpath and soname tokens */
223190543Skib    bool z_nodelete : 1;	/* Do not unload the object and dependencies */
224199829Skib    bool z_noopen : 1;		/* Do not load on dlopen */
225216695Skib    bool z_loadfltr : 1;	/* Immediately load filtees */
226194531Skan    bool ref_nodel : 1;		/* Refcount increased to prevent dlclose */
227194531Skan    bool init_scanned: 1;	/* Object is already on init list. */
228194531Skan    bool on_fini_list: 1;	/* Object is already on fini list. */
229214728Skib    bool dag_inited : 1;	/* Object has its DAG initialized. */
230216695Skib    bool filtees_loaded : 1;	/* Filtees loaded */
23135529Sdfr
232194531Skan    struct link_map linkmap;	/* For GDB and dlinfo() */
23350977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
23450977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
23550977Sjdp    dev_t dev;			/* Object's filesystem's device */
23650977Sjdp    ino_t ino;			/* Object's inode number */
23785004Sdfr    void *priv;			/* Platform-dependant */
23834192Sjdp} Obj_Entry;
23934192Sjdp
24034192Sjdp#define RTLD_MAGIC	0xd550b87a
24134192Sjdp#define RTLD_VERSION	1
24234192Sjdp
243192922Sdfr#define RTLD_STATIC_TLS_EXTRA	128
244133063Sdfr
245153515Skan/* Flags to be passed into symlook_ family of functions. */
246153515Skan#define SYMLOOK_IN_PLT	0x01	/* Lookup for PLT symbol */
247153515Skan#define SYMLOOK_DLSYM	0x02	/* Return newes versioned symbol. Used by
248153515Skan				   dlsym. */
249153515Skan
250199829Skib/* Flags for load_object(). */
251199877Skib#define	RTLD_LO_NOLOAD	0x01	/* dlopen() specified RTLD_NOLOAD. */
252199877Skib#define	RTLD_LO_DLOPEN	0x02	/* Load_object() called from dlopen(). */
253199877Skib#define	RTLD_LO_TRACE	0x04	/* Only tracing. */
254216695Skib#define	RTLD_LO_NODELETE 0x08	/* Loaded object cannot be closed. */
255216695Skib#define	RTLD_LO_FILTEES 0x10	/* Loading filtee. */
256199829Skib
25776296Sjdp/*
25876296Sjdp * Symbol cache entry used during relocation to avoid multiple lookups
25976296Sjdp * of the same symbol.
26076296Sjdp */
26176296Sjdptypedef struct Struct_SymCache {
26276296Sjdp    const Elf_Sym *sym;		/* Symbol table entry */
26376296Sjdp    const Obj_Entry *obj;	/* Shared object which defines it */
26476296Sjdp} SymCache;
26576296Sjdp
266216695Skib/*
267216695Skib * This structure provides a reentrant way to keep a list of objects and
268216695Skib * check which ones have already been processed in some way.
269216695Skib */
270216695Skibtypedef struct Struct_DoneList {
271216695Skib    const Obj_Entry **objs;		/* Array of object pointers */
272216695Skib    unsigned int num_alloc;		/* Allocated size of the array */
273216695Skib    unsigned int num_used;		/* Number of array slots used */
274216695Skib} DoneList;
275216695Skib
276216695Skibstruct Struct_RtldLockState {
277216695Skib	int lockstate;
278216695Skib	jmp_buf env;
279216695Skib};
280216695Skib
281216695Skib/*
282216695Skib * The pack of arguments and results for the symbol lookup functions.
283216695Skib */
284216695Skibtypedef struct Struct_SymLook {
285216695Skib    const char *name;
286216695Skib    unsigned long hash;
287216695Skib    const Ver_Entry *ventry;
288216695Skib    int flags;
289216695Skib    const Obj_Entry *defobj_out;
290216695Skib    const Elf_Sym *sym_out;
291216695Skib    struct Struct_RtldLockState *lockstate;
292216695Skib} SymLook;
293216695Skib
29448871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
29550609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
29634192Sjdpextern void *xcalloc(size_t);
29734192Sjdpextern void *xmalloc(size_t);
29834192Sjdpextern char *xstrdup(const char *);
29938816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
300212497Snwhitehornextern Elf_Sym sym_zero;	/* For resolving undefined weak refs. */
30134192Sjdp
302116563Smdoddextern void dump_relocations (Obj_Entry *);
303116563Smdoddextern void dump_obj_relocations (Obj_Entry *);
304116563Smdoddextern void dump_Elf_Rel (Obj_Entry *, const Elf_Rel *, u_long);
305116563Smdoddextern void dump_Elf_Rela (Obj_Entry *, const Elf_Rela *, u_long);
306116563Smdodd
30738816Sdfr/*
30838816Sdfr * Function declarations.
30938816Sdfr */
31038816Sdfrunsigned long elf_hash(const char *);
31166056Sjdpconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
312216695Skib  const Obj_Entry **, int, SymCache *, struct Struct_RtldLockState *);
31345501Sjdpvoid init_pltgot(Obj_Entry *);
314116557Smdoddvoid lockdflt_init(void);
31550608Sjdpvoid obj_free(Obj_Entry *);
31650608SjdpObj_Entry *obj_new(void);
31745501Sjdpvoid _rtld_bind_start(void);
318216695Skibvoid symlook_init(SymLook *, const char *);
319216695Skibint symlook_obj(SymLook *, const Obj_Entry *);
320133063Sdfrvoid *tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset);
321133063Sdfrvoid *allocate_tls(Obj_Entry *, void *, size_t, size_t);
322133063Sdfrvoid free_tls(void *, size_t, size_t);
323133063Sdfrvoid *allocate_module_tls(int index);
324133063Sdfrbool allocate_tls_offset(Obj_Entry *obj);
325142645Sdfrvoid free_tls_offset(Obj_Entry *obj);
326153515Skanconst Ver_Entry *fetch_ventry(const Obj_Entry *obj, unsigned long);
32738816Sdfr
328116558Smdodd/*
329116558Smdodd * MD function declarations.
330116558Smdodd */
331116558Smdoddint do_copy_relocations(Obj_Entry *);
332216695Skibint reloc_non_plt(Obj_Entry *, Obj_Entry *, struct Struct_RtldLockState *);
333116558Smdoddint reloc_plt(Obj_Entry *);
334216695Skibint reloc_jmpslots(Obj_Entry *, struct Struct_RtldLockState *);
335133063Sdfrvoid allocate_initial_tls(Obj_Entry *);
336116558Smdodd
33734192Sjdp#endif /* } */
338