blob: 87b811f004cfaa6fd3130a8907df55397871ced7 [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
29#include "linker.h"
30#include "linker_debug.h"
31#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080032#include "linker_reloc_iterators.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070033#include "linker_sleb128.h"
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080034
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080035template bool soinfo::relocate<plain_reloc_iterator>(plain_reloc_iterator&& rel_iterator,
36 const soinfo_list_t& global_group,
37 const soinfo_list_t& local_group);
38
39template bool soinfo::relocate<packed_reloc_iterator<sleb128_decoder>>(
40 packed_reloc_iterator<sleb128_decoder>&& rel_iterator,
41 const soinfo_list_t& global_group,
42 const soinfo_list_t& local_group);
43
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080044template <typename ElfRelIteratorT>
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -070045bool soinfo::relocate(ElfRelIteratorT&& rel_iterator,
46 const soinfo_list_t& global_group,
47 const soinfo_list_t& local_group) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070048 VersionTracker version_tracker;
49
50 if (!version_tracker.init(this)) {
51 return false;
52 }
53
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080054 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
55 const auto rel = rel_iterator.next();
56
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080057 if (rel == nullptr) {
58 return false;
59 }
60
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080061 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
62 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
63
64 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
65 ElfW(Addr) sym_addr = 0;
66 const char* sym_name = nullptr;
67
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070068 DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080069 if (type == R_GENERIC_NONE) {
70 continue;
71 }
72
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070073 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080074 soinfo* lsi = nullptr;
75
76 if (sym != 0) {
77 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070078 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
79 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
80
81 if (sym_ver == VER_NDX_LOCAL || sym_ver == VER_NDX_GLOBAL) {
82 // there is no version info for this one
83 if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) {
84 return false;
85 }
86 } else {
87 const version_info* vi = version_tracker.get_version_info(sym_ver);
88
89 if (vi == nullptr) {
90 DL_ERR("cannot find verneed/verdef for version index=%d "
91 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_soname());
92 return false;
93 }
94
95 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
96 return false;
97 }
98 }
99
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800100 if (s == nullptr) {
101 // mips does not support relocation with weak-undefined symbols
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700102 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800103 return false;
104 } else {
105 // We got a definition.
106 sym_addr = lsi->resolve_symbol_address(s);
107 }
108 count_relocation(kRelocSymbol);
109 }
110
111 switch (type) {
112 case R_MIPS_REL32:
113#if defined(__LP64__)
114 // MIPS Elf64_Rel entries contain compound relocations
115 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
116 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
117 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
118 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Nikola Veljkovicdb3078d2015-01-28 16:18:52 +0100119 type, static_cast<unsigned>(ELF64_R_TYPE2(rel->r_info)),
120 static_cast<unsigned>(ELF64_R_TYPE3(rel->r_info)), rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800121 return false;
122 }
123#endif
124 count_relocation(s == nullptr ? kRelocAbsolute : kRelocRelative);
125 MARK(rel->r_offset);
126 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
127 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
128 if (s != nullptr) {
129 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
130 } else {
Dmitriy Ivanov0373d4f2015-04-29 14:41:06 -0700131 *reinterpret_cast<ElfW(Addr)*>(reloc) += load_bias;
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800132 }
133 break;
134 default:
135 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
136 return false;
137 }
138 }
139 return true;
140}
141
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700142bool soinfo::mips_relocate_got(const soinfo_list_t& global_group,
143 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...
166 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
167 got = plt_got_ + mips_local_gotno_;
168 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
169 // This is an undefined reference... try to locate it.
170 const char* sym_name = get_string(sym->st_name);
171 soinfo* lsi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700172 const ElfW(Sym)* s = nullptr;
173 if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) {
174 return false;
175 }
176
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800177 if (s == nullptr) {
178 // We only allow an undefined symbol if this is a weak reference.
179 s = &symtab_[g];
180 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
181 DL_ERR("cannot locate \"%s\"...", sym_name);
182 return false;
183 }
184 *got = 0;
185 } else {
186 // FIXME: is this sufficient?
187 // For reference see NetBSD link loader
188 // 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
189 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
190 }
191 }
192 return true;
193}
194