blob: cb33602bf90de0ac8287f0c21b40c90590bb83f9 [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
Pavel Chupinb7beb692012-08-17 12:53:29 +040037#include <link.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080039#undef PAGE_MASK
40#undef PAGE_SIZE
41#define PAGE_SIZE 4096
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020042#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))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059
60void debugger_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080061
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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080073// 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
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080089#define FLAG_LINKED 0x00000001
90#define FLAG_ERROR 0x00000002
91#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080092#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080093
94#define SOINFO_NAME_LEN 128
95
Elliott Hughes18a206c2012-10-29 17:37:13 -070096struct soinfo {
Elliott Hughes46882792012-08-03 16:49:39 -070097 char name[SOINFO_NAME_LEN];
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +020098 const Elf32_Phdr *phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099 int phnum;
100 unsigned entry;
101 unsigned base;
102 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800103
Nick Kralevich38bccb22011-08-29 13:49:22 -0700104 int unused; // DO NOT USE, maintained for compatibility.
105
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106 unsigned *dynamic;
107
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200108 unsigned unused2; // DO NOT USE, maintained for compatibility
109 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110
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
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700141#if defined(ANDROID_ARM_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142 /* ARM EABI section used for stack unwinding. */
143 unsigned *ARM_exidx;
144 unsigned ARM_exidx_count;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700145#elif defined(ANDROID_MIPS_LINKER)
146#if 0
147 /* not yet */
148 unsigned *mips_pltgot
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700150 unsigned mips_symtabno;
151 unsigned mips_local_gotno;
152 unsigned mips_gotsym;
153#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
155 unsigned refcount;
156 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400157
158 int constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800159
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200160 /* 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;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200163
164 bool has_text_relocations;
165 bool has_DT_SYMBOLIC;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800166};
167
168
169extern soinfo libdl_info;
170
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700172#include <asm/elf.h>
173
174#if defined(ANDROID_ARM_LINKER)
175
176// These aren't defined in <arch-arm/asm/elf.h>.
177#define R_ARM_REL32 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800178#define R_ARM_COPY 20
179#define R_ARM_GLOB_DAT 21
180#define R_ARM_JUMP_SLOT 22
181#define R_ARM_RELATIVE 23
182
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700183#elif defined(ANDROID_MIPS_LINKER)
184
185// These aren't defined in <arch-arm/mips/elf.h>.
186#define R_MIPS_JUMP_SLOT 127
187
188#define DT_MIPS_PLTGOT 0x70000032
189#define DT_MIPS_RWPLT 0x70000034
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800190
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800191#elif defined(ANDROID_X86_LINKER)
192
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700193// x86 has everything it needs in <arch-arm/x86/elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700195#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800196
197#ifndef DT_INIT_ARRAY
198#define DT_INIT_ARRAY 25
199#endif
200
201#ifndef DT_FINI_ARRAY
202#define DT_FINI_ARRAY 26
203#endif
204
205#ifndef DT_INIT_ARRAYSZ
206#define DT_INIT_ARRAYSZ 27
207#endif
208
209#ifndef DT_FINI_ARRAYSZ
210#define DT_FINI_ARRAYSZ 28
211#endif
212
213#ifndef DT_PREINIT_ARRAY
214#define DT_PREINIT_ARRAY 32
215#endif
216
217#ifndef DT_PREINIT_ARRAYSZ
218#define DT_PREINIT_ARRAYSZ 33
219#endif
220
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800221soinfo *find_library(const char *name);
Matt Fischer1698d9e2009-12-31 12:17:56 -0600222Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
Mathias Agopianbda5da02011-09-27 22:30:19 -0700223soinfo *find_containing_library(const void *addr);
Dima Zavin2e855792009-05-20 18:28:09 -0700224const char *linker_get_error(void);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200225
Elliott Hughesbedfe382012-08-14 14:07:59 -0700226int soinfo_unload(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200227Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr);
228Elf32_Sym *soinfo_lookup(soinfo *si, const char *name);
229void soinfo_call_constructors(soinfo *si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800230
Elliott Hughes18a206c2012-10-29 17:37:13 -0700231extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700232
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233#endif