blob: d8c0e1970b611217fa20af370f2381e4857d4a05 [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
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -080032#include <dlfcn.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070033#include <android/dlext.h>
Nick Kralevich9ec0f032012-02-28 10:40:00 -080034#include <elf.h>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070035#include <inttypes.h>
Pavel Chupinb7beb692012-08-17 12:53:29 +040036#include <link.h>
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070037#include <sys/stat.h>
Elliott Hughesafab3ff2015-07-28 14:58:37 -070038#include <unistd.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039
Elliott Hughesafab3ff2015-07-28 14:58:37 -070040#include "private/bionic_page.h"
Elliott Hughes8f2a5a02013-03-15 15:30:25 -070041#include "private/libc_logging.h"
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070042#include "linked_list.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070043#include "linker_logger.h"
Elliott Hughes650be4e2013-03-05 18:47:58 -080044
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070045#include <string>
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070046#include <vector>
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070047
Elliott Hughes650be4e2013-03-05 18:47:58 -080048#define DL_ERR(fmt, x...) \
49 do { \
50 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
51 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
Dimitry Ivanovb996d602016-07-11 18:11:39 -070052 LD_LOG(kLogErrors, "%s\n", linker_get_error_buffer()); \
Elliott Hughes7e5a8cc2013-06-18 13:15:00 -070053 } while (false)
54
55#define DL_WARN(fmt, x...) \
56 do { \
57 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
58 __libc_format_fd(2, "WARNING: linker: "); \
59 __libc_format_fd(2, fmt, ##x); \
60 __libc_format_fd(2, "\n"); \
61 } while (false)
62
Elliott Hughes0266ae52014-02-10 17:46:57 -080063#if defined(__LP64__)
64#define ELFW(what) ELF64_ ## what
65#else
66#define ELFW(what) ELF32_ ## what
67#endif
Elliott Hughes650be4e2013-03-05 18:47:58 -080068
Chris Dearman99186652014-02-06 20:36:51 -080069// mips64 interprets Elf64_Rel structures' r_info field differently.
70// bionic (like other C libraries) has macros that assume regular ELF files,
71// but the dynamic linker needs to be able to load mips64 ELF files.
72#if defined(__mips__) && defined(__LP64__)
73#undef ELF64_R_SYM
74#undef ELF64_R_TYPE
75#undef ELF64_R_INFO
76#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
77#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
78#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
79#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
80#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
81#endif
82
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -080083#define FLAG_LINKED 0x00000001
84#define FLAG_EXE 0x00000004 // The main executable
85#define FLAG_LINKER 0x00000010 // The linker itself
86#define FLAG_GNU_HASH 0x00000040 // uses gnu hash
87#define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
88 // and should not be unmapped
89#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080090
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -070091#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
92
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070093#define SOINFO_VERSION 3
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070094
Dmitriy Ivanov280d5462015-09-28 10:14:17 -070095#if defined(__work_around_b_24465209__)
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080096#define SOINFO_NAME_LEN 128
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070097#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080098
Dimitry Ivanov55437462016-07-20 15:33:07 -070099typedef void (*linker_dtor_function_t)();
100typedef void (*linker_ctor_function_t)(int, char**, char**);
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700101
Chris Dearman99186652014-02-06 20:36:51 -0800102// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
103#if defined(__aarch64__) || defined(__x86_64__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700104#define USE_RELA 1
105#endif
106
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700107struct soinfo;
108
109class SoinfoListAllocator {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800110 public:
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700111 static LinkedListEntry<soinfo>* alloc();
112 static void free(LinkedListEntry<soinfo>* entry);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800113
114 private:
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700115 // unconstructable
116 DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
117};
118
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700119class NamespaceListAllocator {
120 public:
121 static LinkedListEntry<android_namespace_t>* alloc();
122 static void free(LinkedListEntry<android_namespace_t>* entry);
123
124 private:
125 // unconstructable
126 DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceListAllocator);
127};
128
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800129class SymbolName {
130 public:
131 explicit SymbolName(const char* name)
132 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
133 elf_hash_(0), gnu_hash_(0) { }
134
135 const char* get_name() {
136 return name_;
137 }
138
139 uint32_t elf_hash();
140 uint32_t gnu_hash();
141
142 private:
143 const char* name_;
144 bool has_elf_hash_;
145 bool has_gnu_hash_;
146 uint32_t elf_hash_;
147 uint32_t gnu_hash_;
148
149 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
150};
151
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700152struct version_info {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800153 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700154
155 uint32_t elf_hash;
156 const char* name;
157 const soinfo* target_si;
158};
159
160// Class used construct version dependency graph.
161class VersionTracker {
162 public:
163 VersionTracker() = default;
164 bool init(const soinfo* si_from);
165
166 const version_info* get_version_info(ElfW(Versym) source_symver) const;
167 private:
168 bool init_verneed(const soinfo* si_from);
169 bool init_verdef(const soinfo* si_from);
170 void add_version_info(size_t source_index, ElfW(Word) elf_hash,
171 const char* ver_name, const soinfo* target_si);
172
173 std::vector<version_info> version_infos;
174
175 DISALLOW_COPY_AND_ASSIGN(VersionTracker);
176};
177
Elliott Hughes18a206c2012-10-29 17:37:13 -0700178struct soinfo {
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700179 public:
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700180 typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700181 typedef LinkedList<android_namespace_t, NamespaceListAllocator> android_namespace_list_t;
Dmitriy Ivanov280d5462015-09-28 10:14:17 -0700182#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700183 private:
184 char old_name_[SOINFO_NAME_LEN];
185#endif
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700186 public:
Elliott Hughes0266ae52014-02-10 17:46:57 -0800187 const ElfW(Phdr)* phdr;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700188 size_t phnum;
Dimitry Ivanove687d062016-02-16 13:25:29 -0800189#if defined(__work_around_b_24465209__)
190 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
191#endif
Elliott Hughes0266ae52014-02-10 17:46:57 -0800192 ElfW(Addr) base;
Weiwu Chen5ceb8892013-12-03 19:47:34 +0800193 size_t size;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800194
Dmitriy Ivanov280d5462015-09-28 10:14:17 -0700195#if defined(__work_around_b_24465209__)
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700196 uint32_t unused1; // DO NOT USE, maintained for compatibility.
Elliott Hughesc6200592013-09-30 18:43:46 -0700197#endif
Nick Kralevich38bccb22011-08-29 13:49:22 -0700198
Elliott Hughes0266ae52014-02-10 17:46:57 -0800199 ElfW(Dyn)* dynamic;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800200
Dmitriy Ivanov280d5462015-09-28 10:14:17 -0700201#if defined(__work_around_b_24465209__)
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700202 uint32_t unused2; // DO NOT USE, maintained for compatibility
203 uint32_t unused3; // DO NOT USE, maintained for compatibility
Elliott Hughesc6200592013-09-30 18:43:46 -0700204#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800205
Elliott Hughesd23736e2012-11-01 15:16:56 -0700206 soinfo* next;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700207 private:
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800208 uint32_t flags_;
209
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800210 const char* strtab_;
211 ElfW(Sym)* symtab_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800212
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800213 size_t nbucket_;
214 size_t nchain_;
215 uint32_t* bucket_;
216 uint32_t* chain_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800217
Chris Dearman99186652014-02-06 20:36:51 -0800218#if defined(__mips__) || !defined(__LP64__)
219 // This is only used by mips and mips64, but needs to be here for
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700220 // all 32-bit architectures to preserve binary compatibility.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800221 ElfW(Addr)** plt_got_;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700222#endif
223
224#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800225 ElfW(Rela)* plt_rela_;
226 size_t plt_rela_count_;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700227
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800228 ElfW(Rela)* rela_;
229 size_t rela_count_;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700230#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800231 ElfW(Rel)* plt_rel_;
232 size_t plt_rel_count_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800233
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800234 ElfW(Rel)* rel_;
235 size_t rel_count_;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700236#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800237
Dimitry Ivanov55437462016-07-20 15:33:07 -0700238 linker_ctor_function_t* preinit_array_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800239 size_t preinit_array_count_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800240
Dimitry Ivanov55437462016-07-20 15:33:07 -0700241 linker_ctor_function_t* init_array_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800242 size_t init_array_count_;
Dimitry Ivanov55437462016-07-20 15:33:07 -0700243 linker_dtor_function_t* fini_array_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800244 size_t fini_array_count_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800245
Dimitry Ivanov55437462016-07-20 15:33:07 -0700246 linker_ctor_function_t init_func_;
247 linker_dtor_function_t fini_func_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800248
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700249#if defined(__arm__)
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800250 public:
Elliott Hughesd23736e2012-11-01 15:16:56 -0700251 // ARM EABI section used for stack unwinding.
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700252 uint32_t* ARM_exidx;
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700253 size_t ARM_exidx_count;
Dmitriy Ivanov88940912014-11-12 18:20:39 -0800254 private:
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800255#elif defined(__mips__)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800256 uint32_t mips_symtabno_;
257 uint32_t mips_local_gotno_;
258 uint32_t mips_gotsym_;
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700259 bool mips_relocate_got(const VersionTracker& version_tracker,
260 const soinfo_list_t& global_group,
261 const soinfo_list_t& local_group);
Duane Sandbc425c72015-06-01 16:29:14 -0700262#if !defined(__LP64__)
263 bool mips_check_and_adjust_fp_modes();
264#endif
Elliott Hughesd23736e2012-11-01 15:16:56 -0700265#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800266 size_t ref_count_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800267 public:
Elliott Hughes3a9c5d62014-02-10 13:31:13 -0800268 link_map link_map_head;
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +0400269
Elliott Hughesd23736e2012-11-01 15:16:56 -0700270 bool constructors_called;
Nick Kralevich9ec0f032012-02-28 10:40:00 -0800271
Elliott Hughesd23736e2012-11-01 15:16:56 -0700272 // When you read a virtual address from the ELF file, add this
273 // value to get the corresponding address in the process' address space.
Elliott Hughes0266ae52014-02-10 17:46:57 -0800274 ElfW(Addr) load_bias;
Ard Biesheuvel5ae44f32012-08-30 12:48:32 +0200275
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +0000276#if !defined(__LP64__)
277 bool has_text_relocations;
Elliott Hughese4d792a2013-10-28 14:19:05 -0700278#endif
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700279 bool has_DT_SYMBOLIC;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700280
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800281 public:
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700282 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
283 off64_t file_offset, int rtld_flags);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700284 ~soinfo();
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700285
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800286 void call_constructors();
287 void call_destructors();
288 void call_pre_init_constructors();
289 bool prelink_image();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700290 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
291 const android_dlextinfo* extinfo);
Mingwei Shibe910522015-11-12 07:02:14 +0000292 bool protect_relro();
Elliott Hughesd23736e2012-11-01 15:16:56 -0700293
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700294 void add_child(soinfo* child);
295 void remove_all_links();
296
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700297 ino_t get_st_ino() const;
298 dev_t get_st_dev() const;
299 off64_t get_file_offset() const;
Brigid Smithc5a13ef2014-07-23 11:22:25 -0700300
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700301 uint32_t get_rtld_flags() const;
302 uint32_t get_dt_flags_1() const;
303 void set_dt_flags_1(uint32_t dt_flags_1);
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700304
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700305 soinfo_list_t& get_children();
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700306 const soinfo_list_t& get_children() const;
307
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700308 soinfo_list_t& get_parents();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700309
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700310 bool find_symbol_by_name(SymbolName& symbol_name,
311 const version_info* vi,
312 const ElfW(Sym)** symbol) const;
313
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800314 ElfW(Sym)* find_symbol_by_address(const void* addr);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700315 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -0700316
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700317 const char* get_string(ElfW(Word) index) const;
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700318 bool can_unload() const;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800319 bool is_gnu_hash() const;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700320
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700321 bool inline has_min_version(uint32_t min_version __unused) const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -0700322#if defined(__work_around_b_24465209__)
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800323 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700324#else
325 return true;
326#endif
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700327 }
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700328
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800329 bool is_linked() const;
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800330 bool is_linker() const;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800331 bool is_main_executable() const;
332
333 void set_linked();
334 void set_linker_flag();
335 void set_main_executable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700336 void set_nodelete();
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800337
338 void increment_ref_count();
339 size_t decrement_ref_count();
340
341 soinfo* get_local_group_root() const;
342
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700343 void set_soname(const char* soname);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700344 const char* get_soname() const;
345 const char* get_realpath() const;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700346 const ElfW(Versym)* get_versym(size_t n) const;
347 ElfW(Addr) get_verneed_ptr() const;
348 size_t get_verneed_cnt() const;
349 ElfW(Addr) get_verdef_ptr() const;
350 size_t get_verdef_cnt() const;
351
352 bool find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700353
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700354 uint32_t get_target_sdk_version() const;
355
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700356 void set_dt_runpath(const char *);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700357 const std::vector<std::string>& get_dt_runpath() const;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700358 android_namespace_t* get_primary_namespace();
359 void add_secondary_namespace(android_namespace_t* secondary_ns);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700360
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800361 void set_mapped_by_caller(bool reserved_map);
362 bool is_mapped_by_caller() const;
363
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700364 uintptr_t get_handle() const;
365 void generate_handle();
366 void* to_handle();
367
Elliott Hughesd23736e2012-11-01 15:16:56 -0700368 private:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700369 bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800370 ElfW(Sym)* elf_addr_lookup(const void* addr);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700371 bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800372 ElfW(Sym)* gnu_addr_lookup(const void* addr);
373
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -0700374 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
375 const char* sym_name, const version_info** vi);
376
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -0800377 template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -0700378 bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
379 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700380
381 private:
382 // This part of the structure is only available
383 // when FLAG_NEW_SOINFO is set in this->flags.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800384 uint32_t version_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700385
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700386 // version >= 0
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800387 dev_t st_dev_;
388 ino_t st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700389
390 // dependency graph
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800391 soinfo_list_t children_;
392 soinfo_list_t parents_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700393
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700394 // version >= 1
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800395 off64_t file_offset_;
396 uint32_t rtld_flags_;
397 uint32_t dt_flags_1_;
398 size_t strtab_size_;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700399
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800400 // version >= 2
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700401
402 size_t gnu_nbucket_;
403 uint32_t* gnu_bucket_;
404 uint32_t* gnu_chain_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800405 uint32_t gnu_maskwords_;
406 uint32_t gnu_shift2_;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800407 ElfW(Addr)* gnu_bloom_filter_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800408
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800409 soinfo* local_group_root_;
410
Dmitriy Ivanov18a69562015-02-04 16:05:30 -0800411 uint8_t* android_relocs_;
412 size_t android_relocs_size_;
413
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700414 const char* soname_;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700415 std::string realpath_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -0700416
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700417 const ElfW(Versym)* versym_;
418
419 ElfW(Addr) verdef_ptr_;
420 size_t verdef_cnt_;
421
422 ElfW(Addr) verneed_ptr_;
423 size_t verneed_cnt_;
424
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700425 uint32_t target_sdk_version_;
426
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700427 // version >= 3
Evgenii Stepanov68650822015-06-10 13:38:39 -0700428 std::vector<std::string> dt_runpath_;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700429 android_namespace_t* primary_namespace_;
430 android_namespace_list_t secondary_namespaces_;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700431 uintptr_t handle_;
Evgenii Stepanov68650822015-06-10 13:38:39 -0700432
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -0700433 friend soinfo* get_libdl_info();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800434};
435
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700436bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
437 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
438 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800439
440enum RelocationKind {
441 kRelocAbsolute = 0,
442 kRelocRelative,
443 kRelocCopy,
444 kRelocSymbol,
445 kRelocMax
446};
447
448void count_relocation(RelocationKind kind);
449
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -0700450soinfo* get_libdl_info();
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800451
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800452void do_android_get_LD_LIBRARY_PATH(char*, size_t);
Elliott Hughescade4c32012-12-20 14:42:14 -0800453void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700454void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, void* caller_addr);
455int do_dlclose(void* handle);
David 'Digit' Turner16084162012-06-12 16:25:37 +0200456
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700457int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
458
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800459bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
460 void* caller_addr, void** symbol);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700461
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800462int do_dladdr(const void* addr, Dl_info* info);
Elliott Hughesd23736e2012-11-01 15:16:56 -0700463
Elliott Hughes650be4e2013-03-05 18:47:58 -0800464char* linker_get_error_buffer();
465size_t linker_get_error_buffer_size();
466
Dmitriy Ivanov79fd6682015-05-21 17:43:49 -0700467void set_application_target_sdk_version(uint32_t target);
468uint32_t get_application_target_sdk_version();
469
Dimitry Ivanov41fd2952016-05-09 17:37:39 -0700470enum {
471 /* A regular namespace is the namespace with a custom search path that does
472 * not impose any restrictions on the location of native libraries.
473 */
474 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
475
476 /* An isolated namespace requires all the libraries to be on the search path
477 * or under permitted_when_isolated_path. The search path is the union of
478 * ld_library_path and default_library_path.
479 */
480 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
481
482 /* The shared namespace clones the list of libraries of the caller namespace upon creation
483 * which means that they are shared between namespaces - the caller namespace and the new one
484 * will use the same copy of a library if it was loaded prior to android_create_namespace call.
485 *
486 * Note that libraries loaded after the namespace is created will not be shared.
487 *
488 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
489 * permitted_path from the caller's namespace.
490 */
491 ANDROID_NAMESPACE_TYPE_SHARED = 2,
492 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
493 ANDROID_NAMESPACE_TYPE_ISOLATED,
494};
495
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800496bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path);
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700497android_namespace_t* create_namespace(const void* caller_addr,
498 const char* name,
499 const char* ld_library_path,
500 const char* default_library_path,
501 uint64_t type,
502 const char* permitted_when_isolated_path,
503 android_namespace_t* parent_namespace);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700504
Evgenii Stepanovd13e9a62016-07-15 16:31:42 -0700505constexpr unsigned kLibraryAlignmentBits = 18;
506constexpr size_t kLibraryAlignment = 1UL << kLibraryAlignmentBits;
507
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800508#endif