blob: 092cbcc8745c9ddbd0c99cfa42060433e53e8dc4 [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
Mitch Phillips7c1f3772023-09-28 13:45:59 +020037#include "async_safe/CHECK.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"
Mitch Phillips7c1f3772023-09-28 13:45:59 +020040#include "private/bionic_elf_tls.h"
41#include "private/bionic_globals.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070042
43#define FLAG_LINKED 0x00000001
44#define FLAG_EXE 0x00000004 // The main executable
45#define FLAG_LINKER 0x00000010 // The linker itself
46#define FLAG_GNU_HASH 0x00000040 // uses gnu hash
47#define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
48 // and should not be unmapped
dimitry965d06d2017-11-28 16:03:07 +010049#define FLAG_IMAGE_LINKED 0x00000100 // Is image linked - this is a guard on link_image.
50 // The difference between this flag and
51 // FLAG_LINKED is that FLAG_LINKED
52 // means is set when load_group is
53 // successfully loaded whereas this
54 // flag is set to avoid linking image
55 // when link_image called for the
56 // second time. This situation happens
57 // when load group is crossing
58 // namespace boundary twice and second
59 // local group depends on the same libraries.
dimitry55547db2018-05-25 14:17:37 +020060#define FLAG_RESERVED 0x00000200 // This flag was set when there is at least one
dimitry06016f22018-01-05 11:39:28 +010061 // outstanding thread_local dtor
62 // registered with this soinfo. In such
63 // a case the actual unload is
64 // postponed until the last thread_local
65 // destructor associated with this
66 // soinfo is executed and this flag is
67 // unset.
Ryan Prichardae320cd2019-12-10 12:39:05 -080068#define FLAG_PRELINKED 0x00000400 // prelink_image has successfully processed this soinfo
Mitch Phillipse8139f52022-11-23 15:18:01 -080069#define FLAG_GLOBALS_TAGGED 0x00000800 // globals have been tagged by MTE.
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070070#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
71
Mitch Phillipse8139f52022-11-23 15:18:01 -080072#define SOINFO_VERSION 7
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070073
Ryan Prichard339ecef2020-01-02 16:36:06 -080074ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr);
75
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070076typedef void (*linker_dtor_function_t)();
77typedef void (*linker_ctor_function_t)(int, char**, char**);
78
Ryan Prichard339ecef2020-01-02 16:36:06 -080079// An entry within a SymbolLookupList.
80struct SymbolLookupLib {
81 uint32_t gnu_maskwords_ = 0;
82 uint32_t gnu_shift2_ = 0;
83 ElfW(Addr)* gnu_bloom_filter_ = nullptr;
84
85 const char* strtab_;
86 size_t strtab_size_;
87 const ElfW(Sym)* symtab_;
88 const ElfW(Versym)* versym_;
89
90 const uint32_t* gnu_chain_;
91 size_t gnu_nbucket_;
92 uint32_t* gnu_bucket_;
93
94 soinfo* si_ = nullptr;
95
96 bool needs_sysv_lookup() const { return si_ != nullptr && gnu_bloom_filter_ == nullptr; }
97};
98
99// A list of libraries to search for a symbol.
100class SymbolLookupList {
101 std::vector<SymbolLookupLib> libs_;
102 SymbolLookupLib sole_lib_;
103 const SymbolLookupLib* begin_;
104 const SymbolLookupLib* end_;
105 size_t slow_path_count_ = 0;
106
107 public:
108 explicit SymbolLookupList(soinfo* si);
109 SymbolLookupList(const soinfo_list_t& global_group, const soinfo_list_t& local_group);
110 void set_dt_symbolic_lib(soinfo* symbolic_lib);
111
112 const SymbolLookupLib* begin() const { return begin_; }
113 const SymbolLookupLib* end() const { return end_; }
114 bool needs_slow_path() const { return slow_path_count_ > 0; }
115};
116
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700117class SymbolName {
118 public:
119 explicit SymbolName(const char* name)
120 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
121 elf_hash_(0), gnu_hash_(0) { }
122
123 const char* get_name() {
124 return name_;
125 }
126
127 uint32_t elf_hash();
128 uint32_t gnu_hash();
129
130 private:
131 const char* name_;
132 bool has_elf_hash_;
133 bool has_gnu_hash_;
134 uint32_t elf_hash_;
135 uint32_t gnu_hash_;
136
137 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
138};
139
140struct version_info {
141 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
142
143 uint32_t elf_hash;
144 const char* name;
145 const soinfo* target_si;
146};
147
148// TODO(dimitry): remove reference from soinfo member functions to this class.
149class VersionTracker;
150
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800151struct soinfo_tls {
152 TlsSegment segment;
Ryan Prichard16455b52019-01-18 01:00:59 -0800153 size_t module_id = kTlsUninitializedModuleId;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800154};
155
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700156#if defined(__work_around_b_24465209__)
157#define SOINFO_NAME_LEN 128
158#endif
159
160struct soinfo {
161#if defined(__work_around_b_24465209__)
162 private:
163 char old_name_[SOINFO_NAME_LEN];
164#endif
165 public:
166 const ElfW(Phdr)* phdr;
167 size_t phnum;
168#if defined(__work_around_b_24465209__)
169 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
170#endif
171 ElfW(Addr) base;
172 size_t size;
173
174#if defined(__work_around_b_24465209__)
175 uint32_t unused1; // DO NOT USE, maintained for compatibility.
176#endif
177
178 ElfW(Dyn)* dynamic;
179
180#if defined(__work_around_b_24465209__)
181 uint32_t unused2; // DO NOT USE, maintained for compatibility
182 uint32_t unused3; // DO NOT USE, maintained for compatibility
183#endif
184
185 soinfo* next;
186 private:
187 uint32_t flags_;
188
189 const char* strtab_;
190 ElfW(Sym)* symtab_;
191
192 size_t nbucket_;
193 size_t nchain_;
194 uint32_t* bucket_;
195 uint32_t* chain_;
196
Elliott Hughes14798932020-02-13 15:40:07 -0800197#if !defined(__LP64__)
198 ElfW(Addr)** unused4; // DO NOT USE, maintained for compatibility
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700199#endif
200
201#if defined(USE_RELA)
202 ElfW(Rela)* plt_rela_;
203 size_t plt_rela_count_;
204
205 ElfW(Rela)* rela_;
206 size_t rela_count_;
207#else
208 ElfW(Rel)* plt_rel_;
209 size_t plt_rel_count_;
210
211 ElfW(Rel)* rel_;
212 size_t rel_count_;
213#endif
214
215 linker_ctor_function_t* preinit_array_;
216 size_t preinit_array_count_;
217
218 linker_ctor_function_t* init_array_;
219 size_t init_array_count_;
220 linker_dtor_function_t* fini_array_;
221 size_t fini_array_count_;
222
223 linker_ctor_function_t init_func_;
224 linker_dtor_function_t fini_func_;
225
226#if defined(__arm__)
227 public:
228 // ARM EABI section used for stack unwinding.
229 uint32_t* ARM_exidx;
230 size_t ARM_exidx_count;
231 private:
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700232#endif
233 size_t ref_count_;
234 public:
235 link_map link_map_head;
236
237 bool constructors_called;
238
239 // When you read a virtual address from the ELF file, add this
240 // value to get the corresponding address in the process' address space.
241 ElfW(Addr) load_bias;
242
243#if !defined(__LP64__)
244 bool has_text_relocations;
245#endif
246 bool has_DT_SYMBOLIC;
247
248 public:
249 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
250 off64_t file_offset, int rtld_flags);
251 ~soinfo();
252
253 void call_constructors();
254 void call_destructors();
255 void call_pre_init_constructors();
256 bool prelink_image();
Ryan Prichard339ecef2020-01-02 16:36:06 -0800257 bool link_image(const SymbolLookupList& lookup_list, soinfo* local_group_root,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400258 const android_dlextinfo* extinfo, size_t* relro_fd_offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700259 bool protect_relro();
260
Mitch Phillipse8139f52022-11-23 15:18:01 -0800261 void tag_globals();
262
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700263 void add_child(soinfo* child);
264 void remove_all_links();
265
266 ino_t get_st_ino() const;
267 dev_t get_st_dev() const;
268 off64_t get_file_offset() const;
269
270 uint32_t get_rtld_flags() const;
271 uint32_t get_dt_flags_1() const;
272 void set_dt_flags_1(uint32_t dt_flags_1);
273
274 soinfo_list_t& get_children();
275 const soinfo_list_t& get_children() const;
276
277 soinfo_list_t& get_parents();
278
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800279 const ElfW(Sym)* find_symbol_by_name(SymbolName& symbol_name, const version_info* vi) const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700280
281 ElfW(Sym)* find_symbol_by_address(const void* addr);
Ryan Prichard339ecef2020-01-02 16:36:06 -0800282
283 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const {
284 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
285 return call_ifunc_resolver(s->st_value + load_bias);
286 }
287
288 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
289 }
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700290
291 const char* get_string(ElfW(Word) index) const;
292 bool can_unload() const;
293 bool is_gnu_hash() const;
294
295 bool inline has_min_version(uint32_t min_version __unused) const {
296#if defined(__work_around_b_24465209__)
297 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
298#else
299 return true;
300#endif
301 }
302
Ryan Prichard339ecef2020-01-02 16:36:06 -0800303 const ElfW(Versym)* get_versym_table() const {
304 return has_min_version(2) ? versym_ : nullptr;
305 }
306
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700307 bool is_linked() const;
308 bool is_linker() const;
309 bool is_main_executable() const;
310
311 void set_linked();
312 void set_linker_flag();
313 void set_main_executable();
314 void set_nodelete();
315
dimitry965d06d2017-11-28 16:03:07 +0100316 size_t increment_ref_count();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700317 size_t decrement_ref_count();
dimitry06016f22018-01-05 11:39:28 +0100318 size_t get_ref_count() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700319
320 soinfo* get_local_group_root() const;
321
322 void set_soname(const char* soname);
323 const char* get_soname() const;
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700324 void set_realpath(const char* path);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700325 const char* get_realpath() const;
326 const ElfW(Versym)* get_versym(size_t n) const;
327 ElfW(Addr) get_verneed_ptr() const;
328 size_t get_verneed_cnt() const;
329 ElfW(Addr) get_verdef_ptr() const;
330 size_t get_verdef_cnt() const;
331
Elliott Hughesff1428a2018-11-12 16:01:37 -0800332 int get_target_sdk_version() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700333
334 void set_dt_runpath(const char *);
335 const std::vector<std::string>& get_dt_runpath() const;
336 android_namespace_t* get_primary_namespace();
337 void add_secondary_namespace(android_namespace_t* secondary_ns);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800338 android_namespace_list_t& get_secondary_namespaces();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700339
Ryan Prichardc6bec072019-10-03 17:57:15 -0700340 soinfo_tls* get_tls() const {
341 return has_min_version(5) ? tls_.get() : nullptr;
342 }
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800343
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700344 void set_mapped_by_caller(bool reserved_map);
345 bool is_mapped_by_caller() const;
346
347 uintptr_t get_handle() const;
348 void generate_handle();
349 void* to_handle();
350
Ryan Prichard339ecef2020-01-02 16:36:06 -0800351 SymbolLookupLib get_lookup_lib();
352
Evgenii Stepanove0848bb2020-07-14 16:44:57 -0700353 void set_gap_start(ElfW(Addr) gap_start);
354 ElfW(Addr) get_gap_start() const;
355
356 void set_gap_size(size_t gap_size);
357 size_t get_gap_size() const;
358
Mitch Phillips7c1f3772023-09-28 13:45:59 +0200359 const memtag_dynamic_entries_t* memtag_dynamic_entries() const {
360 CHECK(has_min_version(7));
361 return &memtag_dynamic_entries_;
362 }
363 void* memtag_globals() const { return memtag_dynamic_entries()->memtag_globals; }
364 size_t memtag_globalssz() const { return memtag_dynamic_entries()->memtag_globalssz; }
365 bool has_memtag_mode() const { return memtag_dynamic_entries()->has_memtag_mode; }
366 unsigned memtag_mode() const { return memtag_dynamic_entries()->memtag_mode; }
367 bool memtag_heap() const { return memtag_dynamic_entries()->memtag_heap; }
368 bool memtag_stack() const { return memtag_dynamic_entries()->memtag_stack; }
369
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700370 private:
dimitry965d06d2017-11-28 16:03:07 +0100371 bool is_image_linked() const;
372 void set_image_linked();
373
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800374 const ElfW(Sym)* gnu_lookup(SymbolName& symbol_name, const version_info* vi) const;
375 const ElfW(Sym)* elf_lookup(SymbolName& symbol_name, const version_info* vi) const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700376 ElfW(Sym)* gnu_addr_lookup(const void* addr);
Ryan Prichard0e12cce2020-01-02 14:59:11 -0800377 ElfW(Sym)* elf_addr_lookup(const void* addr);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700378
Ryan Prichard339ecef2020-01-02 16:36:06 -0800379 public:
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700380 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
381 const char* sym_name, const version_info** vi);
382
Ryan Prichard339ecef2020-01-02 16:36:06 -0800383 private:
384 bool relocate(const SymbolLookupList& lookup_list);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800385 bool relocate_relr();
Rahul Chaudhryf16b6592018-01-25 15:34:15 -0800386 void apply_relr_reloc(ElfW(Addr) offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700387
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700388 // This part of the structure is only available
389 // when FLAG_NEW_SOINFO is set in this->flags.
390 uint32_t version_;
391
392 // version >= 0
393 dev_t st_dev_;
394 ino_t st_ino_;
395
396 // dependency graph
397 soinfo_list_t children_;
398 soinfo_list_t parents_;
399
400 // version >= 1
401 off64_t file_offset_;
402 uint32_t rtld_flags_;
403 uint32_t dt_flags_1_;
404 size_t strtab_size_;
405
406 // version >= 2
407
408 size_t gnu_nbucket_;
409 uint32_t* gnu_bucket_;
410 uint32_t* gnu_chain_;
411 uint32_t gnu_maskwords_;
412 uint32_t gnu_shift2_;
413 ElfW(Addr)* gnu_bloom_filter_;
414
415 soinfo* local_group_root_;
416
417 uint8_t* android_relocs_;
418 size_t android_relocs_size_;
419
Elliott Hughesf9dd1a72021-01-11 09:04:58 -0800420 std::string soname_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700421 std::string realpath_;
422
423 const ElfW(Versym)* versym_;
424
425 ElfW(Addr) verdef_ptr_;
426 size_t verdef_cnt_;
427
428 ElfW(Addr) verneed_ptr_;
429 size_t verneed_cnt_;
430
Elliott Hughesff1428a2018-11-12 16:01:37 -0800431 int target_sdk_version_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700432
433 // version >= 3
434 std::vector<std::string> dt_runpath_;
435 android_namespace_t* primary_namespace_;
436 android_namespace_list_t secondary_namespaces_;
437 uintptr_t handle_;
438
Ryan Prichardcf9ed122019-06-04 20:56:56 -0700439 friend soinfo* get_libdl_info(const soinfo& linker_si);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800440
441 // version >= 4
442 ElfW(Relr)* relr_;
443 size_t relr_count_;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800444
445 // version >= 5
446 std::unique_ptr<soinfo_tls> tls_;
Ryan Prichardffaae702019-01-23 17:47:10 -0800447 std::vector<TlsDynamicResolverArg> tlsdesc_args_;
Evgenii Stepanove0848bb2020-07-14 16:44:57 -0700448
449 // version >= 6
450 ElfW(Addr) gap_start_;
451 size_t gap_size_;
Mitch Phillips7c1f3772023-09-28 13:45:59 +0200452
453 // version >= 7
454 memtag_dynamic_entries_t memtag_dynamic_entries_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700455};
456
457// This function is used by dlvsym() to calculate hash of sym_ver
458uint32_t calculate_elf_hash(const char* name);
459
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700460const char* fix_dt_needed(const char* dt_needed, const char* sopath);
461
462template<typename F>
463void for_each_dt_needed(const soinfo* si, F action) {
464 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
465 if (d->d_tag == DT_NEEDED) {
466 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
467 }
468 }
469}
Ryan Prichard339ecef2020-01-02 16:36:06 -0800470
471const ElfW(Sym)* soinfo_do_lookup(const char* name, const version_info* vi,
472 soinfo** si_found_in, const SymbolLookupList& lookup_list);