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