blob: 222aca11ed15432990fd43dabbe8943a21f49c39 [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>
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -070033#include <inttypes.h>
Pavel Chupinb7beb692012-08-17 12:53:29 +040034#include <link.h>
Elliott Hughes1272dbd2014-01-09 15:45:07 -080035#include <unistd.h>
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000036#include <android/dlext.h>
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070037#include <sys/stat.h>
Elliott Hughes46882792012-08-03 16:49:39 -070038
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070039#include "private/libc_logging.h"
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070040#include "linked_list.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080041
42#define DL_ERR(fmt, x...) \
43 do { \
44 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
45 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
46 DEBUG("%s\n", linker_get_error_buffer()); \
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -070047 } while (false)
48
49#define DL_WARN(fmt, x...) \
50 do { \
51 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
52 __libc_format_fd(2, "WARNING: linker: "); \
53 __libc_format_fd(2, fmt, ##x); \
54 __libc_format_fd(2, "\n"); \
55 } while (false)
56
Elliott Hughes0266ae52014-02-10 17:46:57 -080057#if defined(__LP64__)
58#define ELFW(what) ELF64_ ## what
59#else
60#define ELFW(what) ELF32_ ## what
61#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080062
Chris Dearman99186652014-02-06 20:36:51 -080063// mips64 interprets Elf64_Rel structures' r_info field differently.
64// bionic (like other C libraries) has macros that assume regular ELF files,
65// but the dynamic linker needs to be able to load mips64 ELF files.
66#if defined(__mips__) && defined(__LP64__)
67#undef ELF64_R_SYM
68#undef ELF64_R_TYPE
69#undef ELF64_R_INFO
70#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
71#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
72#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
73#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
74#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
75#endif
76
Elliott Hughes1a696162012-11-01 13:49:32 -070077// Returns the address of the page containing address 'x'.
78#define PAGE_START(x) ((x) & PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020079
Elliott Hughes1a696162012-11-01 13:49:32 -070080// Returns the offset of address 'x' in its page.
81#define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020082
Elliott Hughes1a696162012-11-01 13:49:32 -070083// Returns the address of the next page after address 'x', unless 'x' is
84// itself at the start of a page.
David 'Digit' Turnerc1bd5592012-06-19 11:21:29 +020085#define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080086
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080087#define FLAG_LINKED 0x00000001
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080088#define FLAG_EXE 0x00000004 // The main executable
Nick Kralevich468319c2011-11-11 15:53:17 -080089#define FLAG_LINKER 0x00000010 // The linker itself
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070090#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080091
Dmitriy Ivanovf947be22014-11-03 21:14:07 -080092#define SOINFO_VERSION 0
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -070093
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080094#define SOINFO_NAME_LEN 128
95
Elliott Hughesca0c11b2013-03-12 10:40:45 -070096typedef void (*linker_function_t)();
97
Chris Dearman99186652014-02-06 20:36:51 -080098// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
99#if defined(__aarch64__) || defined(__x86_64__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700100#define USE_RELA 1
101#endif
102
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700103struct soinfo;
104
105class SoinfoListAllocator {
106public:
107 static LinkedListEntry<soinfo>* alloc();
108 static void free(LinkedListEntry<soinfo>* entry);
109private:
110 // unconstructable
111 DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
112};
113
Elliott Hughes18a206c2012-10-29 17:37:13 -0700114struct soinfo {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700115 public:
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700116 typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
117 public:
Elliott Hughesd23736e2012-11-01 15:16:56 -0700118 char name[SOINFO_NAME_LEN];
Elliott Hughes0266ae52014-02-10 17:46:57 -0800119 const ElfW(Phdr)* phdr;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700120 size_t phnum;
Elliott Hughes0266ae52014-02-10 17:46:57 -0800121 ElfW(Addr) entry;
122 ElfW(Addr) base;
Weiwu Chen5ceb8892013-12-03 19:47:34 +0800123 size_t size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800124
Elliott Hughesc6200592013-09-30 18:43:46 -0700125#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700126 uint32_t unused1; // DO NOT USE, maintained for compatibility.
Elliott Hughesc6200592013-09-30 18:43:46 -0700127#endif
Nick Kralevich38bccb22011-08-29 13:49:22 -0700128
Elliott Hughes0266ae52014-02-10 17:46:57 -0800129 ElfW(Dyn)* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800130
Elliott Hughesc6200592013-09-30 18:43:46 -0700131#ifndef __LP64__
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700132 uint32_t unused2; // DO NOT USE, maintained for compatibility
133 uint32_t unused3; // DO NOT USE, maintained for compatibility
Elliott Hughesc6200592013-09-30 18:43:46 -0700134#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800135
Elliott Hughesd23736e2012-11-01 15:16:56 -0700136 soinfo* next;
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700137 uint32_t flags;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800138
Dmitriy Ivanov0f47d9c2014-09-29 19:14:45 -0700139 private:
Elliott Hughesd23736e2012-11-01 15:16:56 -0700140 const char* strtab;
Dmitriy Ivanov0f47d9c2014-09-29 19:14:45 -0700141 public:
Elliott Hughes0266ae52014-02-10 17:46:57 -0800142 ElfW(Sym)* symtab;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800143
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700144 size_t nbucket;
145 size_t nchain;
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700146 uint32_t* bucket;
147 uint32_t* chain;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800148
Chris Dearman99186652014-02-06 20:36:51 -0800149#if defined(__mips__) || !defined(__LP64__)
150 // This is only used by mips and mips64, but needs to be here for
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700151 // all 32-bit architectures to preserve binary compatibility.
Chris Dearman99186652014-02-06 20:36:51 -0800152 ElfW(Addr)** plt_got;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700153#endif
154
155#if defined(USE_RELA)
Elliott Hughes0266ae52014-02-10 17:46:57 -0800156 ElfW(Rela)* plt_rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700157 size_t plt_rela_count;
158
Elliott Hughes0266ae52014-02-10 17:46:57 -0800159 ElfW(Rela)* rela;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700160 size_t rela_count;
161#else
Elliott Hughes0266ae52014-02-10 17:46:57 -0800162 ElfW(Rel)* plt_rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700163 size_t plt_rel_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800164
Elliott Hughes0266ae52014-02-10 17:46:57 -0800165 ElfW(Rel)* rel;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700166 size_t rel_count;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700167#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800168
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700169 linker_function_t* preinit_array;
170 size_t preinit_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800171
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700172 linker_function_t* init_array;
173 size_t init_array_count;
174 linker_function_t* fini_array;
175 size_t fini_array_count;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800176
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700177 linker_function_t init_func;
178 linker_function_t fini_func;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800179
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700180#if defined(__arm__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700181 // ARM EABI section used for stack unwinding.
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700182 uint32_t* ARM_exidx;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700183 size_t ARM_exidx_count;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700184#elif defined(__mips__)
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700185 uint32_t mips_symtabno;
186 uint32_t mips_local_gotno;
187 uint32_t mips_gotsym;
Elliott Hughesd23736e2012-11-01 15:16:56 -0700188#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800189
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700190 size_t ref_count;
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800191 link_map link_map_head;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400192
Elliott Hughesd23736e2012-11-01 15:16:56 -0700193 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800194
Elliott Hughesd23736e2012-11-01 15:16:56 -0700195 // When you read a virtual address from the ELF file, add this
196 // value to get the corresponding address in the process' address space.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800197 ElfW(Addr) load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200198
Elliott Hughese4d792a2013-10-28 14:19:05 -0700199#if !defined(__LP64__)
Elliott Hughesd23736e2012-11-01 15:16:56 -0700200 bool has_text_relocations;
Elliott Hughese4d792a2013-10-28 14:19:05 -0700201#endif
Dmitriy Ivanovf90e2102014-09-29 12:10:36 -0700202 bool has_DT_SYMBOLIC;
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700203
Dmitriy Ivanovc85e82d2014-09-15 17:00:10 -0700204 soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags);
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700205
Elliott Hughesd23736e2012-11-01 15:16:56 -0700206 void CallConstructors();
207 void CallDestructors();
208 void CallPreInitConstructors();
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700209 bool PrelinkImage();
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800210 bool LinkImage(const soinfo_list_t& local_group, const android_dlextinfo* extinfo);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700211
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700212 void add_child(soinfo* child);
213 void remove_all_links();
214
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800215 ino_t get_st_ino();
216 dev_t get_st_dev();
217 off64_t get_file_offset();
Brigid Smith31b88da2014-07-23 11:22:25 -0700218
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800219 int get_rtld_flags();
Dmitriy Ivanovc85e82d2014-09-15 17:00:10 -0700220
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700221 soinfo_list_t& get_children();
Dmitriy Ivanovae69a952014-09-05 16:42:53 -0700222 soinfo_list_t& get_parents();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700223
Dmitriy Ivanovf4cb6312014-09-11 15:16:03 -0700224 ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
225
Dmitriy Ivanov0f47d9c2014-09-29 19:14:45 -0700226 const char* get_string(ElfW(Word) index) const;
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700227 bool can_unload() const;
Dmitriy Ivanov0f47d9c2014-09-29 19:14:45 -0700228
229 bool inline has_min_version(uint32_t min_version) const {
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700230 return (flags & FLAG_NEW_SOINFO) != 0 && version >= min_version;
231 }
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700232
Elliott Hughesd23736e2012-11-01 15:16:56 -0700233 private:
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700234 void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
235 void CallFunction(const char* function_name, linker_function_t function);
Dmitriy Ivanov7210c412014-09-02 11:47:23 -0700236#if defined(USE_RELA)
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800237 int Relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& local_group);
Dmitriy Ivanov7210c412014-09-02 11:47:23 -0700238#else
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800239 int Relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& local_group);
Dmitriy Ivanov7210c412014-09-02 11:47:23 -0700240#endif
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700241
242 private:
243 // This part of the structure is only available
244 // when FLAG_NEW_SOINFO is set in this->flags.
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700245 uint32_t version;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700246
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700247 // version >= 0
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700248 dev_t st_dev;
249 ino_t st_ino;
250
251 // dependency graph
252 soinfo_list_t children;
253 soinfo_list_t parents;
254
Dmitriy Ivanov3bbd2182014-08-22 12:25:04 -0700255 // version >= 1
Dmitriy Ivanovde017802014-10-03 17:52:44 -0700256 off64_t file_offset;
Dmitriy Ivanovf947be22014-11-03 21:14:07 -0800257 int rtld_flags;
Dmitriy Ivanov0f47d9c2014-09-29 19:14:45 -0700258 size_t strtab_size;
259
260 friend soinfo* get_libdl_info();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800261};
262
Dmitriy Ivanovc87f65d2014-05-19 15:06:58 -0700263soinfo* get_libdl_info();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800264
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800265void do_android_get_LD_LIBRARY_PATH(char*, size_t);
Elliott Hughescade4c32012-12-20 14:42:14 -0800266void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Elliott Hughes1a586292014-06-03 16:23:08 -0700267soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700268void do_dlclose(soinfo* si);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200269
Dmitriy Ivanov94194202014-08-18 15:08:51 -0700270ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700271soinfo* find_containing_library(const void* addr);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800272
Elliott Hughes0266ae52014-02-10 17:46:57 -0800273ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
Dmitriy Ivanov94194202014-08-18 15:08:51 -0700274ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700275
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800276void debuggerd_init();
Elliott Hughes1728b232014-05-14 10:02:03 -0700277extern "C" abort_msg_t* g_abort_message;
Elliott Hughes18a206c2012-10-29 17:37:13 -0700278extern "C" void notify_gdb_of_libraries();
Elliott Hughes46882792012-08-03 16:49:39 -0700279
Elliott Hughes650be4e2013-03-05 18:47:58 -0800280char* linker_get_error_buffer();
281size_t linker_get_error_buffer_size();
282
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283#endif