rtld.h revision 142645
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 142645 2005-02-27 12:55:40Z dfr $
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>
3734192Sjdp#include <stddef.h>
3834192Sjdp
39115396Skan#include "rtld_lock.h"
4045501Sjdp#include "rtld_machdep.h"
4145501Sjdp
42127250Speter#ifdef COMPAT_32BIT
43127250Speter#undef STANDARD_LIBRARY_PATH
44127250Speter#undef _PATH_ELF_HINTS
45127250Speter#define	_PATH_ELF_HINTS		"/var/run/ld-elf32.so.hints"
46127250Speter/* For running 32 bit binaries  */
47127250Speter#define	STANDARD_LIBRARY_PATH	"/lib32:/usr/lib32"
48127250Speter#define LD_ "LD_32_"
49127250Speter#endif
50127250Speter
5134192Sjdp#ifndef STANDARD_LIBRARY_PATH
52119013Sgordon#define STANDARD_LIBRARY_PATH	"/lib:/usr/lib"
5334192Sjdp#endif
54127250Speter#ifndef LD_
55127250Speter#define LD_ "LD_"
56127250Speter#endif
5734192Sjdp
5834192Sjdp#define NEW(type)	((type *) xmalloc(sizeof(type)))
5934192Sjdp#define CNEW(type)	((type *) xcalloc(sizeof(type)))
6034192Sjdp
6134192Sjdp/* We might as well do booleans like C++. */
6234192Sjdptypedef unsigned char bool;
6334192Sjdp#define false	0
6434192Sjdp#define true	1
6534192Sjdp
66133063Sdfrextern size_t tls_last_offset;
67133063Sdfrextern size_t tls_last_size;
68133063Sdfrextern size_t tls_static_space;
69133063Sdfrextern int tls_dtv_generation;
70133063Sdfrextern int tls_max_index;
71133063Sdfr
7250609Sjdpstruct stat;
7334192Sjdpstruct Struct_Obj_Entry;
7434192Sjdp
7555687Sjdp/* Lists of shared objects */
7650608Sjdptypedef struct Struct_Objlist_Entry {
7760938Sjake    STAILQ_ENTRY(Struct_Objlist_Entry) link;
7850608Sjdp    struct Struct_Obj_Entry *obj;
7950608Sjdp} Objlist_Entry;
8050608Sjdp
8160938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
8250608Sjdp
8363870Sjdp/* Types of init and fini functions */
8455687Sjdptypedef void (*InitFunc)(void);
8555687Sjdp
8655687Sjdp/* Lists of shared object dependencies */
8734192Sjdptypedef struct Struct_Needed_Entry {
8834192Sjdp    struct Struct_Needed_Entry *next;
8934192Sjdp    struct Struct_Obj_Entry *obj;
9034192Sjdp    unsigned long name;		/* Offset of name in string table */
9134192Sjdp} Needed_Entry;
9234192Sjdp
9362801Sjdp/* Lock object */
9462801Sjdptypedef struct Struct_LockInfo {
9562801Sjdp    void *context;		/* Client context for creating locks */
9662801Sjdp    void *thelock;		/* The one big lock */
9762801Sjdp    /* Debugging aids. */
9862801Sjdp    volatile int rcount;	/* Number of readers holding lock */
9962801Sjdp    volatile int wcount;	/* Number of writers holding lock */
10062801Sjdp    /* Methods */
10162801Sjdp    void *(*lock_create)(void *context);
10262801Sjdp    void (*rlock_acquire)(void *lock);
10362801Sjdp    void (*wlock_acquire)(void *lock);
10462801Sjdp    void (*rlock_release)(void *lock);
10562801Sjdp    void (*wlock_release)(void *lock);
10662801Sjdp    void (*lock_destroy)(void *lock);
10762801Sjdp    void (*context_destroy)(void *context);
10862801Sjdp} LockInfo;
10962801Sjdp
11034192Sjdp/*
11134192Sjdp * Shared object descriptor.
11234192Sjdp *
11334192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
11434192Sjdp * when the structure is destroyed.
11550977Sjdp *
11650977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
11750977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
11850977Sjdp * near the front, until this can be straightened out.
11934192Sjdp */
12034192Sjdptypedef struct Struct_Obj_Entry {
12134192Sjdp    /*
12234192Sjdp     * These two items have to be set right for compatibility with the
12334192Sjdp     * original ElfKit crt1.o.
12434192Sjdp     */
12538467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
12638467Sjb    Elf_Word version;		/* Version number of struct format */
12734192Sjdp
12834192Sjdp    struct Struct_Obj_Entry *next;
12934192Sjdp    char *path;			/* Pathname of underlying file (%) */
130116511Smdodd    char *origin_path;		/* Directory path of origin file */
13134192Sjdp    int refcount;
13234192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
13334192Sjdp
13434192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
13534192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
13634192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
13734192Sjdp    size_t textsize;		/* Size of text segment in bytes */
13838467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
13934192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
14038467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
14134192Sjdp    caddr_t entry;		/* Entry point */
14238467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
14334192Sjdp    size_t phsize;		/* Size of program header in bytes */
14450610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
14534192Sjdp
146133063Sdfr    /* TLS information */
147133063Sdfr    int tlsindex;		/* Index in DTV for this module */
148133063Sdfr    void *tlsinit;		/* Base address of TLS init block */
149133063Sdfr    size_t tlsinitsize;		/* Size of TLS init block for this module */
150133063Sdfr    size_t tlssize;		/* Size of TLS block for this module */
151133063Sdfr    size_t tlsoffset;		/* Offset of static TLS block for this module */
152133063Sdfr    size_t tlsalign;		/* Alignment of static TLS block */
153133063Sdfr
15434192Sjdp    /* Items from the dynamic section. */
15545501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
15638467Sjb    const Elf_Rel *rel;		/* Relocation entries */
15734192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
15838816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
15938816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
16038467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
16134192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
16238816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
16338816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
16438467Sjb    const Elf_Sym *symtab;	/* Symbol table */
16534192Sjdp    const char *strtab;		/* String table */
16634192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
16734192Sjdp
16885004Sdfr    const Elf_Hashelt *buckets;	/* Hash table buckets array */
16934192Sjdp    unsigned long nbuckets;	/* Number of buckets */
17085004Sdfr    const Elf_Hashelt *chains;	/* Hash table chain array */
17134192Sjdp    unsigned long nchains;	/* Number of chains */
17234192Sjdp
17334192Sjdp    const char *rpath;		/* Search path specified in object */
17434192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
17534192Sjdp
17685677Speter    Elf_Addr init;		/* Initialization function to call */
17785677Speter    Elf_Addr fini;		/* Termination function to call */
17834192Sjdp
17934192Sjdp    bool mainprog;		/* True if this is the main program */
18034192Sjdp    bool rtld;			/* True if this is the dynamic linker */
18134192Sjdp    bool textrel;		/* True if there are relocations to text seg */
18234192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
183116511Smdodd    bool bind_now;		/* True if all relocations should be made first */
18438740Sjdp    bool traced;		/* Already printed in ldd trace output */
18556780Sjdp    bool jmpslots_done;		/* Already have relocated the jump slots */
18663870Sjdp    bool init_done;		/* Already have added object to init list */
187133063Sdfr    bool tls_done;		/* Already allocated offset for static TLS */
18835529Sdfr
189110804Skan    struct link_map linkmap;	/* for GDB and dlinfo() */
19050977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
19150977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
19250977Sjdp    dev_t dev;			/* Object's filesystem's device */
19350977Sjdp    ino_t ino;			/* Object's inode number */
19485004Sdfr    void *priv;			/* Platform-dependant */
19534192Sjdp} Obj_Entry;
19634192Sjdp
19734192Sjdp#define RTLD_MAGIC	0xd550b87a
19834192Sjdp#define RTLD_VERSION	1
19934192Sjdp
200133063Sdfr#define RTLD_STATIC_TLS_EXTRA	64
201133063Sdfr
20276296Sjdp/*
20376296Sjdp * Symbol cache entry used during relocation to avoid multiple lookups
20476296Sjdp * of the same symbol.
20576296Sjdp */
20676296Sjdptypedef struct Struct_SymCache {
20776296Sjdp    const Elf_Sym *sym;		/* Symbol table entry */
20876296Sjdp    const Obj_Entry *obj;	/* Shared object which defines it */
20976296Sjdp} SymCache;
21076296Sjdp
21148871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
21250609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
21334192Sjdpextern void *xcalloc(size_t);
21434192Sjdpextern void *xmalloc(size_t);
21534192Sjdpextern char *xstrdup(const char *);
21638816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
21734192Sjdp
218116563Smdoddextern void dump_relocations (Obj_Entry *);
219116563Smdoddextern void dump_obj_relocations (Obj_Entry *);
220116563Smdoddextern void dump_Elf_Rel (Obj_Entry *, const Elf_Rel *, u_long);
221116563Smdoddextern void dump_Elf_Rela (Obj_Entry *, const Elf_Rela *, u_long);
222116563Smdodd
22338816Sdfr/*
22438816Sdfr * Function declarations.
22538816Sdfr */
22638816Sdfrunsigned long elf_hash(const char *);
22766056Sjdpconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
22876296Sjdp  const Obj_Entry **, bool, SymCache *);
22945501Sjdpvoid init_pltgot(Obj_Entry *);
230116557Smdoddvoid lockdflt_init(void);
23150608Sjdpvoid obj_free(Obj_Entry *);
23250608SjdpObj_Entry *obj_new(void);
23345501Sjdpvoid _rtld_bind_start(void);
23438816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
23538816Sdfr  const Obj_Entry *, bool);
236133063Sdfrvoid *tls_get_addr_common(Elf_Addr** dtvp, int index, size_t offset);
237133063Sdfrvoid *allocate_tls(Obj_Entry *, void *, size_t, size_t);
238133063Sdfrvoid free_tls(void *, size_t, size_t);
239133063Sdfrvoid *allocate_module_tls(int index);
240133063Sdfrbool allocate_tls_offset(Obj_Entry *obj);
241142645Sdfrvoid free_tls_offset(Obj_Entry *obj);
24238816Sdfr
243116558Smdodd/*
244116558Smdodd * MD function declarations.
245116558Smdodd */
246116558Smdoddint do_copy_relocations(Obj_Entry *);
247116558Smdoddint reloc_non_plt(Obj_Entry *, Obj_Entry *);
248116558Smdoddint reloc_plt(Obj_Entry *);
249116558Smdoddint reloc_jmpslots(Obj_Entry *);
250133063Sdfrvoid allocate_initial_tls(Obj_Entry *);
251116558Smdodd
25234192Sjdp#endif /* } */
253