rtld.h revision 60938
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 60938 2000-05-26 02:09:24Z jake $
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
6355687Sjdp/* Lists of init or fini functions */
6455687Sjdptypedef void (*InitFunc)(void);
6555687Sjdp
6655687Sjdptypedef struct Struct_Funclist_Entry {
6760938Sjake    STAILQ_ENTRY(Struct_Funclist_Entry) link;
6855687Sjdp    InitFunc func;
6955687Sjdp} Funclist_Entry;
7055687Sjdp
7160938Sjaketypedef STAILQ_HEAD(Struct_Funclist, Struct_Funclist_Entry) Funclist;
7255687Sjdp
7355687Sjdp/* Lists of shared object dependencies */
7434192Sjdptypedef struct Struct_Needed_Entry {
7534192Sjdp    struct Struct_Needed_Entry *next;
7634192Sjdp    struct Struct_Obj_Entry *obj;
7734192Sjdp    unsigned long name;		/* Offset of name in string table */
7834192Sjdp} Needed_Entry;
7934192Sjdp
8034192Sjdp/*
8134192Sjdp * Shared object descriptor.
8234192Sjdp *
8334192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
8434192Sjdp * when the structure is destroyed.
8550977Sjdp *
8650977Sjdp * CAUTION: It appears that the JDK port peeks into these structures.
8750977Sjdp * It looks at "next" and "mapbase" at least.  Don't add new members
8850977Sjdp * near the front, until this can be straightened out.
8934192Sjdp */
9034192Sjdptypedef struct Struct_Obj_Entry {
9134192Sjdp    /*
9234192Sjdp     * These two items have to be set right for compatibility with the
9334192Sjdp     * original ElfKit crt1.o.
9434192Sjdp     */
9538467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
9638467Sjb    Elf_Word version;		/* Version number of struct format */
9734192Sjdp
9834192Sjdp    struct Struct_Obj_Entry *next;
9934192Sjdp    char *path;			/* Pathname of underlying file (%) */
10034192Sjdp    int refcount;
10134192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
10234192Sjdp
10334192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
10434192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
10534192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
10634192Sjdp    size_t textsize;		/* Size of text segment in bytes */
10738467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
10834192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
10938467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
11034192Sjdp    caddr_t entry;		/* Entry point */
11138467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
11234192Sjdp    size_t phsize;		/* Size of program header in bytes */
11350610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
11434192Sjdp
11534192Sjdp    /* Items from the dynamic section. */
11645501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
11738467Sjb    const Elf_Rel *rel;		/* Relocation entries */
11834192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
11938816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
12038816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
12138467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
12234192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
12338816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
12438816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
12538467Sjb    const Elf_Sym *symtab;	/* Symbol table */
12634192Sjdp    const char *strtab;		/* String table */
12734192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
12834192Sjdp
12938816Sdfr    const Elf_Addr *buckets;	/* Hash table buckets array */
13034192Sjdp    unsigned long nbuckets;	/* Number of buckets */
13138816Sdfr    const Elf_Addr *chains;	/* Hash table chain array */
13234192Sjdp    unsigned long nchains;	/* Number of chains */
13334192Sjdp
13434192Sjdp    const char *rpath;		/* Search path specified in object */
13534192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
13634192Sjdp
13755687Sjdp    InitFunc init;		/* Initialization function to call */
13855687Sjdp    InitFunc fini;		/* Termination function to call */
13934192Sjdp
14034192Sjdp    bool mainprog;		/* True if this is the main program */
14134192Sjdp    bool rtld;			/* True if this is the dynamic linker */
14234192Sjdp    bool textrel;		/* True if there are relocations to text seg */
14334192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
14438740Sjdp    bool traced;		/* Already printed in ldd trace output */
14556780Sjdp    bool jmpslots_done;		/* Already have relocated the jump slots */
14635529Sdfr
14735529Sdfr    struct link_map linkmap;	/* for GDB */
14850977Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
14950977Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
15050977Sjdp    dev_t dev;			/* Object's filesystem's device */
15150977Sjdp    ino_t ino;			/* Object's inode number */
15250977Sjdp    unsigned long mark;		/* Set to "curmark" to avoid repeat visits */
15334192Sjdp} Obj_Entry;
15434192Sjdp
15534192Sjdp#define RTLD_MAGIC	0xd550b87a
15634192Sjdp#define RTLD_VERSION	1
15734192Sjdp
15848871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
15950609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
16034192Sjdpextern void *xcalloc(size_t);
16134192Sjdpextern void *xmalloc(size_t);
16234192Sjdpextern char *xstrdup(const char *);
16338816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
16434192Sjdp
16538816Sdfr/*
16638816Sdfr * Function declarations.
16738816Sdfr */
16838816Sdfrint do_copy_relocations(Obj_Entry *);
16938816Sdfrunsigned long elf_hash(const char *);
17050608Sjdpconst Elf_Sym *find_symdef(unsigned long, Obj_Entry *, const Obj_Entry **,
17150608Sjdp  bool);
17245501Sjdpvoid init_pltgot(Obj_Entry *);
17355122Sjdpvoid lockdflt_acquire(void *);
17455122Sjdpvoid *lockdflt_create(void *);
17555122Sjdpvoid lockdflt_destroy(void *);
17655122Sjdpvoid lockdflt_release(void *);
17750608Sjdpvoid obj_free(Obj_Entry *);
17850608SjdpObj_Entry *obj_new(void);
17945501Sjdpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
18056780Sjdpint reloc_plt(Obj_Entry *);
18156780Sjdpint reloc_jmpslots(Obj_Entry *);
18245501Sjdpvoid _rtld_bind_start(void);
18338816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
18438816Sdfr  const Obj_Entry *, bool);
18538816Sdfr
18634192Sjdp#endif /* } */
187