blob: 8ed433cf126eeed3296e5ebd4e9916e59190f315 [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
Elliott Hughes46882792012-08-03 16:49:39 -070037#ifdef __cplusplus
38extern "C" {
39#endif
Pavel Chupinb7beb692012-08-17 12:53:29 +040040#include <link.h>
Elliott Hughes46882792012-08-03 16:49:39 -070041
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080042#undef PAGE_MASK
43#undef PAGE_SIZE
44#define PAGE_SIZE 4096
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020045#define PAGE_MASK (PAGE_SIZE-1)
46
47/* Convenience macros to make page address/offset computations more explicit */
48
49/* Returns the address of the page starting at address 'x' */
50#define PAGE_START(x) ((x) & ~PAGE_MASK)
51
52/* Returns the offset of address 'x' in its memory page, i.e. this is the
53 * same than 'x' - PAGE_START(x) */
54#define PAGE_OFFSET(x) ((x) & PAGE_MASK)
55
56/* Returns the address of the next page after address 'x', unless 'x' is
57 * itself at the start of a page. Equivalent to:
58 *
59 * (x == PAGE_START(x)) ? x : PAGE_START(x)+PAGE_SIZE
60 */
61#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080062
63void debugger_init();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064
65/* magic shared structures that GDB knows about */
66
67struct link_map
68{
69 uintptr_t l_addr;
70 char * l_name;
71 uintptr_t l_ld;
72 struct link_map * l_next;
73 struct link_map * l_prev;
74};
75
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080076// Values for r_debug->state
77enum {
78 RT_CONSISTENT,
79 RT_ADD,
80 RT_DELETE
81};
82
83struct r_debug
84{
85 int32_t r_version;
86 struct link_map * r_map;
87 void (*r_brk)(void);
88 int32_t r_state;
89 uintptr_t r_ldbase;
90};
91
92typedef struct soinfo soinfo;
93
94#define FLAG_LINKED 0x00000001
95#define FLAG_ERROR 0x00000002
96#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080097#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
99#define SOINFO_NAME_LEN 128
100
101struct soinfo
102{
Elliott Hughes46882792012-08-03 16:49:39 -0700103 char name[SOINFO_NAME_LEN];
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200104 const Elf32_Phdr *phdr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800105 int phnum;
106 unsigned entry;
107 unsigned base;
108 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800109
Nick Kralevich38bccb22011-08-29 13:49:22 -0700110 int unused; // DO NOT USE, maintained for compatibility.
111
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800112 unsigned *dynamic;
113
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +0200114 unsigned unused2; // DO NOT USE, maintained for compatibility
115 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800116
117 soinfo *next;
118 unsigned flags;
119
120 const char *strtab;
121 Elf32_Sym *symtab;
122
123 unsigned nbucket;
124 unsigned nchain;
125 unsigned *bucket;
126 unsigned *chain;
127
128 unsigned *plt_got;
129
130 Elf32_Rel *plt_rel;
131 unsigned plt_rel_count;
132
133 Elf32_Rel *rel;
134 unsigned rel_count;
135
136 unsigned *preinit_array;
137 unsigned preinit_array_count;
138
139 unsigned *init_array;
140 unsigned init_array_count;
141 unsigned *fini_array;
142 unsigned fini_array_count;
143
144 void (*init_func)(void);
145 void (*fini_func)(void);
146
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700147#if defined(ANDROID_ARM_LINKER)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148 /* ARM EABI section used for stack unwinding. */
149 unsigned *ARM_exidx;
150 unsigned ARM_exidx_count;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700151#elif defined(ANDROID_MIPS_LINKER)
152#if 0
153 /* not yet */
154 unsigned *mips_pltgot
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800155#endif
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700156 unsigned mips_symtabno;
157 unsigned mips_local_gotno;
158 unsigned mips_gotsym;
159#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800160
161 unsigned refcount;
162 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400163
164 int constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800165
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +0200166 /* When you read a virtual address from the ELF file, add this
167 * value to get the corresponding address in the process' address space */
168 Elf32_Addr load_bias;
Nick Kralevich5135b3a2012-08-10 21:08:42 -0700169 int has_text_relocations;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800170};
171
172
173extern soinfo libdl_info;
174
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800175
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700176#include <asm/elf.h>
177
178#if defined(ANDROID_ARM_LINKER)
179
180// These aren't defined in <arch-arm/asm/elf.h>.
181#define R_ARM_REL32 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182#define R_ARM_COPY 20
183#define R_ARM_GLOB_DAT 21
184#define R_ARM_JUMP_SLOT 22
185#define R_ARM_RELATIVE 23
186
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700187#elif defined(ANDROID_MIPS_LINKER)
188
189// These aren't defined in <arch-arm/mips/elf.h>.
190#define R_MIPS_JUMP_SLOT 127
191
192#define DT_MIPS_PLTGOT 0x70000032
193#define DT_MIPS_RWPLT 0x70000034
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800194
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800195#elif defined(ANDROID_X86_LINKER)
196
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700197// x86 has everything it needs in <arch-arm/x86/elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800198
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700199#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
201#ifndef DT_INIT_ARRAY
202#define DT_INIT_ARRAY 25
203#endif
204
205#ifndef DT_FINI_ARRAY
206#define DT_FINI_ARRAY 26
207#endif
208
209#ifndef DT_INIT_ARRAYSZ
210#define DT_INIT_ARRAYSZ 27
211#endif
212
213#ifndef DT_FINI_ARRAYSZ
214#define DT_FINI_ARRAYSZ 28
215#endif
216
217#ifndef DT_PREINIT_ARRAY
218#define DT_PREINIT_ARRAY 32
219#endif
220
221#ifndef DT_PREINIT_ARRAYSZ
222#define DT_PREINIT_ARRAYSZ 33
223#endif
224
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800225soinfo *find_library(const char *name);
Matt Fischer1698d9e2009-12-31 12:17:56 -0600226Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start);
Mathias Agopianbda5da02011-09-27 22:30:19 -0700227soinfo *find_containing_library(const void *addr);
Dima Zavin2e855792009-05-20 18:28:09 -0700228const char *linker_get_error(void);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200229
Elliott Hughesbedfe382012-08-14 14:07:59 -0700230int soinfo_unload(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200231Elf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr);
232Elf32_Sym *soinfo_lookup(soinfo *si, const char *name);
233void soinfo_call_constructors(soinfo *si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800234
Elliott Hughes46882792012-08-03 16:49:39 -0700235#ifdef __cplusplus
236};
237#endif
238
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800239#endif