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