blob: 3499cf7422fbae197e79d02fceb199e68358d834 [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>
35
Ryan Pricharde5e69e02019-01-01 18:53:48 -080036#include "private/bionic_elf_tls.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070037#include "linker_namespaces.h"
Ryan Prichard16455b52019-01-18 01:00:59 -080038#include "linker_tls.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070039
40#define FLAG_LINKED 0x00000001
41#define FLAG_EXE 0x00000004 // The main executable
42#define FLAG_LINKER 0x00000010 // The linker itself
43#define FLAG_GNU_HASH 0x00000040 // uses gnu hash
44#define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
45 // and should not be unmapped
dimitry965d06d2017-11-28 16:03:07 +010046#define FLAG_IMAGE_LINKED 0x00000100 // Is image linked - this is a guard on link_image.
47 // The difference between this flag and
48 // FLAG_LINKED is that FLAG_LINKED
49 // means is set when load_group is
50 // successfully loaded whereas this
51 // flag is set to avoid linking image
52 // when link_image called for the
53 // second time. This situation happens
54 // when load group is crossing
55 // namespace boundary twice and second
56 // local group depends on the same libraries.
dimitry55547db2018-05-25 14:17:37 +020057#define FLAG_RESERVED 0x00000200 // This flag was set when there is at least one
dimitry06016f22018-01-05 11:39:28 +010058 // outstanding thread_local dtor
59 // registered with this soinfo. In such
60 // a case the actual unload is
61 // postponed until the last thread_local
62 // destructor associated with this
63 // soinfo is executed and this flag is
64 // unset.
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070065#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
66
Ryan Pricharde5e69e02019-01-01 18:53:48 -080067#define SOINFO_VERSION 5
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070068
69typedef void (*linker_dtor_function_t)();
70typedef void (*linker_ctor_function_t)(int, char**, char**);
71
72class SymbolName {
73 public:
74 explicit SymbolName(const char* name)
75 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
76 elf_hash_(0), gnu_hash_(0) { }
77
78 const char* get_name() {
79 return name_;
80 }
81
82 uint32_t elf_hash();
83 uint32_t gnu_hash();
84
85 private:
86 const char* name_;
87 bool has_elf_hash_;
88 bool has_gnu_hash_;
89 uint32_t elf_hash_;
90 uint32_t gnu_hash_;
91
92 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
93};
94
95struct version_info {
96 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
97
98 uint32_t elf_hash;
99 const char* name;
100 const soinfo* target_si;
101};
102
103// TODO(dimitry): remove reference from soinfo member functions to this class.
104class VersionTracker;
105
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800106struct soinfo_tls {
107 TlsSegment segment;
Ryan Prichard16455b52019-01-18 01:00:59 -0800108 size_t module_id = kTlsUninitializedModuleId;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800109};
110
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700111#if defined(__work_around_b_24465209__)
112#define SOINFO_NAME_LEN 128
113#endif
114
115struct soinfo {
116#if defined(__work_around_b_24465209__)
117 private:
118 char old_name_[SOINFO_NAME_LEN];
119#endif
120 public:
121 const ElfW(Phdr)* phdr;
122 size_t phnum;
123#if defined(__work_around_b_24465209__)
124 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
125#endif
126 ElfW(Addr) base;
127 size_t size;
128
129#if defined(__work_around_b_24465209__)
130 uint32_t unused1; // DO NOT USE, maintained for compatibility.
131#endif
132
133 ElfW(Dyn)* dynamic;
134
135#if defined(__work_around_b_24465209__)
136 uint32_t unused2; // DO NOT USE, maintained for compatibility
137 uint32_t unused3; // DO NOT USE, maintained for compatibility
138#endif
139
140 soinfo* next;
141 private:
142 uint32_t flags_;
143
144 const char* strtab_;
145 ElfW(Sym)* symtab_;
146
147 size_t nbucket_;
148 size_t nchain_;
149 uint32_t* bucket_;
150 uint32_t* chain_;
151
152#if defined(__mips__) || !defined(__LP64__)
153 // This is only used by mips and mips64, but needs to be here for
154 // all 32-bit architectures to preserve binary compatibility.
155 ElfW(Addr)** plt_got_;
156#endif
157
158#if defined(USE_RELA)
159 ElfW(Rela)* plt_rela_;
160 size_t plt_rela_count_;
161
162 ElfW(Rela)* rela_;
163 size_t rela_count_;
164#else
165 ElfW(Rel)* plt_rel_;
166 size_t plt_rel_count_;
167
168 ElfW(Rel)* rel_;
169 size_t rel_count_;
170#endif
171
172 linker_ctor_function_t* preinit_array_;
173 size_t preinit_array_count_;
174
175 linker_ctor_function_t* init_array_;
176 size_t init_array_count_;
177 linker_dtor_function_t* fini_array_;
178 size_t fini_array_count_;
179
180 linker_ctor_function_t init_func_;
181 linker_dtor_function_t fini_func_;
182
183#if defined(__arm__)
184 public:
185 // ARM EABI section used for stack unwinding.
186 uint32_t* ARM_exidx;
187 size_t ARM_exidx_count;
188 private:
189#elif defined(__mips__)
190 uint32_t mips_symtabno_;
191 uint32_t mips_local_gotno_;
192 uint32_t mips_gotsym_;
193 bool mips_relocate_got(const VersionTracker& version_tracker,
194 const soinfo_list_t& global_group,
195 const soinfo_list_t& local_group);
196#if !defined(__LP64__)
197 bool mips_check_and_adjust_fp_modes();
198#endif
199#endif
200 size_t ref_count_;
201 public:
202 link_map link_map_head;
203
204 bool constructors_called;
205
206 // When you read a virtual address from the ELF file, add this
207 // value to get the corresponding address in the process' address space.
208 ElfW(Addr) load_bias;
209
210#if !defined(__LP64__)
211 bool has_text_relocations;
212#endif
213 bool has_DT_SYMBOLIC;
214
215 public:
216 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
217 off64_t file_offset, int rtld_flags);
218 ~soinfo();
219
220 void call_constructors();
221 void call_destructors();
222 void call_pre_init_constructors();
223 bool prelink_image();
224 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
225 const android_dlextinfo* extinfo);
226 bool protect_relro();
227
228 void add_child(soinfo* child);
229 void remove_all_links();
230
231 ino_t get_st_ino() const;
232 dev_t get_st_dev() const;
233 off64_t get_file_offset() const;
234
235 uint32_t get_rtld_flags() const;
236 uint32_t get_dt_flags_1() const;
237 void set_dt_flags_1(uint32_t dt_flags_1);
238
239 soinfo_list_t& get_children();
240 const soinfo_list_t& get_children() const;
241
242 soinfo_list_t& get_parents();
243
244 bool find_symbol_by_name(SymbolName& symbol_name,
245 const version_info* vi,
246 const ElfW(Sym)** symbol) const;
247
248 ElfW(Sym)* find_symbol_by_address(const void* addr);
249 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
250
251 const char* get_string(ElfW(Word) index) const;
252 bool can_unload() const;
253 bool is_gnu_hash() const;
254
255 bool inline has_min_version(uint32_t min_version __unused) const {
256#if defined(__work_around_b_24465209__)
257 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
258#else
259 return true;
260#endif
261 }
262
263 bool is_linked() const;
264 bool is_linker() const;
265 bool is_main_executable() const;
266
267 void set_linked();
268 void set_linker_flag();
269 void set_main_executable();
270 void set_nodelete();
271
dimitry965d06d2017-11-28 16:03:07 +0100272 size_t increment_ref_count();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700273 size_t decrement_ref_count();
dimitry06016f22018-01-05 11:39:28 +0100274 size_t get_ref_count() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700275
276 soinfo* get_local_group_root() const;
277
278 void set_soname(const char* soname);
279 const char* get_soname() const;
280 const char* get_realpath() const;
281 const ElfW(Versym)* get_versym(size_t n) const;
282 ElfW(Addr) get_verneed_ptr() const;
283 size_t get_verneed_cnt() const;
284 ElfW(Addr) get_verdef_ptr() const;
285 size_t get_verdef_cnt() const;
286
Elliott Hughesff1428a2018-11-12 16:01:37 -0800287 int get_target_sdk_version() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700288
289 void set_dt_runpath(const char *);
290 const std::vector<std::string>& get_dt_runpath() const;
291 android_namespace_t* get_primary_namespace();
292 void add_secondary_namespace(android_namespace_t* secondary_ns);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800293 android_namespace_list_t& get_secondary_namespaces();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700294
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800295 soinfo_tls* get_tls() const;
296
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700297 void set_mapped_by_caller(bool reserved_map);
298 bool is_mapped_by_caller() const;
299
300 uintptr_t get_handle() const;
301 void generate_handle();
302 void* to_handle();
303
304 private:
dimitry965d06d2017-11-28 16:03:07 +0100305 bool is_image_linked() const;
306 void set_image_linked();
307
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700308 bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
309 ElfW(Sym)* elf_addr_lookup(const void* addr);
310 bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
311 ElfW(Sym)* gnu_addr_lookup(const void* addr);
312
313 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
314 const char* sym_name, const version_info** vi);
315
316 template<typename ElfRelIteratorT>
317 bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
318 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800319 bool relocate_relr();
Rahul Chaudhryf16b6592018-01-25 15:34:15 -0800320 void apply_relr_reloc(ElfW(Addr) offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700321
322 private:
323 // This part of the structure is only available
324 // when FLAG_NEW_SOINFO is set in this->flags.
325 uint32_t version_;
326
327 // version >= 0
328 dev_t st_dev_;
329 ino_t st_ino_;
330
331 // dependency graph
332 soinfo_list_t children_;
333 soinfo_list_t parents_;
334
335 // version >= 1
336 off64_t file_offset_;
337 uint32_t rtld_flags_;
338 uint32_t dt_flags_1_;
339 size_t strtab_size_;
340
341 // version >= 2
342
343 size_t gnu_nbucket_;
344 uint32_t* gnu_bucket_;
345 uint32_t* gnu_chain_;
346 uint32_t gnu_maskwords_;
347 uint32_t gnu_shift2_;
348 ElfW(Addr)* gnu_bloom_filter_;
349
350 soinfo* local_group_root_;
351
352 uint8_t* android_relocs_;
353 size_t android_relocs_size_;
354
355 const char* soname_;
356 std::string realpath_;
357
358 const ElfW(Versym)* versym_;
359
360 ElfW(Addr) verdef_ptr_;
361 size_t verdef_cnt_;
362
363 ElfW(Addr) verneed_ptr_;
364 size_t verneed_cnt_;
365
Elliott Hughesff1428a2018-11-12 16:01:37 -0800366 int target_sdk_version_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700367
368 // version >= 3
369 std::vector<std::string> dt_runpath_;
370 android_namespace_t* primary_namespace_;
371 android_namespace_list_t secondary_namespaces_;
372 uintptr_t handle_;
373
Ryan Prichard04896452018-08-20 17:44:42 -0700374 friend soinfo* get_libdl_info(const char* linker_path, const soinfo& linker_si);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800375
376 // version >= 4
377 ElfW(Relr)* relr_;
378 size_t relr_count_;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800379
380 // version >= 5
381 std::unique_ptr<soinfo_tls> tls_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700382};
383
384// This function is used by dlvsym() to calculate hash of sym_ver
385uint32_t calculate_elf_hash(const char* name);
386
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700387const char* fix_dt_needed(const char* dt_needed, const char* sopath);
388
389template<typename F>
390void for_each_dt_needed(const soinfo* si, F action) {
391 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
392 if (d->d_tag == DT_NEEDED) {
393 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
394 }
395 }
396}