blob: c162111e98594070077527e6862c1a9265089299 [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 Ivanov18a69562015-02-04 16:05:30 -080033#include "linker_leb128.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
44template bool soinfo::relocate<packed_reloc_iterator<leb128_decoder>>(
45 packed_reloc_iterator<leb128_decoder>&& rel_iterator,
46 const soinfo_list_t& global_group,
47 const soinfo_list_t& local_group);
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080048
49template <typename ElfRelIteratorT>
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -070050bool soinfo::relocate(ElfRelIteratorT&& rel_iterator,
51 const soinfo_list_t& global_group,
52 const soinfo_list_t& local_group) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070053 VersionTracker version_tracker;
54
55 if (!version_tracker.init(this)) {
56 return false;
57 }
58
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080059 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
60 const auto rel = rel_iterator.next();
61
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080062 if (rel == nullptr) {
63 return false;
64 }
65
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080066 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
67 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
68
69 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
70 ElfW(Addr) sym_addr = 0;
71 const char* sym_name = nullptr;
72
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -070073 DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080074 if (type == R_GENERIC_NONE) {
75 continue;
76 }
77
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070078 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080079 soinfo* lsi = nullptr;
80
81 if (sym != 0) {
82 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -070083 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
84 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
85
86 if (sym_ver == VER_NDX_LOCAL || sym_ver == VER_NDX_GLOBAL) {
87 // there is no version info for this one
88 if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) {
89 return false;
90 }
91 } else {
92 const version_info* vi = version_tracker.get_version_info(sym_ver);
93
94 if (vi == nullptr) {
95 DL_ERR("cannot find verneed/verdef for version index=%d "
96 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_soname());
97 return false;
98 }
99
100 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
101 return false;
102 }
103 }
104
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800105 if (s == nullptr) {
106 // mips does not support relocation with weak-undefined symbols
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700107 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800108 return false;
109 } else {
110 // We got a definition.
111 sym_addr = lsi->resolve_symbol_address(s);
112 }
113 count_relocation(kRelocSymbol);
114 }
115
116 switch (type) {
117 case R_MIPS_REL32:
118#if defined(__LP64__)
119 // MIPS Elf64_Rel entries contain compound relocations
120 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
121 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
122 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
123 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Nikola Veljkovicdb3078d2015-01-28 16:18:52 +0100124 type, static_cast<unsigned>(ELF64_R_TYPE2(rel->r_info)),
125 static_cast<unsigned>(ELF64_R_TYPE3(rel->r_info)), rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800126 return false;
127 }
128#endif
129 count_relocation(s == nullptr ? kRelocAbsolute : kRelocRelative);
130 MARK(rel->r_offset);
131 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
132 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
133 if (s != nullptr) {
134 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
135 } else {
136 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
137 }
138 break;
139 default:
140 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
141 return false;
142 }
143 }
144 return true;
145}
146
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700147bool soinfo::mips_relocate_got(const soinfo_list_t& global_group,
148 const soinfo_list_t& local_group) {
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800149 ElfW(Addr)** got = plt_got_;
150 if (got == nullptr) {
151 return true;
152 }
153
154 // got[0] is the address of the lazy resolver function.
155 // got[1] may be used for a GNU extension.
156 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
157 // FIXME: maybe this should be in a separate routine?
158 if ((flags_ & FLAG_LINKER) == 0) {
159 size_t g = 0;
160 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
161 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
162 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
163 }
164 // Relocate the local GOT entries.
165 for (; g < mips_local_gotno_; g++) {
166 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
167 }
168 }
169
170 // Now for the global GOT entries...
171 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
172 got = plt_got_ + mips_local_gotno_;
173 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
174 // This is an undefined reference... try to locate it.
175 const char* sym_name = get_string(sym->st_name);
176 soinfo* lsi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700177 const ElfW(Sym)* s = nullptr;
178 if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) {
179 return false;
180 }
181
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800182 if (s == nullptr) {
183 // We only allow an undefined symbol if this is a weak reference.
184 s = &symtab_[g];
185 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
186 DL_ERR("cannot locate \"%s\"...", sym_name);
187 return false;
188 }
189 *got = 0;
190 } else {
191 // FIXME: is this sufficient?
192 // For reference see NetBSD link loader
193 // 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
194 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
195 }
196 }
197 return true;
198}
199