blob: 2a6c1cd82760a9550e72c277fa9f4a697c3061e9 [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
Elliott Hughes1a696162012-11-01 13:49:32 -070039// Returns the address of the page containing address 'x'.
40#define PAGE_START(x) ((x) & PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020041
Elliott Hughes1a696162012-11-01 13:49:32 -070042// Returns the offset of address 'x' in its page.
43#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020044
Elliott Hughes1a696162012-11-01 13:49:32 -070045// Returns the address of the next page after address 'x', unless 'x' is
46// itself at the start of a page.
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020047#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49void debugger_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
51/* magic shared structures that GDB knows about */
52
53struct link_map
54{
55 uintptr_t l_addr;
56 char * l_name;
57 uintptr_t l_ld;
58 struct link_map * l_next;
59 struct link_map * l_prev;
60};
61
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062// Values for r_debug->state
63enum {
64 RT_CONSISTENT,
65 RT_ADD,
66 RT_DELETE
67};
68
69struct r_debug
70{
71 int32_t r_version;
72 struct link_map * r_map;
73 void (*r_brk)(void);
74 int32_t r_state;
75 uintptr_t r_ldbase;
76};
77
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078#define FLAG_LINKED 0x00000001
79#define FLAG_ERROR 0x00000002
80#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080081#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
83#define SOINFO_NAME_LEN 128
84
Elliott Hughes18a206c2012-10-29 17:37:13 -070085struct soinfo {
Elliott Hughes46882792012-08-03 16:49:39 -070086 char name[SOINFO_NAME_LEN];
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +020087 const Elf32_Phdr *phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088 int phnum;
89 unsigned entry;
90 unsigned base;
91 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Nick Kralevich38bccb22011-08-29 13:49:22 -070093 int unused; // DO NOT USE, maintained for compatibility.
94
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095 unsigned *dynamic;
96
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +020097 unsigned unused2; // DO NOT USE, maintained for compatibility
98 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080099
100 soinfo *next;
101 unsigned flags;
102
103 const char *strtab;
104 Elf32_Sym *symtab;
105
106 unsigned nbucket;
107 unsigned nchain;
108 unsigned *bucket;
109 unsigned *chain;
110
111 unsigned *plt_got;
112
113 Elf32_Rel *plt_rel;
114 unsigned plt_rel_count;
115
116 Elf32_Rel *rel;
117 unsigned rel_count;
118
119 unsigned *preinit_array;
120 unsigned preinit_array_count;
121
122 unsigned *init_array;
123 unsigned init_array_count;
124 unsigned *fini_array;
125 unsigned fini_array_count;
126
127 void (*init_func)(void);
128 void (*fini_func)(void);
129
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700130#if defined(ANDROID_ARM_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800131 /* ARM EABI section used for stack unwinding. */
132 unsigned *ARM_exidx;
133 unsigned ARM_exidx_count;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700134#elif defined(ANDROID_MIPS_LINKER)
135#if 0
136 /* not yet */
137 unsigned *mips_pltgot
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700139 unsigned mips_symtabno;
140 unsigned mips_local_gotno;
141 unsigned mips_gotsym;
142#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
144 unsigned refcount;
145 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400146
147 int constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800148
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200149 /* When you read a virtual address from the ELF file, add this
150 * value to get the corresponding address in the process' address space */
151 Elf32_Addr load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200152
153 bool has_text_relocations;
154 bool has_DT_SYMBOLIC;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155};
156
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157extern soinfo libdl_info;
158
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700159#if defined(ANDROID_ARM_LINKER)
160
161// These aren't defined in <arch-arm/asm/elf.h>.
162#define R_ARM_REL32 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800163#define R_ARM_COPY 20
164#define R_ARM_GLOB_DAT 21
165#define R_ARM_JUMP_SLOT 22
166#define R_ARM_RELATIVE 23
167
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700168#elif defined(ANDROID_MIPS_LINKER)
169
170// These aren't defined in <arch-arm/mips/elf.h>.
171#define R_MIPS_JUMP_SLOT 127
172
173#define DT_MIPS_PLTGOT 0x70000032
174#define DT_MIPS_RWPLT 0x70000034
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800175
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176#elif defined(ANDROID_X86_LINKER)
177
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700178// x86 has everything it needs in <arch-arm/x86/elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700180#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
182#ifndef DT_INIT_ARRAY
183#define DT_INIT_ARRAY 25
184#endif
185
186#ifndef DT_FINI_ARRAY
187#define DT_FINI_ARRAY 26
188#endif
189
190#ifndef DT_INIT_ARRAYSZ
191#define DT_INIT_ARRAYSZ 27
192#endif
193
194#ifndef DT_FINI_ARRAYSZ
195#define DT_FINI_ARRAYSZ 28
196#endif
197
198#ifndef DT_PREINIT_ARRAY
199#define DT_PREINIT_ARRAY 32
200#endif
201
202#ifndef DT_PREINIT_ARRAYSZ
203#define DT_PREINIT_ARRAYSZ 33
204#endif
205
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800206soinfo *find_library(const char *name);
Matt Fischer1698d9e2009-12-31 12:17:56 -0600207Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
Mathias Agopianbda5da02011-09-27 22:30:19 -0700208soinfo *find_containing_library(const void *addr);
Dima Zavin2e855792009-05-20 18:28:09 -0700209const char *linker_get_error(void);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200210
Elliott Hughesbedfe382012-08-14 14:07:59 -0700211int soinfo_unload(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200212Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr);
213Elf32_Sym *soinfo_lookup(soinfo *si, const char *name);
214void soinfo_call_constructors(soinfo *si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800215
Elliott Hughes18a206c2012-10-29 17:37:13 -0700216extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700217
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800218#endif