blob: b8d0587c67e2553f1828e87d24273912db66f677 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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
Nick Kralevich9ec0f032012-02-28 10:40:00 -080032#include <elf.h>
Pavel Chupinb7beb692012-08-17 12:53:29 +040033#include <link.h>
Elliott Hughes1272dbd2014-01-09 15:45:07 -080034#include <unistd.h>
Elliott Hughes46882792012-08-03 16:49:39 -070035
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070036#include "private/libc_logging.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080037
38#define DL_ERR(fmt, x...) \
39 do { \
40 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
41 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
42 DEBUG("%s\n", linker_get_error_buffer()); \
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -070043 } while (false)
44
45#define DL_WARN(fmt, x...) \
46 do { \
47 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
48 __libc_format_fd(2, "WARNING: linker: "); \
49 __libc_format_fd(2, fmt, ##x); \
50 __libc_format_fd(2, "\n"); \
51 } while (false)
52
Elliott Hughes0266ae52014-02-10 17:46:57 -080053#if defined(__LP64__)
54#define ELFW(what) ELF64_ ## what
55#else
56#define ELFW(what) ELF32_ ## what
57#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080058
Chris Dearman99186652014-02-06 20:36:51 -080059// mips64 interprets Elf64_Rel structures' r_info field differently.
60// bionic (like other C libraries) has macros that assume regular ELF files,
61// but the dynamic linker needs to be able to load mips64 ELF files.
62#if defined(__mips__) && defined(__LP64__)
63#undef ELF64_R_SYM
64#undef ELF64_R_TYPE
65#undef ELF64_R_INFO
66#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
67#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
68#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
69#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
70#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
71#endif
72
Elliott Hughes1a696162012-11-01 13:49:32 -070073// Returns the address of the page containing address 'x'.
74#define PAGE_START(x) ((x) & PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020075
Elliott Hughes1a696162012-11-01 13:49:32 -070076// Returns the offset of address 'x' in its page.
77#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020078
Elliott Hughes1a696162012-11-01 13:49:32 -070079// Returns the address of the next page after address 'x', unless 'x' is
80// itself at the start of a page.
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020081#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080082
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080083#define FLAG_LINKED 0x00000001
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080084#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080085#define FLAG_LINKER 0x00000010 // The linker itself
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
87#define SOINFO_NAME_LEN 128
88
Elliott Hughesca0c11b2013-03-12 10:40:45 -070089typedef void (*linker_function_t)();
90
Chris Dearman99186652014-02-06 20:36:51 -080091// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
92#if defined(__aarch64__) || defined(__x86_64__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070093#define USE_RELA 1
94#endif
95
Elliott Hughes18a206c2012-10-29 17:37:13 -070096struct soinfo {
Elliott Hughesca0c11b2013-03-12 10:40:45 -070097 public:
Elliott Hughesd23736e2012-11-01 15:16:56 -070098 char name[SOINFO_NAME_LEN];
Elliott Hughes0266ae52014-02-10 17:46:57 -080099 const ElfW(Phdr)* phdr;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700100 size_t phnum;
Elliott Hughes0266ae52014-02-10 17:46:57 -0800101 ElfW(Addr) entry;
102 ElfW(Addr) base;
Weiwu Chen5ceb8892013-12-03 19:47:34 +0800103 size_t size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800104
Elliott Hughesc6200592013-09-30 18:43:46 -0700105#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700106 uint32_t unused1; // DO NOT USE, maintained for compatibility.
Elliott Hughesc6200592013-09-30 18:43:46 -0700107#endif
Nick Kralevich38bccb22011-08-29 13:49:22 -0700108
Elliott Hughes0266ae52014-02-10 17:46:57 -0800109 ElfW(Dyn)* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800110
Elliott Hughesc6200592013-09-30 18:43:46 -0700111#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700112 uint32_t unused2; // DO NOT USE, maintained for compatibility
113 uint32_t unused3; // DO NOT USE, maintained for compatibility
Elliott Hughesc6200592013-09-30 18:43:46 -0700114#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800115
Elliott Hughesd23736e2012-11-01 15:16:56 -0700116 soinfo* next;
117 unsigned flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800118
Elliott Hughesd23736e2012-11-01 15:16:56 -0700119 const char* strtab;
Elliott Hughes0266ae52014-02-10 17:46:57 -0800120 ElfW(Sym)* symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800121
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700122 size_t nbucket;
123 size_t nchain;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700124 unsigned* bucket;
125 unsigned* chain;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800126
Chris Dearman99186652014-02-06 20:36:51 -0800127#if defined(__mips__) || !defined(__LP64__)
128 // This is only used by mips and mips64, but needs to be here for
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700129 // all 32-bit architectures to preserve binary compatibility.
Chris Dearman99186652014-02-06 20:36:51 -0800130 ElfW(Addr)** plt_got;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700131#endif
132
133#if defined(USE_RELA)
Elliott Hughes0266ae52014-02-10 17:46:57 -0800134 ElfW(Rela)* plt_rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700135 size_t plt_rela_count;
136
Elliott Hughes0266ae52014-02-10 17:46:57 -0800137 ElfW(Rela)* rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700138 size_t rela_count;
139#else
Elliott Hughes0266ae52014-02-10 17:46:57 -0800140 ElfW(Rel)* plt_rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700141 size_t plt_rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800142
Elliott Hughes0266ae52014-02-10 17:46:57 -0800143 ElfW(Rel)* rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700144 size_t rel_count;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700145#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800146
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700147 linker_function_t* preinit_array;
148 size_t preinit_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700150 linker_function_t* init_array;
151 size_t init_array_count;
152 linker_function_t* fini_array;
153 size_t fini_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800154
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700155 linker_function_t init_func;
156 linker_function_t fini_func;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800157
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700158#if defined(__arm__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700159 // ARM EABI section used for stack unwinding.
160 unsigned* ARM_exidx;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700161 size_t ARM_exidx_count;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700162#elif defined(__mips__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700163 unsigned mips_symtabno;
164 unsigned mips_local_gotno;
165 unsigned mips_gotsym;
166#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800167
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700168 size_t ref_count;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800169 link_map link_map_head;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400170
Elliott Hughesd23736e2012-11-01 15:16:56 -0700171 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800172
Elliott Hughesd23736e2012-11-01 15:16:56 -0700173 // When you read a virtual address from the ELF file, add this
174 // value to get the corresponding address in the process' address space.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800175 ElfW(Addr) load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200176
Elliott Hughese4d792a2013-10-28 14:19:05 -0700177#if !defined(__LP64__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700178 bool has_text_relocations;
Elliott Hughese4d792a2013-10-28 14:19:05 -0700179#endif
Elliott Hughesd23736e2012-11-01 15:16:56 -0700180 bool has_DT_SYMBOLIC;
181
182 void CallConstructors();
183 void CallDestructors();
184 void CallPreInitConstructors();
185
186 private:
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700187 void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
188 void CallFunction(const char* function_name, linker_function_t function);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189};
190
Elliott Hughese4d792a2013-10-28 14:19:05 -0700191extern soinfo libdl_info;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800192
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800193void do_android_get_LD_LIBRARY_PATH(char*, size_t);
Elliott Hughescade4c32012-12-20 14:42:14 -0800194void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Elliott Hughese66190d2012-12-18 15:57:55 -0800195soinfo* do_dlopen(const char* name, int flags);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700196int do_dlclose(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200197
Elliott Hughes0266ae52014-02-10 17:46:57 -0800198ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700199soinfo* find_containing_library(const void* addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Elliott Hughes0266ae52014-02-10 17:46:57 -0800201ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
202ElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700203
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800204void debuggerd_init();
Elliott Hughes0d787c12013-04-04 13:46:46 -0700205extern "C" abort_msg_t* gAbortMessage;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700206extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700207
Elliott Hughes650be4e2013-03-05 18:47:58 -0800208char* linker_get_error_buffer();
209size_t linker_get_error_buffer_size();
210
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800211#endif