linker.h revision ba98d9237b0eabc1d8caf2600fd787b988645249
1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *  * Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 *  * Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *    the documentation and/or other materials provided with the
13 *    distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifndef _LINKER_H_
30#define _LINKER_H_
31
32#include <unistd.h>
33#include <sys/types.h>
34#include <elf.h>
35#include <sys/exec_elf.h>
36
37#include <link.h>
38
39#undef PAGE_MASK
40#undef PAGE_SIZE
41#define PAGE_SIZE 4096
42#define PAGE_MASK (PAGE_SIZE-1)
43
44/* Convenience macros to make page address/offset computations more explicit */
45
46/* Returns the address of the page starting at address 'x' */
47#define PAGE_START(x)  ((x) & ~PAGE_MASK)
48
49/* Returns the offset of address 'x' in its memory page, i.e. this is the
50 * same than 'x' - PAGE_START(x) */
51#define PAGE_OFFSET(x) ((x) & PAGE_MASK)
52
53/* Returns the address of the next page after address 'x', unless 'x' is
54 * itself at the start of a page. Equivalent to:
55 *
56 *  (x == PAGE_START(x)) ? x : PAGE_START(x)+PAGE_SIZE
57 */
58#define PAGE_END(x)    PAGE_START((x) + (PAGE_SIZE-1))
59
60void debugger_init();
61
62/* magic shared structures that GDB knows about */
63
64struct link_map
65{
66    uintptr_t l_addr;
67    char * l_name;
68    uintptr_t l_ld;
69    struct link_map * l_next;
70    struct link_map * l_prev;
71};
72
73// Values for r_debug->state
74enum {
75    RT_CONSISTENT,
76    RT_ADD,
77    RT_DELETE
78};
79
80struct r_debug
81{
82    int32_t r_version;
83    struct link_map * r_map;
84    void (*r_brk)(void);
85    int32_t r_state;
86    uintptr_t r_ldbase;
87};
88
89#define FLAG_LINKED     0x00000001
90#define FLAG_ERROR      0x00000002
91#define FLAG_EXE        0x00000004 // The main executable
92#define FLAG_LINKER     0x00000010 // The linker itself
93
94#define SOINFO_NAME_LEN 128
95
96struct soinfo {
97    char name[SOINFO_NAME_LEN];
98    const Elf32_Phdr *phdr;
99    int phnum;
100    unsigned entry;
101    unsigned base;
102    unsigned size;
103
104    int unused;  // DO NOT USE, maintained for compatibility.
105
106    unsigned *dynamic;
107
108    unsigned unused2; // DO NOT USE, maintained for compatibility
109    unsigned unused3; // DO NOT USE, maintained for compatibility
110
111    soinfo *next;
112    unsigned flags;
113
114    const char *strtab;
115    Elf32_Sym *symtab;
116
117    unsigned nbucket;
118    unsigned nchain;
119    unsigned *bucket;
120    unsigned *chain;
121
122    unsigned *plt_got;
123
124    Elf32_Rel *plt_rel;
125    unsigned plt_rel_count;
126
127    Elf32_Rel *rel;
128    unsigned rel_count;
129
130    unsigned *preinit_array;
131    unsigned preinit_array_count;
132
133    unsigned *init_array;
134    unsigned init_array_count;
135    unsigned *fini_array;
136    unsigned fini_array_count;
137
138    void (*init_func)(void);
139    void (*fini_func)(void);
140
141#if defined(ANDROID_ARM_LINKER)
142    /* ARM EABI section used for stack unwinding. */
143    unsigned *ARM_exidx;
144    unsigned ARM_exidx_count;
145#elif defined(ANDROID_MIPS_LINKER)
146#if 0
147     /* not yet */
148     unsigned *mips_pltgot
149#endif
150     unsigned mips_symtabno;
151     unsigned mips_local_gotno;
152     unsigned mips_gotsym;
153#endif /* ANDROID_*_LINKER */
154
155    unsigned refcount;
156    struct link_map linkmap;
157
158    int constructors_called;
159
160    /* When you read a virtual address from the ELF file, add this
161     * value to get the corresponding address in the process' address space */
162    Elf32_Addr load_bias;
163
164    bool has_text_relocations;
165    bool has_DT_SYMBOLIC;
166};
167
168extern soinfo libdl_info;
169
170
171#include <asm/elf.h>
172
173#if defined(ANDROID_ARM_LINKER)
174
175// These aren't defined in <arch-arm/asm/elf.h>.
176#define R_ARM_REL32      3
177#define R_ARM_COPY       20
178#define R_ARM_GLOB_DAT   21
179#define R_ARM_JUMP_SLOT  22
180#define R_ARM_RELATIVE   23
181
182#elif defined(ANDROID_MIPS_LINKER)
183
184// These aren't defined in <arch-arm/mips/elf.h>.
185#define R_MIPS_JUMP_SLOT       127
186
187#define DT_MIPS_PLTGOT         0x70000032
188#define DT_MIPS_RWPLT          0x70000034
189
190#elif defined(ANDROID_X86_LINKER)
191
192// x86 has everything it needs in <arch-arm/x86/elf.h>.
193
194#endif /* ANDROID_*_LINKER */
195
196#ifndef DT_INIT_ARRAY
197#define DT_INIT_ARRAY      25
198#endif
199
200#ifndef DT_FINI_ARRAY
201#define DT_FINI_ARRAY      26
202#endif
203
204#ifndef DT_INIT_ARRAYSZ
205#define DT_INIT_ARRAYSZ    27
206#endif
207
208#ifndef DT_FINI_ARRAYSZ
209#define DT_FINI_ARRAYSZ    28
210#endif
211
212#ifndef DT_PREINIT_ARRAY
213#define DT_PREINIT_ARRAY   32
214#endif
215
216#ifndef DT_PREINIT_ARRAYSZ
217#define DT_PREINIT_ARRAYSZ 33
218#endif
219
220soinfo *find_library(const char *name);
221Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
222soinfo *find_containing_library(const void *addr);
223const char *linker_get_error(void);
224
225int soinfo_unload(soinfo* si);
226Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr);
227Elf32_Sym *soinfo_lookup(soinfo *si, const char *name);
228void soinfo_call_constructors(soinfo *si);
229
230extern "C" void notify_gdb_of_libraries();
231
232#endif
233