blob: 3a339493138b79427928aa55e2763d974abd8bf4 [file] [log] [blame]
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07001/*
2 * Copyright (C) 2016 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
Elliott Hughescbc80ba2018-02-13 14:26:29 -080029#pragma once
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070030
31#include <link.h>
32
Ryan Pricharde5e69e02019-01-01 18:53:48 -080033#include <memory>
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070034#include <string>
Ryan Prichardffaae702019-01-23 17:47:10 -080035#include <vector>
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070036
Ryan Pricharde5e69e02019-01-01 18:53:48 -080037#include "private/bionic_elf_tls.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070038#include "linker_namespaces.h"
Ryan Prichard16455b52019-01-18 01:00:59 -080039#include "linker_tls.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070040
41#define FLAG_LINKED 0x00000001
42#define FLAG_EXE 0x00000004 // The main executable
43#define FLAG_LINKER 0x00000010 // The linker itself
44#define FLAG_GNU_HASH 0x00000040 // uses gnu hash
45#define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
46 // and should not be unmapped
dimitry965d06d2017-11-28 16:03:07 +010047#define FLAG_IMAGE_LINKED 0x00000100 // Is image linked - this is a guard on link_image.
48 // The difference between this flag and
49 // FLAG_LINKED is that FLAG_LINKED
50 // means is set when load_group is
51 // successfully loaded whereas this
52 // flag is set to avoid linking image
53 // when link_image called for the
54 // second time. This situation happens
55 // when load group is crossing
56 // namespace boundary twice and second
57 // local group depends on the same libraries.
dimitry55547db2018-05-25 14:17:37 +020058#define FLAG_RESERVED 0x00000200 // This flag was set when there is at least one
dimitry06016f22018-01-05 11:39:28 +010059 // outstanding thread_local dtor
60 // registered with this soinfo. In such
61 // a case the actual unload is
62 // postponed until the last thread_local
63 // destructor associated with this
64 // soinfo is executed and this flag is
65 // unset.
Ryan Prichardae320cd2019-12-10 12:39:05 -080066#define FLAG_PRELINKED 0x00000400 // prelink_image has successfully processed this soinfo
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070067#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
68
Ryan Pricharde5e69e02019-01-01 18:53:48 -080069#define SOINFO_VERSION 5
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070070
Ryan Prichard339ecef2020-01-02 16:36:06 -080071ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr);
72
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070073typedef void (*linker_dtor_function_t)();
74typedef void (*linker_ctor_function_t)(int, char**, char**);
75
Ryan Prichard339ecef2020-01-02 16:36:06 -080076// An entry within a SymbolLookupList.
77struct SymbolLookupLib {
78 uint32_t gnu_maskwords_ = 0;
79 uint32_t gnu_shift2_ = 0;
80 ElfW(Addr)* gnu_bloom_filter_ = nullptr;
81
82 const char* strtab_;
83 size_t strtab_size_;
84 const ElfW(Sym)* symtab_;
85 const ElfW(Versym)* versym_;
86
87 const uint32_t* gnu_chain_;
88 size_t gnu_nbucket_;
89 uint32_t* gnu_bucket_;
90
91 soinfo* si_ = nullptr;
92
93 bool needs_sysv_lookup() const { return si_ != nullptr && gnu_bloom_filter_ == nullptr; }
94};
95
96// A list of libraries to search for a symbol.
97class SymbolLookupList {
98 std::vector<SymbolLookupLib> libs_;
99 SymbolLookupLib sole_lib_;
100 const SymbolLookupLib* begin_;
101 const SymbolLookupLib* end_;
102 size_t slow_path_count_ = 0;
103
104 public:
105 explicit SymbolLookupList(soinfo* si);
106 SymbolLookupList(const soinfo_list_t& global_group, const soinfo_list_t& local_group);
107 void set_dt_symbolic_lib(soinfo* symbolic_lib);
108
109 const SymbolLookupLib* begin() const { return begin_; }
110 const SymbolLookupLib* end() const { return end_; }
111 bool needs_slow_path() const { return slow_path_count_ > 0; }
112};
113
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700114class SymbolName {
115 public:
116 explicit SymbolName(const char* name)
117 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
118 elf_hash_(0), gnu_hash_(0) { }
119
120 const char* get_name() {
121 return name_;
122 }
123
124 uint32_t elf_hash();
125 uint32_t gnu_hash();
126
127 private:
128 const char* name_;
129 bool has_elf_hash_;
130 bool has_gnu_hash_;
131 uint32_t elf_hash_;
132 uint32_t gnu_hash_;
133
134 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
135};
136
137struct version_info {
138 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
139
140 uint32_t elf_hash;
141 const char* name;
142 const soinfo* target_si;
143};
144
145// TODO(dimitry): remove reference from soinfo member functions to this class.
146class VersionTracker;
147
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800148struct soinfo_tls {
149 TlsSegment segment;
Ryan Prichard16455b52019-01-18 01:00:59 -0800150 size_t module_id = kTlsUninitializedModuleId;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800151};
152
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700153#if defined(__work_around_b_24465209__)
154#define SOINFO_NAME_LEN 128
155#endif
156
157struct soinfo {
158#if defined(__work_around_b_24465209__)
159 private:
160 char old_name_[SOINFO_NAME_LEN];
161#endif
162 public:
163 const ElfW(Phdr)* phdr;
164 size_t phnum;
165#if defined(__work_around_b_24465209__)
166 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
167#endif
168 ElfW(Addr) base;
169 size_t size;
170
171#if defined(__work_around_b_24465209__)
172 uint32_t unused1; // DO NOT USE, maintained for compatibility.
173#endif
174
175 ElfW(Dyn)* dynamic;
176
177#if defined(__work_around_b_24465209__)
178 uint32_t unused2; // DO NOT USE, maintained for compatibility
179 uint32_t unused3; // DO NOT USE, maintained for compatibility
180#endif
181
182 soinfo* next;
183 private:
184 uint32_t flags_;
185
186 const char* strtab_;
187 ElfW(Sym)* symtab_;
188
189 size_t nbucket_;
190 size_t nchain_;
191 uint32_t* bucket_;
192 uint32_t* chain_;
193
194#if defined(__mips__) || !defined(__LP64__)
195 // This is only used by mips and mips64, but needs to be here for
196 // all 32-bit architectures to preserve binary compatibility.
197 ElfW(Addr)** plt_got_;
198#endif
199
200#if defined(USE_RELA)
201 ElfW(Rela)* plt_rela_;
202 size_t plt_rela_count_;
203
204 ElfW(Rela)* rela_;
205 size_t rela_count_;
206#else
207 ElfW(Rel)* plt_rel_;
208 size_t plt_rel_count_;
209
210 ElfW(Rel)* rel_;
211 size_t rel_count_;
212#endif
213
214 linker_ctor_function_t* preinit_array_;
215 size_t preinit_array_count_;
216
217 linker_ctor_function_t* init_array_;
218 size_t init_array_count_;
219 linker_dtor_function_t* fini_array_;
220 size_t fini_array_count_;
221
222 linker_ctor_function_t init_func_;
223 linker_dtor_function_t fini_func_;
224
225#if defined(__arm__)
226 public:
227 // ARM EABI section used for stack unwinding.
228 uint32_t* ARM_exidx;
229 size_t ARM_exidx_count;
230 private:
231#elif defined(__mips__)
232 uint32_t mips_symtabno_;
233 uint32_t mips_local_gotno_;
234 uint32_t mips_gotsym_;
235 bool mips_relocate_got(const VersionTracker& version_tracker,
236 const soinfo_list_t& global_group,
237 const soinfo_list_t& local_group);
238#if !defined(__LP64__)
239 bool mips_check_and_adjust_fp_modes();
240#endif
241#endif
242 size_t ref_count_;
243 public:
244 link_map link_map_head;
245
246 bool constructors_called;
247
248 // When you read a virtual address from the ELF file, add this
249 // value to get the corresponding address in the process' address space.
250 ElfW(Addr) load_bias;
251
252#if !defined(__LP64__)
253 bool has_text_relocations;
254#endif
255 bool has_DT_SYMBOLIC;
256
257 public:
258 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
259 off64_t file_offset, int rtld_flags);
260 ~soinfo();
261
262 void call_constructors();
263 void call_destructors();
264 void call_pre_init_constructors();
265 bool prelink_image();
Ryan Prichard339ecef2020-01-02 16:36:06 -0800266 bool link_image(const SymbolLookupList& lookup_list, soinfo* local_group_root,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400267 const android_dlextinfo* extinfo, size_t* relro_fd_offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700268 bool protect_relro();
269
270 void add_child(soinfo* child);
271 void remove_all_links();
272
273 ino_t get_st_ino() const;
274 dev_t get_st_dev() const;
275 off64_t get_file_offset() const;
276
277 uint32_t get_rtld_flags() const;
278 uint32_t get_dt_flags_1() const;
279 void set_dt_flags_1(uint32_t dt_flags_1);
280
281 soinfo_list_t& get_children();
282 const soinfo_list_t& get_children() const;
283
284 soinfo_list_t& get_parents();
285
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800286 const ElfW(Sym)* find_symbol_by_name(SymbolName& symbol_name, const version_info* vi) const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700287
288 ElfW(Sym)* find_symbol_by_address(const void* addr);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800289
290 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const {
291 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
292 return call_ifunc_resolver(s->st_value + load_bias);
293 }
294
295 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
296 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700297
298 const char* get_string(ElfW(Word) index) const;
299 bool can_unload() const;
300 bool is_gnu_hash() const;
301
302 bool inline has_min_version(uint32_t min_version __unused) const {
303#if defined(__work_around_b_24465209__)
304 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
305#else
306 return true;
307#endif
308 }
309
Ryan Prichard339ecef2020-01-02 16:36:06 -0800310 const ElfW(Versym)* get_versym_table() const {
311 return has_min_version(2) ? versym_ : nullptr;
312 }
313
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700314 bool is_linked() const;
315 bool is_linker() const;
316 bool is_main_executable() const;
317
318 void set_linked();
319 void set_linker_flag();
320 void set_main_executable();
321 void set_nodelete();
322
dimitry965d06d2017-11-28 16:03:07 +0100323 size_t increment_ref_count();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700324 size_t decrement_ref_count();
dimitry06016f22018-01-05 11:39:28 +0100325 size_t get_ref_count() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700326
327 soinfo* get_local_group_root() const;
328
329 void set_soname(const char* soname);
330 const char* get_soname() const;
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700331 void set_realpath(const char* path);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700332 const char* get_realpath() const;
333 const ElfW(Versym)* get_versym(size_t n) const;
334 ElfW(Addr) get_verneed_ptr() const;
335 size_t get_verneed_cnt() const;
336 ElfW(Addr) get_verdef_ptr() const;
337 size_t get_verdef_cnt() const;
338
Elliott Hughesff1428a2018-11-12 16:01:37 -0800339 int get_target_sdk_version() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700340
341 void set_dt_runpath(const char *);
342 const std::vector<std::string>& get_dt_runpath() const;
343 android_namespace_t* get_primary_namespace();
344 void add_secondary_namespace(android_namespace_t* secondary_ns);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800345 android_namespace_list_t& get_secondary_namespaces();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700346
Ryan Prichardc6bec072019-10-03 17:57:15 -0700347 soinfo_tls* get_tls() const {
348 return has_min_version(5) ? tls_.get() : nullptr;
349 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800350
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700351 void set_mapped_by_caller(bool reserved_map);
352 bool is_mapped_by_caller() const;
353
354 uintptr_t get_handle() const;
355 void generate_handle();
356 void* to_handle();
357
Ryan Prichard339ecef2020-01-02 16:36:06 -0800358 SymbolLookupLib get_lookup_lib();
359
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700360 private:
dimitry965d06d2017-11-28 16:03:07 +0100361 bool is_image_linked() const;
362 void set_image_linked();
363
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800364 const ElfW(Sym)* gnu_lookup(SymbolName& symbol_name, const version_info* vi) const;
365 const ElfW(Sym)* elf_lookup(SymbolName& symbol_name, const version_info* vi) const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700366 ElfW(Sym)* gnu_addr_lookup(const void* addr);
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800367 ElfW(Sym)* elf_addr_lookup(const void* addr);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700368
Ryan Prichard339ecef2020-01-02 16:36:06 -0800369 public:
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700370 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
371 const char* sym_name, const version_info** vi);
372
Ryan Prichard339ecef2020-01-02 16:36:06 -0800373 private:
374 bool relocate(const SymbolLookupList& lookup_list);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800375 bool relocate_relr();
Rahul Chaudhryf16b6592018-01-25 15:34:15 -0800376 void apply_relr_reloc(ElfW(Addr) offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700377
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700378 // This part of the structure is only available
379 // when FLAG_NEW_SOINFO is set in this->flags.
380 uint32_t version_;
381
382 // version >= 0
383 dev_t st_dev_;
384 ino_t st_ino_;
385
386 // dependency graph
387 soinfo_list_t children_;
388 soinfo_list_t parents_;
389
390 // version >= 1
391 off64_t file_offset_;
392 uint32_t rtld_flags_;
393 uint32_t dt_flags_1_;
394 size_t strtab_size_;
395
396 // version >= 2
397
398 size_t gnu_nbucket_;
399 uint32_t* gnu_bucket_;
400 uint32_t* gnu_chain_;
401 uint32_t gnu_maskwords_;
402 uint32_t gnu_shift2_;
403 ElfW(Addr)* gnu_bloom_filter_;
404
405 soinfo* local_group_root_;
406
407 uint8_t* android_relocs_;
408 size_t android_relocs_size_;
409
410 const char* soname_;
411 std::string realpath_;
412
413 const ElfW(Versym)* versym_;
414
415 ElfW(Addr) verdef_ptr_;
416 size_t verdef_cnt_;
417
418 ElfW(Addr) verneed_ptr_;
419 size_t verneed_cnt_;
420
Elliott Hughesff1428a2018-11-12 16:01:37 -0800421 int target_sdk_version_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700422
423 // version >= 3
424 std::vector<std::string> dt_runpath_;
425 android_namespace_t* primary_namespace_;
426 android_namespace_list_t secondary_namespaces_;
427 uintptr_t handle_;
428
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700429 friend soinfo* get_libdl_info(const soinfo& linker_si);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800430
431 // version >= 4
432 ElfW(Relr)* relr_;
433 size_t relr_count_;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800434
435 // version >= 5
436 std::unique_ptr<soinfo_tls> tls_;
Ryan Prichardffaae702019-01-23 17:47:10 -0800437 std::vector<TlsDynamicResolverArg> tlsdesc_args_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700438};
439
440// This function is used by dlvsym() to calculate hash of sym_ver
441uint32_t calculate_elf_hash(const char* name);
442
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700443const char* fix_dt_needed(const char* dt_needed, const char* sopath);
444
445template<typename F>
446void for_each_dt_needed(const soinfo* si, F action) {
447 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
448 if (d->d_tag == DT_NEEDED) {
449 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
450 }
451 }
452}
Ryan Prichard339ecef2020-01-02 16:36:06 -0800453
454const ElfW(Sym)* soinfo_do_lookup(const char* name, const version_info* vi,
455 soinfo** si_found_in, const SymbolLookupList& lookup_list);