blob: 54563bb80eb411c53c087e800b86cd999f1e5c6f [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;
Nick Kralevich5135b3a2012-08-10 21:08:42 -0700163 int has_text_relocations;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164};
165
166
167extern soinfo libdl_info;
168
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700170#include <asm/elf.h>
171
172#if defined(ANDROID_ARM_LINKER)
173
174// These aren't defined in <arch-arm/asm/elf.h>.
175#define R_ARM_REL32 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176#define R_ARM_COPY 20
177#define R_ARM_GLOB_DAT 21
178#define R_ARM_JUMP_SLOT 22
179#define R_ARM_RELATIVE 23
180
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700181#elif defined(ANDROID_MIPS_LINKER)
182
183// These aren't defined in <arch-arm/mips/elf.h>.
184#define R_MIPS_JUMP_SLOT 127
185
186#define DT_MIPS_PLTGOT 0x70000032
187#define DT_MIPS_RWPLT 0x70000034
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800188
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189#elif defined(ANDROID_X86_LINKER)
190
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700191// x86 has everything it needs in <arch-arm/x86/elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700193#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
195#ifndef DT_INIT_ARRAY
196#define DT_INIT_ARRAY 25
197#endif
198
199#ifndef DT_FINI_ARRAY
200#define DT_FINI_ARRAY 26
201#endif
202
203#ifndef DT_INIT_ARRAYSZ
204#define DT_INIT_ARRAYSZ 27
205#endif
206
207#ifndef DT_FINI_ARRAYSZ
208#define DT_FINI_ARRAYSZ 28
209#endif
210
211#ifndef DT_PREINIT_ARRAY
212#define DT_PREINIT_ARRAY 32
213#endif
214
215#ifndef DT_PREINIT_ARRAYSZ
216#define DT_PREINIT_ARRAYSZ 33
217#endif
218
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800219soinfo *find_library(const char *name);
Matt Fischer1698d9e2009-12-31 12:17:56 -0600220Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
Mathias Agopianbda5da02011-09-27 22:30:19 -0700221soinfo *find_containing_library(const void *addr);
Dima Zavin2e855792009-05-20 18:28:09 -0700222const char *linker_get_error(void);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200223
Elliott Hughesbedfe382012-08-14 14:07:59 -0700224int soinfo_unload(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200225Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr);
226Elf32_Sym *soinfo_lookup(soinfo *si, const char *name);
227void soinfo_call_constructors(soinfo *si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800228
Elliott Hughes18a206c2012-10-29 17:37:13 -0700229extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700230
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231#endif