blob: 0956ac5260455ddae2e8a7372cb3679408bc7f04 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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>
Nick Kralevich9ec0f032012-02-28 10:40:00 -080034#include <elf.h>
35#include <sys/exec_elf.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080036
37#undef PAGE_MASK
38#undef PAGE_SIZE
39#define PAGE_SIZE 4096
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020040#define PAGE_MASK (PAGE_SIZE-1)
41
42/* Convenience macros to make page address/offset computations more explicit */
43
44/* Returns the address of the page starting at address 'x' */
45#define PAGE_START(x) ((x) & ~PAGE_MASK)
46
47/* Returns the offset of address 'x' in its memory page, i.e. this is the
48 * same than 'x' - PAGE_START(x) */
49#define PAGE_OFFSET(x) ((x) & PAGE_MASK)
50
51/* Returns the address of the next page after address 'x', unless 'x' is
52 * itself at the start of a page. Equivalent to:
53 *
54 * (x == PAGE_START(x)) ? x : PAGE_START(x)+PAGE_SIZE
55 */
56#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057
58void debugger_init();
59const char *addr_to_name(unsigned addr);
60
61/* magic shared structures that GDB knows about */
62
63struct link_map
64{
65 uintptr_t l_addr;
66 char * l_name;
67 uintptr_t l_ld;
68 struct link_map * l_next;
69 struct link_map * l_prev;
70};
71
72/* needed for dl_iterate_phdr to be passed to the callbacks provided */
73struct dl_phdr_info
74{
75 Elf32_Addr dlpi_addr;
76 const char *dlpi_name;
77 const Elf32_Phdr *dlpi_phdr;
78 Elf32_Half dlpi_phnum;
79};
80
81
82// Values for r_debug->state
83enum {
84 RT_CONSISTENT,
85 RT_ADD,
86 RT_DELETE
87};
88
89struct r_debug
90{
91 int32_t r_version;
92 struct link_map * r_map;
93 void (*r_brk)(void);
94 int32_t r_state;
95 uintptr_t r_ldbase;
96};
97
98typedef struct soinfo soinfo;
99
100#define FLAG_LINKED 0x00000001
101#define FLAG_ERROR 0x00000002
102#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -0800103#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
105#define SOINFO_NAME_LEN 128
106
107struct soinfo
108{
109 const char name[SOINFO_NAME_LEN];
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200110 const Elf32_Phdr *phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111 int phnum;
112 unsigned entry;
113 unsigned base;
114 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115
Nick Kralevich38bccb22011-08-29 13:49:22 -0700116 int unused; // DO NOT USE, maintained for compatibility.
117
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118 unsigned *dynamic;
119
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200120 unsigned unused2; // DO NOT USE, maintained for compatibility
121 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
123 soinfo *next;
124 unsigned flags;
125
126 const char *strtab;
127 Elf32_Sym *symtab;
128
129 unsigned nbucket;
130 unsigned nchain;
131 unsigned *bucket;
132 unsigned *chain;
133
134 unsigned *plt_got;
135
136 Elf32_Rel *plt_rel;
137 unsigned plt_rel_count;
138
139 Elf32_Rel *rel;
140 unsigned rel_count;
141
142 unsigned *preinit_array;
143 unsigned preinit_array_count;
144
145 unsigned *init_array;
146 unsigned init_array_count;
147 unsigned *fini_array;
148 unsigned fini_array_count;
149
150 void (*init_func)(void);
151 void (*fini_func)(void);
152
153#ifdef ANDROID_ARM_LINKER
154 /* ARM EABI section used for stack unwinding. */
155 unsigned *ARM_exidx;
156 unsigned ARM_exidx_count;
157#endif
158
159 unsigned refcount;
160 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400161
162 int constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800163
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200164 /* When you read a virtual address from the ELF file, add this
165 * value to get the corresponding address in the process' address space */
166 Elf32_Addr load_bias;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167};
168
169
170extern soinfo libdl_info;
171
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800172#ifdef ANDROID_ARM_LINKER
173
174#define R_ARM_COPY 20
175#define R_ARM_GLOB_DAT 21
176#define R_ARM_JUMP_SLOT 22
177#define R_ARM_RELATIVE 23
178
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800179/* According to the AAPCS specification, we only
180 * need the above relocations. However, in practice,
181 * the following ones turn up from time to time.
182 */
183#define R_ARM_ABS32 2
184#define R_ARM_REL32 3
185
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800186#elif defined(ANDROID_X86_LINKER)
187
188#define R_386_32 1
189#define R_386_PC32 2
190#define R_386_GLOB_DAT 6
191#define R_386_JUMP_SLOT 7
192#define R_386_RELATIVE 8
193
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100194#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195
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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800220soinfo *find_library(const char *name);
Matt Fischer1698d9e2009-12-31 12:17:56 -0600221Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
Mathias Agopianbda5da02011-09-27 22:30:19 -0700222soinfo *find_containing_library(const void *addr);
Dima Zavin2e855792009-05-20 18:28:09 -0700223const char *linker_get_error(void);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200224
225unsigned 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);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800229
230#ifdef ANDROID_ARM_LINKER
231typedef long unsigned int *_Unwind_Ptr;
232_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount);
David 'Digit' Turner70b16682012-01-30 17:17:58 +0100233#elif defined(ANDROID_X86_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, size_t, void *), void *);
235#endif
236
237#endif