The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 34 | #include <elf.h> |
| 35 | #include <sys/exec_elf.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 36 | |
Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 37 | #include <link.h> |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 39 | // Returns the address of the page containing address 'x'. |
| 40 | #define PAGE_START(x) ((x) & PAGE_MASK) |
David 'Digit' Turner | c1bd559 | 2012-06-19 11:21:29 +0200 | [diff] [blame] | 41 | |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 42 | // Returns the offset of address 'x' in its page. |
| 43 | #define PAGE_OFFSET(x) ((x) & ~PAGE_MASK) |
David 'Digit' Turner | c1bd559 | 2012-06-19 11:21:29 +0200 | [diff] [blame] | 44 | |
Elliott Hughes | 1a69616 | 2012-11-01 13:49:32 -0700 | [diff] [blame] | 45 | // Returns the address of the next page after address 'x', unless 'x' is |
| 46 | // itself at the start of a page. |
David 'Digit' Turner | c1bd559 | 2012-06-19 11:21:29 +0200 | [diff] [blame] | 47 | #define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 48 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 49 | // Magic shared structures that GDB knows about. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 50 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 51 | struct 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 57 | }; |
| 58 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | // Values for r_debug->state |
| 60 | enum { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 61 | RT_CONSISTENT, |
| 62 | RT_ADD, |
| 63 | RT_DELETE |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | }; |
| 65 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 66 | struct 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 Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 72 | }; |
| 73 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | #define FLAG_LINKED 0x00000001 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 75 | #define FLAG_EXE 0x00000004 // The main executable |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 76 | #define FLAG_LINKER 0x00000010 // The linker itself |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | |
| 78 | #define SOINFO_NAME_LEN 128 |
| 79 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 80 | struct soinfo { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 81 | char name[SOINFO_NAME_LEN]; |
| 82 | const Elf32_Phdr* phdr; |
| 83 | int phnum; |
| 84 | unsigned entry; |
| 85 | unsigned base; |
| 86 | unsigned size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 87 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 88 | int unused; // DO NOT USE, maintained for compatibility. |
Nick Kralevich | 38bccb2 | 2011-08-29 13:49:22 -0700 | [diff] [blame] | 89 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 90 | unsigned* dynamic; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 91 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 92 | unsigned unused2; // DO NOT USE, maintained for compatibility |
| 93 | unsigned unused3; // DO NOT USE, maintained for compatibility |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 94 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 95 | soinfo* next; |
| 96 | unsigned flags; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 98 | const char* strtab; |
| 99 | Elf32_Sym* symtab; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 100 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 101 | unsigned nbucket; |
| 102 | unsigned nchain; |
| 103 | unsigned* bucket; |
| 104 | unsigned* chain; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 105 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 106 | unsigned* plt_got; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 107 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 108 | Elf32_Rel* plt_rel; |
| 109 | unsigned plt_rel_count; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 110 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 111 | Elf32_Rel* rel; |
| 112 | unsigned rel_count; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 114 | unsigned* preinit_array; |
| 115 | unsigned preinit_array_count; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 116 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 117 | unsigned* init_array; |
| 118 | unsigned init_array_count; |
| 119 | unsigned* fini_array; |
| 120 | unsigned fini_array_count; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 121 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 122 | void (*init_func)(); |
| 123 | void (*fini_func)(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 125 | #if defined(ANDROID_ARM_LINKER) |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 126 | // ARM EABI section used for stack unwinding. |
| 127 | unsigned* ARM_exidx; |
| 128 | unsigned ARM_exidx_count; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 129 | #elif defined(ANDROID_MIPS_LINKER) |
| 130 | #if 0 |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 131 | // Not yet. |
| 132 | unsigned* mips_pltgot |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 133 | #endif |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 134 | unsigned mips_symtabno; |
| 135 | unsigned mips_local_gotno; |
| 136 | unsigned mips_gotsym; |
| 137 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 138 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 139 | unsigned refcount; |
| 140 | struct link_map linkmap; |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 141 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 142 | bool constructors_called; |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 143 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 144 | // When you read a virtual address from the ELF file, add this |
| 145 | // value to get the corresponding address in the process' address space. |
| 146 | Elf32_Addr load_bias; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 147 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 148 | bool has_text_relocations; |
| 149 | bool has_DT_SYMBOLIC; |
| 150 | |
| 151 | void CallConstructors(); |
| 152 | void CallDestructors(); |
| 153 | void CallPreInitConstructors(); |
| 154 | |
| 155 | private: |
| 156 | void CallArray(const char* array_name, unsigned* array, int count, bool reverse); |
| 157 | void CallFunction(const char* function_name, void (*function)()); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 160 | extern soinfo libdl_info; |
| 161 | |
Elliott Hughes | a6a3ac5 | 2013-01-29 15:02:50 -0800 | [diff] [blame^] | 162 | #if defined(ANDROID_MIPS_LINKER) |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 163 | |
| 164 | // These aren't defined in <arch-arm/mips/elf.h>. |
| 165 | #define R_MIPS_JUMP_SLOT 127 |
| 166 | |
| 167 | #define DT_MIPS_PLTGOT 0x70000032 |
| 168 | #define DT_MIPS_RWPLT 0x70000034 |
David 'Digit' Turner | fe62de1 | 2009-12-02 10:54:53 -0800 | [diff] [blame] | 169 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 170 | #endif |
| 171 | |
Elliott Hughes | a6a3ac5 | 2013-01-29 15:02:50 -0800 | [diff] [blame^] | 172 | // These aren't defined in <sys/exec_elf.h>. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 173 | #ifndef DT_PREINIT_ARRAY |
| 174 | #define DT_PREINIT_ARRAY 32 |
| 175 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | #ifndef DT_PREINIT_ARRAYSZ |
| 177 | #define DT_PREINIT_ARRAYSZ 33 |
| 178 | #endif |
| 179 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 180 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 181 | soinfo* do_dlopen(const char* name, int flags); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 182 | int do_dlclose(soinfo* si); |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 183 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 184 | Elf32_Sym* lookup(const char* name, soinfo** found, soinfo* start); |
| 185 | soinfo* find_containing_library(const void* addr); |
| 186 | const char* linker_get_error(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 187 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 188 | Elf32_Sym* soinfo_find_symbol(soinfo* si, const void* addr); |
| 189 | Elf32_Sym* soinfo_lookup(soinfo* si, const char* name); |
| 190 | |
| 191 | void debugger_init(); |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 192 | extern "C" void notify_gdb_of_libraries(); |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 193 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 194 | #endif |