blob: f0bde55cde075023a5180b8356b64ad6fea13522 [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>
50bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
51 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
52 const auto rel = rel_iterator.next();
53
Dmitriy Ivanov18a69562015-02-04 16:05:30 -080054 if (rel == nullptr) {
55 return false;
56 }
57
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080058 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
59 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
60
61 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
62 ElfW(Addr) sym_addr = 0;
63 const char* sym_name = nullptr;
64
65 DEBUG("Processing '%s' relocation at index %zd", this->name, idx);
66 if (type == R_GENERIC_NONE) {
67 continue;
68 }
69
70 ElfW(Sym)* s = nullptr;
71 soinfo* lsi = nullptr;
72
73 if (sym != 0) {
74 sym_name = get_string(symtab_[sym].st_name);
75 s = soinfo_do_lookup(this, sym_name, &lsi, global_group,local_group);
76 if (s == nullptr) {
77 // mips does not support relocation with weak-undefined symbols
78 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
79 return false;
80 } else {
81 // We got a definition.
82 sym_addr = lsi->resolve_symbol_address(s);
83 }
84 count_relocation(kRelocSymbol);
85 }
86
87 switch (type) {
88 case R_MIPS_REL32:
89#if defined(__LP64__)
90 // MIPS Elf64_Rel entries contain compound relocations
91 // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
92 if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
93 ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
94 DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
Nikola Veljkovicdb3078d2015-01-28 16:18:52 +010095 type, static_cast<unsigned>(ELF64_R_TYPE2(rel->r_info)),
96 static_cast<unsigned>(ELF64_R_TYPE3(rel->r_info)), rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -080097 return false;
98 }
99#endif
100 count_relocation(s == nullptr ? kRelocAbsolute : kRelocRelative);
101 MARK(rel->r_offset);
102 TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
103 static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
104 if (s != nullptr) {
105 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
106 } else {
107 *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
108 }
109 break;
110 default:
111 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
112 return false;
113 }
114 }
115 return true;
116}
117
118bool soinfo::mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
119 ElfW(Addr)** got = plt_got_;
120 if (got == nullptr) {
121 return true;
122 }
123
124 // got[0] is the address of the lazy resolver function.
125 // got[1] may be used for a GNU extension.
126 // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
127 // FIXME: maybe this should be in a separate routine?
128 if ((flags_ & FLAG_LINKER) == 0) {
129 size_t g = 0;
130 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
131 if (reinterpret_cast<intptr_t>(got[g]) < 0) {
132 got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
133 }
134 // Relocate the local GOT entries.
135 for (; g < mips_local_gotno_; g++) {
136 got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias);
137 }
138 }
139
140 // Now for the global GOT entries...
141 ElfW(Sym)* sym = symtab_ + mips_gotsym_;
142 got = plt_got_ + mips_local_gotno_;
143 for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) {
144 // This is an undefined reference... try to locate it.
145 const char* sym_name = get_string(sym->st_name);
146 soinfo* lsi = nullptr;
147 ElfW(Sym)* s = soinfo_do_lookup(this, sym_name, &lsi, global_group, local_group);
148 if (s == nullptr) {
149 // We only allow an undefined symbol if this is a weak reference.
150 s = &symtab_[g];
151 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
152 DL_ERR("cannot locate \"%s\"...", sym_name);
153 return false;
154 }
155 *got = 0;
156 } else {
157 // FIXME: is this sufficient?
158 // For reference see NetBSD link loader
159 // 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
160 *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s));
161 }
162 }
163 return true;
164}
165