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