linker.h revision c620059479c47a78d57086d73726c9adc2f337ad
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#include "private/libc_logging.h" 40 41#define DL_ERR(fmt, x...) \ 42 do { \ 43 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \ 44 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \ 45 DEBUG("%s\n", linker_get_error_buffer()); \ 46 } while (false) 47 48#define DL_WARN(fmt, x...) \ 49 do { \ 50 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \ 51 __libc_format_fd(2, "WARNING: linker: "); \ 52 __libc_format_fd(2, fmt, ##x); \ 53 __libc_format_fd(2, "\n"); \ 54 } while (false) 55 56 57// Returns the address of the page containing address 'x'. 58#define PAGE_START(x) ((x) & PAGE_MASK) 59 60// Returns the offset of address 'x' in its page. 61#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK) 62 63// Returns the address of the next page after address 'x', unless 'x' is 64// itself at the start of a page. 65#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) 66 67// Magic shared structures that GDB knows about. 68 69struct link_map_t { 70 uintptr_t l_addr; 71 char* l_name; 72 uintptr_t l_ld; 73 link_map_t* l_next; 74 link_map_t* l_prev; 75}; 76 77// Values for r_debug->state 78enum { 79 RT_CONSISTENT, 80 RT_ADD, 81 RT_DELETE 82}; 83 84struct r_debug { 85 int32_t r_version; 86 link_map_t* r_map; 87 void (*r_brk)(void); 88 int32_t r_state; 89 uintptr_t r_ldbase; 90}; 91 92#define FLAG_LINKED 0x00000001 93#define FLAG_EXE 0x00000004 // The main executable 94#define FLAG_LINKER 0x00000010 // The linker itself 95 96#define SOINFO_NAME_LEN 128 97 98typedef void (*linker_function_t)(); 99 100struct soinfo { 101 public: 102 char name[SOINFO_NAME_LEN]; 103 const Elf_Phdr* phdr; 104 size_t phnum; 105 Elf_Addr entry; 106 Elf_Addr base; 107 unsigned size; 108 109#ifndef __LP64__ 110 uint32_t unused1; // DO NOT USE, maintained for compatibility. 111#endif 112 113 Elf_Dyn* dynamic; 114 115#ifndef __LP64__ 116 uint32_t unused2; // DO NOT USE, maintained for compatibility 117 uint32_t unused3; // DO NOT USE, maintained for compatibility 118#endif 119 120 soinfo* next; 121 unsigned flags; 122 123 const char* strtab; 124 Elf_Sym* symtab; 125 126 size_t nbucket; 127 size_t nchain; 128 unsigned* bucket; 129 unsigned* chain; 130 131 unsigned* plt_got; 132 133 Elf_Rel* plt_rel; 134 size_t plt_rel_count; 135 136 Elf_Rel* rel; 137 size_t rel_count; 138 139 linker_function_t* preinit_array; 140 size_t preinit_array_count; 141 142 linker_function_t* init_array; 143 size_t init_array_count; 144 linker_function_t* fini_array; 145 size_t fini_array_count; 146 147 linker_function_t init_func; 148 linker_function_t fini_func; 149 150#if defined(ANDROID_ARM_LINKER) 151 // ARM EABI section used for stack unwinding. 152 unsigned* ARM_exidx; 153 size_t ARM_exidx_count; 154#elif defined(ANDROID_MIPS_LINKER) 155 unsigned mips_symtabno; 156 unsigned mips_local_gotno; 157 unsigned mips_gotsym; 158#endif 159 160 size_t ref_count; 161 link_map_t link_map; 162 163 bool constructors_called; 164 165 // When you read a virtual address from the ELF file, add this 166 // value to get the corresponding address in the process' address space. 167 Elf_Addr load_bias; 168 169 bool has_text_relocations; 170 bool has_DT_SYMBOLIC; 171 172 void CallConstructors(); 173 void CallDestructors(); 174 void CallPreInitConstructors(); 175 176 private: 177 void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse); 178 void CallFunction(const char* function_name, linker_function_t function); 179}; 180 181extern soinfo libdl_info; 182 183// These aren't defined in <sys/exec_elf.h>. 184#ifndef DT_PREINIT_ARRAY 185#define DT_PREINIT_ARRAY 32 186#endif 187#ifndef DT_PREINIT_ARRAYSZ 188#define DT_PREINIT_ARRAYSZ 33 189#endif 190 191void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); 192soinfo* do_dlopen(const char* name, int flags); 193int do_dlclose(soinfo* si); 194 195Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start); 196soinfo* find_containing_library(const void* addr); 197 198Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr); 199Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name); 200 201void debuggerd_init(); 202extern "C" abort_msg_t* gAbortMessage; 203extern "C" void notify_gdb_of_libraries(); 204 205char* linker_get_error_buffer(); 206size_t linker_get_error_buffer_size(); 207 208#endif 209