linker.h revision cade4c36e7c9c62db3f476a0f9cfc329bac9acb7
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 <elf.h> 35#include <sys/exec_elf.h> 36 37#include <link.h> 38 39// Returns the address of the page containing address 'x'. 40#define PAGE_START(x) ((x) & PAGE_MASK) 41 42// Returns the offset of address 'x' in its page. 43#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK) 44 45// Returns the address of the next page after address 'x', unless 'x' is 46// itself at the start of a page. 47#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) 48 49// Magic shared structures that GDB knows about. 50 51struct 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; 57}; 58 59// Values for r_debug->state 60enum { 61 RT_CONSISTENT, 62 RT_ADD, 63 RT_DELETE 64}; 65 66struct 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; 72}; 73 74#define FLAG_LINKED 0x00000001 75#define FLAG_EXE 0x00000004 // The main executable 76#define FLAG_LINKER 0x00000010 // The linker itself 77 78#define SOINFO_NAME_LEN 128 79 80struct soinfo { 81 char name[SOINFO_NAME_LEN]; 82 const Elf32_Phdr* phdr; 83 int phnum; 84 unsigned entry; 85 unsigned base; 86 unsigned size; 87 88 int unused; // DO NOT USE, maintained for compatibility. 89 90 unsigned* dynamic; 91 92 unsigned unused2; // DO NOT USE, maintained for compatibility 93 unsigned unused3; // DO NOT USE, maintained for compatibility 94 95 soinfo* next; 96 unsigned flags; 97 98 const char* strtab; 99 Elf32_Sym* symtab; 100 101 unsigned nbucket; 102 unsigned nchain; 103 unsigned* bucket; 104 unsigned* chain; 105 106 unsigned* plt_got; 107 108 Elf32_Rel* plt_rel; 109 unsigned plt_rel_count; 110 111 Elf32_Rel* rel; 112 unsigned rel_count; 113 114 unsigned* preinit_array; 115 unsigned preinit_array_count; 116 117 unsigned* init_array; 118 unsigned init_array_count; 119 unsigned* fini_array; 120 unsigned fini_array_count; 121 122 void (*init_func)(); 123 void (*fini_func)(); 124 125#if defined(ANDROID_ARM_LINKER) 126 // ARM EABI section used for stack unwinding. 127 unsigned* ARM_exidx; 128 unsigned ARM_exidx_count; 129#elif defined(ANDROID_MIPS_LINKER) 130#if 0 131 // Not yet. 132 unsigned* mips_pltgot 133#endif 134 unsigned mips_symtabno; 135 unsigned mips_local_gotno; 136 unsigned mips_gotsym; 137#endif 138 139 unsigned refcount; 140 struct link_map linkmap; 141 142 bool constructors_called; 143 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; 147 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)()); 158}; 159 160extern soinfo libdl_info; 161 162#if defined(ANDROID_ARM_LINKER) 163 164// These aren't defined in <arch-arm/asm/elf.h>. 165#define R_ARM_REL32 3 166#define R_ARM_COPY 20 167#define R_ARM_GLOB_DAT 21 168#define R_ARM_JUMP_SLOT 22 169#define R_ARM_RELATIVE 23 170 171#elif defined(ANDROID_MIPS_LINKER) 172 173// These aren't defined in <arch-arm/mips/elf.h>. 174#define R_MIPS_JUMP_SLOT 127 175 176#define DT_MIPS_PLTGOT 0x70000032 177#define DT_MIPS_RWPLT 0x70000034 178 179#elif defined(ANDROID_X86_LINKER) 180 181// x86 has everything it needs in <arch-arm/x86/elf.h>. 182 183#endif /* ANDROID_*_LINKER */ 184 185#ifndef DT_INIT_ARRAY 186#define DT_INIT_ARRAY 25 187#endif 188 189#ifndef DT_FINI_ARRAY 190#define DT_FINI_ARRAY 26 191#endif 192 193#ifndef DT_INIT_ARRAYSZ 194#define DT_INIT_ARRAYSZ 27 195#endif 196 197#ifndef DT_FINI_ARRAYSZ 198#define DT_FINI_ARRAYSZ 28 199#endif 200 201#ifndef DT_PREINIT_ARRAY 202#define DT_PREINIT_ARRAY 32 203#endif 204 205#ifndef DT_PREINIT_ARRAYSZ 206#define DT_PREINIT_ARRAYSZ 33 207#endif 208 209void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); 210soinfo* do_dlopen(const char* name, int flags); 211int do_dlclose(soinfo* si); 212 213Elf32_Sym* lookup(const char* name, soinfo** found, soinfo* start); 214soinfo* find_containing_library(const void* addr); 215const char* linker_get_error(); 216 217Elf32_Sym* soinfo_find_symbol(soinfo* si, const void* addr); 218Elf32_Sym* soinfo_lookup(soinfo* si, const char* name); 219 220void debugger_init(); 221extern "C" void notify_gdb_of_libraries(); 222 223#endif 224