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