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" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 28 | #include "dwarf.h" |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 29 | #include "elf_file_impl.h" |
| 30 | #include "elf_utils.h" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 31 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 32 | #include "utils.h" |
| 33 | |
| 34 | namespace art { |
| 35 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 36 | // ------------------------------------------------------------------- |
| 37 | // Binary GDB JIT Interface as described in |
| 38 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 39 | extern "C" { |
| 40 | typedef enum { |
| 41 | JIT_NOACTION = 0, |
| 42 | JIT_REGISTER_FN, |
| 43 | JIT_UNREGISTER_FN |
| 44 | } JITAction; |
| 45 | |
| 46 | struct JITCodeEntry { |
| 47 | JITCodeEntry* next_; |
| 48 | JITCodeEntry* prev_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 49 | const uint8_t *symfile_addr_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 50 | uint64_t symfile_size_; |
| 51 | }; |
| 52 | |
| 53 | struct JITDescriptor { |
| 54 | uint32_t version_; |
| 55 | uint32_t action_flag_; |
| 56 | JITCodeEntry* relevant_entry_; |
| 57 | JITCodeEntry* first_entry_; |
| 58 | }; |
| 59 | |
| 60 | // GDB will place breakpoint into this function. |
| 61 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 62 | // and inline assembler statement inside. |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 63 | void __attribute__((noinline)) __jit_debug_register_code(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 64 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 65 | __asm__(""); |
| 66 | } |
| 67 | |
| 68 | // GDB will inspect contents of this descriptor. |
| 69 | // Static initialization is necessary to prevent GDB from seeing |
| 70 | // uninitialized descriptor. |
| 71 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 72 | } |
| 73 | |
| 74 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 75 | static JITCodeEntry* CreateCodeEntry(const uint8_t *symfile_addr, |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 76 | uintptr_t symfile_size) { |
| 77 | JITCodeEntry* entry = new JITCodeEntry; |
| 78 | entry->symfile_addr_ = symfile_addr; |
| 79 | entry->symfile_size_ = symfile_size; |
| 80 | entry->prev_ = nullptr; |
| 81 | |
| 82 | // TODO: Do we need a lock here? |
| 83 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 84 | if (entry->next_ != nullptr) { |
| 85 | entry->next_->prev_ = entry; |
| 86 | } |
| 87 | __jit_debug_descriptor.first_entry_ = entry; |
| 88 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 89 | |
| 90 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
| 91 | __jit_debug_register_code(); |
| 92 | return entry; |
| 93 | } |
| 94 | |
| 95 | |
| 96 | static void UnregisterCodeEntry(JITCodeEntry* entry) { |
| 97 | // TODO: Do we need a lock here? |
| 98 | if (entry->prev_ != nullptr) { |
| 99 | entry->prev_->next_ = entry->next_; |
| 100 | } else { |
| 101 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 102 | } |
| 103 | |
| 104 | if (entry->next_ != nullptr) { |
| 105 | entry->next_->prev_ = entry->prev_; |
| 106 | } |
| 107 | |
| 108 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 109 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 110 | __jit_debug_register_code(); |
| 111 | delete entry; |
| 112 | } |
| 113 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 114 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 115 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 116 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 117 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 118 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 119 | ::ElfFileImpl(File* file, bool writable, bool program_header_only, uint8_t* requested_base) |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 120 | : file_(file), |
| 121 | writable_(writable), |
| 122 | program_header_only_(program_header_only), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 123 | header_(nullptr), |
| 124 | base_address_(nullptr), |
| 125 | program_headers_start_(nullptr), |
| 126 | section_headers_start_(nullptr), |
| 127 | dynamic_program_header_(nullptr), |
| 128 | dynamic_section_start_(nullptr), |
| 129 | symtab_section_start_(nullptr), |
| 130 | dynsym_section_start_(nullptr), |
| 131 | strtab_section_start_(nullptr), |
| 132 | dynstr_section_start_(nullptr), |
| 133 | hash_section_start_(nullptr), |
| 134 | symtab_symbol_table_(nullptr), |
| 135 | dynsym_symbol_table_(nullptr), |
| 136 | jit_elf_image_(nullptr), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 137 | jit_gdb_entry_(nullptr), |
| 138 | requested_base_(requested_base) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 139 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 140 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 141 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 142 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 143 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 144 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 145 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 146 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>* |
| 147 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 148 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 149 | ::Open(File* file, bool writable, bool program_header_only, |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 150 | std::string* error_msg, uint8_t* requested_base) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 151 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 152 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>> |
| 153 | elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 154 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 155 | (file, writable, program_header_only, requested_base)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 156 | int prot; |
| 157 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 158 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 159 | prot = PROT_READ | PROT_WRITE; |
| 160 | flags = MAP_SHARED; |
| 161 | } else { |
| 162 | prot = PROT_READ; |
| 163 | flags = MAP_PRIVATE; |
| 164 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 165 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 166 | return nullptr; |
| 167 | } |
| 168 | return elf_file.release(); |
| 169 | } |
| 170 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 171 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 172 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 173 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 174 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 175 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>* |
| 176 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 177 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 178 | ::Open(File* file, int prot, int flags, std::string* error_msg) { |
| 179 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 180 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>> |
| 181 | elf_file(new ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 182 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 183 | (file, (prot & PROT_WRITE) == PROT_WRITE, /*program_header_only*/false, |
| 184 | /*requested_base*/nullptr)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 185 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 186 | return nullptr; |
| 187 | } |
| 188 | return elf_file.release(); |
| 189 | } |
| 190 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 191 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 192 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 193 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 194 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 195 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 196 | ::Setup(int prot, int flags, std::string* error_msg) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 197 | int64_t temp_file_length = file_->GetLength(); |
| 198 | if (temp_file_length < 0) { |
| 199 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 201 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 204 | size_t file_length = static_cast<size_t>(temp_file_length); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 205 | if (file_length < sizeof(Elf_Ehdr)) { |
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 header of " |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 207 | "%zd bytes: '%s'", file_length, sizeof(Elf_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 208 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 209 | return false; |
| 210 | } |
| 211 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 212 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 213 | // first just map ELF header to get program header size information |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 214 | size_t elf_header_size = sizeof(Elf_Ehdr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 215 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 216 | file_->GetPath().c_str(), error_msg), |
| 217 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 218 | return false; |
| 219 | } |
| 220 | // then remap to cover program header |
| 221 | 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] | 222 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 223 | *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] | 224 | "header of %zd bytes: '%s'", file_length, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 225 | sizeof(Elf_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 228 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 229 | file_->GetPath().c_str(), error_msg), |
| 230 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 231 | *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] | 232 | return false; |
| 233 | } |
| 234 | } else { |
| 235 | // otherwise map entire file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 236 | if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 237 | file_->GetPath().c_str(), error_msg), |
| 238 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 239 | *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] | 240 | return false; |
| 241 | } |
| 242 | } |
| 243 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 244 | if (program_header_only_) { |
| 245 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 246 | } else { |
| 247 | if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) { |
| 248 | return false; |
| 249 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 250 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 251 | // Setup section headers. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 252 | if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) { |
| 253 | return false; |
| 254 | } |
| 255 | |
| 256 | // Find shstrtab. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 257 | Elf_Shdr* shstrtab_section_header = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 258 | if (shstrtab_section_header == nullptr) { |
| 259 | *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'", |
| 260 | file_->GetPath().c_str()); |
| 261 | return false; |
| 262 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 263 | |
| 264 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 265 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 266 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 267 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 268 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 269 | return false; |
| 270 | } |
| 271 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 272 | if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 273 | reinterpret_cast<uint8_t**>(&dynamic_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 274 | return false; |
| 275 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 276 | |
| 277 | // Find other sections from section headers |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 278 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 279 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 280 | if (section_header == nullptr) { |
| 281 | *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'", |
| 282 | i, file_->GetPath().c_str()); |
| 283 | return false; |
| 284 | } |
| 285 | switch (section_header->sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 286 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 287 | if (!CheckAndSet(section_header->sh_offset, "symtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 288 | reinterpret_cast<uint8_t**>(&symtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 289 | return false; |
| 290 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 291 | break; |
| 292 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 293 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 294 | if (!CheckAndSet(section_header->sh_offset, "dynsym", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 295 | reinterpret_cast<uint8_t**>(&dynsym_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 296 | return false; |
| 297 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 298 | break; |
| 299 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 300 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 301 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 302 | if ((section_header->sh_flags & SHF_ALLOC) != 0) { |
| 303 | // Check that this is named ".dynstr" and ignore otherwise. |
| 304 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 305 | if (strncmp(".dynstr", header_name, 8) == 0) { |
| 306 | if (!CheckAndSet(section_header->sh_offset, "dynstr", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 307 | reinterpret_cast<uint8_t**>(&dynstr_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 308 | return false; |
| 309 | } |
| 310 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 311 | } else { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 312 | // Check that this is named ".strtab" and ignore otherwise. |
| 313 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 314 | if (strncmp(".strtab", header_name, 8) == 0) { |
| 315 | if (!CheckAndSet(section_header->sh_offset, "strtab", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 316 | reinterpret_cast<uint8_t**>(&strtab_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 320 | } |
| 321 | break; |
| 322 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 323 | case SHT_DYNAMIC: { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 324 | if (reinterpret_cast<uint8_t*>(dynamic_section_start_) != |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 325 | Begin() + section_header->sh_offset) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 326 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 327 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 328 | << reinterpret_cast<void*>(dynamic_section_start_) |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 329 | << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | break; |
| 333 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 334 | case SHT_HASH: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 335 | if (!CheckAndSet(section_header->sh_offset, "hash section", |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 336 | reinterpret_cast<uint8_t**>(&hash_section_start_), error_msg)) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 337 | return false; |
| 338 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 339 | break; |
| 340 | } |
| 341 | } |
| 342 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 343 | |
| 344 | // Check for the existence of some sections. |
| 345 | if (!CheckSectionsExist(error_msg)) { |
| 346 | return false; |
| 347 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 348 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 349 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 350 | return true; |
| 351 | } |
| 352 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 353 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 354 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 355 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 356 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 357 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 358 | ::~ElfFileImpl() { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 359 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 360 | delete symtab_symbol_table_; |
| 361 | delete dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 362 | delete jit_elf_image_; |
| 363 | if (jit_gdb_entry_) { |
| 364 | UnregisterCodeEntry(jit_gdb_entry_); |
| 365 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 368 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 369 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 370 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 371 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 372 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 373 | ::CheckAndSet(Elf32_Off offset, const char* label, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 374 | uint8_t** target, std::string* error_msg) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 375 | if (Begin() + offset >= End()) { |
| 376 | *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label, |
| 377 | file_->GetPath().c_str()); |
| 378 | return false; |
| 379 | } |
| 380 | *target = Begin() + offset; |
| 381 | return true; |
| 382 | } |
| 383 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 384 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 385 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 386 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 387 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 388 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 389 | ::CheckSectionsLinked(const uint8_t* source, const uint8_t* target) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 390 | // Only works in whole-program mode, as we need to iterate over the sections. |
| 391 | // Note that we normally can't search by type, as duplicates are allowed for most section types. |
| 392 | if (program_header_only_) { |
| 393 | return true; |
| 394 | } |
| 395 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 396 | Elf_Shdr* source_section = nullptr; |
| 397 | Elf_Word target_index = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 398 | bool target_found = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 399 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 400 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 401 | |
| 402 | if (Begin() + section_header->sh_offset == source) { |
| 403 | // Found the source. |
| 404 | source_section = section_header; |
| 405 | if (target_index) { |
| 406 | break; |
| 407 | } |
| 408 | } else if (Begin() + section_header->sh_offset == target) { |
| 409 | target_index = i; |
| 410 | target_found = true; |
| 411 | if (source_section != nullptr) { |
| 412 | break; |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | return target_found && source_section != nullptr && source_section->sh_link == target_index; |
| 418 | } |
| 419 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 420 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 421 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 422 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 423 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 424 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 425 | ::CheckSectionsExist(std::string* error_msg) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 426 | if (!program_header_only_) { |
| 427 | // If in full mode, need section headers. |
| 428 | if (section_headers_start_ == nullptr) { |
| 429 | *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str()); |
| 430 | return false; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | // This is redundant, but defensive. |
| 435 | if (dynamic_program_header_ == nullptr) { |
| 436 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 437 | file_->GetPath().c_str()); |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | // Need a dynamic section. This is redundant, but defensive. |
| 442 | if (dynamic_section_start_ == nullptr) { |
| 443 | *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'", |
| 444 | file_->GetPath().c_str()); |
| 445 | return false; |
| 446 | } |
| 447 | |
| 448 | // Symtab validation. These is not really a hard failure, as we are currently not using the |
| 449 | // symtab internally, but it's nice to be defensive. |
| 450 | if (symtab_section_start_ != nullptr) { |
| 451 | // When there's a symtab, there should be a strtab. |
| 452 | if (strtab_section_start_ == nullptr) { |
| 453 | *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str()); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | // The symtab should link to the strtab. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 458 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(symtab_section_start_), |
| 459 | reinterpret_cast<const uint8_t*>(strtab_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 460 | *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'", |
| 461 | file_->GetPath().c_str()); |
| 462 | return false; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | // We always need a dynstr & dynsym. |
| 467 | if (dynstr_section_start_ == nullptr) { |
| 468 | *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str()); |
| 469 | return false; |
| 470 | } |
| 471 | if (dynsym_section_start_ == nullptr) { |
| 472 | *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str()); |
| 473 | return false; |
| 474 | } |
| 475 | |
| 476 | // Need a hash section for dynamic symbol lookup. |
| 477 | if (hash_section_start_ == nullptr) { |
| 478 | *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'", |
| 479 | file_->GetPath().c_str()); |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | // And the hash section should be linking to the dynsym. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 484 | if (!CheckSectionsLinked(reinterpret_cast<const uint8_t*>(hash_section_start_), |
| 485 | reinterpret_cast<const uint8_t*>(dynsym_section_start_))) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 486 | *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'", |
| 487 | file_->GetPath().c_str()); |
| 488 | return false; |
| 489 | } |
| 490 | |
Andreas Gampe | a696c0a | 2014-12-10 20:51:45 -0800 | [diff] [blame^] | 491 | // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for |
| 492 | // us). This is usually the last in an oat file, and a good indicator of whether writing was |
| 493 | // successful (or the process crashed and left garbage). |
| 494 | if (program_header_only_) { |
| 495 | // It might not be mapped, but we can compare against the file size. |
| 496 | int64_t offset = static_cast<int64_t>(GetHeader().e_shoff + |
| 497 | (GetHeader().e_shstrndx * GetHeader().e_shentsize)); |
| 498 | if (offset >= file_->GetLength()) { |
| 499 | *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'", |
| 500 | file_->GetPath().c_str()); |
| 501 | return false; |
| 502 | } |
| 503 | } |
| 504 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 505 | return true; |
| 506 | } |
| 507 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 508 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 509 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 510 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 511 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 512 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 513 | ::SetMap(MemMap* map, std::string* error_msg) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 514 | if (map == nullptr) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 515 | // MemMap::Open should have already set an error. |
| 516 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 517 | return false; |
| 518 | } |
| 519 | map_.reset(map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 520 | CHECK(map_.get() != nullptr) << file_->GetPath(); |
| 521 | CHECK(map_->Begin() != nullptr) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 522 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 523 | header_ = reinterpret_cast<Elf_Ehdr*>(map_->Begin()); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 524 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 525 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 526 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 527 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 528 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 529 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 530 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 531 | header_->e_ident[EI_MAG0], |
| 532 | header_->e_ident[EI_MAG1], |
| 533 | header_->e_ident[EI_MAG2], |
| 534 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 535 | return false; |
| 536 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 537 | uint8_t elf_class = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) ? ELFCLASS64 : ELFCLASS32; |
| 538 | if (elf_class != header_->e_ident[EI_CLASS]) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 539 | *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] | 540 | elf_class, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 541 | file_->GetPath().c_str(), |
| 542 | header_->e_ident[EI_CLASS]); |
| 543 | return false; |
| 544 | } |
| 545 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 546 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 547 | ELFDATA2LSB, |
| 548 | file_->GetPath().c_str(), |
| 549 | header_->e_ident[EI_CLASS]); |
| 550 | return false; |
| 551 | } |
| 552 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 553 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 554 | EV_CURRENT, |
| 555 | file_->GetPath().c_str(), |
| 556 | header_->e_ident[EI_CLASS]); |
| 557 | return false; |
| 558 | } |
| 559 | if (ET_DYN != header_->e_type) { |
| 560 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 561 | ET_DYN, |
| 562 | file_->GetPath().c_str(), |
| 563 | header_->e_type); |
| 564 | return false; |
| 565 | } |
| 566 | if (EV_CURRENT != header_->e_version) { |
| 567 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 568 | EV_CURRENT, |
| 569 | file_->GetPath().c_str(), |
| 570 | header_->e_version); |
| 571 | return false; |
| 572 | } |
| 573 | if (0 != header_->e_entry) { |
| 574 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 575 | 0, |
| 576 | file_->GetPath().c_str(), |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 577 | static_cast<int32_t>(header_->e_entry)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 578 | return false; |
| 579 | } |
| 580 | if (0 == header_->e_phoff) { |
| 581 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 582 | file_->GetPath().c_str()); |
| 583 | return false; |
| 584 | } |
| 585 | if (0 == header_->e_shoff) { |
| 586 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 587 | file_->GetPath().c_str()); |
| 588 | return false; |
| 589 | } |
| 590 | if (0 == header_->e_ehsize) { |
| 591 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 592 | file_->GetPath().c_str()); |
| 593 | return false; |
| 594 | } |
| 595 | if (0 == header_->e_phentsize) { |
| 596 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 597 | file_->GetPath().c_str()); |
| 598 | return false; |
| 599 | } |
| 600 | if (0 == header_->e_phnum) { |
| 601 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 602 | file_->GetPath().c_str()); |
| 603 | return false; |
| 604 | } |
| 605 | if (0 == header_->e_shentsize) { |
| 606 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 607 | file_->GetPath().c_str()); |
| 608 | return false; |
| 609 | } |
| 610 | if (0 == header_->e_shnum) { |
| 611 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 612 | file_->GetPath().c_str()); |
| 613 | return false; |
| 614 | } |
| 615 | if (0 == header_->e_shstrndx) { |
| 616 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 617 | file_->GetPath().c_str()); |
| 618 | return false; |
| 619 | } |
| 620 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 621 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 622 | header_->e_shstrndx, |
| 623 | header_->e_shnum, |
| 624 | file_->GetPath().c_str()); |
| 625 | return false; |
| 626 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 627 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 628 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 629 | if (header_->e_phoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 630 | *error_msg = StringPrintf("Failed to find e_phoff value %" PRIu64 " less than %zd in %s", |
| 631 | static_cast<uint64_t>(header_->e_phoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 632 | Size(), |
| 633 | file_->GetPath().c_str()); |
| 634 | return false; |
| 635 | } |
| 636 | if (header_->e_shoff >= Size()) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 637 | *error_msg = StringPrintf("Failed to find e_shoff value %" PRIu64 " less than %zd in %s", |
| 638 | static_cast<uint64_t>(header_->e_shoff), |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 639 | Size(), |
| 640 | file_->GetPath().c_str()); |
| 641 | return false; |
| 642 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 647 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 648 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 649 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 650 | Elf_Ehdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 651 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 652 | ::GetHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 653 | 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] | 654 | return *header_; |
| 655 | } |
| 656 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 657 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 658 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 659 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 660 | uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 661 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 662 | ::GetProgramHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 663 | CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity |
| 664 | // check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 665 | return program_headers_start_; |
| 666 | } |
| 667 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 668 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 669 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 670 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 671 | uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 672 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 673 | ::GetSectionHeadersStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 674 | CHECK(!program_header_only_); // Only used in "full" mode. |
| 675 | CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 676 | return section_headers_start_; |
| 677 | } |
| 678 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 679 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 680 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 681 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 682 | Elf_Phdr& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 683 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 684 | ::GetDynamicProgramHeader() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 685 | CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 686 | return *dynamic_program_header_; |
| 687 | } |
| 688 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 689 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 690 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 691 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 692 | Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 693 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 694 | ::GetDynamicSectionStart() const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 695 | CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 696 | return dynamic_section_start_; |
| 697 | } |
| 698 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 699 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 700 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 701 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 702 | Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 703 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 704 | ::GetSymbolSectionStart(Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 705 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 706 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 707 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 708 | return symtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 709 | break; |
| 710 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 711 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 712 | return dynsym_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | default: { |
| 716 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 717 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 718 | } |
| 719 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 722 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 723 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 724 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 725 | const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 726 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 727 | ::GetStringSectionStart(Elf_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 728 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 729 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 730 | case SHT_SYMTAB: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 731 | return strtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 732 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 733 | case SHT_DYNSYM: { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 734 | return dynstr_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 735 | } |
| 736 | default: { |
| 737 | LOG(FATAL) << section_type; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 738 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 739 | } |
| 740 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 741 | } |
| 742 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 743 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 744 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 745 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 746 | const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 747 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 748 | ::GetString(Elf_Word section_type, Elf_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 749 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 750 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 751 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 752 | } |
| 753 | const char* string_section_start = GetStringSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 754 | if (string_section_start == nullptr) { |
| 755 | return nullptr; |
| 756 | } |
| 757 | return string_section_start + i; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 758 | } |
| 759 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 760 | // WARNING: The following methods do not check for an error condition (non-existent hash section). |
| 761 | // It is the caller's job to do this. |
| 762 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 763 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 764 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 765 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 766 | Elf_Word* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 767 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 768 | ::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 769 | return hash_section_start_; |
| 770 | } |
| 771 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 772 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 773 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 774 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 775 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 776 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 777 | ::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 778 | return GetHashSectionStart()[0]; |
| 779 | } |
| 780 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 781 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 782 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 783 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 784 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 785 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 786 | ::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 787 | return GetHashSectionStart()[1]; |
| 788 | } |
| 789 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 790 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 791 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 792 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 793 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 794 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 795 | ::GetHashBucket(size_t i, bool* ok) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 796 | if (i >= GetHashBucketNum()) { |
| 797 | *ok = false; |
| 798 | return 0; |
| 799 | } |
| 800 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 801 | // 0 is nbucket, 1 is nchain |
| 802 | return GetHashSectionStart()[2 + i]; |
| 803 | } |
| 804 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 805 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 806 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 807 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 808 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 809 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 810 | ::GetHashChain(size_t i, bool* ok) const { |
Yevgeny Rouban | acb0138 | 2014-11-24 13:40:56 +0600 | [diff] [blame] | 811 | if (i >= GetHashChainNum()) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 812 | *ok = false; |
| 813 | return 0; |
| 814 | } |
| 815 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 816 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 817 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 818 | } |
| 819 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 820 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 821 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 822 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 823 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 824 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 825 | ::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 826 | return GetHeader().e_phnum; |
| 827 | } |
| 828 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 829 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 830 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 831 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 832 | Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 833 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 834 | ::GetProgramHeader(Elf_Word i) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 835 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 836 | uint8_t* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 837 | if (program_header >= End()) { |
| 838 | return nullptr; // Failure condition. |
| 839 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 840 | return reinterpret_cast<Elf_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 843 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 844 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 845 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 846 | Elf_Phdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 847 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 848 | ::FindProgamHeaderByType(Elf_Word type) const { |
| 849 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 850 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 851 | if (program_header->p_type == type) { |
| 852 | return program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 853 | } |
| 854 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 855 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 856 | } |
| 857 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 858 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 859 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 860 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 861 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 862 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 863 | ::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 864 | return GetHeader().e_shnum; |
| 865 | } |
| 866 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 867 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 868 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 869 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 870 | Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 871 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 872 | ::GetSectionHeader(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 873 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 874 | // Even if we Load(), it doesn't bring in all the sections. |
| 875 | CHECK(!program_header_only_) << file_->GetPath(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 876 | if (i >= GetSectionHeaderNum()) { |
| 877 | return nullptr; // Failure condition. |
| 878 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 879 | uint8_t* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 880 | if (section_header >= End()) { |
| 881 | return nullptr; // Failure condition. |
| 882 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 883 | return reinterpret_cast<Elf_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 884 | } |
| 885 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 886 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 887 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 888 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 889 | Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 890 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 891 | ::FindSectionByType(Elf_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 892 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 893 | // We could change this to switch on known types if they were detected during loading. |
| 894 | CHECK(!program_header_only_) << file_->GetPath(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 895 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 896 | Elf_Shdr* section_header = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 897 | if (section_header->sh_type == type) { |
| 898 | return section_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 901 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 902 | } |
| 903 | |
| 904 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 905 | static unsigned elfhash(const char *_name) { |
| 906 | const unsigned char *name = (const unsigned char *) _name; |
| 907 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 908 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 909 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 910 | h = (h << 4) + *name++; |
| 911 | g = h & 0xf0000000; |
| 912 | h ^= g; |
| 913 | h ^= g >> 24; |
| 914 | } |
| 915 | return h; |
| 916 | } |
| 917 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 918 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 919 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 920 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 921 | Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 922 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 923 | ::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 924 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 927 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 928 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 929 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 930 | const uint8_t* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 931 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 932 | ::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 933 | // Check that we have a hash section. |
| 934 | if (GetHashSectionStart() == nullptr) { |
| 935 | return nullptr; // Failure condition. |
| 936 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 937 | const Elf_Sym* sym = FindDynamicSymbol(symbol_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 938 | if (sym != nullptr) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 939 | // TODO: we need to change this to calculate base_address_ in ::Open, |
| 940 | // 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] | 941 | return base_address_ + sym->st_value; |
| 942 | } else { |
| 943 | return nullptr; |
| 944 | } |
| 945 | } |
| 946 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 947 | // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 948 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 949 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 950 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 951 | const Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 952 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 953 | ::FindDynamicSymbol(const std::string& symbol_name) const { |
Andreas Gampe | c48b206 | 2014-09-08 23:39:45 -0700 | [diff] [blame] | 954 | if (GetHashBucketNum() == 0) { |
| 955 | // No dynamic symbols at all. |
| 956 | return nullptr; |
| 957 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 958 | Elf_Word hash = elfhash(symbol_name.c_str()); |
| 959 | Elf_Word bucket_index = hash % GetHashBucketNum(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 960 | bool ok; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 961 | Elf_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 962 | if (!ok) { |
| 963 | return nullptr; |
| 964 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 965 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 966 | Elf_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 967 | if (symbol == nullptr) { |
| 968 | return nullptr; // Failure condition. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 969 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 970 | const char* name = GetString(SHT_DYNSYM, symbol->st_name); |
| 971 | if (symbol_name == name) { |
| 972 | return symbol; |
| 973 | } |
| 974 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok); |
| 975 | if (!ok) { |
| 976 | return nullptr; |
| 977 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 978 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 979 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 980 | } |
| 981 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 982 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 983 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 984 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 985 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 986 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 987 | ::IsSymbolSectionType(Elf_Word section_type) { |
| 988 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
| 989 | } |
| 990 | |
| 991 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 992 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 993 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 994 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 995 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 996 | ::GetSymbolNum(Elf_Shdr& section_header) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 997 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 998 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 999 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 1000 | return section_header.sh_size / section_header.sh_entsize; |
| 1001 | } |
| 1002 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1003 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1004 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1005 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1006 | Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1007 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1008 | ::GetSymbol(Elf_Word section_type, |
| 1009 | Elf_Word i) const { |
| 1010 | Elf_Sym* sym_start = GetSymbolSectionStart(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1011 | if (sym_start == nullptr) { |
| 1012 | return nullptr; |
| 1013 | } |
| 1014 | return sym_start + i; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1015 | } |
| 1016 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1017 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1018 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1019 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1020 | typename ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1021 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1022 | ::SymbolTable** ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1023 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1024 | ::GetSymbolTable(Elf_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1025 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 1026 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1027 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1028 | return &symtab_symbol_table_; |
| 1029 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1030 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1031 | return &dynsym_symbol_table_; |
| 1032 | } |
| 1033 | default: { |
| 1034 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1035 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1036 | } |
| 1037 | } |
| 1038 | } |
| 1039 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1040 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1041 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1042 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1043 | Elf_Sym* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1044 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1045 | ::FindSymbolByName(Elf_Word section_type, |
| 1046 | const std::string& symbol_name, |
| 1047 | bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1048 | CHECK(!program_header_only_) << file_->GetPath(); |
| 1049 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1050 | |
| 1051 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1052 | if (*symbol_table != nullptr || build_map) { |
| 1053 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1054 | DCHECK(build_map); |
| 1055 | *symbol_table = new SymbolTable; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1056 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1057 | if (symbol_section == nullptr) { |
| 1058 | return nullptr; // Failure condition. |
| 1059 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1060 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1061 | if (string_section == nullptr) { |
| 1062 | return nullptr; // Failure condition. |
| 1063 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1064 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1065 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1066 | if (symbol == nullptr) { |
| 1067 | return nullptr; // Failure condition. |
| 1068 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1069 | unsigned char type = (sizeof(Elf_Addr) == sizeof(Elf64_Addr)) |
| 1070 | ? ELF64_ST_TYPE(symbol->st_info) |
| 1071 | : ELF32_ST_TYPE(symbol->st_info); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1072 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1073 | continue; |
| 1074 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1075 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1076 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1077 | continue; |
| 1078 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1079 | std::pair<typename SymbolTable::iterator, bool> result = |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1080 | (*symbol_table)->insert(std::make_pair(name, symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1081 | if (!result.second) { |
| 1082 | // 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] | 1083 | if ((symbol->st_value != result.first->second->st_value) || |
| 1084 | (symbol->st_size != result.first->second->st_size) || |
| 1085 | (symbol->st_info != result.first->second->st_info) || |
| 1086 | (symbol->st_other != result.first->second->st_other) || |
| 1087 | (symbol->st_shndx != result.first->second->st_shndx)) { |
| 1088 | return nullptr; // Failure condition. |
| 1089 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1090 | } |
| 1091 | } |
| 1092 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1093 | CHECK(*symbol_table != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1094 | typename SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1095 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1096 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1097 | } |
| 1098 | return it->second; |
| 1099 | } |
| 1100 | |
| 1101 | // Fall back to linear search |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1102 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1103 | if (symbol_section == nullptr) { |
| 1104 | return nullptr; |
| 1105 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1106 | Elf_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1107 | if (string_section == nullptr) { |
| 1108 | return nullptr; |
| 1109 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1110 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1111 | Elf_Sym* symbol = GetSymbol(section_type, i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1112 | if (symbol == nullptr) { |
| 1113 | return nullptr; // Failure condition. |
| 1114 | } |
| 1115 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1116 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1117 | continue; |
| 1118 | } |
| 1119 | if (symbol_name == name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1120 | return symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1121 | } |
| 1122 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1123 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1124 | } |
| 1125 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1126 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1127 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1128 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1129 | Elf_Addr ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1130 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1131 | ::FindSymbolAddress(Elf_Word section_type, |
| 1132 | const std::string& symbol_name, |
| 1133 | bool build_map) { |
| 1134 | Elf_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1135 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1136 | return 0; |
| 1137 | } |
| 1138 | return symbol->st_value; |
| 1139 | } |
| 1140 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1141 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1142 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1143 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1144 | const char* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1145 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1146 | ::GetString(Elf_Shdr& string_section, Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1147 | CHECK(!program_header_only_) << file_->GetPath(); |
| 1148 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1149 | if (static_cast<Elf_Word>(SHT_STRTAB) != string_section.sh_type) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1150 | return nullptr; // Failure condition. |
| 1151 | } |
| 1152 | if (i >= string_section.sh_size) { |
| 1153 | return nullptr; |
| 1154 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1155 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1156 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1157 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1158 | uint8_t* strings = Begin() + string_section.sh_offset; |
| 1159 | uint8_t* string = strings + i; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1160 | if (string >= End()) { |
| 1161 | return nullptr; |
| 1162 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1163 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1164 | } |
| 1165 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1166 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1167 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1168 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1169 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1170 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1171 | ::GetDynamicNum() const { |
| 1172 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1173 | } |
| 1174 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1175 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1176 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1177 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1178 | Elf_Dyn& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1179 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1180 | ::GetDynamic(Elf_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1181 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 1182 | return *(GetDynamicSectionStart() + i); |
| 1183 | } |
| 1184 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1185 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1186 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1187 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1188 | Elf_Dyn* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1189 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1190 | ::FindDynamicByType(Elf_Sword type) const { |
| 1191 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1192 | Elf_Dyn* dyn = &GetDynamic(i); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1193 | if (dyn->d_tag == type) { |
| 1194 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1195 | } |
| 1196 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1197 | return NULL; |
| 1198 | } |
| 1199 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1200 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1201 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1202 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1203 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1204 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1205 | ::FindDynamicValueByType(Elf_Sword type) const { |
| 1206 | Elf_Dyn* dyn = FindDynamicByType(type); |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 1207 | if (dyn == NULL) { |
| 1208 | return 0; |
| 1209 | } else { |
| 1210 | return dyn->d_un.d_val; |
| 1211 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1212 | } |
| 1213 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1214 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1215 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1216 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1217 | Elf_Rel* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1218 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1219 | ::GetRelSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1220 | 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] | 1221 | return reinterpret_cast<Elf_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1222 | } |
| 1223 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1224 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1225 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1226 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1227 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1228 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1229 | ::GetRelNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1230 | 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] | 1231 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 1232 | return section_header.sh_size / section_header.sh_entsize; |
| 1233 | } |
| 1234 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1235 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1236 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1237 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1238 | Elf_Rel& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1239 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1240 | ::GetRel(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1241 | 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] | 1242 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 1243 | return *(GetRelSectionStart(section_header) + i); |
| 1244 | } |
| 1245 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1246 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1247 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1248 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1249 | Elf_Rela* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1250 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1251 | ::GetRelaSectionStart(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1252 | 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] | 1253 | return reinterpret_cast<Elf_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1254 | } |
| 1255 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1256 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1257 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1258 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1259 | Elf_Word ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1260 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1261 | ::GetRelaNum(Elf_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1262 | 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] | 1263 | return section_header.sh_size / section_header.sh_entsize; |
| 1264 | } |
| 1265 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1266 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1267 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1268 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1269 | Elf_Rela& ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1270 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1271 | ::GetRela(Elf_Shdr& section_header, Elf_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1272 | 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] | 1273 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 1274 | return *(GetRelaSectionStart(section_header) + i); |
| 1275 | } |
| 1276 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1277 | // Base on bionic phdr_table_get_load_size |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1278 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1279 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1280 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1281 | size_t ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1282 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1283 | ::GetLoadedSize() const { |
| 1284 | Elf_Addr min_vaddr = 0xFFFFFFFFu; |
| 1285 | Elf_Addr max_vaddr = 0x00000000u; |
| 1286 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1287 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1288 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1289 | continue; |
| 1290 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1291 | Elf_Addr begin_vaddr = program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1292 | if (begin_vaddr < min_vaddr) { |
| 1293 | min_vaddr = begin_vaddr; |
| 1294 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1295 | Elf_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1296 | if (end_vaddr > max_vaddr) { |
| 1297 | max_vaddr = end_vaddr; |
| 1298 | } |
| 1299 | } |
| 1300 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 1301 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 1302 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 1303 | size_t loaded_size = max_vaddr - min_vaddr; |
| 1304 | return loaded_size; |
| 1305 | } |
| 1306 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1307 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1308 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1309 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1310 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1311 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1312 | ::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1313 | CHECK(program_header_only_) << file_->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1314 | |
| 1315 | if (executable) { |
| 1316 | InstructionSet elf_ISA = kNone; |
| 1317 | switch (GetHeader().e_machine) { |
| 1318 | case EM_ARM: { |
| 1319 | elf_ISA = kArm; |
| 1320 | break; |
| 1321 | } |
| 1322 | case EM_AARCH64: { |
| 1323 | elf_ISA = kArm64; |
| 1324 | break; |
| 1325 | } |
| 1326 | case EM_386: { |
| 1327 | elf_ISA = kX86; |
| 1328 | break; |
| 1329 | } |
| 1330 | case EM_X86_64: { |
| 1331 | elf_ISA = kX86_64; |
| 1332 | break; |
| 1333 | } |
| 1334 | case EM_MIPS: { |
| 1335 | elf_ISA = kMips; |
| 1336 | break; |
| 1337 | } |
| 1338 | } |
| 1339 | |
| 1340 | if (elf_ISA != kRuntimeISA) { |
| 1341 | std::ostringstream oss; |
| 1342 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 1343 | *error_msg = oss.str(); |
| 1344 | return false; |
| 1345 | } |
| 1346 | } |
| 1347 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1348 | bool reserved = false; |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1349 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 1350 | Elf_Phdr* program_header = GetProgramHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1351 | if (program_header == nullptr) { |
| 1352 | *error_msg = StringPrintf("No program header for entry %d in ELF file %s.", |
| 1353 | i, file_->GetPath().c_str()); |
| 1354 | return false; |
| 1355 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1356 | |
| 1357 | // Record .dynamic header information for later use |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1358 | if (program_header->p_type == PT_DYNAMIC) { |
| 1359 | dynamic_program_header_ = program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1360 | continue; |
| 1361 | } |
| 1362 | |
| 1363 | // Not something to load, move on. |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1364 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1365 | continue; |
| 1366 | } |
| 1367 | |
| 1368 | // Found something to load. |
| 1369 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1370 | // Before load the actual segments, reserve a contiguous chunk |
| 1371 | // of required size and address for all segments, but with no |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1372 | // permissions. We'll then carve that up with the proper |
| 1373 | // permissions as we load the actual segments. If p_vaddr is |
| 1374 | // non-zero, the segments require the specific address specified, |
| 1375 | // which either was specified in the file because we already set |
| 1376 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1377 | int64_t temp_file_length = file_->GetLength(); |
| 1378 | if (temp_file_length < 0) { |
| 1379 | errno = -temp_file_length; |
| 1380 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 1381 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 1382 | return false; |
| 1383 | } |
| 1384 | size_t file_length = static_cast<size_t>(temp_file_length); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1385 | if (!reserved) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1386 | uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr); |
| 1387 | uint8_t* reserve_base_override = reserve_base; |
| 1388 | // Override the base (e.g. when compiling with --compile-pic) |
| 1389 | if (requested_base_ != nullptr) { |
| 1390 | reserve_base_override = requested_base_; |
| 1391 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1392 | std::string reservation_name("ElfFile reservation for "); |
| 1393 | reservation_name += file_->GetPath(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1394 | std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1395 | reserve_base_override, |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1396 | GetLoadedSize(), PROT_NONE, false, |
| 1397 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1398 | if (reserve.get() == nullptr) { |
| 1399 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 1400 | reservation_name.c_str(), error_msg->c_str()); |
| 1401 | return false; |
| 1402 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1403 | reserved = true; |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1404 | |
| 1405 | // Base address is the difference of actual mapped location and the p_vaddr |
| 1406 | base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin()) |
| 1407 | - reinterpret_cast<uintptr_t>(reserve_base)); |
| 1408 | // By adding the p_vaddr of a section/symbol to base_address_ we will always get the |
| 1409 | // dynamic memory address of where that object is actually mapped |
| 1410 | // |
| 1411 | // TODO: base_address_ needs to be calculated in ::Open, otherwise |
| 1412 | // FindDynamicSymbolAddress returns the wrong values until Load is called. |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1413 | segments_.push_back(reserve.release()); |
| 1414 | } |
| 1415 | // empty segment, nothing to map |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1416 | if (program_header->p_memsz == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1417 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1418 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1419 | uint8_t* p_vaddr = base_address_ + program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1420 | int prot = 0; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1421 | if (executable && ((program_header->p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1422 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1423 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1424 | if ((program_header->p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1425 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1426 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1427 | if ((program_header->p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1428 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1429 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1430 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1431 | if (writable_) { |
| 1432 | prot |= PROT_WRITE; |
| 1433 | flags |= MAP_SHARED; |
| 1434 | } else { |
| 1435 | flags |= MAP_PRIVATE; |
| 1436 | } |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1437 | if (file_length < (program_header->p_offset + program_header->p_memsz)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1438 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1439 | "%d of %" PRIu64 " bytes: '%s'", file_length, i, |
| 1440 | static_cast<uint64_t>(program_header->p_offset + program_header->p_memsz), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1441 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 1442 | return false; |
| 1443 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1444 | std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1445 | program_header->p_memsz, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1446 | prot, flags, file_->Fd(), |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1447 | program_header->p_offset, |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1448 | true, // implies MAP_FIXED |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1449 | file_->GetPath().c_str(), |
| 1450 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1451 | if (segment.get() == nullptr) { |
| 1452 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 1453 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1454 | return false; |
| 1455 | } |
| 1456 | if (segment->Begin() != p_vaddr) { |
| 1457 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 1458 | "instead mapped to %p", |
| 1459 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1460 | return false; |
| 1461 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1462 | segments_.push_back(segment.release()); |
| 1463 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1464 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1465 | // 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] | 1466 | uint8_t* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr; |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1467 | if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) { |
| 1468 | *error_msg = StringPrintf("dynamic section address invalid in ELF file %s", |
| 1469 | file_->GetPath().c_str()); |
| 1470 | return false; |
| 1471 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1472 | dynamic_section_start_ = reinterpret_cast<Elf_Dyn*>(dsptr); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1473 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1474 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 1475 | Elf_Dyn& elf_dyn = GetDynamic(i); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1476 | uint8_t* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1477 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1478 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1479 | if (!ValidPointer(d_ptr)) { |
| 1480 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1481 | d_ptr, file_->GetPath().c_str()); |
| 1482 | return false; |
| 1483 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1484 | hash_section_start_ = reinterpret_cast<Elf_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1485 | break; |
| 1486 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1487 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1488 | if (!ValidPointer(d_ptr)) { |
| 1489 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1490 | d_ptr, file_->GetPath().c_str()); |
| 1491 | return false; |
| 1492 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1493 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 1494 | break; |
| 1495 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1496 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1497 | if (!ValidPointer(d_ptr)) { |
| 1498 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1499 | d_ptr, file_->GetPath().c_str()); |
| 1500 | return false; |
| 1501 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1502 | dynsym_section_start_ = reinterpret_cast<Elf_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1503 | break; |
| 1504 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1505 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1506 | if (GetDynamicNum() != i+1) { |
| 1507 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 1508 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 1509 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 1510 | return false; |
| 1511 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1512 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | } |
| 1516 | |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1517 | // Check for the existence of some sections. |
| 1518 | if (!CheckSectionsExist(error_msg)) { |
| 1519 | return false; |
| 1520 | } |
| 1521 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1522 | // Use GDB JIT support to do stack backtrace, etc. |
| 1523 | if (executable) { |
| 1524 | GdbJITSupport(); |
| 1525 | } |
| 1526 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1527 | return true; |
| 1528 | } |
| 1529 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1530 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1531 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1532 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1533 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1534 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1535 | ::ValidPointer(const uint8_t* start) const { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1536 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 1537 | const MemMap* segment = segments_[i]; |
| 1538 | if (segment->Begin() <= start && start < segment->End()) { |
| 1539 | return true; |
| 1540 | } |
| 1541 | } |
| 1542 | return false; |
| 1543 | } |
| 1544 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1545 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1546 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 1547 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 1548 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 1549 | Elf_Shdr* ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 1550 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 1551 | ::FindSectionByName(const std::string& name) const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1552 | CHECK(!program_header_only_); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1553 | Elf_Shdr* shstrtab_sec = GetSectionNameStringSection(); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1554 | if (shstrtab_sec == nullptr) { |
| 1555 | return nullptr; |
| 1556 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1557 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 1558 | Elf_Shdr* shdr = GetSectionHeader(i); |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1559 | if (shdr == nullptr) { |
| 1560 | return nullptr; |
| 1561 | } |
| 1562 | const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1563 | if (sec_name == nullptr) { |
| 1564 | continue; |
| 1565 | } |
| 1566 | if (name == sec_name) { |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1567 | return shdr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1571 | } |
| 1572 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1573 | struct PACKED(1) FDE32 { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1574 | uint32_t raw_length_; |
| 1575 | uint32_t GetLength() { |
| 1576 | return raw_length_ + sizeof(raw_length_); |
| 1577 | } |
| 1578 | uint32_t CIE_pointer; |
| 1579 | uint32_t initial_location; |
| 1580 | uint32_t address_range; |
| 1581 | uint8_t instructions[0]; |
| 1582 | }; |
| 1583 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1584 | static FDE32* NextFDE(FDE32* frame) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1585 | uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1586 | fde_bytes += frame->GetLength(); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1587 | return reinterpret_cast<FDE32*>(fde_bytes); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1590 | static bool IsFDE(FDE32* frame) { |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1591 | return frame->CIE_pointer != 0; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1592 | } |
| 1593 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1594 | struct PACKED(1) FDE64 { |
| 1595 | uint32_t raw_length_; |
| 1596 | uint64_t extended_length_; |
| 1597 | uint64_t GetLength() { |
| 1598 | return extended_length_ + sizeof(raw_length_) + sizeof(extended_length_); |
| 1599 | } |
| 1600 | uint64_t CIE_pointer; |
| 1601 | uint64_t initial_location; |
| 1602 | uint64_t address_range; |
| 1603 | uint8_t instructions[0]; |
| 1604 | }; |
| 1605 | |
| 1606 | static FDE64* NextFDE(FDE64* frame) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1607 | uint8_t* fde_bytes = reinterpret_cast<uint8_t*>(frame); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1608 | fde_bytes += frame->GetLength(); |
| 1609 | return reinterpret_cast<FDE64*>(fde_bytes); |
| 1610 | } |
| 1611 | |
| 1612 | static bool IsFDE(FDE64* frame) { |
| 1613 | return frame->CIE_pointer != 0; |
| 1614 | } |
| 1615 | |
| 1616 | static bool FixupEHFrame(off_t base_address_delta, |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1617 | uint8_t* eh_frame, size_t eh_frame_size) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1618 | if (*(reinterpret_cast<uint32_t*>(eh_frame)) == 0xffffffff) { |
| 1619 | FDE64* last_frame = reinterpret_cast<FDE64*>(eh_frame + eh_frame_size); |
| 1620 | FDE64* frame = NextFDE(reinterpret_cast<FDE64*>(eh_frame)); |
| 1621 | for (; frame < last_frame; frame = NextFDE(frame)) { |
| 1622 | if (!IsFDE(frame)) { |
| 1623 | return false; |
| 1624 | } |
| 1625 | frame->initial_location += base_address_delta; |
| 1626 | } |
| 1627 | return true; |
| 1628 | } else { |
| 1629 | FDE32* last_frame = reinterpret_cast<FDE32*>(eh_frame + eh_frame_size); |
| 1630 | FDE32* frame = NextFDE(reinterpret_cast<FDE32*>(eh_frame)); |
| 1631 | for (; frame < last_frame; frame = NextFDE(frame)) { |
| 1632 | if (!IsFDE(frame)) { |
| 1633 | return false; |
| 1634 | } |
| 1635 | frame->initial_location += base_address_delta; |
| 1636 | } |
| 1637 | return true; |
| 1638 | } |
| 1639 | } |
| 1640 | |
| 1641 | static uint8_t* NextLeb128(uint8_t* current) { |
| 1642 | DecodeUnsignedLeb128(const_cast<const uint8_t**>(¤t)); |
| 1643 | return current; |
| 1644 | } |
| 1645 | |
| 1646 | struct PACKED(1) DebugLineHeader { |
| 1647 | uint32_t unit_length_; // TODO 32-bit specific size |
| 1648 | uint16_t version_; |
| 1649 | uint32_t header_length_; // TODO 32-bit specific size |
| 1650 | uint8_t minimum_instruction_lenght_; |
| 1651 | uint8_t maximum_operations_per_instruction_; |
| 1652 | uint8_t default_is_stmt_; |
| 1653 | int8_t line_base_; |
| 1654 | uint8_t line_range_; |
| 1655 | uint8_t opcode_base_; |
| 1656 | uint8_t remaining_[0]; |
| 1657 | |
| 1658 | bool IsStandardOpcode(const uint8_t* op) const { |
| 1659 | return *op != 0 && *op < opcode_base_; |
| 1660 | } |
| 1661 | |
| 1662 | bool IsExtendedOpcode(const uint8_t* op) const { |
| 1663 | return *op == 0; |
| 1664 | } |
| 1665 | |
| 1666 | const uint8_t* GetStandardOpcodeLengths() const { |
| 1667 | return remaining_; |
| 1668 | } |
| 1669 | |
| 1670 | uint8_t* GetNextOpcode(uint8_t* op) const { |
| 1671 | if (IsExtendedOpcode(op)) { |
| 1672 | uint8_t* length_field = op + 1; |
| 1673 | uint32_t length = DecodeUnsignedLeb128(const_cast<const uint8_t**>(&length_field)); |
| 1674 | return length_field + length; |
| 1675 | } else if (!IsStandardOpcode(op)) { |
| 1676 | return op + 1; |
| 1677 | } else if (*op == DW_LNS_fixed_advance_pc) { |
| 1678 | return op + 1 + sizeof(uint16_t); |
| 1679 | } else { |
| 1680 | uint8_t num_args = GetStandardOpcodeLengths()[*op - 1]; |
| 1681 | op += 1; |
| 1682 | for (int i = 0; i < num_args; i++) { |
| 1683 | op = NextLeb128(op); |
| 1684 | } |
| 1685 | return op; |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | uint8_t* GetDebugLineData() const { |
| 1690 | const uint8_t* hdr_start = |
| 1691 | reinterpret_cast<const uint8_t*>(&header_length_) + sizeof(header_length_); |
| 1692 | return const_cast<uint8_t*>(hdr_start + header_length_); |
| 1693 | } |
| 1694 | }; |
| 1695 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1696 | class DebugLineInstructionIterator FINAL { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1697 | public: |
| 1698 | static DebugLineInstructionIterator* Create(DebugLineHeader* header, size_t section_size) { |
| 1699 | std::unique_ptr<DebugLineInstructionIterator> line_iter( |
| 1700 | new DebugLineInstructionIterator(header, section_size)); |
| 1701 | if (line_iter.get() == nullptr) { |
| 1702 | return nullptr; |
| 1703 | } else { |
| 1704 | return line_iter.release(); |
| 1705 | } |
| 1706 | } |
| 1707 | |
| 1708 | ~DebugLineInstructionIterator() {} |
| 1709 | |
| 1710 | bool Next() { |
| 1711 | if (current_instruction_ == nullptr) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1712 | return false; |
| 1713 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1714 | current_instruction_ = header_->GetNextOpcode(current_instruction_); |
| 1715 | if (current_instruction_ >= last_instruction_) { |
| 1716 | current_instruction_ = nullptr; |
| 1717 | return false; |
| 1718 | } else { |
| 1719 | return true; |
| 1720 | } |
| 1721 | } |
| 1722 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1723 | uint8_t* GetInstruction() const { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1724 | return current_instruction_; |
| 1725 | } |
| 1726 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1727 | bool IsExtendedOpcode() const { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1728 | return header_->IsExtendedOpcode(current_instruction_); |
| 1729 | } |
| 1730 | |
| 1731 | uint8_t GetOpcode() { |
| 1732 | if (!IsExtendedOpcode()) { |
| 1733 | return *current_instruction_; |
| 1734 | } else { |
| 1735 | uint8_t* len_ptr = current_instruction_ + 1; |
| 1736 | return *NextLeb128(len_ptr); |
| 1737 | } |
| 1738 | } |
| 1739 | |
| 1740 | uint8_t* GetArguments() { |
| 1741 | if (!IsExtendedOpcode()) { |
| 1742 | return current_instruction_ + 1; |
| 1743 | } else { |
| 1744 | uint8_t* len_ptr = current_instruction_ + 1; |
| 1745 | return NextLeb128(len_ptr) + 1; |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | private: |
| 1750 | DebugLineInstructionIterator(DebugLineHeader* header, size_t size) |
| 1751 | : header_(header), last_instruction_(reinterpret_cast<uint8_t*>(header) + size), |
| 1752 | current_instruction_(header->GetDebugLineData()) {} |
| 1753 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1754 | DebugLineHeader* const header_; |
| 1755 | uint8_t* const last_instruction_; |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 1756 | uint8_t* current_instruction_; |
| 1757 | }; |
| 1758 | |
| 1759 | static bool FixupDebugLine(off_t base_offset_delta, DebugLineInstructionIterator* iter) { |
| 1760 | while (iter->Next()) { |
| 1761 | if (iter->IsExtendedOpcode() && iter->GetOpcode() == DW_LNE_set_address) { |
| 1762 | *reinterpret_cast<uint32_t*>(iter->GetArguments()) += base_offset_delta; |
| 1763 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1764 | } |
| 1765 | return true; |
| 1766 | } |
| 1767 | |
| 1768 | struct PACKED(1) DebugInfoHeader { |
| 1769 | uint32_t unit_length; // TODO 32-bit specific size |
| 1770 | uint16_t version; |
| 1771 | uint32_t debug_abbrev_offset; // TODO 32-bit specific size |
| 1772 | uint8_t address_size; |
| 1773 | }; |
| 1774 | |
| 1775 | // Returns -1 if it is variable length, which we will just disallow for now. |
| 1776 | static int32_t FormLength(uint32_t att) { |
| 1777 | switch (att) { |
| 1778 | case DW_FORM_data1: |
| 1779 | case DW_FORM_flag: |
| 1780 | case DW_FORM_flag_present: |
| 1781 | case DW_FORM_ref1: |
| 1782 | return 1; |
| 1783 | |
| 1784 | case DW_FORM_data2: |
| 1785 | case DW_FORM_ref2: |
| 1786 | return 2; |
| 1787 | |
| 1788 | case DW_FORM_addr: // TODO 32-bit only |
| 1789 | case DW_FORM_ref_addr: // TODO 32-bit only |
| 1790 | case DW_FORM_sec_offset: // TODO 32-bit only |
| 1791 | case DW_FORM_strp: // TODO 32-bit only |
| 1792 | case DW_FORM_data4: |
| 1793 | case DW_FORM_ref4: |
| 1794 | return 4; |
| 1795 | |
| 1796 | case DW_FORM_data8: |
| 1797 | case DW_FORM_ref8: |
| 1798 | case DW_FORM_ref_sig8: |
| 1799 | return 8; |
| 1800 | |
| 1801 | case DW_FORM_block: |
| 1802 | case DW_FORM_block1: |
| 1803 | case DW_FORM_block2: |
| 1804 | case DW_FORM_block4: |
| 1805 | case DW_FORM_exprloc: |
| 1806 | case DW_FORM_indirect: |
| 1807 | case DW_FORM_ref_udata: |
| 1808 | case DW_FORM_sdata: |
| 1809 | case DW_FORM_string: |
| 1810 | case DW_FORM_udata: |
| 1811 | default: |
| 1812 | return -1; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1813 | } |
| 1814 | } |
| 1815 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1816 | class DebugTag FINAL { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1817 | public: |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1818 | ~DebugTag() {} |
| 1819 | // Creates a new tag and moves data pointer up to the start of the next one. |
| 1820 | // nullptr means error. |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1821 | static DebugTag* Create(const uint8_t** data_pointer) { |
| 1822 | const uint8_t* data = *data_pointer; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1823 | uint32_t index = DecodeUnsignedLeb128(&data); |
| 1824 | std::unique_ptr<DebugTag> tag(new DebugTag(index)); |
| 1825 | tag->size_ = static_cast<uint32_t>( |
| 1826 | reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer)); |
| 1827 | // skip the abbrev |
| 1828 | tag->tag_ = DecodeUnsignedLeb128(&data); |
| 1829 | tag->has_child_ = (*data == 0); |
| 1830 | data++; |
| 1831 | while (true) { |
| 1832 | uint32_t attr = DecodeUnsignedLeb128(&data); |
| 1833 | uint32_t form = DecodeUnsignedLeb128(&data); |
| 1834 | if (attr == 0 && form == 0) { |
| 1835 | break; |
| 1836 | } else if (attr == 0 || form == 0) { |
| 1837 | // Bad abbrev. |
| 1838 | return nullptr; |
| 1839 | } |
| 1840 | int32_t size = FormLength(form); |
| 1841 | if (size == -1) { |
| 1842 | return nullptr; |
| 1843 | } |
| 1844 | tag->AddAttribute(attr, static_cast<uint32_t>(size)); |
| 1845 | } |
| 1846 | *data_pointer = data; |
| 1847 | return tag.release(); |
| 1848 | } |
| 1849 | |
| 1850 | uint32_t GetSize() const { |
| 1851 | return size_; |
| 1852 | } |
| 1853 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1854 | bool HasChild() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1855 | return has_child_; |
| 1856 | } |
| 1857 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1858 | uint32_t GetTagNumber() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1859 | return tag_; |
| 1860 | } |
| 1861 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1862 | uint32_t GetIndex() const { |
| 1863 | return index_; |
| 1864 | } |
| 1865 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1866 | // Gets the offset of a particular attribute in this tag structure. |
| 1867 | // Interpretation of the data is left to the consumer. 0 is returned if the |
| 1868 | // tag does not contain the attribute. |
| 1869 | uint32_t GetOffsetOf(uint32_t dwarf_attribute) const { |
| 1870 | auto it = off_map_.find(dwarf_attribute); |
| 1871 | if (it == off_map_.end()) { |
| 1872 | return 0; |
| 1873 | } else { |
| 1874 | return it->second; |
| 1875 | } |
| 1876 | } |
| 1877 | |
| 1878 | // Gets the size of attribute |
| 1879 | uint32_t GetAttrSize(uint32_t dwarf_attribute) const { |
| 1880 | auto it = size_map_.find(dwarf_attribute); |
| 1881 | if (it == size_map_.end()) { |
| 1882 | return 0; |
| 1883 | } else { |
| 1884 | return it->second; |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | private: |
Andreas Gampe | daab38c | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1889 | explicit DebugTag(uint32_t index) : index_(index), size_(0), tag_(0), has_child_(false) {} |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1890 | void AddAttribute(uint32_t type, uint32_t attr_size) { |
| 1891 | off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_)); |
| 1892 | size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size)); |
| 1893 | size_ += attr_size; |
| 1894 | } |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1895 | |
| 1896 | const uint32_t index_; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1897 | std::map<uint32_t, uint32_t> off_map_; |
| 1898 | std::map<uint32_t, uint32_t> size_map_; |
| 1899 | uint32_t size_; |
| 1900 | uint32_t tag_; |
| 1901 | bool has_child_; |
| 1902 | }; |
| 1903 | |
| 1904 | class DebugAbbrev { |
| 1905 | public: |
| 1906 | ~DebugAbbrev() {} |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1907 | static DebugAbbrev* Create(const uint8_t* dbg_abbrev, size_t dbg_abbrev_size) { |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1908 | std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev(dbg_abbrev, dbg_abbrev + dbg_abbrev_size)); |
| 1909 | if (!abbrev->ReadAtOffset(0)) { |
| 1910 | return nullptr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1911 | } |
| 1912 | return abbrev.release(); |
| 1913 | } |
| 1914 | |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1915 | bool ReadAtOffset(uint32_t abbrev_offset) { |
| 1916 | tags_.clear(); |
| 1917 | tag_list_.clear(); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1918 | const uint8_t* dbg_abbrev = begin_ + abbrev_offset; |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1919 | while (dbg_abbrev < end_ && *dbg_abbrev != 0) { |
| 1920 | std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev)); |
| 1921 | if (tag.get() == nullptr) { |
| 1922 | return false; |
| 1923 | } else { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1924 | tags_.insert(std::pair<uint32_t, uint32_t>(tag->GetIndex(), tag_list_.size())); |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1925 | tag_list_.push_back(std::move(tag)); |
| 1926 | } |
| 1927 | } |
| 1928 | return true; |
| 1929 | } |
| 1930 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1931 | DebugTag* ReadTag(const uint8_t* entry) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1932 | uint32_t tag_num = DecodeUnsignedLeb128(&entry); |
| 1933 | auto it = tags_.find(tag_num); |
| 1934 | if (it == tags_.end()) { |
| 1935 | return nullptr; |
| 1936 | } else { |
| 1937 | CHECK_GT(tag_list_.size(), it->second); |
| 1938 | return tag_list_.at(it->second).get(); |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | private: |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1943 | DebugAbbrev(const uint8_t* begin, const uint8_t* end) : begin_(begin), end_(end) {} |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 1944 | const uint8_t* const begin_; |
| 1945 | const uint8_t* const end_; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1946 | std::map<uint32_t, uint32_t> tags_; |
| 1947 | std::vector<std::unique_ptr<DebugTag>> tag_list_; |
| 1948 | }; |
| 1949 | |
| 1950 | class DebugInfoIterator { |
| 1951 | public: |
| 1952 | static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size, |
| 1953 | DebugAbbrev* abbrev) { |
| 1954 | std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev)); |
| 1955 | if (iter->GetCurrentTag() == nullptr) { |
| 1956 | return nullptr; |
| 1957 | } else { |
| 1958 | return iter.release(); |
| 1959 | } |
| 1960 | } |
| 1961 | ~DebugInfoIterator() {} |
| 1962 | |
| 1963 | // Moves to the next DIE. Returns false if at last entry. |
| 1964 | // TODO Handle variable length attributes. |
| 1965 | bool next() { |
| 1966 | if (current_entry_ == nullptr || current_tag_ == nullptr) { |
| 1967 | return false; |
| 1968 | } |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1969 | bool reread_abbrev = false; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1970 | current_entry_ += current_tag_->GetSize(); |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1971 | if (reinterpret_cast<DebugInfoHeader*>(current_entry_) >= next_cu_) { |
| 1972 | current_cu_ = next_cu_; |
| 1973 | next_cu_ = GetNextCu(current_cu_); |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1974 | current_entry_ = reinterpret_cast<uint8_t*>(current_cu_) + sizeof(DebugInfoHeader); |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1975 | reread_abbrev = true; |
| 1976 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1977 | if (current_entry_ >= last_entry_) { |
| 1978 | current_entry_ = nullptr; |
| 1979 | return false; |
| 1980 | } |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 1981 | if (reread_abbrev) { |
| 1982 | abbrev_->ReadAtOffset(current_cu_->debug_abbrev_offset); |
| 1983 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1984 | current_tag_ = abbrev_->ReadTag(current_entry_); |
| 1985 | if (current_tag_ == nullptr) { |
| 1986 | current_entry_ = nullptr; |
| 1987 | return false; |
| 1988 | } else { |
| 1989 | return true; |
| 1990 | } |
| 1991 | } |
| 1992 | |
| 1993 | const DebugTag* GetCurrentTag() { |
| 1994 | return const_cast<DebugTag*>(current_tag_); |
| 1995 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 1996 | uint8_t* GetPointerToField(uint8_t dwarf_field) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1997 | if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) { |
| 1998 | return nullptr; |
| 1999 | } |
| 2000 | uint32_t off = current_tag_->GetOffsetOf(dwarf_field); |
| 2001 | if (off == 0) { |
| 2002 | // tag does not have that field. |
| 2003 | return nullptr; |
| 2004 | } else { |
| 2005 | DCHECK_LT(off, current_tag_->GetSize()); |
| 2006 | return current_entry_ + off; |
| 2007 | } |
| 2008 | } |
| 2009 | |
| 2010 | private: |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 2011 | static DebugInfoHeader* GetNextCu(DebugInfoHeader* hdr) { |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2012 | uint8_t* hdr_byte = reinterpret_cast<uint8_t*>(hdr); |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 2013 | return reinterpret_cast<DebugInfoHeader*>(hdr_byte + sizeof(uint32_t) + hdr->unit_length); |
| 2014 | } |
| 2015 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2016 | DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev) |
| 2017 | : abbrev_(abbrev), |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 2018 | current_cu_(header), |
| 2019 | next_cu_(GetNextCu(header)), |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2020 | last_entry_(reinterpret_cast<uint8_t*>(header) + frame_size), |
| 2021 | current_entry_(reinterpret_cast<uint8_t*>(header) + sizeof(DebugInfoHeader)), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2022 | current_tag_(abbrev_->ReadTag(current_entry_)) {} |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2023 | DebugAbbrev* const abbrev_; |
Alex Light | d338ae0 | 2014-08-13 17:15:38 -0700 | [diff] [blame] | 2024 | DebugInfoHeader* current_cu_; |
| 2025 | DebugInfoHeader* next_cu_; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2026 | uint8_t* const last_entry_; |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2027 | uint8_t* current_entry_; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2028 | DebugTag* current_tag_; |
| 2029 | }; |
| 2030 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2031 | static bool FixupDebugInfo(off_t base_address_delta, DebugInfoIterator* iter) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2032 | do { |
| 2033 | if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) || |
| 2034 | iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2035 | LOG(ERROR) << "DWARF information with 64 bit pointers is not supported yet."; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2036 | return false; |
| 2037 | } |
| 2038 | uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc)); |
| 2039 | uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc)); |
| 2040 | if (PC_low != nullptr && PC_high != nullptr) { |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2041 | *PC_low += base_address_delta; |
| 2042 | *PC_high += base_address_delta; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2043 | } |
| 2044 | } while (iter->next()); |
| 2045 | return true; |
| 2046 | } |
| 2047 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2048 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2049 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2050 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2051 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2052 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2053 | ::FixupDebugSections(off_t base_address_delta) { |
| 2054 | const Elf_Shdr* debug_info = FindSectionByName(".debug_info"); |
| 2055 | const Elf_Shdr* debug_abbrev = FindSectionByName(".debug_abbrev"); |
| 2056 | const Elf_Shdr* eh_frame = FindSectionByName(".eh_frame"); |
| 2057 | const Elf_Shdr* debug_str = FindSectionByName(".debug_str"); |
| 2058 | const Elf_Shdr* debug_line = FindSectionByName(".debug_line"); |
| 2059 | const Elf_Shdr* strtab_sec = FindSectionByName(".strtab"); |
| 2060 | const Elf_Shdr* symtab_sec = FindSectionByName(".symtab"); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2061 | |
| 2062 | if (debug_info == nullptr || debug_abbrev == nullptr || |
| 2063 | debug_str == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) { |
| 2064 | // Release version of ART does not generate debug info. |
| 2065 | return true; |
| 2066 | } |
| 2067 | if (base_address_delta == 0) { |
| 2068 | return true; |
| 2069 | } |
| 2070 | if (eh_frame != nullptr && |
| 2071 | !FixupEHFrame(base_address_delta, Begin() + eh_frame->sh_offset, eh_frame->sh_size)) { |
| 2072 | return false; |
| 2073 | } |
| 2074 | |
| 2075 | std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(Begin() + debug_abbrev->sh_offset, |
| 2076 | debug_abbrev->sh_size)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2077 | if (abbrev.get() == nullptr) { |
| 2078 | return false; |
| 2079 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2080 | DebugInfoHeader* info_header = |
| 2081 | reinterpret_cast<DebugInfoHeader*>(Begin() + debug_info->sh_offset); |
| 2082 | std::unique_ptr<DebugInfoIterator> info_iter(DebugInfoIterator::Create(info_header, |
| 2083 | debug_info->sh_size, |
| 2084 | abbrev.get())); |
| 2085 | if (info_iter.get() == nullptr) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2086 | return false; |
| 2087 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2088 | if (debug_line != nullptr) { |
| 2089 | DebugLineHeader* line_header = |
| 2090 | reinterpret_cast<DebugLineHeader*>(Begin() + debug_line->sh_offset); |
| 2091 | std::unique_ptr<DebugLineInstructionIterator> line_iter( |
| 2092 | DebugLineInstructionIterator::Create(line_header, debug_line->sh_size)); |
| 2093 | if (line_iter.get() == nullptr) { |
| 2094 | return false; |
| 2095 | } |
| 2096 | if (!FixupDebugLine(base_address_delta, line_iter.get())) { |
| 2097 | return false; |
| 2098 | } |
| 2099 | } |
| 2100 | return FixupDebugInfo(base_address_delta, info_iter.get()); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2101 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2102 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2103 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2104 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2105 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2106 | void ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2107 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2108 | ::GdbJITSupport() { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2109 | // We only get here if we only are mapping the program header. |
| 2110 | DCHECK(program_header_only_); |
| 2111 | |
| 2112 | // Well, we need the whole file to do this. |
| 2113 | std::string error_msg; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2114 | // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary |
| 2115 | // sections are there. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2116 | std::unique_ptr<ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2117 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>> |
| 2118 | all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE, |
| 2119 | MAP_PRIVATE, &error_msg)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2120 | if (all_ptr.get() == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2121 | return; |
| 2122 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2123 | ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2124 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off>& all = *all_ptr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2125 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2126 | // 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] | 2127 | const Elf_Shdr* eh_frame = all.FindSectionByName(".eh_frame"); |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2128 | if (eh_frame == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2129 | return; |
| 2130 | } |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2131 | |
| 2132 | // Do we have interesting sections? |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2133 | // We need to add in a strtab and symtab to the image. |
| 2134 | // all is MAP_PRIVATE so it can be written to freely. |
| 2135 | // We also already have strtab and symtab so we are fine there. |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2136 | Elf_Ehdr& elf_hdr = all.GetHeader(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2137 | elf_hdr.e_entry = 0; |
| 2138 | elf_hdr.e_phoff = 0; |
| 2139 | elf_hdr.e_phnum = 0; |
| 2140 | elf_hdr.e_phentsize = 0; |
| 2141 | elf_hdr.e_type = ET_EXEC; |
| 2142 | |
Yevgeny Rouban | e3ea838 | 2014-08-08 16:29:38 +0700 | [diff] [blame] | 2143 | // Since base_address_ is 0 if we are actually loaded at a known address (i.e. this is boot.oat) |
| 2144 | // and the actual address stuff starts at in regular files this is good. |
| 2145 | if (!all.FixupDebugSections(reinterpret_cast<intptr_t>(base_address_))) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2146 | LOG(ERROR) << "Failed to load GDB data"; |
| 2147 | return; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2148 | } |
| 2149 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 2150 | jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size()); |
| 2151 | gdb_file_mapping_.reset(all_ptr.release()); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 2152 | } |
| 2153 | |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2154 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2155 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2156 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2157 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2158 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2159 | ::Strip(std::string* error_msg) { |
| 2160 | // ELF files produced by MCLinker look roughly like this |
| 2161 | // |
| 2162 | // +------------+ |
| 2163 | // | Elf_Ehdr | contains number of Elf_Shdr and offset to first |
| 2164 | // +------------+ |
| 2165 | // | Elf_Phdr | program headers |
| 2166 | // | Elf_Phdr | |
| 2167 | // | ... | |
| 2168 | // | Elf_Phdr | |
| 2169 | // +------------+ |
| 2170 | // | section | mixture of needed and unneeded sections |
| 2171 | // +------------+ |
| 2172 | // | section | |
| 2173 | // +------------+ |
| 2174 | // | ... | |
| 2175 | // +------------+ |
| 2176 | // | section | |
| 2177 | // +------------+ |
| 2178 | // | Elf_Shdr | section headers |
| 2179 | // | Elf_Shdr | |
| 2180 | // | ... | contains offset to section start |
| 2181 | // | Elf_Shdr | |
| 2182 | // +------------+ |
| 2183 | // |
| 2184 | // To strip: |
| 2185 | // - leave the Elf_Ehdr and Elf_Phdr values in place. |
| 2186 | // - walk the sections making a new set of Elf_Shdr section headers for what we want to keep |
| 2187 | // - move the sections are keeping up to fill in gaps of sections we want to strip |
| 2188 | // - write new Elf_Shdr section headers to end of file, updating Elf_Ehdr |
| 2189 | // - truncate rest of file |
| 2190 | // |
| 2191 | |
| 2192 | std::vector<Elf_Shdr> section_headers; |
| 2193 | std::vector<Elf_Word> section_headers_original_indexes; |
| 2194 | section_headers.reserve(GetSectionHeaderNum()); |
| 2195 | |
| 2196 | |
| 2197 | Elf_Shdr* string_section = GetSectionNameStringSection(); |
| 2198 | CHECK(string_section != nullptr); |
| 2199 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 2200 | Elf_Shdr* sh = GetSectionHeader(i); |
| 2201 | CHECK(sh != nullptr); |
| 2202 | const char* name = GetString(*string_section, sh->sh_name); |
| 2203 | if (name == nullptr) { |
| 2204 | CHECK_EQ(0U, i); |
| 2205 | section_headers.push_back(*sh); |
| 2206 | section_headers_original_indexes.push_back(0); |
| 2207 | continue; |
| 2208 | } |
| 2209 | if (StartsWith(name, ".debug") |
| 2210 | || (strcmp(name, ".strtab") == 0) |
| 2211 | || (strcmp(name, ".symtab") == 0)) { |
| 2212 | continue; |
| 2213 | } |
| 2214 | section_headers.push_back(*sh); |
| 2215 | section_headers_original_indexes.push_back(i); |
| 2216 | } |
| 2217 | CHECK_NE(0U, section_headers.size()); |
| 2218 | CHECK_EQ(section_headers.size(), section_headers_original_indexes.size()); |
| 2219 | |
| 2220 | // section 0 is the NULL section, sections start at offset of first section |
| 2221 | CHECK(GetSectionHeader(1) != nullptr); |
| 2222 | Elf_Off offset = GetSectionHeader(1)->sh_offset; |
| 2223 | for (size_t i = 1; i < section_headers.size(); i++) { |
| 2224 | Elf_Shdr& new_sh = section_headers[i]; |
| 2225 | Elf_Shdr* old_sh = GetSectionHeader(section_headers_original_indexes[i]); |
| 2226 | CHECK(old_sh != nullptr); |
| 2227 | CHECK_EQ(new_sh.sh_name, old_sh->sh_name); |
| 2228 | if (old_sh->sh_addralign > 1) { |
| 2229 | offset = RoundUp(offset, old_sh->sh_addralign); |
| 2230 | } |
| 2231 | if (old_sh->sh_offset == offset) { |
| 2232 | // already in place |
| 2233 | offset += old_sh->sh_size; |
| 2234 | continue; |
| 2235 | } |
| 2236 | // shift section earlier |
| 2237 | memmove(Begin() + offset, |
| 2238 | Begin() + old_sh->sh_offset, |
| 2239 | old_sh->sh_size); |
| 2240 | new_sh.sh_offset = offset; |
| 2241 | offset += old_sh->sh_size; |
| 2242 | } |
| 2243 | |
| 2244 | Elf_Off shoff = offset; |
| 2245 | size_t section_headers_size_in_bytes = section_headers.size() * sizeof(Elf_Shdr); |
| 2246 | memcpy(Begin() + offset, §ion_headers[0], section_headers_size_in_bytes); |
| 2247 | offset += section_headers_size_in_bytes; |
| 2248 | |
| 2249 | GetHeader().e_shnum = section_headers.size(); |
| 2250 | GetHeader().e_shoff = shoff; |
| 2251 | int result = ftruncate(file_->Fd(), offset); |
| 2252 | if (result != 0) { |
| 2253 | *error_msg = StringPrintf("Failed to truncate while stripping ELF file: '%s': %s", |
| 2254 | file_->GetPath().c_str(), strerror(errno)); |
| 2255 | return false; |
| 2256 | } |
| 2257 | return true; |
| 2258 | } |
| 2259 | |
| 2260 | static const bool DEBUG_FIXUP = false; |
| 2261 | |
| 2262 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2263 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2264 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2265 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2266 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2267 | ::Fixup(uintptr_t base_address) { |
| 2268 | if (!FixupDynamic(base_address)) { |
| 2269 | LOG(WARNING) << "Failed to fixup .dynamic in " << file_->GetPath(); |
| 2270 | return false; |
| 2271 | } |
| 2272 | if (!FixupSectionHeaders(base_address)) { |
| 2273 | LOG(WARNING) << "Failed to fixup section headers in " << file_->GetPath(); |
| 2274 | return false; |
| 2275 | } |
| 2276 | if (!FixupProgramHeaders(base_address)) { |
| 2277 | LOG(WARNING) << "Failed to fixup program headers in " << file_->GetPath(); |
| 2278 | return false; |
| 2279 | } |
| 2280 | if (!FixupSymbols(base_address, true)) { |
| 2281 | LOG(WARNING) << "Failed to fixup .dynsym in " << file_->GetPath(); |
| 2282 | return false; |
| 2283 | } |
| 2284 | if (!FixupSymbols(base_address, false)) { |
| 2285 | LOG(WARNING) << "Failed to fixup .symtab in " << file_->GetPath(); |
| 2286 | return false; |
| 2287 | } |
| 2288 | if (!FixupRelocations(base_address)) { |
| 2289 | LOG(WARNING) << "Failed to fixup .rel.dyn in " << file_->GetPath(); |
| 2290 | return false; |
| 2291 | } |
| 2292 | if (!FixupDebugSections(base_address)) { |
| 2293 | LOG(WARNING) << "Failed to fixup debug sections in " << file_->GetPath(); |
| 2294 | return false; |
| 2295 | } |
| 2296 | return true; |
| 2297 | } |
| 2298 | |
| 2299 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2300 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2301 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2302 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2303 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2304 | ::FixupDynamic(uintptr_t base_address) { |
| 2305 | for (Elf_Word i = 0; i < GetDynamicNum(); i++) { |
| 2306 | Elf_Dyn& elf_dyn = GetDynamic(i); |
| 2307 | Elf_Word d_tag = elf_dyn.d_tag; |
| 2308 | if (IsDynamicSectionPointer(d_tag, GetHeader().e_machine)) { |
| 2309 | Elf_Addr d_ptr = elf_dyn.d_un.d_ptr; |
| 2310 | if (DEBUG_FIXUP) { |
| 2311 | LOG(INFO) << StringPrintf("In %s moving Elf_Dyn[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 2312 | GetFile().GetPath().c_str(), i, |
| 2313 | static_cast<uint64_t>(d_ptr), |
| 2314 | static_cast<uint64_t>(d_ptr + base_address)); |
| 2315 | } |
| 2316 | d_ptr += base_address; |
| 2317 | elf_dyn.d_un.d_ptr = d_ptr; |
| 2318 | } |
| 2319 | } |
| 2320 | return true; |
| 2321 | } |
| 2322 | |
| 2323 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2324 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2325 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2326 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2327 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2328 | ::FixupSectionHeaders(uintptr_t base_address) { |
| 2329 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 2330 | Elf_Shdr* sh = GetSectionHeader(i); |
| 2331 | CHECK(sh != nullptr); |
| 2332 | // 0 implies that the section will not exist in the memory of the process |
| 2333 | if (sh->sh_addr == 0) { |
| 2334 | continue; |
| 2335 | } |
| 2336 | if (DEBUG_FIXUP) { |
| 2337 | LOG(INFO) << StringPrintf("In %s moving Elf_Shdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 2338 | GetFile().GetPath().c_str(), i, |
| 2339 | static_cast<uint64_t>(sh->sh_addr), |
| 2340 | static_cast<uint64_t>(sh->sh_addr + base_address)); |
| 2341 | } |
| 2342 | sh->sh_addr += base_address; |
| 2343 | } |
| 2344 | return true; |
| 2345 | } |
| 2346 | |
| 2347 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2348 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2349 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2350 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2351 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2352 | ::FixupProgramHeaders(uintptr_t base_address) { |
| 2353 | // TODO: ELFObjectFile doesn't have give to Elf_Phdr, so we do that ourselves for now. |
| 2354 | for (Elf_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 2355 | Elf_Phdr* ph = GetProgramHeader(i); |
| 2356 | CHECK(ph != nullptr); |
| 2357 | CHECK_EQ(ph->p_vaddr, ph->p_paddr) << GetFile().GetPath() << " i=" << i; |
| 2358 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 2359 | << GetFile().GetPath() << " i=" << i; |
| 2360 | if (DEBUG_FIXUP) { |
| 2361 | LOG(INFO) << StringPrintf("In %s moving Elf_Phdr[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 2362 | GetFile().GetPath().c_str(), i, |
| 2363 | static_cast<uint64_t>(ph->p_vaddr), |
| 2364 | static_cast<uint64_t>(ph->p_vaddr + base_address)); |
| 2365 | } |
| 2366 | ph->p_vaddr += base_address; |
| 2367 | ph->p_paddr += base_address; |
| 2368 | CHECK((ph->p_align == 0) || (0 == ((ph->p_vaddr - ph->p_offset) & (ph->p_align - 1)))) |
| 2369 | << GetFile().GetPath() << " i=" << i; |
| 2370 | } |
| 2371 | return true; |
| 2372 | } |
| 2373 | |
| 2374 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2375 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2376 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2377 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2378 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2379 | ::FixupSymbols(uintptr_t base_address, bool dynamic) { |
| 2380 | Elf_Word section_type = dynamic ? SHT_DYNSYM : SHT_SYMTAB; |
| 2381 | // TODO: Unfortunate ELFObjectFile has protected symbol access, so use ElfFile |
| 2382 | Elf_Shdr* symbol_section = FindSectionByType(section_type); |
| 2383 | if (symbol_section == nullptr) { |
| 2384 | // file is missing optional .symtab |
| 2385 | CHECK(!dynamic) << GetFile().GetPath(); |
| 2386 | return true; |
| 2387 | } |
| 2388 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
| 2389 | Elf_Sym* symbol = GetSymbol(section_type, i); |
| 2390 | CHECK(symbol != nullptr); |
| 2391 | if (symbol->st_value != 0) { |
| 2392 | if (DEBUG_FIXUP) { |
| 2393 | LOG(INFO) << StringPrintf("In %s moving Elf_Sym[%d] from 0x%" PRIx64 " to 0x%" PRIx64, |
| 2394 | GetFile().GetPath().c_str(), i, |
| 2395 | static_cast<uint64_t>(symbol->st_value), |
| 2396 | static_cast<uint64_t>(symbol->st_value + base_address)); |
| 2397 | } |
| 2398 | symbol->st_value += base_address; |
| 2399 | } |
| 2400 | } |
| 2401 | return true; |
| 2402 | } |
| 2403 | |
| 2404 | template <typename Elf_Ehdr, typename Elf_Phdr, typename Elf_Shdr, typename Elf_Word, |
| 2405 | typename Elf_Sword, typename Elf_Addr, typename Elf_Sym, typename Elf_Rel, |
| 2406 | typename Elf_Rela, typename Elf_Dyn, typename Elf_Off> |
| 2407 | bool ElfFileImpl<Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Word, |
| 2408 | Elf_Sword, Elf_Addr, Elf_Sym, Elf_Rel, Elf_Rela, Elf_Dyn, Elf_Off> |
| 2409 | ::FixupRelocations(uintptr_t base_address) { |
| 2410 | for (Elf_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 2411 | Elf_Shdr* sh = GetSectionHeader(i); |
| 2412 | CHECK(sh != nullptr); |
| 2413 | if (sh->sh_type == SHT_REL) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2414 | for (uint32_t j = 0; j < GetRelNum(*sh); j++) { |
| 2415 | Elf_Rel& rel = GetRel(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2416 | if (DEBUG_FIXUP) { |
| 2417 | 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] | 2418 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2419 | static_cast<uint64_t>(rel.r_offset), |
| 2420 | static_cast<uint64_t>(rel.r_offset + base_address)); |
| 2421 | } |
| 2422 | rel.r_offset += base_address; |
| 2423 | } |
| 2424 | } else if (sh->sh_type == SHT_RELA) { |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 2425 | for (uint32_t j = 0; j < GetRelaNum(*sh); j++) { |
| 2426 | Elf_Rela& rela = GetRela(*sh, j); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2427 | if (DEBUG_FIXUP) { |
| 2428 | 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] | 2429 | GetFile().GetPath().c_str(), j, |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2430 | static_cast<uint64_t>(rela.r_offset), |
| 2431 | static_cast<uint64_t>(rela.r_offset + base_address)); |
| 2432 | } |
| 2433 | rela.r_offset += base_address; |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | return true; |
| 2438 | } |
| 2439 | |
| 2440 | // Explicit instantiations |
| 2441 | template class ElfFileImpl<Elf32_Ehdr, Elf32_Phdr, Elf32_Shdr, Elf32_Word, |
| 2442 | Elf32_Sword, Elf32_Addr, Elf32_Sym, Elf32_Rel, Elf32_Rela, Elf32_Dyn, Elf32_Off>; |
| 2443 | template class ElfFileImpl<Elf64_Ehdr, Elf64_Phdr, Elf64_Shdr, Elf64_Word, |
| 2444 | Elf64_Sword, Elf64_Addr, Elf64_Sym, Elf64_Rel, Elf64_Rela, Elf64_Dyn, Elf64_Off>; |
| 2445 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2446 | ElfFile::ElfFile(ElfFileImpl32* elf32) : elf32_(elf32), elf64_(nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2447 | } |
| 2448 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2449 | ElfFile::ElfFile(ElfFileImpl64* elf64) : elf32_(nullptr), elf64_(elf64) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | ElfFile::~ElfFile() { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2453 | // Should never have 32 and 64-bit impls. |
| 2454 | CHECK_NE(elf32_.get() == nullptr, elf64_.get() == nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2455 | } |
| 2456 | |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 2457 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, std::string* error_msg, |
| 2458 | uint8_t* requested_base) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2459 | if (file->GetLength() < EI_NIDENT) { |
| 2460 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 2461 | file->GetPath().c_str()); |
| 2462 | return nullptr; |
| 2463 | } |
| 2464 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0, |
| 2465 | file->GetPath().c_str(), error_msg)); |
| 2466 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 2467 | return nullptr; |
| 2468 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2469 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2470 | if (header[EI_CLASS] == ELFCLASS64) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 2471 | ElfFileImpl64* elf_file_impl = ElfFileImpl64::Open(file, writable, program_header_only, |
| 2472 | error_msg, requested_base); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2473 | if (elf_file_impl == nullptr) |
| 2474 | return nullptr; |
| 2475 | return new ElfFile(elf_file_impl); |
| 2476 | } else if (header[EI_CLASS] == ELFCLASS32) { |
Igor Murashkin | 4677476 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 2477 | ElfFileImpl32* elf_file_impl = ElfFileImpl32::Open(file, writable, program_header_only, |
| 2478 | error_msg, requested_base); |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2479 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2480 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2481 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2482 | return new ElfFile(elf_file_impl); |
| 2483 | } else { |
| 2484 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 2485 | ELFCLASS32, ELFCLASS64, |
| 2486 | file->GetPath().c_str(), |
| 2487 | header[EI_CLASS]); |
| 2488 | return nullptr; |
| 2489 | } |
| 2490 | } |
| 2491 | |
| 2492 | ElfFile* ElfFile::Open(File* file, int mmap_prot, int mmap_flags, std::string* error_msg) { |
| 2493 | if (file->GetLength() < EI_NIDENT) { |
| 2494 | *error_msg = StringPrintf("File %s is too short to be a valid ELF file", |
| 2495 | file->GetPath().c_str()); |
| 2496 | return nullptr; |
| 2497 | } |
| 2498 | std::unique_ptr<MemMap> map(MemMap::MapFile(EI_NIDENT, PROT_READ, MAP_PRIVATE, file->Fd(), 0, |
| 2499 | file->GetPath().c_str(), error_msg)); |
| 2500 | if (map == nullptr && map->Size() != EI_NIDENT) { |
| 2501 | return nullptr; |
| 2502 | } |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2503 | uint8_t* header = map->Begin(); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2504 | if (header[EI_CLASS] == ELFCLASS64) { |
| 2505 | 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] | 2506 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2507 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2508 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2509 | return new ElfFile(elf_file_impl); |
| 2510 | } else if (header[EI_CLASS] == ELFCLASS32) { |
| 2511 | 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] | 2512 | if (elf_file_impl == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2513 | return nullptr; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2514 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2515 | return new ElfFile(elf_file_impl); |
| 2516 | } else { |
| 2517 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d or %d in %s, found %d", |
| 2518 | ELFCLASS32, ELFCLASS64, |
| 2519 | file->GetPath().c_str(), |
| 2520 | header[EI_CLASS]); |
| 2521 | return nullptr; |
| 2522 | } |
| 2523 | } |
| 2524 | |
| 2525 | #define DELEGATE_TO_IMPL(func, ...) \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2526 | if (elf64_.get() != nullptr) { \ |
| 2527 | return elf64_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2528 | } else { \ |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2529 | DCHECK(elf32_.get() != nullptr); \ |
| 2530 | return elf32_->func(__VA_ARGS__); \ |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2531 | } |
| 2532 | |
| 2533 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
| 2534 | DELEGATE_TO_IMPL(Load, executable, error_msg); |
| 2535 | } |
| 2536 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2537 | const uint8_t* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2538 | DELEGATE_TO_IMPL(FindDynamicSymbolAddress, symbol_name); |
| 2539 | } |
| 2540 | |
| 2541 | size_t ElfFile::Size() const { |
| 2542 | DELEGATE_TO_IMPL(Size); |
| 2543 | } |
| 2544 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2545 | uint8_t* ElfFile::Begin() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2546 | DELEGATE_TO_IMPL(Begin); |
| 2547 | } |
| 2548 | |
Ian Rogers | 1373595 | 2014-10-08 12:43:28 -0700 | [diff] [blame] | 2549 | uint8_t* ElfFile::End() const { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2550 | DELEGATE_TO_IMPL(End); |
| 2551 | } |
| 2552 | |
| 2553 | const File& ElfFile::GetFile() const { |
| 2554 | DELEGATE_TO_IMPL(GetFile); |
| 2555 | } |
| 2556 | |
| 2557 | bool ElfFile::GetSectionOffsetAndSize(const char* section_name, uint64_t* offset, uint64_t* size) { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2558 | if (elf32_.get() == nullptr) { |
| 2559 | CHECK(elf64_.get() != nullptr); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2560 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2561 | Elf64_Shdr *shdr = elf64_->FindSectionByName(section_name); |
| 2562 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2563 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2564 | } |
| 2565 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2566 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2567 | } |
| 2568 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2569 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2570 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2571 | return true; |
| 2572 | } else { |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2573 | Elf32_Shdr *shdr = elf32_->FindSectionByName(section_name); |
| 2574 | if (shdr == nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2575 | return false; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2576 | } |
| 2577 | if (offset != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2578 | *offset = shdr->sh_offset; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2579 | } |
| 2580 | if (size != nullptr) { |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2581 | *size = shdr->sh_size; |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2582 | } |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2583 | return true; |
| 2584 | } |
| 2585 | } |
| 2586 | |
| 2587 | uint64_t ElfFile::FindSymbolAddress(unsigned section_type, |
| 2588 | const std::string& symbol_name, |
| 2589 | bool build_map) { |
| 2590 | DELEGATE_TO_IMPL(FindSymbolAddress, section_type, symbol_name, build_map); |
| 2591 | } |
| 2592 | |
| 2593 | size_t ElfFile::GetLoadedSize() const { |
| 2594 | DELEGATE_TO_IMPL(GetLoadedSize); |
| 2595 | } |
| 2596 | |
| 2597 | bool ElfFile::Strip(File* file, std::string* error_msg) { |
| 2598 | std::unique_ptr<ElfFile> elf_file(ElfFile::Open(file, true, false, error_msg)); |
| 2599 | if (elf_file.get() == nullptr) { |
| 2600 | return false; |
| 2601 | } |
| 2602 | |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2603 | if (elf_file->elf64_.get() != nullptr) |
| 2604 | return elf_file->elf64_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2605 | else |
Ian Rogers | d4c4d95 | 2014-10-16 20:31:53 -0700 | [diff] [blame] | 2606 | return elf_file->elf32_->Strip(error_msg); |
Tong Shen | 62d1ca3 | 2014-09-03 17:24:56 -0700 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | bool ElfFile::Fixup(uintptr_t base_address) { |
| 2610 | DELEGATE_TO_IMPL(Fixup, base_address); |
| 2611 | } |
| 2612 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 2613 | } // namespace art |