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/SectionLoadList.h" 14 #include "lldb/Target/Target.h" 15 #include "lldb/Utility/ConvertEnum.h" 16 17 using namespace lldb; 18 using namespace lldb_private; 19 20 Section::Section(const ModuleSP &module_sp, ObjectFile *obj_file, 21 user_id_t sect_id, const ConstString &name, 22 SectionType sect_type, addr_t file_addr, addr_t byte_size, 23 lldb::offset_t file_offset, lldb::offset_t file_size, 24 uint32_t log2align, uint32_t flags, 25 uint32_t target_byte_size /*=1*/) 26 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 27 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 28 m_file_addr(file_addr), m_byte_size(byte_size), 29 m_file_offset(file_offset), m_file_size(file_size), 30 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 31 m_thread_specific(false), m_readable(false), m_writable(false), 32 m_executable(false), m_target_byte_size(target_byte_size) { 33 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 34 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 35 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s\n", 36 // this, module_sp.get(), sect_id, file_addr, file_addr + 37 // byte_size, file_offset, file_offset + file_size, flags, 38 // name.GetCString()); 39 } 40 41 Section::Section(const lldb::SectionSP &parent_section_sp, 42 const ModuleSP &module_sp, ObjectFile *obj_file, 43 user_id_t sect_id, const ConstString &name, 44 SectionType sect_type, addr_t file_addr, addr_t byte_size, 45 lldb::offset_t file_offset, lldb::offset_t file_size, 46 uint32_t log2align, uint32_t flags, 47 uint32_t target_byte_size /*=1*/) 48 : ModuleChild(module_sp), UserID(sect_id), Flags(flags), 49 m_obj_file(obj_file), m_type(sect_type), m_parent_wp(), m_name(name), 50 m_file_addr(file_addr), m_byte_size(byte_size), 51 m_file_offset(file_offset), m_file_size(file_size), 52 m_log2align(log2align), m_children(), m_fake(false), m_encrypted(false), 53 m_thread_specific(false), m_readable(false), m_writable(false), 54 m_executable(false), m_target_byte_size(target_byte_size) { 55 // printf ("Section::Section(%p): module=%p, sect_id = 0x%16.16" PRIx64 ", 56 // addr=[0x%16.16" PRIx64 " - 0x%16.16" PRIx64 "), file [0x%16.16" PRIx64 " 57 // - 0x%16.16" PRIx64 "), flags = 0x%8.8x, name = %s.%s\n", 58 // this, module_sp.get(), sect_id, file_addr, file_addr + 59 // byte_size, file_offset, file_offset + file_size, flags, 60 // parent_section_sp->GetName().GetCString(), name.GetCString()); 61 if (parent_section_sp) 62 m_parent_wp = parent_section_sp; 63 } 64 65 Section::~Section() { 66 // printf ("Section::~Section(%p)\n", this); 67 } 68 69 addr_t Section::GetFileAddress() const { 70 SectionSP parent_sp(GetParent()); 71 if (parent_sp) { 72 // This section has a parent which means m_file_addr is an offset into 73 // the parent section, so the file address for this section is the file 74 // address of the parent plus the offset 75 return parent_sp->GetFileAddress() + m_file_addr; 76 } 77 // This section has no parent, so m_file_addr is the file base address 78 return m_file_addr; 79 } 80 81 bool Section::SetFileAddress(lldb::addr_t file_addr) { 82 SectionSP parent_sp(GetParent()); 83 if (parent_sp) { 84 if (m_file_addr >= file_addr) 85 return parent_sp->SetFileAddress(m_file_addr - file_addr); 86 return false; 87 } else { 88 // This section has no parent, so m_file_addr is the file base address 89 m_file_addr = file_addr; 90 return true; 91 } 92 } 93 94 lldb::addr_t Section::GetOffset() const { 95 // This section has a parent which means m_file_addr is an offset. 96 SectionSP parent_sp(GetParent()); 97 if (parent_sp) 98 return m_file_addr; 99 100 // This section has no parent, so there is no offset to be had 101 return 0; 102 } 103 104 addr_t Section::GetLoadBaseAddress(Target *target) const { 105 addr_t load_base_addr = LLDB_INVALID_ADDRESS; 106 SectionSP parent_sp(GetParent()); 107 if (parent_sp) { 108 load_base_addr = parent_sp->GetLoadBaseAddress(target); 109 if (load_base_addr != LLDB_INVALID_ADDRESS) 110 load_base_addr += GetOffset(); 111 } 112 if (load_base_addr == LLDB_INVALID_ADDRESS) { 113 load_base_addr = target->GetSectionLoadList().GetSectionLoadAddress( 114 const_cast<Section *>(this)->shared_from_this()); 115 } 116 return load_base_addr; 117 } 118 119 bool Section::ResolveContainedAddress(addr_t offset, Address &so_addr) const { 120 const size_t num_children = m_children.GetSize(); 121 if (num_children > 0) { 122 for (size_t i = 0; i < num_children; i++) { 123 Section *child_section = m_children.GetSectionAtIndex(i).get(); 124 125 addr_t child_offset = child_section->GetOffset(); 126 if (child_offset <= offset && 127 offset - child_offset < child_section->GetByteSize()) 128 return child_section->ResolveContainedAddress(offset - child_offset, 129 so_addr); 130 } 131 } 132 so_addr.SetOffset(offset); 133 so_addr.SetSection(const_cast<Section *>(this)->shared_from_this()); 134 135 #ifdef LLDB_CONFIGURATION_DEBUG 136 // For debug builds, ensure that there are no orphaned (i.e., moduleless) 137 // sections. 138 assert(GetModule().get()); 139 #endif 140 return true; 141 } 142 143 bool Section::ContainsFileAddress(addr_t vm_addr) const { 144 const addr_t file_addr = GetFileAddress(); 145 if (file_addr != LLDB_INVALID_ADDRESS) { 146 if (file_addr <= vm_addr) { 147 const addr_t offset = (vm_addr - file_addr) * m_target_byte_size; 148 return offset < GetByteSize(); 149 } 150 } 151 return false; 152 } 153 154 int Section::Compare(const Section &a, const Section &b) { 155 if (&a == &b) 156 return 0; 157 158 const ModuleSP a_module_sp = a.GetModule(); 159 const ModuleSP b_module_sp = b.GetModule(); 160 if (a_module_sp == b_module_sp) { 161 user_id_t a_sect_uid = a.GetID(); 162 user_id_t b_sect_uid = b.GetID(); 163 if (a_sect_uid < b_sect_uid) 164 return -1; 165 if (a_sect_uid > b_sect_uid) 166 return 1; 167 return 0; 168 } else { 169 // The modules are different, just compare the module pointers 170 if (a_module_sp.get() < b_module_sp.get()) 171 return -1; 172 else 173 return 1; // We already know the modules aren't equal 174 } 175 } 176 177 void Section::Dump(Stream *s, Target *target, uint32_t depth) const { 178 // s->Printf("%.*p: ", (int)sizeof(void*) * 2, this); 179 s->Indent(); 180 s->Printf("0x%8.8" PRIx64 " %-16s ", GetID(), 181 GetSectionTypeAsCString(m_type)); 182 bool resolved = true; 183 addr_t addr = LLDB_INVALID_ADDRESS; 184 185 if (GetByteSize() == 0) 186 s->Printf("%39s", ""); 187 else { 188 if (target) 189 addr = GetLoadBaseAddress(target); 190 191 if (addr == LLDB_INVALID_ADDRESS) { 192 if (target) 193 resolved = false; 194 addr = GetFileAddress(); 195 } 196 197 VMRange range(addr, addr + m_byte_size); 198 range.Dump(s, 0); 199 } 200 201 s->Printf("%c %c%c%c 0x%8.8" PRIx64 " 0x%8.8" PRIx64 " 0x%8.8x ", 202 resolved ? ' ' : '*', m_readable ? 'r' : '-', 203 m_writable ? 'w' : '-', m_executable ? 'x' : '-', m_file_offset, 204 m_file_size, Get()); 205 206 DumpName(s); 207 208 s->EOL(); 209 210 if (depth > 0) 211 m_children.Dump(s, target, false, depth - 1); 212 } 213 214 void Section::DumpName(Stream *s) const { 215 SectionSP parent_sp(GetParent()); 216 if (parent_sp) { 217 parent_sp->DumpName(s); 218 s->PutChar('.'); 219 } else { 220 // The top most section prints the module basename 221 const char *name = NULL; 222 ModuleSP module_sp(GetModule()); 223 const FileSpec &file_spec = m_obj_file->GetFileSpec(); 224 225 if (m_obj_file) 226 name = file_spec.GetFilename().AsCString(); 227 if ((!name || !name[0]) && module_sp) 228 name = module_sp->GetFileSpec().GetFilename().AsCString(); 229 if (name && name[0]) 230 s->Printf("%s.", name); 231 } 232 m_name.Dump(s); 233 } 234 235 bool Section::IsDescendant(const Section *section) { 236 if (this == section) 237 return true; 238 SectionSP parent_sp(GetParent()); 239 if (parent_sp) 240 return parent_sp->IsDescendant(section); 241 return false; 242 } 243 244 bool Section::Slide(addr_t slide_amount, bool slide_children) { 245 if (m_file_addr != LLDB_INVALID_ADDRESS) { 246 if (slide_amount == 0) 247 return true; 248 249 m_file_addr += slide_amount; 250 251 if (slide_children) 252 m_children.Slide(slide_amount, slide_children); 253 254 return true; 255 } 256 return false; 257 } 258 259 //------------------------------------------------------------------ 260 /// Get the permissions as OR'ed bits from lldb::Permissions 261 //------------------------------------------------------------------ 262 uint32_t Section::GetPermissions() const { 263 uint32_t permissions = 0; 264 if (m_readable) 265 permissions |= ePermissionsReadable; 266 if (m_writable) 267 permissions |= ePermissionsWritable; 268 if (m_executable) 269 permissions |= ePermissionsExecutable; 270 return permissions; 271 } 272 273 //------------------------------------------------------------------ 274 /// Set the permissions using bits OR'ed from lldb::Permissions 275 //------------------------------------------------------------------ 276 void Section::SetPermissions(uint32_t permissions) { 277 m_readable = (permissions & ePermissionsReadable) != 0; 278 m_writable = (permissions & ePermissionsWritable) != 0; 279 m_executable = (permissions & ePermissionsExecutable) != 0; 280 } 281 282 lldb::offset_t Section::GetSectionData(void *dst, lldb::offset_t dst_len, 283 lldb::offset_t offset) { 284 if (m_obj_file) 285 return m_obj_file->ReadSectionData(this, offset, dst, dst_len); 286 return 0; 287 } 288 289 lldb::offset_t Section::GetSectionData(DataExtractor §ion_data) const { 290 if (m_obj_file) 291 return m_obj_file->ReadSectionData(this, section_data); 292 return 0; 293 } 294 295 #pragma mark SectionList 296 297 SectionList::SectionList() : m_sections() {} 298 299 SectionList::~SectionList() {} 300 301 SectionList &SectionList::operator=(const SectionList &rhs) { 302 if (this != &rhs) 303 m_sections = rhs.m_sections; 304 return *this; 305 } 306 307 size_t SectionList::AddSection(const lldb::SectionSP §ion_sp) { 308 if (section_sp) { 309 size_t section_index = m_sections.size(); 310 m_sections.push_back(section_sp); 311 return section_index; 312 } 313 314 return std::numeric_limits<size_t>::max(); 315 } 316 317 // Warning, this can be slow as it's removing items from a std::vector. 318 bool SectionList::DeleteSection(size_t idx) { 319 if (idx < m_sections.size()) { 320 m_sections.erase(m_sections.begin() + idx); 321 return true; 322 } 323 return false; 324 } 325 326 size_t SectionList::FindSectionIndex(const Section *sect) { 327 iterator sect_iter; 328 iterator begin = m_sections.begin(); 329 iterator end = m_sections.end(); 330 for (sect_iter = begin; sect_iter != end; ++sect_iter) { 331 if (sect_iter->get() == sect) { 332 // The secton was already in this section list 333 return std::distance(begin, sect_iter); 334 } 335 } 336 return UINT32_MAX; 337 } 338 339 size_t SectionList::AddUniqueSection(const lldb::SectionSP §_sp) { 340 size_t sect_idx = FindSectionIndex(sect_sp.get()); 341 if (sect_idx == UINT32_MAX) { 342 sect_idx = AddSection(sect_sp); 343 } 344 return sect_idx; 345 } 346 347 bool SectionList::ReplaceSection(user_id_t sect_id, 348 const lldb::SectionSP §_sp, 349 uint32_t depth) { 350 iterator sect_iter, end = m_sections.end(); 351 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 352 if ((*sect_iter)->GetID() == sect_id) { 353 *sect_iter = sect_sp; 354 return true; 355 } else if (depth > 0) { 356 if ((*sect_iter) 357 ->GetChildren() 358 .ReplaceSection(sect_id, sect_sp, depth - 1)) 359 return true; 360 } 361 } 362 return false; 363 } 364 365 size_t SectionList::GetNumSections(uint32_t depth) const { 366 size_t count = m_sections.size(); 367 if (depth > 0) { 368 const_iterator sect_iter, end = m_sections.end(); 369 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 370 count += (*sect_iter)->GetChildren().GetNumSections(depth - 1); 371 } 372 } 373 return count; 374 } 375 376 SectionSP SectionList::GetSectionAtIndex(size_t idx) const { 377 SectionSP sect_sp; 378 if (idx < m_sections.size()) 379 sect_sp = m_sections[idx]; 380 return sect_sp; 381 } 382 383 SectionSP 384 SectionList::FindSectionByName(const ConstString §ion_dstr) const { 385 SectionSP sect_sp; 386 // Check if we have a valid section string 387 if (section_dstr && !m_sections.empty()) { 388 const_iterator sect_iter; 389 const_iterator end = m_sections.end(); 390 for (sect_iter = m_sections.begin(); 391 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 392 Section *child_section = sect_iter->get(); 393 if (child_section) { 394 if (child_section->GetName() == section_dstr) { 395 sect_sp = *sect_iter; 396 } else { 397 sect_sp = 398 child_section->GetChildren().FindSectionByName(section_dstr); 399 } 400 } 401 } 402 } 403 return sect_sp; 404 } 405 406 SectionSP SectionList::FindSectionByID(user_id_t sect_id) const { 407 SectionSP sect_sp; 408 if (sect_id) { 409 const_iterator sect_iter; 410 const_iterator end = m_sections.end(); 411 for (sect_iter = m_sections.begin(); 412 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 413 if ((*sect_iter)->GetID() == sect_id) { 414 sect_sp = *sect_iter; 415 break; 416 } else { 417 sect_sp = (*sect_iter)->GetChildren().FindSectionByID(sect_id); 418 } 419 } 420 } 421 return sect_sp; 422 } 423 424 SectionSP SectionList::FindSectionByType(SectionType sect_type, 425 bool check_children, 426 size_t start_idx) const { 427 SectionSP sect_sp; 428 size_t num_sections = m_sections.size(); 429 for (size_t idx = start_idx; idx < num_sections; ++idx) { 430 if (m_sections[idx]->GetType() == sect_type) { 431 sect_sp = m_sections[idx]; 432 break; 433 } else if (check_children) { 434 sect_sp = m_sections[idx]->GetChildren().FindSectionByType( 435 sect_type, check_children, 0); 436 if (sect_sp) 437 break; 438 } 439 } 440 return sect_sp; 441 } 442 443 SectionSP SectionList::FindSectionContainingFileAddress(addr_t vm_addr, 444 uint32_t depth) const { 445 SectionSP sect_sp; 446 const_iterator sect_iter; 447 const_iterator end = m_sections.end(); 448 for (sect_iter = m_sections.begin(); 449 sect_iter != end && sect_sp.get() == NULL; ++sect_iter) { 450 Section *sect = sect_iter->get(); 451 if (sect->ContainsFileAddress(vm_addr)) { 452 // The file address is in this section. We need to make sure one of our 453 // child 454 // sections doesn't contain this address as well as obeying the depth 455 // limit 456 // that was passed in. 457 if (depth > 0) 458 sect_sp = sect->GetChildren().FindSectionContainingFileAddress( 459 vm_addr, depth - 1); 460 461 if (sect_sp.get() == NULL && !sect->IsFake()) 462 sect_sp = *sect_iter; 463 } 464 } 465 return sect_sp; 466 } 467 468 bool SectionList::ContainsSection(user_id_t sect_id) const { 469 return FindSectionByID(sect_id).get() != NULL; 470 } 471 472 void SectionList::Dump(Stream *s, Target *target, bool show_header, 473 uint32_t depth) const { 474 bool target_has_loaded_sections = 475 target && !target->GetSectionLoadList().IsEmpty(); 476 if (show_header && !m_sections.empty()) { 477 s->Indent(); 478 s->Printf("SectID Type %s Address " 479 " Perm File Off. File Size Flags " 480 " Section Name\n", 481 target_has_loaded_sections ? "Load" : "File"); 482 s->Indent(); 483 s->PutCString("---------- ---------------- " 484 "--------------------------------------- ---- ---------- " 485 "---------- " 486 "---------- ----------------------------\n"); 487 } 488 489 const_iterator sect_iter; 490 const_iterator end = m_sections.end(); 491 for (sect_iter = m_sections.begin(); sect_iter != end; ++sect_iter) { 492 (*sect_iter)->Dump(s, target_has_loaded_sections ? target : NULL, depth); 493 } 494 495 if (show_header && !m_sections.empty()) 496 s->IndentLess(); 497 } 498 499 size_t SectionList::Slide(addr_t slide_amount, bool slide_children) { 500 size_t count = 0; 501 const_iterator pos, end = m_sections.end(); 502 for (pos = m_sections.begin(); pos != end; ++pos) { 503 if ((*pos)->Slide(slide_amount, slide_children)) 504 ++count; 505 } 506 return count; 507 } 508