Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "elf_file.h" |
| 18 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 19 | #include <inttypes.h> |
Nicolas Geoffray | a7f198c | 2014-03-10 11:12:54 +0000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Ian Rogers | d582fa4 | 2014-11-05 23:46:43 -0800 | [diff] [blame] | 23 | #include "arch/instruction_set.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | #include "base/logging.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 25 | #include "base/stringprintf.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 27 | #include "base/unix_file/fd_file.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 28 | #include "elf_file_impl.h" |
| 29 | #include "elf_utils.h" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 30 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 31 | #include "utils.h" |
| 32 | |
| 33 | namespace art { |
| 34 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 35 | // ------------------------------------------------------------------- |
| 36 | // Binary GDB JIT Interface as described in |
| 37 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 38 | extern "C" { |
| 39 | typedef enum { |
| 40 | JIT_NOACTION = 0, |
| 41 | JIT_REGISTER_FN, |
| 42 | JIT_UNREGISTER_FN |
| 43 | } JITAction; |
| 44 | |
| 45 | struct JITCodeEntry { |
| 46 | JITCodeEntry* next_; |
| 47 | JITCodeEntry* prev_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 48 | const uint8_t *symfile_addr_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 49 | uint64_t symfile_size_; |
| 50 | }; |
| 51 | |
| 52 | struct JITDescriptor { |
| 53 | uint32_t version_; |
| 54 | uint32_t action_flag_; |
| 55 | JITCodeEntry* relevant_entry_; |
| 56 | JITCodeEntry* first_entry_; |
| 57 | }; |
| 58 | |
| 59 | // GDB will place breakpoint into this function. |
| 60 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 61 | // and inline assembler statement inside. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 62 | void __attribute__((noinline)) __jit_debug_register_code(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 63 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 64 | __asm__(""); |
| 65 | } |
| 66 | |
| 67 | // GDB will inspect contents of this descriptor. |
| 68 | // Static initialization is necessary to prevent GDB from seeing |
| 69 | // uninitialized descriptor. |
| 70 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 71 | } |
| 72 | |
| 73 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 74 | static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr, |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 75 | uintptr_t symfile_size) { |
| 76 | JITCodeEntry* entry = new JITCodeEntry; |
| 77 | entry->symfile_addr_ = symfile_addr; |
| 78 | entry->symfile_size_ = symfile_size; |
| 79 | entry->prev_ = nullptr; |
| 80 | |
| 81 | // TODO: Do we need a lock here? |
| 82 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 83 | if (entry->next_ != nullptr) { |
| 84 | entry->next_->prev_ = entry; |
| 85 | } |
| 86 | __jit_debug_descriptor.first_entry_ = entry; |
| 87 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 88 | |
| 89 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
| 90 | __jit_debug_register_code(); |
| 91 | return entry; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | static void UnregisterCodeEntry(JITCodeEntry* entry) { |
| 96 | // TODO: Do we need a lock here? |
| 97 | if (entry->prev_ != nullptr) { |
| 98 | entry->prev_->next_ = entry->next_; |
| 99 | } else { |
| 100 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 101 | } |
| 102 | |
| 103 | if (entry->next_ != nullptr) { |
| 104 | entry->next_->prev_ = entry->prev_; |
| 105 | } |
| 106 | |
| 107 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 108 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 109 | __jit_debug_register_code(); |
| 110 | delete entry; |
| 111 | } |
| 112 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 113 | template <typename ElfTypes> |
| 114 | ElfFileImpl<ElfTypes>::ElfFileImpl(File* file, bool writable, |
| 115 | bool program_header_only, |
| 116 | uint8_t* requested_base) |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 117 | : file_(file), |
| 118 | writable_(writable), |
| 119 | program_header_only_(program_header_only), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 120 | header_(nullptr), |
| 121 | base_address_(nullptr), |
| 122 | program_headers_start_(nullptr), |
| 123 | section_headers_start_(nullptr), |
| 124 | dynamic_program_header_(nullptr), |
| 125 | dynamic_section_start_(nullptr), |
| 126 | symtab_section_start_(nullptr), |
| 127 | dynsym_section_start_(nullptr), |
| 128 | strtab_section_start_(nullptr), |
| 129 | dynstr_section_start_(nullptr), |
| 130 | hash_section_start_(nullptr), |
| 131 | symtab_symbol_table_(nullptr), |
| 132 | dynsym_symbol_table_(nullptr), |
| 133 | jit_elf_image_(nullptr), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 134 | jit_gdb_entry_(nullptr), |
| 135 | requested_base_(requested_base) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 136 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 137 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 138 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 139 | template <typename ElfTypes> |
| 140 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open( |
| 141 | File* file, bool writable, bool program_header_only, |
| 142 | std::string* error_msg, uint8_t* requested_base) { |
| 143 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes> |
| 144 | (file, writable, program_header_only, requested_base)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 145 | int prot; |
| 146 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 147 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 148 | prot = PROT_READ | PROT_WRITE; |
| 149 | flags = MAP_SHARED; |
| 150 | } else { |
| 151 | prot = PROT_READ; |
| 152 | flags = MAP_PRIVATE; |
| 153 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 154 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 155 | return nullptr; |
| 156 | } |
| 157 | return elf_file.release(); |
| 158 | } |
| 159 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 160 | template <typename ElfTypes> |
| 161 | ElfFileImpl<ElfTypes>* ElfFileImpl<ElfTypes>::Open( |
| 162 | File* file, int prot, int flags, std::string* error_msg) { |
| 163 | std::unique_ptr<ElfFileImpl<ElfTypes>> elf_file(new ElfFileImpl<ElfTypes> |
| 164 | (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false, |
| 165 | /*requested_base*/nullptr)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 166 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 167 | return nullptr; |
| 168 | } |
| 169 | return elf_file.release(); |
| 170 | } |
| 171 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 172 | template <typename ElfTypes> |
| 173 | bool ElfFileImpl<ElfTypes>::Setup(int prot, int flags, std::string* error_msg) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 174 | int64_t temp_file_length = file_->GetLength(); |
| 175 | if (temp_file_length < 0) { |
| 176 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 177 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 178 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 179 | return false; |
| 180 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 181 | size_t file_length = static_cast<size_t>(temp_file_length); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 182 | if (file_length < sizeof(Elf_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 183 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of " |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 184 | "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 185 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 189 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 190 | // first just map ELF header to get program header size information |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 191 | size_t elf_header_size = sizeof(Elf_Ehdr); |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 192 | if (!SetMap(MemMap::MapFile(elf_header_size, |
| 193 | prot, |
| 194 | flags, |
| 195 | file_->Fd(), |
| 196 | 0, |
| 197 | /*low4_gb*/false, |
| 198 | file_->GetPath().c_str(), |
| 199 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 200 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 201 | return false; |
| 202 | } |
| 203 | // then remap to cover program header |
| 204 | size_t program_header_size = header_->e_phoff + (header_->e_phentsize * header_->e_phnum); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 205 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 206 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF program " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 207 | "header of %zd bytes: '%s'", file_length, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 208 | sizeof(Elf_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 209 | return false; |
| 210 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 211 | if (!SetMap(MemMap::MapFile(program_header_size, |
| 212 | prot, |
| 213 | flags, |
| 214 | file_->Fd(), |
| 215 | 0, |
| 216 | /*low4_gb*/false, |
| 217 | file_->GetPath().c_str(), |
| 218 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 219 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 220 | *error_msg = StringPrintf("Failed to map ELF program headers: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 221 | return false; |
| 222 | } |
| 223 | } else { |
| 224 | // otherwise map entire file |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 225 | if (!SetMap(MemMap::MapFile(file_->GetLength(), |
| 226 | prot, |
| 227 | flags, |
| 228 | file_->Fd(), |
| 229 | 0, |
| 230 | /*low4_gb*/false, |
| 231 | file_->GetPath().c_str(), |
| 232 | error_msg), |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 233 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 234 | *error_msg = StringPrintf("Failed to map ELF file: %s", error_msg->c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 235 | return false; |
| 236 | } |
| 237 | } |
| 238 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 239 | if (program_header_only_) { |
| 240 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 241 | } else { |
| 242 | if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) { |
| 243 | return false; |
| 244 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 245 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 246 | // Setup section headers. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 247 | if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) { |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | // Find shstrtab. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 252 | Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 253 | if (shstrtab_section_header == nullptr) { |
| 254 | *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'", |
| 255 | file_->GetPath().c_str()); |
| 256 | return false; |
| 257 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 258 | |
| 259 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 260 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 261 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 262 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 263 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 267 | if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 268 | reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 269 | return false; |
| 270 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 271 | |
| 272 | // Find other sections from section headers |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 273 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 274 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 275 | if (section_header == nullptr) { |
| 276 | *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'", |
| 277 | i, file_->GetPath().c_str()); |
| 278 | return false; |
| 279 | } |
| 280 | switch (section_header->sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 281 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 282 | if (!CheckAndSet(section_header->sh_offset, "symtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 283 | reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 284 | return false; |
| 285 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 286 | break; |
| 287 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 288 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 289 | if (!CheckAndSet(section_header->sh_offset, "dynsym", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 290 | reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 291 | return false; |
| 292 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 293 | break; |
| 294 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 295 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 296 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 297 | if ((section_header->sh_flags & SHF_ALLOC) != 0) { |
| 298 | // Check that this is named ".dynstr" and ignore otherwise. |
| 299 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 300 | if (strncmp(".dynstr", header_name, 8) == 0) { |
| 301 | if (!CheckAndSet(section_header->sh_offset, "dynstr", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 302 | reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 303 | return false; |
| 304 | } |
| 305 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 306 | } else { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 307 | // Check that this is named ".strtab" and ignore otherwise. |
| 308 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 309 | if (strncmp(".strtab", header_name, 8) == 0) { |
| 310 | if (!CheckAndSet(section_header->sh_offset, "strtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 311 | reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 312 | return false; |
| 313 | } |
| 314 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 315 | } |
| 316 | break; |
| 317 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 318 | case SHT_DYNAMIC: { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 319 | if (reinterpret_cast<uint8_t*>(dynamic_section_start_) != |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 320 | Begin() + section_header->sh_offset) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 321 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 322 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 323 | << reinterpret_cast<void*>(dynamic_section_start_) |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 324 | << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 325 | return false; |
| 326 | } |
| 327 | break; |
| 328 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 329 | case SHT_HASH: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 330 | if (!CheckAndSet(section_header->sh_offset, "hash section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 331 | reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 332 | return false; |
| 333 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | } |
| 337 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 338 | |
| 339 | // Check for the existence of some sections. |
| 340 | if (!CheckSectionsExist(error_msg)) { |
| 341 | return false; |
| 342 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 343 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 344 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 345 | return true; |
| 346 | } |
| 347 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 348 | template <typename ElfTypes> |
| 349 | ElfFileImpl<ElfTypes>::~ElfFileImpl() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 350 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 351 | delete symtab_symbol_table_; |
| 352 | delete dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 353 | delete jit_elf_image_; |
| 354 | if (jit_gdb_entry_) { |
| 355 | UnregisterCodeEntry(jit_gdb_entry_); |
| 356 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 357 | } |
| 358 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 359 | template <typename ElfTypes> |
| 360 | bool ElfFileImpl<ElfTypes>::CheckAndSet(Elf32_Off offset, const char* label, |
| 361 | uint8_t** target, std::string* error_msg) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 362 | if (Begin() + offset >= End()) { |
| 363 | *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label, |
| 364 | file_->GetPath().c_str()); |
| 365 | return false; |
| 366 | } |
| 367 | *target = Begin() + offset; |
| 368 | return true; |
| 369 | } |
| 370 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 371 | template <typename ElfTypes> |
| 372 | bool ElfFileImpl<ElfTypes>::CheckSectionsLinked(const uint8_t* source, |
| 373 | const uint8_t* target) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 374 | // Only works in whole-program mode, as we need to iterate over the sections. |
| 375 | // Note that we normally can't search by type, as duplicates are allowed for most section types. |
| 376 | if (program_header_only_) { |
| 377 | return true; |
| 378 | } |
| 379 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 380 | Elf_Shdr* source_section = nullptr; |
| 381 | Elf_Word target_index = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 382 | bool target_found = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 383 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 384 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 385 | |
| 386 | if (Begin() + section_header->sh_offset == source) { |
| 387 | // Found the source. |
| 388 | source_section = section_header; |
| 389 | if (target_index) { |
| 390 | break; |
| 391 | } |
| 392 | } else if (Begin() + section_header->sh_offset == target) { |
| 393 | target_index = i; |
| 394 | target_found = true; |
| 395 | if (source_section != nullptr) { |
| 396 | break; |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | return target_found && source_section != nullptr && source_section->sh_link == target_index; |
| 402 | } |
| 403 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 404 | template <typename ElfTypes> |
| 405 | bool ElfFileImpl<ElfTypes>::CheckSectionsExist(std::string* error_msg) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 406 | if (!program_header_only_) { |
| 407 | // If in full mode, need section headers. |
| 408 | if (section_headers_start_ == nullptr) { |
| 409 | *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str()); |
| 410 | return false; |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // This is redundant, but defensive. |
| 415 | if (dynamic_program_header_ == nullptr) { |
| 416 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 417 | file_->GetPath().c_str()); |
| 418 | return false; |
| 419 | } |
| 420 | |
| 421 | // Need a dynamic section. This is redundant, but defensive. |
| 422 | if (dynamic_section_start_ == nullptr) { |
| 423 | *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'", |
| 424 | file_->GetPath().c_str()); |
| 425 | return false; |
| 426 | } |
| 427 | |
| 428 | // Symtab validation. These is not really a hard failure, as we are currently not using the |
| 429 | // symtab internally, but it's nice to be defensive. |
| 430 | if (symtab_section_start_ != nullptr) { |
| 431 | // When there's a symtab, there should be a strtab. |
| 432 | if (strtab_section_start_ == nullptr) { |
| 433 | *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str()); |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | // The symtab should link to the strtab. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 438 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_), |
| 439 | reinterpret_cast<const uint8_t*>(strtab_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 440 | *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'", |
| 441 | file_->GetPath().c_str()); |
| 442 | return false; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // We always need a dynstr & dynsym. |
| 447 | if (dynstr_section_start_ == nullptr) { |
| 448 | *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str()); |
| 449 | return false; |
| 450 | } |
| 451 | if (dynsym_section_start_ == nullptr) { |
| 452 | *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str()); |
| 453 | return false; |
| 454 | } |
| 455 | |
| 456 | // Need a hash section for dynamic symbol lookup. |
| 457 | if (hash_section_start_ == nullptr) { |
| 458 | *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'", |
| 459 | file_->GetPath().c_str()); |
| 460 | return false; |
| 461 | } |
| 462 | |
| 463 | // And the hash section should be linking to the dynsym. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 464 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_), |
| 465 | reinterpret_cast<const uint8_t*>(dynsym_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 466 | *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'", |
| 467 | file_->GetPath().c_str()); |
| 468 | return false; |
| 469 | } |
| 470 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 471 | // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for |
| 472 | // us). This is usually the last in an oat file, and a good indicator of whether writing was |
| 473 | // successful (or the process crashed and left garbage). |
| 474 | if (program_header_only_) { |
| 475 | // It might not be mapped, but we can compare against the file size. |
| 476 | int64_t offset = static_cast<int64_t>(GetHeader().e_shoff + |
| 477 | (GetHeader().e_shstrndx * GetHeader().e_shentsize)); |
| 478 | if (offset >= file_->GetLength()) { |
| 479 | *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'", |
| 480 | file_->GetPath().c_str()); |
| 481 | return false; |
| 482 | } |
| 483 | } |
| 484 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 485 | return true; |
| 486 | } |
| 487 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 488 | template <typename ElfTypes> |
| 489 | bool ElfFileImpl<ElfTypes>::SetMap(MemMap* map, std::string* error_msg) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 490 | if (map == nullptr) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 491 | // MemMap::Open should have already set an error. |
| 492 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 493 | return false; |
| 494 | } |
| 495 | map_.reset(map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 496 | CHECK(map_.get() != nullptr) << file_->GetPath(); |
| 497 | CHECK(map_->Begin() != nullptr) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 498 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 499 | header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin()); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 500 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 501 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 502 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 503 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 504 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 505 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 506 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 507 | header_->e_ident[EI_MAG0], |
| 508 | header_->e_ident[EI_MAG1], |
| 509 | header_->e_ident[EI_MAG2], |
| 510 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 511 | return false; |
| 512 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 513 | uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32; |
| 514 | if (elf_class != header_->e_ident[EI_CLASS]) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 515 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 516 | elf_class, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 517 | file_->GetPath().c_str(), |
| 518 | header_->e_ident[EI_CLASS]); |
| 519 | return false; |
| 520 | } |
| 521 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 522 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 523 | ELFDATA2LSB, |
| 524 | file_->GetPath().c_str(), |
| 525 | header_->e_ident[EI_CLASS]); |
| 526 | return false; |
| 527 | } |
| 528 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 529 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 530 | EV_CURRENT, |
| 531 | file_->GetPath().c_str(), |
| 532 | header_->e_ident[EI_CLASS]); |
| 533 | return false; |
| 534 | } |
| 535 | if (ET_DYN != header_->e_type) { |
| 536 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 537 | ET_DYN, |
| 538 | file_->GetPath().c_str(), |
| 539 | header_->e_type); |
| 540 | return false; |
| 541 | } |
| 542 | if (EV_CURRENT != header_->e_version) { |
| 543 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 544 | EV_CURRENT, |
| 545 | file_->GetPath().c_str(), |
| 546 | header_->e_version); |
| 547 | return false; |
| 548 | } |
| 549 | if (0 != header_->e_entry) { |
| 550 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 551 | 0, |
| 552 | file_->GetPath().c_str(), |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 553 | static_cast<int32_t>(header_->e_entry)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | if (0 == header_->e_phoff) { |
| 557 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 558 | file_->GetPath().c_str()); |
| 559 | return false; |
| 560 | } |
| 561 | if (0 == header_->e_shoff) { |
| 562 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 563 | file_->GetPath().c_str()); |
| 564 | return false; |
| 565 | } |
| 566 | if (0 == header_->e_ehsize) { |
| 567 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 568 | file_->GetPath().c_str()); |
| 569 | return false; |
| 570 | } |
| 571 | if (0 == header_->e_phentsize) { |
| 572 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 573 | file_->GetPath().c_str()); |
| 574 | return false; |
| 575 | } |
| 576 | if (0 == header_->e_phnum) { |
| 577 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 578 | file_->GetPath().c_str()); |
| 579 | return false; |
| 580 | } |
| 581 | if (0 == header_->e_shentsize) { |
| 582 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 583 | file_->GetPath().c_str()); |
| 584 | return false; |
| 585 | } |
| 586 | if (0 == header_->e_shnum) { |
| 587 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 588 | file_->GetPath().c_str()); |
| 589 | return false; |
| 590 | } |
| 591 | if (0 == header_->e_shstrndx) { |
| 592 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 593 | file_->GetPath().c_str()); |
| 594 | return false; |
| 595 | } |
| 596 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 597 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 598 | header_->e_shstrndx, |
| 599 | header_->e_shnum, |
| 600 | file_->GetPath().c_str()); |
| 601 | return false; |
| 602 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 603 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 604 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 605 | if (header_->e_phoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 606 | *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s", |
| 607 | static_cast<uint64_t>(header_->e_phoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 608 | Size(), |
| 609 | file_->GetPath().c_str()); |
| 610 | return false; |
| 611 | } |
| 612 | if (header_->e_shoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 613 | *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s", |
| 614 | static_cast<uint64_t>(header_->e_shoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 615 | Size(), |
| 616 | file_->GetPath().c_str()); |
| 617 | return false; |
| 618 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 619 | } |
| 620 | return true; |
| 621 | } |
| 622 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 623 | template <typename ElfTypes> |
| 624 | typename ElfTypes::Ehdr& ElfFileImpl<ElfTypes>::GetHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 625 | CHECK(header_ != nullptr); // Header has been checked in SetMap. This is a sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 626 | return *header_; |
| 627 | } |
| 628 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 629 | template <typename ElfTypes> |
| 630 | uint8_t* ElfFileImpl<ElfTypes>::GetProgramHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 631 | CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity |
| 632 | // check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 633 | return program_headers_start_; |
| 634 | } |
| 635 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 636 | template <typename ElfTypes> |
| 637 | uint8_t* ElfFileImpl<ElfTypes>::GetSectionHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 638 | CHECK(!program_header_only_); // Only used in "full" mode. |
| 639 | CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 640 | return section_headers_start_; |
| 641 | } |
| 642 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 643 | template <typename ElfTypes> |
| 644 | typename ElfTypes::Phdr& ElfFileImpl<ElfTypes>::GetDynamicProgramHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 645 | CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 646 | return *dynamic_program_header_; |
| 647 | } |
| 648 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 649 | template <typename ElfTypes> |
| 650 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::GetDynamicSectionStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 651 | CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 652 | return dynamic_section_start_; |
| 653 | } |
| 654 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 655 | template <typename ElfTypes> |
| 656 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbolSectionStart( |
| 657 | Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 658 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 659 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 660 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 661 | return symtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 662 | break; |
| 663 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 664 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 665 | return dynsym_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 666 | break; |
| 667 | } |
| 668 | default: { |
| 669 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 670 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 671 | } |
| 672 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 673 | } |
| 674 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 675 | template <typename ElfTypes> |
| 676 | const char* ElfFileImpl<ElfTypes>::GetStringSectionStart( |
| 677 | Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 678 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 679 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 680 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 681 | return strtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 682 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 683 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 684 | return dynstr_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 685 | } |
| 686 | default: { |
| 687 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 688 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 689 | } |
| 690 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 691 | } |
| 692 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 693 | template <typename ElfTypes> |
| 694 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Word section_type, |
| 695 | Elf_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 696 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 697 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 698 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 699 | } |
| 700 | const char* string_section_start = GetStringSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 701 | if (string_section_start == nullptr) { |
| 702 | return nullptr; |
| 703 | } |
| 704 | return string_section_start + i; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 707 | // WARNING: The following methods do not check for an error condition (non-existent hash section). |
| 708 | // It is the caller's job to do this. |
| 709 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 710 | template <typename ElfTypes> |
| 711 | typename ElfTypes::Word* ElfFileImpl<ElfTypes>::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 712 | return hash_section_start_; |
| 713 | } |
| 714 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 715 | template <typename ElfTypes> |
| 716 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 717 | return GetHashSectionStart()[0]; |
| 718 | } |
| 719 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 720 | template <typename ElfTypes> |
| 721 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 722 | return GetHashSectionStart()[1]; |
| 723 | } |
| 724 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 725 | template <typename ElfTypes> |
| 726 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashBucket(size_t i, bool* ok) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 727 | if (i >= GetHashBucketNum()) { |
| 728 | *ok = false; |
| 729 | return 0; |
| 730 | } |
| 731 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 732 | // 0 is nbucket, 1 is nchain |
| 733 | return GetHashSectionStart()[2 + i]; |
| 734 | } |
| 735 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 736 | template <typename ElfTypes> |
| 737 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetHashChain(size_t i, bool* ok) const { |
Yevgeny Rouban | acb0138 | 2014-11-24 13:40:56 +0600 | [diff] [blame] | 738 | if (i >= GetHashChainNum()) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 739 | *ok = false; |
| 740 | return 0; |
| 741 | } |
| 742 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 743 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 744 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 745 | } |
| 746 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 747 | template <typename ElfTypes> |
| 748 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 749 | return GetHeader().e_phnum; |
| 750 | } |
| 751 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 752 | template <typename ElfTypes> |
| 753 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::GetProgramHeader(Elf_Word i) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 754 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 755 | uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 756 | if (program_header >= End()) { |
| 757 | return nullptr; // Failure condition. |
| 758 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 759 | return reinterpret_cast<Elf_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 760 | } |
| 761 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 762 | template <typename ElfTypes> |
| 763 | typename ElfTypes::Phdr* ElfFileImpl<ElfTypes>::FindProgamHeaderByType(Elf_Word type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 764 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 765 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 766 | if (program_header->p_type == type) { |
| 767 | return program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 768 | } |
| 769 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 770 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 771 | } |
| 772 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 773 | template <typename ElfTypes> |
| 774 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 775 | return GetHeader().e_shnum; |
| 776 | } |
| 777 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 778 | template <typename ElfTypes> |
| 779 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionHeader(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 780 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 781 | // Even if we Load(), it doesn't bring in all the sections. |
| 782 | CHECK(!program_header_only_) << file_->GetPath(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 783 | if (i >= GetSectionHeaderNum()) { |
| 784 | return nullptr; // Failure condition. |
| 785 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 786 | uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 787 | if (section_header >= End()) { |
| 788 | return nullptr; // Failure condition. |
| 789 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 790 | return reinterpret_cast<Elf_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 791 | } |
| 792 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 793 | template <typename ElfTypes> |
| 794 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByType(Elf_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 795 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 796 | // We could change this to switch on known types if they were detected during loading. |
| 797 | CHECK(!program_header_only_) << file_->GetPath(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 798 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 799 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 800 | if (section_header->sh_type == type) { |
| 801 | return section_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 802 | } |
| 803 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 804 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 808 | static unsigned elfhash(const char *_name) { |
| 809 | const unsigned char *name = (const unsigned char *) _name; |
| 810 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 811 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 812 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 813 | h = (h << 4) + *name++; |
| 814 | g = h & 0xf0000000; |
| 815 | h ^= g; |
| 816 | h ^= g >> 24; |
| 817 | } |
| 818 | return h; |
| 819 | } |
| 820 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 821 | template <typename ElfTypes> |
| 822 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 823 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 824 | } |
| 825 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 826 | template <typename ElfTypes> |
| 827 | const uint8_t* ElfFileImpl<ElfTypes>::FindDynamicSymbolAddress( |
| 828 | const std::string& symbol_name) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 829 | // Check that we have a hash section. |
| 830 | if (GetHashSectionStart() == nullptr) { |
| 831 | return nullptr; // Failure condition. |
| 832 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 833 | const Elf_Sym* sym = FindDynamicSymbol(symbol_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 834 | if (sym != nullptr) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 835 | // TODO: we need to change this to calculate base_address_ in ::Open, |
| 836 | // otherwise it will be wrongly 0 if ::Load has not yet been called. |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 837 | return base_address_ + sym->st_value; |
| 838 | } else { |
| 839 | return nullptr; |
| 840 | } |
| 841 | } |
| 842 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 843 | // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 844 | template <typename ElfTypes> |
| 845 | const typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindDynamicSymbol( |
| 846 | const std::string& symbol_name) const { |
Andreas Gampe | c48b206 | 2014-09-08 23:39:45 -0700 | [diff] [blame] | 847 | if (GetHashBucketNum() == 0) { |
| 848 | // No dynamic symbols at all. |
| 849 | return nullptr; |
| 850 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 851 | Elf_Word hash = elfhash(symbol_name.c_str()); |
| 852 | Elf_Word bucket_index = hash % GetHashBucketNum(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 853 | bool ok; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 854 | Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 855 | if (!ok) { |
| 856 | return nullptr; |
| 857 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 858 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 859 | Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 860 | if (symbol == nullptr) { |
| 861 | return nullptr; // Failure condition. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 862 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 863 | const char* name = GetString(SHT_DYNSYM, symbol->st_name); |
| 864 | if (symbol_name == name) { |
| 865 | return symbol; |
| 866 | } |
| 867 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok); |
| 868 | if (!ok) { |
| 869 | return nullptr; |
| 870 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 871 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 872 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 873 | } |
| 874 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 875 | template <typename ElfTypes> |
| 876 | bool ElfFileImpl<ElfTypes>::IsSymbolSectionType(Elf_Word section_type) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 877 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
| 878 | } |
| 879 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 880 | template <typename ElfTypes> |
| 881 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetSymbolNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 882 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 883 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 884 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 885 | return section_header.sh_size / section_header.sh_entsize; |
| 886 | } |
| 887 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 888 | template <typename ElfTypes> |
| 889 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::GetSymbol(Elf_Word section_type, Elf_Word i) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 890 | Elf_Sym* sym_start = GetSymbolSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 891 | if (sym_start == nullptr) { |
| 892 | return nullptr; |
| 893 | } |
| 894 | return sym_start + i; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 895 | } |
| 896 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 897 | template <typename ElfTypes> |
| 898 | typename ElfFileImpl<ElfTypes>::SymbolTable** |
| 899 | ElfFileImpl<ElfTypes>::GetSymbolTable(Elf_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 900 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 901 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 902 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 903 | return &symtab_symbol_table_; |
| 904 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 905 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 906 | return &dynsym_symbol_table_; |
| 907 | } |
| 908 | default: { |
| 909 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 910 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | } |
| 914 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 915 | template <typename ElfTypes> |
| 916 | typename ElfTypes::Sym* ElfFileImpl<ElfTypes>::FindSymbolByName( |
| 917 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 918 | CHECK(!program_header_only_) << file_->GetPath(); |
| 919 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 920 | |
| 921 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 922 | if (*symbol_table != nullptr || build_map) { |
| 923 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 924 | DCHECK(build_map); |
| 925 | *symbol_table = new SymbolTable; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 926 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 927 | if (symbol_section == nullptr) { |
| 928 | return nullptr; // Failure condition. |
| 929 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 930 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 931 | if (string_section == nullptr) { |
| 932 | return nullptr; // Failure condition. |
| 933 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 934 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 935 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 936 | if (symbol == nullptr) { |
| 937 | return nullptr; // Failure condition. |
| 938 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 939 | unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) |
| 940 | ? ELF64_ST_TYPE(symbol->st_info) |
| 941 | : ELF32_ST_TYPE(symbol->st_info); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 942 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 943 | continue; |
| 944 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 945 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 946 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 947 | continue; |
| 948 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 949 | std::pair<typename SymbolTable::iterator, bool> result = |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 950 | (*symbol_table)->insert(std::make_pair(name, symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 951 | if (!result.second) { |
| 952 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 953 | if ((symbol->st_value != result.first->second->st_value) || |
| 954 | (symbol->st_size != result.first->second->st_size) || |
| 955 | (symbol->st_info != result.first->second->st_info) || |
| 956 | (symbol->st_other != result.first->second->st_other) || |
| 957 | (symbol->st_shndx != result.first->second->st_shndx)) { |
| 958 | return nullptr; // Failure condition. |
| 959 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 960 | } |
| 961 | } |
| 962 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 963 | CHECK(*symbol_table != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 964 | typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 965 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 966 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 967 | } |
| 968 | return it->second; |
| 969 | } |
| 970 | |
| 971 | // Fall back to linear search |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 972 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 973 | if (symbol_section == nullptr) { |
| 974 | return nullptr; |
| 975 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 976 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 977 | if (string_section == nullptr) { |
| 978 | return nullptr; |
| 979 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 980 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 981 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 982 | if (symbol == nullptr) { |
| 983 | return nullptr; // Failure condition. |
| 984 | } |
| 985 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 986 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 987 | continue; |
| 988 | } |
| 989 | if (symbol_name == name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 990 | return symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 991 | } |
| 992 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 993 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 994 | } |
| 995 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 996 | template <typename ElfTypes> |
| 997 | typename ElfTypes::Addr ElfFileImpl<ElfTypes>::FindSymbolAddress( |
| 998 | Elf_Word section_type, const std::string& symbol_name, bool build_map) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 999 | Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1000 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1001 | return 0; |
| 1002 | } |
| 1003 | return symbol->st_value; |
| 1004 | } |
| 1005 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1006 | template <typename ElfTypes> |
| 1007 | const char* ElfFileImpl<ElfTypes>::GetString(Elf_Shdr& string_section, |
| 1008 | Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1009 | CHECK(!program_header_only_) << file_->GetPath(); |
| 1010 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1011 | if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1012 | return nullptr; // Failure condition. |
| 1013 | } |
| 1014 | if (i >= string_section.sh_size) { |
| 1015 | return nullptr; |
| 1016 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1017 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1018 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1019 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1020 | uint8_t* strings = Begin() + string_section.sh_offset; |
| 1021 | uint8_t* string = strings + i; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1022 | if (string >= End()) { |
| 1023 | return nullptr; |
| 1024 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1025 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1028 | template <typename ElfTypes> |
| 1029 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetDynamicNum() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1030 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1033 | template <typename ElfTypes> |
| 1034 | typename ElfTypes::Dyn& ElfFileImpl<ElfTypes>::GetDynamic(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1035 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 1036 | return *(GetDynamicSectionStart() + i); |
| 1037 | } |
| 1038 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1039 | template <typename ElfTypes> |
| 1040 | typename ElfTypes::Dyn* ElfFileImpl<ElfTypes>::FindDynamicByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1041 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1042 | Elf_Dyn* dyn = &GetDynamic(i); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1043 | if (dyn->d_tag == type) { |
| 1044 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1045 | } |
| 1046 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1047 | return nullptr; |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1050 | template <typename ElfTypes> |
| 1051 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::FindDynamicValueByType(Elf_Sword type) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1052 | Elf_Dyn* dyn = FindDynamicByType(type); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1053 | if (dyn == nullptr) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1054 | return 0; |
| 1055 | } else { |
| 1056 | return dyn->d_un.d_val; |
| 1057 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1058 | } |
| 1059 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1060 | template <typename ElfTypes> |
| 1061 | typename ElfTypes::Rel* ElfFileImpl<ElfTypes>::GetRelSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1062 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1063 | return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1064 | } |
| 1065 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1066 | template <typename ElfTypes> |
| 1067 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1068 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1069 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 1070 | return section_header.sh_size / section_header.sh_entsize; |
| 1071 | } |
| 1072 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1073 | template <typename ElfTypes> |
| 1074 | typename ElfTypes::Rel& ElfFileImpl<ElfTypes>::GetRel(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1075 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1076 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 1077 | return *(GetRelSectionStart(section_header) + i); |
| 1078 | } |
| 1079 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1080 | template <typename ElfTypes> |
| 1081 | typename ElfTypes::Rela* ElfFileImpl<ElfTypes>::GetRelaSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1082 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1083 | return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1084 | } |
| 1085 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1086 | template <typename ElfTypes> |
| 1087 | typename ElfTypes::Word ElfFileImpl<ElfTypes>::GetRelaNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1088 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1089 | return section_header.sh_size / section_header.sh_entsize; |
| 1090 | } |
| 1091 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1092 | template <typename ElfTypes> |
| 1093 | typename ElfTypes::Rela& ElfFileImpl<ElfTypes>::GetRela(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1094 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1095 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 1096 | return *(GetRelaSectionStart(section_header) + i); |
| 1097 | } |
| 1098 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1099 | // Base on bionic phdr_table_get_load_size |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1100 | template <typename ElfTypes> |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1101 | bool ElfFileImpl<ElfTypes>::GetLoadedSize(size_t* size, std::string* error_msg) const { |
| 1102 | Elf_Addr min_vaddr = static_cast<Elf_Addr>(-1); |
| 1103 | Elf_Addr max_vaddr = 0u; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1104 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1105 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1106 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1107 | continue; |
| 1108 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1109 | Elf_Addr begin_vaddr = program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1110 | if (begin_vaddr < min_vaddr) { |
| 1111 | min_vaddr = begin_vaddr; |
| 1112 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1113 | Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz; |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1114 | if (UNLIKELY(begin_vaddr > end_vaddr)) { |
| 1115 | std::ostringstream oss; |
| 1116 | oss << "Program header #" << i << " has overflow in p_vaddr+p_memsz: 0x" << std::hex |
| 1117 | << program_header->p_vaddr << "+0x" << program_header->p_memsz << "=0x" << end_vaddr |
| 1118 | << " in ELF file \"" << file_->GetPath() << "\""; |
| 1119 | *error_msg = oss.str(); |
| 1120 | *size = static_cast<size_t>(-1); |
| 1121 | return false; |
| 1122 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1123 | if (end_vaddr > max_vaddr) { |
| 1124 | max_vaddr = end_vaddr; |
| 1125 | } |
| 1126 | } |
| 1127 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 1128 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 1129 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1130 | Elf_Addr loaded_size = max_vaddr - min_vaddr; |
| 1131 | // Check that the loaded_size fits in size_t. |
| 1132 | if (UNLIKELY(loaded_size > std::numeric_limits<size_t>::max())) { |
| 1133 | std::ostringstream oss; |
| 1134 | oss << "Loaded size is 0x" << std::hex << loaded_size << " but maximum size_t is 0x" |
| 1135 | << std::numeric_limits<size_t>::max() << " for ELF file \"" << file_->GetPath() << "\""; |
| 1136 | *error_msg = oss.str(); |
| 1137 | *size = static_cast<size_t>(-1); |
| 1138 | return false; |
| 1139 | } |
| 1140 | *size = loaded_size; |
| 1141 | return true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1142 | } |
| 1143 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1144 | template <typename ElfTypes> |
| 1145 | bool ElfFileImpl<ElfTypes>::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1146 | CHECK(program_header_only_) << file_->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1147 | |
| 1148 | if (executable) { |
Andreas Gampe | 6f61141 | 2015-01-21 22:25:24 -0800 | [diff] [blame] | 1149 | InstructionSet elf_ISA = GetInstructionSetFromELF(GetHeader().e_machine, GetHeader().e_flags); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1150 | if (elf_ISA != kRuntimeISA) { |
| 1151 | std::ostringstream oss; |
| 1152 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 1153 | *error_msg = oss.str(); |
| 1154 | return false; |
| 1155 | } |
| 1156 | } |
| 1157 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1158 | bool reserved = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1159 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1160 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1161 | if (program_header == nullptr) { |
| 1162 | *error_msg = StringPrintf("No program header for entry %d in ELF file %s.", |
| 1163 | i, file_->GetPath().c_str()); |
| 1164 | return false; |
| 1165 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1166 | |
| 1167 | // Record .dynamic header information for later use |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1168 | if (program_header->p_type == PT_DYNAMIC) { |
| 1169 | dynamic_program_header_ = program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1170 | continue; |
| 1171 | } |
| 1172 | |
| 1173 | // Not something to load, move on. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1174 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | // Found something to load. |
| 1179 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1180 | // Before load the actual segments, reserve a contiguous chunk |
| 1181 | // of required size and address for all segments, but with no |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1182 | // permissions. We'll then carve that up with the proper |
| 1183 | // permissions as we load the actual segments. If p_vaddr is |
| 1184 | // non-zero, the segments require the specific address specified, |
| 1185 | // which either was specified in the file because we already set |
| 1186 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1187 | int64_t temp_file_length = file_->GetLength(); |
| 1188 | if (temp_file_length < 0) { |
| 1189 | errno = -temp_file_length; |
| 1190 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 1191 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 1192 | return false; |
| 1193 | } |
| 1194 | size_t file_length = static_cast<size_t>(temp_file_length); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1195 | if (!reserved) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1196 | uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr); |
| 1197 | uint8_t* reserve_base_override = reserve_base; |
| 1198 | // Override the base (e.g. when compiling with --compile-pic) |
| 1199 | if (requested_base_ != nullptr) { |
| 1200 | reserve_base_override = requested_base_; |
| 1201 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1202 | std::string reservation_name("ElfFile reservation for "); |
| 1203 | reservation_name += file_->GetPath(); |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1204 | size_t loaded_size; |
| 1205 | if (!GetLoadedSize(&loaded_size, error_msg)) { |
| 1206 | DCHECK(!error_msg->empty()); |
| 1207 | return false; |
| 1208 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1209 | std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1210 | reserve_base_override, |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1211 | loaded_size, PROT_NONE, false, false, |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1212 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1213 | if (reserve.get() == nullptr) { |
| 1214 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 1215 | reservation_name.c_str(), error_msg->c_str()); |
| 1216 | return false; |
| 1217 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1218 | reserved = true; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1219 | |
| 1220 | // Base address is the difference of actual mapped location and the p_vaddr |
| 1221 | base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin()) |
| 1222 | - reinterpret_cast<uintptr_t>(reserve_base)); |
| 1223 | // By adding the p_vaddr of a section/symbol to base_address_ we will always get the |
| 1224 | // dynamic memory address of where that object is actually mapped |
| 1225 | // |
| 1226 | // TODO: base_address_ needs to be calculated in ::Open, otherwise |
| 1227 | // FindDynamicSymbolAddress returns the wrong values until Load is called. |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1228 | segments_.push_back(reserve.release()); |
| 1229 | } |
| 1230 | // empty segment, nothing to map |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1231 | if (program_header->p_memsz == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1232 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1233 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1234 | uint8_t* p_vaddr = base_address_ + program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1235 | int prot = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1236 | if (executable && ((program_header->p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1237 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1238 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1239 | if ((program_header->p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1240 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1241 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1242 | if ((program_header->p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1243 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1244 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1245 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1246 | if (writable_) { |
| 1247 | prot |= PROT_WRITE; |
| 1248 | flags |= MAP_SHARED; |
| 1249 | } else { |
| 1250 | flags |= MAP_PRIVATE; |
| 1251 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1252 | if (program_header->p_filesz > program_header->p_memsz) { |
| 1253 | *error_msg = StringPrintf("Invalid p_filesz > p_memsz (%" PRIu64 " > %" PRIu64 "): %s", |
| 1254 | static_cast<uint64_t>(program_header->p_filesz), |
| 1255 | static_cast<uint64_t>(program_header->p_memsz), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1256 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 1257 | return false; |
| 1258 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1259 | if (program_header->p_filesz < program_header->p_memsz && |
| 1260 | !IsAligned<kPageSize>(program_header->p_filesz)) { |
| 1261 | *error_msg = StringPrintf("Unsupported unaligned p_filesz < p_memsz (%" PRIu64 |
| 1262 | " < %" PRIu64 "): %s", |
| 1263 | static_cast<uint64_t>(program_header->p_filesz), |
| 1264 | static_cast<uint64_t>(program_header->p_memsz), |
| 1265 | file_->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1266 | return false; |
| 1267 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1268 | if (file_length < (program_header->p_offset + program_header->p_filesz)) { |
| 1269 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
| 1270 | "%d of %" PRIu64 " bytes: '%s'", file_length, i, |
| 1271 | static_cast<uint64_t>(program_header->p_offset + program_header->p_filesz), |
| 1272 | file_->GetPath().c_str()); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1273 | return false; |
| 1274 | } |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1275 | if (program_header->p_filesz != 0u) { |
| 1276 | std::unique_ptr<MemMap> segment( |
| 1277 | MemMap::MapFileAtAddress(p_vaddr, |
| 1278 | program_header->p_filesz, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1279 | prot, |
| 1280 | flags, |
| 1281 | file_->Fd(), |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1282 | program_header->p_offset, |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1283 | /*low4_gb*/false, |
| 1284 | /*reuse*/true, // implies MAP_FIXED |
Vladimir Marko | 5c42c29 | 2015-02-25 12:02:49 +0000 | [diff] [blame] | 1285 | file_->GetPath().c_str(), |
| 1286 | error_msg)); |
| 1287 | if (segment.get() == nullptr) { |
| 1288 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 1289 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1290 | return false; |
| 1291 | } |
| 1292 | if (segment->Begin() != p_vaddr) { |
| 1293 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 1294 | "instead mapped to %p", |
| 1295 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1296 | return false; |
| 1297 | } |
| 1298 | segments_.push_back(segment.release()); |
| 1299 | } |
| 1300 | if (program_header->p_filesz < program_header->p_memsz) { |
| 1301 | std::string name = StringPrintf("Zero-initialized segment %" PRIu64 " of ELF file %s", |
| 1302 | static_cast<uint64_t>(i), file_->GetPath().c_str()); |
| 1303 | std::unique_ptr<MemMap> segment( |
| 1304 | MemMap::MapAnonymous(name.c_str(), |
| 1305 | p_vaddr + program_header->p_filesz, |
| 1306 | program_header->p_memsz - program_header->p_filesz, |
| 1307 | prot, false, true /* reuse */, error_msg)); |
| 1308 | if (segment == nullptr) { |
| 1309 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s: %s", |
| 1310 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1311 | return false; |
| 1312 | } |
| 1313 | if (segment->Begin() != p_vaddr) { |
| 1314 | *error_msg = StringPrintf("Failed to map zero-initialized ELF file segment %d from %s " |
| 1315 | "at expected address %p, instead mapped to %p", |
| 1316 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1317 | return false; |
| 1318 | } |
| 1319 | segments_.push_back(segment.release()); |
| 1320 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1321 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1322 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1323 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1324 | uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1325 | if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) { |
| 1326 | *error_msg = StringPrintf("dynamic section address invalid in ELF file %s", |
| 1327 | file_->GetPath().c_str()); |
| 1328 | return false; |
| 1329 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1330 | dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1331 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1332 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1333 | Elf_Dyn& elf_dyn = GetDynamic(i); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1334 | uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1335 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1336 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1337 | if (!ValidPointer(d_ptr)) { |
| 1338 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1339 | d_ptr, file_->GetPath().c_str()); |
| 1340 | return false; |
| 1341 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1342 | hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1343 | break; |
| 1344 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1345 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1346 | if (!ValidPointer(d_ptr)) { |
| 1347 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1348 | d_ptr, file_->GetPath().c_str()); |
| 1349 | return false; |
| 1350 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1351 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 1352 | break; |
| 1353 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1354 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1355 | if (!ValidPointer(d_ptr)) { |
| 1356 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1357 | d_ptr, file_->GetPath().c_str()); |
| 1358 | return false; |
| 1359 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1360 | dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1361 | break; |
| 1362 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1363 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1364 | if (GetDynamicNum() != i+1) { |
| 1365 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 1366 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 1367 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 1368 | return false; |
| 1369 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1370 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | } |
| 1374 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1375 | // Check for the existence of some sections. |
| 1376 | if (!CheckSectionsExist(error_msg)) { |
| 1377 | return false; |
| 1378 | } |
| 1379 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1380 | // Use GDB JIT support to do stack backtrace, etc. |
| 1381 | if (executable) { |
| 1382 | GdbJITSupport(); |
| 1383 | } |
| 1384 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1385 | return true; |
| 1386 | } |
| 1387 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1388 | template <typename ElfTypes> |
| 1389 | bool ElfFileImpl<ElfTypes>::ValidPointer(const uint8_t* start) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1390 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 1391 | const MemMap* segment = segments_[i]; |
| 1392 | if (segment->Begin() <= start && start < segment->End()) { |
| 1393 | return true; |
| 1394 | } |
| 1395 | } |
| 1396 | return false; |
| 1397 | } |
| 1398 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1399 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1400 | template <typename ElfTypes> |
| 1401 | typename ElfTypes::Shdr* ElfFileImpl<ElfTypes>::FindSectionByName( |
| 1402 | const std::string& name) const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1403 | CHECK(!program_header_only_); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1404 | Elf_Shdr* shstrtab_sec = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1405 | if (shstrtab_sec == nullptr) { |
| 1406 | return nullptr; |
| 1407 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1408 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1409 | Elf_Shdr* shdr = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1410 | if (shdr == nullptr) { |
| 1411 | return nullptr; |
| 1412 | } |
| 1413 | const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1414 | if (sec_name == nullptr) { |
| 1415 | continue; |
| 1416 | } |
| 1417 | if (name == sec_name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1418 | return shdr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1419 | } |
| 1420 | } |
| 1421 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1422 | } |
| 1423 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1424 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1425 | bool ElfFileImpl<ElfTypes>::FixupDebugSections(Elf_Addr base_address_delta) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1426 | if (base_address_delta == 0) { |
| 1427 | return true; |
| 1428 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1429 | return ApplyOatPatchesTo(".debug_frame", base_address_delta) && |
| 1430 | ApplyOatPatchesTo(".debug_info", base_address_delta) && |
| 1431 | ApplyOatPatchesTo(".debug_line", base_address_delta); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1432 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1433 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1434 | template <typename ElfTypes> |
| 1435 | bool ElfFileImpl<ElfTypes>::ApplyOatPatchesTo( |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1436 | const char* target_section_name, Elf_Addr delta) { |
| 1437 | auto target_section = FindSectionByName(target_section_name); |
| 1438 | if (target_section == nullptr) { |
| 1439 | return true; |
| 1440 | } |
| 1441 | std::string patches_name = target_section_name + std::string(".oat_patches"); |
| 1442 | auto patches_section = FindSectionByName(patches_name.c_str()); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1443 | if (patches_section == nullptr) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1444 | LOG(ERROR) << patches_name << " section not found."; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1445 | return false; |
| 1446 | } |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1447 | if (patches_section->sh_type != SHT_OAT_PATCH) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1448 | LOG(ERROR) << "Unexpected type of " << patches_name; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1449 | return false; |
| 1450 | } |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1451 | ApplyOatPatches( |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1452 | Begin() + patches_section->sh_offset, |
| 1453 | Begin() + patches_section->sh_offset + patches_section->sh_size, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1454 | delta, |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1455 | Begin() + target_section->sh_offset, |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1456 | Begin() + target_section->sh_offset + target_section->sh_size); |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1457 | return true; |
| 1458 | } |
| 1459 | |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1460 | // Apply LEB128 encoded patches to given section. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1461 | template <typename ElfTypes> |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1462 | void ElfFileImpl<ElfTypes>::ApplyOatPatches( |
| 1463 | const uint8_t* patches, const uint8_t* patches_end, Elf_Addr delta, |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1464 | uint8_t* to_patch, const uint8_t* to_patch_end) { |
David Srbecky | f898087 | 2015-05-22 17:04:47 +0100 | [diff] [blame] | 1465 | typedef __attribute__((__aligned__(1))) Elf_Addr UnalignedAddress; |
| 1466 | while (patches < patches_end) { |
| 1467 | to_patch += DecodeUnsignedLeb128(&patches); |
| 1468 | DCHECK_LE(patches, patches_end) << "Unexpected end of patch list."; |
| 1469 | DCHECK_LT(to_patch, to_patch_end) << "Patch past the end of section."; |
| 1470 | *reinterpret_cast<UnalignedAddress*>(to_patch) += delta; |
David Srbecky | 2f6cdb0 | 2015-04-11 00:17:53 +0100 | [diff] [blame] | 1471 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1472 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1473 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1474 | template <typename ElfTypes> |
| 1475 | void ElfFileImpl<ElfTypes>::GdbJITSupport() { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1476 | // We only get here if we only are mapping the program header. |
| 1477 | DCHECK(program_header_only_); |
| 1478 | |
| 1479 | // Well, we need the whole file to do this. |
| 1480 | std::string error_msg; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1481 | // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary |
| 1482 | // sections are there. |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1483 | std::unique_ptr<ElfFileImpl<ElfTypes>> all_ptr( |
| 1484 | Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE, MAP_PRIVATE, &error_msg)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1485 | if (all_ptr.get() == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1486 | return; |
| 1487 | } |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1488 | ElfFileImpl<ElfTypes>& all = *all_ptr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1489 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1490 | // We need the eh_frame for gdb but debug info might be present without it. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1491 | const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame"); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1492 | if (eh_frame == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1493 | return; |
| 1494 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1495 | |
| 1496 | // Do we have interesting sections? |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1497 | // We need to add in a strtab and symtab to the image. |
| 1498 | // all is MAP_PRIVATE so it can be written to freely. |
| 1499 | // We also already have strtab and symtab so we are fine there. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1500 | Elf_Ehdr& elf_hdr = all.GetHeader(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1501 | elf_hdr.e_entry = 0; |
| 1502 | elf_hdr.e_phoff = 0; |
| 1503 | elf_hdr.e_phnum = 0; |
| 1504 | elf_hdr.e_phentsize = 0; |
| 1505 | elf_hdr.e_type = ET_EXEC; |
| 1506 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1507 | // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat) |
| 1508 | // and the actual address stuff starts at in regular files this is good. |
| 1509 | if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1510 | LOG(ERROR) << "Failed to load GDB data"; |
| 1511 | return; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1512 | } |
| 1513 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1514 | jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size()); |
| 1515 | gdb_file_mapping_.reset(all_ptr.release()); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1516 | } |
| 1517 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1518 | template <typename ElfTypes> |
| 1519 | bool ElfFileImpl<ElfTypes>::Strip(std::string* error_msg) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1520 | // ELF files produced by MCLinker look roughly like this |
| 1521 | // |
| 1522 | // +------------+ |
| 1523 | // | Elf_Ehdr | contains number of Elf_Shdr and offset to first |
| 1524 | // +------------+ |
| 1525 | // | Elf_Phdr | program headers |
| 1526 | // | Elf_Phdr | |
| 1527 | // | ... | |
| 1528 | // | Elf_Phdr | |
| 1529 | // +------------+ |
| 1530 | // | section | mixture of needed and unneeded sections |
| 1531 | // +------------+ |
| 1532 | // | section | |
| 1533 | // +------------+ |
| 1534 | // | ... | |
| 1535 | // +------------+ |
| 1536 | // | section | |
| 1537 | // +------------+ |
| 1538 | // | Elf_Shdr | section headers |
| 1539 | // | Elf_Shdr | |
| 1540 | // | ... | contains offset to section start |
| 1541 | // | Elf_Shdr | |
| 1542 | // +------------+ |
| 1543 | // |
| 1544 | // To strip: |
| 1545 | // - leave the Elf_Ehdr and Elf_Phdr values in place. |
| 1546 | // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep |
| 1547 | // - move the sections are keeping up to fill in gaps of sections we want to strip |
| 1548 | // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr |
| 1549 | // - truncate rest of file |
| 1550 | // |
| 1551 | |
| 1552 | std::vector<Elf_Shdr> section_headers; |
| 1553 | std::vector<Elf_Word> section_headers_original_indexes; |
| 1554 | section_headers.reserve(GetSectionHeaderNum()); |
| 1555 | |
| 1556 | |
| 1557 | Elf_Shdr* string_section = GetSectionNameStringSection(); |
| 1558 | CHECK(string_section != nullptr); |
| 1559 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1560 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1561 | CHECK(sh != nullptr); |
| 1562 | const char* name = GetString(*string_section, sh->sh_name); |
| 1563 | if (name == nullptr) { |
| 1564 | CHECK_EQ(0U, i); |
| 1565 | section_headers.push_back(*sh); |
| 1566 | section_headers_original_indexes.push_back(0); |
| 1567 | continue; |
| 1568 | } |
| 1569 | if (StartsWith(name, ".debug") |
| 1570 | || (strcmp(name, ".strtab") == 0) |
| 1571 | || (strcmp(name, ".symtab") == 0)) { |
| 1572 | continue; |
| 1573 | } |
| 1574 | section_headers.push_back(*sh); |
| 1575 | section_headers_original_indexes.push_back(i); |
| 1576 | } |
| 1577 | CHECK_NE(0U, section_headers.size()); |
| 1578 | CHECK_EQ(section_headers.size(), section_headers_original_indexes.size()); |
| 1579 | |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 1580 | // section 0 is the null section, sections start at offset of first section |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1581 | CHECK(GetSectionHeader(1) != nullptr); |
| 1582 | Elf_Off offset = GetSectionHeader(1)->sh_offset; |
| 1583 | for (size_t i = 1; i < section_headers.size(); i++) { |
| 1584 | Elf_Shdr& new_sh = section_headers[i]; |
| 1585 | Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]); |
| 1586 | CHECK(old_sh != nullptr); |
| 1587 | CHECK_EQ(new_sh.sh_name, old_sh->sh_name); |
| 1588 | if (old_sh->sh_addralign > 1) { |
| 1589 | offset = RoundUp(offset, old_sh->sh_addralign); |
| 1590 | } |
| 1591 | if (old_sh->sh_offset == offset) { |
| 1592 | // already in place |
| 1593 | offset += old_sh->sh_size; |
| 1594 | continue; |
| 1595 | } |
| 1596 | // shift section earlier |
| 1597 | memmove(Begin() + offset, |
| 1598 | Begin() + old_sh->sh_offset, |
| 1599 | old_sh->sh_size); |
| 1600 | new_sh.sh_offset = offset; |
| 1601 | offset += old_sh->sh_size; |
| 1602 | } |
| 1603 | |
| 1604 | Elf_Off shoff = offset; |
| 1605 | size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr); |
| 1606 | memcpy(Begin() + offset, §ion_headers[0], section_headers_size_in_bytes); |
| 1607 | offset += section_headers_size_in_bytes; |
| 1608 | |
| 1609 | GetHeader().e_shnum = section_headers.size(); |
| 1610 | GetHeader().e_shoff = shoff; |
| 1611 | int result = ftruncate(file_->Fd(), offset); |
| 1612 | if (result != 0) { |
| 1613 | *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s", |
| 1614 | file_->GetPath().c_str(), strerror(errno)); |
| 1615 | return false; |
| 1616 | } |
| 1617 | return true; |
| 1618 | } |
| 1619 | |
| 1620 | static const bool DEBUG_FIXUP = false; |
| 1621 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1622 | template <typename ElfTypes> |
| 1623 | bool ElfFileImpl<ElfTypes>::Fixup(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1624 | if (!FixupDynamic(base_address)) { |
| 1625 | LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath(); |
| 1626 | return false; |
| 1627 | } |
| 1628 | if (!FixupSectionHeaders(base_address)) { |
| 1629 | LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath(); |
| 1630 | return false; |
| 1631 | } |
| 1632 | if (!FixupProgramHeaders(base_address)) { |
| 1633 | LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath(); |
| 1634 | return false; |
| 1635 | } |
| 1636 | if (!FixupSymbols(base_address, true)) { |
| 1637 | LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath(); |
| 1638 | return false; |
| 1639 | } |
| 1640 | if (!FixupSymbols(base_address, false)) { |
| 1641 | LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath(); |
| 1642 | return false; |
| 1643 | } |
| 1644 | if (!FixupRelocations(base_address)) { |
| 1645 | LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath(); |
| 1646 | return false; |
| 1647 | } |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1648 | static_assert(sizeof(Elf_Off) >= sizeof(base_address), "Potentially losing precision."); |
| 1649 | if (!FixupDebugSections(static_cast<Elf_Off>(base_address))) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1650 | LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath(); |
| 1651 | return false; |
| 1652 | } |
| 1653 | return true; |
| 1654 | } |
| 1655 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1656 | template <typename ElfTypes> |
| 1657 | bool ElfFileImpl<ElfTypes>::FixupDynamic(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1658 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1659 | Elf_Dyn& elf_dyn = GetDynamic(i); |
| 1660 | Elf_Word d_tag = elf_dyn.d_tag; |
| 1661 | if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) { |
| 1662 | Elf_Addr d_ptr = elf_dyn.d_un.d_ptr; |
| 1663 | if (DEBUG_FIXUP) { |
| 1664 | LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1665 | GetFile().GetPath().c_str(), i, |
| 1666 | static_cast<uint64_t>(d_ptr), |
| 1667 | static_cast<uint64_t>(d_ptr + base_address)); |
| 1668 | } |
| 1669 | d_ptr += base_address; |
| 1670 | elf_dyn.d_un.d_ptr = d_ptr; |
| 1671 | } |
| 1672 | } |
| 1673 | return true; |
| 1674 | } |
| 1675 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1676 | template <typename ElfTypes> |
| 1677 | bool ElfFileImpl<ElfTypes>::FixupSectionHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1678 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1679 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1680 | CHECK(sh != nullptr); |
| 1681 | // 0 implies that the section will not exist in the memory of the process |
| 1682 | if (sh->sh_addr == 0) { |
| 1683 | continue; |
| 1684 | } |
| 1685 | if (DEBUG_FIXUP) { |
| 1686 | LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1687 | GetFile().GetPath().c_str(), i, |
| 1688 | static_cast<uint64_t>(sh->sh_addr), |
| 1689 | static_cast<uint64_t>(sh->sh_addr + base_address)); |
| 1690 | } |
| 1691 | sh->sh_addr += base_address; |
| 1692 | } |
| 1693 | return true; |
| 1694 | } |
| 1695 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1696 | template <typename ElfTypes> |
| 1697 | bool ElfFileImpl<ElfTypes>::FixupProgramHeaders(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1698 | // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now. |
| 1699 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1700 | Elf_Phdr* ph = GetProgramHeader(i); |
| 1701 | CHECK(ph != nullptr); |
| 1702 | CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i; |
| 1703 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 1704 | << GetFile().GetPath() << " i=" << i; |
| 1705 | if (DEBUG_FIXUP) { |
| 1706 | LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1707 | GetFile().GetPath().c_str(), i, |
| 1708 | static_cast<uint64_t>(ph->p_vaddr), |
| 1709 | static_cast<uint64_t>(ph->p_vaddr + base_address)); |
| 1710 | } |
| 1711 | ph->p_vaddr += base_address; |
| 1712 | ph->p_paddr += base_address; |
| 1713 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 1714 | << GetFile().GetPath() << " i=" << i; |
| 1715 | } |
| 1716 | return true; |
| 1717 | } |
| 1718 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1719 | template <typename ElfTypes> |
| 1720 | bool ElfFileImpl<ElfTypes>::FixupSymbols(Elf_Addr base_address, bool dynamic) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1721 | Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB; |
| 1722 | // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile |
| 1723 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
| 1724 | if (symbol_section == nullptr) { |
| 1725 | // file is missing optional .symtab |
| 1726 | CHECK(!dynamic) << GetFile().GetPath(); |
| 1727 | return true; |
| 1728 | } |
| 1729 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
| 1730 | Elf_Sym* symbol = GetSymbol(section_type, i); |
| 1731 | CHECK(symbol != nullptr); |
| 1732 | if (symbol->st_value != 0) { |
| 1733 | if (DEBUG_FIXUP) { |
| 1734 | LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 1735 | GetFile().GetPath().c_str(), i, |
| 1736 | static_cast<uint64_t>(symbol->st_value), |
| 1737 | static_cast<uint64_t>(symbol->st_value + base_address)); |
| 1738 | } |
| 1739 | symbol->st_value += base_address; |
| 1740 | } |
| 1741 | } |
| 1742 | return true; |
| 1743 | } |
| 1744 | |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1745 | template <typename ElfTypes> |
| 1746 | bool ElfFileImpl<ElfTypes>::FixupRelocations(Elf_Addr base_address) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1747 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 1748 | Elf_Shdr* sh = GetSectionHeader(i); |
| 1749 | CHECK(sh != nullptr); |
| 1750 | if (sh->sh_type == SHT_REL) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1751 | for (uint32_t j = 0; j < GetRelNum(*sh); j++) { |
| 1752 | Elf_Rel& rel = GetRel(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1753 | if (DEBUG_FIXUP) { |
| 1754 | LOG(INFO) << StringPrintf("In %s moving Elf_Rel[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1755 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1756 | static_cast<uint64_t>(rel.r_offset), |
| 1757 | static_cast<uint64_t>(rel.r_offset + base_address)); |
| 1758 | } |
| 1759 | rel.r_offset += base_address; |
| 1760 | } |
| 1761 | } else if (sh->sh_type == SHT_RELA) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1762 | for (uint32_t j = 0; j < GetRelaNum(*sh); j++) { |
| 1763 | Elf_Rela& rela = GetRela(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1764 | if (DEBUG_FIXUP) { |
| 1765 | LOG(INFO) << StringPrintf("In %s moving Elf_Rela[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 1766 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1767 | static_cast<uint64_t>(rela.r_offset), |
| 1768 | static_cast<uint64_t>(rela.r_offset + base_address)); |
| 1769 | } |
| 1770 | rela.r_offset += base_address; |
| 1771 | } |
| 1772 | } |
| 1773 | } |
| 1774 | return true; |
| 1775 | } |
| 1776 | |
| 1777 | // Explicit instantiations |
David Srbecky | 533c207 | 2015-04-22 12:20:22 +0100 | [diff] [blame] | 1778 | template class ElfFileImpl<ElfTypes32>; |
| 1779 | template class ElfFileImpl<ElfTypes64>; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1780 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1781 | ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1782 | } |
| 1783 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1784 | ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | ElfFile::~ElfFile() { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1788 | // Should never have 32 and 64-bit impls. |
| 1789 | CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1792 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg, |
| 1793 | uint8_t* requested_base) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1794 | if (file->GetLength() < EI_NIDENT) { |
| 1795 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1796 | file->GetPath().c_str()); |
| 1797 | return nullptr; |
| 1798 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1799 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, |
| 1800 | PROT_READ, |
| 1801 | MAP_PRIVATE, |
| 1802 | file->Fd(), |
| 1803 | 0, |
| 1804 | /*low4_gb*/false, |
| 1805 | file->GetPath().c_str(), |
| 1806 | error_msg)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1807 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 1808 | return nullptr; |
| 1809 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1810 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1811 | if (header[EI_CLASS] == ELFCLASS64) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1812 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only, |
| 1813 | error_msg, requested_base); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1814 | if (elf_file_impl == nullptr) |
| 1815 | return nullptr; |
| 1816 | return new ElfFile(elf_file_impl); |
| 1817 | } else if (header[EI_CLASS] == ELFCLASS32) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1818 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only, |
| 1819 | error_msg, requested_base); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1820 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1821 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1822 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1823 | return new ElfFile(elf_file_impl); |
| 1824 | } else { |
| 1825 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1826 | ELFCLASS32, ELFCLASS64, |
| 1827 | file->GetPath().c_str(), |
| 1828 | header[EI_CLASS]); |
| 1829 | return nullptr; |
| 1830 | } |
| 1831 | } |
| 1832 | |
| 1833 | ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) { |
| 1834 | if (file->GetLength() < EI_NIDENT) { |
| 1835 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 1836 | file->GetPath().c_str()); |
| 1837 | return nullptr; |
| 1838 | } |
Mathieu Chartier | 42bddce | 2015-11-09 15:16:56 -0800 | [diff] [blame] | 1839 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, |
| 1840 | PROT_READ, |
| 1841 | MAP_PRIVATE, |
| 1842 | file->Fd(), |
| 1843 | 0, |
| 1844 | /*low4_gb*/false, |
| 1845 | file->GetPath().c_str(), |
| 1846 | error_msg)); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1847 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 1848 | return nullptr; |
| 1849 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1850 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1851 | if (header[EI_CLASS] == ELFCLASS64) { |
| 1852 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, mmap_prot, mmap_flags, error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1853 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1854 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1855 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1856 | return new ElfFile(elf_file_impl); |
| 1857 | } else if (header[EI_CLASS] == ELFCLASS32) { |
| 1858 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, mmap_prot, mmap_flags, error_msg); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1859 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1860 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1861 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1862 | return new ElfFile(elf_file_impl); |
| 1863 | } else { |
| 1864 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 1865 | ELFCLASS32, ELFCLASS64, |
| 1866 | file->GetPath().c_str(), |
| 1867 | header[EI_CLASS]); |
| 1868 | return nullptr; |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | #define DELEGATE_TO_IMPL(func, ...) \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1873 | if (elf64_.get() != nullptr) { \ |
| 1874 | return elf64_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1875 | } else { \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1876 | DCHECK(elf32_.get() != nullptr); \ |
| 1877 | return elf32_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1878 | } |
| 1879 | |
| 1880 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
| 1881 | DELEGATE_TO_IMPL(Load, executable, error_msg); |
| 1882 | } |
| 1883 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1884 | const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1885 | DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name); |
| 1886 | } |
| 1887 | |
| 1888 | size_t ElfFile::Size() const { |
| 1889 | DELEGATE_TO_IMPL(Size); |
| 1890 | } |
| 1891 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1892 | uint8_t* ElfFile::Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1893 | DELEGATE_TO_IMPL(Begin); |
| 1894 | } |
| 1895 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1896 | uint8_t* ElfFile::End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1897 | DELEGATE_TO_IMPL(End); |
| 1898 | } |
| 1899 | |
| 1900 | const File& ElfFile::GetFile() const { |
| 1901 | DELEGATE_TO_IMPL(GetFile); |
| 1902 | } |
| 1903 | |
Alex Light | 0eb76d2 | 2015-08-11 18:03:47 -0700 | [diff] [blame] | 1904 | bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, |
| 1905 | uint64_t* size) const { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1906 | if (elf32_.get() == nullptr) { |
| 1907 | CHECK(elf64_.get() != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1908 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1909 | Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name); |
| 1910 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1911 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1912 | } |
| 1913 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1914 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1915 | } |
| 1916 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1917 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1918 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1919 | return true; |
| 1920 | } else { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1921 | Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name); |
| 1922 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1923 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1924 | } |
| 1925 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1926 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1927 | } |
| 1928 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1929 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1930 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1931 | return true; |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | uint64_t ElfFile::FindSymbolAddress(unsigned section_type, |
| 1936 | const std::string& symbol_name, |
| 1937 | bool build_map) { |
| 1938 | DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map); |
| 1939 | } |
| 1940 | |
Vladimir Marko | 3fc9903 | 2015-05-13 19:06:30 +0100 | [diff] [blame] | 1941 | bool ElfFile::GetLoadedSize(size_t* size, std::string* error_msg) const { |
| 1942 | DELEGATE_TO_IMPL(GetLoadedSize, size, error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1943 | } |
| 1944 | |
| 1945 | bool ElfFile::Strip(File* file, std::string* error_msg) { |
| 1946 | std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg)); |
| 1947 | if (elf_file.get() == nullptr) { |
| 1948 | return false; |
| 1949 | } |
| 1950 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1951 | if (elf_file->elf64_.get() != nullptr) |
| 1952 | return elf_file->elf64_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1953 | else |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1954 | return elf_file->elf32_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Andreas Gampe | 3c54b00 | 2015-04-07 16:09:30 -0700 | [diff] [blame] | 1957 | bool ElfFile::Fixup(uint64_t base_address) { |
| 1958 | if (elf64_.get() != nullptr) { |
| 1959 | return elf64_->Fixup(static_cast<Elf64_Addr>(base_address)); |
| 1960 | } else { |
| 1961 | DCHECK(elf32_.get() != nullptr); |
| 1962 | CHECK(IsUint<32>(base_address)) << std::hex << base_address; |
| 1963 | return elf32_->Fixup(static_cast<Elf32_Addr>(base_address)); |
| 1964 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1965 | DELEGATE_TO_IMPL(Fixup, base_address); |
| 1966 | } |
| 1967 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1968 | } // namespace art |