rtld.h revision 63870
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 63870 2000-07-26 04:24:40Z jdp $
2634192Sjdp */
2734192Sjdp
2834192Sjdp#ifndef RTLD_H /* { */
2934192Sjdp#define RTLD_H 1
3034192Sjdp
3134192Sjdp#include <sys/types.h>
3250608Sjdp#include <sys/queue.h>
3334192Sjdp
3435529Sdfr#include <link.h>
3534192Sjdp#include <elf.h>
3634192Sjdp#include <stddef.h>
3734192Sjdp
3845501Sjdp#include "rtld_machdep.h"
3945501Sjdp
4034192Sjdp#ifndef STANDARD_LIBRARY_PATH
4134192Sjdp#define STANDARD_LIBRARY_PATH	"/usr/lib/elf:/usr/lib"
4234192Sjdp#endif
4334192Sjdp
4434192Sjdp#define NEW(type)	((type *) xmalloc(sizeof(type)))
4534192Sjdp#define CNEW(type)	((type *) xcalloc(sizeof(type)))
4634192Sjdp
4734192Sjdp/* We might as well do booleans like C++. */
4834192Sjdptypedef unsigned char bool;
4934192Sjdp#define false	0
5034192Sjdp#define true	1
5134192Sjdp
5250609Sjdpstruct stat;
5334192Sjdpstruct Struct_Obj_Entry;
5434192Sjdp
5555687Sjdp/* Lists of shared objects */
5650608Sjdptypedef struct Struct_Objlist_Entry {
5760938Sjake    STAILQ_ENTRY(Struct_Objlist_Entry) link;
5850608Sjdp    struct Struct_Obj_Entry *obj;
5950608Sjdp} Objlist_Entry;
6050608Sjdp
6160938Sjaketypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
6250608Sjdp
6363870Sjdp/* Types of init and fini functions */
6455687Sjdptypedef void (*InitFunc)(void);
6555687Sjdp
6655687Sjdp/* Lists of shared object dependencies */
6734192Sjdptypedef struct Struct_Needed_Entry {
6834192Sjdp    struct Struct_Needed_Entry *next;
6934192Sjdp    struct Struct_Obj_Entry *obj;
7034192Sjdp    unsigned long name;		/* Offset of name in string table */
7134192Sjdp} Needed_Entry;
7234192Sjdp
7362801Sjdp/* Lock object */
7462801Sjdptypedef struct Struct_LockInfo {
7562801Sjdp    void *context;		/* Client context for creating locks */
7662801Sjdp    void *thelock;		/* The one big lock */
7762801Sjdp    /* Debugging aids. */
7862801Sjdp    volatile int rcount;	/* Number of readers holding lock */
7962801Sjdp    volatile int wcount;	/* Number of writers holding lock */
8062801Sjdp    /* Methods */
8162801Sjdp    void *(*lock_create)(void *context);
8262801Sjdp    void (*rlock_acquire)(void *lock);
8362801Sjdp    void (*wlock_acquire)(void *lock);
8462801Sjdp    void (*rlock_release)(void *lock);
8562801Sjdp    void (*wlock_release)(void *lock);
8662801Sjdp    void (*lock_destroy)(void *lock);
8762801Sjdp    void (*context_destroy)(void *context);
8862801Sjdp} LockInfo;
8962801Sjdp
9034192Sjdp/*
9134192Sjdp * Shared object descriptor.
9234192Sjdp *
9334192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
9434192Sjdp * when the structure is destroyed.
9550977Sjdp *
9650977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
9750977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
9850977Sjdp * near the front, until this can be straightened out.
9934192Sjdp */
10034192Sjdptypedef struct Struct_Obj_Entry {
10134192Sjdp    /*
10234192Sjdp     * These two items have to be set right for compatibility with the
10334192Sjdp     * original ElfKit crt1.o.
10434192Sjdp     */
10538467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
10638467Sjb    Elf_Word version;		/* Version number of struct format */
10734192Sjdp
10834192Sjdp    struct Struct_Obj_Entry *next;
10934192Sjdp    char *path;			/* Pathname of underlying file (%) */
11034192Sjdp    int refcount;
11134192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
11234192Sjdp
11334192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
11434192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
11534192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
11634192Sjdp    size_t textsize;		/* Size of text segment in bytes */
11738467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
11834192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
11938467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
12034192Sjdp    caddr_t entry;		/* Entry point */
12138467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
12234192Sjdp    size_t phsize;		/* Size of program header in bytes */
12350610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
12434192Sjdp
12534192Sjdp    /* Items from the dynamic section. */
12645501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
12738467Sjb    const Elf_Rel *rel;		/* Relocation entries */
12834192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
12938816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
13038816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
13138467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
13234192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
13338816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
13438816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
13538467Sjb    const Elf_Sym *symtab;	/* Symbol table */
13634192Sjdp    const char *strtab;		/* String table */
13734192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
13834192Sjdp
13938816Sdfr    const Elf_Addr *buckets;	/* Hash table buckets array */
14034192Sjdp    unsigned long nbuckets;	/* Number of buckets */
14138816Sdfr    const Elf_Addr *chains;	/* Hash table chain array */
14234192Sjdp    unsigned long nchains;	/* Number of chains */
14334192Sjdp
14434192Sjdp    const char *rpath;		/* Search path specified in object */
14534192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
14634192Sjdp
14755687Sjdp    InitFunc init;		/* Initialization function to call */
14855687Sjdp    InitFunc fini;		/* Termination function to call */
14934192Sjdp
15034192Sjdp    bool mainprog;		/* True if this is the main program */
15134192Sjdp    bool rtld;			/* True if this is the dynamic linker */
15234192Sjdp    bool textrel;		/* True if there are relocations to text seg */
15334192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
15438740Sjdp    bool traced;		/* Already printed in ldd trace output */
15556780Sjdp    bool jmpslots_done;		/* Already have relocated the jump slots */
15663870Sjdp    bool init_done;		/* Already have added object to init list */
15735529Sdfr
15835529Sdfr    struct link_map linkmap;	/* for GDB */
15950977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
16050977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
16150977Sjdp    dev_t dev;			/* Object's filesystem's device */
16250977Sjdp    ino_t ino;			/* Object's inode number */
16334192Sjdp} Obj_Entry;
16434192Sjdp
16534192Sjdp#define RTLD_MAGIC	0xd550b87a
16634192Sjdp#define RTLD_VERSION	1
16734192Sjdp
16848871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
16950609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
17034192Sjdpextern void *xcalloc(size_t);
17134192Sjdpextern void *xmalloc(size_t);
17234192Sjdpextern char *xstrdup(const char *);
17338816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
17434192Sjdp
17538816Sdfr/*
17638816Sdfr * Function declarations.
17738816Sdfr */
17838816Sdfrint do_copy_relocations(Obj_Entry *);
17938816Sdfrunsigned long elf_hash(const char *);
18050608Sjdpconst Elf_Sym *find_symdef(unsigned long, Obj_Entry *, const Obj_Entry **,
18150608Sjdp  bool);
18245501Sjdpvoid init_pltgot(Obj_Entry *);
18362801Sjdpvoid lockdflt_init(LockInfo *);
18450608Sjdpvoid obj_free(Obj_Entry *);
18550608SjdpObj_Entry *obj_new(void);
18645501Sjdpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
18756780Sjdpint reloc_plt(Obj_Entry *);
18856780Sjdpint reloc_jmpslots(Obj_Entry *);
18945501Sjdpvoid _rtld_bind_start(void);
19038816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
19138816Sdfr  const Obj_Entry *, bool);
19238816Sdfr
19334192Sjdp#endif /* } */
194