1 /*
2 * Copyright (C) 2008 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 */
29 #ifndef _LINKER_H_
30 #define _LINKER_H_
32 #include <elf.h>
33 #include <inttypes.h>
34 #include <link.h>
35 #include <unistd.h>
36 #include <android/dlext.h>
37 #include <sys/stat.h>
39 #include "private/libc_logging.h"
40 #include "linked_list.h"
42 #define DL_ERR(fmt, x...) \
43 do { \
44 __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
45 /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
46 DEBUG("%s\n", linker_get_error_buffer()); \
47 } while (false)
49 #define DL_WARN(fmt, x...) \
50 do { \
51 __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
52 __libc_format_fd(2, "WARNING: linker: "); \
53 __libc_format_fd(2, fmt, ##x); \
54 __libc_format_fd(2, "\n"); \
55 } while (false)
57 #if defined(__LP64__)
58 #define ELFW(what) ELF64_ ## what
59 #else
60 #define ELFW(what) ELF32_ ## what
61 #endif
63 // mips64 interprets Elf64_Rel structures' r_info field differently.
64 // bionic (like other C libraries) has macros that assume regular ELF files,
65 // but the dynamic linker needs to be able to load mips64 ELF files.
66 #if defined(__mips__) && defined(__LP64__)
67 #undef ELF64_R_SYM
68 #undef ELF64_R_TYPE
69 #undef ELF64_R_INFO
70 #define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
71 #define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
72 #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
73 #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
74 #define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
75 #endif
77 // Returns the address of the page containing address 'x'.
78 #define PAGE_START(x) ((x) & PAGE_MASK)
80 // Returns the offset of address 'x' in its page.
81 #define PAGE_OFFSET(x) ((x) & ~PAGE_MASK)
83 // Returns the address of the next page after address 'x', unless 'x' is
84 // itself at the start of a page.
85 #define PAGE_END(x) PAGE_START((x) + (PAGE_SIZE-1))
87 #define FLAG_LINKED 0x00000001
88 #define FLAG_EXE 0x00000004 // The main executable
89 #define FLAG_LINKER 0x00000010 // The linker itself
90 #define FLAG_GNU_HASH 0x00000040 // uses gnu hash
91 #define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
93 #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
95 #define SOINFO_VERSION 1
97 #define SOINFO_NAME_LEN 128
99 typedef void (*linker_function_t)();
101 // Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
102 #if defined(__aarch64__) || defined(__x86_64__)
103 #define USE_RELA 1
104 #endif
106 struct soinfo;
108 class SoinfoListAllocator {
109 public:
110 static LinkedListEntry<soinfo>* alloc();
111 static void free(LinkedListEntry<soinfo>* entry);
113 private:
114 // unconstructable
115 DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
116 };
118 class SymbolName {
119 public:
120 explicit SymbolName(const char* name)
121 : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
122 elf_hash_(0), gnu_hash_(0) { }
124 const char* get_name() {
125 return name_;
126 }
128 uint32_t elf_hash();
129 uint32_t gnu_hash();
131 private:
132 const char* name_;
133 bool has_elf_hash_;
134 bool has_gnu_hash_;
135 uint32_t elf_hash_;
136 uint32_t gnu_hash_;
138 DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
139 };
141 struct soinfo {
142 public:
143 typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
144 public:
145 char name[SOINFO_NAME_LEN];
146 const ElfW(Phdr)* phdr;
147 size_t phnum;
148 ElfW(Addr) entry;
149 ElfW(Addr) base;
150 size_t size;
152 #ifndef __LP64__
153 uint32_t unused1; // DO NOT USE, maintained for compatibility.
154 #endif
156 ElfW(Dyn)* dynamic;
158 #ifndef __LP64__
159 uint32_t unused2; // DO NOT USE, maintained for compatibility
160 uint32_t unused3; // DO NOT USE, maintained for compatibility
161 #endif
163 soinfo* next;
164 private:
165 uint32_t flags_;
167 const char* strtab_;
168 ElfW(Sym)* symtab_;
170 size_t nbucket_;
171 size_t nchain_;
172 uint32_t* bucket_;
173 uint32_t* chain_;
175 #if defined(__mips__) || !defined(__LP64__)
176 // This is only used by mips and mips64, but needs to be here for
177 // all 32-bit architectures to preserve binary compatibility.
178 ElfW(Addr)** plt_got_;
179 #endif
181 #if defined(USE_RELA)
182 ElfW(Rela)* plt_rela_;
183 size_t plt_rela_count_;
185 ElfW(Rela)* rela_;
186 size_t rela_count_;
187 #else
188 ElfW(Rel)* plt_rel_;
189 size_t plt_rel_count_;
191 ElfW(Rel)* rel_;
192 size_t rel_count_;
193 #endif
195 linker_function_t* preinit_array_;
196 size_t preinit_array_count_;
198 linker_function_t* init_array_;
199 size_t init_array_count_;
200 linker_function_t* fini_array_;
201 size_t fini_array_count_;
203 linker_function_t init_func_;
204 linker_function_t fini_func_;
206 #if defined(__arm__)
207 public:
208 // ARM EABI section used for stack unwinding.
209 uint32_t* ARM_exidx;
210 size_t ARM_exidx_count;
211 private:
212 #elif defined(__mips__)
213 uint32_t mips_symtabno_;
214 uint32_t mips_local_gotno_;
215 uint32_t mips_gotsym_;
216 bool mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group);
218 #endif
219 size_t ref_count_;
220 public:
221 link_map link_map_head;
223 bool constructors_called;
225 // When you read a virtual address from the ELF file, add this
226 // value to get the corresponding address in the process' address space.
227 ElfW(Addr) load_bias;
229 #if !defined(__LP64__)
230 bool has_text_relocations;
231 #endif
232 bool has_DT_SYMBOLIC;
234 public:
235 soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags);
237 void call_constructors();
238 void call_destructors();
239 void call_pre_init_constructors();
240 bool prelink_image();
241 bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, const android_dlextinfo* extinfo);
243 void add_child(soinfo* child);
244 void remove_all_links();
246 ino_t get_st_ino() const;
247 dev_t get_st_dev() const;
248 off64_t get_file_offset() const;
250 uint32_t get_rtld_flags() const;
251 uint32_t get_dt_flags_1() const;
252 void set_dt_flags_1(uint32_t dt_flags_1);
254 soinfo_list_t& get_children();
255 soinfo_list_t& get_parents();
257 ElfW(Sym)* find_symbol_by_name(SymbolName& symbol_name);
258 ElfW(Sym)* find_symbol_by_address(const void* addr);
259 ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
261 const char* get_string(ElfW(Word) index) const;
262 bool can_unload() const;
263 bool is_gnu_hash() const;
265 bool inline has_min_version(uint32_t min_version) const {
266 return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
267 }
269 bool is_linked() const;
270 bool is_main_executable() const;
272 void set_linked();
273 void set_linker_flag();
274 void set_main_executable();
276 void increment_ref_count();
277 size_t decrement_ref_count();
279 soinfo* get_local_group_root() const;
281 private:
282 ElfW(Sym)* elf_lookup(SymbolName& symbol_name);
283 ElfW(Sym)* elf_addr_lookup(const void* addr);
284 ElfW(Sym)* gnu_lookup(SymbolName& symbol_name);
285 ElfW(Sym)* gnu_addr_lookup(const void* addr);
287 void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
288 void call_function(const char* function_name, linker_function_t function);
289 #if defined(USE_RELA)
290 int relocate(ElfW(Rela)* rela, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
291 #else
292 int relocate(ElfW(Rel)* rel, unsigned count, const soinfo_list_t& global_group, const soinfo_list_t& local_group);
293 #endif
295 private:
296 // This part of the structure is only available
297 // when FLAG_NEW_SOINFO is set in this->flags.
298 uint32_t version_;
300 // version >= 0
301 dev_t st_dev_;
302 ino_t st_ino_;
304 // dependency graph
305 soinfo_list_t children_;
306 soinfo_list_t parents_;
308 // version >= 1
309 off64_t file_offset_;
310 uint32_t rtld_flags_;
311 uint32_t dt_flags_1_;
312 size_t strtab_size_;
314 // version >= 2
315 uint32_t gnu_maskwords_;
316 uint32_t gnu_shift2_;
317 ElfW(Addr)* gnu_bloom_filter_;
319 soinfo* local_group_root_;
321 friend soinfo* get_libdl_info();
322 };
324 soinfo* get_libdl_info();
326 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
327 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
328 soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
329 void do_dlclose(soinfo* si);
331 ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
332 soinfo* find_containing_library(const void* addr);
334 ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
336 void debuggerd_init();
337 extern "C" abort_msg_t* g_abort_message;
338 extern "C" void notify_gdb_of_libraries();
340 char* linker_get_error_buffer();
341 size_t linker_get_error_buffer_size();
343 #endif