rtld.h revision 45501
185909Simp/*-
285909Simp * Copyright 1996-1998 John D. Polstra.
3122116Sbde * All rights reserved.
4122116Sbde *
5122116Sbde * Redistribution and use in source and binary forms, with or without
685909Simp * modification, are permitted provided that the following conditions
785909Simp * are met:
885909Simp * 1. Redistributions of source code must retain the above copyright
985909Simp *    notice, this list of conditions and the following disclaimer.
1085909Simp * 2. Redistributions in binary form must reproduce the above copyright
1185909Simp *    notice, this list of conditions and the following disclaimer in the
1285909Simp *    documentation and/or other materials provided with the distribution.
1391512Sobrien *
14116341Smarkm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1585909Simp * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1685909Simp * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1785909Simp * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1885909Simp * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19126890Strhodes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20126890Strhodes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21126890Strhodes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22127543Skensmith * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23127204Sobrien * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24127259Smarcel *
25127259Smarcel *      $Id: rtld.h,v 1.5 1998/09/04 19:03:57 dfr Exp $
26127543Skensmith */
27127543Skensmith
28127204Sobrien#ifndef RTLD_H /* { */
2985909Simp#define RTLD_H 1
30127204Sobrien
31126890Strhodes#include <sys/types.h>
32124834Sru
33126890Strhodes#include <link.h>
34124834Sru#include <elf.h>
3585909Simp#include <stddef.h>
36126890Strhodes
37126890Strhodes#include "rtld_machdep.h"
38126890Strhodes
3985909Simp#ifndef STANDARD_LIBRARY_PATH
40126890Strhodes#define STANDARD_LIBRARY_PATH	"/usr/lib/elf:/usr/lib"
4185909Simp#endif
42126890Strhodes
43126890Strhodes#define NEW(type)	((type *) xmalloc(sizeof(type)))
44126890Strhodes#define CNEW(type)	((type *) xcalloc(sizeof(type)))
45126890Strhodes
46126890Strhodes/* We might as well do booleans like C++. */
4785909Simptypedef unsigned char bool;
48126890Strhodes#define false	0
49126890Strhodes#define true	1
50126938Strhodes
5185909Simpstruct Struct_Obj_Entry;
5285909Simp
5385909Simptypedef struct Struct_Needed_Entry {
54130416Smlaier    struct Struct_Needed_Entry *next;
55130416Smlaier    struct Struct_Obj_Entry *obj;
56130416Smlaier    unsigned long name;		/* Offset of name in string table */
5785909Simp} Needed_Entry;
5885909Simp
5985909Simp/*
60126263Smlaier * Shared object descriptor.
61126263Smlaier *
62126263Smlaier * Items marked with "(%)" are dynamically allocated, and must be freed
63116817Ssam * when the structure is destroyed.
64116817Ssam */
65116817Ssamtypedef struct Struct_Obj_Entry {
66121613Sharti    /*
67121613Sharti     * These two items have to be set right for compatibility with the
68121613Sharti     * original ElfKit crt1.o.
6999932Sbde     */
70122116Sbde    Elf_Word magic;		/* Magic number (sanity check) */
71126890Strhodes    Elf_Word version;		/* Version number of struct format */
72124834Sru
73122116Sbde    struct Struct_Obj_Entry *next;
74126890Strhodes    char *path;			/* Pathname of underlying file (%) */
7585909Simp    int refcount;
7685909Simp    int dl_refcount;		/* Number of times loaded by dlopen */
7785909Simp
7885909Simp    /* These items are computed by map_object() or by digest_phdr(). */
7999923Sbde    caddr_t mapbase;		/* Base address of mapped region */
80126938Strhodes    size_t mapsize;		/* Size of mapped region in bytes */
81126938Strhodes    size_t textsize;		/* Size of text segment in bytes */
82126938Strhodes    Elf_Addr vaddrbase;		/* Base address in shared object file */
8399932Sbde    caddr_t relocbase;		/* Relocation constant = mapbase - vaddrbase */
84126938Strhodes    const Elf_Dyn *dynamic;	/* Dynamic section */
8599932Sbde    caddr_t entry;		/* Entry point */
8699932Sbde    const Elf_Phdr *phdr;	/* Program header if it is mapped, else NULL */
87126890Strhodes    size_t phsize;		/* Size of program header in bytes */
88126890Strhodes
89126890Strhodes    /* Items from the dynamic section. */
90126890Strhodes    Elf_Addr *pltgot;		/* PLT or GOT, depending on architecture */
91123989Sbde    const Elf_Rel *rel;		/* Relocation entries */
92126890Strhodes    unsigned long relsize;	/* Size in bytes of relocation info */
9399932Sbde    const Elf_Rela *rela;	/* Relocation entries with addend */
94126890Strhodes    unsigned long relasize;	/* Size in bytes of addend relocation info */
95126890Strhodes    const Elf_Rel *pltrel;	/* PLT relocation entries */
96126890Strhodes    unsigned long pltrelsize;	/* Size in bytes of PLT relocation info */
9799923Sbde    const Elf_Rela *pltrela;	/* PLT relocation entries with addend */
98126890Strhodes    unsigned long pltrelasize;	/* Size in bytes of PLT addend reloc info */
9999923Sbde    const Elf_Sym *symtab;	/* Symbol table */
10099932Sbde    const char *strtab;		/* String table */
10185909Simp    unsigned long strsize;	/* Size in bytes of string table */
10291002Speter
10385909Simp    const Elf_Addr *buckets;	/* Hash table buckets array */
10485909Simp    unsigned long nbuckets;	/* Number of buckets */
10585909Simp    const Elf_Addr *chains;	/* Hash table chain array */
10685909Simp    unsigned long nchains;	/* Number of chains */
107116341Smarkm
108116341Smarkm    const char *rpath;		/* Search path specified in object */
109116341Smarkm    Needed_Entry *needed;	/* Shared objects needed by this one (%) */
11091002Speter
11191002Speter    void (*init)(void);		/* Initialization function to call */
11291002Speter    void (*fini)(void);		/* Termination function to call */
113105489Smux
11485909Simp    bool mainprog;		/* True if this is the main program */
115105462Smux    bool rtld;			/* True if this is the dynamic linker */
116105462Smux    bool textrel;		/* True if there are relocations to text seg */
11785909Simp    bool symbolic;		/* True if generated with "-Bsymbolic" */
118116341Smarkm    bool traced;		/* Already printed in ldd trace output */
119116341Smarkm
120131210Simp    struct link_map linkmap;	/* for GDB */
121111684Sru} Obj_Entry;
12285909Simp
123111684Sru#define RTLD_MAGIC	0xd550b87a
124111684Sru#define RTLD_VERSION	1
125111684Sru
126123966Sbdeextern void _rtld_error(const char *, ...);
12789180Smsmithextern Obj_Entry *map_object(int);
12885909Simpextern void *xcalloc(size_t);
12985909Simpextern void *xmalloc(size_t);
130123966Sbdeextern char *xstrdup(const char *);
13185909Simpextern Elf_Addr _GLOBAL_OFFSET_TABLE_[];
13285909Simp
13388893Simp/*
13488893Simp * Function declarations.
13588893Simp */
13688893Simpint do_copy_relocations(Obj_Entry *);
13790789Sphkunsigned long elf_hash(const char *);
13890789Sphkconst Elf_Sym *find_symdef(unsigned long, const Obj_Entry *,
13990789Sphk  const Obj_Entry **, bool);
14088893Simpvoid init_pltgot(Obj_Entry *);
14188893Simpint reloc_non_plt(Obj_Entry *, Obj_Entry *);
14288893Simpint reloc_plt(Obj_Entry *, bool);
14388893Simpvoid _rtld_bind_start(void);
144125772Sruconst Elf_Sym *symlook_obj(const char *, unsigned long,
14588893Simp  const Obj_Entry *, bool);
146
147#endif /* } */
148