xref: /src/contrib/llvm-project/lldb/source/Symbol/ObjectFile.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1cfca06d7SDimitry Andric //===-- ObjectFile.cpp ----------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
914f1b3e8SDimitry Andric #include "lldb/Symbol/ObjectFile.h"
10f034231aSEd Maste #include "lldb/Core/Module.h"
11f034231aSEd Maste #include "lldb/Core/ModuleSpec.h"
12f034231aSEd Maste #include "lldb/Core/PluginManager.h"
13f034231aSEd Maste #include "lldb/Core/Section.h"
14ead24645SDimitry Andric #include "lldb/Symbol/CallFrameInfo.h"
15f034231aSEd Maste #include "lldb/Symbol/ObjectContainer.h"
16f034231aSEd Maste #include "lldb/Symbol/SymbolFile.h"
17f034231aSEd Maste #include "lldb/Target/Process.h"
1874a628f7SDimitry Andric #include "lldb/Target/SectionLoadList.h"
1974a628f7SDimitry Andric #include "lldb/Target/Target.h"
2074a628f7SDimitry Andric #include "lldb/Utility/DataBuffer.h"
2174a628f7SDimitry Andric #include "lldb/Utility/DataBufferHeap.h"
22145449b1SDimitry Andric #include "lldb/Utility/LLDBLog.h"
2374a628f7SDimitry Andric #include "lldb/Utility/Log.h"
241b306c26SDimitry Andric #include "lldb/Utility/Timer.h"
2514f1b3e8SDimitry Andric #include "lldb/lldb-private.h"
26f034231aSEd Maste 
2777fc4c14SDimitry Andric #include "llvm/Support/DJB.h"
2877fc4c14SDimitry Andric 
29f034231aSEd Maste using namespace lldb;
30f034231aSEd Maste using namespace lldb_private;
31f034231aSEd Maste 
32ead24645SDimitry Andric char ObjectFile::ID;
33145449b1SDimitry Andric size_t ObjectFile::g_initial_bytes_to_read = 512;
34ead24645SDimitry Andric 
35b60736ecSDimitry 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)36b60736ecSDimitry Andric CreateObjectFromContainer(const lldb::ModuleSP &module_sp, const FileSpec *file,
37b60736ecSDimitry Andric                           lldb::offset_t file_offset, lldb::offset_t file_size,
38145449b1SDimitry Andric                           DataBufferSP data_sp, lldb::offset_t &data_offset) {
39b60736ecSDimitry Andric   ObjectContainerCreateInstance callback;
40b60736ecSDimitry Andric   for (uint32_t idx = 0;
41b60736ecSDimitry Andric        (callback = PluginManager::GetObjectContainerCreateCallbackAtIndex(
42b60736ecSDimitry Andric             idx)) != nullptr;
43b60736ecSDimitry Andric        ++idx) {
44b60736ecSDimitry Andric     std::unique_ptr<ObjectContainer> object_container_up(callback(
45b60736ecSDimitry Andric         module_sp, data_sp, data_offset, file, file_offset, file_size));
46b60736ecSDimitry Andric     if (object_container_up)
47b60736ecSDimitry Andric       return object_container_up->GetObjectFile(file);
48b60736ecSDimitry Andric   }
49b60736ecSDimitry Andric   return {};
50b60736ecSDimitry Andric }
51b60736ecSDimitry Andric 
52f034231aSEd Maste 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)5314f1b3e8SDimitry Andric ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
5414f1b3e8SDimitry Andric                        lldb::offset_t file_offset, lldb::offset_t file_size,
5514f1b3e8SDimitry Andric                        DataBufferSP &data_sp, lldb::offset_t &data_offset) {
56b60736ecSDimitry Andric   LLDB_SCOPED_TIMERF(
5714f1b3e8SDimitry Andric       "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
5814f1b3e8SDimitry Andric       "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
59f034231aSEd Maste       module_sp->GetFileSpec().GetPath().c_str(),
6014f1b3e8SDimitry Andric       static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
610cac4ca3SEd Maste       static_cast<uint64_t>(file_size));
62b60736ecSDimitry Andric 
63b60736ecSDimitry Andric   if (!module_sp)
64b60736ecSDimitry Andric     return {};
65b60736ecSDimitry Andric 
66b60736ecSDimitry Andric   if (!file)
67b60736ecSDimitry Andric     return {};
68f034231aSEd Maste 
6914f1b3e8SDimitry Andric   if (!data_sp) {
70cfca06d7SDimitry Andric     const bool file_exists = FileSystem::Instance().Exists(*file);
71f73363f1SDimitry Andric     // We have an object name which most likely means we have a .o file in
72f73363f1SDimitry Andric     // a static archive (.a file). Try and see if we have a cached archive
73f73363f1SDimitry Andric     // first without reading any data first
7414f1b3e8SDimitry Andric     if (file_exists && module_sp->GetObjectName()) {
75b60736ecSDimitry Andric       ObjectFileSP object_file_sp = CreateObjectFromContainer(
76b60736ecSDimitry Andric           module_sp, file, file_offset, file_size, data_sp, data_offset);
77b60736ecSDimitry Andric       if (object_file_sp)
78f034231aSEd Maste         return object_file_sp;
79f034231aSEd Maste     }
80f73363f1SDimitry Andric     // Ok, we didn't find any containers that have a named object, now lets
81f73363f1SDimitry Andric     // read the first 512 bytes from the file so the object file and object
82f73363f1SDimitry Andric     // container plug-ins can use these bytes to see if they can parse this
83f73363f1SDimitry Andric     // file.
8414f1b3e8SDimitry Andric     if (file_size > 0) {
85145449b1SDimitry Andric       data_sp = FileSystem::Instance().CreateDataBuffer(
86145449b1SDimitry Andric           file->GetPath(), g_initial_bytes_to_read, file_offset);
87f034231aSEd Maste       data_offset = 0;
88f034231aSEd Maste     }
89f034231aSEd Maste   }
90f034231aSEd Maste 
9114f1b3e8SDimitry Andric   if (!data_sp || data_sp->GetByteSize() == 0) {
92f034231aSEd Maste     // Check for archive file with format "/path/to/archive.a(object.o)"
93ead24645SDimitry Andric     llvm::SmallString<256> path_with_object;
94ead24645SDimitry Andric     module_sp->GetFileSpec().GetPath(path_with_object);
95f034231aSEd Maste 
96b60736ecSDimitry Andric     FileSpec archive_file;
97f034231aSEd Maste     ConstString archive_object;
98f034231aSEd Maste     const bool must_exist = true;
99b60736ecSDimitry Andric     if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,
100b60736ecSDimitry Andric                                                archive_object, must_exist)) {
10194994d37SDimitry Andric       file_size = FileSystem::Instance().GetByteSize(archive_file);
10214f1b3e8SDimitry Andric       if (file_size > 0) {
103f034231aSEd Maste         file = &archive_file;
104f034231aSEd Maste         module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
10514f1b3e8SDimitry Andric         // Check if this is a object container by iterating through all
106f73363f1SDimitry Andric         // object container plugin instances and then trying to get an
107f73363f1SDimitry Andric         // object file from the container plugins since we had a name.
108f73363f1SDimitry Andric         // Also, don't read
109f034231aSEd Maste         // ANY data in case there is data cached in the container plug-ins
110f73363f1SDimitry Andric         // (like BSD archives caching the contained objects within an
111f73363f1SDimitry Andric         // file).
112b60736ecSDimitry Andric         ObjectFileSP object_file_sp = CreateObjectFromContainer(
113b60736ecSDimitry Andric             module_sp, file, file_offset, file_size, data_sp, data_offset);
114b60736ecSDimitry Andric         if (object_file_sp)
115f034231aSEd Maste           return object_file_sp;
116f73363f1SDimitry Andric         // We failed to find any cached object files in the container plug-
117f73363f1SDimitry Andric         // ins, so lets read the first 512 bytes and try again below...
11894994d37SDimitry Andric         data_sp = FileSystem::Instance().CreateDataBuffer(
119145449b1SDimitry Andric             archive_file.GetPath(), g_initial_bytes_to_read, file_offset);
120f034231aSEd Maste       }
121f034231aSEd Maste     }
122f034231aSEd Maste   }
123f034231aSEd Maste 
12414f1b3e8SDimitry Andric   if (data_sp && data_sp->GetByteSize() > 0) {
125f73363f1SDimitry Andric     // Check if this is a normal object file by iterating through all
126f73363f1SDimitry Andric     // object file plugin instances.
127b60736ecSDimitry Andric     ObjectFileCreateInstance callback;
12814f1b3e8SDimitry Andric     for (uint32_t idx = 0;
129b60736ecSDimitry Andric          (callback = PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) !=
13014f1b3e8SDimitry Andric          nullptr;
13114f1b3e8SDimitry Andric          ++idx) {
132b60736ecSDimitry Andric       ObjectFileSP object_file_sp(callback(module_sp, data_sp, data_offset,
133b60736ecSDimitry Andric                                            file, file_offset, file_size));
134f034231aSEd Maste       if (object_file_sp.get())
135f034231aSEd Maste         return object_file_sp;
136f034231aSEd Maste     }
137f034231aSEd Maste 
138f73363f1SDimitry Andric     // Check if this is a object container by iterating through all object
139f73363f1SDimitry Andric     // container plugin instances and then trying to get an object file
140f73363f1SDimitry Andric     // from the container.
141b60736ecSDimitry Andric     ObjectFileSP object_file_sp = CreateObjectFromContainer(
142b60736ecSDimitry Andric         module_sp, file, file_offset, file_size, data_sp, data_offset);
143b60736ecSDimitry Andric     if (object_file_sp)
144f034231aSEd Maste       return object_file_sp;
145f034231aSEd Maste   }
146b60736ecSDimitry Andric 
147f73363f1SDimitry Andric   // We didn't find it, so clear our shared pointer in case it contains
148f73363f1SDimitry Andric   // anything and return an empty shared pointer
149b60736ecSDimitry Andric   return {};
150f034231aSEd Maste }
151f034231aSEd Maste 
FindPlugin(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,WritableDataBufferSP data_sp)15214f1b3e8SDimitry Andric ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,
153f034231aSEd Maste                                     const ProcessSP &process_sp,
154f034231aSEd Maste                                     lldb::addr_t header_addr,
155145449b1SDimitry Andric                                     WritableDataBufferSP data_sp) {
156f034231aSEd Maste   ObjectFileSP object_file_sp;
157f034231aSEd Maste 
15814f1b3e8SDimitry Andric   if (module_sp) {
159b60736ecSDimitry Andric     LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "
16014f1b3e8SDimitry Andric                        "%s, process = %p, header_addr = "
16114f1b3e8SDimitry Andric                        "0x%" PRIx64 ")",
162f034231aSEd Maste                        module_sp->GetFileSpec().GetPath().c_str(),
1630cac4ca3SEd Maste                        static_cast<void *>(process_sp.get()), header_addr);
164f034231aSEd Maste     uint32_t idx;
165f034231aSEd Maste 
166f73363f1SDimitry Andric     // Check if this is a normal object file by iterating through all object
167f73363f1SDimitry Andric     // file plugin instances.
168f034231aSEd Maste     ObjectFileCreateMemoryInstance create_callback;
16914f1b3e8SDimitry Andric     for (idx = 0;
17014f1b3e8SDimitry Andric          (create_callback =
17114f1b3e8SDimitry Andric               PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) !=
17214f1b3e8SDimitry Andric          nullptr;
17314f1b3e8SDimitry Andric          ++idx) {
17414f1b3e8SDimitry Andric       object_file_sp.reset(
17514f1b3e8SDimitry Andric           create_callback(module_sp, data_sp, process_sp, header_addr));
176f034231aSEd Maste       if (object_file_sp.get())
177f034231aSEd Maste         return object_file_sp;
178f034231aSEd Maste     }
179f034231aSEd Maste   }
1800cac4ca3SEd Maste 
181f73363f1SDimitry Andric   // We didn't find it, so clear our shared pointer in case it contains
182f73363f1SDimitry Andric   // anything and return an empty shared pointer
183f034231aSEd Maste   object_file_sp.reset();
184f034231aSEd Maste   return object_file_sp;
185f034231aSEd Maste }
186f034231aSEd Maste 
IsObjectFile(lldb_private::FileSpec file_spec)187ac9a064cSDimitry Andric bool ObjectFile::IsObjectFile(lldb_private::FileSpec file_spec) {
188ac9a064cSDimitry Andric   DataBufferSP data_sp;
189ac9a064cSDimitry Andric   offset_t data_offset = 0;
190ac9a064cSDimitry Andric   ModuleSP module_sp = std::make_shared<Module>(file_spec);
191ac9a064cSDimitry Andric   return static_cast<bool>(ObjectFile::FindPlugin(
192ac9a064cSDimitry Andric       module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),
193ac9a064cSDimitry Andric       data_sp, data_offset));
194ac9a064cSDimitry Andric }
195ac9a064cSDimitry Andric 
GetModuleSpecifications(const FileSpec & file,lldb::offset_t file_offset,lldb::offset_t file_size,ModuleSpecList & specs,DataBufferSP data_sp)19614f1b3e8SDimitry Andric size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
197f034231aSEd Maste                                            lldb::offset_t file_offset,
198f034231aSEd Maste                                            lldb::offset_t file_size,
199cfca06d7SDimitry Andric                                            ModuleSpecList &specs,
200cfca06d7SDimitry Andric                                            DataBufferSP data_sp) {
201cfca06d7SDimitry Andric   if (!data_sp)
202145449b1SDimitry Andric     data_sp = FileSystem::Instance().CreateDataBuffer(
203145449b1SDimitry Andric         file.GetPath(), g_initial_bytes_to_read, file_offset);
20414f1b3e8SDimitry Andric   if (data_sp) {
20514f1b3e8SDimitry Andric     if (file_size == 0) {
20694994d37SDimitry Andric       const lldb::offset_t actual_file_size =
20794994d37SDimitry Andric           FileSystem::Instance().GetByteSize(file);
208f034231aSEd Maste       if (actual_file_size > file_offset)
209f034231aSEd Maste         file_size = actual_file_size - file_offset;
210f034231aSEd Maste     }
211f034231aSEd Maste     return ObjectFile::GetModuleSpecifications(file,        // file spec
212f034231aSEd Maste                                                data_sp,     // data bytes
213f034231aSEd Maste                                                0,           // data offset
214f034231aSEd Maste                                                file_offset, // file offset
215f034231aSEd Maste                                                file_size,   // file length
216f034231aSEd Maste                                                specs);
217f034231aSEd Maste   }
218f034231aSEd Maste   return 0;
219f034231aSEd Maste }
220f034231aSEd Maste 
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)22114f1b3e8SDimitry Andric size_t ObjectFile::GetModuleSpecifications(
22214f1b3e8SDimitry Andric     const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
22314f1b3e8SDimitry Andric     lldb::offset_t data_offset, lldb::offset_t file_offset,
22414f1b3e8SDimitry Andric     lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
225f034231aSEd Maste   const size_t initial_count = specs.GetSize();
226f034231aSEd Maste   ObjectFileGetModuleSpecifications callback;
227f034231aSEd Maste   uint32_t i;
228f034231aSEd Maste   // Try the ObjectFile plug-ins
22914f1b3e8SDimitry Andric   for (i = 0;
23014f1b3e8SDimitry Andric        (callback =
23114f1b3e8SDimitry Andric             PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
23214f1b3e8SDimitry Andric                 i)) != nullptr;
23314f1b3e8SDimitry Andric        ++i) {
234f034231aSEd Maste     if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
235f034231aSEd Maste       return specs.GetSize() - initial_count;
236f034231aSEd Maste   }
237f034231aSEd Maste 
238f034231aSEd Maste   // Try the ObjectContainer plug-ins
23914f1b3e8SDimitry Andric   for (i = 0;
24014f1b3e8SDimitry Andric        (callback = PluginManager::
24114f1b3e8SDimitry Andric             GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) !=
24214f1b3e8SDimitry Andric        nullptr;
24314f1b3e8SDimitry Andric        ++i) {
244f034231aSEd Maste     if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
245f034231aSEd Maste       return specs.GetSize() - initial_count;
246f034231aSEd Maste   }
247f034231aSEd Maste   return 0;
248f034231aSEd Maste }
249f034231aSEd Maste 
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)250f034231aSEd Maste ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
251f034231aSEd Maste                        const FileSpec *file_spec_ptr,
25214f1b3e8SDimitry Andric                        lldb::offset_t file_offset, lldb::offset_t length,
253145449b1SDimitry Andric                        lldb::DataBufferSP data_sp, lldb::offset_t data_offset)
25414f1b3e8SDimitry Andric     : ModuleChild(module_sp),
255f034231aSEd Maste       m_file(), // This file could be different from the original module's file
25614f1b3e8SDimitry Andric       m_type(eTypeInvalid), m_strata(eStrataInvalid),
2575f29bb8aSDimitry Andric       m_file_offset(file_offset), m_length(length), m_data(), m_process_wp(),
2585f29bb8aSDimitry Andric       m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_up(), m_symtab_up(),
259f65dcba8SDimitry Andric       m_symtab_once_up(new llvm::once_flag()) {
260f034231aSEd Maste   if (file_spec_ptr)
261f034231aSEd Maste     m_file = *file_spec_ptr;
262f034231aSEd Maste   if (data_sp)
263f034231aSEd Maste     m_data.SetData(data_sp, data_offset, length);
264145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
265ead24645SDimitry Andric   LLDB_LOGF(log,
266ead24645SDimitry Andric             "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
26714f1b3e8SDimitry Andric             "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
26814f1b3e8SDimitry Andric             static_cast<void *>(this), static_cast<void *>(module_sp.get()),
269f034231aSEd Maste             module_sp->GetSpecificationDescription().c_str(),
27014f1b3e8SDimitry Andric             m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
27114f1b3e8SDimitry Andric             m_length);
272f034231aSEd Maste }
273f034231aSEd Maste 
ObjectFile(const lldb::ModuleSP & module_sp,const ProcessSP & process_sp,lldb::addr_t header_addr,DataBufferSP header_data_sp)274f034231aSEd Maste ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
27514f1b3e8SDimitry Andric                        const ProcessSP &process_sp, lldb::addr_t header_addr,
276145449b1SDimitry Andric                        DataBufferSP header_data_sp)
27714f1b3e8SDimitry Andric     : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
27814f1b3e8SDimitry Andric       m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),
2795f29bb8aSDimitry Andric       m_process_wp(process_sp), m_memory_addr(header_addr), m_sections_up(),
280f65dcba8SDimitry Andric       m_symtab_up(), m_symtab_once_up(new llvm::once_flag()) {
281f034231aSEd Maste   if (header_data_sp)
282f034231aSEd Maste     m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());
283145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
284ead24645SDimitry Andric   LLDB_LOGF(log,
285ead24645SDimitry Andric             "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
28614f1b3e8SDimitry Andric             "header_addr = 0x%" PRIx64,
28714f1b3e8SDimitry Andric             static_cast<void *>(this), static_cast<void *>(module_sp.get()),
288f034231aSEd Maste             module_sp->GetSpecificationDescription().c_str(),
2890cac4ca3SEd Maste             static_cast<void *>(process_sp.get()), m_memory_addr);
290f034231aSEd Maste }
291f034231aSEd Maste 
~ObjectFile()29214f1b3e8SDimitry Andric ObjectFile::~ObjectFile() {
293145449b1SDimitry Andric   Log *log = GetLog(LLDBLog::Object);
294ead24645SDimitry Andric   LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
295f034231aSEd Maste }
296f034231aSEd Maste 
SetModulesArchitecture(const ArchSpec & new_arch)29714f1b3e8SDimitry Andric bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {
298f034231aSEd Maste   ModuleSP module_sp(GetModule());
299f034231aSEd Maste   if (module_sp)
300f034231aSEd Maste     return module_sp->SetArchitecture(new_arch);
301f034231aSEd Maste   return false;
302f034231aSEd Maste }
303f034231aSEd Maste 
GetAddressClass(addr_t file_addr)30414f1b3e8SDimitry Andric AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {
305f034231aSEd Maste   Symtab *symtab = GetSymtab();
30614f1b3e8SDimitry Andric   if (symtab) {
307f034231aSEd Maste     Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
30814f1b3e8SDimitry Andric     if (symbol) {
30914f1b3e8SDimitry Andric       if (symbol->ValueIsAddress()) {
3105e95aa85SEd Maste         const SectionSP section_sp(symbol->GetAddressRef().GetSection());
31114f1b3e8SDimitry Andric         if (section_sp) {
312f034231aSEd Maste           const SectionType section_type = section_sp->GetType();
31314f1b3e8SDimitry Andric           switch (section_type) {
3140cac4ca3SEd Maste           case eSectionTypeInvalid:
315f73363f1SDimitry Andric             return AddressClass::eUnknown;
3160cac4ca3SEd Maste           case eSectionTypeCode:
317f73363f1SDimitry Andric             return AddressClass::eCode;
3180cac4ca3SEd Maste           case eSectionTypeContainer:
319f73363f1SDimitry Andric             return AddressClass::eUnknown;
320f034231aSEd Maste           case eSectionTypeData:
321f034231aSEd Maste           case eSectionTypeDataCString:
322f034231aSEd Maste           case eSectionTypeDataCStringPointers:
323f034231aSEd Maste           case eSectionTypeDataSymbolAddress:
324f034231aSEd Maste           case eSectionTypeData4:
325f034231aSEd Maste           case eSectionTypeData8:
326f034231aSEd Maste           case eSectionTypeData16:
327f034231aSEd Maste           case eSectionTypeDataPointers:
328f034231aSEd Maste           case eSectionTypeZeroFill:
329f034231aSEd Maste           case eSectionTypeDataObjCMessageRefs:
330f034231aSEd Maste           case eSectionTypeDataObjCCFStrings:
331e81d9d49SDimitry Andric           case eSectionTypeGoSymtab:
332f73363f1SDimitry Andric             return AddressClass::eData;
333f034231aSEd Maste           case eSectionTypeDebug:
334f034231aSEd Maste           case eSectionTypeDWARFDebugAbbrev:
33594994d37SDimitry Andric           case eSectionTypeDWARFDebugAbbrevDwo:
336e81d9d49SDimitry Andric           case eSectionTypeDWARFDebugAddr:
337f034231aSEd Maste           case eSectionTypeDWARFDebugAranges:
338ef5d0b5eSDimitry Andric           case eSectionTypeDWARFDebugCuIndex:
339f034231aSEd Maste           case eSectionTypeDWARFDebugFrame:
340f034231aSEd Maste           case eSectionTypeDWARFDebugInfo:
34194994d37SDimitry Andric           case eSectionTypeDWARFDebugInfoDwo:
342f034231aSEd Maste           case eSectionTypeDWARFDebugLine:
34394994d37SDimitry Andric           case eSectionTypeDWARFDebugLineStr:
344f034231aSEd Maste           case eSectionTypeDWARFDebugLoc:
345706b4fc4SDimitry Andric           case eSectionTypeDWARFDebugLocDwo:
34694994d37SDimitry Andric           case eSectionTypeDWARFDebugLocLists:
347706b4fc4SDimitry Andric           case eSectionTypeDWARFDebugLocListsDwo:
348f034231aSEd Maste           case eSectionTypeDWARFDebugMacInfo:
349e81d9d49SDimitry Andric           case eSectionTypeDWARFDebugMacro:
350f73363f1SDimitry Andric           case eSectionTypeDWARFDebugNames:
351f034231aSEd Maste           case eSectionTypeDWARFDebugPubNames:
352f034231aSEd Maste           case eSectionTypeDWARFDebugPubTypes:
353f034231aSEd Maste           case eSectionTypeDWARFDebugRanges:
35494994d37SDimitry Andric           case eSectionTypeDWARFDebugRngLists:
355706b4fc4SDimitry Andric           case eSectionTypeDWARFDebugRngListsDwo:
356f034231aSEd Maste           case eSectionTypeDWARFDebugStr:
35794994d37SDimitry Andric           case eSectionTypeDWARFDebugStrDwo:
358e81d9d49SDimitry Andric           case eSectionTypeDWARFDebugStrOffsets:
35994994d37SDimitry Andric           case eSectionTypeDWARFDebugStrOffsetsDwo:
360cfca06d7SDimitry Andric           case eSectionTypeDWARFDebugTuIndex:
361f73363f1SDimitry Andric           case eSectionTypeDWARFDebugTypes:
3625f29bb8aSDimitry Andric           case eSectionTypeDWARFDebugTypesDwo:
363f034231aSEd Maste           case eSectionTypeDWARFAppleNames:
364f034231aSEd Maste           case eSectionTypeDWARFAppleTypes:
365f034231aSEd Maste           case eSectionTypeDWARFAppleNamespaces:
366f034231aSEd Maste           case eSectionTypeDWARFAppleObjC:
367f73363f1SDimitry Andric           case eSectionTypeDWARFGNUDebugAltLink:
3687fa27ce4SDimitry Andric           case eSectionTypeCTF:
369b1c73532SDimitry Andric           case eSectionTypeSwiftModules:
370f73363f1SDimitry Andric             return AddressClass::eDebug;
3710cac4ca3SEd Maste           case eSectionTypeEHFrame:
372e81d9d49SDimitry Andric           case eSectionTypeARMexidx:
373e81d9d49SDimitry Andric           case eSectionTypeARMextab:
374205afe67SEd Maste           case eSectionTypeCompactUnwind:
375f73363f1SDimitry Andric             return AddressClass::eRuntime;
376f034231aSEd Maste           case eSectionTypeELFSymbolTable:
377f034231aSEd Maste           case eSectionTypeELFDynamicSymbols:
378f034231aSEd Maste           case eSectionTypeELFRelocationEntries:
379f034231aSEd Maste           case eSectionTypeELFDynamicLinkInfo:
3800cac4ca3SEd Maste           case eSectionTypeOther:
381f73363f1SDimitry Andric             return AddressClass::eUnknown;
382f3fbd1c0SDimitry Andric           case eSectionTypeAbsoluteAddress:
38314f1b3e8SDimitry Andric             // In case of absolute sections decide the address class based on
384f73363f1SDimitry Andric             // the symbol type because the section type isn't specify if it is
385f73363f1SDimitry Andric             // a code or a data section.
386f3fbd1c0SDimitry Andric             break;
387f034231aSEd Maste           }
388f034231aSEd Maste         }
389f034231aSEd Maste       }
390f034231aSEd Maste 
391f034231aSEd Maste       const SymbolType symbol_type = symbol->GetType();
39214f1b3e8SDimitry Andric       switch (symbol_type) {
39314f1b3e8SDimitry Andric       case eSymbolTypeAny:
394f73363f1SDimitry Andric         return AddressClass::eUnknown;
39514f1b3e8SDimitry Andric       case eSymbolTypeAbsolute:
396f73363f1SDimitry Andric         return AddressClass::eUnknown;
39714f1b3e8SDimitry Andric       case eSymbolTypeCode:
398f73363f1SDimitry Andric         return AddressClass::eCode;
39914f1b3e8SDimitry Andric       case eSymbolTypeTrampoline:
400f73363f1SDimitry Andric         return AddressClass::eCode;
40114f1b3e8SDimitry Andric       case eSymbolTypeResolver:
402f73363f1SDimitry Andric         return AddressClass::eCode;
40314f1b3e8SDimitry Andric       case eSymbolTypeData:
404f73363f1SDimitry Andric         return AddressClass::eData;
40514f1b3e8SDimitry Andric       case eSymbolTypeRuntime:
406f73363f1SDimitry Andric         return AddressClass::eRuntime;
40714f1b3e8SDimitry Andric       case eSymbolTypeException:
408f73363f1SDimitry Andric         return AddressClass::eRuntime;
40914f1b3e8SDimitry Andric       case eSymbolTypeSourceFile:
410f73363f1SDimitry Andric         return AddressClass::eDebug;
41114f1b3e8SDimitry Andric       case eSymbolTypeHeaderFile:
412f73363f1SDimitry Andric         return AddressClass::eDebug;
41314f1b3e8SDimitry Andric       case eSymbolTypeObjectFile:
414f73363f1SDimitry Andric         return AddressClass::eDebug;
41514f1b3e8SDimitry Andric       case eSymbolTypeCommonBlock:
416f73363f1SDimitry Andric         return AddressClass::eDebug;
41714f1b3e8SDimitry Andric       case eSymbolTypeBlock:
418f73363f1SDimitry Andric         return AddressClass::eDebug;
41914f1b3e8SDimitry Andric       case eSymbolTypeLocal:
420f73363f1SDimitry Andric         return AddressClass::eData;
42114f1b3e8SDimitry Andric       case eSymbolTypeParam:
422f73363f1SDimitry Andric         return AddressClass::eData;
42314f1b3e8SDimitry Andric       case eSymbolTypeVariable:
424f73363f1SDimitry Andric         return AddressClass::eData;
42514f1b3e8SDimitry Andric       case eSymbolTypeVariableType:
426f73363f1SDimitry Andric         return AddressClass::eDebug;
42714f1b3e8SDimitry Andric       case eSymbolTypeLineEntry:
428f73363f1SDimitry Andric         return AddressClass::eDebug;
42914f1b3e8SDimitry Andric       case eSymbolTypeLineHeader:
430f73363f1SDimitry Andric         return AddressClass::eDebug;
43114f1b3e8SDimitry Andric       case eSymbolTypeScopeBegin:
432f73363f1SDimitry Andric         return AddressClass::eDebug;
43314f1b3e8SDimitry Andric       case eSymbolTypeScopeEnd:
434f73363f1SDimitry Andric         return AddressClass::eDebug;
43514f1b3e8SDimitry Andric       case eSymbolTypeAdditional:
436f73363f1SDimitry Andric         return AddressClass::eUnknown;
43714f1b3e8SDimitry Andric       case eSymbolTypeCompiler:
438f73363f1SDimitry Andric         return AddressClass::eDebug;
43914f1b3e8SDimitry Andric       case eSymbolTypeInstrumentation:
440f73363f1SDimitry Andric         return AddressClass::eDebug;
44114f1b3e8SDimitry Andric       case eSymbolTypeUndefined:
442f73363f1SDimitry Andric         return AddressClass::eUnknown;
44314f1b3e8SDimitry Andric       case eSymbolTypeObjCClass:
444f73363f1SDimitry Andric         return AddressClass::eRuntime;
44514f1b3e8SDimitry Andric       case eSymbolTypeObjCMetaClass:
446f73363f1SDimitry Andric         return AddressClass::eRuntime;
44714f1b3e8SDimitry Andric       case eSymbolTypeObjCIVar:
448f73363f1SDimitry Andric         return AddressClass::eRuntime;
44914f1b3e8SDimitry Andric       case eSymbolTypeReExported:
450f73363f1SDimitry Andric         return AddressClass::eRuntime;
451f034231aSEd Maste       }
452f034231aSEd Maste     }
453f034231aSEd Maste   }
454f73363f1SDimitry Andric   return AddressClass::eUnknown;
455f034231aSEd Maste }
456f034231aSEd Maste 
ReadMemory(const ProcessSP & process_sp,lldb::addr_t addr,size_t byte_size)45714f1b3e8SDimitry Andric DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,
45814f1b3e8SDimitry Andric                                     lldb::addr_t addr, size_t byte_size) {
459f034231aSEd Maste   DataBufferSP data_sp;
46014f1b3e8SDimitry Andric   if (process_sp) {
4615f29bb8aSDimitry Andric     std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
462b76161e4SDimitry Andric     Status error;
46314f1b3e8SDimitry Andric     const size_t bytes_read = process_sp->ReadMemory(
4645f29bb8aSDimitry Andric         addr, data_up->GetBytes(), data_up->GetByteSize(), error);
465f034231aSEd Maste     if (bytes_read == byte_size)
4665f29bb8aSDimitry Andric       data_sp.reset(data_up.release());
467f034231aSEd Maste   }
468f034231aSEd Maste   return data_sp;
469f034231aSEd Maste }
470f034231aSEd Maste 
GetData(lldb::offset_t offset,size_t length,DataExtractor & data) const47114f1b3e8SDimitry Andric size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
47214f1b3e8SDimitry Andric                            DataExtractor &data) const {
47314f1b3e8SDimitry Andric   // The entire file has already been mmap'ed into m_data, so just copy from
474f73363f1SDimitry Andric   // there as the back mmap buffer will be shared with shared pointers.
475f034231aSEd Maste   return data.SetData(m_data, offset, length);
476f034231aSEd Maste }
477f034231aSEd Maste 
CopyData(lldb::offset_t offset,size_t length,void * dst) const47814f1b3e8SDimitry Andric size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
47914f1b3e8SDimitry Andric                             void *dst) const {
48014f1b3e8SDimitry Andric   // The entire file has already been mmap'ed into m_data, so just copy from
481f73363f1SDimitry Andric   // there Note that the data remains in target byte order.
482f21a844fSEd Maste   return m_data.CopyData(offset, length, dst);
483f034231aSEd Maste }
484f034231aSEd Maste 
ReadSectionData(Section * section,lldb::offset_t section_offset,void * dst,size_t dst_len)485ef5d0b5eSDimitry Andric size_t ObjectFile::ReadSectionData(Section *section,
48614f1b3e8SDimitry Andric                                    lldb::offset_t section_offset, void *dst,
487ef5d0b5eSDimitry Andric                                    size_t dst_len) {
488205afe67SEd Maste   assert(section);
489205afe67SEd Maste   section_offset *= section->GetTargetByteSize();
490205afe67SEd Maste 
491f034231aSEd Maste   // If some other objectfile owns this data, pass this to them.
492f034231aSEd Maste   if (section->GetObjectFile() != this)
49314f1b3e8SDimitry Andric     return section->GetObjectFile()->ReadSectionData(section, section_offset,
49414f1b3e8SDimitry Andric                                                      dst, dst_len);
495f034231aSEd Maste 
496b60736ecSDimitry Andric   if (!section->IsRelocated())
497b60736ecSDimitry Andric     RelocateSection(section);
498b60736ecSDimitry Andric 
49914f1b3e8SDimitry Andric   if (IsInMemory()) {
500f034231aSEd Maste     ProcessSP process_sp(m_process_wp.lock());
50114f1b3e8SDimitry Andric     if (process_sp) {
502b76161e4SDimitry Andric       Status error;
50314f1b3e8SDimitry Andric       const addr_t base_load_addr =
50414f1b3e8SDimitry Andric           section->GetLoadBaseAddress(&process_sp->GetTarget());
505f034231aSEd Maste       if (base_load_addr != LLDB_INVALID_ADDRESS)
50614f1b3e8SDimitry Andric         return process_sp->ReadMemory(base_load_addr + section_offset, dst,
50714f1b3e8SDimitry Andric                                       dst_len, error);
508f034231aSEd Maste     }
50914f1b3e8SDimitry Andric   } else {
5100cac4ca3SEd Maste     const lldb::offset_t section_file_size = section->GetFileSize();
51114f1b3e8SDimitry Andric     if (section_offset < section_file_size) {
5120cac4ca3SEd Maste       const size_t section_bytes_left = section_file_size - section_offset;
5130cac4ca3SEd Maste       size_t section_dst_len = dst_len;
514f034231aSEd Maste       if (section_dst_len > section_bytes_left)
515f034231aSEd Maste         section_dst_len = section_bytes_left;
51614f1b3e8SDimitry Andric       return CopyData(section->GetFileOffset() + section_offset,
51714f1b3e8SDimitry Andric                       section_dst_len, dst);
51814f1b3e8SDimitry Andric     } else {
51914f1b3e8SDimitry Andric       if (section->GetType() == eSectionTypeZeroFill) {
520f034231aSEd Maste         const uint64_t section_size = section->GetByteSize();
521f034231aSEd Maste         const uint64_t section_bytes_left = section_size - section_offset;
522f034231aSEd Maste         uint64_t section_dst_len = dst_len;
523f034231aSEd Maste         if (section_dst_len > section_bytes_left)
524f034231aSEd Maste           section_dst_len = section_bytes_left;
525f21a844fSEd Maste         memset(dst, 0, section_dst_len);
526f034231aSEd Maste         return section_dst_len;
527f034231aSEd Maste       }
528f034231aSEd Maste     }
529f034231aSEd Maste   }
530f034231aSEd Maste   return 0;
531f034231aSEd Maste }
532f034231aSEd Maste 
533f034231aSEd Maste // Get the section data the file on disk
ReadSectionData(Section * section,DataExtractor & section_data)534ef5d0b5eSDimitry Andric size_t ObjectFile::ReadSectionData(Section *section,
535ef5d0b5eSDimitry Andric                                    DataExtractor &section_data) {
536f034231aSEd Maste   // If some other objectfile owns this data, pass this to them.
537f034231aSEd Maste   if (section->GetObjectFile() != this)
538f034231aSEd Maste     return section->GetObjectFile()->ReadSectionData(section, section_data);
539f034231aSEd Maste 
540b60736ecSDimitry Andric   if (!section->IsRelocated())
541b60736ecSDimitry Andric     RelocateSection(section);
542b60736ecSDimitry Andric 
54314f1b3e8SDimitry Andric   if (IsInMemory()) {
544f034231aSEd Maste     ProcessSP process_sp(m_process_wp.lock());
54514f1b3e8SDimitry Andric     if (process_sp) {
54614f1b3e8SDimitry Andric       const addr_t base_load_addr =
54714f1b3e8SDimitry Andric           section->GetLoadBaseAddress(&process_sp->GetTarget());
54814f1b3e8SDimitry Andric       if (base_load_addr != LLDB_INVALID_ADDRESS) {
54914f1b3e8SDimitry Andric         DataBufferSP data_sp(
55014f1b3e8SDimitry Andric             ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
55114f1b3e8SDimitry Andric         if (data_sp) {
552f034231aSEd Maste           section_data.SetData(data_sp, 0, data_sp->GetByteSize());
553f034231aSEd Maste           section_data.SetByteOrder(process_sp->GetByteOrder());
554f034231aSEd Maste           section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
555f034231aSEd Maste           return section_data.GetByteSize();
556f034231aSEd Maste         }
557f034231aSEd Maste       }
558f034231aSEd Maste     }
559b60736ecSDimitry Andric   }
560b60736ecSDimitry Andric 
561f73363f1SDimitry Andric   // The object file now contains a full mmap'ed copy of the object file
562f73363f1SDimitry Andric   // data, so just use this
563b1c73532SDimitry Andric   return GetData(section->GetFileOffset(), GetSectionDataSize(section),
56414f1b3e8SDimitry Andric                  section_data);
565f034231aSEd Maste }
566f034231aSEd Maste 
SplitArchivePathWithObject(llvm::StringRef path_with_object,FileSpec & archive_file,ConstString & archive_object,bool must_exist)567ead24645SDimitry Andric bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
56814f1b3e8SDimitry Andric                                             FileSpec &archive_file,
56914f1b3e8SDimitry Andric                                             ConstString &archive_object,
57014f1b3e8SDimitry Andric                                             bool must_exist) {
571ead24645SDimitry Andric   size_t len = path_with_object.size();
572ead24645SDimitry Andric   if (len < 2 || path_with_object.back() != ')')
573f034231aSEd Maste     return false;
574ead24645SDimitry Andric   llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
575ead24645SDimitry Andric   if (archive.empty())
576ead24645SDimitry Andric     return false;
577ead24645SDimitry Andric   llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
578ead24645SDimitry Andric   archive_file.SetFile(archive, FileSpec::Style::native);
579ead24645SDimitry Andric   if (must_exist && !FileSystem::Instance().Exists(archive_file))
580ead24645SDimitry Andric     return false;
581ead24645SDimitry Andric   archive_object.SetString(object);
582ead24645SDimitry Andric   return true;
583f034231aSEd Maste }
584f034231aSEd Maste 
ClearSymtab()58514f1b3e8SDimitry Andric void ObjectFile::ClearSymtab() {
586f034231aSEd Maste   ModuleSP module_sp(GetModule());
58714f1b3e8SDimitry Andric   if (module_sp) {
588145449b1SDimitry Andric     Log *log = GetLog(LLDBLog::Object);
589ead24645SDimitry Andric     LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
5900cac4ca3SEd Maste               static_cast<void *>(this),
5915f29bb8aSDimitry Andric               static_cast<void *>(m_symtab_up.get()));
592f65dcba8SDimitry Andric     // Since we need to clear the symbol table, we need a new llvm::once_flag
593f65dcba8SDimitry Andric     // instance so we can safely create another symbol table
594f65dcba8SDimitry Andric     m_symtab_once_up.reset(new llvm::once_flag());
5955f29bb8aSDimitry Andric     m_symtab_up.reset();
596f034231aSEd Maste   }
597f034231aSEd Maste }
598f034231aSEd Maste 
GetSectionList(bool update_module_section_list)59914f1b3e8SDimitry Andric SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
6005f29bb8aSDimitry Andric   if (m_sections_up == nullptr) {
60114f1b3e8SDimitry Andric     if (update_module_section_list) {
602f034231aSEd Maste       ModuleSP module_sp(GetModule());
60314f1b3e8SDimitry Andric       if (module_sp) {
604f3fbd1c0SDimitry Andric         std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
605f034231aSEd Maste         CreateSections(*module_sp->GetUnifiedSectionList());
606f034231aSEd Maste       }
60714f1b3e8SDimitry Andric     } else {
608e81d9d49SDimitry Andric       SectionList unified_section_list;
609e81d9d49SDimitry Andric       CreateSections(unified_section_list);
610e81d9d49SDimitry Andric     }
611e81d9d49SDimitry Andric   }
6125f29bb8aSDimitry Andric   return m_sections_up.get();
613f034231aSEd Maste }
614e81d9d49SDimitry Andric 
615e81d9d49SDimitry Andric lldb::SymbolType
GetSymbolTypeFromName(llvm::StringRef name,lldb::SymbolType symbol_type_hint)616e81d9d49SDimitry Andric ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,
61714f1b3e8SDimitry Andric                                   lldb::SymbolType symbol_type_hint) {
61814f1b3e8SDimitry Andric   if (!name.empty()) {
619312c0ed1SDimitry Andric     if (name.starts_with("_OBJC_")) {
620e81d9d49SDimitry Andric       // ObjC
621312c0ed1SDimitry Andric       if (name.starts_with("_OBJC_CLASS_$_"))
622e81d9d49SDimitry Andric         return lldb::eSymbolTypeObjCClass;
623312c0ed1SDimitry Andric       if (name.starts_with("_OBJC_METACLASS_$_"))
624e81d9d49SDimitry Andric         return lldb::eSymbolTypeObjCMetaClass;
625312c0ed1SDimitry Andric       if (name.starts_with("_OBJC_IVAR_$_"))
626e81d9d49SDimitry Andric         return lldb::eSymbolTypeObjCIVar;
627312c0ed1SDimitry Andric     } else if (name.starts_with(".objc_class_name_")) {
628e81d9d49SDimitry Andric       // ObjC v1
629e81d9d49SDimitry Andric       return lldb::eSymbolTypeObjCClass;
630e81d9d49SDimitry Andric     }
631e81d9d49SDimitry Andric   }
632e81d9d49SDimitry Andric   return symbol_type_hint;
633e81d9d49SDimitry Andric }
634f3fbd1c0SDimitry Andric 
635f73363f1SDimitry Andric std::vector<ObjectFile::LoadableData>
GetLoadableData(Target & target)636f73363f1SDimitry Andric ObjectFile::GetLoadableData(Target &target) {
637f73363f1SDimitry Andric   std::vector<LoadableData> loadables;
63874a628f7SDimitry Andric   SectionList *section_list = GetSectionList();
63974a628f7SDimitry Andric   if (!section_list)
640f73363f1SDimitry Andric     return loadables;
641f73363f1SDimitry Andric   // Create a list of loadable data from loadable sections
64274a628f7SDimitry Andric   size_t section_count = section_list->GetNumSections(0);
64374a628f7SDimitry Andric   for (size_t i = 0; i < section_count; ++i) {
644f73363f1SDimitry Andric     LoadableData loadable;
64574a628f7SDimitry Andric     SectionSP section_sp = section_list->GetSectionAtIndex(i);
646f73363f1SDimitry Andric     loadable.Dest =
647f73363f1SDimitry Andric         target.GetSectionLoadList().GetSectionLoadAddress(section_sp);
648f73363f1SDimitry Andric     if (loadable.Dest == LLDB_INVALID_ADDRESS)
649f73363f1SDimitry Andric       continue;
65074a628f7SDimitry Andric     // We can skip sections like bss
65174a628f7SDimitry Andric     if (section_sp->GetFileSize() == 0)
65274a628f7SDimitry Andric       continue;
653f73363f1SDimitry Andric     DataExtractor section_data;
65474a628f7SDimitry Andric     section_sp->GetSectionData(section_data);
655f73363f1SDimitry Andric     loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),
656f73363f1SDimitry Andric                                                 section_data.GetByteSize());
657f73363f1SDimitry Andric     loadables.push_back(loadable);
65874a628f7SDimitry Andric   }
659f73363f1SDimitry Andric   return loadables;
66074a628f7SDimitry Andric }
661ef5d0b5eSDimitry Andric 
CreateCallFrameInfo()662ead24645SDimitry Andric std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
663ead24645SDimitry Andric   return {};
664ead24645SDimitry Andric }
665ead24645SDimitry Andric 
RelocateSection(lldb_private::Section * section)666ef5d0b5eSDimitry Andric void ObjectFile::RelocateSection(lldb_private::Section *section)
667ef5d0b5eSDimitry Andric {
668ef5d0b5eSDimitry Andric }
66923629167SDimitry Andric 
MapFileData(const FileSpec & file,uint64_t Size,uint64_t Offset)67023629167SDimitry Andric DataBufferSP ObjectFile::MapFileData(const FileSpec &file, uint64_t Size,
67123629167SDimitry Andric                                      uint64_t Offset) {
67294994d37SDimitry Andric   return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
67394994d37SDimitry Andric }
67494994d37SDimitry Andric 
format(const ObjectFile::Type & type,raw_ostream & OS,StringRef Style)67594994d37SDimitry Andric void llvm::format_provider<ObjectFile::Type>::format(
67694994d37SDimitry Andric     const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
67794994d37SDimitry Andric   switch (type) {
67894994d37SDimitry Andric   case ObjectFile::eTypeInvalid:
67994994d37SDimitry Andric     OS << "invalid";
68094994d37SDimitry Andric     break;
68194994d37SDimitry Andric   case ObjectFile::eTypeCoreFile:
68294994d37SDimitry Andric     OS << "core file";
68394994d37SDimitry Andric     break;
68494994d37SDimitry Andric   case ObjectFile::eTypeExecutable:
68594994d37SDimitry Andric     OS << "executable";
68694994d37SDimitry Andric     break;
68794994d37SDimitry Andric   case ObjectFile::eTypeDebugInfo:
68894994d37SDimitry Andric     OS << "debug info";
68994994d37SDimitry Andric     break;
69094994d37SDimitry Andric   case ObjectFile::eTypeDynamicLinker:
69194994d37SDimitry Andric     OS << "dynamic linker";
69294994d37SDimitry Andric     break;
69394994d37SDimitry Andric   case ObjectFile::eTypeObjectFile:
69494994d37SDimitry Andric     OS << "object file";
69594994d37SDimitry Andric     break;
69694994d37SDimitry Andric   case ObjectFile::eTypeSharedLibrary:
69794994d37SDimitry Andric     OS << "shared library";
69894994d37SDimitry Andric     break;
69994994d37SDimitry Andric   case ObjectFile::eTypeStubLibrary:
70094994d37SDimitry Andric     OS << "stub library";
70194994d37SDimitry Andric     break;
70294994d37SDimitry Andric   case ObjectFile::eTypeJIT:
70394994d37SDimitry Andric     OS << "jit";
70494994d37SDimitry Andric     break;
70594994d37SDimitry Andric   case ObjectFile::eTypeUnknown:
70694994d37SDimitry Andric     OS << "unknown";
70794994d37SDimitry Andric     break;
70894994d37SDimitry Andric   }
70994994d37SDimitry Andric }
71094994d37SDimitry Andric 
format(const ObjectFile::Strata & strata,raw_ostream & OS,StringRef Style)71194994d37SDimitry Andric void llvm::format_provider<ObjectFile::Strata>::format(
71294994d37SDimitry Andric     const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
71394994d37SDimitry Andric   switch (strata) {
71494994d37SDimitry Andric   case ObjectFile::eStrataInvalid:
71594994d37SDimitry Andric     OS << "invalid";
71694994d37SDimitry Andric     break;
71794994d37SDimitry Andric   case ObjectFile::eStrataUnknown:
71894994d37SDimitry Andric     OS << "unknown";
71994994d37SDimitry Andric     break;
72094994d37SDimitry Andric   case ObjectFile::eStrataUser:
72194994d37SDimitry Andric     OS << "user";
72294994d37SDimitry Andric     break;
72394994d37SDimitry Andric   case ObjectFile::eStrataKernel:
72494994d37SDimitry Andric     OS << "kernel";
72594994d37SDimitry Andric     break;
72694994d37SDimitry Andric   case ObjectFile::eStrataRawImage:
72794994d37SDimitry Andric     OS << "raw image";
72894994d37SDimitry Andric     break;
72994994d37SDimitry Andric   case ObjectFile::eStrataJIT:
73094994d37SDimitry Andric     OS << "jit";
73194994d37SDimitry Andric     break;
73294994d37SDimitry Andric   }
73323629167SDimitry Andric }
734f65dcba8SDimitry Andric 
735f65dcba8SDimitry Andric 
GetSymtab()736f65dcba8SDimitry Andric Symtab *ObjectFile::GetSymtab() {
737f65dcba8SDimitry Andric   ModuleSP module_sp(GetModule());
738f65dcba8SDimitry Andric   if (module_sp) {
739f65dcba8SDimitry Andric     // We can't take the module lock in ObjectFile::GetSymtab() or we can
740f65dcba8SDimitry Andric     // deadlock in DWARF indexing when any file asks for the symbol table from
741f65dcba8SDimitry Andric     // an object file. This currently happens in the preloading of symbols in
742f65dcba8SDimitry Andric     // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
743f65dcba8SDimitry Andric     // module lock, and then threads will be spun up to index the DWARF and
744f65dcba8SDimitry Andric     // any of those threads might end up trying to relocate items in the DWARF
745f65dcba8SDimitry Andric     // sections which causes ObjectFile::GetSectionData(...) to relocate section
746f65dcba8SDimitry Andric     // data which requires the symbol table.
747f65dcba8SDimitry Andric     //
748f65dcba8SDimitry Andric     // So to work around this, we create the symbol table one time using
749f65dcba8SDimitry Andric     // llvm::once_flag, lock it, and then set the unique pointer. Any other
750f65dcba8SDimitry Andric     // thread that gets ahold of the symbol table before parsing is done, will
751f65dcba8SDimitry Andric     // not be able to access the symbol table contents since all APIs in Symtab
752f65dcba8SDimitry Andric     // are protected by a mutex in the Symtab object itself.
753f65dcba8SDimitry Andric     llvm::call_once(*m_symtab_once_up, [&]() {
754f65dcba8SDimitry Andric       Symtab *symtab = new Symtab(this);
755f65dcba8SDimitry Andric       std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
756f65dcba8SDimitry Andric       m_symtab_up.reset(symtab);
75777fc4c14SDimitry Andric       if (!m_symtab_up->LoadFromCache()) {
75877fc4c14SDimitry Andric         ElapsedTime elapsed(module_sp->GetSymtabParseTime());
759f65dcba8SDimitry Andric         ParseSymtab(*m_symtab_up);
760f65dcba8SDimitry Andric         m_symtab_up->Finalize();
76177fc4c14SDimitry Andric       }
762f65dcba8SDimitry Andric     });
763f65dcba8SDimitry Andric   }
764f65dcba8SDimitry Andric   return m_symtab_up.get();
765f65dcba8SDimitry Andric }
76677fc4c14SDimitry Andric 
GetCacheHash()76777fc4c14SDimitry Andric uint32_t ObjectFile::GetCacheHash() {
76877fc4c14SDimitry Andric   if (m_cache_hash)
76977fc4c14SDimitry Andric     return *m_cache_hash;
77077fc4c14SDimitry Andric   StreamString strm;
77177fc4c14SDimitry Andric   strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
77277fc4c14SDimitry Andric   m_cache_hash = llvm::djbHash(strm.GetString());
77377fc4c14SDimitry Andric   return *m_cache_hash;
77477fc4c14SDimitry Andric }
7757fa27ce4SDimitry Andric 
7767fa27ce4SDimitry Andric namespace llvm {
7777fa27ce4SDimitry Andric namespace json {
7787fa27ce4SDimitry Andric 
fromJSON(const llvm::json::Value & value,lldb_private::ObjectFile::Type & type,llvm::json::Path path)7797fa27ce4SDimitry Andric bool fromJSON(const llvm::json::Value &value,
7807fa27ce4SDimitry Andric               lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
7817fa27ce4SDimitry Andric   if (auto str = value.getAsString()) {
7827fa27ce4SDimitry Andric     type = llvm::StringSwitch<ObjectFile::Type>(*str)
7837fa27ce4SDimitry Andric                .Case("corefile", ObjectFile::eTypeCoreFile)
7847fa27ce4SDimitry Andric                .Case("executable", ObjectFile::eTypeExecutable)
7857fa27ce4SDimitry Andric                .Case("debuginfo", ObjectFile::eTypeDebugInfo)
7867fa27ce4SDimitry Andric                .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
7877fa27ce4SDimitry Andric                .Case("objectfile", ObjectFile::eTypeObjectFile)
7887fa27ce4SDimitry Andric                .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
7897fa27ce4SDimitry Andric                .Case("stublibrary", ObjectFile::eTypeStubLibrary)
7907fa27ce4SDimitry Andric                .Case("jit", ObjectFile::eTypeJIT)
7917fa27ce4SDimitry Andric                .Case("unknown", ObjectFile::eTypeUnknown)
7927fa27ce4SDimitry Andric                .Default(ObjectFile::eTypeInvalid);
7937fa27ce4SDimitry Andric 
7947fa27ce4SDimitry Andric     if (type == ObjectFile::eTypeInvalid) {
7957fa27ce4SDimitry Andric       path.report("invalid object type");
7967fa27ce4SDimitry Andric       return false;
7977fa27ce4SDimitry Andric     }
7987fa27ce4SDimitry Andric 
7997fa27ce4SDimitry Andric     return true;
8007fa27ce4SDimitry Andric   }
8017fa27ce4SDimitry Andric   path.report("expected string");
8027fa27ce4SDimitry Andric   return false;
8037fa27ce4SDimitry Andric }
8047fa27ce4SDimitry Andric } // namespace json
8057fa27ce4SDimitry Andric } // namespace llvm
806