| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 1 | /* | 
|  | 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 Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 32 | #include "linker_reloc_iterators.h" | 
| Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 33 | #include "linker_sleb128.h" | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 34 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 35 | template 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 |  | 
|  | 39 | template 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 Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 44 | template <typename ElfRelIteratorT> | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 45 | bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, | 
|  | 46 | const soinfo_list_t& global_group, | 
|  | 47 | const soinfo_list_t& local_group) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 48 | VersionTracker version_tracker; | 
|  | 49 |  | 
|  | 50 | if (!version_tracker.init(this)) { | 
|  | 51 | return false; | 
|  | 52 | } | 
|  | 53 |  | 
| Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 54 | for (size_t idx = 0; rel_iterator.has_next(); ++idx) { | 
|  | 55 | const auto rel = rel_iterator.next(); | 
|  | 56 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 57 | if (rel == nullptr) { | 
|  | 58 | return false; | 
|  | 59 | } | 
|  | 60 |  | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 61 | 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 Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 68 | DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 69 | if (type == R_GENERIC_NONE) { | 
|  | 70 | continue; | 
|  | 71 | } | 
|  | 72 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 73 | const ElfW(Sym)* s = nullptr; | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 74 | soinfo* lsi = nullptr; | 
|  | 75 |  | 
|  | 76 | if (sym != 0) { | 
|  | 77 | sym_name = get_string(symtab_[sym].st_name); | 
| Dmitriy Ivanov | 114bd83 | 2015-04-30 16:11:48 -0700 | [diff] [blame^] | 78 | const version_info* vi = nullptr; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 79 |  | 
| Dmitriy Ivanov | 114bd83 | 2015-04-30 16:11:48 -0700 | [diff] [blame^] | 80 | if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) { | 
|  | 81 | return false; | 
|  | 82 | } | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 83 |  | 
| Dmitriy Ivanov | 114bd83 | 2015-04-30 16:11:48 -0700 | [diff] [blame^] | 84 | if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) { | 
|  | 85 | return false; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 86 | } | 
|  | 87 |  | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 88 | if (s == nullptr) { | 
|  | 89 | // mips does not support relocation with weak-undefined symbols | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 90 | DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname()); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 91 | return false; | 
|  | 92 | } else { | 
|  | 93 | // We got a definition. | 
|  | 94 | sym_addr = lsi->resolve_symbol_address(s); | 
|  | 95 | } | 
|  | 96 | count_relocation(kRelocSymbol); | 
|  | 97 | } | 
|  | 98 |  | 
|  | 99 | switch (type) { | 
|  | 100 | case R_MIPS_REL32: | 
|  | 101 | #if defined(__LP64__) | 
|  | 102 | // MIPS Elf64_Rel entries contain compound relocations | 
|  | 103 | // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case | 
|  | 104 | if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 || | 
|  | 105 | ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) { | 
|  | 106 | DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)", | 
| Nikola Veljkovic | db3078d | 2015-01-28 16:18:52 +0100 | [diff] [blame] | 107 | type, static_cast<unsigned>(ELF64_R_TYPE2(rel->r_info)), | 
|  | 108 | static_cast<unsigned>(ELF64_R_TYPE3(rel->r_info)), rel, idx); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 109 | return false; | 
|  | 110 | } | 
|  | 111 | #endif | 
|  | 112 | count_relocation(s == nullptr ? kRelocAbsolute : kRelocRelative); | 
|  | 113 | MARK(rel->r_offset); | 
|  | 114 | TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc), | 
|  | 115 | static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*"); | 
|  | 116 | if (s != nullptr) { | 
|  | 117 | *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; | 
|  | 118 | } else { | 
| Dmitriy Ivanov | e97d519 | 2015-04-29 14:41:06 -0700 | [diff] [blame] | 119 | *reinterpret_cast<ElfW(Addr)*>(reloc) += load_bias; | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 120 | } | 
|  | 121 | break; | 
|  | 122 | default: | 
|  | 123 | DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx); | 
|  | 124 | return false; | 
|  | 125 | } | 
|  | 126 | } | 
|  | 127 | return true; | 
|  | 128 | } | 
|  | 129 |  | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 130 | bool soinfo::mips_relocate_got(const soinfo_list_t& global_group, | 
|  | 131 | const soinfo_list_t& local_group) { | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 132 | ElfW(Addr)** got = plt_got_; | 
|  | 133 | if (got == nullptr) { | 
|  | 134 | return true; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | // got[0] is the address of the lazy resolver function. | 
|  | 138 | // got[1] may be used for a GNU extension. | 
|  | 139 | // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start). | 
|  | 140 | // FIXME: maybe this should be in a separate routine? | 
|  | 141 | if ((flags_ & FLAG_LINKER) == 0) { | 
|  | 142 | size_t g = 0; | 
|  | 143 | got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef); | 
|  | 144 | if (reinterpret_cast<intptr_t>(got[g]) < 0) { | 
|  | 145 | got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed); | 
|  | 146 | } | 
|  | 147 | // Relocate the local GOT entries. | 
|  | 148 | for (; g < mips_local_gotno_; g++) { | 
|  | 149 | got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + load_bias); | 
|  | 150 | } | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | // Now for the global GOT entries... | 
|  | 154 | ElfW(Sym)* sym = symtab_ + mips_gotsym_; | 
|  | 155 | got = plt_got_ + mips_local_gotno_; | 
|  | 156 | for (size_t g = mips_gotsym_; g < mips_symtabno_; g++, sym++, got++) { | 
|  | 157 | // This is an undefined reference... try to locate it. | 
|  | 158 | const char* sym_name = get_string(sym->st_name); | 
|  | 159 | soinfo* lsi = nullptr; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 160 | const ElfW(Sym)* s = nullptr; | 
|  | 161 | if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) { | 
|  | 162 | return false; | 
|  | 163 | } | 
|  | 164 |  | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 165 | if (s == nullptr) { | 
|  | 166 | // We only allow an undefined symbol if this is a weak reference. | 
|  | 167 | s = &symtab_[g]; | 
|  | 168 | if (ELF_ST_BIND(s->st_info) != STB_WEAK) { | 
|  | 169 | DL_ERR("cannot locate \"%s\"...", sym_name); | 
|  | 170 | return false; | 
|  | 171 | } | 
|  | 172 | *got = 0; | 
|  | 173 | } else { | 
|  | 174 | // FIXME: is this sufficient? | 
|  | 175 | // For reference see NetBSD link loader | 
|  | 176 | // 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 | 
|  | 177 | *got = reinterpret_cast<ElfW(Addr)*>(lsi->resolve_symbol_address(s)); | 
|  | 178 | } | 
|  | 179 | } | 
|  | 180 | return true; | 
|  | 181 | } | 
|  | 182 |  |