rtld.h revision 76296
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 76296 2001-05-05 23:21:05Z jdp $
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
3945501Sjdp#include "rtld_machdep.h"
4045501Sjdp
4134192Sjdp#ifndef STANDARD_LIBRARY_PATH
4234192Sjdp#define STANDARD_LIBRARY_PATH	"/usr/lib/elf:/usr/lib"
4334192Sjdp#endif
4434192Sjdp
4534192Sjdp#define NEW(type)	((type *) xmalloc(sizeof(type)))
4634192Sjdp#define CNEW(type)	((type *) xcalloc(sizeof(type)))
4734192Sjdp
4834192Sjdp/* We might as well do booleans like C++. */
4934192Sjdptypedef unsigned char bool;
5034192Sjdp#define false	0
5134192Sjdp#define true	1
5234192Sjdp
5350609Sjdpstruct stat;
5434192Sjdpstruct Struct_Obj_Entry;
5534192Sjdp
5655687Sjdp/* Lists of shared objects */
5750608Sjdptypedef struct Struct_Objlist_Entry {
5860938Sjake    STAILQ_ENTRY(Struct_Objlist_Entry) link;
5950608Sjdp    struct Struct_Obj_Entry *obj;
6050608Sjdp} Objlist_Entry;
6150608Sjdp
6260938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
6350608Sjdp
6463870Sjdp/* Types of init and fini functions */
6555687Sjdptypedef void (*InitFunc)(void);
6655687Sjdp
6755687Sjdp/* Lists of shared object dependencies */
6834192Sjdptypedef struct Struct_Needed_Entry {
6934192Sjdp    struct Struct_Needed_Entry *next;
7034192Sjdp    struct Struct_Obj_Entry *obj;
7134192Sjdp    unsigned long name;		/* Offset of name in string table */
7234192Sjdp} Needed_Entry;
7334192Sjdp
7462801Sjdp/* Lock object */
7562801Sjdptypedef struct Struct_LockInfo {
7662801Sjdp    void *context;		/* Client context for creating locks */
7762801Sjdp    void *thelock;		/* The one big lock */
7862801Sjdp    /* Debugging aids. */
7962801Sjdp    volatile int rcount;	/* Number of readers holding lock */
8062801Sjdp    volatile int wcount;	/* Number of writers holding lock */
8162801Sjdp    /* Methods */
8262801Sjdp    void *(*lock_create)(void *context);
8362801Sjdp    void (*rlock_acquire)(void *lock);
8462801Sjdp    void (*wlock_acquire)(void *lock);
8562801Sjdp    void (*rlock_release)(void *lock);
8662801Sjdp    void (*wlock_release)(void *lock);
8762801Sjdp    void (*lock_destroy)(void *lock);
8862801Sjdp    void (*context_destroy)(void *context);
8962801Sjdp} LockInfo;
9062801Sjdp
9134192Sjdp/*
9234192Sjdp * Shared object descriptor.
9334192Sjdp *
9434192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
9534192Sjdp * when the structure is destroyed.
9650977Sjdp *
9750977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
9850977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
9950977Sjdp * near the front, until this can be straightened out.
10034192Sjdp */
10134192Sjdptypedef struct Struct_Obj_Entry {
10234192Sjdp    /*
10334192Sjdp     * These two items have to be set right for compatibility with the
10434192Sjdp     * original ElfKit crt1.o.
10534192Sjdp     */
10638467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
10738467Sjb    Elf_Word version;		/* Version number of struct format */
10834192Sjdp
10934192Sjdp    struct Struct_Obj_Entry *next;
11034192Sjdp    char *path;			/* Pathname of underlying file (%) */
11134192Sjdp    int refcount;
11234192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
11334192Sjdp
11434192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
11534192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
11634192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
11734192Sjdp    size_t textsize;		/* Size of text segment in bytes */
11838467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
11934192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
12038467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
12134192Sjdp    caddr_t entry;		/* Entry point */
12238467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
12334192Sjdp    size_t phsize;		/* Size of program header in bytes */
12450610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
12534192Sjdp
12634192Sjdp    /* Items from the dynamic section. */
12745501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
12838467Sjb    const Elf_Rel *rel;		/* Relocation entries */
12934192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
13038816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
13138816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
13238467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
13334192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
13438816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
13538816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
13638467Sjb    const Elf_Sym *symtab;	/* Symbol table */
13734192Sjdp    const char *strtab;		/* String table */
13834192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
13934192Sjdp
14038816Sdfr    const Elf_Addr *buckets;	/* Hash table buckets array */
14134192Sjdp    unsigned long nbuckets;	/* Number of buckets */
14238816Sdfr    const Elf_Addr *chains;	/* Hash table chain array */
14334192Sjdp    unsigned long nchains;	/* Number of chains */
14434192Sjdp
14534192Sjdp    const char *rpath;		/* Search path specified in object */
14634192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
14734192Sjdp
14855687Sjdp    InitFunc init;		/* Initialization function to call */
14955687Sjdp    InitFunc fini;		/* Termination function to call */
15034192Sjdp
15134192Sjdp    bool mainprog;		/* True if this is the main program */
15234192Sjdp    bool rtld;			/* True if this is the dynamic linker */
15334192Sjdp    bool textrel;		/* True if there are relocations to text seg */
15434192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
15538740Sjdp    bool traced;		/* Already printed in ldd trace output */
15656780Sjdp    bool jmpslots_done;		/* Already have relocated the jump slots */
15763870Sjdp    bool init_done;		/* Already have added object to init list */
15835529Sdfr
15935529Sdfr    struct link_map linkmap;	/* for GDB */
16050977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
16150977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
16250977Sjdp    dev_t dev;			/* Object's filesystem's device */
16350977Sjdp    ino_t ino;			/* Object's inode number */
16434192Sjdp} Obj_Entry;
16534192Sjdp
16634192Sjdp#define RTLD_MAGIC	0xd550b87a
16734192Sjdp#define RTLD_VERSION	1
16834192Sjdp
16976296Sjdp/*
17076296Sjdp * Symbol cache entry used during relocation to avoid multiple lookups
17176296Sjdp * of the same symbol.
17276296Sjdp */
17376296Sjdptypedef struct Struct_SymCache {
17476296Sjdp    const Elf_Sym *sym;		/* Symbol table entry */
17576296Sjdp    const Obj_Entry *obj;	/* Shared object which defines it */
17676296Sjdp} SymCache;
17776296Sjdp
17848871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
17950609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
18034192Sjdpextern void *xcalloc(size_t);
18134192Sjdpextern void *xmalloc(size_t);
18234192Sjdpextern char *xstrdup(const char *);
18338816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
18434192Sjdp
18538816Sdfr/*
18638816Sdfr * Function declarations.
18738816Sdfr */
18838816Sdfrint do_copy_relocations(Obj_Entry *);
18938816Sdfrunsigned long elf_hash(const char *);
19066056Sjdpconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
19176296Sjdp  const Obj_Entry **, bool, SymCache *);
19245501Sjdpvoid init_pltgot(Obj_Entry *);
19362801Sjdpvoid lockdflt_init(LockInfo *);
19450608Sjdpvoid obj_free(Obj_Entry *);
19550608SjdpObj_Entry *obj_new(void);
19645501Sjdpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
19756780Sjdpint reloc_plt(Obj_Entry *);
19856780Sjdpint reloc_jmpslots(Obj_Entry *);
19945501Sjdpvoid _rtld_bind_start(void);
20038816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
20138816Sdfr  const Obj_Entry *, bool);
20238816Sdfr
20334192Sjdp#endif /* } */
204