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 = FileSystem::Instance().Exists(*file); 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 = FileSystem::Instance().GetByteSize(archive_file); 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 = 216 FileSystem::Instance().GetByteSize(file); 217 if (actual_file_size > file_offset) 218 file_size = actual_file_size - file_offset; 219 } 220 return ObjectFile::GetModuleSpecifications(file, // file spec 221 data_sp, // data bytes 222 0, // data offset 223 file_offset, // file offset 224 file_size, // file length 225 specs); 226 } 227 return 0; 228 } 229 230 size_t ObjectFile::GetModuleSpecifications( 231 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp, 232 lldb::offset_t data_offset, lldb::offset_t file_offset, 233 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) { 234 const size_t initial_count = specs.GetSize(); 235 ObjectFileGetModuleSpecifications callback; 236 uint32_t i; 237 // Try the ObjectFile plug-ins 238 for (i = 0; 239 (callback = 240 PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex( 241 i)) != nullptr; 242 ++i) { 243 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) 244 return specs.GetSize() - initial_count; 245 } 246 247 // Try the ObjectContainer plug-ins 248 for (i = 0; 249 (callback = PluginManager:: 250 GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) != 251 nullptr; 252 ++i) { 253 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0) 254 return specs.GetSize() - initial_count; 255 } 256 return 0; 257 } 258 259 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, 260 const FileSpec *file_spec_ptr, 261 lldb::offset_t file_offset, lldb::offset_t length, 262 const lldb::DataBufferSP &data_sp, 263 lldb::offset_t data_offset) 264 : ModuleChild(module_sp), 265 m_file(), // This file could be different from the original module's file 266 m_type(eTypeInvalid), m_strata(eStrataInvalid), 267 m_file_offset(file_offset), m_length(length), m_data(), 268 m_unwind_table(*this), m_process_wp(), 269 m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_ap(), m_symtab_ap(), 270 m_synthetic_symbol_idx(0) { 271 if (file_spec_ptr) 272 m_file = *file_spec_ptr; 273 if (data_sp) 274 m_data.SetData(data_sp, data_offset, length); 275 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 276 if (log) 277 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, " 278 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64, 279 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 280 module_sp->GetSpecificationDescription().c_str(), 281 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset, 282 m_length); 283 } 284 285 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp, 286 const ProcessSP &process_sp, lldb::addr_t header_addr, 287 DataBufferSP &header_data_sp) 288 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid), 289 m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(), 290 m_unwind_table(*this), m_process_wp(process_sp), 291 m_memory_addr(header_addr), m_sections_ap(), m_symtab_ap(), 292 m_synthetic_symbol_idx(0) { 293 if (header_data_sp) 294 m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize()); 295 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 296 if (log) 297 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, " 298 "header_addr = 0x%" PRIx64, 299 static_cast<void *>(this), static_cast<void *>(module_sp.get()), 300 module_sp->GetSpecificationDescription().c_str(), 301 static_cast<void *>(process_sp.get()), m_memory_addr); 302 } 303 304 ObjectFile::~ObjectFile() { 305 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 306 if (log) 307 log->Printf("%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this)); 308 } 309 310 bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) { 311 ModuleSP module_sp(GetModule()); 312 if (module_sp) 313 return module_sp->SetArchitecture(new_arch); 314 return false; 315 } 316 317 AddressClass ObjectFile::GetAddressClass(addr_t file_addr) { 318 Symtab *symtab = GetSymtab(); 319 if (symtab) { 320 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr); 321 if (symbol) { 322 if (symbol->ValueIsAddress()) { 323 const SectionSP section_sp(symbol->GetAddressRef().GetSection()); 324 if (section_sp) { 325 const SectionType section_type = section_sp->GetType(); 326 switch (section_type) { 327 case eSectionTypeInvalid: 328 return AddressClass::eUnknown; 329 case eSectionTypeCode: 330 return AddressClass::eCode; 331 case eSectionTypeContainer: 332 return AddressClass::eUnknown; 333 case eSectionTypeData: 334 case eSectionTypeDataCString: 335 case eSectionTypeDataCStringPointers: 336 case eSectionTypeDataSymbolAddress: 337 case eSectionTypeData4: 338 case eSectionTypeData8: 339 case eSectionTypeData16: 340 case eSectionTypeDataPointers: 341 case eSectionTypeZeroFill: 342 case eSectionTypeDataObjCMessageRefs: 343 case eSectionTypeDataObjCCFStrings: 344 case eSectionTypeGoSymtab: 345 return AddressClass::eData; 346 case eSectionTypeDebug: 347 case eSectionTypeDWARFDebugAbbrev: 348 case eSectionTypeDWARFDebugAddr: 349 case eSectionTypeDWARFDebugAranges: 350 case eSectionTypeDWARFDebugCuIndex: 351 case eSectionTypeDWARFDebugFrame: 352 case eSectionTypeDWARFDebugInfo: 353 case eSectionTypeDWARFDebugLine: 354 case eSectionTypeDWARFDebugLineStr: 355 case eSectionTypeDWARFDebugLoc: 356 case eSectionTypeDWARFDebugLocLists: 357 case eSectionTypeDWARFDebugMacInfo: 358 case eSectionTypeDWARFDebugMacro: 359 case eSectionTypeDWARFDebugNames: 360 case eSectionTypeDWARFDebugPubNames: 361 case eSectionTypeDWARFDebugPubTypes: 362 case eSectionTypeDWARFDebugRanges: 363 case eSectionTypeDWARFDebugRngLists: 364 case eSectionTypeDWARFDebugStr: 365 case eSectionTypeDWARFDebugStrOffsets: 366 case eSectionTypeDWARFDebugTypes: 367 case eSectionTypeDWARFAppleNames: 368 case eSectionTypeDWARFAppleTypes: 369 case eSectionTypeDWARFAppleNamespaces: 370 case eSectionTypeDWARFAppleObjC: 371 case eSectionTypeDWARFGNUDebugAltLink: 372 return AddressClass::eDebug; 373 case eSectionTypeEHFrame: 374 case eSectionTypeARMexidx: 375 case eSectionTypeARMextab: 376 case eSectionTypeCompactUnwind: 377 return AddressClass::eRuntime; 378 case eSectionTypeELFSymbolTable: 379 case eSectionTypeELFDynamicSymbols: 380 case eSectionTypeELFRelocationEntries: 381 case eSectionTypeELFDynamicLinkInfo: 382 case eSectionTypeOther: 383 return AddressClass::eUnknown; 384 case eSectionTypeAbsoluteAddress: 385 // In case of absolute sections decide the address class based on 386 // the symbol type because the section type isn't specify if it is 387 // a code or a data section. 388 break; 389 } 390 } 391 } 392 393 const SymbolType symbol_type = symbol->GetType(); 394 switch (symbol_type) { 395 case eSymbolTypeAny: 396 return AddressClass::eUnknown; 397 case eSymbolTypeAbsolute: 398 return AddressClass::eUnknown; 399 case eSymbolTypeCode: 400 return AddressClass::eCode; 401 case eSymbolTypeTrampoline: 402 return AddressClass::eCode; 403 case eSymbolTypeResolver: 404 return AddressClass::eCode; 405 case eSymbolTypeData: 406 return AddressClass::eData; 407 case eSymbolTypeRuntime: 408 return AddressClass::eRuntime; 409 case eSymbolTypeException: 410 return AddressClass::eRuntime; 411 case eSymbolTypeSourceFile: 412 return AddressClass::eDebug; 413 case eSymbolTypeHeaderFile: 414 return AddressClass::eDebug; 415 case eSymbolTypeObjectFile: 416 return AddressClass::eDebug; 417 case eSymbolTypeCommonBlock: 418 return AddressClass::eDebug; 419 case eSymbolTypeBlock: 420 return AddressClass::eDebug; 421 case eSymbolTypeLocal: 422 return AddressClass::eData; 423 case eSymbolTypeParam: 424 return AddressClass::eData; 425 case eSymbolTypeVariable: 426 return AddressClass::eData; 427 case eSymbolTypeVariableType: 428 return AddressClass::eDebug; 429 case eSymbolTypeLineEntry: 430 return AddressClass::eDebug; 431 case eSymbolTypeLineHeader: 432 return AddressClass::eDebug; 433 case eSymbolTypeScopeBegin: 434 return AddressClass::eDebug; 435 case eSymbolTypeScopeEnd: 436 return AddressClass::eDebug; 437 case eSymbolTypeAdditional: 438 return AddressClass::eUnknown; 439 case eSymbolTypeCompiler: 440 return AddressClass::eDebug; 441 case eSymbolTypeInstrumentation: 442 return AddressClass::eDebug; 443 case eSymbolTypeUndefined: 444 return AddressClass::eUnknown; 445 case eSymbolTypeObjCClass: 446 return AddressClass::eRuntime; 447 case eSymbolTypeObjCMetaClass: 448 return AddressClass::eRuntime; 449 case eSymbolTypeObjCIVar: 450 return AddressClass::eRuntime; 451 case eSymbolTypeReExported: 452 return AddressClass::eRuntime; 453 } 454 } 455 } 456 return AddressClass::eUnknown; 457 } 458 459 DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp, 460 lldb::addr_t addr, size_t byte_size) { 461 DataBufferSP data_sp; 462 if (process_sp) { 463 std::unique_ptr<DataBufferHeap> data_ap(new DataBufferHeap(byte_size, 0)); 464 Status error; 465 const size_t bytes_read = process_sp->ReadMemory( 466 addr, data_ap->GetBytes(), data_ap->GetByteSize(), error); 467 if (bytes_read == byte_size) 468 data_sp.reset(data_ap.release()); 469 } 470 return data_sp; 471 } 472 473 size_t ObjectFile::GetData(lldb::offset_t offset, size_t length, 474 DataExtractor &data) const { 475 // The entire file has already been mmap'ed into m_data, so just copy from 476 // there as the back mmap buffer will be shared with shared pointers. 477 return data.SetData(m_data, offset, length); 478 } 479 480 size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length, 481 void *dst) const { 482 // The entire file has already been mmap'ed into m_data, so just copy from 483 // there Note that the data remains in target byte order. 484 return m_data.CopyData(offset, length, dst); 485 } 486 487 size_t ObjectFile::ReadSectionData(Section *section, 488 lldb::offset_t section_offset, void *dst, 489 size_t dst_len) { 490 assert(section); 491 section_offset *= section->GetTargetByteSize(); 492 493 // If some other objectfile owns this data, pass this to them. 494 if (section->GetObjectFile() != this) 495 return section->GetObjectFile()->ReadSectionData(section, section_offset, 496 dst, dst_len); 497 498 if (IsInMemory()) { 499 ProcessSP process_sp(m_process_wp.lock()); 500 if (process_sp) { 501 Status error; 502 const addr_t base_load_addr = 503 section->GetLoadBaseAddress(&process_sp->GetTarget()); 504 if (base_load_addr != LLDB_INVALID_ADDRESS) 505 return process_sp->ReadMemory(base_load_addr + section_offset, dst, 506 dst_len, error); 507 } 508 } else { 509 if (!section->IsRelocated()) 510 RelocateSection(section); 511 512 const lldb::offset_t section_file_size = section->GetFileSize(); 513 if (section_offset < section_file_size) { 514 const size_t section_bytes_left = section_file_size - section_offset; 515 size_t section_dst_len = dst_len; 516 if (section_dst_len > section_bytes_left) 517 section_dst_len = section_bytes_left; 518 return CopyData(section->GetFileOffset() + section_offset, 519 section_dst_len, dst); 520 } else { 521 if (section->GetType() == eSectionTypeZeroFill) { 522 const uint64_t section_size = section->GetByteSize(); 523 const uint64_t section_bytes_left = section_size - section_offset; 524 uint64_t section_dst_len = dst_len; 525 if (section_dst_len > section_bytes_left) 526 section_dst_len = section_bytes_left; 527 memset(dst, 0, section_dst_len); 528 return section_dst_len; 529 } 530 } 531 } 532 return 0; 533 } 534 535 //---------------------------------------------------------------------- 536 // Get the section data the file on disk 537 //---------------------------------------------------------------------- 538 size_t ObjectFile::ReadSectionData(Section *section, 539 DataExtractor §ion_data) { 540 // If some other objectfile owns this data, pass this to them. 541 if (section->GetObjectFile() != this) 542 return section->GetObjectFile()->ReadSectionData(section, section_data); 543 544 if (IsInMemory()) { 545 ProcessSP process_sp(m_process_wp.lock()); 546 if (process_sp) { 547 const addr_t base_load_addr = 548 section->GetLoadBaseAddress(&process_sp->GetTarget()); 549 if (base_load_addr != LLDB_INVALID_ADDRESS) { 550 DataBufferSP data_sp( 551 ReadMemory(process_sp, base_load_addr, section->GetByteSize())); 552 if (data_sp) { 553 section_data.SetData(data_sp, 0, data_sp->GetByteSize()); 554 section_data.SetByteOrder(process_sp->GetByteOrder()); 555 section_data.SetAddressByteSize(process_sp->GetAddressByteSize()); 556 return section_data.GetByteSize(); 557 } 558 } 559 } 560 return GetData(section->GetFileOffset(), section->GetFileSize(), 561 section_data); 562 } else { 563 // The object file now contains a full mmap'ed copy of the object file 564 // data, so just use this 565 if (!section->IsRelocated()) 566 RelocateSection(section); 567 568 return GetData(section->GetFileOffset(), section->GetFileSize(), 569 section_data); 570 } 571 } 572 573 bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object, 574 FileSpec &archive_file, 575 ConstString &archive_object, 576 bool must_exist) { 577 RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$")); 578 RegularExpression::Match regex_match(2); 579 if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object), 580 ®ex_match)) { 581 std::string path; 582 std::string obj; 583 if (regex_match.GetMatchAtIndex(path_with_object, 1, path) && 584 regex_match.GetMatchAtIndex(path_with_object, 2, obj)) { 585 archive_file.SetFile(path, FileSpec::Style::native); 586 archive_object.SetCString(obj.c_str()); 587 if (must_exist && !FileSystem::Instance().Exists(archive_file)) 588 return false; 589 return true; 590 } 591 } 592 return false; 593 } 594 595 void ObjectFile::ClearSymtab() { 596 ModuleSP module_sp(GetModule()); 597 if (module_sp) { 598 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 599 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT)); 600 if (log) 601 log->Printf("%p ObjectFile::ClearSymtab () symtab = %p", 602 static_cast<void *>(this), 603 static_cast<void *>(m_symtab_ap.get())); 604 m_symtab_ap.reset(); 605 } 606 } 607 608 SectionList *ObjectFile::GetSectionList(bool update_module_section_list) { 609 if (m_sections_ap.get() == nullptr) { 610 if (update_module_section_list) { 611 ModuleSP module_sp(GetModule()); 612 if (module_sp) { 613 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex()); 614 CreateSections(*module_sp->GetUnifiedSectionList()); 615 } 616 } else { 617 SectionList unified_section_list; 618 CreateSections(unified_section_list); 619 } 620 } 621 return m_sections_ap.get(); 622 } 623 624 lldb::SymbolType 625 ObjectFile::GetSymbolTypeFromName(llvm::StringRef name, 626 lldb::SymbolType symbol_type_hint) { 627 if (!name.empty()) { 628 if (name.startswith("_OBJC_")) { 629 // ObjC 630 if (name.startswith("_OBJC_CLASS_$_")) 631 return lldb::eSymbolTypeObjCClass; 632 if (name.startswith("_OBJC_METACLASS_$_")) 633 return lldb::eSymbolTypeObjCMetaClass; 634 if (name.startswith("_OBJC_IVAR_$_")) 635 return lldb::eSymbolTypeObjCIVar; 636 } else if (name.startswith(".objc_class_name_")) { 637 // ObjC v1 638 return lldb::eSymbolTypeObjCClass; 639 } 640 } 641 return symbol_type_hint; 642 } 643 644 ConstString ObjectFile::GetNextSyntheticSymbolName() { 645 StreamString ss; 646 ConstString file_name = GetModule()->GetFileSpec().GetFilename(); 647 ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx, 648 file_name.GetCString()); 649 return ConstString(ss.GetString()); 650 } 651 652 std::vector<ObjectFile::LoadableData> 653 ObjectFile::GetLoadableData(Target &target) { 654 std::vector<LoadableData> loadables; 655 SectionList *section_list = GetSectionList(); 656 if (!section_list) 657 return loadables; 658 // Create a list of loadable data from loadable sections 659 size_t section_count = section_list->GetNumSections(0); 660 for (size_t i = 0; i < section_count; ++i) { 661 LoadableData loadable; 662 SectionSP section_sp = section_list->GetSectionAtIndex(i); 663 loadable.Dest = 664 target.GetSectionLoadList().GetSectionLoadAddress(section_sp); 665 if (loadable.Dest == LLDB_INVALID_ADDRESS) 666 continue; 667 // We can skip sections like bss 668 if (section_sp->GetFileSize() == 0) 669 continue; 670 DataExtractor section_data; 671 section_sp->GetSectionData(section_data); 672 loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(), 673 section_data.GetByteSize()); 674 loadables.push_back(loadable); 675 } 676 return loadables; 677 } 678 679 void ObjectFile::RelocateSection(lldb_private::Section *section) 680 { 681 } 682 683 DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size, 684 uint64_t Offset) { 685 return DataBufferLLVM::CreateSliceFromPath(file.GetPath(), Size, Offset); 686 } 687