blob: 87e385db3f894b41893fc51b4c8256422e21c680 [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;
113 TlsModule* module = nullptr;
114};
115
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700116#if defined(__work_around_b_24465209__)
117#define SOINFO_NAME_LEN 128
118#endif
119
120struct soinfo {
121#if defined(__work_around_b_24465209__)
122 private:
123 char old_name_[SOINFO_NAME_LEN];
124#endif
125 public:
126 const ElfW(Phdr)* phdr;
127 size_t phnum;
128#if defined(__work_around_b_24465209__)
129 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
130#endif
131 ElfW(Addr) base;
132 size_t size;
133
134#if defined(__work_around_b_24465209__)
135 uint32_t unused1; // DO NOT USE, maintained for compatibility.
136#endif
137
138 ElfW(Dyn)* dynamic;
139
140#if defined(__work_around_b_24465209__)
141 uint32_t unused2; // DO NOT USE, maintained for compatibility
142 uint32_t unused3; // DO NOT USE, maintained for compatibility
143#endif
144
145 soinfo* next;
146 private:
147 uint32_t flags_;
148
149 const char* strtab_;
150 ElfW(Sym)* symtab_;
151
152 size_t nbucket_;
153 size_t nchain_;
154 uint32_t* bucket_;
155 uint32_t* chain_;
156
157#if defined(__mips__) || !defined(__LP64__)
158 // This is only used by mips and mips64, but needs to be here for
159 // all 32-bit architectures to preserve binary compatibility.
160 ElfW(Addr)** plt_got_;
161#endif
162
163#if defined(USE_RELA)
164 ElfW(Rela)* plt_rela_;
165 size_t plt_rela_count_;
166
167 ElfW(Rela)* rela_;
168 size_t rela_count_;
169#else
170 ElfW(Rel)* plt_rel_;
171 size_t plt_rel_count_;
172
173 ElfW(Rel)* rel_;
174 size_t rel_count_;
175#endif
176
177 linker_ctor_function_t* preinit_array_;
178 size_t preinit_array_count_;
179
180 linker_ctor_function_t* init_array_;
181 size_t init_array_count_;
182 linker_dtor_function_t* fini_array_;
183 size_t fini_array_count_;
184
185 linker_ctor_function_t init_func_;
186 linker_dtor_function_t fini_func_;
187
188#if defined(__arm__)
189 public:
190 // ARM EABI section used for stack unwinding.
191 uint32_t* ARM_exidx;
192 size_t ARM_exidx_count;
193 private:
194#elif defined(__mips__)
195 uint32_t mips_symtabno_;
196 uint32_t mips_local_gotno_;
197 uint32_t mips_gotsym_;
198 bool mips_relocate_got(const VersionTracker& version_tracker,
199 const soinfo_list_t& global_group,
200 const soinfo_list_t& local_group);
201#if !defined(__LP64__)
202 bool mips_check_and_adjust_fp_modes();
203#endif
204#endif
205 size_t ref_count_;
206 public:
207 link_map link_map_head;
208
209 bool constructors_called;
210
211 // When you read a virtual address from the ELF file, add this
212 // value to get the corresponding address in the process' address space.
213 ElfW(Addr) load_bias;
214
215#if !defined(__LP64__)
216 bool has_text_relocations;
217#endif
218 bool has_DT_SYMBOLIC;
219
220 public:
221 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
222 off64_t file_offset, int rtld_flags);
223 ~soinfo();
224
225 void call_constructors();
226 void call_destructors();
227 void call_pre_init_constructors();
228 bool prelink_image();
229 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
230 const android_dlextinfo* extinfo);
231 bool protect_relro();
232
233 void add_child(soinfo* child);
234 void remove_all_links();
235
236 ino_t get_st_ino() const;
237 dev_t get_st_dev() const;
238 off64_t get_file_offset() const;
239
240 uint32_t get_rtld_flags() const;
241 uint32_t get_dt_flags_1() const;
242 void set_dt_flags_1(uint32_t dt_flags_1);
243
244 soinfo_list_t& get_children();
245 const soinfo_list_t& get_children() const;
246
247 soinfo_list_t& get_parents();
248
249 bool find_symbol_by_name(SymbolName& symbol_name,
250 const version_info* vi,
251 const ElfW(Sym)** symbol) const;
252
253 ElfW(Sym)* find_symbol_by_address(const void* addr);
254 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
255
256 const char* get_string(ElfW(Word) index) const;
257 bool can_unload() const;
258 bool is_gnu_hash() const;
259
260 bool inline has_min_version(uint32_t min_version __unused) const {
261#if defined(__work_around_b_24465209__)
262 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
263#else
264 return true;
265#endif
266 }
267
268 bool is_linked() const;
269 bool is_linker() const;
270 bool is_main_executable() const;
271
272 void set_linked();
273 void set_linker_flag();
274 void set_main_executable();
275 void set_nodelete();
276
dimitry965d06d2017-11-28 16:03:07 +0100277 size_t increment_ref_count();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700278 size_t decrement_ref_count();
dimitry06016f22018-01-05 11:39:28 +0100279 size_t get_ref_count() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700280
281 soinfo* get_local_group_root() const;
282
283 void set_soname(const char* soname);
284 const char* get_soname() const;
285 const char* get_realpath() const;
286 const ElfW(Versym)* get_versym(size_t n) const;
287 ElfW(Addr) get_verneed_ptr() const;
288 size_t get_verneed_cnt() const;
289 ElfW(Addr) get_verdef_ptr() const;
290 size_t get_verdef_cnt() const;
291
Elliott Hughesff1428a2018-11-12 16:01:37 -0800292 int get_target_sdk_version() const;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700293
294 void set_dt_runpath(const char *);
295 const std::vector<std::string>& get_dt_runpath() const;
296 android_namespace_t* get_primary_namespace();
297 void add_secondary_namespace(android_namespace_t* secondary_ns);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800298 android_namespace_list_t& get_secondary_namespaces();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700299
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800300 soinfo_tls* get_tls() const;
301
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700302 void set_mapped_by_caller(bool reserved_map);
303 bool is_mapped_by_caller() const;
304
305 uintptr_t get_handle() const;
306 void generate_handle();
307 void* to_handle();
308
309 private:
dimitry965d06d2017-11-28 16:03:07 +0100310 bool is_image_linked() const;
311 void set_image_linked();
312
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700313 bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
314 ElfW(Sym)* elf_addr_lookup(const void* addr);
315 bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
316 ElfW(Sym)* gnu_addr_lookup(const void* addr);
317
318 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
319 const char* sym_name, const version_info** vi);
320
321 template<typename ElfRelIteratorT>
322 bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
323 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800324 bool relocate_relr();
Rahul Chaudhryf16b6592018-01-25 15:34:15 -0800325 void apply_relr_reloc(ElfW(Addr) offset);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700326
327 private:
328 // This part of the structure is only available
329 // when FLAG_NEW_SOINFO is set in this->flags.
330 uint32_t version_;
331
332 // version >= 0
333 dev_t st_dev_;
334 ino_t st_ino_;
335
336 // dependency graph
337 soinfo_list_t children_;
338 soinfo_list_t parents_;
339
340 // version >= 1
341 off64_t file_offset_;
342 uint32_t rtld_flags_;
343 uint32_t dt_flags_1_;
344 size_t strtab_size_;
345
346 // version >= 2
347
348 size_t gnu_nbucket_;
349 uint32_t* gnu_bucket_;
350 uint32_t* gnu_chain_;
351 uint32_t gnu_maskwords_;
352 uint32_t gnu_shift2_;
353 ElfW(Addr)* gnu_bloom_filter_;
354
355 soinfo* local_group_root_;
356
357 uint8_t* android_relocs_;
358 size_t android_relocs_size_;
359
360 const char* soname_;
361 std::string realpath_;
362
363 const ElfW(Versym)* versym_;
364
365 ElfW(Addr) verdef_ptr_;
366 size_t verdef_cnt_;
367
368 ElfW(Addr) verneed_ptr_;
369 size_t verneed_cnt_;
370
Elliott Hughesff1428a2018-11-12 16:01:37 -0800371 int target_sdk_version_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700372
373 // version >= 3
374 std::vector<std::string> dt_runpath_;
375 android_namespace_t* primary_namespace_;
376 android_namespace_list_t secondary_namespaces_;
377 uintptr_t handle_;
378
Ryan Prichard04896452018-08-20 17:44:42 -0700379 friend soinfo* get_libdl_info(const char* linker_path, const soinfo& linker_si);
Rahul Chaudhryb7feec72017-12-19 15:25:23 -0800380
381 // version >= 4
382 ElfW(Relr)* relr_;
383 size_t relr_count_;
Ryan Pricharde5e69e02019-01-01 18:53:48 -0800384
385 // version >= 5
386 std::unique_ptr<soinfo_tls> tls_;
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700387};
388
389// This function is used by dlvsym() to calculate hash of sym_ver
390uint32_t calculate_elf_hash(const char* name);
391
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700392const char* fix_dt_needed(const char* dt_needed, const char* sopath);
393
394template<typename F>
395void for_each_dt_needed(const soinfo* si, F action) {
396 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
397 if (d->d_tag == DT_NEEDED) {
398 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
399 }
400 }
401}