15ffd83dbSDimitry Andric //===-- ObjectFile.cpp ----------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
100b57cec5SDimitry Andric #include "lldb/Core/Module.h"
110b57cec5SDimitry Andric #include "lldb/Core/ModuleSpec.h"
120b57cec5SDimitry Andric #include "lldb/Core/PluginManager.h"
130b57cec5SDimitry Andric #include "lldb/Core/Section.h"
149dba64beSDimitry Andric #include "lldb/Symbol/CallFrameInfo.h"
150b57cec5SDimitry Andric #include "lldb/Symbol/ObjectContainer.h"
160b57cec5SDimitry Andric #include "lldb/Symbol/SymbolFile.h"
170b57cec5SDimitry Andric #include "lldb/Target/Process.h"
180b57cec5SDimitry Andric #include "lldb/Target/SectionLoadList.h"
190b57cec5SDimitry Andric #include "lldb/Target/Target.h"
200b57cec5SDimitry Andric #include "lldb/Utility/DataBuffer.h"
210b57cec5SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
2281ad6265SDimitry Andric #include "lldb/Utility/LLDBLog.h"
230b57cec5SDimitry Andric #include "lldb/Utility/Log.h"
240b57cec5SDimitry Andric #include "lldb/Utility/Timer.h"
250b57cec5SDimitry Andric #include "lldb/lldb-private.h"
260b57cec5SDimitry Andric 
270eae32dcSDimitry Andric #include "llvm/Support/DJB.h"
280eae32dcSDimitry Andric 
290b57cec5SDimitry Andric using namespace lldb;
300b57cec5SDimitry Andric using namespace lldb_private;
310b57cec5SDimitry Andric 
329dba64beSDimitry Andric char ObjectFile::ID;
3381ad6265SDimitry Andric size_t ObjectFile::g_initial_bytes_to_read = 512;
349dba64beSDimitry Andric 
35e8d8bef9SDimitry Andric static ObjectFileSP
CreateObjectFromContainer(const lldb::ModuleSP & module_sp,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t file_size,DataBufferSP data_sp,lldb::offset_t & data_offset)36e8d8bef9SDimitry Andric CreateObjectFromContainer(const lldb::ModuleSP &module_sp, const FileSpec *file,
37e8d8bef9SDimitry Andric                           lldb::offset_t file_offset, lldb::offset_t file_size,
3881ad6265SDimitry Andric                           DataBufferSP data_sp, lldb::offset_t &data_offset) {
39e8d8bef9SDimitry Andric   ObjectContainerCreateInstance callback;
40e8d8bef9SDimitry Andric   for (uint32_t idx = 0;
41e8d8bef9SDimitry Andric        (callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(
42e8d8bef9SDimitry Andric             idx)) != nullptr;
43e8d8bef9SDimitry Andric        ++idx) {
44e8d8bef9SDimitry Andric     std::unique_ptr<ObjectContainer> object_container_up(callback(
45e8d8bef9SDimitry Andric         module_sp, data_sp, data_offset, file, file_offset, file_size));
46e8d8bef9SDimitry Andric     if (object_container_up)
47e8d8bef9SDimitry Andric       return object_container_up->GetObjectFile(file);
48e8d8bef9SDimitry Andric   }
49e8d8bef9SDimitry Andric   return {};
50e8d8bef9SDimitry Andric }
51e8d8bef9SDimitry Andric 
520b57cec5SDimitry Andric ObjectFileSP
FindPlugin(const lldb::ModuleSP & module_sp,const FileSpec * file,lldb::offset_t file_offset,lldb::offset_t file_size,DataBufferSP & data_sp,lldb::offset_t & data_offset)530b57cec5SDimitry Andric ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
540b57cec5SDimitry Andric                        lldb::offset_t file_offset, lldb::offset_t file_size,
550b57cec5SDimitry Andric                        DataBufferSP &data_sp, lldb::offset_t &data_offset) {
56e8d8bef9SDimitry Andric   LLDB_SCOPED_TIMERF(
570b57cec5SDimitry Andric       "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
580b57cec5SDimitry Andric       "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
590b57cec5SDimitry Andric       module_sp->GetFileSpec().GetPath().c_str(),
600b57cec5SDimitry Andric       static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
610b57cec5SDimitry Andric       static_cast<uint64_t>(file_size));
62e8d8bef9SDimitry Andric 
63e8d8bef9SDimitry Andric   if (!module_sp)
64e8d8bef9SDimitry Andric     return {};
65e8d8bef9SDimitry Andric 
66e8d8bef9SDimitry Andric   if (!file)
67e8d8bef9SDimitry Andric     return {};
680b57cec5SDimitry Andric 
690b57cec5SDimitry Andric   if (!data_sp) {
705ffd83dbSDimitry Andric     const bool file_exists = FileSystem::Instance().Exists(*file);
710b57cec5SDimitry Andric     // We have an object name which most likely means we have a .o file in
720b57cec5SDimitry Andric     // a static archive (.a file). Try and see if we have a cached archive
730b57cec5SDimitry Andric     // first without reading any data first
740b57cec5SDimitry Andric     if (file_exists && module_sp->GetObjectName()) {
75e8d8bef9SDimitry Andric       ObjectFileSP object_file_sp = CreateObjectFromContainer(
76e8d8bef9SDimitry Andric           module_sp, file, file_offset, file_size, data_sp, data_offset);
77e8d8bef9SDimitry Andric       if (object_file_sp)
780b57cec5SDimitry Andric         return object_file_sp;
790b57cec5SDimitry Andric     }
800b57cec5SDimitry Andric     // Ok, we didn't find any containers that have a named object, now lets
810b57cec5SDimitry Andric     // read the first 512 bytes from the file so the object file and object
820b57cec5SDimitry Andric     // container plug-ins can use these bytes to see if they can parse this
830b57cec5SDimitry Andric     // file.
840b57cec5SDimitry Andric     if (file_size > 0) {
8581ad6265SDimitry Andric       data_sp = FileSystem::Instance().CreateDataBuffer(
8681ad6265SDimitry Andric           file->GetPath(), g_initial_bytes_to_read, file_offset);
870b57cec5SDimitry Andric       data_offset = 0;
880b57cec5SDimitry Andric     }
890b57cec5SDimitry Andric   }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric   if (!data_sp || data_sp->GetByteSize() == 0) {
920b57cec5SDimitry Andric     // Check for archive file with format "/path/to/archive.a(object.o)"
939dba64beSDimitry Andric     llvm::SmallString<256> path_with_object;
949dba64beSDimitry Andric     module_sp->GetFileSpec().GetPath(path_with_object);
950b57cec5SDimitry Andric 
96e8d8bef9SDimitry Andric     FileSpec archive_file;
970b57cec5SDimitry Andric     ConstString archive_object;
980b57cec5SDimitry Andric     const bool must_exist = true;
99e8d8bef9SDimitry Andric     if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,
100e8d8bef9SDimitry Andric                                                archive_object, must_exist)) {
1010b57cec5SDimitry Andric       file_size = FileSystem::Instance().GetByteSize(archive_file);
1020b57cec5SDimitry Andric       if (file_size > 0) {
1030b57cec5SDimitry Andric         file = &archive_file;
1040b57cec5SDimitry Andric         module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
1050b57cec5SDimitry Andric         // Check if this is a object container by iterating through all
1060b57cec5SDimitry Andric         // object container plugin instances and then trying to get an
1070b57cec5SDimitry Andric         // object file from the container plugins since we had a name.
1080b57cec5SDimitry Andric         // Also, don't read
1090b57cec5SDimitry Andric         // ANY data in case there is data cached in the container plug-ins
1100b57cec5SDimitry Andric         // (like BSD archives caching the contained objects within an
1110b57cec5SDimitry Andric         // file).
112e8d8bef9SDimitry Andric         ObjectFileSP object_file_sp = CreateObjectFromContainer(
113e8d8bef9SDimitry Andric             module_sp, file, file_offset, file_size, data_sp, data_offset);
114e8d8bef9SDimitry Andric         if (object_file_sp)
1150b57cec5SDimitry Andric           return object_file_sp;
1160b57cec5SDimitry Andric         // We failed to find any cached object files in the container plug-
1170b57cec5SDimitry Andric         // ins, so lets read the first 512 bytes and try again below...
1180b57cec5SDimitry Andric         data_sp = FileSystem::Instance().CreateDataBuffer(
11981ad6265SDimitry Andric             archive_file.GetPath(), g_initial_bytes_to_read, file_offset);
1200b57cec5SDimitry Andric       }
1210b57cec5SDimitry Andric     }
1220b57cec5SDimitry Andric   }
1230b57cec5SDimitry Andric 
1240b57cec5SDimitry Andric   if (data_sp && data_sp->GetByteSize() > 0) {
1250b57cec5SDimitry Andric     // Check if this is a normal object file by iterating through all
1260b57cec5SDimitry Andric     // object file plugin instances.
127e8d8bef9SDimitry Andric     ObjectFileCreateInstance callback;
1280b57cec5SDimitry Andric     for (uint32_t idx = 0;
129e8d8bef9SDimitry Andric          (callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) !=
1300b57cec5SDimitry Andric          nullptr;
1310b57cec5SDimitry Andric          ++idx) {
132e8d8bef9SDimitry Andric       ObjectFileSP object_file_sp(callback(module_sp, data_sp, data_offset,
133e8d8bef9SDimitry Andric                                            file, file_offset, file_size));
1340b57cec5SDimitry Andric       if (object_file_sp.get())
1350b57cec5SDimitry Andric         return object_file_sp;
1360b57cec5SDimitry Andric     }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric     // Check if this is a object container by iterating through all object
1390b57cec5SDimitry Andric     // container plugin instances and then trying to get an object file
1400b57cec5SDimitry Andric     // from the container.
141e8d8bef9SDimitry Andric     ObjectFileSP object_file_sp = CreateObjectFromContainer(
142e8d8bef9SDimitry Andric         module_sp, file, file_offset, file_size, data_sp, data_offset);
143e8d8bef9SDimitry Andric     if (object_file_sp)
1440b57cec5SDimitry Andric       return object_file_sp;
1450b57cec5SDimitry Andric   }
146e8d8bef9SDimitry Andric 
1470b57cec5SDimitry Andric   // We didn't find it, so clear our shared pointer in case it contains
1480b57cec5SDimitry Andric   // anything and return an empty shared pointer
149e8d8bef9SDimitry Andric   return {};
1500b57cec5SDimitry Andric }
1510b57cec5SDimitry Andric 
FindPlugin(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,WritableDataBufferSP data_sp)1520b57cec5SDimitry Andric ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,
1530b57cec5SDimitry Andric                                     const ProcessSP &process_sp,
1540b57cec5SDimitry Andric                                     lldb::addr_t header_addr,
15581ad6265SDimitry Andric                                     WritableDataBufferSP data_sp) {
1560b57cec5SDimitry Andric   ObjectFileSP object_file_sp;
1570b57cec5SDimitry Andric 
1580b57cec5SDimitry Andric   if (module_sp) {
159e8d8bef9SDimitry Andric     LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "
1600b57cec5SDimitry Andric                        "%s, process = %p, header_addr = "
1610b57cec5SDimitry Andric                        "0x%" PRIx64 ")",
1620b57cec5SDimitry Andric                        module_sp->GetFileSpec().GetPath().c_str(),
1630b57cec5SDimitry Andric                        static_cast<void *>(process_sp.get()), header_addr);
1640b57cec5SDimitry Andric     uint32_t idx;
1650b57cec5SDimitry Andric 
1660b57cec5SDimitry Andric     // Check if this is a normal object file by iterating through all object
1670b57cec5SDimitry Andric     // file plugin instances.
1680b57cec5SDimitry Andric     ObjectFileCreateMemoryInstance create_callback;
1690b57cec5SDimitry Andric     for (idx = 0;
1700b57cec5SDimitry Andric          (create_callback =
1710b57cec5SDimitry Andric               PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) !=
1720b57cec5SDimitry Andric          nullptr;
1730b57cec5SDimitry Andric          ++idx) {
1740b57cec5SDimitry Andric       object_file_sp.reset(
1750b57cec5SDimitry Andric           create_callback(module_sp, data_sp, process_sp, header_addr));
1760b57cec5SDimitry Andric       if (object_file_sp.get())
1770b57cec5SDimitry Andric         return object_file_sp;
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric   }
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric   // We didn't find it, so clear our shared pointer in case it contains
1820b57cec5SDimitry Andric   // anything and return an empty shared pointer
1830b57cec5SDimitry Andric   object_file_sp.reset();
1840b57cec5SDimitry Andric   return object_file_sp;
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric 
GetModuleSpecifications(const FileSpec & file,lldb::offset_t file_offset,lldb::offset_t file_size,ModuleSpecList & specs,DataBufferSP data_sp)1870b57cec5SDimitry Andric size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
1880b57cec5SDimitry Andric                                            lldb::offset_t file_offset,
1890b57cec5SDimitry Andric                                            lldb::offset_t file_size,
1905ffd83dbSDimitry Andric                                            ModuleSpecList &specs,
1915ffd83dbSDimitry Andric                                            DataBufferSP data_sp) {
1925ffd83dbSDimitry Andric   if (!data_sp)
19381ad6265SDimitry Andric     data_sp = FileSystem::Instance().CreateDataBuffer(
19481ad6265SDimitry Andric         file.GetPath(), g_initial_bytes_to_read, file_offset);
1950b57cec5SDimitry Andric   if (data_sp) {
1960b57cec5SDimitry Andric     if (file_size == 0) {
1970b57cec5SDimitry Andric       const lldb::offset_t actual_file_size =
1980b57cec5SDimitry Andric           FileSystem::Instance().GetByteSize(file);
1990b57cec5SDimitry Andric       if (actual_file_size > file_offset)
2000b57cec5SDimitry Andric         file_size = actual_file_size - file_offset;
2010b57cec5SDimitry Andric     }
2020b57cec5SDimitry Andric     return ObjectFile::GetModuleSpecifications(file,        // file spec
2030b57cec5SDimitry Andric                                                data_sp,     // data bytes
2040b57cec5SDimitry Andric                                                0,           // data offset
2050b57cec5SDimitry Andric                                                file_offset, // file offset
2060b57cec5SDimitry Andric                                                file_size,   // file length
2070b57cec5SDimitry Andric                                                specs);
2080b57cec5SDimitry Andric   }
2090b57cec5SDimitry Andric   return 0;
2100b57cec5SDimitry Andric }
2110b57cec5SDimitry Andric 
GetModuleSpecifications(const lldb_private::FileSpec & file,lldb::DataBufferSP & data_sp,lldb::offset_t data_offset,lldb::offset_t file_offset,lldb::offset_t file_size,lldb_private::ModuleSpecList & specs)2120b57cec5SDimitry Andric size_t ObjectFile::GetModuleSpecifications(
2130b57cec5SDimitry Andric     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
2140b57cec5SDimitry Andric     lldb::offset_t data_offset, lldb::offset_t file_offset,
2150b57cec5SDimitry Andric     lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
2160b57cec5SDimitry Andric   const size_t initial_count = specs.GetSize();
2170b57cec5SDimitry Andric   ObjectFileGetModuleSpecifications callback;
2180b57cec5SDimitry Andric   uint32_t i;
2190b57cec5SDimitry Andric   // Try the ObjectFile plug-ins
2200b57cec5SDimitry Andric   for (i = 0;
2210b57cec5SDimitry Andric        (callback =
2220b57cec5SDimitry Andric             PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
2230b57cec5SDimitry Andric                 i)) != nullptr;
2240b57cec5SDimitry Andric        ++i) {
2250b57cec5SDimitry Andric     if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
2260b57cec5SDimitry Andric       return specs.GetSize() - initial_count;
2270b57cec5SDimitry Andric   }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric   // Try the ObjectContainer plug-ins
2300b57cec5SDimitry Andric   for (i = 0;
2310b57cec5SDimitry Andric        (callback = PluginManager::
2320b57cec5SDimitry Andric             GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) !=
2330b57cec5SDimitry Andric        nullptr;
2340b57cec5SDimitry Andric        ++i) {
2350b57cec5SDimitry Andric     if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
2360b57cec5SDimitry Andric       return specs.GetSize() - initial_count;
2370b57cec5SDimitry Andric   }
2380b57cec5SDimitry Andric   return 0;
2390b57cec5SDimitry Andric }
2400b57cec5SDimitry Andric 
ObjectFile(const lldb::ModuleSP & module_sp,const FileSpec * file_spec_ptr,lldb::offset_t file_offset,lldb::offset_t length,lldb::DataBufferSP data_sp,lldb::offset_t data_offset)2410b57cec5SDimitry Andric ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
2420b57cec5SDimitry Andric                        const FileSpec *file_spec_ptr,
2430b57cec5SDimitry Andric                        lldb::offset_t file_offset, lldb::offset_t length,
24481ad6265SDimitry Andric                        lldb::DataBufferSP data_sp, lldb::offset_t data_offset)
2450b57cec5SDimitry Andric     : ModuleChild(module_sp),
2460b57cec5SDimitry Andric       m_file(), // This file could be different from the original module's file
2470b57cec5SDimitry Andric       m_type(eTypeInvalid), m_strata(eStrataInvalid),
2480b57cec5SDimitry Andric       m_file_offset(file_offset), m_length(length), m_data(), m_process_wp(),
2490b57cec5SDimitry Andric       m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(),
2504824e7fdSDimitry Andric       m_symtab_once_up(new llvm::once_flag()) {
2510b57cec5SDimitry Andric   if (file_spec_ptr)
2520b57cec5SDimitry Andric     m_file = *file_spec_ptr;
2530b57cec5SDimitry Andric   if (data_sp)
2540b57cec5SDimitry Andric     m_data.SetData(data_sp, data_offset, length);
25581ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
2569dba64beSDimitry Andric   LLDB_LOGF(log,
2579dba64beSDimitry Andric             "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
2580b57cec5SDimitry Andric             "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
2590b57cec5SDimitry Andric             static_cast<void *>(this), static_cast<void *>(module_sp.get()),
2600b57cec5SDimitry Andric             module_sp->GetSpecificationDescription().c_str(),
2610b57cec5SDimitry Andric             m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
2620b57cec5SDimitry Andric             m_length);
2630b57cec5SDimitry Andric }
2640b57cec5SDimitry Andric 
ObjectFile(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,DataBufferSP header_data_sp)2650b57cec5SDimitry Andric ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
2660b57cec5SDimitry Andric                        const ProcessSP &process_sp, lldb::addr_t header_addr,
26781ad6265SDimitry Andric                        DataBufferSP header_data_sp)
2680b57cec5SDimitry Andric     : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
2690b57cec5SDimitry Andric       m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),
2700b57cec5SDimitry Andric       m_process_wp(process_sp), m_memory_addr(header_addr), m_sections_up(),
2714824e7fdSDimitry Andric       m_symtab_up(), m_symtab_once_up(new llvm::once_flag()) {
2720b57cec5SDimitry Andric   if (header_data_sp)
2730b57cec5SDimitry Andric     m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());
27481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
2759dba64beSDimitry Andric   LLDB_LOGF(log,
2769dba64beSDimitry Andric             "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
2770b57cec5SDimitry Andric             "header_addr = 0x%" PRIx64,
2780b57cec5SDimitry Andric             static_cast<void *>(this), static_cast<void *>(module_sp.get()),
2790b57cec5SDimitry Andric             module_sp->GetSpecificationDescription().c_str(),
2800b57cec5SDimitry Andric             static_cast<void *>(process_sp.get()), m_memory_addr);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
~ObjectFile()2830b57cec5SDimitry Andric ObjectFile::~ObjectFile() {
28481ad6265SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
2859dba64beSDimitry Andric   LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
2860b57cec5SDimitry Andric }
2870b57cec5SDimitry Andric 
SetModulesArchitecture(const ArchSpec & new_arch)2880b57cec5SDimitry Andric bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {
2890b57cec5SDimitry Andric   ModuleSP module_sp(GetModule());
2900b57cec5SDimitry Andric   if (module_sp)
2910b57cec5SDimitry Andric     return module_sp->SetArchitecture(new_arch);
2920b57cec5SDimitry Andric   return false;
2930b57cec5SDimitry Andric }
2940b57cec5SDimitry Andric 
GetAddressClass(addr_t file_addr)2950b57cec5SDimitry Andric AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {
2960b57cec5SDimitry Andric   Symtab *symtab = GetSymtab();
2970b57cec5SDimitry Andric   if (symtab) {
2980b57cec5SDimitry Andric     Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
2990b57cec5SDimitry Andric     if (symbol) {
3000b57cec5SDimitry Andric       if (symbol->ValueIsAddress()) {
3010b57cec5SDimitry Andric         const SectionSP section_sp(symbol->GetAddressRef().GetSection());
3020b57cec5SDimitry Andric         if (section_sp) {
3030b57cec5SDimitry Andric           const SectionType section_type = section_sp->GetType();
3040b57cec5SDimitry Andric           switch (section_type) {
3050b57cec5SDimitry Andric           case eSectionTypeInvalid:
3060b57cec5SDimitry Andric             return AddressClass::eUnknown;
3070b57cec5SDimitry Andric           case eSectionTypeCode:
3080b57cec5SDimitry Andric             return AddressClass::eCode;
3090b57cec5SDimitry Andric           case eSectionTypeContainer:
3100b57cec5SDimitry Andric             return AddressClass::eUnknown;
3110b57cec5SDimitry Andric           case eSectionTypeData:
3120b57cec5SDimitry Andric           case eSectionTypeDataCString:
3130b57cec5SDimitry Andric           case eSectionTypeDataCStringPointers:
3140b57cec5SDimitry Andric           case eSectionTypeDataSymbolAddress:
3150b57cec5SDimitry Andric           case eSectionTypeData4:
3160b57cec5SDimitry Andric           case eSectionTypeData8:
3170b57cec5SDimitry Andric           case eSectionTypeData16:
3180b57cec5SDimitry Andric           case eSectionTypeDataPointers:
3190b57cec5SDimitry Andric           case eSectionTypeZeroFill:
3200b57cec5SDimitry Andric           case eSectionTypeDataObjCMessageRefs:
3210b57cec5SDimitry Andric           case eSectionTypeDataObjCCFStrings:
3220b57cec5SDimitry Andric           case eSectionTypeGoSymtab:
3230b57cec5SDimitry Andric             return AddressClass::eData;
3240b57cec5SDimitry Andric           case eSectionTypeDebug:
3250b57cec5SDimitry Andric           case eSectionTypeDWARFDebugAbbrev:
3260b57cec5SDimitry Andric           case eSectionTypeDWARFDebugAbbrevDwo:
3270b57cec5SDimitry Andric           case eSectionTypeDWARFDebugAddr:
3280b57cec5SDimitry Andric           case eSectionTypeDWARFDebugAranges:
3290b57cec5SDimitry Andric           case eSectionTypeDWARFDebugCuIndex:
3300b57cec5SDimitry Andric           case eSectionTypeDWARFDebugFrame:
3310b57cec5SDimitry Andric           case eSectionTypeDWARFDebugInfo:
3320b57cec5SDimitry Andric           case eSectionTypeDWARFDebugInfoDwo:
3330b57cec5SDimitry Andric           case eSectionTypeDWARFDebugLine:
3340b57cec5SDimitry Andric           case eSectionTypeDWARFDebugLineStr:
3350b57cec5SDimitry Andric           case eSectionTypeDWARFDebugLoc:
336480093f4SDimitry Andric           case eSectionTypeDWARFDebugLocDwo:
3370b57cec5SDimitry Andric           case eSectionTypeDWARFDebugLocLists:
338480093f4SDimitry Andric           case eSectionTypeDWARFDebugLocListsDwo:
3390b57cec5SDimitry Andric           case eSectionTypeDWARFDebugMacInfo:
3400b57cec5SDimitry Andric           case eSectionTypeDWARFDebugMacro:
3410b57cec5SDimitry Andric           case eSectionTypeDWARFDebugNames:
3420b57cec5SDimitry Andric           case eSectionTypeDWARFDebugPubNames:
3430b57cec5SDimitry Andric           case eSectionTypeDWARFDebugPubTypes:
3440b57cec5SDimitry Andric           case eSectionTypeDWARFDebugRanges:
3450b57cec5SDimitry Andric           case eSectionTypeDWARFDebugRngLists:
346480093f4SDimitry Andric           case eSectionTypeDWARFDebugRngListsDwo:
3470b57cec5SDimitry Andric           case eSectionTypeDWARFDebugStr:
3480b57cec5SDimitry Andric           case eSectionTypeDWARFDebugStrDwo:
3490b57cec5SDimitry Andric           case eSectionTypeDWARFDebugStrOffsets:
3500b57cec5SDimitry Andric           case eSectionTypeDWARFDebugStrOffsetsDwo:
3515ffd83dbSDimitry Andric           case eSectionTypeDWARFDebugTuIndex:
3520b57cec5SDimitry Andric           case eSectionTypeDWARFDebugTypes:
3530b57cec5SDimitry Andric           case eSectionTypeDWARFDebugTypesDwo:
3540b57cec5SDimitry Andric           case eSectionTypeDWARFAppleNames:
3550b57cec5SDimitry Andric           case eSectionTypeDWARFAppleTypes:
3560b57cec5SDimitry Andric           case eSectionTypeDWARFAppleNamespaces:
3570b57cec5SDimitry Andric           case eSectionTypeDWARFAppleObjC:
3580b57cec5SDimitry Andric           case eSectionTypeDWARFGNUDebugAltLink:
35906c3fb27SDimitry Andric           case eSectionTypeCTF:
3605f757f3fSDimitry Andric           case eSectionTypeSwiftModules:
3610b57cec5SDimitry Andric             return AddressClass::eDebug;
3620b57cec5SDimitry Andric           case eSectionTypeEHFrame:
3630b57cec5SDimitry Andric           case eSectionTypeARMexidx:
3640b57cec5SDimitry Andric           case eSectionTypeARMextab:
3650b57cec5SDimitry Andric           case eSectionTypeCompactUnwind:
3660b57cec5SDimitry Andric             return AddressClass::eRuntime;
3670b57cec5SDimitry Andric           case eSectionTypeELFSymbolTable:
3680b57cec5SDimitry Andric           case eSectionTypeELFDynamicSymbols:
3690b57cec5SDimitry Andric           case eSectionTypeELFRelocationEntries:
3700b57cec5SDimitry Andric           case eSectionTypeELFDynamicLinkInfo:
3710b57cec5SDimitry Andric           case eSectionTypeOther:
3720b57cec5SDimitry Andric             return AddressClass::eUnknown;
3730b57cec5SDimitry Andric           case eSectionTypeAbsoluteAddress:
3740b57cec5SDimitry Andric             // In case of absolute sections decide the address class based on
3750b57cec5SDimitry Andric             // the symbol type because the section type isn't specify if it is
3760b57cec5SDimitry Andric             // a code or a data section.
3770b57cec5SDimitry Andric             break;
3780b57cec5SDimitry Andric           }
3790b57cec5SDimitry Andric         }
3800b57cec5SDimitry Andric       }
3810b57cec5SDimitry Andric 
3820b57cec5SDimitry Andric       const SymbolType symbol_type = symbol->GetType();
3830b57cec5SDimitry Andric       switch (symbol_type) {
3840b57cec5SDimitry Andric       case eSymbolTypeAny:
3850b57cec5SDimitry Andric         return AddressClass::eUnknown;
3860b57cec5SDimitry Andric       case eSymbolTypeAbsolute:
3870b57cec5SDimitry Andric         return AddressClass::eUnknown;
3880b57cec5SDimitry Andric       case eSymbolTypeCode:
3890b57cec5SDimitry Andric         return AddressClass::eCode;
3900b57cec5SDimitry Andric       case eSymbolTypeTrampoline:
3910b57cec5SDimitry Andric         return AddressClass::eCode;
3920b57cec5SDimitry Andric       case eSymbolTypeResolver:
3930b57cec5SDimitry Andric         return AddressClass::eCode;
3940b57cec5SDimitry Andric       case eSymbolTypeData:
3950b57cec5SDimitry Andric         return AddressClass::eData;
3960b57cec5SDimitry Andric       case eSymbolTypeRuntime:
3970b57cec5SDimitry Andric         return AddressClass::eRuntime;
3980b57cec5SDimitry Andric       case eSymbolTypeException:
3990b57cec5SDimitry Andric         return AddressClass::eRuntime;
4000b57cec5SDimitry Andric       case eSymbolTypeSourceFile:
4010b57cec5SDimitry Andric         return AddressClass::eDebug;
4020b57cec5SDimitry Andric       case eSymbolTypeHeaderFile:
4030b57cec5SDimitry Andric         return AddressClass::eDebug;
4040b57cec5SDimitry Andric       case eSymbolTypeObjectFile:
4050b57cec5SDimitry Andric         return AddressClass::eDebug;
4060b57cec5SDimitry Andric       case eSymbolTypeCommonBlock:
4070b57cec5SDimitry Andric         return AddressClass::eDebug;
4080b57cec5SDimitry Andric       case eSymbolTypeBlock:
4090b57cec5SDimitry Andric         return AddressClass::eDebug;
4100b57cec5SDimitry Andric       case eSymbolTypeLocal:
4110b57cec5SDimitry Andric         return AddressClass::eData;
4120b57cec5SDimitry Andric       case eSymbolTypeParam:
4130b57cec5SDimitry Andric         return AddressClass::eData;
4140b57cec5SDimitry Andric       case eSymbolTypeVariable:
4150b57cec5SDimitry Andric         return AddressClass::eData;
4160b57cec5SDimitry Andric       case eSymbolTypeVariableType:
4170b57cec5SDimitry Andric         return AddressClass::eDebug;
4180b57cec5SDimitry Andric       case eSymbolTypeLineEntry:
4190b57cec5SDimitry Andric         return AddressClass::eDebug;
4200b57cec5SDimitry Andric       case eSymbolTypeLineHeader:
4210b57cec5SDimitry Andric         return AddressClass::eDebug;
4220b57cec5SDimitry Andric       case eSymbolTypeScopeBegin:
4230b57cec5SDimitry Andric         return AddressClass::eDebug;
4240b57cec5SDimitry Andric       case eSymbolTypeScopeEnd:
4250b57cec5SDimitry Andric         return AddressClass::eDebug;
4260b57cec5SDimitry Andric       case eSymbolTypeAdditional:
4270b57cec5SDimitry Andric         return AddressClass::eUnknown;
4280b57cec5SDimitry Andric       case eSymbolTypeCompiler:
4290b57cec5SDimitry Andric         return AddressClass::eDebug;
4300b57cec5SDimitry Andric       case eSymbolTypeInstrumentation:
4310b57cec5SDimitry Andric         return AddressClass::eDebug;
4320b57cec5SDimitry Andric       case eSymbolTypeUndefined:
4330b57cec5SDimitry Andric         return AddressClass::eUnknown;
4340b57cec5SDimitry Andric       case eSymbolTypeObjCClass:
4350b57cec5SDimitry Andric         return AddressClass::eRuntime;
4360b57cec5SDimitry Andric       case eSymbolTypeObjCMetaClass:
4370b57cec5SDimitry Andric         return AddressClass::eRuntime;
4380b57cec5SDimitry Andric       case eSymbolTypeObjCIVar:
4390b57cec5SDimitry Andric         return AddressClass::eRuntime;
4400b57cec5SDimitry Andric       case eSymbolTypeReExported:
4410b57cec5SDimitry Andric         return AddressClass::eRuntime;
4420b57cec5SDimitry Andric       }
4430b57cec5SDimitry Andric     }
4440b57cec5SDimitry Andric   }
4450b57cec5SDimitry Andric   return AddressClass::eUnknown;
4460b57cec5SDimitry Andric }
4470b57cec5SDimitry Andric 
ReadMemory(const ProcessSP & process_sp,lldb::addr_t addr,size_t byte_size)4480b57cec5SDimitry Andric DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,
4490b57cec5SDimitry Andric                                     lldb::addr_t addr, size_t byte_size) {
4500b57cec5SDimitry Andric   DataBufferSP data_sp;
4510b57cec5SDimitry Andric   if (process_sp) {
4520b57cec5SDimitry Andric     std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
4530b57cec5SDimitry Andric     Status error;
4540b57cec5SDimitry Andric     const size_t bytes_read = process_sp->ReadMemory(
4550b57cec5SDimitry Andric         addr, data_up->GetBytes(), data_up->GetByteSize(), error);
4560b57cec5SDimitry Andric     if (bytes_read == byte_size)
4570b57cec5SDimitry Andric       data_sp.reset(data_up.release());
4580b57cec5SDimitry Andric   }
4590b57cec5SDimitry Andric   return data_sp;
4600b57cec5SDimitry Andric }
4610b57cec5SDimitry Andric 
GetData(lldb::offset_t offset,size_t length,DataExtractor & data) const4620b57cec5SDimitry Andric size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
4630b57cec5SDimitry Andric                            DataExtractor &data) const {
4640b57cec5SDimitry Andric   // The entire file has already been mmap'ed into m_data, so just copy from
4650b57cec5SDimitry Andric   // there as the back mmap buffer will be shared with shared pointers.
4660b57cec5SDimitry Andric   return data.SetData(m_data, offset, length);
4670b57cec5SDimitry Andric }
4680b57cec5SDimitry Andric 
CopyData(lldb::offset_t offset,size_t length,void * dst) const4690b57cec5SDimitry Andric size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
4700b57cec5SDimitry Andric                             void *dst) const {
4710b57cec5SDimitry Andric   // The entire file has already been mmap'ed into m_data, so just copy from
4720b57cec5SDimitry Andric   // there Note that the data remains in target byte order.
4730b57cec5SDimitry Andric   return m_data.CopyData(offset, length, dst);
4740b57cec5SDimitry Andric }
4750b57cec5SDimitry Andric 
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)4760b57cec5SDimitry Andric size_t ObjectFile::ReadSectionData(Section *section,
4770b57cec5SDimitry Andric                                    lldb::offset_t section_offset, void *dst,
4780b57cec5SDimitry Andric                                    size_t dst_len) {
4790b57cec5SDimitry Andric   assert(section);
4800b57cec5SDimitry Andric   section_offset *= section->GetTargetByteSize();
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric   // If some other objectfile owns this data, pass this to them.
4830b57cec5SDimitry Andric   if (section->GetObjectFile() != this)
4840b57cec5SDimitry Andric     return section->GetObjectFile()->ReadSectionData(section, section_offset,
4850b57cec5SDimitry Andric                                                      dst, dst_len);
4860b57cec5SDimitry Andric 
487e8d8bef9SDimitry Andric   if (!section->IsRelocated())
488e8d8bef9SDimitry Andric     RelocateSection(section);
489e8d8bef9SDimitry Andric 
4900b57cec5SDimitry Andric   if (IsInMemory()) {
4910b57cec5SDimitry Andric     ProcessSP process_sp(m_process_wp.lock());
4920b57cec5SDimitry Andric     if (process_sp) {
4930b57cec5SDimitry Andric       Status error;
4940b57cec5SDimitry Andric       const addr_t base_load_addr =
4950b57cec5SDimitry Andric           section->GetLoadBaseAddress(&process_sp->GetTarget());
4960b57cec5SDimitry Andric       if (base_load_addr != LLDB_INVALID_ADDRESS)
4970b57cec5SDimitry Andric         return process_sp->ReadMemory(base_load_addr + section_offset, dst,
4980b57cec5SDimitry Andric                                       dst_len, error);
4990b57cec5SDimitry Andric     }
5000b57cec5SDimitry Andric   } else {
5010b57cec5SDimitry Andric     const lldb::offset_t section_file_size = section->GetFileSize();
5020b57cec5SDimitry Andric     if (section_offset < section_file_size) {
5030b57cec5SDimitry Andric       const size_t section_bytes_left = section_file_size - section_offset;
5040b57cec5SDimitry Andric       size_t section_dst_len = dst_len;
5050b57cec5SDimitry Andric       if (section_dst_len > section_bytes_left)
5060b57cec5SDimitry Andric         section_dst_len = section_bytes_left;
5070b57cec5SDimitry Andric       return CopyData(section->GetFileOffset() + section_offset,
5080b57cec5SDimitry Andric                       section_dst_len, dst);
5090b57cec5SDimitry Andric     } else {
5100b57cec5SDimitry Andric       if (section->GetType() == eSectionTypeZeroFill) {
5110b57cec5SDimitry Andric         const uint64_t section_size = section->GetByteSize();
5120b57cec5SDimitry Andric         const uint64_t section_bytes_left = section_size - section_offset;
5130b57cec5SDimitry Andric         uint64_t section_dst_len = dst_len;
5140b57cec5SDimitry Andric         if (section_dst_len > section_bytes_left)
5150b57cec5SDimitry Andric           section_dst_len = section_bytes_left;
5160b57cec5SDimitry Andric         memset(dst, 0, section_dst_len);
5170b57cec5SDimitry Andric         return section_dst_len;
5180b57cec5SDimitry Andric       }
5190b57cec5SDimitry Andric     }
5200b57cec5SDimitry Andric   }
5210b57cec5SDimitry Andric   return 0;
5220b57cec5SDimitry Andric }
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric // Get the section data the file on disk
ReadSectionData(Section * section,DataExtractor & section_data)5250b57cec5SDimitry Andric size_t ObjectFile::ReadSectionData(Section *section,
5260b57cec5SDimitry Andric                                    DataExtractor &section_data) {
5270b57cec5SDimitry Andric   // If some other objectfile owns this data, pass this to them.
5280b57cec5SDimitry Andric   if (section->GetObjectFile() != this)
5290b57cec5SDimitry Andric     return section->GetObjectFile()->ReadSectionData(section, section_data);
5300b57cec5SDimitry Andric 
531e8d8bef9SDimitry Andric   if (!section->IsRelocated())
532e8d8bef9SDimitry Andric     RelocateSection(section);
533e8d8bef9SDimitry Andric 
5340b57cec5SDimitry Andric   if (IsInMemory()) {
5350b57cec5SDimitry Andric     ProcessSP process_sp(m_process_wp.lock());
5360b57cec5SDimitry Andric     if (process_sp) {
5370b57cec5SDimitry Andric       const addr_t base_load_addr =
5380b57cec5SDimitry Andric           section->GetLoadBaseAddress(&process_sp->GetTarget());
5390b57cec5SDimitry Andric       if (base_load_addr != LLDB_INVALID_ADDRESS) {
5400b57cec5SDimitry Andric         DataBufferSP data_sp(
5410b57cec5SDimitry Andric             ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
5420b57cec5SDimitry Andric         if (data_sp) {
5430b57cec5SDimitry Andric           section_data.SetData(data_sp, 0, data_sp->GetByteSize());
5440b57cec5SDimitry Andric           section_data.SetByteOrder(process_sp->GetByteOrder());
5450b57cec5SDimitry Andric           section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
5460b57cec5SDimitry Andric           return section_data.GetByteSize();
5470b57cec5SDimitry Andric         }
5480b57cec5SDimitry Andric       }
5490b57cec5SDimitry Andric     }
550e8d8bef9SDimitry Andric   }
551e8d8bef9SDimitry Andric 
5520b57cec5SDimitry Andric   // The object file now contains a full mmap'ed copy of the object file
5530b57cec5SDimitry Andric   // data, so just use this
5545f757f3fSDimitry Andric   return GetData(section->GetFileOffset(), GetSectionDataSize(section),
5550b57cec5SDimitry Andric                  section_data);
5560b57cec5SDimitry Andric }
5570b57cec5SDimitry Andric 
SplitArchivePathWithObject(llvm::StringRef path_with_object,FileSpec & archive_file,ConstString & archive_object,bool must_exist)5589dba64beSDimitry Andric bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
5590b57cec5SDimitry Andric                                             FileSpec &archive_file,
5600b57cec5SDimitry Andric                                             ConstString &archive_object,
5610b57cec5SDimitry Andric                                             bool must_exist) {
5629dba64beSDimitry Andric   size_t len = path_with_object.size();
5639dba64beSDimitry Andric   if (len < 2 || path_with_object.back() != ')')
5640b57cec5SDimitry Andric     return false;
5659dba64beSDimitry Andric   llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
5669dba64beSDimitry Andric   if (archive.empty())
5679dba64beSDimitry Andric     return false;
5689dba64beSDimitry Andric   llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
5699dba64beSDimitry Andric   archive_file.SetFile(archive, FileSpec::Style::native);
5709dba64beSDimitry Andric   if (must_exist && !FileSystem::Instance().Exists(archive_file))
5719dba64beSDimitry Andric     return false;
5729dba64beSDimitry Andric   archive_object.SetString(object);
5739dba64beSDimitry Andric   return true;
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
ClearSymtab()5760b57cec5SDimitry Andric void ObjectFile::ClearSymtab() {
5770b57cec5SDimitry Andric   ModuleSP module_sp(GetModule());
5780b57cec5SDimitry Andric   if (module_sp) {
57981ad6265SDimitry Andric     Log *log = GetLog(LLDBLog::Object);
5809dba64beSDimitry Andric     LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
5810b57cec5SDimitry Andric               static_cast<void *>(this),
5820b57cec5SDimitry Andric               static_cast<void *>(m_symtab_up.get()));
5834824e7fdSDimitry Andric     // Since we need to clear the symbol table, we need a new llvm::once_flag
5844824e7fdSDimitry Andric     // instance so we can safely create another symbol table
5854824e7fdSDimitry Andric     m_symtab_once_up.reset(new llvm::once_flag());
5860b57cec5SDimitry Andric     m_symtab_up.reset();
5870b57cec5SDimitry Andric   }
5880b57cec5SDimitry Andric }
5890b57cec5SDimitry Andric 
GetSectionList(bool update_module_section_list)5900b57cec5SDimitry Andric SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
5910b57cec5SDimitry Andric   if (m_sections_up == nullptr) {
5920b57cec5SDimitry Andric     if (update_module_section_list) {
5930b57cec5SDimitry Andric       ModuleSP module_sp(GetModule());
5940b57cec5SDimitry Andric       if (module_sp) {
5950b57cec5SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
5960b57cec5SDimitry Andric         CreateSections(*module_sp->GetUnifiedSectionList());
5970b57cec5SDimitry Andric       }
5980b57cec5SDimitry Andric     } else {
5990b57cec5SDimitry Andric       SectionList unified_section_list;
6000b57cec5SDimitry Andric       CreateSections(unified_section_list);
6010b57cec5SDimitry Andric     }
6020b57cec5SDimitry Andric   }
6030b57cec5SDimitry Andric   return m_sections_up.get();
6040b57cec5SDimitry Andric }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric lldb::SymbolType
GetSymbolTypeFromName(llvm::StringRef name,lldb::SymbolType symbol_type_hint)6070b57cec5SDimitry Andric ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,
6080b57cec5SDimitry Andric                                   lldb::SymbolType symbol_type_hint) {
6090b57cec5SDimitry Andric   if (!name.empty()) {
6105f757f3fSDimitry Andric     if (name.starts_with("_OBJC_")) {
6110b57cec5SDimitry Andric       // ObjC
6125f757f3fSDimitry Andric       if (name.starts_with("_OBJC_CLASS_$_"))
6130b57cec5SDimitry Andric         return lldb::eSymbolTypeObjCClass;
6145f757f3fSDimitry Andric       if (name.starts_with("_OBJC_METACLASS_$_"))
6150b57cec5SDimitry Andric         return lldb::eSymbolTypeObjCMetaClass;
6165f757f3fSDimitry Andric       if (name.starts_with("_OBJC_IVAR_$_"))
6170b57cec5SDimitry Andric         return lldb::eSymbolTypeObjCIVar;
6185f757f3fSDimitry Andric     } else if (name.starts_with(".objc_class_name_")) {
6190b57cec5SDimitry Andric       // ObjC v1
6200b57cec5SDimitry Andric       return lldb::eSymbolTypeObjCClass;
6210b57cec5SDimitry Andric     }
6220b57cec5SDimitry Andric   }
6230b57cec5SDimitry Andric   return symbol_type_hint;
6240b57cec5SDimitry Andric }
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)6270b57cec5SDimitry Andric ObjectFile::GetLoadableData(Target &target) {
6280b57cec5SDimitry Andric   std::vector<LoadableData> loadables;
6290b57cec5SDimitry Andric   SectionList *section_list = GetSectionList();
6300b57cec5SDimitry Andric   if (!section_list)
6310b57cec5SDimitry Andric     return loadables;
6320b57cec5SDimitry Andric   // Create a list of loadable data from loadable sections
6330b57cec5SDimitry Andric   size_t section_count = section_list->GetNumSections(0);
6340b57cec5SDimitry Andric   for (size_t i = 0; i < section_count; ++i) {
6350b57cec5SDimitry Andric     LoadableData loadable;
6360b57cec5SDimitry Andric     SectionSP section_sp = section_list->GetSectionAtIndex(i);
6370b57cec5SDimitry Andric     loadable.Dest =
6380b57cec5SDimitry Andric         target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
6390b57cec5SDimitry Andric     if (loadable.Dest == LLDB_INVALID_ADDRESS)
6400b57cec5SDimitry Andric       continue;
6410b57cec5SDimitry Andric     // We can skip sections like bss
6420b57cec5SDimitry Andric     if (section_sp->GetFileSize() == 0)
6430b57cec5SDimitry Andric       continue;
6440b57cec5SDimitry Andric     DataExtractor section_data;
6450b57cec5SDimitry Andric     section_sp->GetSectionData(section_data);
6460b57cec5SDimitry Andric     loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),
6470b57cec5SDimitry Andric                                                 section_data.GetByteSize());
6480b57cec5SDimitry Andric     loadables.push_back(loadable);
6490b57cec5SDimitry Andric   }
6500b57cec5SDimitry Andric   return loadables;
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
CreateCallFrameInfo()6539dba64beSDimitry Andric std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
6549dba64beSDimitry Andric   return {};
6559dba64beSDimitry Andric }
6569dba64beSDimitry Andric 
RelocateSection(lldb_private::Section * section)6570b57cec5SDimitry Andric void ObjectFile::RelocateSection(lldb_private::Section *section)
6580b57cec5SDimitry Andric {
6590b57cec5SDimitry Andric }
6600b57cec5SDimitry Andric 
MapFileData(const FileSpec & file,uint64_t Size,uint64_t Offset)6610b57cec5SDimitry Andric DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size,
6620b57cec5SDimitry Andric                                      uint64_t Offset) {
6630b57cec5SDimitry Andric   return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
6640b57cec5SDimitry Andric }
6650b57cec5SDimitry Andric 
format(const ObjectFile::Type & type,raw_ostream & OS,StringRef Style)6660b57cec5SDimitry Andric void llvm::format_provider<ObjectFile::Type>::format(
6670b57cec5SDimitry Andric     const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
6680b57cec5SDimitry Andric   switch (type) {
6690b57cec5SDimitry Andric   case ObjectFile::eTypeInvalid:
6700b57cec5SDimitry Andric     OS << "invalid";
6710b57cec5SDimitry Andric     break;
6720b57cec5SDimitry Andric   case ObjectFile::eTypeCoreFile:
6730b57cec5SDimitry Andric     OS << "core file";
6740b57cec5SDimitry Andric     break;
6750b57cec5SDimitry Andric   case ObjectFile::eTypeExecutable:
6760b57cec5SDimitry Andric     OS << "executable";
6770b57cec5SDimitry Andric     break;
6780b57cec5SDimitry Andric   case ObjectFile::eTypeDebugInfo:
6790b57cec5SDimitry Andric     OS << "debug info";
6800b57cec5SDimitry Andric     break;
6810b57cec5SDimitry Andric   case ObjectFile::eTypeDynamicLinker:
6820b57cec5SDimitry Andric     OS << "dynamic linker";
6830b57cec5SDimitry Andric     break;
6840b57cec5SDimitry Andric   case ObjectFile::eTypeObjectFile:
6850b57cec5SDimitry Andric     OS << "object file";
6860b57cec5SDimitry Andric     break;
6870b57cec5SDimitry Andric   case ObjectFile::eTypeSharedLibrary:
6880b57cec5SDimitry Andric     OS << "shared library";
6890b57cec5SDimitry Andric     break;
6900b57cec5SDimitry Andric   case ObjectFile::eTypeStubLibrary:
6910b57cec5SDimitry Andric     OS << "stub library";
6920b57cec5SDimitry Andric     break;
6930b57cec5SDimitry Andric   case ObjectFile::eTypeJIT:
6940b57cec5SDimitry Andric     OS << "jit";
6950b57cec5SDimitry Andric     break;
6960b57cec5SDimitry Andric   case ObjectFile::eTypeUnknown:
6970b57cec5SDimitry Andric     OS << "unknown";
6980b57cec5SDimitry Andric     break;
6990b57cec5SDimitry Andric   }
7000b57cec5SDimitry Andric }
7010b57cec5SDimitry Andric 
format(const ObjectFile::Strata & strata,raw_ostream & OS,StringRef Style)7020b57cec5SDimitry Andric void llvm::format_provider<ObjectFile::Strata>::format(
7030b57cec5SDimitry Andric     const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
7040b57cec5SDimitry Andric   switch (strata) {
7050b57cec5SDimitry Andric   case ObjectFile::eStrataInvalid:
7060b57cec5SDimitry Andric     OS << "invalid";
7070b57cec5SDimitry Andric     break;
7080b57cec5SDimitry Andric   case ObjectFile::eStrataUnknown:
7090b57cec5SDimitry Andric     OS << "unknown";
7100b57cec5SDimitry Andric     break;
7110b57cec5SDimitry Andric   case ObjectFile::eStrataUser:
7120b57cec5SDimitry Andric     OS << "user";
7130b57cec5SDimitry Andric     break;
7140b57cec5SDimitry Andric   case ObjectFile::eStrataKernel:
7150b57cec5SDimitry Andric     OS << "kernel";
7160b57cec5SDimitry Andric     break;
7170b57cec5SDimitry Andric   case ObjectFile::eStrataRawImage:
7180b57cec5SDimitry Andric     OS << "raw image";
7190b57cec5SDimitry Andric     break;
7200b57cec5SDimitry Andric   case ObjectFile::eStrataJIT:
7210b57cec5SDimitry Andric     OS << "jit";
7220b57cec5SDimitry Andric     break;
7230b57cec5SDimitry Andric   }
7240b57cec5SDimitry Andric }
7254824e7fdSDimitry Andric 
7264824e7fdSDimitry Andric 
GetSymtab()7274824e7fdSDimitry Andric Symtab *ObjectFile::GetSymtab() {
7284824e7fdSDimitry Andric   ModuleSP module_sp(GetModule());
7294824e7fdSDimitry Andric   if (module_sp) {
7304824e7fdSDimitry Andric     // We can't take the module lock in ObjectFile::GetSymtab() or we can
7314824e7fdSDimitry Andric     // deadlock in DWARF indexing when any file asks for the symbol table from
7324824e7fdSDimitry Andric     // an object file. This currently happens in the preloading of symbols in
7334824e7fdSDimitry Andric     // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
7344824e7fdSDimitry Andric     // module lock, and then threads will be spun up to index the DWARF and
7354824e7fdSDimitry Andric     // any of those threads might end up trying to relocate items in the DWARF
7364824e7fdSDimitry Andric     // sections which causes ObjectFile::GetSectionData(...) to relocate section
7374824e7fdSDimitry Andric     // data which requires the symbol table.
7384824e7fdSDimitry Andric     //
7394824e7fdSDimitry Andric     // So to work around this, we create the symbol table one time using
7404824e7fdSDimitry Andric     // llvm::once_flag, lock it, and then set the unique pointer. Any other
7414824e7fdSDimitry Andric     // thread that gets ahold of the symbol table before parsing is done, will
7424824e7fdSDimitry Andric     // not be able to access the symbol table contents since all APIs in Symtab
7434824e7fdSDimitry Andric     // are protected by a mutex in the Symtab object itself.
7444824e7fdSDimitry Andric     llvm::call_once(*m_symtab_once_up, [&]() {
7454824e7fdSDimitry Andric       Symtab *symtab = new Symtab(this);
7464824e7fdSDimitry Andric       std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
7474824e7fdSDimitry Andric       m_symtab_up.reset(symtab);
7480eae32dcSDimitry Andric       if (!m_symtab_up->LoadFromCache()) {
7490eae32dcSDimitry Andric         ElapsedTime elapsed(module_sp->GetSymtabParseTime());
7504824e7fdSDimitry Andric         ParseSymtab(*m_symtab_up);
7514824e7fdSDimitry Andric         m_symtab_up->Finalize();
7520eae32dcSDimitry Andric       }
7534824e7fdSDimitry Andric     });
7544824e7fdSDimitry Andric   }
7554824e7fdSDimitry Andric   return m_symtab_up.get();
7564824e7fdSDimitry Andric }
7570eae32dcSDimitry Andric 
GetCacheHash()7580eae32dcSDimitry Andric uint32_t ObjectFile::GetCacheHash() {
7590eae32dcSDimitry Andric   if (m_cache_hash)
7600eae32dcSDimitry Andric     return *m_cache_hash;
7610eae32dcSDimitry Andric   StreamString strm;
7620eae32dcSDimitry Andric   strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
7630eae32dcSDimitry Andric   m_cache_hash = llvm::djbHash(strm.GetString());
7640eae32dcSDimitry Andric   return *m_cache_hash;
7650eae32dcSDimitry Andric }
76606c3fb27SDimitry Andric 
76706c3fb27SDimitry Andric namespace llvm {
76806c3fb27SDimitry Andric namespace json {
76906c3fb27SDimitry Andric 
fromJSON(const llvm::json::Value & value,lldb_private::ObjectFile::Type & type,llvm::json::Path path)77006c3fb27SDimitry Andric bool fromJSON(const llvm::json::Value &value,
77106c3fb27SDimitry Andric               lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
77206c3fb27SDimitry Andric   if (auto str = value.getAsString()) {
77306c3fb27SDimitry Andric     type = llvm::StringSwitch<ObjectFile::Type>(*str)
77406c3fb27SDimitry Andric                .Case("corefile", ObjectFile::eTypeCoreFile)
77506c3fb27SDimitry Andric                .Case("executable", ObjectFile::eTypeExecutable)
77606c3fb27SDimitry Andric                .Case("debuginfo", ObjectFile::eTypeDebugInfo)
77706c3fb27SDimitry Andric                .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
77806c3fb27SDimitry Andric                .Case("objectfile", ObjectFile::eTypeObjectFile)
77906c3fb27SDimitry Andric                .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
78006c3fb27SDimitry Andric                .Case("stublibrary", ObjectFile::eTypeStubLibrary)
78106c3fb27SDimitry Andric                .Case("jit", ObjectFile::eTypeJIT)
78206c3fb27SDimitry Andric                .Case("unknown", ObjectFile::eTypeUnknown)
78306c3fb27SDimitry Andric                .Default(ObjectFile::eTypeInvalid);
78406c3fb27SDimitry Andric 
78506c3fb27SDimitry Andric     if (type == ObjectFile::eTypeInvalid) {
78606c3fb27SDimitry Andric       path.report("invalid object type");
78706c3fb27SDimitry Andric       return false;
78806c3fb27SDimitry Andric     }
78906c3fb27SDimitry Andric 
79006c3fb27SDimitry Andric     return true;
79106c3fb27SDimitry Andric   }
79206c3fb27SDimitry Andric   path.report("expected string");
79306c3fb27SDimitry Andric   return false;
79406c3fb27SDimitry Andric }
79506c3fb27SDimitry Andric } // namespace json
79606c3fb27SDimitry Andric } // namespace llvm
797