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