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