1 //===-- Section.cpp ---------------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Core/Section.h" 11 #include "lldb/Core/Address.h" // for Address 12 #include "lldb/Core/Module.h" 13 #include "lldb/Symbol/ObjectFile.h" 14 #include "lldb/Target/SectionLoadList.h" 15 #include "lldb/Target/Target.h" 16 #include "lldb/Utility/FileSpec.h" // for FileSpec 17 #include "lldb/Utility/Stream.h" // for Stream 18 #include "lldb/Utility/VMRange.h" // for VMRange 19 20 #include <inttypes.h> // for PRIx64 21 #include <limits> // for numeric_limits 22 #include <utility> // for distance 23 24 namespace lldb_private { 25 class DataExtractor; 26 } 27 using namespace lldb; 28 using namespace lldb_private; 29 30 static const char *GetSectionTypeAsCString(lldb::SectionType sect_type) { 31 switch (sect_type) { 32 case eSectionTypeInvalid: 33 return "invalid"; 34 case eSectionTypeCode: 35 return "code"; 36 case eSectionTypeContainer: 37 return "container"; 38 case eSectionTypeData: 39 return "data"; 40 case eSectionTypeDataCString: 41 return "data-cstr"; 42 case eSectionTypeDataCStringPointers: 43 return "data-cstr-ptr"; 44 case eSectionTypeDataSymbolAddress: 45 return "data-symbol-addr"; 46 case eSectionTypeData4: 47 return "data-4-byte"; 48 case eSectionTypeData8: 49 return "data-8-byte"; 50 case eSectionTypeData16: 51 return "data-16-byte"; 52 case eSectionTypeDataPointers: 53 return "data-ptrs"; 54 case eSectionTypeDebug: 55 return "debug"; 56 case eSectionTypeZeroFill: 57 return "zero-fill"; 58 case eSectionTypeDataObjCMessageRefs: 59 return "objc-message-refs"; 60 case eSectionTypeDataObjCCFStrings: 61 return "objc-cfstrings"; 62 case eSectionTypeDWARFDebugAbbrev: 63 return "dwarf-abbrev"; 64 case eSectionTypeDWARFDebugAddr: 65 return "dwarf-addr"; 66 case eSectionTypeDWARFDebugAranges: 67 return "dwarf-aranges"; 68 case eSectionTypeDWARFDebugCuIndex: 69 return "dwarf-cu-index"; 70 case eSectionTypeDWARFDebugFrame: 71 return "dwarf-frame"; 72 case eSectionTypeDWARFDebugInfo: 73 return "dwarf-info"; 74 case eSectionTypeDWARFDebugLine: 75 return "dwarf-line"; 76 case eSectionTypeDWARFDebugLoc: 77 return "dwarf-loc"; 78 case eSectionTypeDWARFDebugMacInfo: 79 return "dwarf-macinfo"; 80 case eSectionTypeDWARFDebugMacro: 81 return "dwarf-macro"; 82 case eSectionTypeDWARFDebugPubNames: 83 return "dwarf-pubnames"; 84 case eSectionTypeDWARFDebugPubTypes: 85 return "dwarf-pubtypes"; 86 case eSectionTypeDWARFDebugRanges: 87 return "dwarf-ranges"; 88 case eSectionTypeDWARFDebugStr: 89 return "dwarf-str"; 90 case eSectionTypeDWARFDebugStrOffsets: 91 return "dwarf-str-offsets"; 92 case eSectionTypeELFSymbolTable: 93 return "elf-symbol-table"; 94 case eSectionTypeELFDynamicSymbols: 95 return "elf-dynamic-symbols"; 96 case eSectionTypeELFRelocationEntries: 97 return "elf-relocation-entries"; 98 case eSectionTypeELFDynamicLinkInfo: 99 return "elf-dynamic-link-info"; 100 case eSectionTypeDWARFAppleNames: 101 return "apple-names"; 102 case eSectionTypeDWARFAppleTypes: 103 return "apple-types"; 104 case eSectionTypeDWARFAppleNamespaces: 105 return "apple-namespaces"; 106 case eSectionTypeDWARFAppleObjC: 107 return "apple-objc"; 108 case eSectionTypeEHFrame: 109 return "eh-frame"; 110 case eSectionTypeARMexidx: 111 return "ARM.exidx"; 112 case eSectionTypeARMextab: 113 return "ARM.extab"; 114 case eSectionTypeCompactUnwind: 115 return "compact-unwind"; 116 case eSectionTypeGoSymtab: 117 return "go-symtab"; 118 case eSectionTypeAbsoluteAddress: 119 return "absolute"; 120 case eSectionTypeOther: 121 return "regular"; 122 } 123 return "unknown"; 124 } 125 126 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file, 127 user_id_t sect_id, const ConstString &name, 128 SectionType sect_type, addr_t file_addr, addr_t byte_size, 129 lldb::offset_t file_offset, lldb::offset_t file_size, 130 uint32_t log2align, uint32_t flags, 131 uint32_t target_byte_size /*=1*/) 132 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 133 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 134 m_file_addr(file_addr), m_byte_size(byte_size), 135 m_file_offset(file_offset), m_file_size(file_size), 136 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 137 m_thread_specific(false), m_readable(false), m_writable(false), 138 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) { 139 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 140 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 141 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n", 142 // this, module_sp.get(), sect_id, file_addr, file_addr + 143 // byte_size, file_offset, file_offset + file_size, flags, 144 // name.GetCString()); 145 } 146 147 Section::Section(const lldb::SectionSP &parent_section_sp, 148 const ModuleSP &module_sp, ObjectFile *obj_file, 149 user_id_t sect_id, const ConstString &name, 150 SectionType sect_type, addr_t file_addr, addr_t byte_size, 151 lldb::offset_t file_offset, lldb::offset_t file_size, 152 uint32_t log2align, uint32_t flags, 153 uint32_t target_byte_size /*=1*/) 154 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 155 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 156 m_file_addr(file_addr), m_byte_size(byte_size), 157 m_file_offset(file_offset), m_file_size(file_size), 158 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 159 m_thread_specific(false), m_readable(false), m_writable(false), 160 m_executable(false), m_relocated(false), m_target_byte_size(target_byte_size) { 161 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 162 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 163 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n", 164 // this, module_sp.get(), sect_id, file_addr, file_addr + 165 // byte_size, file_offset, file_offset + file_size, flags, 166 // parent_section_sp->GetName().GetCString(), name.GetCString()); 167 if (parent_section_sp) 168 m_parent_wp = parent_section_sp; 169 } 170 171 Section::~Section() { 172 // printf ("Section::~Section(%p)\n", this); 173 } 174 175 addr_t Section::GetFileAddress() const { 176 SectionSP parent_sp(GetParent()); 177 if (parent_sp) { 178 // This section has a parent which means m_file_addr is an offset into 179 // the parent section, so the file address for this section is the file 180 // address of the parent plus the offset 181 return parent_sp->GetFileAddress() + m_file_addr; 182 } 183 // This section has no parent, so m_file_addr is the file base address 184 return m_file_addr; 185 } 186 187 bool Section::SetFileAddress(lldb::addr_t file_addr) { 188 SectionSP parent_sp(GetParent()); 189 if (parent_sp) { 190 if (m_file_addr >= file_addr) 191 return parent_sp->SetFileAddress(m_file_addr - file_addr); 192 return false; 193 } else { 194 // This section has no parent, so m_file_addr is the file base address 195 m_file_addr = file_addr; 196 return true; 197 } 198 } 199 200 lldb::addr_t Section::GetOffset() const { 201 // This section has a parent which means m_file_addr is an offset. 202 SectionSP parent_sp(GetParent()); 203 if (parent_sp) 204 return m_file_addr; 205 206 // This section has no parent, so there is no offset to be had 207 return 0; 208 } 209 210 addr_t Section::GetLoadBaseAddress(Target *target) const { 211 addr_t load_base_addr = LLDB_INVALID_ADDRESS; 212 SectionSP parent_sp(GetParent()); 213 if (parent_sp) { 214 load_base_addr = parent_sp->GetLoadBaseAddress(target); 215 if (load_base_addr != LLDB_INVALID_ADDRESS) 216 load_base_addr += GetOffset(); 217 } 218 if (load_base_addr == LLDB_INVALID_ADDRESS) { 219 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress( 220 const_cast<Section *>(this)->shared_from_this()); 221 } 222 return load_base_addr; 223 } 224 225 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr, 226 bool allow_section_end) const { 227 const size_t num_children = m_children.GetSize(); 228 for (size_t i = 0; i < num_children; i++) { 229 Section *child_section = m_children.GetSectionAtIndex(i).get(); 230 231 addr_t child_offset = child_section->GetOffset(); 232 if (child_offset <= offset && 233 offset - child_offset < 234 child_section->GetByteSize() + (allow_section_end ? 1 : 0)) 235 return child_section->ResolveContainedAddress(offset - child_offset, 236 so_addr, allow_section_end); 237 } 238 so_addr.SetOffset(offset); 239 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this()); 240 241 #ifdef LLDB_CONFIGURATION_DEBUG 242 // For debug builds, ensure that there are no orphaned (i.e., moduleless) 243 // sections. 244 assert(GetModule().get()); 245 #endif 246 return true; 247 } 248 249 bool Section::ContainsFileAddress(addr_t vm_addr) const { 250 const addr_t file_addr = GetFileAddress(); 251 if (file_addr != LLDB_INVALID_ADDRESS) { 252 if (file_addr <= vm_addr) { 253 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size; 254 return offset < GetByteSize(); 255 } 256 } 257 return false; 258 } 259 260 int Section::Compare(const Section &a, const Section &b) { 261 if (&a == &b) 262 return 0; 263 264 const ModuleSP a_module_sp = a.GetModule(); 265 const ModuleSP b_module_sp = b.GetModule(); 266 if (a_module_sp == b_module_sp) { 267 user_id_t a_sect_uid = a.GetID(); 268 user_id_t b_sect_uid = b.GetID(); 269 if (a_sect_uid < b_sect_uid) 270 return -1; 271 if (a_sect_uid > b_sect_uid) 272 return 1; 273 return 0; 274 } else { 275 // The modules are different, just compare the module pointers 276 if (a_module_sp.get() < b_module_sp.get()) 277 return -1; 278 else 279 return 1; // We already know the modules aren't equal 280 } 281 } 282 283 void Section::Dump(Stream *s, Target *target, uint32_t depth) const { 284 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 285 s->Indent(); 286 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), 287 GetSectionTypeAsCString(m_type)); 288 bool resolved = true; 289 addr_t addr = LLDB_INVALID_ADDRESS; 290 291 if (GetByteSize() == 0) 292 s->Printf("%39s", ""); 293 else { 294 if (target) 295 addr = GetLoadBaseAddress(target); 296 297 if (addr == LLDB_INVALID_ADDRESS) { 298 if (target) 299 resolved = false; 300 addr = GetFileAddress(); 301 } 302 303 VMRange range(addr, addr + m_byte_size); 304 range.Dump(s, 0); 305 } 306 307 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", 308 resolved ? ' ' : '*', m_readable ? 'r' : '-', 309 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset, 310 m_file_size, Get()); 311 312 DumpName(s); 313 314 s->EOL(); 315 316 if (depth > 0) 317 m_children.Dump(s, target, false, depth - 1); 318 } 319 320 void Section::DumpName(Stream *s) const { 321 SectionSP parent_sp(GetParent()); 322 if (parent_sp) { 323 parent_sp->DumpName(s); 324 s->PutChar('.'); 325 } else { 326 // The top most section prints the module basename 327 const char *name = NULL; 328 ModuleSP module_sp(GetModule()); 329 330 if (m_obj_file) { 331 const FileSpec &file_spec = m_obj_file->GetFileSpec(); 332 name = file_spec.GetFilename().AsCString(); 333 } 334 if ((!name || !name[0]) && module_sp) 335 name = module_sp->GetFileSpec().GetFilename().AsCString(); 336 if (name && name[0]) 337 s->Printf("%s.", name); 338 } 339 m_name.Dump(s); 340 } 341 342 bool Section::IsDescendant(const Section *section) { 343 if (this == section) 344 return true; 345 SectionSP parent_sp(GetParent()); 346 if (parent_sp) 347 return parent_sp->IsDescendant(section); 348 return false; 349 } 350 351 bool Section::Slide(addr_t slide_amount, bool slide_children) { 352 if (m_file_addr != LLDB_INVALID_ADDRESS) { 353 if (slide_amount == 0) 354 return true; 355 356 m_file_addr += slide_amount; 357 358 if (slide_children) 359 m_children.Slide(slide_amount, slide_children); 360 361 return true; 362 } 363 return false; 364 } 365 366 //------------------------------------------------------------------ 367 /// Get the permissions as OR'ed bits from lldb::Permissions 368 //------------------------------------------------------------------ 369 uint32_t Section::GetPermissions() const { 370 uint32_t permissions = 0; 371 if (m_readable) 372 permissions |= ePermissionsReadable; 373 if (m_writable) 374 permissions |= ePermissionsWritable; 375 if (m_executable) 376 permissions |= ePermissionsExecutable; 377 return permissions; 378 } 379 380 //------------------------------------------------------------------ 381 /// Set the permissions using bits OR'ed from lldb::Permissions 382 //------------------------------------------------------------------ 383 void Section::SetPermissions(uint32_t permissions) { 384 m_readable = (permissions & ePermissionsReadable) != 0; 385 m_writable = (permissions & ePermissionsWritable) != 0; 386 m_executable = (permissions & ePermissionsExecutable) != 0; 387 } 388 389 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len, 390 lldb::offset_t offset) { 391 if (m_obj_file) 392 return m_obj_file->ReadSectionData(this, offset, dst, dst_len); 393 return 0; 394 } 395 396 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) { 397 if (m_obj_file) 398 return m_obj_file->ReadSectionData(this, section_data); 399 return 0; 400 } 401 402 #pragma mark SectionList 403 404 SectionList::SectionList() : m_sections() {} 405 406 SectionList::~SectionList() {} 407 408 SectionList &SectionList::operator=(const SectionList &rhs) { 409 if (this != &rhs) 410 m_sections = rhs.m_sections; 411 return *this; 412 } 413 414 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) { 415 if (section_sp) { 416 size_t section_index = m_sections.size(); 417 m_sections.push_back(section_sp); 418 return section_index; 419 } 420 421 return std::numeric_limits<size_t>::max(); 422 } 423 424 // Warning, this can be slow as it's removing items from a std::vector. 425 bool SectionList::DeleteSection(size_t idx) { 426 if (idx < m_sections.size()) { 427 m_sections.erase(m_sections.begin() + idx); 428 return true; 429 } 430 return false; 431 } 432 433 size_t SectionList::FindSectionIndex(const Section *sect) { 434 iterator sect_iter; 435 iterator begin = m_sections.begin(); 436 iterator end = m_sections.end(); 437 for (sect_iter = begin; sect_iter != end; ++sect_iter) { 438 if (sect_iter->get() == sect) { 439 // The secton was already in this section list 440 return std::distance(begin, sect_iter); 441 } 442 } 443 return UINT32_MAX; 444 } 445 446 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) { 447 size_t sect_idx = FindSectionIndex(sect_sp.get()); 448 if (sect_idx == UINT32_MAX) { 449 sect_idx = AddSection(sect_sp); 450 } 451 return sect_idx; 452 } 453 454 bool SectionList::ReplaceSection(user_id_t sect_id, 455 const lldb::SectionSP §_sp, 456 uint32_t depth) { 457 iterator sect_iter, end = m_sections.end(); 458 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 459 if ((*sect_iter)->GetID() == sect_id) { 460 *sect_iter = sect_sp; 461 return true; 462 } else if (depth > 0) { 463 if ((*sect_iter) 464 ->GetChildren() 465 .ReplaceSection(sect_id, sect_sp, depth - 1)) 466 return true; 467 } 468 } 469 return false; 470 } 471 472 size_t SectionList::GetNumSections(uint32_t depth) const { 473 size_t count = m_sections.size(); 474 if (depth > 0) { 475 const_iterator sect_iter, end = m_sections.end(); 476 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 477 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 478 } 479 } 480 return count; 481 } 482 483 SectionSP SectionList::GetSectionAtIndex(size_t idx) const { 484 SectionSP sect_sp; 485 if (idx < m_sections.size()) 486 sect_sp = m_sections[idx]; 487 return sect_sp; 488 } 489 490 SectionSP 491 SectionList::FindSectionByName(const ConstString §ion_dstr) const { 492 SectionSP sect_sp; 493 // Check if we have a valid section string 494 if (section_dstr && !m_sections.empty()) { 495 const_iterator sect_iter; 496 const_iterator end = m_sections.end(); 497 for (sect_iter = m_sections.begin(); 498 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 499 Section *child_section = sect_iter->get(); 500 if (child_section) { 501 if (child_section->GetName() == section_dstr) { 502 sect_sp = *sect_iter; 503 } else { 504 sect_sp = 505 child_section->GetChildren().FindSectionByName(section_dstr); 506 } 507 } 508 } 509 } 510 return sect_sp; 511 } 512 513 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const { 514 SectionSP sect_sp; 515 if (sect_id) { 516 const_iterator sect_iter; 517 const_iterator end = m_sections.end(); 518 for (sect_iter = m_sections.begin(); 519 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 520 if ((*sect_iter)->GetID() == sect_id) { 521 sect_sp = *sect_iter; 522 break; 523 } else { 524 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id); 525 } 526 } 527 } 528 return sect_sp; 529 } 530 531 SectionSP SectionList::FindSectionByType(SectionType sect_type, 532 bool check_children, 533 size_t start_idx) const { 534 SectionSP sect_sp; 535 size_t num_sections = m_sections.size(); 536 for (size_t idx = start_idx; idx < num_sections; ++idx) { 537 if (m_sections[idx]->GetType() == sect_type) { 538 sect_sp = m_sections[idx]; 539 break; 540 } else if (check_children) { 541 sect_sp = m_sections[idx]->GetChildren().FindSectionByType( 542 sect_type, check_children, 0); 543 if (sect_sp) 544 break; 545 } 546 } 547 return sect_sp; 548 } 549 550 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr, 551 uint32_t depth) const { 552 SectionSP sect_sp; 553 const_iterator sect_iter; 554 const_iterator end = m_sections.end(); 555 for (sect_iter = m_sections.begin(); 556 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 557 Section *sect = sect_iter->get(); 558 if (sect->ContainsFileAddress(vm_addr)) { 559 // The file address is in this section. We need to make sure one of our 560 // child 561 // sections doesn't contain this address as well as obeying the depth 562 // limit 563 // that was passed in. 564 if (depth > 0) 565 sect_sp = sect->GetChildren().FindSectionContainingFileAddress( 566 vm_addr, depth - 1); 567 568 if (sect_sp.get() == NULL && !sect->IsFake()) 569 sect_sp = *sect_iter; 570 } 571 } 572 return sect_sp; 573 } 574 575 bool SectionList::ContainsSection(user_id_t sect_id) const { 576 return FindSectionByID(sect_id).get() != NULL; 577 } 578 579 void SectionList::Dump(Stream *s, Target *target, bool show_header, 580 uint32_t depth) const { 581 bool target_has_loaded_sections = 582 target && !target->GetSectionLoadList().IsEmpty(); 583 if (show_header && !m_sections.empty()) { 584 s->Indent(); 585 s->Printf("SectID Type %s Address " 586 " Perm File Off. File Size Flags " 587 " Section Name\n", 588 target_has_loaded_sections ? "Load" : "File"); 589 s->Indent(); 590 s->PutCString("---------- ---------------- " 591 "--------------------------------------- ---- ---------- " 592 "---------- " 593 "---------- ----------------------------\n"); 594 } 595 596 const_iterator sect_iter; 597 const_iterator end = m_sections.end(); 598 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 599 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 600 } 601 602 if (show_header && !m_sections.empty()) 603 s->IndentLess(); 604 } 605 606 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) { 607 size_t count = 0; 608 const_iterator pos, end = m_sections.end(); 609 for (pos = m_sections.begin(); pos != end; ++pos) { 610 if ((*pos)->Slide(slide_amount, slide_children)) 611 ++count; 612 } 613 return count; 614 } 615