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