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 | |
| 19 | #include "base/logging.h" |
| 20 | #include "base/stl_util.h" |
| 21 | #include "utils.h" |
| 22 | |
| 23 | namespace art { |
| 24 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame^] | 25 | // ------------------------------------------------------------------- |
| 26 | // Binary GDB JIT Interface as described in |
| 27 | // http://sourceware.org/gdb/onlinedocs/gdb/Declarations.html |
| 28 | extern "C" { |
| 29 | typedef enum { |
| 30 | JIT_NOACTION = 0, |
| 31 | JIT_REGISTER_FN, |
| 32 | JIT_UNREGISTER_FN |
| 33 | } JITAction; |
| 34 | |
| 35 | struct JITCodeEntry { |
| 36 | JITCodeEntry* next_; |
| 37 | JITCodeEntry* prev_; |
| 38 | const byte *symfile_addr_; |
| 39 | uint64_t symfile_size_; |
| 40 | }; |
| 41 | |
| 42 | struct JITDescriptor { |
| 43 | uint32_t version_; |
| 44 | uint32_t action_flag_; |
| 45 | JITCodeEntry* relevant_entry_; |
| 46 | JITCodeEntry* first_entry_; |
| 47 | }; |
| 48 | |
| 49 | // GDB will place breakpoint into this function. |
| 50 | // To prevent GCC from inlining or removing it we place noinline attribute |
| 51 | // and inline assembler statement inside. |
| 52 | void __attribute__((noinline)) __jit_debug_register_code() { |
| 53 | __asm__(""); |
| 54 | } |
| 55 | |
| 56 | // GDB will inspect contents of this descriptor. |
| 57 | // Static initialization is necessary to prevent GDB from seeing |
| 58 | // uninitialized descriptor. |
| 59 | JITDescriptor __jit_debug_descriptor = { 1, JIT_NOACTION, nullptr, nullptr }; |
| 60 | } |
| 61 | |
| 62 | |
| 63 | static JITCodeEntry* CreateCodeEntry(const byte *symfile_addr, |
| 64 | uintptr_t symfile_size) { |
| 65 | JITCodeEntry* entry = new JITCodeEntry; |
| 66 | entry->symfile_addr_ = symfile_addr; |
| 67 | entry->symfile_size_ = symfile_size; |
| 68 | entry->prev_ = nullptr; |
| 69 | |
| 70 | // TODO: Do we need a lock here? |
| 71 | entry->next_ = __jit_debug_descriptor.first_entry_; |
| 72 | if (entry->next_ != nullptr) { |
| 73 | entry->next_->prev_ = entry; |
| 74 | } |
| 75 | __jit_debug_descriptor.first_entry_ = entry; |
| 76 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 77 | |
| 78 | __jit_debug_descriptor.action_flag_ = JIT_REGISTER_FN; |
| 79 | __jit_debug_register_code(); |
| 80 | return entry; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | static void UnregisterCodeEntry(JITCodeEntry* entry) { |
| 85 | // TODO: Do we need a lock here? |
| 86 | if (entry->prev_ != nullptr) { |
| 87 | entry->prev_->next_ = entry->next_; |
| 88 | } else { |
| 89 | __jit_debug_descriptor.first_entry_ = entry->next_; |
| 90 | } |
| 91 | |
| 92 | if (entry->next_ != nullptr) { |
| 93 | entry->next_->prev_ = entry->prev_; |
| 94 | } |
| 95 | |
| 96 | __jit_debug_descriptor.relevant_entry_ = entry; |
| 97 | __jit_debug_descriptor.action_flag_ = JIT_UNREGISTER_FN; |
| 98 | __jit_debug_register_code(); |
| 99 | delete entry; |
| 100 | } |
| 101 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 102 | ElfFile::ElfFile(File* file, bool writable, bool program_header_only) |
| 103 | : file_(file), |
| 104 | writable_(writable), |
| 105 | program_header_only_(program_header_only), |
Brian Carlstrom | 02c8cc6 | 2013-07-18 15:54:44 -0700 | [diff] [blame] | 106 | header_(NULL), |
| 107 | base_address_(NULL), |
| 108 | program_headers_start_(NULL), |
| 109 | section_headers_start_(NULL), |
| 110 | dynamic_program_header_(NULL), |
| 111 | dynamic_section_start_(NULL), |
| 112 | symtab_section_start_(NULL), |
| 113 | dynsym_section_start_(NULL), |
| 114 | strtab_section_start_(NULL), |
| 115 | dynstr_section_start_(NULL), |
| 116 | hash_section_start_(NULL), |
| 117 | symtab_symbol_table_(NULL), |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame^] | 118 | dynsym_symbol_table_(NULL), |
| 119 | jit_elf_image_(NULL), |
| 120 | jit_gdb_entry_(NULL) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 121 | CHECK(file != NULL); |
| 122 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 123 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 124 | ElfFile* ElfFile::Open(File* file, bool writable, bool program_header_only, |
| 125 | std::string* error_msg) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 126 | UniquePtr<ElfFile> elf_file(new ElfFile(file, writable, program_header_only)); |
| 127 | if (!elf_file->Setup(error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 128 | return nullptr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 129 | } |
| 130 | return elf_file.release(); |
| 131 | } |
| 132 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 133 | bool ElfFile::Setup(std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 134 | int prot; |
| 135 | int flags; |
| 136 | if (writable_) { |
| 137 | prot = PROT_READ | PROT_WRITE; |
| 138 | flags = MAP_SHARED; |
| 139 | } else { |
| 140 | prot = PROT_READ; |
| 141 | flags = MAP_PRIVATE; |
| 142 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 143 | int64_t temp_file_length = file_->GetLength(); |
| 144 | if (temp_file_length < 0) { |
| 145 | errno = -temp_file_length; |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 146 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 147 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 148 | return false; |
| 149 | } |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 150 | size_t file_length = static_cast<size_t>(temp_file_length); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 151 | if (file_length < sizeof(Elf32_Ehdr)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 152 | *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] | 153 | "%zd bytes: '%s'", file_length, sizeof(Elf32_Ehdr), |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 154 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 155 | return false; |
| 156 | } |
| 157 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 158 | if (program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 159 | // first just map ELF header to get program header size information |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 160 | size_t elf_header_size = sizeof(Elf32_Ehdr); |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 161 | if (!SetMap(MemMap::MapFile(elf_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 162 | file_->GetPath().c_str(), error_msg), |
| 163 | error_msg)) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 164 | return false; |
| 165 | } |
| 166 | // then remap to cover program header |
| 167 | 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] | 168 | if (file_length < program_header_size) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 169 | *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] | 170 | "header of %zd bytes: '%s'", file_length, |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 171 | sizeof(Elf32_Ehdr), file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 172 | return false; |
| 173 | } |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 174 | if (!SetMap(MemMap::MapFile(program_header_size, prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 175 | file_->GetPath().c_str(), error_msg), |
| 176 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 177 | *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] | 178 | return false; |
| 179 | } |
| 180 | } else { |
| 181 | // otherwise map entire file |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 182 | if (!SetMap(MemMap::MapFile(file_->GetLength(), prot, flags, file_->Fd(), 0, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 183 | file_->GetPath().c_str(), error_msg), |
| 184 | error_msg)) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 185 | *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] | 186 | return false; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Either way, the program header is relative to the elf header |
| 191 | program_headers_start_ = Begin() + GetHeader().e_phoff; |
| 192 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 193 | if (!program_header_only_) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 194 | // Setup section headers. |
| 195 | section_headers_start_ = Begin() + GetHeader().e_shoff; |
| 196 | |
| 197 | // Find .dynamic section info from program header |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 198 | dynamic_program_header_ = FindProgamHeaderByType(PT_DYNAMIC); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 199 | if (dynamic_program_header_ == NULL) { |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 200 | *error_msg = StringPrintf("Failed to find PT_DYNAMIC program header in ELF file: '%s'", |
| 201 | file_->GetPath().c_str()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 202 | return false; |
| 203 | } |
| 204 | |
| 205 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 206 | = reinterpret_cast<Elf32_Dyn*>(Begin() + GetDynamicProgramHeader().p_offset); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 207 | |
| 208 | // Find other sections from section headers |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 209 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 210 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 211 | byte* section_addr = Begin() + section_header.sh_offset; |
| 212 | switch (section_header.sh_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 213 | case SHT_SYMTAB: { |
| 214 | symtab_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 215 | break; |
| 216 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 217 | case SHT_DYNSYM: { |
| 218 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 219 | break; |
| 220 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 221 | case SHT_STRTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 222 | // TODO: base these off of sh_link from .symtab and .dynsym above |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 223 | if ((section_header.sh_flags & SHF_ALLOC) != 0) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 224 | dynstr_section_start_ = reinterpret_cast<char*>(section_addr); |
| 225 | } else { |
| 226 | strtab_section_start_ = reinterpret_cast<char*>(section_addr); |
| 227 | } |
| 228 | break; |
| 229 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 230 | case SHT_DYNAMIC: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 231 | if (reinterpret_cast<byte*>(dynamic_section_start_) != section_addr) { |
| 232 | LOG(WARNING) << "Failed to find matching SHT_DYNAMIC for PT_DYNAMIC in " |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 233 | << file_->GetPath() << ": " << std::hex |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 234 | << reinterpret_cast<void*>(dynamic_section_start_) |
| 235 | << " != " << reinterpret_cast<void*>(section_addr); |
| 236 | return false; |
| 237 | } |
| 238 | break; |
| 239 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 240 | case SHT_HASH: { |
| 241 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(section_addr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | return true; |
| 248 | } |
| 249 | |
| 250 | ElfFile::~ElfFile() { |
| 251 | STLDeleteElements(&segments_); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 252 | delete symtab_symbol_table_; |
| 253 | delete dynsym_symbol_table_; |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame^] | 254 | delete jit_elf_image_; |
| 255 | if (jit_gdb_entry_) { |
| 256 | UnregisterCodeEntry(jit_gdb_entry_); |
| 257 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 260 | bool ElfFile::SetMap(MemMap* map, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 261 | if (map == NULL) { |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 262 | // MemMap::Open should have already set an error. |
| 263 | DCHECK(!error_msg->empty()); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 264 | return false; |
| 265 | } |
| 266 | map_.reset(map); |
| 267 | CHECK(map_.get() != NULL) << file_->GetPath(); |
| 268 | CHECK(map_->Begin() != NULL) << file_->GetPath(); |
| 269 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 270 | header_ = reinterpret_cast<Elf32_Ehdr*>(map_->Begin()); |
| 271 | if ((ELFMAG0 != header_->e_ident[EI_MAG0]) |
| 272 | || (ELFMAG1 != header_->e_ident[EI_MAG1]) |
| 273 | || (ELFMAG2 != header_->e_ident[EI_MAG2]) |
| 274 | || (ELFMAG3 != header_->e_ident[EI_MAG3])) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 275 | *error_msg = StringPrintf("Failed to find ELF magic value %d %d %d %d in %s, found %d %d %d %d", |
| 276 | ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, |
Brian Carlstrom | d0c09dc | 2013-11-06 18:25:35 -0800 | [diff] [blame] | 277 | file_->GetPath().c_str(), |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 278 | header_->e_ident[EI_MAG0], |
| 279 | header_->e_ident[EI_MAG1], |
| 280 | header_->e_ident[EI_MAG2], |
| 281 | header_->e_ident[EI_MAG3]); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 282 | return false; |
| 283 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 284 | if (ELFCLASS32 != header_->e_ident[EI_CLASS]) { |
| 285 | *error_msg = StringPrintf("Failed to find expected EI_CLASS value %d in %s, found %d", |
| 286 | ELFCLASS32, |
| 287 | file_->GetPath().c_str(), |
| 288 | header_->e_ident[EI_CLASS]); |
| 289 | return false; |
| 290 | } |
| 291 | if (ELFDATA2LSB != header_->e_ident[EI_DATA]) { |
| 292 | *error_msg = StringPrintf("Failed to find expected EI_DATA value %d in %s, found %d", |
| 293 | ELFDATA2LSB, |
| 294 | file_->GetPath().c_str(), |
| 295 | header_->e_ident[EI_CLASS]); |
| 296 | return false; |
| 297 | } |
| 298 | if (EV_CURRENT != header_->e_ident[EI_VERSION]) { |
| 299 | *error_msg = StringPrintf("Failed to find expected EI_VERSION value %d in %s, found %d", |
| 300 | EV_CURRENT, |
| 301 | file_->GetPath().c_str(), |
| 302 | header_->e_ident[EI_CLASS]); |
| 303 | return false; |
| 304 | } |
| 305 | if (ET_DYN != header_->e_type) { |
| 306 | *error_msg = StringPrintf("Failed to find expected e_type value %d in %s, found %d", |
| 307 | ET_DYN, |
| 308 | file_->GetPath().c_str(), |
| 309 | header_->e_type); |
| 310 | return false; |
| 311 | } |
| 312 | if (EV_CURRENT != header_->e_version) { |
| 313 | *error_msg = StringPrintf("Failed to find expected e_version value %d in %s, found %d", |
| 314 | EV_CURRENT, |
| 315 | file_->GetPath().c_str(), |
| 316 | header_->e_version); |
| 317 | return false; |
| 318 | } |
| 319 | if (0 != header_->e_entry) { |
| 320 | *error_msg = StringPrintf("Failed to find expected e_entry value %d in %s, found %d", |
| 321 | 0, |
| 322 | file_->GetPath().c_str(), |
| 323 | header_->e_entry); |
| 324 | return false; |
| 325 | } |
| 326 | if (0 == header_->e_phoff) { |
| 327 | *error_msg = StringPrintf("Failed to find non-zero e_phoff value in %s", |
| 328 | file_->GetPath().c_str()); |
| 329 | return false; |
| 330 | } |
| 331 | if (0 == header_->e_shoff) { |
| 332 | *error_msg = StringPrintf("Failed to find non-zero e_shoff value in %s", |
| 333 | file_->GetPath().c_str()); |
| 334 | return false; |
| 335 | } |
| 336 | if (0 == header_->e_ehsize) { |
| 337 | *error_msg = StringPrintf("Failed to find non-zero e_ehsize value in %s", |
| 338 | file_->GetPath().c_str()); |
| 339 | return false; |
| 340 | } |
| 341 | if (0 == header_->e_phentsize) { |
| 342 | *error_msg = StringPrintf("Failed to find non-zero e_phentsize value in %s", |
| 343 | file_->GetPath().c_str()); |
| 344 | return false; |
| 345 | } |
| 346 | if (0 == header_->e_phnum) { |
| 347 | *error_msg = StringPrintf("Failed to find non-zero e_phnum value in %s", |
| 348 | file_->GetPath().c_str()); |
| 349 | return false; |
| 350 | } |
| 351 | if (0 == header_->e_shentsize) { |
| 352 | *error_msg = StringPrintf("Failed to find non-zero e_shentsize value in %s", |
| 353 | file_->GetPath().c_str()); |
| 354 | return false; |
| 355 | } |
| 356 | if (0 == header_->e_shnum) { |
| 357 | *error_msg = StringPrintf("Failed to find non-zero e_shnum value in %s", |
| 358 | file_->GetPath().c_str()); |
| 359 | return false; |
| 360 | } |
| 361 | if (0 == header_->e_shstrndx) { |
| 362 | *error_msg = StringPrintf("Failed to find non-zero e_shstrndx value in %s", |
| 363 | file_->GetPath().c_str()); |
| 364 | return false; |
| 365 | } |
| 366 | if (header_->e_shstrndx >= header_->e_shnum) { |
| 367 | *error_msg = StringPrintf("Failed to find e_shnum value %d less than %d in %s", |
| 368 | header_->e_shstrndx, |
| 369 | header_->e_shnum, |
| 370 | file_->GetPath().c_str()); |
| 371 | return false; |
| 372 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 373 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 374 | if (!program_header_only_) { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 375 | if (header_->e_phoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 376 | *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] | 377 | header_->e_phoff, |
| 378 | Size(), |
| 379 | file_->GetPath().c_str()); |
| 380 | return false; |
| 381 | } |
| 382 | if (header_->e_shoff >= Size()) { |
Dmitry Petrochenko | 659d87d | 2014-02-27 14:23:11 +0700 | [diff] [blame] | 383 | *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] | 384 | header_->e_shoff, |
| 385 | Size(), |
| 386 | file_->GetPath().c_str()); |
| 387 | return false; |
| 388 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 389 | } |
| 390 | return true; |
| 391 | } |
| 392 | |
| 393 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 394 | Elf32_Ehdr& ElfFile::GetHeader() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 395 | CHECK(header_ != NULL); |
| 396 | return *header_; |
| 397 | } |
| 398 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 399 | byte* ElfFile::GetProgramHeadersStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 400 | CHECK(program_headers_start_ != NULL); |
| 401 | return program_headers_start_; |
| 402 | } |
| 403 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 404 | byte* ElfFile::GetSectionHeadersStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 405 | CHECK(section_headers_start_ != NULL); |
| 406 | return section_headers_start_; |
| 407 | } |
| 408 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 409 | Elf32_Phdr& ElfFile::GetDynamicProgramHeader() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 410 | CHECK(dynamic_program_header_ != NULL); |
| 411 | return *dynamic_program_header_; |
| 412 | } |
| 413 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 414 | Elf32_Dyn* ElfFile::GetDynamicSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 415 | CHECK(dynamic_section_start_ != NULL); |
| 416 | return dynamic_section_start_; |
| 417 | } |
| 418 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 419 | Elf32_Sym* ElfFile::GetSymbolSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 420 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 421 | Elf32_Sym* symbol_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 422 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 423 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 424 | symbol_section_start = symtab_section_start_; |
| 425 | break; |
| 426 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 427 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 428 | symbol_section_start = dynsym_section_start_; |
| 429 | break; |
| 430 | } |
| 431 | default: { |
| 432 | LOG(FATAL) << section_type; |
| 433 | symbol_section_start = NULL; |
| 434 | } |
| 435 | } |
| 436 | CHECK(symbol_section_start != NULL); |
| 437 | return symbol_section_start; |
| 438 | } |
| 439 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 440 | const char* ElfFile::GetStringSectionStart(Elf32_Word section_type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 441 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 442 | const char* string_section_start; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 443 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 444 | case SHT_SYMTAB: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 445 | string_section_start = strtab_section_start_; |
| 446 | break; |
| 447 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 448 | case SHT_DYNSYM: { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 449 | string_section_start = dynstr_section_start_; |
| 450 | break; |
| 451 | } |
| 452 | default: { |
| 453 | LOG(FATAL) << section_type; |
| 454 | string_section_start = NULL; |
| 455 | } |
| 456 | } |
| 457 | CHECK(string_section_start != NULL); |
| 458 | return string_section_start; |
| 459 | } |
| 460 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 461 | const char* ElfFile::GetString(Elf32_Word section_type, Elf32_Word i) const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 462 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 463 | if (i == 0) { |
| 464 | return NULL; |
| 465 | } |
| 466 | const char* string_section_start = GetStringSectionStart(section_type); |
| 467 | const char* string = string_section_start + i; |
| 468 | return string; |
| 469 | } |
| 470 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 471 | Elf32_Word* ElfFile::GetHashSectionStart() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 472 | CHECK(hash_section_start_ != NULL); |
| 473 | return hash_section_start_; |
| 474 | } |
| 475 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 476 | Elf32_Word ElfFile::GetHashBucketNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 477 | return GetHashSectionStart()[0]; |
| 478 | } |
| 479 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 480 | Elf32_Word ElfFile::GetHashChainNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 481 | return GetHashSectionStart()[1]; |
| 482 | } |
| 483 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 484 | Elf32_Word ElfFile::GetHashBucket(size_t i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 485 | CHECK_LT(i, GetHashBucketNum()); |
| 486 | // 0 is nbucket, 1 is nchain |
| 487 | return GetHashSectionStart()[2 + i]; |
| 488 | } |
| 489 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 490 | Elf32_Word ElfFile::GetHashChain(size_t i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 491 | CHECK_LT(i, GetHashChainNum()); |
| 492 | // 0 is nbucket, 1 is nchain, & chains are after buckets |
| 493 | return GetHashSectionStart()[2 + GetHashBucketNum() + i]; |
| 494 | } |
| 495 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 496 | Elf32_Word ElfFile::GetProgramHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 497 | return GetHeader().e_phnum; |
| 498 | } |
| 499 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 500 | Elf32_Phdr& ElfFile::GetProgramHeader(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 501 | CHECK_LT(i, GetProgramHeaderNum()) << file_->GetPath(); |
| 502 | byte* program_header = GetProgramHeadersStart() + (i * GetHeader().e_phentsize); |
| 503 | CHECK_LT(program_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 504 | return *reinterpret_cast<Elf32_Phdr*>(program_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 505 | } |
| 506 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 507 | Elf32_Phdr* ElfFile::FindProgamHeaderByType(Elf32_Word type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 508 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 509 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 510 | if (program_header.p_type == type) { |
| 511 | return &program_header; |
| 512 | } |
| 513 | } |
| 514 | return NULL; |
| 515 | } |
| 516 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 517 | Elf32_Word ElfFile::GetSectionHeaderNum() const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 518 | return GetHeader().e_shnum; |
| 519 | } |
| 520 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 521 | Elf32_Shdr& ElfFile::GetSectionHeader(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 522 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 523 | // Even if we Load(), it doesn't bring in all the sections. |
| 524 | CHECK(!program_header_only_) << file_->GetPath(); |
| 525 | CHECK_LT(i, GetSectionHeaderNum()) << file_->GetPath(); |
| 526 | byte* section_header = GetSectionHeadersStart() + (i * GetHeader().e_shentsize); |
| 527 | CHECK_LT(section_header, End()) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 528 | return *reinterpret_cast<Elf32_Shdr*>(section_header); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 531 | Elf32_Shdr* ElfFile::FindSectionByType(Elf32_Word type) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 532 | // Can only access arbitrary sections when we have the whole file, not just program header. |
| 533 | // We could change this to switch on known types if they were detected during loading. |
| 534 | CHECK(!program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 535 | for (Elf32_Word i = 0; i < GetSectionHeaderNum(); i++) { |
| 536 | Elf32_Shdr& section_header = GetSectionHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 537 | if (section_header.sh_type == type) { |
| 538 | return §ion_header; |
| 539 | } |
| 540 | } |
| 541 | return NULL; |
| 542 | } |
| 543 | |
| 544 | // from bionic |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 545 | static unsigned elfhash(const char *_name) { |
| 546 | const unsigned char *name = (const unsigned char *) _name; |
| 547 | unsigned h = 0, g; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 548 | |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 549 | while (*name) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 550 | h = (h << 4) + *name++; |
| 551 | g = h & 0xf0000000; |
| 552 | h ^= g; |
| 553 | h ^= g >> 24; |
| 554 | } |
| 555 | return h; |
| 556 | } |
| 557 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 558 | Elf32_Shdr& ElfFile::GetSectionNameStringSection() const { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 559 | return GetSectionHeader(GetHeader().e_shstrndx); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 562 | const byte* ElfFile::FindDynamicSymbolAddress(const std::string& symbol_name) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 563 | Elf32_Word hash = elfhash(symbol_name.c_str()); |
| 564 | Elf32_Word bucket_index = hash % GetHashBucketNum(); |
| 565 | Elf32_Word symbol_and_chain_index = GetHashBucket(bucket_index); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 566 | while (symbol_and_chain_index != 0 /* STN_UNDEF */) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 567 | Elf32_Sym& symbol = GetSymbol(SHT_DYNSYM, symbol_and_chain_index); |
| 568 | const char* name = GetString(SHT_DYNSYM, symbol.st_name); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 569 | if (symbol_name == name) { |
| 570 | return base_address_ + symbol.st_value; |
| 571 | } |
| 572 | symbol_and_chain_index = GetHashChain(symbol_and_chain_index); |
| 573 | } |
| 574 | return NULL; |
| 575 | } |
| 576 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 577 | bool ElfFile::IsSymbolSectionType(Elf32_Word section_type) { |
| 578 | return ((section_type == SHT_SYMTAB) || (section_type == SHT_DYNSYM)); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 579 | } |
| 580 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 581 | Elf32_Word ElfFile::GetSymbolNum(Elf32_Shdr& section_header) const { |
| 582 | CHECK(IsSymbolSectionType(section_header.sh_type)) |
| 583 | << file_->GetPath() << " " << section_header.sh_type; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 584 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 585 | return section_header.sh_size / section_header.sh_entsize; |
| 586 | } |
| 587 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 588 | Elf32_Sym& ElfFile::GetSymbol(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 589 | Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 590 | return *(GetSymbolSectionStart(section_type) + i); |
| 591 | } |
| 592 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 593 | ElfFile::SymbolTable** ElfFile::GetSymbolTable(Elf32_Word section_type) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 594 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
| 595 | switch (section_type) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 596 | case SHT_SYMTAB: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 597 | return &symtab_symbol_table_; |
| 598 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 599 | case SHT_DYNSYM: { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 600 | return &dynsym_symbol_table_; |
| 601 | } |
| 602 | default: { |
| 603 | LOG(FATAL) << section_type; |
| 604 | return NULL; |
| 605 | } |
| 606 | } |
| 607 | } |
| 608 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 609 | Elf32_Sym* ElfFile::FindSymbolByName(Elf32_Word section_type, |
| 610 | const std::string& symbol_name, |
| 611 | bool build_map) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 612 | CHECK(!program_header_only_) << file_->GetPath(); |
| 613 | CHECK(IsSymbolSectionType(section_type)) << file_->GetPath() << " " << section_type; |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 614 | |
| 615 | SymbolTable** symbol_table = GetSymbolTable(section_type); |
| 616 | if (*symbol_table != NULL || build_map) { |
| 617 | if (*symbol_table == NULL) { |
| 618 | DCHECK(build_map); |
| 619 | *symbol_table = new SymbolTable; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 620 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 621 | CHECK(symbol_section != NULL) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 622 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 623 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 624 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
| 625 | unsigned char type = ELF32_ST_TYPE(symbol.st_info); |
| 626 | if (type == STT_NOTYPE) { |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 627 | continue; |
| 628 | } |
| 629 | const char* name = GetString(string_section, symbol.st_name); |
| 630 | if (name == NULL) { |
| 631 | continue; |
| 632 | } |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 633 | std::pair<SymbolTable::iterator, bool> result = |
| 634 | (*symbol_table)->insert(std::make_pair(name, &symbol)); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 635 | if (!result.second) { |
| 636 | // If a duplicate, make sure it has the same logical value. Seen on x86. |
| 637 | CHECK_EQ(symbol.st_value, result.first->second->st_value); |
| 638 | CHECK_EQ(symbol.st_size, result.first->second->st_size); |
| 639 | CHECK_EQ(symbol.st_info, result.first->second->st_info); |
| 640 | CHECK_EQ(symbol.st_other, result.first->second->st_other); |
| 641 | CHECK_EQ(symbol.st_shndx, result.first->second->st_shndx); |
| 642 | } |
| 643 | } |
| 644 | } |
| 645 | CHECK(*symbol_table != NULL); |
| 646 | SymbolTable::const_iterator it = (*symbol_table)->find(symbol_name); |
| 647 | if (it == (*symbol_table)->end()) { |
| 648 | return NULL; |
| 649 | } |
| 650 | return it->second; |
| 651 | } |
| 652 | |
| 653 | // Fall back to linear search |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 654 | Elf32_Shdr* symbol_section = FindSectionByType(section_type); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 655 | CHECK(symbol_section != NULL) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 656 | Elf32_Shdr& string_section = GetSectionHeader(symbol_section->sh_link); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 657 | for (uint32_t i = 0; i < GetSymbolNum(*symbol_section); i++) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 658 | Elf32_Sym& symbol = GetSymbol(section_type, i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 659 | const char* name = GetString(string_section, symbol.st_name); |
| 660 | if (name == NULL) { |
| 661 | continue; |
| 662 | } |
| 663 | if (symbol_name == name) { |
| 664 | return &symbol; |
| 665 | } |
| 666 | } |
| 667 | return NULL; |
| 668 | } |
| 669 | |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 670 | Elf32_Addr ElfFile::FindSymbolAddress(Elf32_Word section_type, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 671 | const std::string& symbol_name, |
| 672 | bool build_map) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 673 | Elf32_Sym* symbol = FindSymbolByName(section_type, symbol_name, build_map); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 674 | if (symbol == NULL) { |
| 675 | return 0; |
| 676 | } |
| 677 | return symbol->st_value; |
| 678 | } |
| 679 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 680 | const char* ElfFile::GetString(Elf32_Shdr& string_section, Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 681 | CHECK(!program_header_only_) << file_->GetPath(); |
| 682 | // TODO: remove this static_cast from enum when using -std=gnu++0x |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 683 | CHECK_EQ(static_cast<Elf32_Word>(SHT_STRTAB), string_section.sh_type) << file_->GetPath(); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 684 | CHECK_LT(i, string_section.sh_size) << file_->GetPath(); |
| 685 | if (i == 0) { |
| 686 | return NULL; |
| 687 | } |
| 688 | byte* strings = Begin() + string_section.sh_offset; |
| 689 | byte* string = strings + i; |
| 690 | CHECK_LT(string, End()) << file_->GetPath(); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 691 | return reinterpret_cast<const char*>(string); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 694 | Elf32_Word ElfFile::GetDynamicNum() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 695 | return GetDynamicProgramHeader().p_filesz / sizeof(Elf32_Dyn); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 696 | } |
| 697 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 698 | Elf32_Dyn& ElfFile::GetDynamic(Elf32_Word i) const { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 699 | CHECK_LT(i, GetDynamicNum()) << file_->GetPath(); |
| 700 | return *(GetDynamicSectionStart() + i); |
| 701 | } |
| 702 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 703 | Elf32_Word ElfFile::FindDynamicValueByType(Elf32_Sword type) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 704 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 705 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 706 | if (elf_dyn.d_tag == type) { |
| 707 | return elf_dyn.d_un.d_val; |
| 708 | } |
| 709 | } |
| 710 | return 0; |
| 711 | } |
| 712 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 713 | Elf32_Rel* ElfFile::GetRelSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 714 | CHECK(SHT_REL == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 715 | return reinterpret_cast<Elf32_Rel*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 718 | Elf32_Word ElfFile::GetRelNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 719 | 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] | 720 | CHECK_NE(0U, section_header.sh_entsize) << file_->GetPath(); |
| 721 | return section_header.sh_size / section_header.sh_entsize; |
| 722 | } |
| 723 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 724 | Elf32_Rel& ElfFile::GetRel(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 725 | 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] | 726 | CHECK_LT(i, GetRelNum(section_header)) << file_->GetPath(); |
| 727 | return *(GetRelSectionStart(section_header) + i); |
| 728 | } |
| 729 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 730 | Elf32_Rela* ElfFile::GetRelaSectionStart(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 731 | CHECK(SHT_RELA == section_header.sh_type) << file_->GetPath() << " " << section_header.sh_type; |
| 732 | return reinterpret_cast<Elf32_Rela*>(Begin() + section_header.sh_offset); |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 733 | } |
| 734 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 735 | Elf32_Word ElfFile::GetRelaNum(Elf32_Shdr& section_header) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 736 | 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] | 737 | return section_header.sh_size / section_header.sh_entsize; |
| 738 | } |
| 739 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 740 | Elf32_Rela& ElfFile::GetRela(Elf32_Shdr& section_header, Elf32_Word i) const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 741 | 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] | 742 | CHECK_LT(i, GetRelaNum(section_header)) << file_->GetPath(); |
| 743 | return *(GetRelaSectionStart(section_header) + i); |
| 744 | } |
| 745 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 746 | // Base on bionic phdr_table_get_load_size |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 747 | size_t ElfFile::GetLoadedSize() const { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 748 | Elf32_Addr min_vaddr = 0xFFFFFFFFu; |
| 749 | Elf32_Addr max_vaddr = 0x00000000u; |
| 750 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 751 | Elf32_Phdr& program_header = GetProgramHeader(i); |
| 752 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 753 | continue; |
| 754 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 755 | Elf32_Addr begin_vaddr = program_header.p_vaddr; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 756 | if (begin_vaddr < min_vaddr) { |
| 757 | min_vaddr = begin_vaddr; |
| 758 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 759 | Elf32_Addr end_vaddr = program_header.p_vaddr + program_header.p_memsz; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 760 | if (end_vaddr > max_vaddr) { |
| 761 | max_vaddr = end_vaddr; |
| 762 | } |
| 763 | } |
| 764 | min_vaddr = RoundDown(min_vaddr, kPageSize); |
| 765 | max_vaddr = RoundUp(max_vaddr, kPageSize); |
| 766 | CHECK_LT(min_vaddr, max_vaddr) << file_->GetPath(); |
| 767 | size_t loaded_size = max_vaddr - min_vaddr; |
| 768 | return loaded_size; |
| 769 | } |
| 770 | |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 771 | bool ElfFile::Load(bool executable, std::string* error_msg) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 772 | CHECK(program_header_only_) << file_->GetPath(); |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 773 | for (Elf32_Word i = 0; i < GetProgramHeaderNum(); i++) { |
| 774 | Elf32_Phdr& program_header = GetProgramHeader(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 775 | |
| 776 | // Record .dynamic header information for later use |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 777 | if (program_header.p_type == PT_DYNAMIC) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 778 | dynamic_program_header_ = &program_header; |
| 779 | continue; |
| 780 | } |
| 781 | |
| 782 | // Not something to load, move on. |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 783 | if (program_header.p_type != PT_LOAD) { |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 784 | continue; |
| 785 | } |
| 786 | |
| 787 | // Found something to load. |
| 788 | |
| 789 | // If p_vaddr is zero, it must be the first loadable segment, |
| 790 | // since they must be in order. Since it is zero, there isn't a |
| 791 | // specific address requested, so first request a contiguous chunk |
| 792 | // of required size for all segments, but with no |
| 793 | // permissions. We'll then carve that up with the proper |
| 794 | // permissions as we load the actual segments. If p_vaddr is |
| 795 | // non-zero, the segments require the specific address specified, |
| 796 | // which either was specified in the file because we already set |
| 797 | // base_address_ after the first zero segment). |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 798 | int64_t temp_file_length = file_->GetLength(); |
| 799 | if (temp_file_length < 0) { |
| 800 | errno = -temp_file_length; |
| 801 | *error_msg = StringPrintf("Failed to get length of file: '%s' fd=%d: %s", |
| 802 | file_->GetPath().c_str(), file_->Fd(), strerror(errno)); |
| 803 | return false; |
| 804 | } |
| 805 | size_t file_length = static_cast<size_t>(temp_file_length); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 806 | if (program_header.p_vaddr == 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 807 | std::string reservation_name("ElfFile reservation for "); |
| 808 | reservation_name += file_->GetPath(); |
| 809 | UniquePtr<MemMap> reserve(MemMap::MapAnonymous(reservation_name.c_str(), |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 810 | NULL, GetLoadedSize(), PROT_NONE, false, |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 811 | error_msg)); |
| 812 | if (reserve.get() == nullptr) { |
| 813 | *error_msg = StringPrintf("Failed to allocate %s: %s", |
| 814 | reservation_name.c_str(), error_msg->c_str()); |
| 815 | return false; |
| 816 | } |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 817 | base_address_ = reserve->Begin(); |
| 818 | segments_.push_back(reserve.release()); |
| 819 | } |
| 820 | // empty segment, nothing to map |
| 821 | if (program_header.p_memsz == 0) { |
| 822 | continue; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 823 | } |
| 824 | byte* p_vaddr = base_address_ + program_header.p_vaddr; |
| 825 | int prot = 0; |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 826 | if (executable && ((program_header.p_flags & PF_X) != 0)) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 827 | prot |= PROT_EXEC; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 828 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 829 | if ((program_header.p_flags & PF_W) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 830 | prot |= PROT_WRITE; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 831 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 832 | if ((program_header.p_flags & PF_R) != 0) { |
Brian Carlstrom | 6a47b9d | 2013-05-17 10:58:25 -0700 | [diff] [blame] | 833 | prot |= PROT_READ; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 834 | } |
| 835 | int flags = MAP_FIXED; |
| 836 | if (writable_) { |
| 837 | prot |= PROT_WRITE; |
| 838 | flags |= MAP_SHARED; |
| 839 | } else { |
| 840 | flags |= MAP_PRIVATE; |
| 841 | } |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 842 | if (file_length < (program_header.p_offset + program_header.p_memsz)) { |
Ian Rogers | cdfcf37 | 2014-01-23 20:38:36 -0800 | [diff] [blame] | 843 | *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] | 844 | "%d of %d bytes: '%s'", file_length, i, |
| 845 | program_header.p_offset + program_header.p_memsz, |
| 846 | file_->GetPath().c_str()); |
Brian Carlstrom | 3a22361 | 2013-10-10 17:18:24 -0700 | [diff] [blame] | 847 | return false; |
| 848 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 849 | UniquePtr<MemMap> segment(MemMap::MapFileAtAddress(p_vaddr, |
| 850 | program_header.p_memsz, |
| 851 | prot, flags, file_->Fd(), |
| 852 | program_header.p_offset, |
Ian Rogers | 8d31bbd | 2013-10-13 10:44:14 -0700 | [diff] [blame] | 853 | true, |
| 854 | file_->GetPath().c_str(), |
| 855 | error_msg)); |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 856 | if (segment.get() == nullptr) { |
| 857 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s: %s", |
| 858 | i, file_->GetPath().c_str(), error_msg->c_str()); |
| 859 | return false; |
| 860 | } |
| 861 | if (segment->Begin() != p_vaddr) { |
| 862 | *error_msg = StringPrintf("Failed to map ELF file segment %d from %s at expected address %p, " |
| 863 | "instead mapped to %p", |
| 864 | i, file_->GetPath().c_str(), p_vaddr, segment->Begin()); |
| 865 | return false; |
| 866 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 867 | segments_.push_back(segment.release()); |
| 868 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 869 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 870 | // Now that we are done loading, .dynamic should be in memory to find .dynstr, .dynsym, .hash |
| 871 | dynamic_section_start_ |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 872 | = reinterpret_cast<Elf32_Dyn*>(base_address_ + GetDynamicProgramHeader().p_vaddr); |
| 873 | for (Elf32_Word i = 0; i < GetDynamicNum(); i++) { |
| 874 | Elf32_Dyn& elf_dyn = GetDynamic(i); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 875 | byte* d_ptr = base_address_ + elf_dyn.d_un.d_ptr; |
| 876 | switch (elf_dyn.d_tag) { |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 877 | case DT_HASH: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 878 | if (!ValidPointer(d_ptr)) { |
| 879 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 880 | d_ptr, file_->GetPath().c_str()); |
| 881 | return false; |
| 882 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 883 | hash_section_start_ = reinterpret_cast<Elf32_Word*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 884 | break; |
| 885 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 886 | case DT_STRTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 887 | if (!ValidPointer(d_ptr)) { |
| 888 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 889 | d_ptr, file_->GetPath().c_str()); |
| 890 | return false; |
| 891 | } |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 892 | dynstr_section_start_ = reinterpret_cast<char*>(d_ptr); |
| 893 | break; |
| 894 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 895 | case DT_SYMTAB: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 896 | if (!ValidPointer(d_ptr)) { |
| 897 | *error_msg = StringPrintf("DT_HASH value %p does not refer to a loaded ELF segment of %s", |
| 898 | d_ptr, file_->GetPath().c_str()); |
| 899 | return false; |
| 900 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 901 | dynsym_section_start_ = reinterpret_cast<Elf32_Sym*>(d_ptr); |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 902 | break; |
| 903 | } |
Nicolas Geoffray | 50cfe74 | 2014-02-19 13:27:42 +0000 | [diff] [blame] | 904 | case DT_NULL: { |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 905 | if (GetDynamicNum() != i+1) { |
| 906 | *error_msg = StringPrintf("DT_NULL found after %d .dynamic entries, " |
| 907 | "expected %d as implied by size of PT_DYNAMIC segment in %s", |
| 908 | i + 1, GetDynamicNum(), file_->GetPath().c_str()); |
| 909 | return false; |
| 910 | } |
Brian Carlstrom | 265091e | 2013-01-30 14:08:26 -0800 | [diff] [blame] | 911 | break; |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | } |
| 915 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame^] | 916 | // Use GDB JIT support to do stack backtrace, etc. |
| 917 | if (executable) { |
| 918 | GdbJITSupport(); |
| 919 | } |
| 920 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 921 | return true; |
| 922 | } |
| 923 | |
Brian Carlstrom | c140945 | 2014-02-26 14:06:23 -0800 | [diff] [blame] | 924 | bool ElfFile::ValidPointer(const byte* start) const { |
| 925 | for (size_t i = 0; i < segments_.size(); ++i) { |
| 926 | const MemMap* segment = segments_[i]; |
| 927 | if (segment->Begin() <= start && start < segment->End()) { |
| 928 | return true; |
| 929 | } |
| 930 | } |
| 931 | return false; |
| 932 | } |
| 933 | |
Mark Mendell | ae9fd93 | 2014-02-10 16:14:35 -0800 | [diff] [blame^] | 934 | static bool check_section_name(ElfFile& file, int section_num, const char *name) { |
| 935 | Elf32_Shdr& section_header = file.GetSectionHeader(section_num); |
| 936 | const char *section_name = file.GetString(SHT_SYMTAB, section_header.sh_name); |
| 937 | return strcmp(name, section_name) == 0; |
| 938 | } |
| 939 | |
| 940 | static void IncrementUint32(byte *p, uint32_t increment) { |
| 941 | uint32_t *u = reinterpret_cast<uint32_t *>(p); |
| 942 | *u += increment; |
| 943 | } |
| 944 | |
| 945 | static void RoundAndClear(byte *image, uint32_t& offset, int pwr2) { |
| 946 | uint32_t mask = pwr2 - 1; |
| 947 | while (offset & mask) { |
| 948 | image[offset++] = 0; |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | // Simple macro to bump a point to a section header to the next one. |
| 953 | #define BUMP_SHENT(sp) \ |
| 954 | sp = reinterpret_cast<Elf32_Shdr *> (\ |
| 955 | reinterpret_cast<byte*>(sp) + elf_hdr.e_shentsize);\ |
| 956 | offset += elf_hdr.e_shentsize |
| 957 | |
| 958 | void ElfFile::GdbJITSupport() { |
| 959 | // We only get here if we only are mapping the program header. |
| 960 | DCHECK(program_header_only_); |
| 961 | |
| 962 | // Well, we need the whole file to do this. |
| 963 | std::string error_msg; |
| 964 | UniquePtr<ElfFile> ptr(Open(const_cast<File*>(file_), false, false, &error_msg)); |
| 965 | ElfFile& all = *ptr; |
| 966 | |
| 967 | // Do we have interesting sections? |
| 968 | // Is this an OAT file with interesting sections? |
| 969 | if (all.GetSectionHeaderNum() != kExpectedSectionsInOATFile) { |
| 970 | return; |
| 971 | } |
| 972 | if (!check_section_name(all, 8, ".debug_info") || |
| 973 | !check_section_name(all, 9, ".debug_abbrev") || |
| 974 | !check_section_name(all, 10, ".debug_frame") || |
| 975 | !check_section_name(all, 11, ".debug_str")) { |
| 976 | return; |
| 977 | } |
| 978 | |
| 979 | // Okay, we are good enough. Fake up an ELF image and tell GDB about it. |
| 980 | // We need some extra space for the debug and string sections, the ELF header, and the |
| 981 | // section header. |
| 982 | uint32_t needed_size = KB; |
| 983 | |
| 984 | for (Elf32_Word i = 1; i < all.GetSectionHeaderNum(); i++) { |
| 985 | Elf32_Shdr& section_header = all.GetSectionHeader(i); |
| 986 | if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) { |
| 987 | // Debug section: we need it. |
| 988 | needed_size += section_header.sh_size; |
| 989 | } else if (section_header.sh_type == SHT_STRTAB && |
| 990 | strcmp(".shstrtab", |
| 991 | all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) { |
| 992 | // We also need the shared string table. |
| 993 | needed_size += section_header.sh_size; |
| 994 | |
| 995 | // We also need the extra strings .symtab\0.strtab\0 |
| 996 | needed_size += 16; |
| 997 | } |
| 998 | } |
| 999 | |
| 1000 | // Start creating our image. |
| 1001 | jit_elf_image_ = new byte[needed_size]; |
| 1002 | |
| 1003 | // Create the Elf Header by copying the old one |
| 1004 | Elf32_Ehdr& elf_hdr = |
| 1005 | *reinterpret_cast<Elf32_Ehdr*>(jit_elf_image_); |
| 1006 | |
| 1007 | elf_hdr = all.GetHeader(); |
| 1008 | elf_hdr.e_entry = 0; |
| 1009 | elf_hdr.e_phoff = 0; |
| 1010 | elf_hdr.e_phnum = 0; |
| 1011 | elf_hdr.e_phentsize = 0; |
| 1012 | elf_hdr.e_type = ET_EXEC; |
| 1013 | |
| 1014 | uint32_t offset = sizeof(Elf32_Ehdr); |
| 1015 | |
| 1016 | // Copy the debug sections and string table. |
| 1017 | uint32_t debug_offsets[kExpectedSectionsInOATFile]; |
| 1018 | memset(debug_offsets, '\0', sizeof debug_offsets); |
| 1019 | Elf32_Shdr *text_header = nullptr; |
| 1020 | int extra_shstrtab_entries = -1; |
| 1021 | int text_section_index = -1; |
| 1022 | int section_index = 1; |
| 1023 | for (Elf32_Word i = 1; i < kExpectedSectionsInOATFile; i++) { |
| 1024 | Elf32_Shdr& section_header = all.GetSectionHeader(i); |
| 1025 | // Round up to multiple of 4, ensuring zero fill. |
| 1026 | RoundAndClear(jit_elf_image_, offset, 4); |
| 1027 | if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) { |
| 1028 | // Debug section: we need it. Unfortunately, it wasn't mapped in. |
| 1029 | debug_offsets[i] = offset; |
| 1030 | // Read it from the file. |
| 1031 | lseek(file_->Fd(), section_header.sh_offset, SEEK_SET); |
| 1032 | read(file_->Fd(), jit_elf_image_ + offset, section_header.sh_size); |
| 1033 | offset += section_header.sh_size; |
| 1034 | section_index++; |
| 1035 | offset += 16; |
| 1036 | } else if (section_header.sh_type == SHT_STRTAB && |
| 1037 | strcmp(".shstrtab", |
| 1038 | all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) { |
| 1039 | // We also need the shared string table. |
| 1040 | debug_offsets[i] = offset; |
| 1041 | // Read it from the file. |
| 1042 | lseek(file_->Fd(), section_header.sh_offset, SEEK_SET); |
| 1043 | read(file_->Fd(), jit_elf_image_ + offset, section_header.sh_size); |
| 1044 | offset += section_header.sh_size; |
| 1045 | // We also need the extra strings .symtab\0.strtab\0 |
| 1046 | extra_shstrtab_entries = section_header.sh_size; |
| 1047 | memcpy(jit_elf_image_+offset, ".symtab\0.strtab\0", 16); |
| 1048 | offset += 16; |
| 1049 | section_index++; |
| 1050 | } else if (section_header.sh_flags & SHF_EXECINSTR) { |
| 1051 | DCHECK(strcmp(".text", all.GetString(SHT_SYMTAB, |
| 1052 | section_header.sh_name)) == 0); |
| 1053 | text_header = §ion_header; |
| 1054 | text_section_index = section_index++; |
| 1055 | } |
| 1056 | } |
| 1057 | DCHECK(text_header != nullptr); |
| 1058 | DCHECK_NE(extra_shstrtab_entries, -1); |
| 1059 | |
| 1060 | // We now need to update the addresses for debug_info and debug_frame to get to the |
| 1061 | // correct offset within the .text section. |
| 1062 | uint32_t text_start_addr = 0; |
| 1063 | for (uint32_t i = 0; i < segments_.size(); i++) { |
| 1064 | if (segments_[i]->GetProtect() & PROT_EXEC) { |
| 1065 | // We found the .text section. |
| 1066 | text_start_addr = reinterpret_cast<uint32_t>(segments_[i]->Begin()); |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | DCHECK_NE(text_start_addr, 0U); |
| 1071 | |
| 1072 | byte *p = jit_elf_image_+debug_offsets[8]; |
| 1073 | byte *end = p + all.GetSectionHeader(8).sh_size; |
| 1074 | |
| 1075 | // For debug_info; patch compilation using low_pc @ offset 13, high_pc at offset 17. |
| 1076 | IncrementUint32(p + 13, text_start_addr); |
| 1077 | IncrementUint32(p + 17, text_start_addr); |
| 1078 | |
| 1079 | // Now fix the low_pc, high_pc for each method address. |
| 1080 | // First method starts at offset 0x15, each subsequent method is 1+3*4 bytes further. |
| 1081 | for (p += 0x15; p < end; p += 1 /* attr# */ + 3 * sizeof(uint32_t) /* addresses */) { |
| 1082 | IncrementUint32(p + 1 + sizeof(uint32_t), text_start_addr); |
| 1083 | IncrementUint32(p + 1 + 2 * sizeof(uint32_t), text_start_addr); |
| 1084 | } |
| 1085 | |
| 1086 | // Now we have to handle the debug_frame method start addresses |
| 1087 | p = jit_elf_image_+debug_offsets[10]; |
| 1088 | end = p + all.GetSectionHeader(10).sh_size; |
| 1089 | |
| 1090 | // Skip past the CIE. |
| 1091 | p += *reinterpret_cast<uint32_t *>(p) + 4; |
| 1092 | |
| 1093 | // And walk the FDEs. |
| 1094 | for (; p < end; p += *reinterpret_cast<uint32_t *>(p) + sizeof(uint32_t)) { |
| 1095 | IncrementUint32(p + 2 * sizeof(uint32_t), text_start_addr); |
| 1096 | } |
| 1097 | |
| 1098 | // Create the data for the symbol table. |
| 1099 | const int kSymbtabAlignment = 16; |
| 1100 | RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment); |
| 1101 | uint32_t symtab_offset = offset; |
| 1102 | |
| 1103 | // First entry is empty. |
| 1104 | memset(jit_elf_image_+offset, 0, sizeof(Elf32_Sym)); |
| 1105 | offset += sizeof(Elf32_Sym); |
| 1106 | |
| 1107 | // Symbol 1 is the real .text section. |
| 1108 | Elf32_Sym& sym_ent = *reinterpret_cast<Elf32_Sym*>(jit_elf_image_+offset); |
| 1109 | sym_ent.st_name = 1; /* .text */ |
| 1110 | sym_ent.st_value = text_start_addr; |
| 1111 | sym_ent.st_size = text_header->sh_size; |
| 1112 | SetBindingAndType(&sym_ent, STB_LOCAL, STT_SECTION); |
| 1113 | sym_ent.st_other = 0; |
| 1114 | sym_ent.st_shndx = text_section_index; |
| 1115 | offset += sizeof(Elf32_Sym); |
| 1116 | |
| 1117 | // Create the data for the string table. |
| 1118 | RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment); |
| 1119 | const int kTextStringSize = 7; |
| 1120 | uint32_t strtab_offset = offset; |
| 1121 | memcpy(jit_elf_image_+offset, "\0.text", kTextStringSize); |
| 1122 | offset += kTextStringSize; |
| 1123 | |
| 1124 | // Create the section header table. |
| 1125 | // Round up to multiple of kSymbtabAlignment, ensuring zero fill. |
| 1126 | RoundAndClear(jit_elf_image_, offset, kSymbtabAlignment); |
| 1127 | elf_hdr.e_shoff = offset; |
| 1128 | Elf32_Shdr *sp = |
| 1129 | reinterpret_cast<Elf32_Shdr *>(jit_elf_image_ + offset); |
| 1130 | |
| 1131 | // Copy the first empty index. |
| 1132 | *sp = all.GetSectionHeader(0); |
| 1133 | BUMP_SHENT(sp); |
| 1134 | |
| 1135 | elf_hdr.e_shnum = 1; |
| 1136 | for (Elf32_Word i = 1; i < kExpectedSectionsInOATFile; i++) { |
| 1137 | Elf32_Shdr& section_header = all.GetSectionHeader(i); |
| 1138 | if (section_header.sh_addr == 0 && section_header.sh_type != SHT_DYNSYM) { |
| 1139 | // Debug section: we need it. |
| 1140 | *sp = section_header; |
| 1141 | sp->sh_offset = debug_offsets[i]; |
| 1142 | sp->sh_addr = 0; |
| 1143 | elf_hdr.e_shnum++; |
| 1144 | BUMP_SHENT(sp); |
| 1145 | } else if (section_header.sh_type == SHT_STRTAB && |
| 1146 | strcmp(".shstrtab", |
| 1147 | all.GetString(SHT_SYMTAB, section_header.sh_name)) == 0) { |
| 1148 | // We also need the shared string table. |
| 1149 | *sp = section_header; |
| 1150 | sp->sh_offset = debug_offsets[i]; |
| 1151 | sp->sh_size += 16; /* sizeof ".symtab\0.strtab\0" */ |
| 1152 | sp->sh_addr = 0; |
| 1153 | elf_hdr.e_shstrndx = elf_hdr.e_shnum; |
| 1154 | elf_hdr.e_shnum++; |
| 1155 | BUMP_SHENT(sp); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | // Add a .text section for the matching code section. |
| 1160 | *sp = *text_header; |
| 1161 | sp->sh_type = SHT_NOBITS; |
| 1162 | sp->sh_offset = 0; |
| 1163 | sp->sh_addr = text_start_addr; |
| 1164 | elf_hdr.e_shnum++; |
| 1165 | BUMP_SHENT(sp); |
| 1166 | |
| 1167 | // .symtab section: Need an empty index and the .text entry |
| 1168 | sp->sh_name = extra_shstrtab_entries; |
| 1169 | sp->sh_type = SHT_SYMTAB; |
| 1170 | sp->sh_flags = 0; |
| 1171 | sp->sh_addr = 0; |
| 1172 | sp->sh_offset = symtab_offset; |
| 1173 | sp->sh_size = 2 * sizeof(Elf32_Sym); |
| 1174 | sp->sh_link = elf_hdr.e_shnum + 1; // Link to .strtab section. |
| 1175 | sp->sh_info = 0; |
| 1176 | sp->sh_addralign = 16; |
| 1177 | sp->sh_entsize = sizeof(Elf32_Sym); |
| 1178 | elf_hdr.e_shnum++; |
| 1179 | BUMP_SHENT(sp); |
| 1180 | |
| 1181 | // .strtab section: Enough for .text\0. |
| 1182 | sp->sh_name = extra_shstrtab_entries + 8; |
| 1183 | sp->sh_type = SHT_STRTAB; |
| 1184 | sp->sh_flags = 0; |
| 1185 | sp->sh_addr = 0; |
| 1186 | sp->sh_offset = strtab_offset; |
| 1187 | sp->sh_size = kTextStringSize; |
| 1188 | sp->sh_link = 0; |
| 1189 | sp->sh_info = 0; |
| 1190 | sp->sh_addralign = 16; |
| 1191 | sp->sh_entsize = 0; |
| 1192 | elf_hdr.e_shnum++; |
| 1193 | BUMP_SHENT(sp); |
| 1194 | |
| 1195 | // We now have enough information to tell GDB about our file. |
| 1196 | jit_gdb_entry_ = CreateCodeEntry(jit_elf_image_, offset); |
| 1197 | } |
| 1198 | |
Brian Carlstrom | 700c8d3 | 2012-11-05 10:42:02 -0800 | [diff] [blame] | 1199 | } // namespace art |