blob: 273c36cfd31980ade50cf92edd5bef5843e85875 [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.
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070055#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
56
57#define SOINFO_VERSION 3
58
59typedef void (*linker_dtor_function_t)();
60typedef void (*linker_ctor_function_t)(int, char**, char**);
61
62class SymbolName {
63 public:
64 explicit SymbolName(const char* name)
65 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
66 elf_hash_(0), gnu_hash_(0) { }
67
68 const char* get_name() {
69 return name_;
70 }
71
72 uint32_t elf_hash();
73 uint32_t gnu_hash();
74
75 private:
76 const char* name_;
77 bool has_elf_hash_;
78 bool has_gnu_hash_;
79 uint32_t elf_hash_;
80 uint32_t gnu_hash_;
81
82 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
83};
84
85struct version_info {
86 constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
87
88 uint32_t elf_hash;
89 const char* name;
90 const soinfo* target_si;
91};
92
93// TODO(dimitry): remove reference from soinfo member functions to this class.
94class VersionTracker;
95
96#if defined(__work_around_b_24465209__)
97#define SOINFO_NAME_LEN 128
98#endif
99
100struct soinfo {
101#if defined(__work_around_b_24465209__)
102 private:
103 char old_name_[SOINFO_NAME_LEN];
104#endif
105 public:
106 const ElfW(Phdr)* phdr;
107 size_t phnum;
108#if defined(__work_around_b_24465209__)
109 ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
110#endif
111 ElfW(Addr) base;
112 size_t size;
113
114#if defined(__work_around_b_24465209__)
115 uint32_t unused1; // DO NOT USE, maintained for compatibility.
116#endif
117
118 ElfW(Dyn)* dynamic;
119
120#if defined(__work_around_b_24465209__)
121 uint32_t unused2; // DO NOT USE, maintained for compatibility
122 uint32_t unused3; // DO NOT USE, maintained for compatibility
123#endif
124
125 soinfo* next;
126 private:
127 uint32_t flags_;
128
129 const char* strtab_;
130 ElfW(Sym)* symtab_;
131
132 size_t nbucket_;
133 size_t nchain_;
134 uint32_t* bucket_;
135 uint32_t* chain_;
136
137#if defined(__mips__) || !defined(__LP64__)
138 // This is only used by mips and mips64, but needs to be here for
139 // all 32-bit architectures to preserve binary compatibility.
140 ElfW(Addr)** plt_got_;
141#endif
142
143#if defined(USE_RELA)
144 ElfW(Rela)* plt_rela_;
145 size_t plt_rela_count_;
146
147 ElfW(Rela)* rela_;
148 size_t rela_count_;
149#else
150 ElfW(Rel)* plt_rel_;
151 size_t plt_rel_count_;
152
153 ElfW(Rel)* rel_;
154 size_t rel_count_;
155#endif
156
157 linker_ctor_function_t* preinit_array_;
158 size_t preinit_array_count_;
159
160 linker_ctor_function_t* init_array_;
161 size_t init_array_count_;
162 linker_dtor_function_t* fini_array_;
163 size_t fini_array_count_;
164
165 linker_ctor_function_t init_func_;
166 linker_dtor_function_t fini_func_;
167
168#if defined(__arm__)
169 public:
170 // ARM EABI section used for stack unwinding.
171 uint32_t* ARM_exidx;
172 size_t ARM_exidx_count;
173 private:
174#elif defined(__mips__)
175 uint32_t mips_symtabno_;
176 uint32_t mips_local_gotno_;
177 uint32_t mips_gotsym_;
178 bool mips_relocate_got(const VersionTracker& version_tracker,
179 const soinfo_list_t& global_group,
180 const soinfo_list_t& local_group);
181#if !defined(__LP64__)
182 bool mips_check_and_adjust_fp_modes();
183#endif
184#endif
185 size_t ref_count_;
186 public:
187 link_map link_map_head;
188
189 bool constructors_called;
190
191 // When you read a virtual address from the ELF file, add this
192 // value to get the corresponding address in the process' address space.
193 ElfW(Addr) load_bias;
194
195#if !defined(__LP64__)
196 bool has_text_relocations;
197#endif
198 bool has_DT_SYMBOLIC;
199
200 public:
201 soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
202 off64_t file_offset, int rtld_flags);
203 ~soinfo();
204
205 void call_constructors();
206 void call_destructors();
207 void call_pre_init_constructors();
208 bool prelink_image();
209 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
210 const android_dlextinfo* extinfo);
211 bool protect_relro();
212
213 void add_child(soinfo* child);
214 void remove_all_links();
215
216 ino_t get_st_ino() const;
217 dev_t get_st_dev() const;
218 off64_t get_file_offset() const;
219
220 uint32_t get_rtld_flags() const;
221 uint32_t get_dt_flags_1() const;
222 void set_dt_flags_1(uint32_t dt_flags_1);
223
224 soinfo_list_t& get_children();
225 const soinfo_list_t& get_children() const;
226
227 soinfo_list_t& get_parents();
228
229 bool find_symbol_by_name(SymbolName& symbol_name,
230 const version_info* vi,
231 const ElfW(Sym)** symbol) const;
232
233 ElfW(Sym)* find_symbol_by_address(const void* addr);
234 ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
235
236 const char* get_string(ElfW(Word) index) const;
237 bool can_unload() const;
238 bool is_gnu_hash() const;
239
240 bool inline has_min_version(uint32_t min_version __unused) const {
241#if defined(__work_around_b_24465209__)
242 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
243#else
244 return true;
245#endif
246 }
247
248 bool is_linked() const;
249 bool is_linker() const;
250 bool is_main_executable() const;
251
252 void set_linked();
253 void set_linker_flag();
254 void set_main_executable();
255 void set_nodelete();
256
dimitry965d06d2017-11-28 16:03:07 +0100257 size_t increment_ref_count();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700258 size_t decrement_ref_count();
259
260 soinfo* get_local_group_root() const;
261
262 void set_soname(const char* soname);
263 const char* get_soname() const;
264 const char* get_realpath() const;
265 const ElfW(Versym)* get_versym(size_t n) const;
266 ElfW(Addr) get_verneed_ptr() const;
267 size_t get_verneed_cnt() const;
268 ElfW(Addr) get_verdef_ptr() const;
269 size_t get_verdef_cnt() const;
270
271 uint32_t get_target_sdk_version() const;
272
273 void set_dt_runpath(const char *);
274 const std::vector<std::string>& get_dt_runpath() const;
275 android_namespace_t* get_primary_namespace();
276 void add_secondary_namespace(android_namespace_t* secondary_ns);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800277 android_namespace_list_t& get_secondary_namespaces();
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700278
279 void set_mapped_by_caller(bool reserved_map);
280 bool is_mapped_by_caller() const;
281
282 uintptr_t get_handle() const;
283 void generate_handle();
284 void* to_handle();
285
286 private:
dimitry965d06d2017-11-28 16:03:07 +0100287 bool is_image_linked() const;
288 void set_image_linked();
289
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700290 bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
291 ElfW(Sym)* elf_addr_lookup(const void* addr);
292 bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
293 ElfW(Sym)* gnu_addr_lookup(const void* addr);
294
295 bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
296 const char* sym_name, const version_info** vi);
297
298 template<typename ElfRelIteratorT>
299 bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
300 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
301
302 private:
303 // This part of the structure is only available
304 // when FLAG_NEW_SOINFO is set in this->flags.
305 uint32_t version_;
306
307 // version >= 0
308 dev_t st_dev_;
309 ino_t st_ino_;
310
311 // dependency graph
312 soinfo_list_t children_;
313 soinfo_list_t parents_;
314
315 // version >= 1
316 off64_t file_offset_;
317 uint32_t rtld_flags_;
318 uint32_t dt_flags_1_;
319 size_t strtab_size_;
320
321 // version >= 2
322
323 size_t gnu_nbucket_;
324 uint32_t* gnu_bucket_;
325 uint32_t* gnu_chain_;
326 uint32_t gnu_maskwords_;
327 uint32_t gnu_shift2_;
328 ElfW(Addr)* gnu_bloom_filter_;
329
330 soinfo* local_group_root_;
331
332 uint8_t* android_relocs_;
333 size_t android_relocs_size_;
334
335 const char* soname_;
336 std::string realpath_;
337
338 const ElfW(Versym)* versym_;
339
340 ElfW(Addr) verdef_ptr_;
341 size_t verdef_cnt_;
342
343 ElfW(Addr) verneed_ptr_;
344 size_t verneed_cnt_;
345
346 uint32_t target_sdk_version_;
347
348 // version >= 3
349 std::vector<std::string> dt_runpath_;
350 android_namespace_t* primary_namespace_;
351 android_namespace_list_t secondary_namespaces_;
352 uintptr_t handle_;
353
dimitry7abea572017-08-29 18:14:49 +0200354 friend soinfo* get_libdl_info(const char* linker_path,
355 const soinfo& linker_si,
356 const link_map& linker_map);
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700357};
358
359// This function is used by dlvsym() to calculate hash of sym_ver
360uint32_t calculate_elf_hash(const char* name);
361
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700362const char* fix_dt_needed(const char* dt_needed, const char* sopath);
363
364template<typename F>
365void for_each_dt_needed(const soinfo* si, F action) {
366 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
367 if (d->d_tag == DT_NEEDED) {
368 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
369 }
370 }
371}
372
Dimitry Ivanov48ec2882016-08-04 11:50:36 -0700373#endif /* __LINKER_SOINFO_H */