rtld.h revision 50610
134192Sjdp/*-
234192Sjdp * Copyright 1996-1998 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 50610 1999-08-30 01:54:13Z 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
5550608Sjdptypedef struct Struct_Objlist_Entry {
5650608Sjdp    STAILQ_ENTRY(Struct_Objlist_Entry) link;
5750608Sjdp    struct Struct_Obj_Entry *obj;
5850608Sjdp} Objlist_Entry;
5950608Sjdp
6050608Sjdptypedef STAILQ_HEAD(Struct_Objlist, Struct_Objlist_Entry) Objlist;
6150608Sjdp
6234192Sjdptypedef struct Struct_Needed_Entry {
6334192Sjdp    struct Struct_Needed_Entry *next;
6434192Sjdp    struct Struct_Obj_Entry *obj;
6534192Sjdp    unsigned long name;		/* Offset of name in string table */
6634192Sjdp} Needed_Entry;
6734192Sjdp
6834192Sjdp/*
6934192Sjdp * Shared object descriptor.
7034192Sjdp *
7134192Sjdp * Items marked with "(%)" are dynamically allocated, and must be freed
7234192Sjdp * when the structure is destroyed.
7334192Sjdp */
7434192Sjdptypedef struct Struct_Obj_Entry {
7534192Sjdp    /*
7634192Sjdp     * These two items have to be set right for compatibility with the
7734192Sjdp     * original ElfKit crt1.o.
7834192Sjdp     */
7938467Sjb    Elf_Word magic;		/* Magic number (sanity check) */
8038467Sjb    Elf_Word version;		/* Version number of struct format */
8134192Sjdp
8234192Sjdp    struct Struct_Obj_Entry *next;
8350608Sjdp    Objlist dldags;		/* Object belongs to these dlopened DAGs (%) */
8450608Sjdp    Objlist dagmembers;		/* DAG has these members (%) */
8534192Sjdp    char *path;			/* Pathname of underlying file (%) */
8650609Sjdp    dev_t dev;			/* Object's filesystem's device */
8750609Sjdp    ino_t ino;			/* Object's inode number */
8850608Sjdp    unsigned long mark;		/* Set to "curmark" to avoid repeat visits */
8934192Sjdp    int refcount;
9034192Sjdp    int dl_refcount;		/* Number of times loaded by dlopen */
9134192Sjdp
9234192Sjdp    /* These items are computed by map_object() or by digest_phdr(). */
9334192Sjdp    caddr_t mapbase;		/* Base address of mapped region */
9434192Sjdp    size_t mapsize;		/* Size of mapped region in bytes */
9534192Sjdp    size_t textsize;		/* Size of text segment in bytes */
9638467Sjb    Elf_Addr vaddrbase;		/* Base address in shared object file */
9734192Sjdp    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
9838467Sjb    const Elf_Dyn *dynamic;	/* Dynamic section */
9934192Sjdp    caddr_t entry;		/* Entry point */
10038467Sjb    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
10134192Sjdp    size_t phsize;		/* Size of program header in bytes */
10250610Sjdp    const char *interp;		/* Pathname of the interpreter, if any */
10334192Sjdp
10434192Sjdp    /* Items from the dynamic section. */
10545501Sjdp    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
10638467Sjb    const Elf_Rel *rel;		/* Relocation entries */
10734192Sjdp    unsigned long relsize;	/* Size in bytes of relocation info */
10838816Sdfr    const Elf_Rela *rela;	/* Relocation entries with addend */
10938816Sdfr    unsigned long relasize;	/* Size in bytes of addend relocation info */
11038467Sjb    const Elf_Rel *pltrel;	/* PLT relocation entries */
11134192Sjdp    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
11238816Sdfr    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
11338816Sdfr    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
11438467Sjb    const Elf_Sym *symtab;	/* Symbol table */
11534192Sjdp    const char *strtab;		/* String table */
11634192Sjdp    unsigned long strsize;	/* Size in bytes of string table */
11734192Sjdp
11838816Sdfr    const Elf_Addr *buckets;	/* Hash table buckets array */
11934192Sjdp    unsigned long nbuckets;	/* Number of buckets */
12038816Sdfr    const Elf_Addr *chains;	/* Hash table chain array */
12134192Sjdp    unsigned long nchains;	/* Number of chains */
12234192Sjdp
12334192Sjdp    const char *rpath;		/* Search path specified in object */
12434192Sjdp    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
12534192Sjdp
12634192Sjdp    void (*init)(void);		/* Initialization function to call */
12734192Sjdp    void (*fini)(void);		/* Termination function to call */
12834192Sjdp
12934192Sjdp    bool mainprog;		/* True if this is the main program */
13034192Sjdp    bool rtld;			/* True if this is the dynamic linker */
13134192Sjdp    bool textrel;		/* True if there are relocations to text seg */
13234192Sjdp    bool symbolic;		/* True if generated with "-Bsymbolic" */
13338740Sjdp    bool traced;		/* Already printed in ldd trace output */
13435529Sdfr
13535529Sdfr    struct link_map linkmap;	/* for GDB */
13634192Sjdp} Obj_Entry;
13734192Sjdp
13834192Sjdp#define RTLD_MAGIC	0xd550b87a
13934192Sjdp#define RTLD_VERSION	1
14034192Sjdp
14148871Sjdpextern void _rtld_error(const char *, ...) __printflike(1, 2);
14250609Sjdpextern Obj_Entry *map_object(int, const char *, const struct stat *);
14334192Sjdpextern void *xcalloc(size_t);
14434192Sjdpextern void *xmalloc(size_t);
14534192Sjdpextern char *xstrdup(const char *);
14638816Sdfrextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
14734192Sjdp
14838816Sdfr/*
14938816Sdfr * Function declarations.
15038816Sdfr */
15138816Sdfrint do_copy_relocations(Obj_Entry *);
15238816Sdfrunsigned long elf_hash(const char *);
15350608Sjdpconst Elf_Sym *find_symdef(unsigned long, Obj_Entry *, const Obj_Entry **,
15450608Sjdp  bool);
15545501Sjdpvoid init_pltgot(Obj_Entry *);
15650608Sjdpvoid obj_free(Obj_Entry *);
15750608SjdpObj_Entry *obj_new(void);
15845501Sjdpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
15945501Sjdpint reloc_plt(Obj_Entry *, bool);
16045501Sjdpvoid _rtld_bind_start(void);
16138816Sdfrconst Elf_Sym *symlook_obj(const char *, unsigned long,
16238816Sdfr  const Obj_Entry *, bool);
16338816Sdfr
16434192Sjdp#endif /* } */
165