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