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/Module.h" 12 #include "lldb/Symbol/ObjectFile.h" 13 #include "lldb/Target/Target.h" 14 15 using namespace lldb; 16 using namespace lldb_private; 17 18 Section::Section 19 ( 20 Section *parent, 21 Module* module, 22 user_id_t sect_id, 23 const ConstString &name, 24 SectionType sect_type, 25 addr_t file_addr, 26 addr_t byte_size, 27 uint64_t file_offset, 28 uint64_t file_size, 29 uint32_t flags 30 ) : 31 ModuleChild (module), 32 UserID (sect_id), 33 Flags (flags), 34 m_parent (parent), 35 m_name (name), 36 m_type (sect_type), 37 m_file_addr (file_addr), 38 m_byte_size (byte_size), 39 m_file_offset (file_offset), 40 m_file_size (file_size), 41 m_children (), 42 m_fake (false), 43 m_linked_section(NULL), 44 m_linked_offset (0) 45 { 46 } 47 48 Section::~Section() 49 { 50 } 51 52 53 // Get a valid shared pointer to this section object 54 SectionSP 55 Section::GetSharedPointer() const 56 { 57 SectionSP this_sp; 58 if (m_parent) 59 this_sp = m_parent->GetChildren().GetSharedPointer (this, false); 60 else 61 { 62 ObjectFile *objfile = m_module->GetObjectFile(); 63 if (objfile) 64 { 65 SectionList *section_list = objfile->GetSectionList(); 66 if (section_list) 67 this_sp = section_list->GetSharedPointer (this, false); 68 } 69 } 70 return this_sp; 71 } 72 73 74 75 ConstString& 76 Section::GetName() 77 { 78 if (m_linked_section) 79 return const_cast<Section *>(m_linked_section)->GetName(); 80 return m_name; 81 } 82 83 const ConstString& 84 Section::GetName() const 85 { 86 if (m_linked_section) 87 return m_linked_section->GetName(); 88 return m_name; 89 } 90 91 addr_t 92 Section::GetFileAddress () const 93 { 94 if (m_parent) 95 { 96 // This section has a parent which means m_file_addr is an offset into 97 // the parent section, so the file address for this section is the file 98 // address of the parent plus the offset 99 return m_parent->GetFileAddress() + m_file_addr; 100 } 101 // This section has no parent, so m_file_addr is the file base address 102 return m_file_addr; 103 } 104 105 addr_t 106 Section::GetLinkedFileAddress () const 107 { 108 if (m_linked_section) 109 return m_linked_section->GetFileAddress() + m_linked_offset; 110 return LLDB_INVALID_ADDRESS; 111 } 112 113 114 addr_t 115 Section::GetLoadBaseAddress (Target *target) const 116 { 117 addr_t load_base_addr = LLDB_INVALID_ADDRESS; 118 if (m_linked_section) 119 { 120 load_base_addr = m_linked_section->GetLoadBaseAddress(target); 121 if (load_base_addr != LLDB_INVALID_ADDRESS) 122 load_base_addr += m_linked_offset; 123 } 124 else 125 if (m_parent) 126 { 127 load_base_addr = m_parent->GetLoadBaseAddress (target); 128 if (load_base_addr != LLDB_INVALID_ADDRESS) 129 load_base_addr += GetOffset(); 130 } 131 else 132 { 133 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress (this); 134 } 135 136 return load_base_addr; 137 } 138 139 bool 140 Section::ResolveContainedAddress (addr_t offset, Address &so_addr) const 141 { 142 const uint32_t num_children = m_children.GetSize(); 143 if (num_children > 0) 144 { 145 for (uint32_t i=0; i<num_children; i++) 146 { 147 Section* child_section = m_children.GetSectionAtIndex (i).get(); 148 149 addr_t child_offset = child_section->GetOffset(); 150 if (child_offset <= offset && offset - child_offset < child_section->GetByteSize()) 151 return child_section->ResolveContainedAddress (offset - child_offset, so_addr); 152 } 153 } 154 if (m_linked_section) 155 { 156 so_addr.SetOffset(m_linked_offset + offset); 157 so_addr.SetSection(m_linked_section); 158 } 159 else 160 { 161 so_addr.SetOffset(offset); 162 so_addr.SetSection(this); 163 } 164 return true; 165 } 166 167 bool 168 Section::ContainsFileAddress (addr_t vm_addr) const 169 { 170 const addr_t file_addr = GetFileAddress(); 171 if (file_addr != LLDB_INVALID_ADDRESS) 172 { 173 if (file_addr <= vm_addr) 174 { 175 const addr_t offset = vm_addr - file_addr; 176 return offset < GetByteSize(); 177 } 178 } 179 return false; 180 } 181 182 bool 183 Section::ContainsLinkedFileAddress (addr_t vm_addr) const 184 { 185 const addr_t linked_file_addr = GetLinkedFileAddress(); 186 if (linked_file_addr != LLDB_INVALID_ADDRESS) 187 { 188 if (linked_file_addr <= vm_addr) 189 { 190 const addr_t offset = vm_addr - linked_file_addr; 191 return offset < GetByteSize(); 192 } 193 } 194 return false; 195 } 196 197 int 198 Section::Compare (const Section& a, const Section& b) 199 { 200 if (&a == &b) 201 return 0; 202 203 const Module* a_module = a.GetModule(); 204 const Module* b_module = b.GetModule(); 205 if (a_module == b_module) 206 { 207 user_id_t a_sect_uid = a.GetID(); 208 user_id_t b_sect_uid = b.GetID(); 209 if (a_sect_uid < b_sect_uid) 210 return -1; 211 if (a_sect_uid > b_sect_uid) 212 return 1; 213 return 0; 214 } 215 else 216 { 217 // The modules are different, just compare the module pointers 218 if (a_module < b_module) 219 return -1; 220 else 221 return 1; // We already know the modules aren't equal 222 } 223 } 224 225 226 void 227 Section::Dump (Stream *s, Target *target, uint32_t depth) const 228 { 229 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 230 s->Indent(); 231 s->Printf("0x%8.8llx %-16s ", GetID(), GetSectionTypeAsCString (m_type)); 232 bool resolved = true; 233 addr_t addr = LLDB_INVALID_ADDRESS; 234 235 if (GetByteSize() == 0) 236 s->Printf("%39s", ""); 237 else 238 { 239 if (target && m_linked_section == NULL) 240 addr = GetLoadBaseAddress (target); 241 242 if (addr == LLDB_INVALID_ADDRESS) 243 { 244 if (target) 245 resolved = false; 246 addr = GetFileAddress(); 247 } 248 249 VMRange range(addr, addr + m_byte_size); 250 range.Dump (s, 0); 251 } 252 253 s->Printf("%c 0x%8.8llx 0x%8.8llx 0x%8.8x ", resolved ? ' ' : '*', m_file_offset, m_file_size, Get()); 254 255 DumpName (s); 256 257 s->EOL(); 258 259 if (m_linked_section) 260 { 261 addr = LLDB_INVALID_ADDRESS; 262 resolved = true; 263 if (target) 264 { 265 addr = m_linked_section->GetLoadBaseAddress(target); 266 if (addr != LLDB_INVALID_ADDRESS) 267 addr += m_linked_offset; 268 } 269 270 if (addr == LLDB_INVALID_ADDRESS) 271 { 272 if (target) 273 resolved = false; 274 addr = m_linked_section->GetFileAddress() + m_linked_offset; 275 } 276 277 int indent = 26 + s->GetIndentLevel(); 278 s->Printf("%*.*s", indent, indent, ""); 279 VMRange linked_range(addr, addr + m_byte_size); 280 linked_range.Dump (s, 0); 281 indent = 3 * (sizeof(uint32_t) * 2 + 2 + 1) + 1; 282 s->Printf("%c%*.*s", resolved ? ' ' : '*', indent, indent, ""); 283 284 m_linked_section->DumpName(s); 285 s->Printf(" + 0x%llx\n", m_linked_offset); 286 } 287 288 if (depth > 0) 289 m_children.Dump(s, target, false, depth - 1); 290 } 291 292 void 293 Section::DumpName (Stream *s) const 294 { 295 if (m_parent == NULL) 296 { 297 // The top most section prints the module basename 298 const char *module_basename = m_module->GetFileSpec().GetFilename().AsCString(); 299 if (module_basename && module_basename[0]) 300 s->Printf("%s.", module_basename); 301 } 302 else 303 { 304 m_parent->DumpName (s); 305 s->PutChar('.'); 306 } 307 m_name.Dump(s); 308 } 309 310 bool 311 Section::IsDescendant (const Section *section) 312 { 313 if (this == section) 314 return true; 315 if (m_parent) 316 return m_parent->IsDescendant (section); 317 return false; 318 } 319 320 bool 321 Section::Slide (addr_t slide_amount, bool slide_children) 322 { 323 if (m_file_addr != LLDB_INVALID_ADDRESS) 324 { 325 if (slide_amount == 0) 326 return true; 327 328 m_file_addr += slide_amount; 329 330 if (slide_children) 331 m_children.Slide (slide_amount, slide_children); 332 333 return true; 334 } 335 return false; 336 } 337 338 void 339 Section::SetLinkedLocation (const Section *linked_section, uint64_t linked_offset) 340 { 341 if (linked_section) 342 m_module = linked_section->GetModule(); 343 m_linked_section = linked_section; 344 m_linked_offset = linked_offset; 345 } 346 347 #pragma mark SectionList 348 349 SectionList::SectionList () : 350 m_sections() 351 { 352 } 353 354 355 SectionList::~SectionList () 356 { 357 } 358 359 uint32_t 360 SectionList::AddSection (SectionSP& sect_sp) 361 { 362 uint32_t section_index = m_sections.size(); 363 m_sections.push_back(sect_sp); 364 return section_index; 365 } 366 367 uint32_t 368 SectionList::FindSectionIndex (const Section* sect) 369 { 370 iterator sect_iter; 371 iterator begin = m_sections.begin(); 372 iterator end = m_sections.end(); 373 for (sect_iter = begin; sect_iter != end; ++sect_iter) 374 { 375 if (sect_iter->get() == sect) 376 { 377 // The secton was already in this section list 378 return std::distance (begin, sect_iter); 379 } 380 } 381 return UINT32_MAX; 382 } 383 384 uint32_t 385 SectionList::AddUniqueSection (SectionSP& sect_sp) 386 { 387 uint32_t sect_idx = FindSectionIndex (sect_sp.get()); 388 if (sect_idx == UINT32_MAX) 389 sect_idx = AddSection (sect_sp); 390 return sect_idx; 391 } 392 393 394 bool 395 SectionList::ReplaceSection (user_id_t sect_id, SectionSP& sect_sp, uint32_t depth) 396 { 397 iterator sect_iter, end = m_sections.end(); 398 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 399 { 400 if ((*sect_iter)->GetID() == sect_id) 401 { 402 *sect_iter = sect_sp; 403 return true; 404 } 405 else if (depth > 0) 406 { 407 if ((*sect_iter)->GetChildren().ReplaceSection(sect_id, sect_sp, depth - 1)) 408 return true; 409 } 410 } 411 return false; 412 } 413 414 415 size_t 416 SectionList::GetNumSections (uint32_t depth) const 417 { 418 size_t count = m_sections.size(); 419 if (depth > 0) 420 { 421 const_iterator sect_iter, end = m_sections.end(); 422 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 423 { 424 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 425 } 426 } 427 return count; 428 } 429 430 SectionSP 431 SectionList::GetSectionAtIndex (uint32_t idx) const 432 { 433 SectionSP sect_sp; 434 if (idx < m_sections.size()) 435 sect_sp = m_sections[idx]; 436 return sect_sp; 437 } 438 439 SectionSP 440 SectionList::FindSectionByName (const ConstString §ion_dstr) const 441 { 442 SectionSP sect_sp; 443 // Check if we have a valid section string 444 if (section_dstr) 445 { 446 const_iterator sect_iter; 447 const_iterator end = m_sections.end(); 448 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 449 { 450 if ((*sect_iter)->GetName() == section_dstr) 451 { 452 sect_sp = *sect_iter; 453 } 454 else 455 { 456 sect_sp = (*sect_iter)->GetChildren().FindSectionByName(section_dstr); 457 } 458 } 459 } 460 return sect_sp; 461 } 462 463 SectionSP 464 SectionList::FindSectionByID (user_id_t sect_id) const 465 { 466 SectionSP sect_sp; 467 if (sect_id) 468 { 469 const_iterator sect_iter; 470 const_iterator end = m_sections.end(); 471 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 472 { 473 if ((*sect_iter)->GetID() == sect_id) 474 { 475 sect_sp = *sect_iter; 476 break; 477 } 478 else 479 { 480 sect_sp = (*sect_iter)->GetChildren().FindSectionByID (sect_id); 481 } 482 } 483 } 484 return sect_sp; 485 } 486 487 488 SectionSP 489 SectionList::FindSectionByType (SectionType sect_type, bool check_children, uint32_t start_idx) const 490 { 491 SectionSP sect_sp; 492 uint32_t num_sections = m_sections.size(); 493 for (uint32_t idx = start_idx; idx < num_sections; ++idx) 494 { 495 if (m_sections[idx]->GetType() == sect_type) 496 { 497 sect_sp = m_sections[idx]; 498 break; 499 } 500 else if (check_children) 501 { 502 sect_sp = m_sections[idx]->GetChildren().FindSectionByType (sect_type, check_children, 0); 503 if (sect_sp) 504 break; 505 } 506 } 507 return sect_sp; 508 } 509 510 SectionSP 511 SectionList::GetSharedPointer (const Section *section, bool check_children) const 512 { 513 SectionSP sect_sp; 514 if (section) 515 { 516 const_iterator sect_iter; 517 const_iterator end = m_sections.end(); 518 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 519 { 520 if (sect_iter->get() == section) 521 { 522 sect_sp = *sect_iter; 523 break; 524 } 525 else if (check_children) 526 { 527 sect_sp = (*sect_iter)->GetChildren().GetSharedPointer (section, true); 528 } 529 } 530 } 531 return sect_sp; 532 } 533 534 535 536 SectionSP 537 SectionList::FindSectionContainingFileAddress (addr_t vm_addr, uint32_t depth) const 538 { 539 SectionSP sect_sp; 540 const_iterator sect_iter; 541 const_iterator end = m_sections.end(); 542 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 543 { 544 Section *sect = sect_iter->get(); 545 if (sect->ContainsFileAddress (vm_addr)) 546 { 547 // The file address is in this section. We need to make sure one of our child 548 // sections doesn't contain this address as well as obeying the depth limit 549 // that was passed in. 550 if (depth > 0) 551 sect_sp = sect->GetChildren().FindSectionContainingFileAddress(vm_addr, depth - 1); 552 553 if (sect_sp.get() == NULL && !sect->IsFake()) 554 sect_sp = *sect_iter; 555 } 556 } 557 return sect_sp; 558 } 559 560 561 SectionSP 562 SectionList::FindSectionContainingLinkedFileAddress (addr_t vm_addr, uint32_t depth) const 563 { 564 SectionSP sect_sp; 565 const_iterator sect_iter; 566 const_iterator end = m_sections.end(); 567 for (sect_iter = m_sections.begin(); sect_iter != end && sect_sp.get() == NULL; ++sect_iter) 568 { 569 Section *sect = sect_iter->get(); 570 if (sect->ContainsLinkedFileAddress (vm_addr)) 571 { 572 sect_sp = *sect_iter; 573 } 574 else if (depth > 0) 575 { 576 sect_sp = sect->GetChildren().FindSectionContainingLinkedFileAddress (vm_addr, depth - 1); 577 } 578 } 579 return sect_sp; 580 } 581 582 bool 583 SectionList::ContainsSection(user_id_t sect_id) const 584 { 585 return FindSectionByID (sect_id).get() != NULL; 586 } 587 588 void 589 SectionList::Dump (Stream *s, Target *target, bool show_header, uint32_t depth) const 590 { 591 bool target_has_loaded_sections = target && !target->GetSectionLoadList().IsEmpty(); 592 if (show_header && !m_sections.empty()) 593 { 594 s->Indent(); 595 s->Printf( "SectID Type %s Address File Off. File Size Flags Section Name\n", target_has_loaded_sections ? "Load" : "File"); 596 s->Indent(); 597 s->PutCString("---------- ---------------- --------------------------------------- ---------- ---------- ---------- ----------------------------\n"); 598 } 599 600 601 const_iterator sect_iter; 602 const_iterator end = m_sections.end(); 603 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) 604 { 605 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 606 } 607 608 if (show_header && !m_sections.empty()) 609 s->IndentLess(); 610 611 } 612 613 size_t 614 SectionList::Slide (addr_t slide_amount, bool slide_children) 615 { 616 size_t count = 0; 617 const_iterator pos, end = m_sections.end(); 618 for (pos = m_sections.begin(); pos != end; ++pos) 619 { 620 if ((*pos)->Slide(slide_amount, slide_children)) 621 ++count; 622 } 623 return count; 624 } 625 626