blob: 27fb68bcb2aad6ed76d1a6b58d3f9abed34d374b [file] [log] [blame]
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08001/*
2 * Copyright (C) 2015 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
Duane Sandbc425c72015-06-01 16:29:14 -070029#if !defined(__LP64__) && __mips_isa_rev >= 5
30#include <sys/prctl.h>
31#if defined(PR_SET_FP_MODE)
32#error "remove following defs when avail in Android's kernel headers"
33#else
34#define PR_SET_FP_MODE 45
35#define PR_GET_FP_MODE 46
36#define PR_FP_MODE_FR (1 << 0)
37#define PR_FP_MODE_FRE (1 << 1)
38#endif
39#endif
40
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080041#include "linker.h"
42#include "linker_debug.h"
Duane Sandbc425c72015-06-01 16:29:14 -070043#include "linker_phdr.h"
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080044#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080045#include "linker_reloc_iterators.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070046#include "linker_sleb128.h"
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080047
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -070048template bool soinfo::relocate<plain_reloc_iterator>(const VersionTracker& version_tracker,
49 plain_reloc_iterator&& rel_iterator,
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080050 const soinfo_list_t& global_group,
51 const soinfo_list_t& local_group);
52
53template bool soinfo::relocate<packed_reloc_iterator<sleb128_decoder>>(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -070054 const VersionTracker& version_tracker,
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080055 packed_reloc_iterator<sleb128_decoder>&& rel_iterator,
56 const soinfo_list_t& global_group,
57 const soinfo_list_t& local_group);
58
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059template <typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -070060bool soinfo::relocate(const VersionTracker& version_tracker,
61 ElfRelIteratorT&& rel_iterator,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -070062 const soinfo_list_t& global_group,
63 const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080064 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
65 const auto rel = rel_iterator.next();
66
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080067 if (rel == nullptr) {
68 return false;
69 }
70
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080071 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
72 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
73
74 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
75 ElfW(Addr) sym_addr = 0;
76 const char* sym_name = nullptr;
77
Duane Sandbc425c72015-06-01 16:29:14 -070078 DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080079 if (type == R_GENERIC_NONE) {
80 continue;
81 }
82
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070083 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080084 soinfo* lsi = nullptr;
85
86 if (sym != 0) {
87 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -070088 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070089
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -070090 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
91 return false;
92 }
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070093
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -070094 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
95 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070096 }
97
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080098 if (s == nullptr) {
99 // mips does not support relocation with weak-undefined symbols
Duane Sandbc425c72015-06-01 16:29:14 -0700100 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...",
101 sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800102 return false;
103 } else {
104 // We got a definition.
105 sym_addr = lsi->resolve_symbol_address(s);
106 }
107 count_relocation(kRelocSymbol);
108 }
109
110 switch (type) {
111 case R_MIPS_REL32:
112#if defined(__LP64__)
113 // MIPS Elf64_Rel entries contain compound relocations
114 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
115 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
116 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
117 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Nikola Veljkovicdb3078d2015-01-28 16:18:52 +0100118 type, static_cast<unsigned>(ELF64_R_TYPE2(rel->r_info)),
119 static_cast<unsigned>(ELF64_R_TYPE3(rel->r_info)), rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800120 return false;
121 }
122#endif
123 count_relocation(s == nullptr ? kRelocAbsolute : kRelocRelative);
124 MARK(rel->r_offset);
125 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
126 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
127 if (s != nullptr) {
128 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
129 } else {
Dmitriy Ivanov0373d4f2015-04-29 14:41:06 -0700130 *reinterpret_cast<ElfW(Addr)*>(reloc) += load_bias;
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800131 }
132 break;
133 default:
134 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
135 return false;
136 }
137 }
138 return true;
139}
140
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700141bool soinfo::mips_relocate_got(const VersionTracker& version_tracker,
142 const soinfo_list_t& global_group,
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700143 const soinfo_list_t& local_group) {
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800144 ElfW(Addr)** got = plt_got_;
145 if (got == nullptr) {
146 return true;
147 }
148
149 // got[0] is the address of the lazy resolver function.
150 // got[1] may be used for a GNU extension.
151 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
152 // FIXME: maybe this should be in a separate routine?
153 if ((flags_ & FLAG_LINKER) == 0) {
154 size_t g = 0;
155 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
156 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
157 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
158 }
159 // Relocate the local GOT entries.
160 for (; g < mips_local_gotno_; g++) {
161 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
162 }
163 }
164
165 // Now for the global GOT entries...
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800166 got = plt_got_ + mips_local_gotno_;
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700167 for (ElfW(Word) sym = mips_gotsym_; sym < mips_symtabno_; sym++, got++) {
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800168 // This is an undefined reference... try to locate it.
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700169 const ElfW(Sym)* local_sym = symtab_ + sym;
170 const char* sym_name = get_string(local_sym->st_name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800171 soinfo* lsi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700172 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700173
Dmitriy Ivanovdbe26fd2015-05-04 19:30:49 -0700174 ElfW(Word) st_visibility = (local_sym->st_other & 0x3);
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700175
Dmitriy Ivanovdbe26fd2015-05-04 19:30:49 -0700176 if (st_visibility == STV_DEFAULT) {
177 const version_info* vi = nullptr;
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700178
Dmitriy Ivanovdbe26fd2015-05-04 19:30:49 -0700179 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
180 return false;
181 }
182
183 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
184 return false;
185 }
186 } else if (st_visibility == STV_PROTECTED) {
187 if (local_sym->st_value == 0) {
Duane Sandbc425c72015-06-01 16:29:14 -0700188 DL_ERR("%s: invalid symbol \"%s\" (PROTECTED/UNDEFINED) ",
189 get_realpath(), sym_name);
Dmitriy Ivanovdbe26fd2015-05-04 19:30:49 -0700190 return false;
191 }
192 s = local_sym;
193 lsi = this;
194 } else {
Duane Sandbc425c72015-06-01 16:29:14 -0700195 DL_ERR("%s: invalid symbol \"%s\" visibility: 0x%x",
196 get_realpath(), sym_name, st_visibility);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700197 return false;
198 }
199
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800200 if (s == nullptr) {
201 // We only allow an undefined symbol if this is a weak reference.
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -0700202 if (ELF_ST_BIND(local_sym->st_info) != STB_WEAK) {
Duane Sandbc425c72015-06-01 16:29:14 -0700203 DL_ERR("%s: cannot locate \"%s\"...", get_realpath(), sym_name);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800204 return false;
205 }
206 *got = 0;
207 } else {
208 // FIXME: is this sufficient?
209 // For reference see NetBSD link loader
210 // http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
211 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
212 }
213 }
214 return true;
215}
216
Duane Sandbc425c72015-06-01 16:29:14 -0700217#if !defined(__LP64__)
218
219// Checks for mips32's various floating point abis.
220// (Mips64 Android has a single floating point abi and doesn't need any checks)
221
222// Linux kernel has declarations similar to the following
223// in <linux>/arch/mips/include/asm/elf.h,
224// but that non-uapi internal header file will never be imported
225// into bionic's kernel headers.
226
227#define PT_MIPS_ABIFLAGS 0x70000003 // is .MIPS.abiflags segment
228
229struct mips_elf_abiflags_v0 {
230 uint16_t version; // version of this structure
231 uint8_t isa_level, isa_rev, gpr_size, cpr1_size, cpr2_size;
232 uint8_t fp_abi; // mips32 ABI variants for floating point
Douglas Leungdccc4082015-09-17 15:16:53 -0700233 uint32_t isa_ext, ases, flags1, flags2;
Duane Sandbc425c72015-06-01 16:29:14 -0700234};
235
236// Bits of flags1:
237#define MIPS_AFL_FLAGS1_ODDSPREG 1 // Uses odd-numbered single-prec fp regs
238
239// Some values of fp_abi: via compiler flag:
240#define MIPS_ABI_FP_DOUBLE 1 // -mdouble-float
241#define MIPS_ABI_FP_XX 5 // -mfpxx
242#define MIPS_ABI_FP_64A 7 // -mips32r* -mfp64 -mno-odd-spreg
243
244#if __mips_isa_rev >= 5
245static bool mips_fre_mode_on = false; // have set FRE=1 mode for process
246#endif
247
248bool soinfo::mips_check_and_adjust_fp_modes() {
249 mips_elf_abiflags_v0* abiflags = nullptr;
250 int mips_fpabi;
251
252 // Find soinfo's optional .MIPS.abiflags segment
253 for (size_t i = 0; i<phnum; ++i) {
254 const ElfW(Phdr)& ph = phdr[i];
255 if (ph.p_type == PT_MIPS_ABIFLAGS) {
256 if (ph.p_filesz < sizeof (mips_elf_abiflags_v0)) {
257 DL_ERR("Corrupt PT_MIPS_ABIFLAGS header found \"%s\"", get_realpath());
258 return false;
259 }
260 abiflags = reinterpret_cast<mips_elf_abiflags_v0*>(ph.p_vaddr + load_bias);
261 break;
262 }
263 }
264
265 // FP ABI-variant compatibility checks for MIPS o32 ABI
266 if (abiflags == nullptr) {
Duane Sand0acb15e2015-07-28 14:04:29 -0700267 // Old compilers and some translators don't emit the new abiflags section.
268 const char* filename = get_realpath();
269 size_t len = strlen(filename);
270 if (len > 4 && (strcmp(filename+len-4, ".dex") == 0 ||
271 strcmp(filename+len-4, ".oat") == 0 )) {
272 // Assume dex2oat is compatible with target
273 mips_fpabi = MIPS_ABI_FP_XX;
274 } else {
275 // Old Android compilers used -mfp32 -mdouble-float -modd-spreg defaults,
276 // ie FP32 aka DOUBLE, using FR=0 mode fpregs & odd single-prec fpregs
277 mips_fpabi = MIPS_ABI_FP_DOUBLE;
278 }
Duane Sandbc425c72015-06-01 16:29:14 -0700279 } else {
280 mips_fpabi = abiflags->fp_abi;
281 if ( (abiflags->flags1 & MIPS_AFL_FLAGS1_ODDSPREG)
282 && (mips_fpabi == MIPS_ABI_FP_XX ||
283 mips_fpabi == MIPS_ABI_FP_64A ) ) {
284 // Android supports fewer cases than Linux
285 DL_ERR("Unsupported odd-single-prec FloatPt reg uses in \"%s\"",
286 get_realpath());
287 return false;
288 }
289 }
290 if (!(mips_fpabi == MIPS_ABI_FP_DOUBLE ||
291#if __mips_isa_rev >= 5
292 mips_fpabi == MIPS_ABI_FP_64A ||
293#endif
294 mips_fpabi == MIPS_ABI_FP_XX )) {
295 DL_ERR("Unsupported MIPS32 FloatPt ABI %d found in \"%s\"",
296 mips_fpabi, get_realpath());
297 return false;
298 }
299
300#if __mips_isa_rev >= 5
301 // Adjust process's FR Emulation mode, if needed
302 //
303 // On Mips R5 & R6, Android runs continuously in FR=1 64bit-fpreg mode.
304 // NDK mips32 apps compiled with old compilers generate FP32 code
305 // which expects FR=0 32-bit fp registers.
306 // NDK mips32 apps compiled with newer compilers generate modeless
307 // FPXX code which runs on both FR=0 and FR=1 modes.
308 // Android itself is compiled in FP64A which requires FR=1 mode.
309 // FP32, FPXX, and FP64A all interlink okay, without dynamic FR mode
310 // changes during calls. For details, see
311 // http://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking
312 // Processes containing FR32 FR=0 code are run via kernel software assist,
313 // which maps all odd-numbered single-precision reg refs onto the
314 // upper half of the paired even-numbered double-precision reg.
315 // FRE=1 triggers traps to the kernel's emulator on every single-precision
316 // fp op (for both odd and even-numbered registers).
317 // Turning on FRE=1 traps is done at most once per process, simultanously
318 // for all threads of that process, when dlopen discovers FP32 code.
319 // The kernel repacks threads' registers when FRE mode is turn on or off.
320 // These asynchronous adjustments are wrong if any thread was executing
321 // FPXX code using odd-numbered single-precision regs.
322 // Current Android compilers default to the -mno-oddspreg option,
323 // and this requirement is checked by Android's dlopen.
324 // So FRE can always be safely turned on for FP32, anytime.
325 // Deferred enhancement: Allow loading of odd-spreg FPXX modules.
326
327 if (mips_fpabi == MIPS_ABI_FP_DOUBLE && !mips_fre_mode_on) {
328 // Turn on FRE mode, which emulates mode-sensitive FR=0 code on FR=1
329 // register files, by trapping to kernel on refs to single-precision regs
330 if (prctl(PR_SET_FP_MODE, PR_FP_MODE_FR|PR_FP_MODE_FRE)) {
331 DL_ERR("Kernel or cpu failed to set FRE mode required for running \"%s\"",
332 get_realpath());
333 return false;
334 }
335 DL_WARN("Using FRE=1 mode to run \"%s\"", get_realpath());
336 mips_fre_mode_on = true; // Avoid future redundant mode-switch calls
337 // FRE mode is never turned back off.
338 // Deferred enhancement:
339 // Reset FRE mode when dlclose() removes all FP32 modules
340 }
341#else
342 // Android runs continuously in FR=0 32bit-fpreg mode.
343#endif // __mips_isa_rev
344 return true;
345}
346
347#endif // __LP64___