rtld.h revision 116511
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 116511 2003-06-18 03:34:29Z mdodd $
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
4234192Sjdp#ifndef STANDARD_LIBRARY_PATH
43110801Skan#define STANDARD_LIBRARY_PATH	"/usr/lib"
4434192Sjdp#endif
4534192Sjdp
4634192Sjdp#define NEW(type)	((type *) xmalloc(sizeof(type)))
4734192Sjdp#define CNEW(type)	((type *) xcalloc(sizeof(type)))
4834192Sjdp
4934192Sjdp/* We might as well do booleans like C++. */
5034192Sjdptypedef unsigned char bool;
5134192Sjdp#define false	0
5234192Sjdp#define true	1
5334192Sjdp
5450609Sjdpstruct stat;
5534192Sjdpstruct Struct_Obj_Entry;
5634192Sjdp
5755687Sjdp/* Lists of shared objects */
5850608Sjdptypedef struct Struct_Objlist_Entry {
5960938Sjake    STAILQ_ENTRY(Struct_Objlist_Entry) link;
6050608Sjdp    struct Struct_Obj_Entry *obj;
6150608Sjdp} Objlist_Entry;
6250608Sjdp
6360938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
6450608Sjdp
6563870Sjdp/* Types of init and fini functions */
6655687Sjdptypedef void (*InitFunc)(void);
6755687Sjdp
6855687Sjdp/* Lists of shared object dependencies */
6934192Sjdptypedef struct Struct_Needed_Entry {
7034192Sjdp    struct Struct_Needed_Entry *next;
7134192Sjdp    struct Struct_Obj_Entry *obj;
7234192Sjdp    unsigned long name;		/* Offset of name in string table */
7334192Sjdp} Needed_Entry;
7434192Sjdp
7562801Sjdp/* Lock object */
7662801Sjdptypedef struct Struct_LockInfo {
7762801Sjdp    void *context;		/* Client context for creating locks */
7862801Sjdp    void *thelock;		/* The one big lock */
7962801Sjdp    /* Debugging aids. */
8062801Sjdp    volatile int rcount;	/* Number of readers holding lock */
8162801Sjdp    volatile int wcount;	/* Number of writers holding lock */
8262801Sjdp    /* Methods */
8362801Sjdp    void *(*lock_create)(void *context);
8462801Sjdp    void (*rlock_acquire)(void *lock);
8562801Sjdp    void (*wlock_acquire)(void *lock);
8662801Sjdp    void (*rlock_release)(void *lock);
8762801Sjdp    void (*wlock_release)(void *lock);
8862801Sjdp    void (*lock_destroy)(void *lock);
8962801Sjdp    void (*context_destroy)(void *context);
9062801Sjdp} LockInfo;
9162801Sjdp
9234192Sjdp/*
9334192Sjdp * Shared object descriptor.
9434192Sjdp *
9534192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
9634192Sjdp * when the structure is destroyed.
9750977Sjdp *
9850977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
9950977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
10050977Sjdp * near the front, until this can be straightened out.
10134192Sjdp */
10234192Sjdptypedef struct Struct_Obj_Entry {
10334192Sjdp    /*
10434192Sjdp     * These two items have to be set right for compatibility with the
10534192Sjdp     * original ElfKit crt1.o.
10634192Sjdp     */
10738467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
10838467Sjb    Elf_Word version;		/* Version number of struct format */
10934192Sjdp
11034192Sjdp    struct Struct_Obj_Entry *next;
11134192Sjdp    char *path;			/* Pathname of underlying file (%) */
112116511Smdodd    char *origin_path;		/* Directory path of origin file */
11334192Sjdp    int refcount;
11434192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
11534192Sjdp
11634192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
11734192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
11834192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
11934192Sjdp    size_t textsize;		/* Size of text segment in bytes */
12038467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
12134192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
12238467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
12334192Sjdp    caddr_t entry;		/* Entry point */
12438467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
12534192Sjdp    size_t phsize;		/* Size of program header in bytes */
12650610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
12734192Sjdp
12834192Sjdp    /* Items from the dynamic section. */
12945501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
13038467Sjb    const Elf_Rel *rel;		/* Relocation entries */
13134192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
13238816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
13338816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
13438467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
13534192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
13638816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
13738816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
13838467Sjb    const Elf_Sym *symtab;	/* Symbol table */
13934192Sjdp    const char *strtab;		/* String table */
14034192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
14134192Sjdp
14285004Sdfr    const Elf_Hashelt *buckets;	/* Hash table buckets array */
14334192Sjdp    unsigned long nbuckets;	/* Number of buckets */
14485004Sdfr    const Elf_Hashelt *chains;	/* Hash table chain array */
14534192Sjdp    unsigned long nchains;	/* Number of chains */
14634192Sjdp
14734192Sjdp    const char *rpath;		/* Search path specified in object */
14834192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
14934192Sjdp
15085677Speter    Elf_Addr init;		/* Initialization function to call */
15185677Speter    Elf_Addr fini;		/* Termination function to call */
15234192Sjdp
15334192Sjdp    bool mainprog;		/* True if this is the main program */
15434192Sjdp    bool rtld;			/* True if this is the dynamic linker */
15534192Sjdp    bool textrel;		/* True if there are relocations to text seg */
15634192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
157116511Smdodd    bool bind_now;		/* True if all relocations should be made first */
15838740Sjdp    bool traced;		/* Already printed in ldd trace output */
15956780Sjdp    bool jmpslots_done;		/* Already have relocated the jump slots */
16063870Sjdp    bool init_done;		/* Already have added object to init list */
16135529Sdfr
162110804Skan    struct link_map linkmap;	/* for GDB and dlinfo() */
16350977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
16450977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
16550977Sjdp    dev_t dev;			/* Object's filesystem's device */
16650977Sjdp    ino_t ino;			/* Object's inode number */
16785004Sdfr    void *priv;			/* Platform-dependant */
16834192Sjdp} Obj_Entry;
16934192Sjdp
17034192Sjdp#define RTLD_MAGIC	0xd550b87a
17134192Sjdp#define RTLD_VERSION	1
17234192Sjdp
17376296Sjdp/*
17476296Sjdp * Symbol cache entry used during relocation to avoid multiple lookups
17576296Sjdp * of the same symbol.
17676296Sjdp */
17776296Sjdptypedef struct Struct_SymCache {
17876296Sjdp    const Elf_Sym *sym;		/* Symbol table entry */
17976296Sjdp    const Obj_Entry *obj;	/* Shared object which defines it */
18076296Sjdp} SymCache;
18176296Sjdp
18248871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
18350609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
18434192Sjdpextern void *xcalloc(size_t);
18534192Sjdpextern void *xmalloc(size_t);
18634192Sjdpextern char *xstrdup(const char *);
18738816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
18834192Sjdp
18938816Sdfr/*
19038816Sdfr * Function declarations.
19138816Sdfr */
19238816Sdfrint do_copy_relocations(Obj_Entry *);
19338816Sdfrunsigned long elf_hash(const char *);
19466056Sjdpconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
19576296Sjdp  const Obj_Entry **, bool, SymCache *);
19645501Sjdpvoid init_pltgot(Obj_Entry *);
197115396Skanvoid lockdflt_init();
19850608Sjdpvoid obj_free(Obj_Entry *);
19950608SjdpObj_Entry *obj_new(void);
20045501Sjdpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
20156780Sjdpint reloc_plt(Obj_Entry *);
20256780Sjdpint reloc_jmpslots(Obj_Entry *);
20345501Sjdpvoid _rtld_bind_start(void);
20438816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
20538816Sdfr  const Obj_Entry *, bool);
20638816Sdfr
20734192Sjdp#endif /* } */
208