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