Path: blob/main/contrib/llvm-project/lldb/source/Symbol/ObjectFile.cpp
39587 views
//===-- ObjectFile.cpp ----------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "lldb/Symbol/ObjectFile.h"9#include "lldb/Core/Module.h"10#include "lldb/Core/ModuleSpec.h"11#include "lldb/Core/PluginManager.h"12#include "lldb/Core/Section.h"13#include "lldb/Symbol/CallFrameInfo.h"14#include "lldb/Symbol/ObjectContainer.h"15#include "lldb/Symbol/SymbolFile.h"16#include "lldb/Target/Process.h"17#include "lldb/Target/SectionLoadList.h"18#include "lldb/Target/Target.h"19#include "lldb/Utility/DataBuffer.h"20#include "lldb/Utility/DataBufferHeap.h"21#include "lldb/Utility/LLDBLog.h"22#include "lldb/Utility/Log.h"23#include "lldb/Utility/Timer.h"24#include "lldb/lldb-private.h"2526#include "llvm/Support/DJB.h"2728using namespace lldb;29using namespace lldb_private;3031char ObjectFile::ID;32size_t ObjectFile::g_initial_bytes_to_read = 512;3334static ObjectFileSP35CreateObjectFromContainer(const lldb::ModuleSP &module_sp, const FileSpec *file,36lldb::offset_t file_offset, lldb::offset_t file_size,37DataBufferSP data_sp, lldb::offset_t &data_offset) {38ObjectContainerCreateInstance callback;39for (uint32_t idx = 0;40(callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(41idx)) != nullptr;42++idx) {43std::unique_ptr<ObjectContainer> object_container_up(callback(44module_sp, data_sp, data_offset, file, file_offset, file_size));45if (object_container_up)46return object_container_up->GetObjectFile(file);47}48return {};49}5051ObjectFileSP52ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,53lldb::offset_t file_offset, lldb::offset_t file_size,54DataBufferSP &data_sp, lldb::offset_t &data_offset) {55LLDB_SCOPED_TIMERF(56"ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "57"0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",58module_sp->GetFileSpec().GetPath().c_str(),59static_cast<const void *>(file), static_cast<uint64_t>(file_offset),60static_cast<uint64_t>(file_size));6162if (!module_sp)63return {};6465if (!file)66return {};6768if (!data_sp) {69const bool file_exists = FileSystem::Instance().Exists(*file);70// We have an object name which most likely means we have a .o file in71// a static archive (.a file). Try and see if we have a cached archive72// first without reading any data first73if (file_exists && module_sp->GetObjectName()) {74ObjectFileSP object_file_sp = CreateObjectFromContainer(75module_sp, file, file_offset, file_size, data_sp, data_offset);76if (object_file_sp)77return object_file_sp;78}79// Ok, we didn't find any containers that have a named object, now lets80// read the first 512 bytes from the file so the object file and object81// container plug-ins can use these bytes to see if they can parse this82// file.83if (file_size > 0) {84data_sp = FileSystem::Instance().CreateDataBuffer(85file->GetPath(), g_initial_bytes_to_read, file_offset);86data_offset = 0;87}88}8990if (!data_sp || data_sp->GetByteSize() == 0) {91// Check for archive file with format "/path/to/archive.a(object.o)"92llvm::SmallString<256> path_with_object;93module_sp->GetFileSpec().GetPath(path_with_object);9495FileSpec archive_file;96ConstString archive_object;97const bool must_exist = true;98if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,99archive_object, must_exist)) {100file_size = FileSystem::Instance().GetByteSize(archive_file);101if (file_size > 0) {102file = &archive_file;103module_sp->SetFileSpecAndObjectName(archive_file, archive_object);104// Check if this is a object container by iterating through all105// object container plugin instances and then trying to get an106// object file from the container plugins since we had a name.107// Also, don't read108// ANY data in case there is data cached in the container plug-ins109// (like BSD archives caching the contained objects within an110// file).111ObjectFileSP object_file_sp = CreateObjectFromContainer(112module_sp, file, file_offset, file_size, data_sp, data_offset);113if (object_file_sp)114return object_file_sp;115// We failed to find any cached object files in the container plug-116// ins, so lets read the first 512 bytes and try again below...117data_sp = FileSystem::Instance().CreateDataBuffer(118archive_file.GetPath(), g_initial_bytes_to_read, file_offset);119}120}121}122123if (data_sp && data_sp->GetByteSize() > 0) {124// Check if this is a normal object file by iterating through all125// object file plugin instances.126ObjectFileCreateInstance callback;127for (uint32_t idx = 0;128(callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) !=129nullptr;130++idx) {131ObjectFileSP object_file_sp(callback(module_sp, data_sp, data_offset,132file, file_offset, file_size));133if (object_file_sp.get())134return object_file_sp;135}136137// Check if this is a object container by iterating through all object138// container plugin instances and then trying to get an object file139// from the container.140ObjectFileSP object_file_sp = CreateObjectFromContainer(141module_sp, file, file_offset, file_size, data_sp, data_offset);142if (object_file_sp)143return object_file_sp;144}145146// We didn't find it, so clear our shared pointer in case it contains147// anything and return an empty shared pointer148return {};149}150151ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,152const ProcessSP &process_sp,153lldb::addr_t header_addr,154WritableDataBufferSP data_sp) {155ObjectFileSP object_file_sp;156157if (module_sp) {158LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "159"%s, process = %p, header_addr = "160"0x%" PRIx64 ")",161module_sp->GetFileSpec().GetPath().c_str(),162static_cast<void *>(process_sp.get()), header_addr);163uint32_t idx;164165// Check if this is a normal object file by iterating through all object166// file plugin instances.167ObjectFileCreateMemoryInstance create_callback;168for (idx = 0;169(create_callback =170PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) !=171nullptr;172++idx) {173object_file_sp.reset(174create_callback(module_sp, data_sp, process_sp, header_addr));175if (object_file_sp.get())176return object_file_sp;177}178}179180// We didn't find it, so clear our shared pointer in case it contains181// anything and return an empty shared pointer182object_file_sp.reset();183return object_file_sp;184}185186bool ObjectFile::IsObjectFile(lldb_private::FileSpec file_spec) {187DataBufferSP data_sp;188offset_t data_offset = 0;189ModuleSP module_sp = std::make_shared<Module>(file_spec);190return static_cast<bool>(ObjectFile::FindPlugin(191module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),192data_sp, data_offset));193}194195size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,196lldb::offset_t file_offset,197lldb::offset_t file_size,198ModuleSpecList &specs,199DataBufferSP data_sp) {200if (!data_sp)201data_sp = FileSystem::Instance().CreateDataBuffer(202file.GetPath(), g_initial_bytes_to_read, file_offset);203if (data_sp) {204if (file_size == 0) {205const lldb::offset_t actual_file_size =206FileSystem::Instance().GetByteSize(file);207if (actual_file_size > file_offset)208file_size = actual_file_size - file_offset;209}210return ObjectFile::GetModuleSpecifications(file, // file spec211data_sp, // data bytes2120, // data offset213file_offset, // file offset214file_size, // file length215specs);216}217return 0;218}219220size_t ObjectFile::GetModuleSpecifications(221const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,222lldb::offset_t data_offset, lldb::offset_t file_offset,223lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {224const size_t initial_count = specs.GetSize();225ObjectFileGetModuleSpecifications callback;226uint32_t i;227// Try the ObjectFile plug-ins228for (i = 0;229(callback =230PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(231i)) != nullptr;232++i) {233if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)234return specs.GetSize() - initial_count;235}236237// Try the ObjectContainer plug-ins238for (i = 0;239(callback = PluginManager::240GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) !=241nullptr;242++i) {243if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)244return specs.GetSize() - initial_count;245}246return 0;247}248249ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,250const FileSpec *file_spec_ptr,251lldb::offset_t file_offset, lldb::offset_t length,252lldb::DataBufferSP data_sp, lldb::offset_t data_offset)253: ModuleChild(module_sp),254m_file(), // This file could be different from the original module's file255m_type(eTypeInvalid), m_strata(eStrataInvalid),256m_file_offset(file_offset), m_length(length), m_data(), m_process_wp(),257m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(),258m_symtab_once_up(new llvm::once_flag()) {259if (file_spec_ptr)260m_file = *file_spec_ptr;261if (data_sp)262m_data.SetData(data_sp, data_offset, length);263Log *log = GetLog(LLDBLog::Object);264LLDB_LOGF(log,265"%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "266"file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,267static_cast<void *>(this), static_cast<void *>(module_sp.get()),268module_sp->GetSpecificationDescription().c_str(),269m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,270m_length);271}272273ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,274const ProcessSP &process_sp, lldb::addr_t header_addr,275DataBufferSP header_data_sp)276: ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),277m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),278m_process_wp(process_sp), m_memory_addr(header_addr), m_sections_up(),279m_symtab_up(), m_symtab_once_up(new llvm::once_flag()) {280if (header_data_sp)281m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());282Log *log = GetLog(LLDBLog::Object);283LLDB_LOGF(log,284"%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "285"header_addr = 0x%" PRIx64,286static_cast<void *>(this), static_cast<void *>(module_sp.get()),287module_sp->GetSpecificationDescription().c_str(),288static_cast<void *>(process_sp.get()), m_memory_addr);289}290291ObjectFile::~ObjectFile() {292Log *log = GetLog(LLDBLog::Object);293LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));294}295296bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {297ModuleSP module_sp(GetModule());298if (module_sp)299return module_sp->SetArchitecture(new_arch);300return false;301}302303AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {304Symtab *symtab = GetSymtab();305if (symtab) {306Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);307if (symbol) {308if (symbol->ValueIsAddress()) {309const SectionSP section_sp(symbol->GetAddressRef().GetSection());310if (section_sp) {311const SectionType section_type = section_sp->GetType();312switch (section_type) {313case eSectionTypeInvalid:314return AddressClass::eUnknown;315case eSectionTypeCode:316return AddressClass::eCode;317case eSectionTypeContainer:318return AddressClass::eUnknown;319case eSectionTypeData:320case eSectionTypeDataCString:321case eSectionTypeDataCStringPointers:322case eSectionTypeDataSymbolAddress:323case eSectionTypeData4:324case eSectionTypeData8:325case eSectionTypeData16:326case eSectionTypeDataPointers:327case eSectionTypeZeroFill:328case eSectionTypeDataObjCMessageRefs:329case eSectionTypeDataObjCCFStrings:330case eSectionTypeGoSymtab:331return AddressClass::eData;332case eSectionTypeDebug:333case eSectionTypeDWARFDebugAbbrev:334case eSectionTypeDWARFDebugAbbrevDwo:335case eSectionTypeDWARFDebugAddr:336case eSectionTypeDWARFDebugAranges:337case eSectionTypeDWARFDebugCuIndex:338case eSectionTypeDWARFDebugFrame:339case eSectionTypeDWARFDebugInfo:340case eSectionTypeDWARFDebugInfoDwo:341case eSectionTypeDWARFDebugLine:342case eSectionTypeDWARFDebugLineStr:343case eSectionTypeDWARFDebugLoc:344case eSectionTypeDWARFDebugLocDwo:345case eSectionTypeDWARFDebugLocLists:346case eSectionTypeDWARFDebugLocListsDwo:347case eSectionTypeDWARFDebugMacInfo:348case eSectionTypeDWARFDebugMacro:349case eSectionTypeDWARFDebugNames:350case eSectionTypeDWARFDebugPubNames:351case eSectionTypeDWARFDebugPubTypes:352case eSectionTypeDWARFDebugRanges:353case eSectionTypeDWARFDebugRngLists:354case eSectionTypeDWARFDebugRngListsDwo:355case eSectionTypeDWARFDebugStr:356case eSectionTypeDWARFDebugStrDwo:357case eSectionTypeDWARFDebugStrOffsets:358case eSectionTypeDWARFDebugStrOffsetsDwo:359case eSectionTypeDWARFDebugTuIndex:360case eSectionTypeDWARFDebugTypes:361case eSectionTypeDWARFDebugTypesDwo:362case eSectionTypeDWARFAppleNames:363case eSectionTypeDWARFAppleTypes:364case eSectionTypeDWARFAppleNamespaces:365case eSectionTypeDWARFAppleObjC:366case eSectionTypeDWARFGNUDebugAltLink:367case eSectionTypeCTF:368case eSectionTypeSwiftModules:369return AddressClass::eDebug;370case eSectionTypeEHFrame:371case eSectionTypeARMexidx:372case eSectionTypeARMextab:373case eSectionTypeCompactUnwind:374return AddressClass::eRuntime;375case eSectionTypeELFSymbolTable:376case eSectionTypeELFDynamicSymbols:377case eSectionTypeELFRelocationEntries:378case eSectionTypeELFDynamicLinkInfo:379case eSectionTypeOther:380return AddressClass::eUnknown;381case eSectionTypeAbsoluteAddress:382// In case of absolute sections decide the address class based on383// the symbol type because the section type isn't specify if it is384// a code or a data section.385break;386}387}388}389390const SymbolType symbol_type = symbol->GetType();391switch (symbol_type) {392case eSymbolTypeAny:393return AddressClass::eUnknown;394case eSymbolTypeAbsolute:395return AddressClass::eUnknown;396case eSymbolTypeCode:397return AddressClass::eCode;398case eSymbolTypeTrampoline:399return AddressClass::eCode;400case eSymbolTypeResolver:401return AddressClass::eCode;402case eSymbolTypeData:403return AddressClass::eData;404case eSymbolTypeRuntime:405return AddressClass::eRuntime;406case eSymbolTypeException:407return AddressClass::eRuntime;408case eSymbolTypeSourceFile:409return AddressClass::eDebug;410case eSymbolTypeHeaderFile:411return AddressClass::eDebug;412case eSymbolTypeObjectFile:413return AddressClass::eDebug;414case eSymbolTypeCommonBlock:415return AddressClass::eDebug;416case eSymbolTypeBlock:417return AddressClass::eDebug;418case eSymbolTypeLocal:419return AddressClass::eData;420case eSymbolTypeParam:421return AddressClass::eData;422case eSymbolTypeVariable:423return AddressClass::eData;424case eSymbolTypeVariableType:425return AddressClass::eDebug;426case eSymbolTypeLineEntry:427return AddressClass::eDebug;428case eSymbolTypeLineHeader:429return AddressClass::eDebug;430case eSymbolTypeScopeBegin:431return AddressClass::eDebug;432case eSymbolTypeScopeEnd:433return AddressClass::eDebug;434case eSymbolTypeAdditional:435return AddressClass::eUnknown;436case eSymbolTypeCompiler:437return AddressClass::eDebug;438case eSymbolTypeInstrumentation:439return AddressClass::eDebug;440case eSymbolTypeUndefined:441return AddressClass::eUnknown;442case eSymbolTypeObjCClass:443return AddressClass::eRuntime;444case eSymbolTypeObjCMetaClass:445return AddressClass::eRuntime;446case eSymbolTypeObjCIVar:447return AddressClass::eRuntime;448case eSymbolTypeReExported:449return AddressClass::eRuntime;450}451}452}453return AddressClass::eUnknown;454}455456DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,457lldb::addr_t addr, size_t byte_size) {458DataBufferSP data_sp;459if (process_sp) {460std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));461Status error;462const size_t bytes_read = process_sp->ReadMemory(463addr, data_up->GetBytes(), data_up->GetByteSize(), error);464if (bytes_read == byte_size)465data_sp.reset(data_up.release());466}467return data_sp;468}469470size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,471DataExtractor &data) const {472// The entire file has already been mmap'ed into m_data, so just copy from473// there as the back mmap buffer will be shared with shared pointers.474return data.SetData(m_data, offset, length);475}476477size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,478void *dst) const {479// The entire file has already been mmap'ed into m_data, so just copy from480// there Note that the data remains in target byte order.481return m_data.CopyData(offset, length, dst);482}483484size_t ObjectFile::ReadSectionData(Section *section,485lldb::offset_t section_offset, void *dst,486size_t dst_len) {487assert(section);488section_offset *= section->GetTargetByteSize();489490// If some other objectfile owns this data, pass this to them.491if (section->GetObjectFile() != this)492return section->GetObjectFile()->ReadSectionData(section, section_offset,493dst, dst_len);494495if (!section->IsRelocated())496RelocateSection(section);497498if (IsInMemory()) {499ProcessSP process_sp(m_process_wp.lock());500if (process_sp) {501Status error;502const addr_t base_load_addr =503section->GetLoadBaseAddress(&process_sp->GetTarget());504if (base_load_addr != LLDB_INVALID_ADDRESS)505return process_sp->ReadMemory(base_load_addr + section_offset, dst,506dst_len, error);507}508} else {509const lldb::offset_t section_file_size = section->GetFileSize();510if (section_offset < section_file_size) {511const size_t section_bytes_left = section_file_size - section_offset;512size_t section_dst_len = dst_len;513if (section_dst_len > section_bytes_left)514section_dst_len = section_bytes_left;515return CopyData(section->GetFileOffset() + section_offset,516section_dst_len, dst);517} else {518if (section->GetType() == eSectionTypeZeroFill) {519const uint64_t section_size = section->GetByteSize();520const uint64_t section_bytes_left = section_size - section_offset;521uint64_t section_dst_len = dst_len;522if (section_dst_len > section_bytes_left)523section_dst_len = section_bytes_left;524memset(dst, 0, section_dst_len);525return section_dst_len;526}527}528}529return 0;530}531532// Get the section data the file on disk533size_t ObjectFile::ReadSectionData(Section *section,534DataExtractor §ion_data) {535// If some other objectfile owns this data, pass this to them.536if (section->GetObjectFile() != this)537return section->GetObjectFile()->ReadSectionData(section, section_data);538539if (!section->IsRelocated())540RelocateSection(section);541542if (IsInMemory()) {543ProcessSP process_sp(m_process_wp.lock());544if (process_sp) {545const addr_t base_load_addr =546section->GetLoadBaseAddress(&process_sp->GetTarget());547if (base_load_addr != LLDB_INVALID_ADDRESS) {548DataBufferSP data_sp(549ReadMemory(process_sp, base_load_addr, section->GetByteSize()));550if (data_sp) {551section_data.SetData(data_sp, 0, data_sp->GetByteSize());552section_data.SetByteOrder(process_sp->GetByteOrder());553section_data.SetAddressByteSize(process_sp->GetAddressByteSize());554return section_data.GetByteSize();555}556}557}558}559560// The object file now contains a full mmap'ed copy of the object file561// data, so just use this562return GetData(section->GetFileOffset(), GetSectionDataSize(section),563section_data);564}565566bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,567FileSpec &archive_file,568ConstString &archive_object,569bool must_exist) {570size_t len = path_with_object.size();571if (len < 2 || path_with_object.back() != ')')572return false;573llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));574if (archive.empty())575return false;576llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();577archive_file.SetFile(archive, FileSpec::Style::native);578if (must_exist && !FileSystem::Instance().Exists(archive_file))579return false;580archive_object.SetString(object);581return true;582}583584void ObjectFile::ClearSymtab() {585ModuleSP module_sp(GetModule());586if (module_sp) {587Log *log = GetLog(LLDBLog::Object);588LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",589static_cast<void *>(this),590static_cast<void *>(m_symtab_up.get()));591// Since we need to clear the symbol table, we need a new llvm::once_flag592// instance so we can safely create another symbol table593m_symtab_once_up.reset(new llvm::once_flag());594m_symtab_up.reset();595}596}597598SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {599if (m_sections_up == nullptr) {600if (update_module_section_list) {601ModuleSP module_sp(GetModule());602if (module_sp) {603std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());604CreateSections(*module_sp->GetUnifiedSectionList());605}606} else {607SectionList unified_section_list;608CreateSections(unified_section_list);609}610}611return m_sections_up.get();612}613614lldb::SymbolType615ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,616lldb::SymbolType symbol_type_hint) {617if (!name.empty()) {618if (name.starts_with("_OBJC_")) {619// ObjC620if (name.starts_with("_OBJC_CLASS_$_"))621return lldb::eSymbolTypeObjCClass;622if (name.starts_with("_OBJC_METACLASS_$_"))623return lldb::eSymbolTypeObjCMetaClass;624if (name.starts_with("_OBJC_IVAR_$_"))625return lldb::eSymbolTypeObjCIVar;626} else if (name.starts_with(".objc_class_name_")) {627// ObjC v1628return lldb::eSymbolTypeObjCClass;629}630}631return symbol_type_hint;632}633634std::vector<ObjectFile::LoadableData>635ObjectFile::GetLoadableData(Target &target) {636std::vector<LoadableData> loadables;637SectionList *section_list = GetSectionList();638if (!section_list)639return loadables;640// Create a list of loadable data from loadable sections641size_t section_count = section_list->GetNumSections(0);642for (size_t i = 0; i < section_count; ++i) {643LoadableData loadable;644SectionSP section_sp = section_list->GetSectionAtIndex(i);645loadable.Dest =646target.GetSectionLoadList().GetSectionLoadAddress(section_sp);647if (loadable.Dest == LLDB_INVALID_ADDRESS)648continue;649// We can skip sections like bss650if (section_sp->GetFileSize() == 0)651continue;652DataExtractor section_data;653section_sp->GetSectionData(section_data);654loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),655section_data.GetByteSize());656loadables.push_back(loadable);657}658return loadables;659}660661std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {662return {};663}664665void ObjectFile::RelocateSection(lldb_private::Section *section)666{667}668669DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size,670uint64_t Offset) {671return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);672}673674void llvm::format_provider<ObjectFile::Type>::format(675const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {676switch (type) {677case ObjectFile::eTypeInvalid:678OS << "invalid";679break;680case ObjectFile::eTypeCoreFile:681OS << "core file";682break;683case ObjectFile::eTypeExecutable:684OS << "executable";685break;686case ObjectFile::eTypeDebugInfo:687OS << "debug info";688break;689case ObjectFile::eTypeDynamicLinker:690OS << "dynamic linker";691break;692case ObjectFile::eTypeObjectFile:693OS << "object file";694break;695case ObjectFile::eTypeSharedLibrary:696OS << "shared library";697break;698case ObjectFile::eTypeStubLibrary:699OS << "stub library";700break;701case ObjectFile::eTypeJIT:702OS << "jit";703break;704case ObjectFile::eTypeUnknown:705OS << "unknown";706break;707}708}709710void llvm::format_provider<ObjectFile::Strata>::format(711const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {712switch (strata) {713case ObjectFile::eStrataInvalid:714OS << "invalid";715break;716case ObjectFile::eStrataUnknown:717OS << "unknown";718break;719case ObjectFile::eStrataUser:720OS << "user";721break;722case ObjectFile::eStrataKernel:723OS << "kernel";724break;725case ObjectFile::eStrataRawImage:726OS << "raw image";727break;728case ObjectFile::eStrataJIT:729OS << "jit";730break;731}732}733734735Symtab *ObjectFile::GetSymtab() {736ModuleSP module_sp(GetModule());737if (module_sp) {738// We can't take the module lock in ObjectFile::GetSymtab() or we can739// deadlock in DWARF indexing when any file asks for the symbol table from740// an object file. This currently happens in the preloading of symbols in741// SymbolFileDWARF::PreloadSymbols() because the main thread will take the742// module lock, and then threads will be spun up to index the DWARF and743// any of those threads might end up trying to relocate items in the DWARF744// sections which causes ObjectFile::GetSectionData(...) to relocate section745// data which requires the symbol table.746//747// So to work around this, we create the symbol table one time using748// llvm::once_flag, lock it, and then set the unique pointer. Any other749// thread that gets ahold of the symbol table before parsing is done, will750// not be able to access the symbol table contents since all APIs in Symtab751// are protected by a mutex in the Symtab object itself.752llvm::call_once(*m_symtab_once_up, [&]() {753Symtab *symtab = new Symtab(this);754std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());755m_symtab_up.reset(symtab);756if (!m_symtab_up->LoadFromCache()) {757ElapsedTime elapsed(module_sp->GetSymtabParseTime());758ParseSymtab(*m_symtab_up);759m_symtab_up->Finalize();760}761});762}763return m_symtab_up.get();764}765766uint32_t ObjectFile::GetCacheHash() {767if (m_cache_hash)768return *m_cache_hash;769StreamString strm;770strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());771m_cache_hash = llvm::djbHash(strm.GetString());772return *m_cache_hash;773}774775namespace llvm {776namespace json {777778bool fromJSON(const llvm::json::Value &value,779lldb_private::ObjectFile::Type &type, llvm::json::Path path) {780if (auto str = value.getAsString()) {781type = llvm::StringSwitch<ObjectFile::Type>(*str)782.Case("corefile", ObjectFile::eTypeCoreFile)783.Case("executable", ObjectFile::eTypeExecutable)784.Case("debuginfo", ObjectFile::eTypeDebugInfo)785.Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)786.Case("objectfile", ObjectFile::eTypeObjectFile)787.Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)788.Case("stublibrary", ObjectFile::eTypeStubLibrary)789.Case("jit", ObjectFile::eTypeJIT)790.Case("unknown", ObjectFile::eTypeUnknown)791.Default(ObjectFile::eTypeInvalid);792793if (type == ObjectFile::eTypeInvalid) {794path.report("invalid object type");795return false;796}797798return true;799}800path.report("expected string");801return false;802}803} // namespace json804} // namespace llvm805806807