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 | |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 109 | ElfFile::ElfFile(File* file, bool writable, bool program_header_only, uint8_t* requested_base) |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 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), |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 127 | jit_gdb_entry_(nullptr), |
| 128 | requested_base_(requested_base) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 129 | CHECK(file != nullptr); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 130 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 131 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 132 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 133 | std::string* error_msg, uint8_t* requested_base) { |
| 134 | std::unique_ptr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only, |
| 135 | requested_base)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 136 | int prot; |
| 137 | int flags; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 138 | if (writable) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 139 | prot = PROT_READ | PROT_WRITE; |
| 140 | flags = MAP_SHARED; |
| 141 | } else { |
| 142 | prot = PROT_READ; |
| 143 | flags = MAP_PRIVATE; |
| 144 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 145 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 146 | return nullptr; |
| 147 | } |
| 148 | return elf_file.release(); |
| 149 | } |
| 150 | |
| 151 | ElfFile* ElfFile::Open(File* file, int prot, int flags, std::string* error_msg) { |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 152 | std::unique_ptr<ElfFile> elf_file(new ElfFile(file, (prot & PROT_WRITE) == PROT_WRITE, false, |
| 153 | /*requested_base*/nullptr)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 154 | if (!elf_file->Setup(prot, flags, error_msg)) { |
| 155 | return nullptr; |
| 156 | } |
| 157 | return elf_file.release(); |
| 158 | } |
| 159 | |
| 160 | bool ElfFile::Setup(int prot, int flags, std::string* error_msg) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 161 | int64_t temp_file_length = file_->GetLength(); |
| 162 | if (temp_file_length < 0) { |
| 163 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 164 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 165 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 166 | return false; |
| 167 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 168 | size_t file_length = static_cast<size_t>(temp_file_length); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 169 | if (file_length < sizeof(Elf32_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 170 | *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] | 171 | "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 172 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 173 | return false; |
| 174 | } |
| 175 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 176 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 177 | // first just map ELF header to get program header size information |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 178 | size_t elf_header_size = sizeof(Elf32_Ehdr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 179 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 180 | file_->GetPath().c_str(), error_msg), |
| 181 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 182 | return false; |
| 183 | } |
| 184 | // then remap to cover program header |
| 185 | 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] | 186 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 187 | *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] | 188 | "header of %zd bytes: '%s'", file_length, |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 189 | sizeof(Elf32_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 190 | return false; |
| 191 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 192 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 193 | file_->GetPath().c_str(), error_msg), |
| 194 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 195 | *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] | 196 | return false; |
| 197 | } |
| 198 | } else { |
| 199 | // otherwise map entire file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 201 | file_->GetPath().c_str(), error_msg), |
| 202 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 203 | *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] | 204 | return false; |
| 205 | } |
| 206 | } |
| 207 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 208 | if (program_header_only_) { |
| 209 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 210 | } else { |
| 211 | if (!CheckAndSet(GetHeader().e_phoff, "program headers", &program_headers_start_, error_msg)) { |
| 212 | return false; |
| 213 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 214 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 215 | // Setup section headers. |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 216 | if (!CheckAndSet(GetHeader().e_shoff, "section headers", §ion_headers_start_, error_msg)) { |
| 217 | return false; |
| 218 | } |
| 219 | |
| 220 | // Find shstrtab. |
| 221 | Elf32_Shdr* shstrtab_section_header = GetSectionNameStringSection(); |
| 222 | if (shstrtab_section_header == nullptr) { |
| 223 | *error_msg = StringPrintf("Failed to find shstrtab section header in ELF file: '%s'", |
| 224 | file_->GetPath().c_str()); |
| 225 | return false; |
| 226 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 227 | |
| 228 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 229 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 230 | if (dynamic_program_header_ == nullptr) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 231 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 232 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 236 | if (!CheckAndSet(GetDynamicProgramHeader().p_offset, "dynamic section", |
| 237 | reinterpret_cast<byte**>(&dynamic_section_start_), error_msg)) { |
| 238 | return false; |
| 239 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 240 | |
| 241 | // Find other sections from section headers |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 242 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 243 | Elf32_Shdr* section_header = GetSectionHeader(i); |
| 244 | if (section_header == nullptr) { |
| 245 | *error_msg = StringPrintf("Failed to find section header for section %d in ELF file: '%s'", |
| 246 | i, file_->GetPath().c_str()); |
| 247 | return false; |
| 248 | } |
| 249 | switch (section_header->sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 250 | case SHT_SYMTAB: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 251 | if (!CheckAndSet(section_header->sh_offset, "symtab", |
| 252 | reinterpret_cast<byte**>(&symtab_section_start_), error_msg)) { |
| 253 | return false; |
| 254 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 255 | break; |
| 256 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 257 | case SHT_DYNSYM: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 258 | if (!CheckAndSet(section_header->sh_offset, "dynsym", |
| 259 | reinterpret_cast<byte**>(&dynsym_section_start_), error_msg)) { |
| 260 | return false; |
| 261 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 262 | break; |
| 263 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 264 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 265 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 266 | if ((section_header->sh_flags & SHF_ALLOC) != 0) { |
| 267 | // Check that this is named ".dynstr" and ignore otherwise. |
| 268 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 269 | if (strncmp(".dynstr", header_name, 8) == 0) { |
| 270 | if (!CheckAndSet(section_header->sh_offset, "dynstr", |
| 271 | reinterpret_cast<byte**>(&dynstr_section_start_), error_msg)) { |
| 272 | return false; |
| 273 | } |
| 274 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 275 | } else { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 276 | // Check that this is named ".strtab" and ignore otherwise. |
| 277 | const char* header_name = GetString(*shstrtab_section_header, section_header->sh_name); |
| 278 | if (strncmp(".strtab", header_name, 8) == 0) { |
| 279 | if (!CheckAndSet(section_header->sh_offset, "strtab", |
| 280 | reinterpret_cast<byte**>(&strtab_section_start_), error_msg)) { |
| 281 | return false; |
| 282 | } |
| 283 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 284 | } |
| 285 | break; |
| 286 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 287 | case SHT_DYNAMIC: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 288 | if (reinterpret_cast<byte*>(dynamic_section_start_) != |
| 289 | Begin() + section_header->sh_offset) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 290 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 291 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 292 | << reinterpret_cast<void*>(dynamic_section_start_) |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 293 | << " != " << reinterpret_cast<void*>(Begin() + section_header->sh_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 294 | return false; |
| 295 | } |
| 296 | break; |
| 297 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 298 | case SHT_HASH: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 299 | if (!CheckAndSet(section_header->sh_offset, "hash section", |
| 300 | reinterpret_cast<byte**>(&hash_section_start_), error_msg)) { |
| 301 | return false; |
| 302 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 303 | break; |
| 304 | } |
| 305 | } |
| 306 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 307 | |
| 308 | // Check for the existence of some sections. |
| 309 | if (!CheckSectionsExist(error_msg)) { |
| 310 | return false; |
| 311 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 312 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 313 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 314 | return true; |
| 315 | } |
| 316 | |
| 317 | ElfFile::~ElfFile() { |
| 318 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 319 | delete symtab_symbol_table_; |
| 320 | delete dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 321 | delete jit_elf_image_; |
| 322 | if (jit_gdb_entry_) { |
| 323 | UnregisterCodeEntry(jit_gdb_entry_); |
| 324 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 327 | bool ElfFile::CheckAndSet(Elf32_Off offset, const char* label, |
| 328 | byte** target, std::string* error_msg) { |
| 329 | if (Begin() + offset >= End()) { |
| 330 | *error_msg = StringPrintf("Offset %d is out of range for %s in ELF file: '%s'", offset, label, |
| 331 | file_->GetPath().c_str()); |
| 332 | return false; |
| 333 | } |
| 334 | *target = Begin() + offset; |
| 335 | return true; |
| 336 | } |
| 337 | |
| 338 | bool ElfFile::CheckSectionsLinked(const byte* source, const byte* target) const { |
| 339 | // Only works in whole-program mode, as we need to iterate over the sections. |
| 340 | // Note that we normally can't search by type, as duplicates are allowed for most section types. |
| 341 | if (program_header_only_) { |
| 342 | return true; |
| 343 | } |
| 344 | |
| 345 | Elf32_Shdr* source_section = nullptr; |
| 346 | Elf32_Word target_index = 0; |
| 347 | bool target_found = false; |
| 348 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 349 | Elf32_Shdr* section_header = GetSectionHeader(i); |
| 350 | |
| 351 | if (Begin() + section_header->sh_offset == source) { |
| 352 | // Found the source. |
| 353 | source_section = section_header; |
| 354 | if (target_index) { |
| 355 | break; |
| 356 | } |
| 357 | } else if (Begin() + section_header->sh_offset == target) { |
| 358 | target_index = i; |
| 359 | target_found = true; |
| 360 | if (source_section != nullptr) { |
| 361 | break; |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | return target_found && source_section != nullptr && source_section->sh_link == target_index; |
| 367 | } |
| 368 | |
| 369 | bool ElfFile::CheckSectionsExist(std::string* error_msg) const { |
| 370 | if (!program_header_only_) { |
| 371 | // If in full mode, need section headers. |
| 372 | if (section_headers_start_ == nullptr) { |
| 373 | *error_msg = StringPrintf("No section headers in ELF file: '%s'", file_->GetPath().c_str()); |
| 374 | return false; |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | // This is redundant, but defensive. |
| 379 | if (dynamic_program_header_ == nullptr) { |
| 380 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 381 | file_->GetPath().c_str()); |
| 382 | return false; |
| 383 | } |
| 384 | |
| 385 | // Need a dynamic section. This is redundant, but defensive. |
| 386 | if (dynamic_section_start_ == nullptr) { |
| 387 | *error_msg = StringPrintf("Failed to find dynamic section in ELF file: '%s'", |
| 388 | file_->GetPath().c_str()); |
| 389 | return false; |
| 390 | } |
| 391 | |
| 392 | // Symtab validation. These is not really a hard failure, as we are currently not using the |
| 393 | // symtab internally, but it's nice to be defensive. |
| 394 | if (symtab_section_start_ != nullptr) { |
| 395 | // When there's a symtab, there should be a strtab. |
| 396 | if (strtab_section_start_ == nullptr) { |
| 397 | *error_msg = StringPrintf("No strtab for symtab in ELF file: '%s'", file_->GetPath().c_str()); |
| 398 | return false; |
| 399 | } |
| 400 | |
| 401 | // The symtab should link to the strtab. |
| 402 | if (!CheckSectionsLinked(reinterpret_cast<const byte*>(symtab_section_start_), |
| 403 | reinterpret_cast<const byte*>(strtab_section_start_))) { |
| 404 | *error_msg = StringPrintf("Symtab is not linked to the strtab in ELF file: '%s'", |
| 405 | file_->GetPath().c_str()); |
| 406 | return false; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | // We always need a dynstr & dynsym. |
| 411 | if (dynstr_section_start_ == nullptr) { |
| 412 | *error_msg = StringPrintf("No dynstr in ELF file: '%s'", file_->GetPath().c_str()); |
| 413 | return false; |
| 414 | } |
| 415 | if (dynsym_section_start_ == nullptr) { |
| 416 | *error_msg = StringPrintf("No dynsym in ELF file: '%s'", file_->GetPath().c_str()); |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | // Need a hash section for dynamic symbol lookup. |
| 421 | if (hash_section_start_ == nullptr) { |
| 422 | *error_msg = StringPrintf("Failed to find hash section in ELF file: '%s'", |
| 423 | file_->GetPath().c_str()); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | // And the hash section should be linking to the dynsym. |
| 428 | if (!CheckSectionsLinked(reinterpret_cast<const byte*>(hash_section_start_), |
| 429 | reinterpret_cast<const byte*>(dynsym_section_start_))) { |
| 430 | *error_msg = StringPrintf("Hash section is not linked to the dynstr in ELF file: '%s'", |
| 431 | file_->GetPath().c_str()); |
| 432 | return false; |
| 433 | } |
| 434 | |
Andreas Gampe | ad00fed | 2014-12-10 20:51:45 -0800 | [diff] [blame] | 435 | // We'd also like to confirm a shstrtab in program_header_only_ mode (else Open() does this for |
| 436 | // us). This is usually the last in an oat file, and a good indicator of whether writing was |
| 437 | // successful (or the process crashed and left garbage). |
| 438 | if (program_header_only_) { |
| 439 | // It might not be mapped, but we can compare against the file size. |
| 440 | int64_t offset = static_cast<int64_t>(GetHeader().e_shoff + |
| 441 | (GetHeader().e_shstrndx * GetHeader().e_shentsize)); |
| 442 | if (offset >= file_->GetLength()) { |
| 443 | *error_msg = StringPrintf("Shstrtab is not in the mapped ELF file: '%s'", |
| 444 | file_->GetPath().c_str()); |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 449 | return true; |
| 450 | } |
| 451 | |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 452 | bool ElfFile::SetMap(MemMap* map, std::string* error_msg) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 453 | if (map == nullptr) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 454 | // MemMap::Open should have already set an error. |
| 455 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | map_.reset(map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 459 | CHECK(map_.get() != nullptr) << file_->GetPath(); |
| 460 | CHECK(map_->Begin() != nullptr) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 461 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 462 | header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin()); |
| 463 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 464 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 465 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 466 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 467 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 468 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 469 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 470 | header_->e_ident[EI_MAG0], |
| 471 | header_->e_ident[EI_MAG1], |
| 472 | header_->e_ident[EI_MAG2], |
| 473 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 474 | return false; |
| 475 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 476 | if (ELFCLASS32 != header_->e_ident[EI_CLASS]) { |
| 477 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
| 478 | ELFCLASS32, |
| 479 | file_->GetPath().c_str(), |
| 480 | header_->e_ident[EI_CLASS]); |
| 481 | return false; |
| 482 | } |
| 483 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 484 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 485 | ELFDATA2LSB, |
| 486 | file_->GetPath().c_str(), |
| 487 | header_->e_ident[EI_CLASS]); |
| 488 | return false; |
| 489 | } |
| 490 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 491 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 492 | EV_CURRENT, |
| 493 | file_->GetPath().c_str(), |
| 494 | header_->e_ident[EI_CLASS]); |
| 495 | return false; |
| 496 | } |
| 497 | if (ET_DYN != header_->e_type) { |
| 498 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 499 | ET_DYN, |
| 500 | file_->GetPath().c_str(), |
| 501 | header_->e_type); |
| 502 | return false; |
| 503 | } |
| 504 | if (EV_CURRENT != header_->e_version) { |
| 505 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 506 | EV_CURRENT, |
| 507 | file_->GetPath().c_str(), |
| 508 | header_->e_version); |
| 509 | return false; |
| 510 | } |
| 511 | if (0 != header_->e_entry) { |
| 512 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 513 | 0, |
| 514 | file_->GetPath().c_str(), |
| 515 | header_->e_entry); |
| 516 | return false; |
| 517 | } |
| 518 | if (0 == header_->e_phoff) { |
| 519 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 520 | file_->GetPath().c_str()); |
| 521 | return false; |
| 522 | } |
| 523 | if (0 == header_->e_shoff) { |
| 524 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 525 | file_->GetPath().c_str()); |
| 526 | return false; |
| 527 | } |
| 528 | if (0 == header_->e_ehsize) { |
| 529 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 530 | file_->GetPath().c_str()); |
| 531 | return false; |
| 532 | } |
| 533 | if (0 == header_->e_phentsize) { |
| 534 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 535 | file_->GetPath().c_str()); |
| 536 | return false; |
| 537 | } |
| 538 | if (0 == header_->e_phnum) { |
| 539 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 540 | file_->GetPath().c_str()); |
| 541 | return false; |
| 542 | } |
| 543 | if (0 == header_->e_shentsize) { |
| 544 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 545 | file_->GetPath().c_str()); |
| 546 | return false; |
| 547 | } |
| 548 | if (0 == header_->e_shnum) { |
| 549 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 550 | file_->GetPath().c_str()); |
| 551 | return false; |
| 552 | } |
| 553 | if (0 == header_->e_shstrndx) { |
| 554 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 555 | file_->GetPath().c_str()); |
| 556 | return false; |
| 557 | } |
| 558 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 559 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 560 | header_->e_shstrndx, |
| 561 | header_->e_shnum, |
| 562 | file_->GetPath().c_str()); |
| 563 | return false; |
| 564 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 565 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 566 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 567 | if (header_->e_phoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 568 | *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] | 569 | header_->e_phoff, |
| 570 | Size(), |
| 571 | file_->GetPath().c_str()); |
| 572 | return false; |
| 573 | } |
| 574 | if (header_->e_shoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 575 | *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] | 576 | header_->e_shoff, |
| 577 | Size(), |
| 578 | file_->GetPath().c_str()); |
| 579 | return false; |
| 580 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 581 | } |
| 582 | return true; |
| 583 | } |
| 584 | |
| 585 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 586 | Elf32_Ehdr& ElfFile::GetHeader() const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 587 | 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] | 588 | return *header_; |
| 589 | } |
| 590 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 591 | byte* ElfFile::GetProgramHeadersStart() const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 592 | CHECK(program_headers_start_ != nullptr); // Header has been set in Setup. This is a sanity |
| 593 | // check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 594 | return program_headers_start_; |
| 595 | } |
| 596 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 597 | byte* ElfFile::GetSectionHeadersStart() const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 598 | CHECK(!program_header_only_); // Only used in "full" mode. |
| 599 | CHECK(section_headers_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 600 | return section_headers_start_; |
| 601 | } |
| 602 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 603 | Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 604 | CHECK(dynamic_program_header_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 605 | return *dynamic_program_header_; |
| 606 | } |
| 607 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 608 | Elf32_Dyn* ElfFile::GetDynamicSectionStart() const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 609 | CHECK(dynamic_section_start_ != nullptr); // Is checked in CheckSectionsExist. Sanity check. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 610 | return dynamic_section_start_; |
| 611 | } |
| 612 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 613 | static bool IsSymbolSectionType(Elf32_Word section_type) { |
| 614 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
| 615 | } |
| 616 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 617 | Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 618 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 619 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 620 | case SHT_SYMTAB: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 621 | return symtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 622 | break; |
| 623 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 624 | case SHT_DYNSYM: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 625 | return dynsym_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | default: { |
| 629 | LOG(FATAL) << section_type; |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 630 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 631 | } |
| 632 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 633 | } |
| 634 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 635 | const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 636 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 637 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 638 | case SHT_SYMTAB: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 639 | return strtab_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 640 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 641 | case SHT_DYNSYM: { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 642 | return dynstr_section_start_; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 643 | } |
| 644 | default: { |
| 645 | LOG(FATAL) << section_type; |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 646 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 647 | } |
| 648 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 649 | } |
| 650 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 651 | const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 652 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 653 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 654 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 655 | } |
| 656 | const char* string_section_start = GetStringSectionStart(section_type); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 657 | if (string_section_start == nullptr) { |
| 658 | return nullptr; |
| 659 | } |
| 660 | return string_section_start + i; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 661 | } |
| 662 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 663 | // WARNING: The following methods do not check for an error condition (non-existent hash section). |
| 664 | // It is the caller's job to do this. |
| 665 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 666 | Elf32_Word* ElfFile::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 667 | return hash_section_start_; |
| 668 | } |
| 669 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 670 | Elf32_Word ElfFile::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 671 | return GetHashSectionStart()[0]; |
| 672 | } |
| 673 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 674 | Elf32_Word ElfFile::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 675 | return GetHashSectionStart()[1]; |
| 676 | } |
| 677 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 678 | Elf32_Word ElfFile::GetHashBucket(size_t i, bool* ok) const { |
| 679 | if (i >= GetHashBucketNum()) { |
| 680 | *ok = false; |
| 681 | return 0; |
| 682 | } |
| 683 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 684 | // 0 is nbucket, 1 is nchain |
| 685 | return GetHashSectionStart()[2 + i]; |
| 686 | } |
| 687 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 688 | Elf32_Word ElfFile::GetHashChain(size_t i, bool* ok) const { |
| 689 | if (i >= GetHashBucketNum()) { |
| 690 | *ok = false; |
| 691 | return 0; |
| 692 | } |
| 693 | *ok = true; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 694 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 695 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 696 | } |
| 697 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 698 | Elf32_Word ElfFile::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 699 | return GetHeader().e_phnum; |
| 700 | } |
| 701 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 702 | Elf32_Phdr* ElfFile::GetProgramHeader(Elf32_Word i) const { |
| 703 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); // Sanity check for caller. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 704 | byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 705 | if (program_header >= End()) { |
| 706 | return nullptr; // Failure condition. |
| 707 | } |
| 708 | return reinterpret_cast<Elf32_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 709 | } |
| 710 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 711 | Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 712 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 713 | Elf32_Phdr* program_header = GetProgramHeader(i); |
| 714 | if (program_header->p_type == type) { |
| 715 | return program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 716 | } |
| 717 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 718 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 721 | Elf32_Word ElfFile::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 722 | return GetHeader().e_shnum; |
| 723 | } |
| 724 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 725 | Elf32_Shdr* ElfFile::GetSectionHeader(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 726 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 727 | // Even if we Load(), it doesn't bring in all the sections. |
| 728 | CHECK(!program_header_only_) << file_->GetPath(); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 729 | if (i >= GetSectionHeaderNum()) { |
| 730 | return nullptr; // Failure condition. |
| 731 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 732 | byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 733 | if (section_header >= End()) { |
| 734 | return nullptr; // Failure condition. |
| 735 | } |
| 736 | return reinterpret_cast<Elf32_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 737 | } |
| 738 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 739 | Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 740 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 741 | // We could change this to switch on known types if they were detected during loading. |
| 742 | CHECK(!program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 743 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 744 | Elf32_Shdr* section_header = GetSectionHeader(i); |
| 745 | if (section_header->sh_type == type) { |
| 746 | return section_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 747 | } |
| 748 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 749 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 753 | static unsigned elfhash(const char *_name) { |
| 754 | const unsigned char *name = (const unsigned char *) _name; |
| 755 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 756 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 757 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 758 | h = (h << 4) + *name++; |
| 759 | g = h & 0xf0000000; |
| 760 | h ^= g; |
| 761 | h ^= g >> 24; |
| 762 | } |
| 763 | return h; |
| 764 | } |
| 765 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 766 | Elf32_Shdr* ElfFile::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 767 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 768 | } |
| 769 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 770 | const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 771 | // Check that we have a hash section. |
| 772 | if (GetHashSectionStart() == nullptr) { |
| 773 | return nullptr; // Failure condition. |
| 774 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 775 | const Elf32_Sym* sym = FindDynamicSymbol(symbol_name); |
| 776 | if (sym != nullptr) { |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 777 | // TODO: we need to change this to calculate base_address_ in ::Open, |
| 778 | // 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] | 779 | return base_address_ + sym->st_value; |
| 780 | } else { |
| 781 | return nullptr; |
| 782 | } |
| 783 | } |
| 784 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 785 | // WARNING: Only called from FindDynamicSymbolAddress. Elides check for hash section. |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 786 | const Elf32_Sym* ElfFile::FindDynamicSymbol(const std::string& symbol_name) const { |
Andreas Gampe | 1b2140c | 2014-09-08 23:39:45 -0700 | [diff] [blame] | 787 | if (GetHashBucketNum() == 0) { |
| 788 | // No dynamic symbols at all. |
| 789 | return nullptr; |
| 790 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 791 | Elf32_Word hash = elfhash(symbol_name.c_str()); |
| 792 | Elf32_Word bucket_index = hash % GetHashBucketNum(); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 793 | bool ok; |
| 794 | Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index, &ok); |
| 795 | if (!ok) { |
| 796 | return nullptr; |
| 797 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 798 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 799 | Elf32_Sym* symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
| 800 | if (symbol == nullptr) { |
| 801 | return nullptr; // Failure condition. |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 802 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 803 | const char* name = GetString(SHT_DYNSYM, symbol->st_name); |
| 804 | if (symbol_name == name) { |
| 805 | return symbol; |
| 806 | } |
| 807 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index, &ok); |
| 808 | if (!ok) { |
| 809 | return nullptr; |
| 810 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 811 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 812 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 813 | } |
| 814 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 815 | Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const { |
| 816 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 817 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 818 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 819 | return section_header.sh_size / section_header.sh_entsize; |
| 820 | } |
| 821 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 822 | Elf32_Sym* ElfFile::GetSymbol(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 823 | Elf32_Word i) const { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 824 | Elf32_Sym* sym_start = GetSymbolSectionStart(section_type); |
| 825 | if (sym_start == nullptr) { |
| 826 | return nullptr; |
| 827 | } |
| 828 | return sym_start + i; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 829 | } |
| 830 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 831 | ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 832 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 833 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 834 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 835 | return &symtab_symbol_table_; |
| 836 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 837 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 838 | return &dynsym_symbol_table_; |
| 839 | } |
| 840 | default: { |
| 841 | LOG(FATAL) << section_type; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 842 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 847 | Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type, |
| 848 | const std::string& symbol_name, |
| 849 | bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 850 | CHECK(!program_header_only_) << file_->GetPath(); |
| 851 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 852 | |
| 853 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 854 | if (*symbol_table != nullptr || build_map) { |
| 855 | if (*symbol_table == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 856 | DCHECK(build_map); |
| 857 | *symbol_table = new SymbolTable; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 858 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 859 | if (symbol_section == nullptr) { |
| 860 | return nullptr; // Failure condition. |
| 861 | } |
| 862 | Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
| 863 | if (string_section == nullptr) { |
| 864 | return nullptr; // Failure condition. |
| 865 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 866 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 867 | Elf32_Sym* symbol = GetSymbol(section_type, i); |
| 868 | if (symbol == nullptr) { |
| 869 | return nullptr; // Failure condition. |
| 870 | } |
| 871 | unsigned char type = ELF32_ST_TYPE(symbol->st_info); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 872 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 873 | continue; |
| 874 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 875 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 876 | if (name == nullptr) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 877 | continue; |
| 878 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 879 | std::pair<SymbolTable::iterator, bool> result = |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 880 | (*symbol_table)->insert(std::make_pair(name, symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 881 | if (!result.second) { |
| 882 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 883 | if ((symbol->st_value != result.first->second->st_value) || |
| 884 | (symbol->st_size != result.first->second->st_size) || |
| 885 | (symbol->st_info != result.first->second->st_info) || |
| 886 | (symbol->st_other != result.first->second->st_other) || |
| 887 | (symbol->st_shndx != result.first->second->st_shndx)) { |
| 888 | return nullptr; // Failure condition. |
| 889 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 890 | } |
| 891 | } |
| 892 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 893 | CHECK(*symbol_table != nullptr); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 894 | SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
| 895 | if (it == (*symbol_table)->end()) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 896 | return nullptr; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 897 | } |
| 898 | return it->second; |
| 899 | } |
| 900 | |
| 901 | // Fall back to linear search |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 902 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 903 | if (symbol_section == nullptr) { |
| 904 | return nullptr; |
| 905 | } |
| 906 | Elf32_Shdr* string_section = GetSectionHeader(symbol_section->sh_link); |
| 907 | if (string_section == nullptr) { |
| 908 | return nullptr; |
| 909 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 910 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 911 | Elf32_Sym* symbol = GetSymbol(section_type, i); |
| 912 | if (symbol == nullptr) { |
| 913 | return nullptr; // Failure condition. |
| 914 | } |
| 915 | const char* name = GetString(*string_section, symbol->st_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 916 | if (name == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 917 | continue; |
| 918 | } |
| 919 | if (symbol_name == name) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 920 | return symbol; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 921 | } |
| 922 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 923 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 924 | } |
| 925 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 926 | Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 927 | const std::string& symbol_name, |
| 928 | bool build_map) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 929 | Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 930 | if (symbol == nullptr) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 931 | return 0; |
| 932 | } |
| 933 | return symbol->st_value; |
| 934 | } |
| 935 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 936 | const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 937 | CHECK(!program_header_only_) << file_->GetPath(); |
| 938 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 939 | if (static_cast<Elf32_Word>(SHT_STRTAB) != string_section.sh_type) { |
| 940 | return nullptr; // Failure condition. |
| 941 | } |
| 942 | if (i >= string_section.sh_size) { |
| 943 | return nullptr; |
| 944 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 945 | if (i == 0) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 946 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 947 | } |
| 948 | byte* strings = Begin() + string_section.sh_offset; |
| 949 | byte* string = strings + i; |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 950 | if (string >= End()) { |
| 951 | return nullptr; |
| 952 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 953 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 956 | Elf32_Word ElfFile::GetDynamicNum() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 957 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 958 | } |
| 959 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 960 | Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 961 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 962 | return *(GetDynamicSectionStart() + i); |
| 963 | } |
| 964 | |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 965 | Elf32_Dyn* ElfFile::FindDynamicByType(Elf32_Sword type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 966 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 967 | Elf32_Dyn* dyn = &GetDynamic(i); |
| 968 | if (dyn->d_tag == type) { |
| 969 | return dyn; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
Alex Light | 53cb16b | 2014-06-12 11:26:29 -0700 | [diff] [blame] | 972 | return NULL; |
| 973 | } |
| 974 | |
| 975 | Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const { |
| 976 | Elf32_Dyn* dyn = FindDynamicByType(type); |
| 977 | if (dyn == NULL) { |
| 978 | return 0; |
| 979 | } else { |
| 980 | return dyn->d_un.d_val; |
| 981 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 982 | } |
| 983 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 984 | Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 985 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 986 | return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 989 | Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 990 | 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] | 991 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 992 | return section_header.sh_size / section_header.sh_entsize; |
| 993 | } |
| 994 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 995 | Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 996 | 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] | 997 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 998 | return *(GetRelSectionStart(section_header) + i); |
| 999 | } |
| 1000 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1001 | Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1002 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 1003 | return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1006 | Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1007 | 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] | 1008 | return section_header.sh_size / section_header.sh_entsize; |
| 1009 | } |
| 1010 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1011 | Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1012 | 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] | 1013 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 1014 | return *(GetRelaSectionStart(section_header) + i); |
| 1015 | } |
| 1016 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1017 | // Base on bionic phdr_table_get_load_size |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1018 | size_t ElfFile::GetLoadedSize() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1019 | Elf32_Addr min_vaddr = 0xFFFFFFFFu; |
| 1020 | Elf32_Addr max_vaddr = 0x00000000u; |
| 1021 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1022 | Elf32_Phdr* program_header = GetProgramHeader(i); |
| 1023 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1024 | continue; |
| 1025 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1026 | Elf32_Addr begin_vaddr = program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1027 | if (begin_vaddr < min_vaddr) { |
| 1028 | min_vaddr = begin_vaddr; |
| 1029 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1030 | Elf32_Addr end_vaddr = program_header->p_vaddr + program_header->p_memsz; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1031 | if (end_vaddr > max_vaddr) { |
| 1032 | max_vaddr = end_vaddr; |
| 1033 | } |
| 1034 | } |
| 1035 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 1036 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 1037 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 1038 | size_t loaded_size = max_vaddr - min_vaddr; |
| 1039 | return loaded_size; |
| 1040 | } |
| 1041 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1042 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1043 | CHECK(program_header_only_) << file_->GetPath(); |
Andreas Gampe | 91268c1 | 2014-04-03 17:50:24 -0700 | [diff] [blame] | 1044 | |
| 1045 | if (executable) { |
| 1046 | InstructionSet elf_ISA = kNone; |
| 1047 | switch (GetHeader().e_machine) { |
| 1048 | case EM_ARM: { |
| 1049 | elf_ISA = kArm; |
| 1050 | break; |
| 1051 | } |
| 1052 | case EM_AARCH64: { |
| 1053 | elf_ISA = kArm64; |
| 1054 | break; |
| 1055 | } |
| 1056 | case EM_386: { |
| 1057 | elf_ISA = kX86; |
| 1058 | break; |
| 1059 | } |
| 1060 | case EM_X86_64: { |
| 1061 | elf_ISA = kX86_64; |
| 1062 | break; |
| 1063 | } |
| 1064 | case EM_MIPS: { |
| 1065 | elf_ISA = kMips; |
| 1066 | break; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | if (elf_ISA != kRuntimeISA) { |
| 1071 | std::ostringstream oss; |
| 1072 | oss << "Expected ISA " << kRuntimeISA << " but found " << elf_ISA; |
| 1073 | *error_msg = oss.str(); |
| 1074 | return false; |
| 1075 | } |
| 1076 | } |
| 1077 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1078 | bool reserved = false; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1079 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1080 | Elf32_Phdr* program_header = GetProgramHeader(i); |
| 1081 | if (program_header == nullptr) { |
| 1082 | *error_msg = StringPrintf("No program header for entry %d in ELF file %s.", |
| 1083 | i, file_->GetPath().c_str()); |
| 1084 | return false; |
| 1085 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1086 | |
| 1087 | // Record .dynamic header information for later use |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1088 | if (program_header->p_type == PT_DYNAMIC) { |
| 1089 | dynamic_program_header_ = program_header; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1090 | continue; |
| 1091 | } |
| 1092 | |
| 1093 | // Not something to load, move on. |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1094 | if (program_header->p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1095 | continue; |
| 1096 | } |
| 1097 | |
| 1098 | // Found something to load. |
| 1099 | |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1100 | // Before load the actual segments, reserve a contiguous chunk |
| 1101 | // of required size and address for all segments, but with no |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1102 | // permissions. We'll then carve that up with the proper |
| 1103 | // permissions as we load the actual segments. If p_vaddr is |
| 1104 | // non-zero, the segments require the specific address specified, |
| 1105 | // which either was specified in the file because we already set |
| 1106 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1107 | int64_t temp_file_length = file_->GetLength(); |
| 1108 | if (temp_file_length < 0) { |
| 1109 | errno = -temp_file_length; |
| 1110 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 1111 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 1112 | return false; |
| 1113 | } |
| 1114 | size_t file_length = static_cast<size_t>(temp_file_length); |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1115 | if (!reserved) { |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1116 | uint8_t* reserve_base = reinterpret_cast<uint8_t*>(program_header->p_vaddr); |
| 1117 | uint8_t* reserve_base_override = reserve_base; |
| 1118 | // Override the base (e.g. when compiling with --compile-pic) |
| 1119 | if (requested_base_ != nullptr) { |
| 1120 | reserve_base_override = requested_base_; |
| 1121 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1122 | std::string reservation_name("ElfFile reservation for "); |
| 1123 | reservation_name += file_->GetPath(); |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1124 | std::unique_ptr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1125 | reserve_base_override, |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1126 | GetLoadedSize(), PROT_NONE, false, |
| 1127 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1128 | if (reserve.get() == nullptr) { |
| 1129 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 1130 | reservation_name.c_str(), error_msg->c_str()); |
| 1131 | return false; |
| 1132 | } |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1133 | reserved = true; |
Igor Murashkin | 90ca5c0 | 2014-10-22 11:37:02 -0700 | [diff] [blame] | 1134 | |
| 1135 | // Base address is the difference of actual mapped location and the p_vaddr |
| 1136 | base_address_ = reinterpret_cast<uint8_t*>(reinterpret_cast<uintptr_t>(reserve->Begin()) |
| 1137 | - reinterpret_cast<uintptr_t>(reserve_base)); |
| 1138 | // By adding the p_vaddr of a section/symbol to base_address_ we will always get the |
| 1139 | // dynamic memory address of where that object is actually mapped |
| 1140 | // |
| 1141 | // TODO: base_address_ needs to be calculated in ::Open, otherwise |
| 1142 | // FindDynamicSymbolAddress returns the wrong values until Load is called. |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1143 | segments_.push_back(reserve.release()); |
| 1144 | } |
| 1145 | // empty segment, nothing to map |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1146 | if (program_header->p_memsz == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1147 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1148 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1149 | byte* p_vaddr = base_address_ + program_header->p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1150 | int prot = 0; |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1151 | if (executable && ((program_header->p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1152 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1153 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1154 | if ((program_header->p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1155 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1156 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1157 | if ((program_header->p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 1158 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1159 | } |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1160 | int flags = 0; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1161 | if (writable_) { |
| 1162 | prot |= PROT_WRITE; |
| 1163 | flags |= MAP_SHARED; |
| 1164 | } else { |
| 1165 | flags |= MAP_PRIVATE; |
| 1166 | } |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1167 | if (file_length < (program_header->p_offset + program_header->p_memsz)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 1168 | *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] | 1169 | "%d of %d bytes: '%s'", file_length, i, |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1170 | program_header->p_offset + program_header->p_memsz, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1171 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 1172 | return false; |
| 1173 | } |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 1174 | std::unique_ptr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1175 | program_header->p_memsz, |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1176 | prot, flags, file_->Fd(), |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1177 | program_header->p_offset, |
Hiroshi Yamauchi | 4fb5df8 | 2014-03-13 15:10:27 -0700 | [diff] [blame] | 1178 | true, // implies MAP_FIXED |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 1179 | file_->GetPath().c_str(), |
| 1180 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1181 | if (segment.get() == nullptr) { |
| 1182 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 1183 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 1184 | return false; |
| 1185 | } |
| 1186 | if (segment->Begin() != p_vaddr) { |
| 1187 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 1188 | "instead mapped to %p", |
| 1189 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 1190 | return false; |
| 1191 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1192 | segments_.push_back(segment.release()); |
| 1193 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1194 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1195 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1196 | byte* dsptr = base_address_ + GetDynamicProgramHeader().p_vaddr; |
| 1197 | if ((dsptr < Begin() || dsptr >= End()) && !ValidPointer(dsptr)) { |
| 1198 | *error_msg = StringPrintf("dynamic section address invalid in ELF file %s", |
| 1199 | file_->GetPath().c_str()); |
| 1200 | return false; |
| 1201 | } |
| 1202 | dynamic_section_start_ = reinterpret_cast<Elf32_Dyn*>(dsptr); |
| 1203 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1204 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 1205 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1206 | byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
| 1207 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1208 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1209 | if (!ValidPointer(d_ptr)) { |
| 1210 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1211 | d_ptr, file_->GetPath().c_str()); |
| 1212 | return false; |
| 1213 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1214 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1215 | break; |
| 1216 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1217 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1218 | if (!ValidPointer(d_ptr)) { |
| 1219 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1220 | d_ptr, file_->GetPath().c_str()); |
| 1221 | return false; |
| 1222 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1223 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 1224 | break; |
| 1225 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1226 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1227 | if (!ValidPointer(d_ptr)) { |
| 1228 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 1229 | d_ptr, file_->GetPath().c_str()); |
| 1230 | return false; |
| 1231 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1232 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1233 | break; |
| 1234 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 1235 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1236 | if (GetDynamicNum() != i+1) { |
| 1237 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 1238 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 1239 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 1240 | return false; |
| 1241 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 1242 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1243 | } |
| 1244 | } |
| 1245 | } |
| 1246 | |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1247 | // Check for the existence of some sections. |
| 1248 | if (!CheckSectionsExist(error_msg)) { |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1252 | // Use GDB JIT support to do stack backtrace, etc. |
| 1253 | if (executable) { |
| 1254 | GdbJITSupport(); |
| 1255 | } |
| 1256 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1257 | return true; |
| 1258 | } |
| 1259 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 1260 | bool ElfFile::ValidPointer(const byte* start) const { |
| 1261 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 1262 | const MemMap* segment = segments_[i]; |
| 1263 | if (segment->Begin() <= start && start < segment->End()) { |
| 1264 | return true; |
| 1265 | } |
| 1266 | } |
| 1267 | return false; |
| 1268 | } |
| 1269 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1270 | |
| 1271 | Elf32_Shdr* ElfFile::FindSectionByName(const std::string& name) const { |
| 1272 | CHECK(!program_header_only_); |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1273 | Elf32_Shdr* shstrtab_sec = GetSectionNameStringSection(); |
| 1274 | if (shstrtab_sec == nullptr) { |
| 1275 | return nullptr; |
| 1276 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1277 | for (uint32_t i = 0; i < GetSectionHeaderNum(); i++) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1278 | Elf32_Shdr* shdr = GetSectionHeader(i); |
| 1279 | if (shdr == nullptr) { |
| 1280 | return nullptr; |
| 1281 | } |
| 1282 | const char* sec_name = GetString(*shstrtab_sec, shdr->sh_name); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1283 | if (sec_name == nullptr) { |
| 1284 | continue; |
| 1285 | } |
| 1286 | if (name == sec_name) { |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1287 | return shdr; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | return nullptr; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1293 | struct PACKED(1) FDE { |
| 1294 | uint32_t raw_length_; |
| 1295 | uint32_t GetLength() { |
| 1296 | return raw_length_ + sizeof(raw_length_); |
| 1297 | } |
| 1298 | uint32_t CIE_pointer; |
| 1299 | uint32_t initial_location; |
| 1300 | uint32_t address_range; |
| 1301 | uint8_t instructions[0]; |
| 1302 | }; |
| 1303 | |
| 1304 | static FDE* NextFDE(FDE* frame) { |
| 1305 | byte* fde_bytes = reinterpret_cast<byte*>(frame); |
| 1306 | fde_bytes += frame->GetLength(); |
| 1307 | return reinterpret_cast<FDE*>(fde_bytes); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1308 | } |
| 1309 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1310 | static bool IsFDE(FDE* frame) { |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1311 | return frame->CIE_pointer != 0; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | // TODO This only works for 32-bit Elf Files. |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1315 | static bool FixupEHFrame(uintptr_t text_start, byte* eh_frame, size_t eh_frame_size) { |
| 1316 | FDE* last_frame = reinterpret_cast<FDE*>(eh_frame + eh_frame_size); |
| 1317 | FDE* frame = NextFDE(reinterpret_cast<FDE*>(eh_frame)); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1318 | for (; frame < last_frame; frame = NextFDE(frame)) { |
| 1319 | if (!IsFDE(frame)) { |
| 1320 | return false; |
| 1321 | } |
| 1322 | frame->initial_location += text_start; |
| 1323 | } |
| 1324 | return true; |
| 1325 | } |
| 1326 | |
| 1327 | struct PACKED(1) DebugInfoHeader { |
| 1328 | uint32_t unit_length; // TODO 32-bit specific size |
| 1329 | uint16_t version; |
| 1330 | uint32_t debug_abbrev_offset; // TODO 32-bit specific size |
| 1331 | uint8_t address_size; |
| 1332 | }; |
| 1333 | |
| 1334 | // Returns -1 if it is variable length, which we will just disallow for now. |
| 1335 | static int32_t FormLength(uint32_t att) { |
| 1336 | switch (att) { |
| 1337 | case DW_FORM_data1: |
| 1338 | case DW_FORM_flag: |
| 1339 | case DW_FORM_flag_present: |
| 1340 | case DW_FORM_ref1: |
| 1341 | return 1; |
| 1342 | |
| 1343 | case DW_FORM_data2: |
| 1344 | case DW_FORM_ref2: |
| 1345 | return 2; |
| 1346 | |
| 1347 | case DW_FORM_addr: // TODO 32-bit only |
| 1348 | case DW_FORM_ref_addr: // TODO 32-bit only |
| 1349 | case DW_FORM_sec_offset: // TODO 32-bit only |
| 1350 | case DW_FORM_strp: // TODO 32-bit only |
| 1351 | case DW_FORM_data4: |
| 1352 | case DW_FORM_ref4: |
| 1353 | return 4; |
| 1354 | |
| 1355 | case DW_FORM_data8: |
| 1356 | case DW_FORM_ref8: |
| 1357 | case DW_FORM_ref_sig8: |
| 1358 | return 8; |
| 1359 | |
| 1360 | case DW_FORM_block: |
| 1361 | case DW_FORM_block1: |
| 1362 | case DW_FORM_block2: |
| 1363 | case DW_FORM_block4: |
| 1364 | case DW_FORM_exprloc: |
| 1365 | case DW_FORM_indirect: |
| 1366 | case DW_FORM_ref_udata: |
| 1367 | case DW_FORM_sdata: |
| 1368 | case DW_FORM_string: |
| 1369 | case DW_FORM_udata: |
| 1370 | default: |
| 1371 | return -1; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1372 | } |
| 1373 | } |
| 1374 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1375 | class DebugTag { |
| 1376 | public: |
| 1377 | const uint32_t index_; |
| 1378 | ~DebugTag() {} |
| 1379 | // Creates a new tag and moves data pointer up to the start of the next one. |
| 1380 | // nullptr means error. |
| 1381 | static DebugTag* Create(const byte** data_pointer) { |
| 1382 | const byte* data = *data_pointer; |
| 1383 | uint32_t index = DecodeUnsignedLeb128(&data); |
| 1384 | std::unique_ptr<DebugTag> tag(new DebugTag(index)); |
| 1385 | tag->size_ = static_cast<uint32_t>( |
| 1386 | reinterpret_cast<uintptr_t>(data) - reinterpret_cast<uintptr_t>(*data_pointer)); |
| 1387 | // skip the abbrev |
| 1388 | tag->tag_ = DecodeUnsignedLeb128(&data); |
| 1389 | tag->has_child_ = (*data == 0); |
| 1390 | data++; |
| 1391 | while (true) { |
| 1392 | uint32_t attr = DecodeUnsignedLeb128(&data); |
| 1393 | uint32_t form = DecodeUnsignedLeb128(&data); |
| 1394 | if (attr == 0 && form == 0) { |
| 1395 | break; |
| 1396 | } else if (attr == 0 || form == 0) { |
| 1397 | // Bad abbrev. |
| 1398 | return nullptr; |
| 1399 | } |
| 1400 | int32_t size = FormLength(form); |
| 1401 | if (size == -1) { |
| 1402 | return nullptr; |
| 1403 | } |
| 1404 | tag->AddAttribute(attr, static_cast<uint32_t>(size)); |
| 1405 | } |
| 1406 | *data_pointer = data; |
| 1407 | return tag.release(); |
| 1408 | } |
| 1409 | |
| 1410 | uint32_t GetSize() const { |
| 1411 | return size_; |
| 1412 | } |
| 1413 | |
| 1414 | bool HasChild() { |
| 1415 | return has_child_; |
| 1416 | } |
| 1417 | |
| 1418 | uint32_t GetTagNumber() { |
| 1419 | return tag_; |
| 1420 | } |
| 1421 | |
| 1422 | // Gets the offset of a particular attribute in this tag structure. |
| 1423 | // Interpretation of the data is left to the consumer. 0 is returned if the |
| 1424 | // tag does not contain the attribute. |
| 1425 | uint32_t GetOffsetOf(uint32_t dwarf_attribute) const { |
| 1426 | auto it = off_map_.find(dwarf_attribute); |
| 1427 | if (it == off_map_.end()) { |
| 1428 | return 0; |
| 1429 | } else { |
| 1430 | return it->second; |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | // Gets the size of attribute |
| 1435 | uint32_t GetAttrSize(uint32_t dwarf_attribute) const { |
| 1436 | auto it = size_map_.find(dwarf_attribute); |
| 1437 | if (it == size_map_.end()) { |
| 1438 | return 0; |
| 1439 | } else { |
| 1440 | return it->second; |
| 1441 | } |
| 1442 | } |
| 1443 | |
| 1444 | private: |
Andreas Gampe | afa6b8e | 2014-09-12 18:38:24 -0700 | [diff] [blame] | 1445 | 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] | 1446 | void AddAttribute(uint32_t type, uint32_t attr_size) { |
| 1447 | off_map_.insert(std::pair<uint32_t, uint32_t>(type, size_)); |
| 1448 | size_map_.insert(std::pair<uint32_t, uint32_t>(type, attr_size)); |
| 1449 | size_ += attr_size; |
| 1450 | } |
| 1451 | std::map<uint32_t, uint32_t> off_map_; |
| 1452 | std::map<uint32_t, uint32_t> size_map_; |
| 1453 | uint32_t size_; |
| 1454 | uint32_t tag_; |
| 1455 | bool has_child_; |
| 1456 | }; |
| 1457 | |
| 1458 | class DebugAbbrev { |
| 1459 | public: |
| 1460 | ~DebugAbbrev() {} |
| 1461 | static DebugAbbrev* Create(const byte* dbg_abbrev, size_t dbg_abbrev_size) { |
| 1462 | std::unique_ptr<DebugAbbrev> abbrev(new DebugAbbrev); |
| 1463 | const byte* last = dbg_abbrev + dbg_abbrev_size; |
| 1464 | while (dbg_abbrev < last) { |
| 1465 | std::unique_ptr<DebugTag> tag(DebugTag::Create(&dbg_abbrev)); |
| 1466 | if (tag.get() == nullptr) { |
| 1467 | return nullptr; |
| 1468 | } else { |
| 1469 | abbrev->tags_.insert(std::pair<uint32_t, uint32_t>(tag->index_, abbrev->tag_list_.size())); |
| 1470 | abbrev->tag_list_.push_back(std::move(tag)); |
| 1471 | } |
| 1472 | } |
| 1473 | return abbrev.release(); |
| 1474 | } |
| 1475 | |
| 1476 | DebugTag* ReadTag(const byte* entry) { |
| 1477 | uint32_t tag_num = DecodeUnsignedLeb128(&entry); |
| 1478 | auto it = tags_.find(tag_num); |
| 1479 | if (it == tags_.end()) { |
| 1480 | return nullptr; |
| 1481 | } else { |
| 1482 | CHECK_GT(tag_list_.size(), it->second); |
| 1483 | return tag_list_.at(it->second).get(); |
| 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | private: |
| 1488 | DebugAbbrev() {} |
| 1489 | std::map<uint32_t, uint32_t> tags_; |
| 1490 | std::vector<std::unique_ptr<DebugTag>> tag_list_; |
| 1491 | }; |
| 1492 | |
| 1493 | class DebugInfoIterator { |
| 1494 | public: |
| 1495 | static DebugInfoIterator* Create(DebugInfoHeader* header, size_t frame_size, |
| 1496 | DebugAbbrev* abbrev) { |
| 1497 | std::unique_ptr<DebugInfoIterator> iter(new DebugInfoIterator(header, frame_size, abbrev)); |
| 1498 | if (iter->GetCurrentTag() == nullptr) { |
| 1499 | return nullptr; |
| 1500 | } else { |
| 1501 | return iter.release(); |
| 1502 | } |
| 1503 | } |
| 1504 | ~DebugInfoIterator() {} |
| 1505 | |
| 1506 | // Moves to the next DIE. Returns false if at last entry. |
| 1507 | // TODO Handle variable length attributes. |
| 1508 | bool next() { |
| 1509 | if (current_entry_ == nullptr || current_tag_ == nullptr) { |
| 1510 | return false; |
| 1511 | } |
| 1512 | current_entry_ += current_tag_->GetSize(); |
| 1513 | if (current_entry_ >= last_entry_) { |
| 1514 | current_entry_ = nullptr; |
| 1515 | return false; |
| 1516 | } |
| 1517 | current_tag_ = abbrev_->ReadTag(current_entry_); |
| 1518 | if (current_tag_ == nullptr) { |
| 1519 | current_entry_ = nullptr; |
| 1520 | return false; |
| 1521 | } else { |
| 1522 | return true; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | const DebugTag* GetCurrentTag() { |
| 1527 | return const_cast<DebugTag*>(current_tag_); |
| 1528 | } |
| 1529 | byte* GetPointerToField(uint8_t dwarf_field) { |
| 1530 | if (current_tag_ == nullptr || current_entry_ == nullptr || current_entry_ >= last_entry_) { |
| 1531 | return nullptr; |
| 1532 | } |
| 1533 | uint32_t off = current_tag_->GetOffsetOf(dwarf_field); |
| 1534 | if (off == 0) { |
| 1535 | // tag does not have that field. |
| 1536 | return nullptr; |
| 1537 | } else { |
| 1538 | DCHECK_LT(off, current_tag_->GetSize()); |
| 1539 | return current_entry_ + off; |
| 1540 | } |
| 1541 | } |
| 1542 | |
| 1543 | private: |
| 1544 | DebugInfoIterator(DebugInfoHeader* header, size_t frame_size, DebugAbbrev* abbrev) |
| 1545 | : abbrev_(abbrev), |
| 1546 | last_entry_(reinterpret_cast<byte*>(header) + frame_size), |
| 1547 | current_entry_(reinterpret_cast<byte*>(header) + sizeof(DebugInfoHeader)), |
| 1548 | current_tag_(abbrev_->ReadTag(current_entry_)) {} |
| 1549 | DebugAbbrev* abbrev_; |
| 1550 | byte* last_entry_; |
| 1551 | byte* current_entry_; |
| 1552 | DebugTag* current_tag_; |
| 1553 | }; |
| 1554 | |
| 1555 | static bool FixupDebugInfo(uint32_t text_start, DebugInfoIterator* iter) { |
| 1556 | do { |
| 1557 | if (iter->GetCurrentTag()->GetAttrSize(DW_AT_low_pc) != sizeof(int32_t) || |
| 1558 | iter->GetCurrentTag()->GetAttrSize(DW_AT_high_pc) != sizeof(int32_t)) { |
| 1559 | return false; |
| 1560 | } |
| 1561 | uint32_t* PC_low = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_low_pc)); |
| 1562 | uint32_t* PC_high = reinterpret_cast<uint32_t*>(iter->GetPointerToField(DW_AT_high_pc)); |
| 1563 | if (PC_low != nullptr && PC_high != nullptr) { |
| 1564 | *PC_low += text_start; |
| 1565 | *PC_high += text_start; |
| 1566 | } |
| 1567 | } while (iter->next()); |
| 1568 | return true; |
| 1569 | } |
| 1570 | |
| 1571 | static bool FixupDebugSections(const byte* dbg_abbrev, size_t dbg_abbrev_size, |
| 1572 | uintptr_t text_start, |
| 1573 | byte* dbg_info, size_t dbg_info_size, |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1574 | byte* eh_frame, size_t eh_frame_size) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1575 | std::unique_ptr<DebugAbbrev> abbrev(DebugAbbrev::Create(dbg_abbrev, dbg_abbrev_size)); |
| 1576 | if (abbrev.get() == nullptr) { |
| 1577 | return false; |
| 1578 | } |
| 1579 | std::unique_ptr<DebugInfoIterator> iter( |
| 1580 | DebugInfoIterator::Create(reinterpret_cast<DebugInfoHeader*>(dbg_info), |
| 1581 | dbg_info_size, abbrev.get())); |
| 1582 | if (iter.get() == nullptr) { |
| 1583 | return false; |
| 1584 | } |
| 1585 | return FixupDebugInfo(text_start, iter.get()) |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1586 | && FixupEHFrame(text_start, eh_frame, eh_frame_size); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1587 | } |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1588 | |
| 1589 | void ElfFile::GdbJITSupport() { |
| 1590 | // We only get here if we only are mapping the program header. |
| 1591 | DCHECK(program_header_only_); |
| 1592 | |
| 1593 | // Well, we need the whole file to do this. |
| 1594 | std::string error_msg; |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1595 | // Make it MAP_PRIVATE so we can just give it to gdb if all the necessary |
| 1596 | // sections are there. |
| 1597 | std::unique_ptr<ElfFile> all_ptr(Open(const_cast<File*>(file_), PROT_READ | PROT_WRITE, |
| 1598 | MAP_PRIVATE, &error_msg)); |
| 1599 | if (all_ptr.get() == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1600 | return; |
| 1601 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1602 | ElfFile& all = *all_ptr; |
| 1603 | |
| 1604 | // Do we have interesting sections? |
| 1605 | const Elf32_Shdr* debug_info = all.FindSectionByName(".debug_info"); |
| 1606 | const Elf32_Shdr* debug_abbrev = all.FindSectionByName(".debug_abbrev"); |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1607 | const Elf32_Shdr* eh_frame = all.FindSectionByName(".eh_frame"); |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1608 | const Elf32_Shdr* debug_str = all.FindSectionByName(".debug_str"); |
| 1609 | const Elf32_Shdr* strtab_sec = all.FindSectionByName(".strtab"); |
| 1610 | const Elf32_Shdr* symtab_sec = all.FindSectionByName(".symtab"); |
| 1611 | Elf32_Shdr* text_sec = all.FindSectionByName(".text"); |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1612 | if (debug_info == nullptr || debug_abbrev == nullptr || eh_frame == nullptr || |
Jim_Guo | a62a588 | 2014-04-28 11:11:57 +0800 | [diff] [blame] | 1613 | debug_str == nullptr || text_sec == nullptr || strtab_sec == nullptr || |
| 1614 | symtab_sec == nullptr) { |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1615 | return; |
| 1616 | } |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1617 | // We need to add in a strtab and symtab to the image. |
| 1618 | // all is MAP_PRIVATE so it can be written to freely. |
| 1619 | // We also already have strtab and symtab so we are fine there. |
| 1620 | Elf32_Ehdr& elf_hdr = all.GetHeader(); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1621 | elf_hdr.e_entry = 0; |
| 1622 | elf_hdr.e_phoff = 0; |
| 1623 | elf_hdr.e_phnum = 0; |
| 1624 | elf_hdr.e_phentsize = 0; |
| 1625 | elf_hdr.e_type = ET_EXEC; |
| 1626 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1627 | text_sec->sh_type = SHT_NOBITS; |
| 1628 | text_sec->sh_offset = 0; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1629 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1630 | if (!FixupDebugSections( |
| 1631 | all.Begin() + debug_abbrev->sh_offset, debug_abbrev->sh_size, text_sec->sh_addr, |
| 1632 | all.Begin() + debug_info->sh_offset, debug_info->sh_size, |
Tong Shen | 35e1e6a | 2014-07-30 09:31:22 -0700 | [diff] [blame] | 1633 | all.Begin() + eh_frame->sh_offset, eh_frame->sh_size)) { |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1634 | LOG(ERROR) << "Failed to load GDB data"; |
| 1635 | return; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1636 | } |
| 1637 | |
Alex Light | 3470ab4 | 2014-06-18 10:35:45 -0700 | [diff] [blame] | 1638 | jit_gdb_entry_ = CreateCodeEntry(all.Begin(), all.Size()); |
| 1639 | gdb_file_mapping_.reset(all_ptr.release()); |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame] | 1640 | } |
| 1641 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1642 | } // namespace art |