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