linker.h revision 7059b1f02ea9197728c851edd9ae0dd7688fa700
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 <linux/elf.h>
35
36#undef PAGE_MASK
37#undef PAGE_SIZE
38#define PAGE_SIZE 4096
39#define PAGE_MASK 4095
40
41void debugger_init();
42const char *addr_to_name(unsigned addr);
43
44/* magic shared structures that GDB knows about */
45
46struct link_map
47{
48    uintptr_t l_addr;
49    char * l_name;
50    uintptr_t l_ld;
51    struct link_map * l_next;
52    struct link_map * l_prev;
53};
54
55/* needed for dl_iterate_phdr to be passed to the callbacks provided */
56struct dl_phdr_info
57{
58    Elf32_Addr dlpi_addr;
59    const char *dlpi_name;
60    const Elf32_Phdr *dlpi_phdr;
61    Elf32_Half dlpi_phnum;
62};
63
64
65// Values for r_debug->state
66enum {
67    RT_CONSISTENT,
68    RT_ADD,
69    RT_DELETE
70};
71
72struct r_debug
73{
74    int32_t r_version;
75    struct link_map * r_map;
76    void (*r_brk)(void);
77    int32_t r_state;
78    uintptr_t r_ldbase;
79};
80
81typedef struct soinfo soinfo;
82
83#define FLAG_LINKED     0x00000001
84#define FLAG_ERROR      0x00000002
85#define FLAG_EXE        0x00000004 // The main executable
86
87#define SOINFO_NAME_LEN 128
88
89struct soinfo
90{
91    const char name[SOINFO_NAME_LEN];
92    Elf32_Phdr *phdr;
93    int phnum;
94    unsigned entry;
95    unsigned base;
96    unsigned size;
97
98    unsigned *dynamic;
99
100    unsigned wrprotect_start;
101    unsigned wrprotect_end;
102
103    soinfo *next;
104    unsigned flags;
105
106    const char *strtab;
107    Elf32_Sym *symtab;
108
109    unsigned nbucket;
110    unsigned nchain;
111    unsigned *bucket;
112    unsigned *chain;
113
114    unsigned *plt_got;
115
116    Elf32_Rel *plt_rel;
117    unsigned plt_rel_count;
118
119    Elf32_Rel *rel;
120    unsigned rel_count;
121
122#ifdef ANDROID_SH_LINKER
123    Elf32_Rela *plt_rela;
124    unsigned plt_rela_count;
125
126    Elf32_Rela *rela;
127    unsigned rela_count;
128#endif /* ANDROID_SH_LINKER */
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#ifdef ANDROID_ARM_LINKER
142    /* ARM EABI section used for stack unwinding. */
143    unsigned *ARM_exidx;
144    unsigned ARM_exidx_count;
145#endif
146
147    unsigned refcount;
148    struct link_map linkmap;
149};
150
151
152extern soinfo libdl_info;
153
154#ifdef ANDROID_ARM_LINKER
155
156#define R_ARM_COPY       20
157#define R_ARM_GLOB_DAT   21
158#define R_ARM_JUMP_SLOT  22
159#define R_ARM_RELATIVE   23
160
161/* According to the AAPCS specification, we only
162 * need the above relocations. However, in practice,
163 * the following ones turn up from time to time.
164 */
165#define R_ARM_ABS32      2
166#define R_ARM_REL32      3
167
168#elif defined(ANDROID_X86_LINKER)
169
170#define R_386_32         1
171#define R_386_PC32       2
172#define R_386_GLOB_DAT   6
173#define R_386_JUMP_SLOT  7
174#define R_386_RELATIVE   8
175
176#elif defined(ANDROID_SH_LINKER)
177
178#define R_SH_DIR32      1
179#define R_SH_GLOB_DAT   163
180#define R_SH_JUMP_SLOT  164
181#define R_SH_RELATIVE   165
182
183#endif /* ANDROID_*_LINKER */
184
185
186#ifndef DT_INIT_ARRAY
187#define DT_INIT_ARRAY      25
188#endif
189
190#ifndef DT_FINI_ARRAY
191#define DT_FINI_ARRAY      26
192#endif
193
194#ifndef DT_INIT_ARRAYSZ
195#define DT_INIT_ARRAYSZ    27
196#endif
197
198#ifndef DT_FINI_ARRAYSZ
199#define DT_FINI_ARRAYSZ    28
200#endif
201
202#ifndef DT_PREINIT_ARRAY
203#define DT_PREINIT_ARRAY   32
204#endif
205
206#ifndef DT_PREINIT_ARRAYSZ
207#define DT_PREINIT_ARRAYSZ 33
208#endif
209
210soinfo *find_library(const char *name);
211unsigned unload_library(soinfo *si);
212Elf32_Sym *lookup_in_library(soinfo *si, const char *name);
213Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
214soinfo *find_containing_library(void *addr);
215Elf32_Sym *find_containing_symbol(void *addr, soinfo *si);
216const char *linker_get_error(void);
217
218#ifdef ANDROID_ARM_LINKER
219typedef long unsigned int *_Unwind_Ptr;
220_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
221#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_LINKER)
222int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, size_t, void *), void *);
223#endif
224
225#endif
226