2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINKER_H_
|
|
|
|
#define _LINKER_H_
|
|
|
|
|
2012-02-28 19:40:00 +01:00
|
|
|
#include <elf.h>
|
2012-08-17 10:53:29 +02:00
|
|
|
#include <link.h>
|
2014-01-10 00:45:07 +01:00
|
|
|
#include <unistd.h>
|
2014-02-06 15:34:21 +01:00
|
|
|
#include <android/dlext.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2013-03-15 23:30:25 +01:00
|
|
|
#include "private/libc_logging.h"
|
2013-03-06 03:47:58 +01:00
|
|
|
|
|
|
|
#define DL_ERR(fmt, x...) \
|
|
|
|
do { \
|
|
|
|
__libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
|
|
|
|
/* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
|
|
|
|
DEBUG("%s\n", linker_get_error_buffer()); \
|
2013-06-18 22:15:00 +02:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define DL_WARN(fmt, x...) \
|
|
|
|
do { \
|
|
|
|
__libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
|
|
|
|
__libc_format_fd(2, "WARNING: linker: "); \
|
|
|
|
__libc_format_fd(2, fmt, ##x); \
|
|
|
|
__libc_format_fd(2, "\n"); \
|
|
|
|
} while (false)
|
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define ELFW(what) ELF64_ ## what
|
|
|
|
#else
|
|
|
|
#define ELFW(what) ELF32_ ## what
|
|
|
|
#endif
|
2013-03-06 03:47:58 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
// mips64 interprets Elf64_Rel structures' r_info field differently.
|
|
|
|
// bionic (like other C libraries) has macros that assume regular ELF files,
|
|
|
|
// but the dynamic linker needs to be able to load mips64 ELF files.
|
|
|
|
#if defined(__mips__) && defined(__LP64__)
|
|
|
|
#undef ELF64_R_SYM
|
|
|
|
#undef ELF64_R_TYPE
|
|
|
|
#undef ELF64_R_INFO
|
|
|
|
#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
|
|
|
|
#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
|
|
|
|
#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
|
|
|
|
#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
|
|
|
|
#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
|
|
|
|
#endif
|
|
|
|
|
2012-11-01 21:49:32 +01:00
|
|
|
// Returns the address of the page containing address 'x'.
|
|
|
|
#define PAGE_START(x) ((x) & PAGE_MASK)
|
2012-06-19 11:21:29 +02:00
|
|
|
|
2012-11-01 21:49:32 +01:00
|
|
|
// Returns the offset of address 'x' in its page.
|
|
|
|
#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
|
2012-06-19 11:21:29 +02:00
|
|
|
|
2012-11-01 21:49:32 +01:00
|
|
|
// Returns the address of the next page after address 'x', unless 'x' is
|
|
|
|
// itself at the start of a page.
|
2012-06-19 11:21:29 +02:00
|
|
|
#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
#define FLAG_LINKED 0x00000001
|
|
|
|
#define FLAG_EXE 0x00000004 // The main executable
|
2011-11-12 00:53:17 +01:00
|
|
|
#define FLAG_LINKER 0x00000010 // The linker itself
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
#define SOINFO_NAME_LEN 128
|
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
typedef void (*linker_function_t)();
|
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
|
|
|
|
#if defined(__aarch64__) || defined(__x86_64__)
|
2013-10-26 02:38:02 +02:00
|
|
|
#define USE_RELA 1
|
|
|
|
#endif
|
|
|
|
|
2012-10-30 01:37:13 +01:00
|
|
|
struct soinfo {
|
2013-03-12 18:40:45 +01:00
|
|
|
public:
|
2012-11-01 23:16:56 +01:00
|
|
|
char name[SOINFO_NAME_LEN];
|
2014-02-11 02:46:57 +01:00
|
|
|
const ElfW(Phdr)* phdr;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t phnum;
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) entry;
|
|
|
|
ElfW(Addr) base;
|
2013-12-03 12:47:34 +01:00
|
|
|
size_t size;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-10-01 03:43:46 +02:00
|
|
|
#ifndef __LP64__
|
2013-03-12 18:40:45 +01:00
|
|
|
uint32_t unused1; // DO NOT USE, maintained for compatibility.
|
2013-10-01 03:43:46 +02:00
|
|
|
#endif
|
2011-08-29 22:49:22 +02:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Dyn)* dynamic;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-10-01 03:43:46 +02:00
|
|
|
#ifndef __LP64__
|
2013-03-12 18:40:45 +01:00
|
|
|
uint32_t unused2; // DO NOT USE, maintained for compatibility
|
|
|
|
uint32_t unused3; // DO NOT USE, maintained for compatibility
|
2013-10-01 03:43:46 +02:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
soinfo* next;
|
|
|
|
unsigned flags;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
const char* strtab;
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Sym)* symtab;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t nbucket;
|
|
|
|
size_t nchain;
|
2012-11-01 23:16:56 +01:00
|
|
|
unsigned* bucket;
|
|
|
|
unsigned* chain;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
#if defined(__mips__) || !defined(__LP64__)
|
|
|
|
// This is only used by mips and mips64, but needs to be here for
|
2013-10-26 02:38:02 +02:00
|
|
|
// all 32-bit architectures to preserve binary compatibility.
|
2014-02-07 05:36:51 +01:00
|
|
|
ElfW(Addr)** plt_got;
|
2013-10-26 02:38:02 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_RELA)
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Rela)* plt_rela;
|
2013-10-05 02:01:33 +02:00
|
|
|
size_t plt_rela_count;
|
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Rela)* rela;
|
2013-10-05 02:01:33 +02:00
|
|
|
size_t rela_count;
|
|
|
|
#else
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Rel)* plt_rel;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t plt_rel_count;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Rel)* rel;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t rel_count;
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
linker_function_t* preinit_array;
|
|
|
|
size_t preinit_array_count;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
linker_function_t* init_array;
|
|
|
|
size_t init_array_count;
|
|
|
|
linker_function_t* fini_array;
|
|
|
|
size_t fini_array_count;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
linker_function_t init_func;
|
|
|
|
linker_function_t fini_func;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__arm__)
|
2012-11-01 23:16:56 +01:00
|
|
|
// ARM EABI section used for stack unwinding.
|
|
|
|
unsigned* ARM_exidx;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t ARM_exidx_count;
|
2013-10-26 02:38:02 +02:00
|
|
|
#elif defined(__mips__)
|
2012-11-01 23:16:56 +01:00
|
|
|
unsigned mips_symtabno;
|
|
|
|
unsigned mips_local_gotno;
|
|
|
|
unsigned mips_gotsym;
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t ref_count;
|
2014-02-10 22:31:13 +01:00
|
|
|
link_map link_map_head;
|
2011-12-21 10:03:54 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
bool constructors_called;
|
2012-02-28 19:40:00 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
// When you read a virtual address from the ELF file, add this
|
|
|
|
// value to get the corresponding address in the process' address space.
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) load_bias;
|
2012-08-30 12:48:32 +02:00
|
|
|
|
2013-10-28 22:19:05 +01:00
|
|
|
#if !defined(__LP64__)
|
2012-11-01 23:16:56 +01:00
|
|
|
bool has_text_relocations;
|
2013-10-28 22:19:05 +01:00
|
|
|
#endif
|
2012-11-01 23:16:56 +01:00
|
|
|
bool has_DT_SYMBOLIC;
|
|
|
|
|
|
|
|
void CallConstructors();
|
|
|
|
void CallDestructors();
|
|
|
|
void CallPreInitConstructors();
|
|
|
|
|
|
|
|
private:
|
2013-03-12 18:40:45 +01:00
|
|
|
void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
|
|
|
|
void CallFunction(const char* function_name, linker_function_t function);
|
2009-03-04 04:28:35 +01:00
|
|
|
};
|
|
|
|
|
2013-10-28 22:19:05 +01:00
|
|
|
extern soinfo libdl_info;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-01-14 01:37:47 +01:00
|
|
|
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
|
2012-12-20 23:42:14 +01:00
|
|
|
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
|
2014-02-06 15:34:21 +01:00
|
|
|
soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
|
2012-11-01 23:16:56 +01:00
|
|
|
int do_dlclose(soinfo* si);
|
2012-06-12 16:25:37 +02:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
|
2012-11-01 23:16:56 +01:00
|
|
|
soinfo* find_containing_library(const void* addr);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
|
|
|
|
ElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name);
|
2012-11-01 23:16:56 +01:00
|
|
|
|
2013-03-01 00:58:45 +01:00
|
|
|
void debuggerd_init();
|
2014-05-14 19:02:03 +02:00
|
|
|
extern "C" abort_msg_t* g_abort_message;
|
2012-10-30 01:37:13 +01:00
|
|
|
extern "C" void notify_gdb_of_libraries();
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2013-03-06 03:47:58 +01:00
|
|
|
char* linker_get_error_buffer();
|
|
|
|
size_t linker_get_error_buffer_size();
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|