linker.h revision 3a9c5d66dc8d41272f51482b713717af7049697e
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 21dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Copyright (C) 2008 The Android Open Source Project 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved. 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met: 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions of source code must retain the above copyright 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer. 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions in binary form must reproduce the above copyright 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer in 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the documentation and/or other materials provided with the 131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * distribution. 141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE. 271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifndef _LINKER_H_ 301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define _LINKER_H_ 311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 329ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich#include <elf.h> 33b7beb6907599b36809effe1b4db76039ac649518Pavel Chupin#include <link.h> 341272dbd1d76c979358fff3beae9de0c1462345afElliott Hughes#include <unistd.h> 354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 368f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#include "private/libc_logging.h" 37650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 38650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes#define DL_ERR(fmt, x...) \ 39650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes do { \ 40650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \ 41650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \ 42650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes DEBUG("%s\n", linker_get_error_buffer()); \ 437e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes } while (false) 447e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes 457e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes#define DL_WARN(fmt, x...) \ 467e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes do { \ 477e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \ 487e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes __libc_format_fd(2, "WARNING: linker: "); \ 497e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes __libc_format_fd(2, fmt, ##x); \ 507e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes __libc_format_fd(2, "\n"); \ 517e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes } while (false) 527e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes 53650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 541a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the address of the page containing address 'x'. 551a6961650c82168864afe040dbdc05977db701dfElliott Hughes#define PAGE_START(x) ((x) & PAGE_MASK) 56c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 571a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the offset of address 'x' in its page. 581a6961650c82168864afe040dbdc05977db701dfElliott Hughes#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK) 59c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner 601a6961650c82168864afe040dbdc05977db701dfElliott Hughes// Returns the address of the next page after address 'x', unless 'x' is 611a6961650c82168864afe040dbdc05977db701dfElliott Hughes// itself at the start of a page. 62c1bd559d5b0fdcc25db2b6ae2705914103b24699David 'Digit' Turner#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1)) 631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FLAG_LINKED 0x00000001 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define FLAG_EXE 0x00000004 // The main executable 66468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich#define FLAG_LINKER 0x00000010 // The linker itself 671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#define SOINFO_NAME_LEN 128 691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 70ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughestypedef void (*linker_function_t)(); 71ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes 724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes// Android uses REL for 32-bit but only uses RELA for 64-bit. 734eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__) 744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#define USE_RELA 1 754eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes 7718a206c81d9743481e364384affd43306911283dElliott Hughesstruct soinfo { 78ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes public: 79d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes char name[SOINFO_NAME_LEN]; 80c620059479c47a78d57086d73726c9adc2f337adElliott Hughes const Elf_Phdr* phdr; 81ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t phnum; 82c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Addr entry; 83c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Addr base; 845ceb889ac9a36e74e4e00c34c57cbb58d73e228cWeiwu Chen size_t size; 851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 86c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#ifndef __LP64__ 87ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes uint32_t unused1; // DO NOT USE, maintained for compatibility. 88c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#endif 8938bccb271f121fc06eaa0d8fbd3c982bc44c36b7Nick Kralevich 90c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Dyn* dynamic; 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 92c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#ifndef __LP64__ 93ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes uint32_t unused2; // DO NOT USE, maintained for compatibility 94ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes uint32_t unused3; // DO NOT USE, maintained for compatibility 95c620059479c47a78d57086d73726c9adc2f337adElliott Hughes#endif 961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 97d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* next; 98d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned flags; 991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 100d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes const char* strtab; 101c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Sym* symtab; 1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 103ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t nbucket; 104ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t nchain; 105d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned* bucket; 106d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned* chain; 1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1084eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__) 1094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // This is only used by 32-bit MIPS, but needs to be here for 1104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // all 32-bit architectures to preserve binary compatibility. 1114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes unsigned* plt_got; 1124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1134eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes 1144eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes Elf_Rela* plt_rela; 116c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes size_t plt_rela_count; 117c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes Elf_Rela* rela; 119c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes size_t rela_count; 120c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 121c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Rel* plt_rel; 122ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t plt_rel_count; 1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 124c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Rel* rel; 125ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t rel_count; 126c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 1271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 128ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes linker_function_t* preinit_array; 129ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t preinit_array_count; 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 131ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes linker_function_t* init_array; 132ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t init_array_count; 133ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes linker_function_t* fini_array; 134ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t fini_array_count; 1351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 136ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes linker_function_t init_func; 137ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes linker_function_t fini_func; 1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 140d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // ARM EABI section used for stack unwinding. 141d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned* ARM_exidx; 142ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t ARM_exidx_count; 1434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__) 144d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned mips_symtabno; 145d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned mips_local_gotno; 146d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes unsigned mips_gotsym; 1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 149ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes size_t ref_count; 1503a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes link_map link_map_head; 151e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 152d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes bool constructors_called; 1539ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich 154d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // When you read a virtual address from the ELF file, add this 155d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // value to get the corresponding address in the process' address space. 156c620059479c47a78d57086d73726c9adc2f337adElliott Hughes Elf_Addr load_bias; 1575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 158e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__) 159d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes bool has_text_relocations; 160e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 161d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes bool has_DT_SYMBOLIC; 162d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 163d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes void CallConstructors(); 164d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes void CallDestructors(); 165d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes void CallPreInitConstructors(); 166d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 167d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes private: 168ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse); 169ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes void CallFunction(const char* function_name, linker_function_t function); 1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}; 1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 172e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughesextern soinfo libdl_info; 1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 174a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char*, size_t); 175cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); 176e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughessoinfo* do_dlopen(const char* name, int flags); 177d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si); 17816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner 179c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start); 180d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughessoinfo* find_containing_library(const void* addr); 1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 182c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dladdr_find_symbol(soinfo* si, const void* addr); 183c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dlsym_handle_lookup(soinfo* si, const char* name); 184d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 185d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromvoid debuggerd_init(); 1860d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughesextern "C" abort_msg_t* gAbortMessage; 18718a206c81d9743481e364384affd43306911283dElliott Hughesextern "C" void notify_gdb_of_libraries(); 1884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 189650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer(); 190650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size(); 191650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 193