1 //===-- ObjectFile.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/Symbol/ObjectFile.h" 11 #include "lldb/Core/Module.h" 12 #include "lldb/Core/ModuleSpec.h" 13 #include "lldb/Core/PluginManager.h" 14 #include "lldb/Core/Section.h" 15 #include "lldb/Symbol/ObjectContainer.h" 16 #include "lldb/Symbol/SymbolFile.h" 17 #include "lldb/Target/Process.h" 18 #include "lldb/Target/SectionLoadList.h" 19 #include "lldb/Target/Target.h" 20 #include "lldb/Utility/DataBuffer.h" 21 #include "lldb/Utility/DataBufferHeap.h" 22 #include "lldb/Utility/DataBufferLLVM.h" 23 #include "lldb/Utility/Log.h" 24 #include "lldb/Utility/RegularExpression.h" 25 #include "lldb/Utility/Timer.h" 26 #include "lldb/lldb-private.h" 27 28 using namespace lldb; 29 using namespace lldb_private; 30 31 ObjectFileSP 32 ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file, 33 lldb::offset_t file_offset, lldb::offset_t file_size, 34 DataBufferSP &data_sp, lldb::offset_t &data_offset) { 35 ObjectFileSP object_file_sp; 36 37 if (module_sp) { 38 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 39 Timer scoped_timer( 40 func_cat, 41 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = " 42 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")", 43 module_sp->GetFileSpec().GetPath().c_str(), 44 static_cast<const void *>(file), static_cast<uint64_t>(file_offset), 45 static_cast<uint64_t>(file_size)); 46 if (file) { 47 FileSpec archive_file; 48 ObjectContainerCreateInstance create_object_container_callback; 49 50 const bool file_exists = file->Exists(); 51 if (!data_sp) { 52 // We have an object name which most likely means we have a .o file in 53 // a static archive (.a file). Try and see if we have a cached archive 54 // first without reading any data first 55 if (file_exists && module_sp->GetObjectName()) { 56 for (uint32_t idx = 0; 57 (create_object_container_callback = 58 PluginManager::GetObjectContainerCreateCallbackAtIndex( 59 idx)) != nullptr; 60 ++idx) { 61 std::unique_ptr<ObjectContainer> object_container_ap( 62 create_object_container_callback(module_sp, data_sp, 63 data_offset, file, file_offset, 64 file_size)); 65 66 if (object_container_ap.get()) 67 object_file_sp = object_container_ap->GetObjectFile(file); 68 69 if (object_file_sp.get()) 70 return object_file_sp; 71 } 72 } 73 // Ok, we didn't find any containers that have a named object, now lets 74 // read the first 512 bytes from the file so the object file and object 75 // container plug-ins can use these bytes to see if they can parse this 76 // file. 77 if (file_size > 0) { 78 data_sp = 79 DataBufferLLVM::CreateSliceFromPath(file->GetPath(), 512, file_offset); 80 data_offset = 0; 81 } 82 } 83 84 if (!data_sp || data_sp->GetByteSize() == 0) { 85 // Check for archive file with format "/path/to/archive.a(object.o)" 86 char path_with_object[PATH_MAX * 2]; 87 module_sp->GetFileSpec().GetPath(path_with_object, 88 sizeof(path_with_object)); 89 90 ConstString archive_object; 91 const bool must_exist = true; 92 if (ObjectFile::SplitArchivePathWithObject( 93 path_with_object, archive_file, archive_object, must_exist)) { 94 file_size = archive_file.GetByteSize(); 95 if (file_size > 0) { 96 file = &archive_file; 97 module_sp->SetFileSpecAndObjectName(archive_file, archive_object); 98 // Check if this is a object container by iterating through all 99 // object container plugin instances and then trying to get an 100 // object file from the container plugins since we had a name. 101 // Also, don't read 102 // ANY data in case there is data cached in the container plug-ins 103 // (like BSD archives caching the contained objects within an 104 // file). 105 for (uint32_t idx = 0; 106 (create_object_container_callback = 107 PluginManager::GetObjectContainerCreateCallbackAtIndex( 108 idx)) != nullptr; 109 ++idx) { 110 std::unique_ptr<ObjectContainer> object_container_ap( 111 create_object_container_callback(module_sp, data_sp, 112 data_offset, file, 113 file_offset, file_size)); 114 115 if (object_container_ap.get()) 116 object_file_sp = object_container_ap->GetObjectFile(file); 117 118 if (object_file_sp.get()) 119 return object_file_sp; 120 } 121 // We failed to find any cached object files in the container plug- 122 // ins, so lets read the first 512 bytes and try again below... 123 data_sp = DataBufferLLVM::CreateSliceFromPath(archive_file.GetPath(), 124 512, file_offset); 125 } 126 } 127 } 128 129 if (data_sp && data_sp->GetByteSize() > 0) { 130 // Check if this is a normal object file by iterating through all 131 // object file plugin instances. 132 ObjectFileCreateInstance create_object_file_callback; 133 for (uint32_t idx = 0; 134 (create_object_file_callback = 135 PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) != 136 nullptr; 137 ++idx) { 138 object_file_sp.reset(create_object_file_callback( 139 module_sp, data_sp, data_offset, file, file_offset, file_size)); 140 if (object_file_sp.get()) 141 return object_file_sp; 142 } 143 144 // Check if this is a object container by iterating through all object 145 // container plugin instances and then trying to get an object file 146 // from the container. 147 for (uint32_t idx = 0; 148 (create_object_container_callback = 149 PluginManager::GetObjectContainerCreateCallbackAtIndex( 150 idx)) != nullptr; 151 ++idx) { 152 std::unique_ptr<ObjectContainer> object_container_ap( 153 create_object_container_callback(module_sp, data_sp, data_offset, 154 file, file_offset, file_size)); 155 156 if (object_container_ap.get()) 157 object_file_sp = object_container_ap->GetObjectFile(file); 158 159 if (object_file_sp.get()) 160 return object_file_sp; 161 } 162 } 163 } 164 } 165 // We didn't find it, so clear our shared pointer in case it contains 166 // anything and return an empty shared pointer 167 object_file_sp.reset(); 168 return object_file_sp; 169 } 170 171 ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, 172 const ProcessSP &process_sp, 173 lldb::addr_t header_addr, 174 DataBufferSP &data_sp) { 175 ObjectFileSP object_file_sp; 176 177 if (module_sp) { 178 static Timer::Category func_cat(LLVM_PRETTY_FUNCTION); 179 Timer scoped_timer(func_cat, 180 "ObjectFile::FindPlugin (module = " 181 "%s, process = %p, header_addr = " 182 "0x%" PRIx64 ")", 183 module_sp->GetFileSpec().GetPath().c_str(), 184 static_cast<void *>(process_sp.get()), header_addr); 185 uint32_t idx; 186 187 // Check if this is a normal object file by iterating through all object 188 // file plugin instances. 189 ObjectFileCreateMemoryInstance create_callback; 190 for (idx = 0; 191 (create_callback = 192 PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) != 193 nullptr; 194 ++idx) { 195 object_file_sp.reset( 196 create_callback(module_sp, data_sp, process_sp, header_addr)); 197 if (object_file_sp.get()) 198 return object_file_sp; 199 } 200 } 201 202 // We didn't find it, so clear our shared pointer in case it contains 203 // anything and return an empty shared pointer 204 object_file_sp.reset(); 205 return object_file_sp; 206 } 207 208 size_t ObjectFile::GetModuleSpecifications(const FileSpec &file, 209 lldb::offset_t file_offset, 210 lldb::offset_t file_size, 211 ModuleSpecList &specs) { 212 DataBufferSP data_sp = DataBufferLLVM::CreateSliceFromPath(file.GetPath(), 512, file_offset); 213 if (data_sp) { 214 if (file_size == 0) { 215 const lldb::offset_t actual_file_size = file.GetByteSize(); 216 if (actual_file_size > file_offset) 217 file_size = actual_file_size - file_offset; 218 } 219 return ObjectFile::GetModuleSpecifications(file, // file spec 220 data_sp, // data bytes 221 0, // data offset 222 file_offset, // file offset 223 file_size, // file length 224 specs); 225 } 226 return 0; 227 } 228 229 size_t ObjectFile::GetModuleSpecifications( 230 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 231 lldb::offset_t data_offset, lldb::offset_t file_offset, 232 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { 233 const size_t initial_count = specs.GetSize(); 234 ObjectFileGetModuleSpecifications callback; 235 uint32_t i; 236 // Try the ObjectFile plug-ins 237 for (i = 0; 238 (callback = 239 PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex( 240 i)) != nullptr; 241 ++i) { 242 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) 243 return specs.GetSize() - initial_count; 244 } 245 246 // Try the ObjectContainer plug-ins 247 for (i = 0; 248 (callback = PluginManager:: 249 GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != 250 nullptr; 251 ++i) { 252 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) 253 return specs.GetSize() - initial_count; 254 } 255 return 0; 256 } 257 258 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, 259 const FileSpec *file_spec_ptr, 260 lldb::offset_t file_offset, lldb::offset_t length, 261 const lldb::DataBufferSP &data_sp, 262 lldb::offset_t data_offset) 263 : ModuleChild(module_sp), 264 m_file(), // This file could be different from the original module's file 265 m_type(eTypeInvalid), m_strata(eStrataInvalid), 266 m_file_offset(file_offset), m_length(length), m_data(), 267 m_unwind_table(*this), m_process_wp(), 268 m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_ap(), m_symtab_ap(), 269 m_synthetic_symbol_idx(0) { 270 if (file_spec_ptr) 271 m_file = *file_spec_ptr; 272 if (data_sp) 273 m_data.SetData(data_sp, data_offset, length); 274 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 275 if (log) 276 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, " 277 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 278 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 279 module_sp->GetSpecificationDescription().c_str(), 280 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset, 281 m_length); 282 } 283 284 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, 285 const ProcessSP &process_sp, lldb::addr_t header_addr, 286 DataBufferSP &header_data_sp) 287 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid), 288 m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(), 289 m_unwind_table(*this), m_process_wp(process_sp), 290 m_memory_addr(header_addr), m_sections_ap(), m_symtab_ap(), 291 m_synthetic_symbol_idx(0) { 292 if (header_data_sp) 293 m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize()); 294 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 295 if (log) 296 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, " 297 "header_addr = 0x%" PRIx64, 298 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 299 module_sp->GetSpecificationDescription().c_str(), 300 static_cast<void *>(process_sp.get()), m_memory_addr); 301 } 302 303 ObjectFile::~ObjectFile() { 304 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 305 if (log) 306 log->Printf("%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this)); 307 } 308 309 bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) { 310 ModuleSP module_sp(GetModule()); 311 if (module_sp) 312 return module_sp->SetArchitecture(new_arch); 313 return false; 314 } 315 316 AddressClass ObjectFile::GetAddressClass(addr_t file_addr) { 317 Symtab *symtab = GetSymtab(); 318 if (symtab) { 319 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 320 if (symbol) { 321 if (symbol->ValueIsAddress()) { 322 const SectionSP section_sp(symbol->GetAddressRef().GetSection()); 323 if (section_sp) { 324 const SectionType section_type = section_sp->GetType(); 325 switch (section_type) { 326 case eSectionTypeInvalid: 327 return AddressClass::eUnknown; 328 case eSectionTypeCode: 329 return AddressClass::eCode; 330 case eSectionTypeContainer: 331 return AddressClass::eUnknown; 332 case eSectionTypeData: 333 case eSectionTypeDataCString: 334 case eSectionTypeDataCStringPointers: 335 case eSectionTypeDataSymbolAddress: 336 case eSectionTypeData4: 337 case eSectionTypeData8: 338 case eSectionTypeData16: 339 case eSectionTypeDataPointers: 340 case eSectionTypeZeroFill: 341 case eSectionTypeDataObjCMessageRefs: 342 case eSectionTypeDataObjCCFStrings: 343 case eSectionTypeGoSymtab: 344 return AddressClass::eData; 345 case eSectionTypeDebug: 346 case eSectionTypeDWARFDebugAbbrev: 347 case eSectionTypeDWARFDebugAddr: 348 case eSectionTypeDWARFDebugAranges: 349 case eSectionTypeDWARFDebugCuIndex: 350 case eSectionTypeDWARFDebugFrame: 351 case eSectionTypeDWARFDebugInfo: 352 case eSectionTypeDWARFDebugLine: 353 case eSectionTypeDWARFDebugLineStr: 354 case eSectionTypeDWARFDebugLoc: 355 case eSectionTypeDWARFDebugMacInfo: 356 case eSectionTypeDWARFDebugMacro: 357 case eSectionTypeDWARFDebugNames: 358 case eSectionTypeDWARFDebugPubNames: 359 case eSectionTypeDWARFDebugPubTypes: 360 case eSectionTypeDWARFDebugRanges: 361 case eSectionTypeDWARFDebugRngLists: 362 case eSectionTypeDWARFDebugStr: 363 case eSectionTypeDWARFDebugStrOffsets: 364 case eSectionTypeDWARFDebugTypes: 365 case eSectionTypeDWARFAppleNames: 366 case eSectionTypeDWARFAppleTypes: 367 case eSectionTypeDWARFAppleNamespaces: 368 case eSectionTypeDWARFAppleObjC: 369 case eSectionTypeDWARFGNUDebugAltLink: 370 return AddressClass::eDebug; 371 case eSectionTypeEHFrame: 372 case eSectionTypeARMexidx: 373 case eSectionTypeARMextab: 374 case eSectionTypeCompactUnwind: 375 return AddressClass::eRuntime; 376 case eSectionTypeELFSymbolTable: 377 case eSectionTypeELFDynamicSymbols: 378 case eSectionTypeELFRelocationEntries: 379 case eSectionTypeELFDynamicLinkInfo: 380 case eSectionTypeOther: 381 return AddressClass::eUnknown; 382 case eSectionTypeAbsoluteAddress: 383 // In case of absolute sections decide the address class based on 384 // the symbol type because the section type isn't specify if it is 385 // a code or a data section. 386 break; 387 } 388 } 389 } 390 391 const SymbolType symbol_type = symbol->GetType(); 392 switch (symbol_type) { 393 case eSymbolTypeAny: 394 return AddressClass::eUnknown; 395 case eSymbolTypeAbsolute: 396 return AddressClass::eUnknown; 397 case eSymbolTypeCode: 398 return AddressClass::eCode; 399 case eSymbolTypeTrampoline: 400 return AddressClass::eCode; 401 case eSymbolTypeResolver: 402 return AddressClass::eCode; 403 case eSymbolTypeData: 404 return AddressClass::eData; 405 case eSymbolTypeRuntime: 406 return AddressClass::eRuntime; 407 case eSymbolTypeException: 408 return AddressClass::eRuntime; 409 case eSymbolTypeSourceFile: 410 return AddressClass::eDebug; 411 case eSymbolTypeHeaderFile: 412 return AddressClass::eDebug; 413 case eSymbolTypeObjectFile: 414 return AddressClass::eDebug; 415 case eSymbolTypeCommonBlock: 416 return AddressClass::eDebug; 417 case eSymbolTypeBlock: 418 return AddressClass::eDebug; 419 case eSymbolTypeLocal: 420 return AddressClass::eData; 421 case eSymbolTypeParam: 422 return AddressClass::eData; 423 case eSymbolTypeVariable: 424 return AddressClass::eData; 425 case eSymbolTypeVariableType: 426 return AddressClass::eDebug; 427 case eSymbolTypeLineEntry: 428 return AddressClass::eDebug; 429 case eSymbolTypeLineHeader: 430 return AddressClass::eDebug; 431 case eSymbolTypeScopeBegin: 432 return AddressClass::eDebug; 433 case eSymbolTypeScopeEnd: 434 return AddressClass::eDebug; 435 case eSymbolTypeAdditional: 436 return AddressClass::eUnknown; 437 case eSymbolTypeCompiler: 438 return AddressClass::eDebug; 439 case eSymbolTypeInstrumentation: 440 return AddressClass::eDebug; 441 case eSymbolTypeUndefined: 442 return AddressClass::eUnknown; 443 case eSymbolTypeObjCClass: 444 return AddressClass::eRuntime; 445 case eSymbolTypeObjCMetaClass: 446 return AddressClass::eRuntime; 447 case eSymbolTypeObjCIVar: 448 return AddressClass::eRuntime; 449 case eSymbolTypeReExported: 450 return AddressClass::eRuntime; 451 } 452 } 453 } 454 return AddressClass::eUnknown; 455 } 456 457 DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp, 458 lldb::addr_t addr, size_t byte_size) { 459 DataBufferSP data_sp; 460 if (process_sp) { 461 std::unique_ptr<DataBufferHeap> data_ap(new DataBufferHeap(byte_size, 0)); 462 Status error; 463 const size_t bytes_read = process_sp->ReadMemory( 464 addr, data_ap->GetBytes(), data_ap->GetByteSize(), error); 465 if (bytes_read == byte_size) 466 data_sp.reset(data_ap.release()); 467 } 468 return data_sp; 469 } 470 471 size_t ObjectFile::GetData(lldb::offset_t offset, size_t length, 472 DataExtractor &data) const { 473 // The entire file has already been mmap'ed into m_data, so just copy from 474 // there as the back mmap buffer will be shared with shared pointers. 475 return data.SetData(m_data, offset, length); 476 } 477 478 size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length, 479 void *dst) const { 480 // The entire file has already been mmap'ed into m_data, so just copy from 481 // there Note that the data remains in target byte order. 482 return m_data.CopyData(offset, length, dst); 483 } 484 485 size_t ObjectFile::ReadSectionData(Section *section, 486 lldb::offset_t section_offset, void *dst, 487 size_t dst_len) { 488 assert(section); 489 section_offset *= section->GetTargetByteSize(); 490 491 // If some other objectfile owns this data, pass this to them. 492 if (section->GetObjectFile() != this) 493 return section->GetObjectFile()->ReadSectionData(section, section_offset, 494 dst, dst_len); 495 496 if (IsInMemory()) { 497 ProcessSP process_sp(m_process_wp.lock()); 498 if (process_sp) { 499 Status error; 500 const addr_t base_load_addr = 501 section->GetLoadBaseAddress(&process_sp->GetTarget()); 502 if (base_load_addr != LLDB_INVALID_ADDRESS) 503 return process_sp->ReadMemory(base_load_addr + section_offset, dst, 504 dst_len, error); 505 } 506 } else { 507 if (!section->IsRelocated()) 508 RelocateSection(section); 509 510 const lldb::offset_t section_file_size = section->GetFileSize(); 511 if (section_offset < section_file_size) { 512 const size_t section_bytes_left = section_file_size - section_offset; 513 size_t section_dst_len = dst_len; 514 if (section_dst_len > section_bytes_left) 515 section_dst_len = section_bytes_left; 516 return CopyData(section->GetFileOffset() + section_offset, 517 section_dst_len, dst); 518 } else { 519 if (section->GetType() == eSectionTypeZeroFill) { 520 const uint64_t section_size = section->GetByteSize(); 521 const uint64_t section_bytes_left = section_size - section_offset; 522 uint64_t section_dst_len = dst_len; 523 if (section_dst_len > section_bytes_left) 524 section_dst_len = section_bytes_left; 525 memset(dst, 0, section_dst_len); 526 return section_dst_len; 527 } 528 } 529 } 530 return 0; 531 } 532 533 //---------------------------------------------------------------------- 534 // Get the section data the file on disk 535 //---------------------------------------------------------------------- 536 size_t ObjectFile::ReadSectionData(Section *section, 537 DataExtractor §ion_data) { 538 // If some other objectfile owns this data, pass this to them. 539 if (section->GetObjectFile() != this) 540 return section->GetObjectFile()->ReadSectionData(section, section_data); 541 542 if (IsInMemory()) { 543 ProcessSP process_sp(m_process_wp.lock()); 544 if (process_sp) { 545 const addr_t base_load_addr = 546 section->GetLoadBaseAddress(&process_sp->GetTarget()); 547 if (base_load_addr != LLDB_INVALID_ADDRESS) { 548 DataBufferSP data_sp( 549 ReadMemory(process_sp, base_load_addr, section->GetByteSize())); 550 if (data_sp) { 551 section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 552 section_data.SetByteOrder(process_sp->GetByteOrder()); 553 section_data.SetAddressByteSize(process_sp->GetAddressByteSize()); 554 return section_data.GetByteSize(); 555 } 556 } 557 } 558 return GetData(section->GetFileOffset(), section->GetFileSize(), 559 section_data); 560 } else { 561 // The object file now contains a full mmap'ed copy of the object file 562 // data, so just use this 563 if (!section->IsRelocated()) 564 RelocateSection(section); 565 566 return GetData(section->GetFileOffset(), section->GetFileSize(), 567 section_data); 568 } 569 } 570 571 bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object, 572 FileSpec &archive_file, 573 ConstString &archive_object, 574 bool must_exist) { 575 RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$")); 576 RegularExpression::Match regex_match(2); 577 if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object), 578 ®ex_match)) { 579 std::string path; 580 std::string obj; 581 if (regex_match.GetMatchAtIndex(path_with_object, 1, path) && 582 regex_match.GetMatchAtIndex(path_with_object, 2, obj)) { 583 archive_file.SetFile(path, false, FileSpec::Style::native); 584 archive_object.SetCString(obj.c_str()); 585 if (must_exist && !archive_file.Exists()) 586 return false; 587 return true; 588 } 589 } 590 return false; 591 } 592 593 void ObjectFile::ClearSymtab() { 594 ModuleSP module_sp(GetModule()); 595 if (module_sp) { 596 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 597 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 598 if (log) 599 log->Printf("%p ObjectFile::ClearSymtab () symtab = %p", 600 static_cast<void *>(this), 601 static_cast<void *>(m_symtab_ap.get())); 602 m_symtab_ap.reset(); 603 } 604 } 605 606 SectionList *ObjectFile::GetSectionList(bool update_module_section_list) { 607 if (m_sections_ap.get() == nullptr) { 608 if (update_module_section_list) { 609 ModuleSP module_sp(GetModule()); 610 if (module_sp) { 611 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 612 CreateSections(*module_sp->GetUnifiedSectionList()); 613 } 614 } else { 615 SectionList unified_section_list; 616 CreateSections(unified_section_list); 617 } 618 } 619 return m_sections_ap.get(); 620 } 621 622 lldb::SymbolType 623 ObjectFile::GetSymbolTypeFromName(llvm::StringRef name, 624 lldb::SymbolType symbol_type_hint) { 625 if (!name.empty()) { 626 if (name.startswith("_OBJC_")) { 627 // ObjC 628 if (name.startswith("_OBJC_CLASS_$_")) 629 return lldb::eSymbolTypeObjCClass; 630 if (name.startswith("_OBJC_METACLASS_$_")) 631 return lldb::eSymbolTypeObjCMetaClass; 632 if (name.startswith("_OBJC_IVAR_$_")) 633 return lldb::eSymbolTypeObjCIVar; 634 } else if (name.startswith(".objc_class_name_")) { 635 // ObjC v1 636 return lldb::eSymbolTypeObjCClass; 637 } 638 } 639 return symbol_type_hint; 640 } 641 642 ConstString ObjectFile::GetNextSyntheticSymbolName() { 643 StreamString ss; 644 ConstString file_name = GetModule()->GetFileSpec().GetFilename(); 645 ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx, 646 file_name.GetCString()); 647 return ConstString(ss.GetString()); 648 } 649 650 std::vector<ObjectFile::LoadableData> 651 ObjectFile::GetLoadableData(Target &target) { 652 std::vector<LoadableData> loadables; 653 SectionList *section_list = GetSectionList(); 654 if (!section_list) 655 return loadables; 656 // Create a list of loadable data from loadable sections 657 size_t section_count = section_list->GetNumSections(0); 658 for (size_t i = 0; i < section_count; ++i) { 659 LoadableData loadable; 660 SectionSP section_sp = section_list->GetSectionAtIndex(i); 661 loadable.Dest = 662 target.GetSectionLoadList().GetSectionLoadAddress(section_sp); 663 if (loadable.Dest == LLDB_INVALID_ADDRESS) 664 continue; 665 // We can skip sections like bss 666 if (section_sp->GetFileSize() == 0) 667 continue; 668 DataExtractor section_data; 669 section_sp->GetSectionData(section_data); 670 loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(), 671 section_data.GetByteSize()); 672 loadables.push_back(loadable); 673 } 674 return loadables; 675 } 676 677 void ObjectFile::RelocateSection(lldb_private::Section *section) 678 { 679 } 680 681 DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size, 682 uint64_t Offset) { 683 return DataBufferLLVM::CreateSliceFromPath(file.GetPath(), Size, Offset); 684 } 685