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