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 | |
Nicolas Geoffray | a7f198c | 2014-03-10 11:12:54 +0000 | [diff] [blame] | 19 | #include <sys/types.h> |
| 20 | #include <unistd.h> |
| 21 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 22 | #include "base/logging.h" |
Ian Rogers | 576ca0c | 2014-06-06 15:58:22 -0700 | [diff] [blame] | 23 | #include "base/stringprintf.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 24 | #include "base/stl_util.h" |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 25 | #include "dwarf.h" |
| 26 | #include "leb128.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 27 | #include "utils.h" |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 28 | #include "instruction_set.h" |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 32 | // ------------------------------------------------------------------- |
| 33 | // Binary GDB JIT Interface as described in |
| 34 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 35 | extern "C" { |
| 36 | typedef enum { |
| 37 | JIT_NOACTION = 0, |
| 38 | JIT_REGISTER_FN, |
| 39 | JIT_UNREGISTER_FN |
| 40 | } JITAction; |
| 41 | |
| 42 | struct JITCodeEntry { |
| 43 | JITCodeEntry* next_; |
| 44 | JITCodeEntry* prev_; |
| 45 | const byte *symfile_addr_; |
| 46 | uint64_t symfile_size_; |
| 47 | }; |
| 48 | |
| 49 | struct JITDescriptor { |
| 50 | uint32_t version_; |
| 51 | uint32_t action_flag_; |
| 52 | JITCodeEntry* relevant_entry_; |
| 53 | JITCodeEntry* first_entry_; |
| 54 | }; |
| 55 | |
| 56 | // GDB will place breakpoint into this function. |
| 57 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 58 | // and inline assembler statement inside. |
| 59 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 60 | __asm__(""); |
| 61 | } |
| 62 | |
| 63 | // GDB will inspect contents of this descriptor. |
| 64 | // Static initialization is necessary to prevent GDB from seeing |
| 65 | // uninitialized descriptor. |
| 66 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static JITCodeEntry* CreateCodeEntry(const byte *symfile_addr, |
| 71 | uintptr_t symfile_size) { |
| 72 | JITCodeEntry* entry = new JITCodeEntry; |
| 73 | entry->symfile_addr_ = symfile_addr; |
| 74 | entry->symfile_size_ = symfile_size; |
| 75 | entry->prev_ = nullptr; |
| 76 | |
| 77 | // TODO: Do we need a lock here? |
| 78 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 79 | if (entry->next_ != nullptr) { |
| 80 | entry->next_->prev_ = entry; |
| 81 | } |
| 82 | __jit_debug_descriptor.first_entry_ = entry; |
| 83 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 84 | |
| 85 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
| 86 | __jit_debug_register_code(); |
| 87 | return entry; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static void UnregisterCodeEntry(JITCodeEntry* entry) { |
| 92 | // TODO: Do we need a lock here? |
| 93 | if (entry->prev_ != nullptr) { |
| 94 | entry->prev_->next_ = entry->next_; |
| 95 | } else { |
| 96 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 97 | } |
| 98 | |
| 99 | if (entry->next_ != nullptr) { |
| 100 | entry->next_->prev_ = entry->prev_; |
| 101 | } |
| 102 | |
| 103 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 104 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 105 | __jit_debug_register_code(); |
| 106 | delete entry; |
| 107 | } |
| 108 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 109 | ElfFile::ElfFile(File* file, bool writable, bool program_header_only) |
| 110 | : file_(file), |
| 111 | writable_(writable), |
| 112 | program_header_only_(program_header_only), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 113 | header_(nullptr), |
| 114 | base_address_(nullptr), |
| 115 | program_headers_start_(nullptr), |
| 116 | section_headers_start_(nullptr), |
| 117 | dynamic_program_header_(nullptr), |
| 118 | dynamic_section_start_(nullptr), |
| 119 | symtab_section_start_(nullptr), |
| 120 | dynsym_section_start_(nullptr), |
| 121 | strtab_section_start_(nullptr), |
| 122 | dynstr_section_start_(nullptr), |
| 123 | hash_section_start_(nullptr), |
| 124 | symtab_symbol_table_(nullptr), |
| 125 | dynsym_symbol_table_(nullptr), |
| 126 | jit_elf_image_(nullptr), |
| 127 | jit_gdb_entry_(nullptr) { |
| 128 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 129 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 130 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 131 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, |
| 132 | std::string* error_msg) { |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 133 | std::unique_ptr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 134 | int prot; |
| 135 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 136 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 137 | prot = PROT_READ | PROT_WRITE; |
| 138 | flags = MAP_SHARED; |
| 139 | } else { |
| 140 | prot = PROT_READ; |
| 141 | flags = MAP_PRIVATE; |
| 142 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 143 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 144 | return nullptr; |
| 145 | } |
| 146 | return elf_file.release(); |
| 147 | } |
| 148 | |
| 149 | ElfFile* ElfFile::Open(File* file, int prot, int flags, std::string* error_msg) { |
| 150 | std::unique_ptr<ElfFile> elf_file(new ElfFile(file, (prot & PROT_WRITE) == PROT_WRITE, false)); |
| 151 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 152 | return nullptr; |
| 153 | } |
| 154 | return elf_file.release(); |
| 155 | } |
| 156 | |
| 157 | bool ElfFile::Setup(int prot, int flags, std::string* error_msg) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 158 | int64_t temp_file_length = file_->GetLength(); |
| 159 | if (temp_file_length < 0) { |
| 160 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 161 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 162 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 163 | return false; |
| 164 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 165 | size_t file_length = static_cast<size_t>(temp_file_length); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 166 | if (file_length < sizeof(Elf32_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 167 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF header of " |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 168 | "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 169 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 170 | return false; |
| 171 | } |
| 172 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 173 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 174 | // first just map ELF header to get program header size information |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 175 | size_t elf_header_size = sizeof(Elf32_Ehdr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 176 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 177 | file_->GetPath().c_str(), error_msg), |
| 178 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 179 | return false; |
| 180 | } |
| 181 | // then remap to cover program header |
| 182 | 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] | 183 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 184 | *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] | 185 | "header of %zd bytes: '%s'", file_length, |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 186 | sizeof(Elf32_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 187 | return false; |
| 188 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 189 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 190 | file_->GetPath().c_str(), error_msg), |
| 191 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 192 | *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] | 193 | return false; |
| 194 | } |
| 195 | } else { |
| 196 | // otherwise map entire file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 197 | if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 198 | file_->GetPath().c_str(), error_msg), |
| 199 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | *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] | 201 | return false; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // Either way, the program header is relative to the elf header |
| 206 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 207 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 208 | if (!program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 209 | // Setup section headers. |
| 210 | section_headers_start_ = Begin() + GetHeader().e_shoff; |
| 211 | |
| 212 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 213 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 214 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 215 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 216 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 217 | return false; |
| 218 | } |
| 219 | |
| 220 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 221 | = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 222 | |
| 223 | // Find other sections from section headers |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 224 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 225 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 226 | byte* section_addr = Begin() + section_header.sh_offset; |
| 227 | switch (section_header.sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 228 | case SHT_SYMTAB: { |
| 229 | symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 230 | break; |
| 231 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 232 | case SHT_DYNSYM: { |
| 233 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 234 | break; |
| 235 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 236 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 237 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 238 | if ((section_header.sh_flags & SHF_ALLOC) != 0) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 239 | dynstr_section_start_ = reinterpret_cast<char*>(section_addr); |
| 240 | } else { |
| 241 | strtab_section_start_ = reinterpret_cast<char*>(section_addr); |
| 242 | } |
| 243 | break; |
| 244 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 245 | case SHT_DYNAMIC: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 246 | if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) { |
| 247 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 248 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 249 | << reinterpret_cast<void*>(dynamic_section_start_) |
| 250 | << " != " << reinterpret_cast<void*>(section_addr); |
| 251 | return false; |
| 252 | } |
| 253 | break; |
| 254 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 255 | case SHT_HASH: { |
| 256 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 257 | break; |
| 258 | } |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | return true; |
| 263 | } |
| 264 | |
| 265 | ElfFile::~ElfFile() { |
| 266 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 267 | delete symtab_symbol_table_; |
| 268 | delete dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 269 | delete jit_elf_image_; |
| 270 | if (jit_gdb_entry_) { |
| 271 | UnregisterCodeEntry(jit_gdb_entry_); |
| 272 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 275 | bool ElfFile::SetMap(MemMap* map, std::string* error_msg) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 276 | if (map == nullptr) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 277 | // MemMap::Open should have already set an error. |
| 278 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 279 | return false; |
| 280 | } |
| 281 | map_.reset(map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 282 | CHECK(map_.get() != nullptr) << file_->GetPath(); |
| 283 | CHECK(map_->Begin() != nullptr) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 284 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 285 | header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin()); |
| 286 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 287 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 288 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 289 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 290 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 291 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 292 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 293 | header_->e_ident[EI_MAG0], |
| 294 | header_->e_ident[EI_MAG1], |
| 295 | header_->e_ident[EI_MAG2], |
| 296 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 297 | return false; |
| 298 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 299 | if (ELFCLASS32 != header_->e_ident[EI_CLASS]) { |
| 300 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
| 301 | ELFCLASS32, |
| 302 | file_->GetPath().c_str(), |
| 303 | header_->e_ident[EI_CLASS]); |
| 304 | return false; |
| 305 | } |
| 306 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 307 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 308 | ELFDATA2LSB, |
| 309 | file_->GetPath().c_str(), |
| 310 | header_->e_ident[EI_CLASS]); |
| 311 | return false; |
| 312 | } |
| 313 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 314 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 315 | EV_CURRENT, |
| 316 | file_->GetPath().c_str(), |
| 317 | header_->e_ident[EI_CLASS]); |
| 318 | return false; |
| 319 | } |
| 320 | if (ET_DYN != header_->e_type) { |
| 321 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 322 | ET_DYN, |
| 323 | file_->GetPath().c_str(), |
| 324 | header_->e_type); |
| 325 | return false; |
| 326 | } |
| 327 | if (EV_CURRENT != header_->e_version) { |
| 328 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 329 | EV_CURRENT, |
| 330 | file_->GetPath().c_str(), |
| 331 | header_->e_version); |
| 332 | return false; |
| 333 | } |
| 334 | if (0 != header_->e_entry) { |
| 335 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 336 | 0, |
| 337 | file_->GetPath().c_str(), |
| 338 | header_->e_entry); |
| 339 | return false; |
| 340 | } |
| 341 | if (0 == header_->e_phoff) { |
| 342 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 343 | file_->GetPath().c_str()); |
| 344 | return false; |
| 345 | } |
| 346 | if (0 == header_->e_shoff) { |
| 347 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 348 | file_->GetPath().c_str()); |
| 349 | return false; |
| 350 | } |
| 351 | if (0 == header_->e_ehsize) { |
| 352 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 353 | file_->GetPath().c_str()); |
| 354 | return false; |
| 355 | } |
| 356 | if (0 == header_->e_phentsize) { |
| 357 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 358 | file_->GetPath().c_str()); |
| 359 | return false; |
| 360 | } |
| 361 | if (0 == header_->e_phnum) { |
| 362 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 363 | file_->GetPath().c_str()); |
| 364 | return false; |
| 365 | } |
| 366 | if (0 == header_->e_shentsize) { |
| 367 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 368 | file_->GetPath().c_str()); |
| 369 | return false; |
| 370 | } |
| 371 | if (0 == header_->e_shnum) { |
| 372 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 373 | file_->GetPath().c_str()); |
| 374 | return false; |
| 375 | } |
| 376 | if (0 == header_->e_shstrndx) { |
| 377 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 378 | file_->GetPath().c_str()); |
| 379 | return false; |
| 380 | } |
| 381 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 382 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 383 | header_->e_shstrndx, |
| 384 | header_->e_shnum, |
| 385 | file_->GetPath().c_str()); |
| 386 | return false; |
| 387 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 388 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 389 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 390 | if (header_->e_phoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 391 | *error_msg = StringPrintf("Failed to find e_phoff value %d less than %zd in %s", |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 392 | header_->e_phoff, |
| 393 | Size(), |
| 394 | file_->GetPath().c_str()); |
| 395 | return false; |
| 396 | } |
| 397 | if (header_->e_shoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 398 | *error_msg = StringPrintf("Failed to find e_shoff value %d less than %zd in %s", |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 399 | header_->e_shoff, |
| 400 | Size(), |
| 401 | file_->GetPath().c_str()); |
| 402 | return false; |
| 403 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 404 | } |
| 405 | return true; |
| 406 | } |
| 407 | |
| 408 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 409 | Elf32_Ehdr& ElfFile::GetHeader() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 410 | CHECK(header_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 411 | return *header_; |
| 412 | } |
| 413 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 414 | byte* ElfFile::GetProgramHeadersStart() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 415 | CHECK(program_headers_start_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 416 | return program_headers_start_; |
| 417 | } |
| 418 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 419 | byte* ElfFile::GetSectionHeadersStart() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 420 | CHECK(section_headers_start_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 421 | return section_headers_start_; |
| 422 | } |
| 423 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 424 | Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 425 | CHECK(dynamic_program_header_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 426 | return *dynamic_program_header_; |
| 427 | } |
| 428 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 429 | Elf32_Dyn* ElfFile::GetDynamicSectionStart() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 430 | CHECK(dynamic_section_start_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 431 | return dynamic_section_start_; |
| 432 | } |
| 433 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 434 | Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 435 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 436 | Elf32_Sym* symbol_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 437 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 438 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 439 | symbol_section_start = symtab_section_start_; |
| 440 | break; |
| 441 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 442 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 443 | symbol_section_start = dynsym_section_start_; |
| 444 | break; |
| 445 | } |
| 446 | default: { |
| 447 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 448 | symbol_section_start = nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 449 | } |
| 450 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 451 | CHECK(symbol_section_start != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 452 | return symbol_section_start; |
| 453 | } |
| 454 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 455 | const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 456 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 457 | const char* string_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 458 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 459 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 460 | string_section_start = strtab_section_start_; |
| 461 | break; |
| 462 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 463 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 464 | string_section_start = dynstr_section_start_; |
| 465 | break; |
| 466 | } |
| 467 | default: { |
| 468 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 469 | string_section_start = nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 470 | } |
| 471 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 472 | CHECK(string_section_start != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 473 | return string_section_start; |
| 474 | } |
| 475 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 476 | const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 477 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 478 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 479 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 480 | } |
| 481 | const char* string_section_start = GetStringSectionStart(section_type); |
| 482 | const char* string = string_section_start + i; |
| 483 | return string; |
| 484 | } |
| 485 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 486 | Elf32_Word* ElfFile::GetHashSectionStart() const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 487 | CHECK(hash_section_start_ != nullptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 488 | return hash_section_start_; |
| 489 | } |
| 490 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 491 | Elf32_Word ElfFile::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 492 | return GetHashSectionStart()[0]; |
| 493 | } |
| 494 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 495 | Elf32_Word ElfFile::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 496 | return GetHashSectionStart()[1]; |
| 497 | } |
| 498 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 499 | Elf32_Word ElfFile::GetHashBucket(size_t i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 500 | CHECK_LT(i, GetHashBucketNum()); |
| 501 | // 0 is nbucket, 1 is nchain |
| 502 | return GetHashSectionStart()[2 + i]; |
| 503 | } |
| 504 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 505 | Elf32_Word ElfFile::GetHashChain(size_t i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 506 | CHECK_LT(i, GetHashChainNum()); |
| 507 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 508 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 509 | } |
| 510 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 511 | Elf32_Word ElfFile::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 512 | return GetHeader().e_phnum; |
| 513 | } |
| 514 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 515 | Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 516 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); |
| 517 | byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
| 518 | CHECK_LT(program_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 519 | return *reinterpret_cast<Elf32_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 520 | } |
| 521 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 522 | Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 523 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 524 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 525 | if (program_header.p_type == type) { |
| 526 | return &program_header; |
| 527 | } |
| 528 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 529 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 530 | } |
| 531 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 532 | Elf32_Word ElfFile::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 533 | return GetHeader().e_shnum; |
| 534 | } |
| 535 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 536 | Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 537 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 538 | // Even if we Load(), it doesn't bring in all the sections. |
| 539 | CHECK(!program_header_only_) << file_->GetPath(); |
| 540 | CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath(); |
| 541 | byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
| 542 | CHECK_LT(section_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 543 | return *reinterpret_cast<Elf32_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 544 | } |
| 545 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 546 | Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 547 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 548 | // We could change this to switch on known types if they were detected during loading. |
| 549 | CHECK(!program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 550 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 551 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 552 | if (section_header.sh_type == type) { |
| 553 | return §ion_header; |
| 554 | } |
| 555 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 556 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 560 | static unsigned elfhash(const char *_name) { |
| 561 | const unsigned char *name = (const unsigned char *) _name; |
| 562 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 563 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 564 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 565 | h = (h << 4) + *name++; |
| 566 | g = h & 0xf0000000; |
| 567 | h ^= g; |
| 568 | h ^= g >> 24; |
| 569 | } |
| 570 | return h; |
| 571 | } |
| 572 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 573 | Elf32_Shdr& ElfFile::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 574 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 575 | } |
| 576 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 577 | const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 578 | const Elf32_Sym* sym = FindDynamicSymbol(symbol_name); |
| 579 | if (sym != nullptr) { |
| 580 | return base_address_ + sym->st_value; |
| 581 | } else { |
| 582 | return nullptr; |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | const Elf32_Sym* ElfFile::FindDynamicSymbol(const std::string& symbol_name) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 587 | Elf32_Word hash = elfhash(symbol_name.c_str()); |
| 588 | Elf32_Word bucket_index = hash % GetHashBucketNum(); |
| 589 | Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 590 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 591 | Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
| 592 | const char* name = GetString(SHT_DYNSYM, symbol.st_name); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 593 | if (symbol_name == name) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 594 | return &symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 595 | } |
| 596 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index); |
| 597 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 598 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 599 | } |
| 600 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 601 | bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) { |
| 602 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 603 | } |
| 604 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 605 | Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const { |
| 606 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 607 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 608 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 609 | return section_header.sh_size / section_header.sh_entsize; |
| 610 | } |
| 611 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 612 | Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 613 | Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 614 | return *(GetSymbolSectionStart(section_type) + i); |
| 615 | } |
| 616 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 617 | ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 618 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 619 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 620 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 621 | return &symtab_symbol_table_; |
| 622 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 623 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 624 | return &dynsym_symbol_table_; |
| 625 | } |
| 626 | default: { |
| 627 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 628 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | } |
| 632 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 633 | Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type, |
| 634 | const std::string& symbol_name, |
| 635 | bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 636 | CHECK(!program_header_only_) << file_->GetPath(); |
| 637 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 638 | |
| 639 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 640 | if (*symbol_table != nullptr || build_map) { |
| 641 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 642 | DCHECK(build_map); |
| 643 | *symbol_table = new SymbolTable; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 644 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 645 | CHECK(symbol_section != nullptr) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 646 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 647 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 648 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
| 649 | unsigned char type = ELF32_ST_TYPE(symbol.st_info); |
| 650 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 651 | continue; |
| 652 | } |
| 653 | const char* name = GetString(string_section, symbol.st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 654 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 655 | continue; |
| 656 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 657 | std::pair<SymbolTable::iterator, bool> result = |
| 658 | (*symbol_table)->insert(std::make_pair(name, &symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 659 | if (!result.second) { |
| 660 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
| 661 | CHECK_EQ(symbol.st_value, result.first->second->st_value); |
| 662 | CHECK_EQ(symbol.st_size, result.first->second->st_size); |
| 663 | CHECK_EQ(symbol.st_info, result.first->second->st_info); |
| 664 | CHECK_EQ(symbol.st_other, result.first->second->st_other); |
| 665 | CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx); |
| 666 | } |
| 667 | } |
| 668 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 669 | CHECK(*symbol_table != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 670 | SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
| 671 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 672 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 673 | } |
| 674 | return it->second; |
| 675 | } |
| 676 | |
| 677 | // Fall back to linear search |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 678 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 679 | CHECK(symbol_section != nullptr) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 680 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 681 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 682 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 683 | const char* name = GetString(string_section, symbol.st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 684 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 685 | continue; |
| 686 | } |
| 687 | if (symbol_name == name) { |
| 688 | return &symbol; |
| 689 | } |
| 690 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 691 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 694 | Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 695 | const std::string& symbol_name, |
| 696 | bool build_map) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 697 | Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 698 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | return symbol->st_value; |
| 702 | } |
| 703 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 704 | const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 705 | CHECK(!program_header_only_) << file_->GetPath(); |
| 706 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 707 | CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 708 | CHECK_LT(i, string_section.sh_size) << file_->GetPath(); |
| 709 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 710 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 711 | } |
| 712 | byte* strings = Begin() + string_section.sh_offset; |
| 713 | byte* string = strings + i; |
| 714 | CHECK_LT(string, End()) << file_->GetPath(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 715 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 718 | Elf32_Word ElfFile::GetDynamicNum() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 719 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 720 | } |
| 721 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 722 | Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 723 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 724 | return *(GetDynamicSectionStart() + i); |
| 725 | } |
| 726 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 727 | Elf32_Dyn* ElfFile::FindDynamicByType(Elf32_Sword type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 728 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 729 | Elf32_Dyn* dyn = &GetDynamic(i); |
| 730 | if (dyn->d_tag == type) { |
| 731 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 732 | } |
| 733 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 734 | return NULL; |
| 735 | } |
| 736 | |
| 737 | Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const { |
| 738 | Elf32_Dyn* dyn = FindDynamicByType(type); |
| 739 | if (dyn == NULL) { |
| 740 | return 0; |
| 741 | } else { |
| 742 | return dyn->d_un.d_val; |
| 743 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 744 | } |
| 745 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 746 | Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 747 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 748 | return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 749 | } |
| 750 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 751 | Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 752 | 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] | 753 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 754 | return section_header.sh_size / section_header.sh_entsize; |
| 755 | } |
| 756 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 757 | Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 758 | 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] | 759 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 760 | return *(GetRelSectionStart(section_header) + i); |
| 761 | } |
| 762 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 763 | Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 764 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 765 | return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 766 | } |
| 767 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 768 | Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 769 | 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] | 770 | return section_header.sh_size / section_header.sh_entsize; |
| 771 | } |
| 772 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 773 | Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 774 | 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] | 775 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 776 | return *(GetRelaSectionStart(section_header) + i); |
| 777 | } |
| 778 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 779 | // Base on bionic phdr_table_get_load_size |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 780 | size_t ElfFile::GetLoadedSize() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 781 | Elf32_Addr min_vaddr = 0xFFFFFFFFu; |
| 782 | Elf32_Addr max_vaddr = 0x00000000u; |
| 783 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 784 | Elf32_Phdr& program_header = GetProgramHeader(i); |
| 785 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 786 | continue; |
| 787 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 788 | Elf32_Addr begin_vaddr = program_header.p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 789 | if (begin_vaddr < min_vaddr) { |
| 790 | min_vaddr = begin_vaddr; |
| 791 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 792 | Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 793 | if (end_vaddr > max_vaddr) { |
| 794 | max_vaddr = end_vaddr; |
| 795 | } |
| 796 | } |
| 797 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 798 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 799 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 800 | size_t loaded_size = max_vaddr - min_vaddr; |
| 801 | return loaded_size; |
| 802 | } |
| 803 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 804 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 805 | CHECK(program_header_only_) << file_->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 806 | |
| 807 | if (executable) { |
| 808 | InstructionSet elf_ISA = kNone; |
| 809 | switch (GetHeader().e_machine) { |
| 810 | case EM_ARM: { |
| 811 | elf_ISA = kArm; |
| 812 | break; |
| 813 | } |
| 814 | case EM_AARCH64: { |
| 815 | elf_ISA = kArm64; |
| 816 | break; |
| 817 | } |
| 818 | case EM_386: { |
| 819 | elf_ISA = kX86; |
| 820 | break; |
| 821 | } |
| 822 | case EM_X86_64: { |
| 823 | elf_ISA = kX86_64; |
| 824 | break; |
| 825 | } |
| 826 | case EM_MIPS: { |
| 827 | elf_ISA = kMips; |
| 828 | break; |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | if (elf_ISA != kRuntimeISA) { |
| 833 | std::ostringstream oss; |
| 834 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 835 | *error_msg = oss.str(); |
| 836 | return false; |
| 837 | } |
| 838 | } |
| 839 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 840 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 841 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 842 | |
| 843 | // Record .dynamic header information for later use |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 844 | if (program_header.p_type == PT_DYNAMIC) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 845 | dynamic_program_header_ = &program_header; |
| 846 | continue; |
| 847 | } |
| 848 | |
| 849 | // Not something to load, move on. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 850 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 851 | continue; |
| 852 | } |
| 853 | |
| 854 | // Found something to load. |
| 855 | |
| 856 | // If p_vaddr is zero, it must be the first loadable segment, |
| 857 | // since they must be in order. Since it is zero, there isn't a |
| 858 | // specific address requested, so first request a contiguous chunk |
| 859 | // of required size for all segments, but with no |
| 860 | // permissions. We'll then carve that up with the proper |
| 861 | // permissions as we load the actual segments. If p_vaddr is |
| 862 | // non-zero, the segments require the specific address specified, |
| 863 | // which either was specified in the file because we already set |
| 864 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 865 | int64_t temp_file_length = file_->GetLength(); |
| 866 | if (temp_file_length < 0) { |
| 867 | errno = -temp_file_length; |
| 868 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 869 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 870 | return false; |
| 871 | } |
| 872 | size_t file_length = static_cast<size_t>(temp_file_length); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 873 | if (program_header.p_vaddr == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 874 | std::string reservation_name("ElfFile reservation for "); |
| 875 | reservation_name += file_->GetPath(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 876 | std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 877 | nullptr, GetLoadedSize(), PROT_NONE, false, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 878 | error_msg)); |
| 879 | if (reserve.get() == nullptr) { |
| 880 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 881 | reservation_name.c_str(), error_msg->c_str()); |
| 882 | return false; |
| 883 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 884 | base_address_ = reserve->Begin(); |
| 885 | segments_.push_back(reserve.release()); |
| 886 | } |
| 887 | // empty segment, nothing to map |
| 888 | if (program_header.p_memsz == 0) { |
| 889 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 890 | } |
| 891 | byte* p_vaddr = base_address_ + program_header.p_vaddr; |
| 892 | int prot = 0; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 893 | if (executable && ((program_header.p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 894 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 895 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 896 | if ((program_header.p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 897 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 898 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 899 | if ((program_header.p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 900 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 901 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 902 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 903 | if (writable_) { |
| 904 | prot |= PROT_WRITE; |
| 905 | flags |= MAP_SHARED; |
| 906 | } else { |
| 907 | flags |= MAP_PRIVATE; |
| 908 | } |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 909 | if (file_length < (program_header.p_offset + program_header.p_memsz)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 910 | *error_msg = StringPrintf("File size of %zd bytes not large enough to contain ELF segment " |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 911 | "%d of %d bytes: '%s'", file_length, i, |
| 912 | program_header.p_offset + program_header.p_memsz, |
| 913 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 914 | return false; |
| 915 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 916 | std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 917 | program_header.p_memsz, |
| 918 | prot, flags, file_->Fd(), |
| 919 | program_header.p_offset, |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 920 | true, // implies MAP_FIXED |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 921 | file_->GetPath().c_str(), |
| 922 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 923 | if (segment.get() == nullptr) { |
| 924 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 925 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 926 | return false; |
| 927 | } |
| 928 | if (segment->Begin() != p_vaddr) { |
| 929 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 930 | "instead mapped to %p", |
| 931 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 932 | return false; |
| 933 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 934 | segments_.push_back(segment.release()); |
| 935 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 936 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 937 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
| 938 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 939 | = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr); |
| 940 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 941 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 942 | byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
| 943 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 944 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 945 | if (!ValidPointer(d_ptr)) { |
| 946 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 947 | d_ptr, file_->GetPath().c_str()); |
| 948 | return false; |
| 949 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 950 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 951 | break; |
| 952 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 953 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 954 | if (!ValidPointer(d_ptr)) { |
| 955 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 956 | d_ptr, file_->GetPath().c_str()); |
| 957 | return false; |
| 958 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 959 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 960 | break; |
| 961 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 962 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 963 | if (!ValidPointer(d_ptr)) { |
| 964 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 965 | d_ptr, file_->GetPath().c_str()); |
| 966 | return false; |
| 967 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 968 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 969 | break; |
| 970 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 971 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 972 | if (GetDynamicNum() != i+1) { |
| 973 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 974 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 975 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 976 | return false; |
| 977 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 978 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 979 | } |
| 980 | } |
| 981 | } |
| 982 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 983 | // Use GDB JIT support to do stack backtrace, etc. |
| 984 | if (executable) { |
| 985 | GdbJITSupport(); |
| 986 | } |
| 987 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 988 | return true; |
| 989 | } |
| 990 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 991 | bool ElfFile::ValidPointer(const byte* start) const { |
| 992 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 993 | const MemMap* segment = segments_[i]; |
| 994 | if (segment->Begin() <= start && start < segment->End()) { |
| 995 | return true; |
| 996 | } |
| 997 | } |
| 998 | return false; |
| 999 | } |
| 1000 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1001 | |
| 1002 | Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const { |
| 1003 | CHECK(!program_header_only_); |
| 1004 | Elf32_Shdr& shstrtab_sec = GetSectionNameStringSection(); |
| 1005 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
| 1006 | Elf32_Shdr& shdr = GetSectionHeader(i); |
| 1007 | const char* sec_name = GetString(shstrtab_sec, shdr.sh_name); |
| 1008 | if (sec_name == nullptr) { |
| 1009 | continue; |
| 1010 | } |
| 1011 | if (name == sec_name) { |
| 1012 | return &shdr; |
| 1013 | } |
| 1014 | } |
| 1015 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1018 | struct PACKED(1) FDE { |
| 1019 | uint32_t raw_length_; |
| 1020 | uint32_t GetLength() { |
| 1021 | return raw_length_ + sizeof(raw_length_); |
| 1022 | } |
| 1023 | uint32_t CIE_pointer; |
| 1024 | uint32_t initial_location; |
| 1025 | uint32_t address_range; |
| 1026 | uint8_t instructions[0]; |
| 1027 | }; |
| 1028 | |
| 1029 | static FDE* NextFDE(FDE* frame) { |
| 1030 | byte* fde_bytes = reinterpret_cast<byte*>(frame); |
| 1031 | fde_bytes += frame->GetLength(); |
| 1032 | return reinterpret_cast<FDE*>(fde_bytes); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1035 | static bool IsFDE(FDE* frame) { |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1036 | return frame->CIE_pointer != 0; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | // TODO This only works for 32-bit Elf Files. |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1040 | static bool FixupEHFrame(uintptr_t text_start, byte* eh_frame, size_t eh_frame_size) { |
| 1041 | FDE* last_frame = reinterpret_cast<FDE*>(eh_frame + eh_frame_size); |
| 1042 | FDE* frame = NextFDE(reinterpret_cast<FDE*>(eh_frame)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1043 | for (; frame < last_frame; frame = NextFDE(frame)) { |
| 1044 | if (!IsFDE(frame)) { |
| 1045 | return false; |
| 1046 | } |
| 1047 | frame->initial_location += text_start; |
| 1048 | } |
| 1049 | return true; |
| 1050 | } |
| 1051 | |
| 1052 | struct PACKED(1) DebugInfoHeader { |
| 1053 | uint32_t unit_length; // TODO 32-bit specific size |
| 1054 | uint16_t version; |
| 1055 | uint32_t debug_abbrev_offset; // TODO 32-bit specific size |
| 1056 | uint8_t address_size; |
| 1057 | }; |
| 1058 | |
| 1059 | // Returns -1 if it is variable length, which we will just disallow for now. |
| 1060 | static int32_t FormLength(uint32_t att) { |
| 1061 | switch (att) { |
| 1062 | case DW_FORM_data1: |
| 1063 | case DW_FORM_flag: |
| 1064 | case DW_FORM_flag_present: |
| 1065 | case DW_FORM_ref1: |
| 1066 | return 1; |
| 1067 | |
| 1068 | case DW_FORM_data2: |
| 1069 | case DW_FORM_ref2: |
| 1070 | return 2; |
| 1071 | |
| 1072 | case DW_FORM_addr: // TODO 32-bit only |
| 1073 | case DW_FORM_ref_addr: // TODO 32-bit only |
| 1074 | case DW_FORM_sec_offset: // TODO 32-bit only |
| 1075 | case DW_FORM_strp: // TODO 32-bit only |
| 1076 | case DW_FORM_data4: |
| 1077 | case DW_FORM_ref4: |
| 1078 | return 4; |
| 1079 | |
| 1080 | case DW_FORM_data8: |
| 1081 | case DW_FORM_ref8: |
| 1082 | case DW_FORM_ref_sig8: |
| 1083 | return 8; |
| 1084 | |
| 1085 | case DW_FORM_block: |
| 1086 | case DW_FORM_block1: |
| 1087 | case DW_FORM_block2: |
| 1088 | case DW_FORM_block4: |
| 1089 | case DW_FORM_exprloc: |
| 1090 | case DW_FORM_indirect: |
| 1091 | case DW_FORM_ref_udata: |
| 1092 | case DW_FORM_sdata: |
| 1093 | case DW_FORM_string: |
| 1094 | case DW_FORM_udata: |
| 1095 | default: |
| 1096 | return -1; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1100 | class DebugTag { |
| 1101 | public: |
| 1102 | const uint32_t index_; |
| 1103 | ~DebugTag() {} |
| 1104 | // Creates a new tag and moves data pointer up to the start of the next one. |
| 1105 | // nullptr means error. |
| 1106 | static DebugTag* Create(const byte** data_pointer) { |
| 1107 | const byte* data = *data_pointer; |
| 1108 | uint32_t index = DecodeUnsignedLeb128(&data); |
| 1109 | std::unique_ptr<DebugTag> tag(new DebugTag(index)); |
| 1110 | tag->size_ = static_cast<uint32_t>( |
| 1111 | reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer)); |
| 1112 | // skip the abbrev |
| 1113 | tag->tag_ = DecodeUnsignedLeb128(&data); |
| 1114 | tag->has_child_ = (*data == 0); |
| 1115 | data++; |
| 1116 | while (true) { |
| 1117 | uint32_t attr = DecodeUnsignedLeb128(&data); |
| 1118 | uint32_t form = DecodeUnsignedLeb128(&data); |
| 1119 | if (attr == 0 && form == 0) { |
| 1120 | break; |
| 1121 | } else if (attr == 0 || form == 0) { |
| 1122 | // Bad abbrev. |
| 1123 | return nullptr; |
| 1124 | } |
| 1125 | int32_t size = FormLength(form); |
| 1126 | if (size == -1) { |
| 1127 | return nullptr; |
| 1128 | } |
| 1129 | tag->AddAttribute(attr, static_cast<uint32_t>(size)); |
| 1130 | } |
| 1131 | *data_pointer = data; |
| 1132 | return tag.release(); |
| 1133 | } |
| 1134 | |
| 1135 | uint32_t GetSize() const { |
| 1136 | return size_; |
| 1137 | } |
| 1138 | |
| 1139 | bool HasChild() { |
| 1140 | return has_child_; |
| 1141 | } |
| 1142 | |
| 1143 | uint32_t GetTagNumber() { |
| 1144 | return tag_; |
| 1145 | } |
| 1146 | |
| 1147 | // Gets the offset of a particular attribute in this tag structure. |
| 1148 | // Interpretation of the data is left to the consumer. 0 is returned if the |
| 1149 | // tag does not contain the attribute. |
| 1150 | uint32_t GetOffsetOf(uint32_t dwarf_attribute) const { |
| 1151 | auto it = off_map_.find(dwarf_attribute); |
| 1152 | if (it == off_map_.end()) { |
| 1153 | return 0; |
| 1154 | } else { |
| 1155 | return it->second; |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // Gets the size of attribute |
| 1160 | uint32_t GetAttrSize(uint32_t dwarf_attribute) const { |
| 1161 | auto it = size_map_.find(dwarf_attribute); |
| 1162 | if (it == size_map_.end()) { |
| 1163 | return 0; |
| 1164 | } else { |
| 1165 | return it->second; |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | private: |
| 1170 | explicit DebugTag(uint32_t index) : index_(index) {} |
| 1171 | void AddAttribute(uint32_t type, uint32_t attr_size) { |
| 1172 | off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_)); |
| 1173 | size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size)); |
| 1174 | size_ += attr_size; |
| 1175 | } |
| 1176 | std::map<uint32_t, uint32_t> off_map_; |
| 1177 | std::map<uint32_t, uint32_t> size_map_; |
| 1178 | uint32_t size_; |
| 1179 | uint32_t tag_; |
| 1180 | bool has_child_; |
| 1181 | }; |
| 1182 | |
| 1183 | class DebugAbbrev { |
| 1184 | public: |
| 1185 | ~DebugAbbrev() {} |
| 1186 | static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) { |
| 1187 | std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev); |
| 1188 | const byte* last = dbg_abbrev + dbg_abbrev_size; |
| 1189 | while (dbg_abbrev < last) { |
| 1190 | std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev)); |
| 1191 | if (tag.get() == nullptr) { |
| 1192 | return nullptr; |
| 1193 | } else { |
| 1194 | abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size())); |
| 1195 | abbrev->tag_list_.push_back(std::move(tag)); |
| 1196 | } |
| 1197 | } |
| 1198 | return abbrev.release(); |
| 1199 | } |
| 1200 | |
| 1201 | DebugTag* ReadTag(const byte* entry) { |
| 1202 | uint32_t tag_num = DecodeUnsignedLeb128(&entry); |
| 1203 | auto it = tags_.find(tag_num); |
| 1204 | if (it == tags_.end()) { |
| 1205 | return nullptr; |
| 1206 | } else { |
| 1207 | CHECK_GT(tag_list_.size(), it->second); |
| 1208 | return tag_list_.at(it->second).get(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | private: |
| 1213 | DebugAbbrev() {} |
| 1214 | std::map<uint32_t, uint32_t> tags_; |
| 1215 | std::vector<std::unique_ptr<DebugTag>> tag_list_; |
| 1216 | }; |
| 1217 | |
| 1218 | class DebugInfoIterator { |
| 1219 | public: |
| 1220 | static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size, |
| 1221 | DebugAbbrev* abbrev) { |
| 1222 | std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev)); |
| 1223 | if (iter->GetCurrentTag() == nullptr) { |
| 1224 | return nullptr; |
| 1225 | } else { |
| 1226 | return iter.release(); |
| 1227 | } |
| 1228 | } |
| 1229 | ~DebugInfoIterator() {} |
| 1230 | |
| 1231 | // Moves to the next DIE. Returns false if at last entry. |
| 1232 | // TODO Handle variable length attributes. |
| 1233 | bool next() { |
| 1234 | if (current_entry_ == nullptr || current_tag_ == nullptr) { |
| 1235 | return false; |
| 1236 | } |
| 1237 | current_entry_ += current_tag_->GetSize(); |
| 1238 | if (current_entry_ >= last_entry_) { |
| 1239 | current_entry_ = nullptr; |
| 1240 | return false; |
| 1241 | } |
| 1242 | current_tag_ = abbrev_->ReadTag(current_entry_); |
| 1243 | if (current_tag_ == nullptr) { |
| 1244 | current_entry_ = nullptr; |
| 1245 | return false; |
| 1246 | } else { |
| 1247 | return true; |
| 1248 | } |
| 1249 | } |
| 1250 | |
| 1251 | const DebugTag* GetCurrentTag() { |
| 1252 | return const_cast<DebugTag*>(current_tag_); |
| 1253 | } |
| 1254 | byte* GetPointerToField(uint8_t dwarf_field) { |
| 1255 | if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) { |
| 1256 | return nullptr; |
| 1257 | } |
| 1258 | uint32_t off = current_tag_->GetOffsetOf(dwarf_field); |
| 1259 | if (off == 0) { |
| 1260 | // tag does not have that field. |
| 1261 | return nullptr; |
| 1262 | } else { |
| 1263 | DCHECK_LT(off, current_tag_->GetSize()); |
| 1264 | return current_entry_ + off; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | private: |
| 1269 | DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev) |
| 1270 | : abbrev_(abbrev), |
| 1271 | last_entry_(reinterpret_cast<byte*>(header) + frame_size), |
| 1272 | current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)), |
| 1273 | current_tag_(abbrev_->ReadTag(current_entry_)) {} |
| 1274 | DebugAbbrev* abbrev_; |
| 1275 | byte* last_entry_; |
| 1276 | byte* current_entry_; |
| 1277 | DebugTag* current_tag_; |
| 1278 | }; |
| 1279 | |
| 1280 | static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) { |
| 1281 | do { |
| 1282 | if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) || |
| 1283 | iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) { |
| 1284 | return false; |
| 1285 | } |
| 1286 | uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc)); |
| 1287 | uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc)); |
| 1288 | if (PC_low != nullptr && PC_high != nullptr) { |
| 1289 | *PC_low += text_start; |
| 1290 | *PC_high += text_start; |
| 1291 | } |
| 1292 | } while (iter->next()); |
| 1293 | return true; |
| 1294 | } |
| 1295 | |
| 1296 | static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size, |
| 1297 | uintptr_t text_start, |
| 1298 | byte* dbg_info, size_t dbg_info_size, |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1299 | byte* eh_frame, size_t eh_frame_size) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1300 | std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size)); |
| 1301 | if (abbrev.get() == nullptr) { |
| 1302 | return false; |
| 1303 | } |
| 1304 | std::unique_ptr<DebugInfoIterator> iter( |
| 1305 | DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info), |
| 1306 | dbg_info_size, abbrev.get())); |
| 1307 | if (iter.get() == nullptr) { |
| 1308 | return false; |
| 1309 | } |
| 1310 | return FixupDebugInfo(text_start, iter.get()) |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1311 | && FixupEHFrame(text_start, eh_frame, eh_frame_size); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1312 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1313 | |
| 1314 | void ElfFile::GdbJITSupport() { |
| 1315 | // We only get here if we only are mapping the program header. |
| 1316 | DCHECK(program_header_only_); |
| 1317 | |
| 1318 | // Well, we need the whole file to do this. |
| 1319 | std::string error_msg; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1320 | // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary |
| 1321 | // sections are there. |
| 1322 | std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE, |
| 1323 | MAP_PRIVATE, &error_msg)); |
| 1324 | if (all_ptr.get() == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1325 | return; |
| 1326 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1327 | ElfFile& all = *all_ptr; |
| 1328 | |
| 1329 | // Do we have interesting sections? |
| 1330 | const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info"); |
| 1331 | const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev"); |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1332 | const Elf32_Shdr* eh_frame = all.FindSectionByName(".eh_frame"); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1333 | const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str"); |
| 1334 | const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab"); |
| 1335 | const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab"); |
| 1336 | Elf32_Shdr* text_sec = all.FindSectionByName(".text"); |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1337 | if (debug_info == nullptr || debug_abbrev == nullptr || eh_frame == nullptr || |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1338 | debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr || symtab_sec == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1339 | return; |
| 1340 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1341 | // We need to add in a strtab and symtab to the image. |
| 1342 | // all is MAP_PRIVATE so it can be written to freely. |
| 1343 | // We also already have strtab and symtab so we are fine there. |
| 1344 | Elf32_Ehdr& elf_hdr = all.GetHeader(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1345 | elf_hdr.e_entry = 0; |
| 1346 | elf_hdr.e_phoff = 0; |
| 1347 | elf_hdr.e_phnum = 0; |
| 1348 | elf_hdr.e_phentsize = 0; |
| 1349 | elf_hdr.e_type = ET_EXEC; |
| 1350 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1351 | text_sec->sh_type = SHT_NOBITS; |
| 1352 | text_sec->sh_offset = 0; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1353 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1354 | if (!FixupDebugSections( |
| 1355 | all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr, |
| 1356 | all.Begin() + debug_info->sh_offset, debug_info->sh_size, |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1357 | all.Begin() + eh_frame->sh_offset, eh_frame->sh_size)) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1358 | LOG(ERROR) << "Failed to load GDB data"; |
| 1359 | return; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1360 | } |
| 1361 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1362 | jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size()); |
| 1363 | gdb_file_mapping_.reset(all_ptr.release()); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1364 | } |
| 1365 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1366 | } // namespace art |