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 | |
| 37 | #undef PAGE_MASK |
| 38 | #undef PAGE_SIZE |
| 39 | #define PAGE_SIZE 4096 |
| 40 | #define PAGE_MASK 4095 |
| 41 | |
| 42 | void debugger_init(); |
| 43 | const char *addr_to_name(unsigned addr); |
| 44 | |
| 45 | /* magic shared structures that GDB knows about */ |
| 46 | |
| 47 | struct link_map |
| 48 | { |
| 49 | uintptr_t l_addr; |
| 50 | char * l_name; |
| 51 | uintptr_t l_ld; |
| 52 | struct link_map * l_next; |
| 53 | struct link_map * l_prev; |
| 54 | }; |
| 55 | |
| 56 | /* needed for dl_iterate_phdr to be passed to the callbacks provided */ |
| 57 | struct dl_phdr_info |
| 58 | { |
| 59 | Elf32_Addr dlpi_addr; |
| 60 | const char *dlpi_name; |
| 61 | const Elf32_Phdr *dlpi_phdr; |
| 62 | Elf32_Half dlpi_phnum; |
| 63 | }; |
| 64 | |
| 65 | |
| 66 | // Values for r_debug->state |
| 67 | enum { |
| 68 | RT_CONSISTENT, |
| 69 | RT_ADD, |
| 70 | RT_DELETE |
| 71 | }; |
| 72 | |
| 73 | struct r_debug |
| 74 | { |
| 75 | int32_t r_version; |
| 76 | struct link_map * r_map; |
| 77 | void (*r_brk)(void); |
| 78 | int32_t r_state; |
| 79 | uintptr_t r_ldbase; |
| 80 | }; |
| 81 | |
| 82 | typedef struct soinfo soinfo; |
| 83 | |
| 84 | #define FLAG_LINKED 0x00000001 |
| 85 | #define FLAG_ERROR 0x00000002 |
| 86 | #define FLAG_EXE 0x00000004 // The main executable |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 87 | #define FLAG_LINKER 0x00000010 // The linker itself |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 88 | |
| 89 | #define SOINFO_NAME_LEN 128 |
| 90 | |
| 91 | struct soinfo |
| 92 | { |
| 93 | const char name[SOINFO_NAME_LEN]; |
| 94 | Elf32_Phdr *phdr; |
| 95 | int phnum; |
| 96 | unsigned entry; |
| 97 | unsigned base; |
| 98 | unsigned size; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 99 | |
Nick Kralevich | 38bccb2 | 2011-08-29 13:49:22 -0700 | [diff] [blame] | 100 | int unused; // DO NOT USE, maintained for compatibility. |
| 101 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 102 | unsigned *dynamic; |
| 103 | |
| 104 | unsigned wrprotect_start; |
| 105 | unsigned wrprotect_end; |
| 106 | |
| 107 | soinfo *next; |
| 108 | unsigned flags; |
| 109 | |
| 110 | const char *strtab; |
| 111 | Elf32_Sym *symtab; |
| 112 | |
| 113 | unsigned nbucket; |
| 114 | unsigned nchain; |
| 115 | unsigned *bucket; |
| 116 | unsigned *chain; |
| 117 | |
| 118 | unsigned *plt_got; |
| 119 | |
| 120 | Elf32_Rel *plt_rel; |
| 121 | unsigned plt_rel_count; |
| 122 | |
| 123 | Elf32_Rel *rel; |
| 124 | unsigned rel_count; |
| 125 | |
| 126 | unsigned *preinit_array; |
| 127 | unsigned preinit_array_count; |
| 128 | |
| 129 | unsigned *init_array; |
| 130 | unsigned init_array_count; |
| 131 | unsigned *fini_array; |
| 132 | unsigned fini_array_count; |
| 133 | |
| 134 | void (*init_func)(void); |
| 135 | void (*fini_func)(void); |
| 136 | |
| 137 | #ifdef ANDROID_ARM_LINKER |
| 138 | /* ARM EABI section used for stack unwinding. */ |
| 139 | unsigned *ARM_exidx; |
| 140 | unsigned ARM_exidx_count; |
| 141 | #endif |
| 142 | |
| 143 | unsigned refcount; |
| 144 | struct link_map linkmap; |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 145 | |
| 146 | int constructors_called; |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 147 | |
| 148 | Elf32_Addr gnu_relro_start; |
| 149 | unsigned gnu_relro_len; |
| 150 | |
Ji-Hwan Lee | f186a18 | 2012-05-31 20:20:36 +0900 | [diff] [blame^] | 151 | /* When you read a virtual address from the ELF file, add the load |
| 152 | * address (= "base" field) minus this value (= "load_offset") to get the |
| 153 | * real, corresponding address in the process' address space */ |
| 154 | Elf32_Addr load_offset; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | |
| 158 | extern soinfo libdl_info; |
| 159 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 160 | #ifdef ANDROID_ARM_LINKER |
| 161 | |
| 162 | #define R_ARM_COPY 20 |
| 163 | #define R_ARM_GLOB_DAT 21 |
| 164 | #define R_ARM_JUMP_SLOT 22 |
| 165 | #define R_ARM_RELATIVE 23 |
| 166 | |
David 'Digit' Turner | fe62de1 | 2009-12-02 10:54:53 -0800 | [diff] [blame] | 167 | /* According to the AAPCS specification, we only |
| 168 | * need the above relocations. However, in practice, |
| 169 | * the following ones turn up from time to time. |
| 170 | */ |
| 171 | #define R_ARM_ABS32 2 |
| 172 | #define R_ARM_REL32 3 |
| 173 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 174 | #elif defined(ANDROID_X86_LINKER) |
| 175 | |
| 176 | #define R_386_32 1 |
| 177 | #define R_386_PC32 2 |
| 178 | #define R_386_GLOB_DAT 6 |
| 179 | #define R_386_JUMP_SLOT 7 |
| 180 | #define R_386_RELATIVE 8 |
| 181 | |
David 'Digit' Turner | 70b1668 | 2012-01-30 17:17:58 +0100 | [diff] [blame] | 182 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 183 | |
| 184 | #ifndef DT_INIT_ARRAY |
| 185 | #define DT_INIT_ARRAY 25 |
| 186 | #endif |
| 187 | |
| 188 | #ifndef DT_FINI_ARRAY |
| 189 | #define DT_FINI_ARRAY 26 |
| 190 | #endif |
| 191 | |
| 192 | #ifndef DT_INIT_ARRAYSZ |
| 193 | #define DT_INIT_ARRAYSZ 27 |
| 194 | #endif |
| 195 | |
| 196 | #ifndef DT_FINI_ARRAYSZ |
| 197 | #define DT_FINI_ARRAYSZ 28 |
| 198 | #endif |
| 199 | |
| 200 | #ifndef DT_PREINIT_ARRAY |
| 201 | #define DT_PREINIT_ARRAY 32 |
| 202 | #endif |
| 203 | |
| 204 | #ifndef DT_PREINIT_ARRAYSZ |
| 205 | #define DT_PREINIT_ARRAYSZ 33 |
| 206 | #endif |
| 207 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 208 | soinfo *find_library(const char *name); |
| 209 | unsigned unload_library(soinfo *si); |
| 210 | Elf32_Sym *lookup_in_library(soinfo *si, const char *name); |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 211 | Elf32_Sym *lookup(const char *name, soinfo **found, soinfo *start); |
Mathias Agopian | bda5da0 | 2011-09-27 22:30:19 -0700 | [diff] [blame] | 212 | soinfo *find_containing_library(const void *addr); |
| 213 | Elf32_Sym *find_containing_symbol(const void *addr, soinfo *si); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 214 | const char *linker_get_error(void); |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 215 | void call_constructors_recursive(soinfo *si); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 216 | |
| 217 | #ifdef ANDROID_ARM_LINKER |
| 218 | typedef long unsigned int *_Unwind_Ptr; |
| 219 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount); |
David 'Digit' Turner | 70b1668 | 2012-01-30 17:17:58 +0100 | [diff] [blame] | 220 | #elif defined(ANDROID_X86_LINKER) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 221 | int dl_iterate_phdr(int (*cb)(struct dl_phdr_info *, size_t, void *), void *); |
| 222 | #endif |
| 223 | |
| 224 | #endif |