blob: 60c76fa91ddb11df7b153e0052bb3840510587eb [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
Elliott Hughesd23736e2012-11-01 15:16:56 -070049// Magic shared structures that GDB knows about.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080050
Elliott Hughesd23736e2012-11-01 15:16:56 -070051struct link_map {
52 uintptr_t l_addr;
53 char * l_name;
54 uintptr_t l_ld;
55 struct link_map * l_next;
56 struct link_map * l_prev;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080057};
58
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080059// Values for r_debug->state
60enum {
Elliott Hughesd23736e2012-11-01 15:16:56 -070061 RT_CONSISTENT,
62 RT_ADD,
63 RT_DELETE
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080064};
65
Elliott Hughesd23736e2012-11-01 15:16:56 -070066struct r_debug {
67 int32_t r_version;
68 struct link_map* r_map;
69 void (*r_brk)(void);
70 int32_t r_state;
71 uintptr_t r_ldbase;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080072};
73
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080074#define FLAG_LINKED 0x00000001
75#define FLAG_ERROR 0x00000002
76#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080077#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080078
79#define SOINFO_NAME_LEN 128
80
Elliott Hughes18a206c2012-10-29 17:37:13 -070081struct soinfo {
Elliott Hughesd23736e2012-11-01 15:16:56 -070082 char name[SOINFO_NAME_LEN];
83 const Elf32_Phdr* phdr;
84 int phnum;
85 unsigned entry;
86 unsigned base;
87 unsigned size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088
Elliott Hughesd23736e2012-11-01 15:16:56 -070089 int unused; // DO NOT USE, maintained for compatibility.
Nick Kralevich38bccb22011-08-29 13:49:22 -070090
Elliott Hughesd23736e2012-11-01 15:16:56 -070091 unsigned* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080092
Elliott Hughesd23736e2012-11-01 15:16:56 -070093 unsigned unused2; // DO NOT USE, maintained for compatibility
94 unsigned unused3; // DO NOT USE, maintained for compatibility
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080095
Elliott Hughesd23736e2012-11-01 15:16:56 -070096 soinfo* next;
97 unsigned flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Elliott Hughesd23736e2012-11-01 15:16:56 -070099 const char* strtab;
100 Elf32_Sym* symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800101
Elliott Hughesd23736e2012-11-01 15:16:56 -0700102 unsigned nbucket;
103 unsigned nchain;
104 unsigned* bucket;
105 unsigned* chain;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800106
Elliott Hughesd23736e2012-11-01 15:16:56 -0700107 unsigned* plt_got;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800108
Elliott Hughesd23736e2012-11-01 15:16:56 -0700109 Elf32_Rel* plt_rel;
110 unsigned plt_rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800111
Elliott Hughesd23736e2012-11-01 15:16:56 -0700112 Elf32_Rel* rel;
113 unsigned rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800114
Elliott Hughesd23736e2012-11-01 15:16:56 -0700115 unsigned* preinit_array;
116 unsigned preinit_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800117
Elliott Hughesd23736e2012-11-01 15:16:56 -0700118 unsigned* init_array;
119 unsigned init_array_count;
120 unsigned* fini_array;
121 unsigned fini_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800122
Elliott Hughesd23736e2012-11-01 15:16:56 -0700123 void (*init_func)();
124 void (*fini_func)();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800125
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700126#if defined(ANDROID_ARM_LINKER)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700127 // ARM EABI section used for stack unwinding.
128 unsigned* ARM_exidx;
129 unsigned ARM_exidx_count;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700130#elif defined(ANDROID_MIPS_LINKER)
131#if 0
Elliott Hughesd23736e2012-11-01 15:16:56 -0700132 // Not yet.
133 unsigned* mips_pltgot
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800134#endif
Elliott Hughesd23736e2012-11-01 15:16:56 -0700135 unsigned mips_symtabno;
136 unsigned mips_local_gotno;
137 unsigned mips_gotsym;
138#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800139
Elliott Hughesd23736e2012-11-01 15:16:56 -0700140 unsigned refcount;
141 struct link_map linkmap;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400142
Elliott Hughesd23736e2012-11-01 15:16:56 -0700143 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800144
Elliott Hughesd23736e2012-11-01 15:16:56 -0700145 // When you read a virtual address from the ELF file, add this
146 // value to get the corresponding address in the process' address space.
147 Elf32_Addr load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200148
Elliott Hughesd23736e2012-11-01 15:16:56 -0700149 bool has_text_relocations;
150 bool has_DT_SYMBOLIC;
151
152 void CallConstructors();
153 void CallDestructors();
154 void CallPreInitConstructors();
155
156 private:
157 void CallArray(const char* array_name, unsigned* array, int count, bool reverse);
158 void CallFunction(const char* function_name, void (*function)());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159};
160
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800161extern soinfo libdl_info;
162
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700163#if defined(ANDROID_ARM_LINKER)
164
165// These aren't defined in <arch-arm/asm/elf.h>.
166#define R_ARM_REL32 3
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167#define R_ARM_COPY 20
168#define R_ARM_GLOB_DAT 21
169#define R_ARM_JUMP_SLOT 22
170#define R_ARM_RELATIVE 23
171
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700172#elif defined(ANDROID_MIPS_LINKER)
173
174// These aren't defined in <arch-arm/mips/elf.h>.
175#define R_MIPS_JUMP_SLOT 127
176
177#define DT_MIPS_PLTGOT 0x70000032
178#define DT_MIPS_RWPLT 0x70000034
David 'Digit' Turnerfe62de12009-12-02 10:54:53 -0800179
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800180#elif defined(ANDROID_X86_LINKER)
181
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700182// x86 has everything it needs in <arch-arm/x86/elf.h>.
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800183
Raghu Gandhamd7daacb2012-07-31 12:07:22 -0700184#endif /* ANDROID_*_LINKER */
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800185
186#ifndef DT_INIT_ARRAY
187#define DT_INIT_ARRAY 25
188#endif
189
190#ifndef DT_FINI_ARRAY
191#define DT_FINI_ARRAY 26
192#endif
193
194#ifndef DT_INIT_ARRAYSZ
195#define DT_INIT_ARRAYSZ 27
196#endif
197
198#ifndef DT_FINI_ARRAYSZ
199#define DT_FINI_ARRAYSZ 28
200#endif
201
202#ifndef DT_PREINIT_ARRAY
203#define DT_PREINIT_ARRAY 32
204#endif
205
206#ifndef DT_PREINIT_ARRAYSZ
207#define DT_PREINIT_ARRAYSZ 33
208#endif
209
Elliott Hughese66190d2012-12-18 15:57:55 -0800210soinfo* do_dlopen(const char* name, int flags);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700211int do_dlclose(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200212
Elliott Hughesd23736e2012-11-01 15:16:56 -0700213Elf32_Sym* lookup(const char* name, soinfo** found, soinfo* start);
214soinfo* find_containing_library(const void* addr);
215const char* linker_get_error();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800216
Elliott Hughesd23736e2012-11-01 15:16:56 -0700217Elf32_Sym* soinfo_find_symbol(soinfo* si, const void* addr);
218Elf32_Sym* soinfo_lookup(soinfo* si, const char* name);
219
220void debugger_init();
Elliott Hughes18a206c2012-10-29 17:37:13 -0700221extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700222
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800223#endif