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