1 //===-- ObjectFile.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Symbol/ObjectFile.h"
11 #include "Plugins/ObjectContainer/BSD-Archive/ObjectContainerBSDArchive.h"
12 #include "lldb/Core/DataBuffer.h"
13 #include "lldb/Core/DataBufferHeap.h"
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/ModuleSpec.h"
17 #include "lldb/Core/PluginManager.h"
18 #include "lldb/Core/RegularExpression.h"
19 #include "lldb/Core/Section.h"
20 #include "lldb/Core/Timer.h"
21 #include "lldb/Symbol/ObjectContainer.h"
22 #include "lldb/Symbol/SymbolFile.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/lldb-private.h"
27 using namespace lldb_private;
30 ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file,
31 lldb::offset_t file_offset, lldb::offset_t file_size,
32 DataBufferSP &data_sp, lldb::offset_t &data_offset) {
33 ObjectFileSP object_file_sp;
38 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
39 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
40 module_sp->GetFileSpec().GetPath().c_str(),
41 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
42 static_cast<uint64_t>(file_size));
44 FileSpec archive_file;
45 ObjectContainerCreateInstance create_object_container_callback;
47 const bool file_exists = file->Exists();
49 // We have an object name which most likely means we have
50 // a .o file in a static archive (.a file). Try and see if
51 // we have a cached archive first without reading any data
53 if (file_exists && module_sp->GetObjectName()) {
54 for (uint32_t idx = 0;
55 (create_object_container_callback =
56 PluginManager::GetObjectContainerCreateCallbackAtIndex(
59 std::unique_ptr<ObjectContainer> object_container_ap(
60 create_object_container_callback(module_sp, data_sp,
61 data_offset, file, file_offset,
64 if (object_container_ap.get())
65 object_file_sp = object_container_ap->GetObjectFile(file);
67 if (object_file_sp.get())
68 return object_file_sp;
71 // Ok, we didn't find any containers that have a named object, now
72 // lets read the first 512 bytes from the file so the object file
73 // and object container plug-ins can use these bytes to see if they
74 // can parse this file.
76 data_sp = file->ReadFileContents(file_offset,
77 std::min<size_t>(512, file_size));
82 if (!data_sp || data_sp->GetByteSize() == 0) {
83 // Check for archive file with format "/path/to/archive.a(object.o)"
84 char path_with_object[PATH_MAX * 2];
85 module_sp->GetFileSpec().GetPath(path_with_object,
86 sizeof(path_with_object));
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 = archive_file.GetByteSize();
95 module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
96 // Check if this is a object container by iterating through all
98 // container plugin instances and then trying to get an object file
99 // from the container plugins since we had a name. 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 file).
102 for (uint32_t idx = 0;
103 (create_object_container_callback =
104 PluginManager::GetObjectContainerCreateCallbackAtIndex(
107 std::unique_ptr<ObjectContainer> object_container_ap(
108 create_object_container_callback(module_sp, data_sp,
110 file_offset, file_size));
112 if (object_container_ap.get())
113 object_file_sp = object_container_ap->GetObjectFile(file);
115 if (object_file_sp.get())
116 return object_file_sp;
118 // We failed to find any cached object files in the container
119 // plug-ins, so lets read the first 512 bytes and try again below...
120 data_sp = archive_file.ReadFileContents(file_offset, 512);
125 if (data_sp && data_sp->GetByteSize() > 0) {
126 // Check if this is a normal object file by iterating through
127 // all object file plugin instances.
128 ObjectFileCreateInstance create_object_file_callback;
129 for (uint32_t idx = 0;
130 (create_object_file_callback =
131 PluginManager::GetObjectFileCreateCallbackAtIndex(idx)) !=
134 object_file_sp.reset(create_object_file_callback(
135 module_sp, data_sp, data_offset, file, file_offset, file_size));
136 if (object_file_sp.get())
137 return object_file_sp;
140 // Check if this is a object container by iterating through
141 // all object container plugin instances and then trying to get
142 // an object file from the container.
143 for (uint32_t idx = 0;
144 (create_object_container_callback =
145 PluginManager::GetObjectContainerCreateCallbackAtIndex(
148 std::unique_ptr<ObjectContainer> object_container_ap(
149 create_object_container_callback(module_sp, data_sp, data_offset,
150 file, file_offset, file_size));
152 if (object_container_ap.get())
153 object_file_sp = object_container_ap->GetObjectFile(file);
155 if (object_file_sp.get())
156 return object_file_sp;
161 // We didn't find it, so clear our shared pointer in case it
162 // contains anything and return an empty shared pointer
163 object_file_sp.reset();
164 return object_file_sp;
167 ObjectFileSP ObjectFile::FindPlugin(const lldb::ModuleSP &module_sp,
168 const ProcessSP &process_sp,
169 lldb::addr_t header_addr,
170 DataBufferSP &data_sp) {
171 ObjectFileSP object_file_sp;
174 Timer scoped_timer(LLVM_PRETTY_FUNCTION, "ObjectFile::FindPlugin (module = "
175 "%s, process = %p, header_addr = "
177 module_sp->GetFileSpec().GetPath().c_str(),
178 static_cast<void *>(process_sp.get()), header_addr);
181 // Check if this is a normal object file by iterating through
182 // all object file plugin instances.
183 ObjectFileCreateMemoryInstance create_callback;
186 PluginManager::GetObjectFileCreateMemoryCallbackAtIndex(idx)) !=
189 object_file_sp.reset(
190 create_callback(module_sp, data_sp, process_sp, header_addr));
191 if (object_file_sp.get())
192 return object_file_sp;
196 // We didn't find it, so clear our shared pointer in case it
197 // contains anything and return an empty shared pointer
198 object_file_sp.reset();
199 return object_file_sp;
202 size_t ObjectFile::GetModuleSpecifications(const FileSpec &file,
203 lldb::offset_t file_offset,
204 lldb::offset_t file_size,
205 ModuleSpecList &specs) {
206 DataBufferSP data_sp(file.ReadFileContents(file_offset, 512));
208 if (file_size == 0) {
209 const lldb::offset_t actual_file_size = file.GetByteSize();
210 if (actual_file_size > file_offset)
211 file_size = actual_file_size - file_offset;
213 return ObjectFile::GetModuleSpecifications(file, // file spec
214 data_sp, // data bytes
216 file_offset, // file offset
217 file_size, // file length
223 size_t ObjectFile::GetModuleSpecifications(
224 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
225 lldb::offset_t data_offset, lldb::offset_t file_offset,
226 lldb::offset_t file_size, lldb_private::ModuleSpecList &specs) {
227 const size_t initial_count = specs.GetSize();
228 ObjectFileGetModuleSpecifications callback;
230 // Try the ObjectFile plug-ins
233 PluginManager::GetObjectFileGetModuleSpecificationsCallbackAtIndex(
236 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
237 return specs.GetSize() - initial_count;
240 // Try the ObjectContainer plug-ins
242 (callback = PluginManager::
243 GetObjectContainerGetModuleSpecificationsCallbackAtIndex(i)) !=
246 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
247 return specs.GetSize() - initial_count;
252 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
253 const FileSpec *file_spec_ptr,
254 lldb::offset_t file_offset, lldb::offset_t length,
255 const lldb::DataBufferSP &data_sp,
256 lldb::offset_t data_offset)
257 : ModuleChild(module_sp),
258 m_file(), // This file could be different from the original module's file
259 m_type(eTypeInvalid), m_strata(eStrataInvalid),
260 m_file_offset(file_offset), m_length(length), m_data(),
261 m_unwind_table(*this), m_process_wp(),
262 m_memory_addr(LLDB_INVALID_ADDRESS), m_sections_ap(), m_symtab_ap(),
263 m_synthetic_symbol_idx(0) {
265 m_file = *file_spec_ptr;
267 m_data.SetData(data_sp, data_offset, length);
268 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
270 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
271 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
272 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
273 module_sp->GetSpecificationDescription().c_str(),
274 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
278 ObjectFile::ObjectFile(const lldb::ModuleSP &module_sp,
279 const ProcessSP &process_sp, lldb::addr_t header_addr,
280 DataBufferSP &header_data_sp)
281 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
282 m_strata(eStrataInvalid), m_file_offset(0), m_length(0), m_data(),
283 m_unwind_table(*this), m_process_wp(process_sp),
284 m_memory_addr(header_addr), m_sections_ap(), m_symtab_ap(),
285 m_synthetic_symbol_idx(0) {
287 m_data.SetData(header_data_sp, 0, header_data_sp->GetByteSize());
288 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
290 log->Printf("%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
291 "header_addr = 0x%" PRIx64,
292 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
293 module_sp->GetSpecificationDescription().c_str(),
294 static_cast<void *>(process_sp.get()), m_memory_addr);
297 ObjectFile::~ObjectFile() {
298 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
300 log->Printf("%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
303 bool ObjectFile::SetModulesArchitecture(const ArchSpec &new_arch) {
304 ModuleSP module_sp(GetModule());
306 return module_sp->SetArchitecture(new_arch);
310 AddressClass ObjectFile::GetAddressClass(addr_t file_addr) {
311 Symtab *symtab = GetSymtab();
313 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
315 if (symbol->ValueIsAddress()) {
316 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
318 const SectionType section_type = section_sp->GetType();
319 switch (section_type) {
320 case eSectionTypeInvalid:
321 return eAddressClassUnknown;
322 case eSectionTypeCode:
323 return eAddressClassCode;
324 case eSectionTypeContainer:
325 return eAddressClassUnknown;
326 case eSectionTypeData:
327 case eSectionTypeDataCString:
328 case eSectionTypeDataCStringPointers:
329 case eSectionTypeDataSymbolAddress:
330 case eSectionTypeData4:
331 case eSectionTypeData8:
332 case eSectionTypeData16:
333 case eSectionTypeDataPointers:
334 case eSectionTypeZeroFill:
335 case eSectionTypeDataObjCMessageRefs:
336 case eSectionTypeDataObjCCFStrings:
337 case eSectionTypeGoSymtab:
338 return eAddressClassData;
339 case eSectionTypeDebug:
340 case eSectionTypeDWARFDebugAbbrev:
341 case eSectionTypeDWARFDebugAddr:
342 case eSectionTypeDWARFDebugAranges:
343 case eSectionTypeDWARFDebugFrame:
344 case eSectionTypeDWARFDebugInfo:
345 case eSectionTypeDWARFDebugLine:
346 case eSectionTypeDWARFDebugLoc:
347 case eSectionTypeDWARFDebugMacInfo:
348 case eSectionTypeDWARFDebugMacro:
349 case eSectionTypeDWARFDebugPubNames:
350 case eSectionTypeDWARFDebugPubTypes:
351 case eSectionTypeDWARFDebugRanges:
352 case eSectionTypeDWARFDebugStr:
353 case eSectionTypeDWARFDebugStrOffsets:
354 case eSectionTypeDWARFAppleNames:
355 case eSectionTypeDWARFAppleTypes:
356 case eSectionTypeDWARFAppleNamespaces:
357 case eSectionTypeDWARFAppleObjC:
358 return eAddressClassDebug;
359 case eSectionTypeEHFrame:
360 case eSectionTypeARMexidx:
361 case eSectionTypeARMextab:
362 case eSectionTypeCompactUnwind:
363 return eAddressClassRuntime;
364 case eSectionTypeELFSymbolTable:
365 case eSectionTypeELFDynamicSymbols:
366 case eSectionTypeELFRelocationEntries:
367 case eSectionTypeELFDynamicLinkInfo:
368 case eSectionTypeOther:
369 return eAddressClassUnknown;
370 case eSectionTypeAbsoluteAddress:
371 // In case of absolute sections decide the address class based on
373 // type because the section type isn't specify if it is a code or a
381 const SymbolType symbol_type = symbol->GetType();
382 switch (symbol_type) {
384 return eAddressClassUnknown;
385 case eSymbolTypeAbsolute:
386 return eAddressClassUnknown;
387 case eSymbolTypeCode:
388 return eAddressClassCode;
389 case eSymbolTypeTrampoline:
390 return eAddressClassCode;
391 case eSymbolTypeResolver:
392 return eAddressClassCode;
393 case eSymbolTypeData:
394 return eAddressClassData;
395 case eSymbolTypeRuntime:
396 return eAddressClassRuntime;
397 case eSymbolTypeException:
398 return eAddressClassRuntime;
399 case eSymbolTypeSourceFile:
400 return eAddressClassDebug;
401 case eSymbolTypeHeaderFile:
402 return eAddressClassDebug;
403 case eSymbolTypeObjectFile:
404 return eAddressClassDebug;
405 case eSymbolTypeCommonBlock:
406 return eAddressClassDebug;
407 case eSymbolTypeBlock:
408 return eAddressClassDebug;
409 case eSymbolTypeLocal:
410 return eAddressClassData;
411 case eSymbolTypeParam:
412 return eAddressClassData;
413 case eSymbolTypeVariable:
414 return eAddressClassData;
415 case eSymbolTypeVariableType:
416 return eAddressClassDebug;
417 case eSymbolTypeLineEntry:
418 return eAddressClassDebug;
419 case eSymbolTypeLineHeader:
420 return eAddressClassDebug;
421 case eSymbolTypeScopeBegin:
422 return eAddressClassDebug;
423 case eSymbolTypeScopeEnd:
424 return eAddressClassDebug;
425 case eSymbolTypeAdditional:
426 return eAddressClassUnknown;
427 case eSymbolTypeCompiler:
428 return eAddressClassDebug;
429 case eSymbolTypeInstrumentation:
430 return eAddressClassDebug;
431 case eSymbolTypeUndefined:
432 return eAddressClassUnknown;
433 case eSymbolTypeObjCClass:
434 return eAddressClassRuntime;
435 case eSymbolTypeObjCMetaClass:
436 return eAddressClassRuntime;
437 case eSymbolTypeObjCIVar:
438 return eAddressClassRuntime;
439 case eSymbolTypeReExported:
440 return eAddressClassRuntime;
444 return eAddressClassUnknown;
447 DataBufferSP ObjectFile::ReadMemory(const ProcessSP &process_sp,
448 lldb::addr_t addr, size_t byte_size) {
449 DataBufferSP data_sp;
451 std::unique_ptr<DataBufferHeap> data_ap(new DataBufferHeap(byte_size, 0));
453 const size_t bytes_read = process_sp->ReadMemory(
454 addr, data_ap->GetBytes(), data_ap->GetByteSize(), error);
455 if (bytes_read == byte_size)
456 data_sp.reset(data_ap.release());
461 size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
462 DataExtractor &data) const {
463 // The entire file has already been mmap'ed into m_data, so just copy from
465 // as the back mmap buffer will be shared with shared pointers.
466 return data.SetData(m_data, offset, length);
469 size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
471 // The entire file has already been mmap'ed into m_data, so just copy from
473 // Note that the data remains in target byte order.
474 return m_data.CopyData(offset, length, dst);
477 size_t ObjectFile::ReadSectionData(const Section *section,
478 lldb::offset_t section_offset, void *dst,
479 size_t dst_len) const {
481 section_offset *= section->GetTargetByteSize();
483 // If some other objectfile owns this data, pass this to them.
484 if (section->GetObjectFile() != this)
485 return section->GetObjectFile()->ReadSectionData(section, section_offset,
489 ProcessSP process_sp(m_process_wp.lock());
492 const addr_t base_load_addr =
493 section->GetLoadBaseAddress(&process_sp->GetTarget());
494 if (base_load_addr != LLDB_INVALID_ADDRESS)
495 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
499 const lldb::offset_t section_file_size = section->GetFileSize();
500 if (section_offset < section_file_size) {
501 const size_t section_bytes_left = section_file_size - section_offset;
502 size_t section_dst_len = dst_len;
503 if (section_dst_len > section_bytes_left)
504 section_dst_len = section_bytes_left;
505 return CopyData(section->GetFileOffset() + section_offset,
506 section_dst_len, dst);
508 if (section->GetType() == eSectionTypeZeroFill) {
509 const uint64_t section_size = section->GetByteSize();
510 const uint64_t section_bytes_left = section_size - section_offset;
511 uint64_t section_dst_len = dst_len;
512 if (section_dst_len > section_bytes_left)
513 section_dst_len = section_bytes_left;
514 memset(dst, 0, section_dst_len);
515 return section_dst_len;
522 //----------------------------------------------------------------------
523 // Get the section data the file on disk
524 //----------------------------------------------------------------------
525 size_t ObjectFile::ReadSectionData(const Section *section,
526 DataExtractor §ion_data) const {
527 // If some other objectfile owns this data, pass this to them.
528 if (section->GetObjectFile() != this)
529 return section->GetObjectFile()->ReadSectionData(section, section_data);
532 ProcessSP process_sp(m_process_wp.lock());
534 const addr_t base_load_addr =
535 section->GetLoadBaseAddress(&process_sp->GetTarget());
536 if (base_load_addr != LLDB_INVALID_ADDRESS) {
537 DataBufferSP data_sp(
538 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
540 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
541 section_data.SetByteOrder(process_sp->GetByteOrder());
542 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
543 return section_data.GetByteSize();
547 return GetData(section->GetFileOffset(), section->GetFileSize(),
550 // The object file now contains a full mmap'ed copy of the object file data,
552 return MemoryMapSectionData(section, section_data);
556 size_t ObjectFile::MemoryMapSectionData(const Section *section,
557 DataExtractor §ion_data) const {
558 // If some other objectfile owns this data, pass this to them.
559 if (section->GetObjectFile() != this)
560 return section->GetObjectFile()->MemoryMapSectionData(section,
564 return ReadSectionData(section, section_data);
566 // The object file now contains a full mmap'ed copy of the object file data,
568 return GetData(section->GetFileOffset(), section->GetFileSize(),
573 bool ObjectFile::SplitArchivePathWithObject(const char *path_with_object,
574 FileSpec &archive_file,
575 ConstString &archive_object,
577 RegularExpression g_object_regex(llvm::StringRef("(.*)\\(([^\\)]+)\\)$"));
578 RegularExpression::Match regex_match(2);
579 if (g_object_regex.Execute(llvm::StringRef::withNullAsEmpty(path_with_object),
583 if (regex_match.GetMatchAtIndex(path_with_object, 1, path) &&
584 regex_match.GetMatchAtIndex(path_with_object, 2, obj)) {
585 archive_file.SetFile(path, false);
586 archive_object.SetCString(obj.c_str());
587 if (must_exist && !archive_file.Exists())
595 void ObjectFile::ClearSymtab() {
596 ModuleSP module_sp(GetModule());
598 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
599 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_OBJECT));
601 log->Printf("%p ObjectFile::ClearSymtab () symtab = %p",
602 static_cast<void *>(this),
603 static_cast<void *>(m_symtab_ap.get()));
608 SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
609 if (m_sections_ap.get() == nullptr) {
610 if (update_module_section_list) {
611 ModuleSP module_sp(GetModule());
613 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
614 CreateSections(*module_sp->GetUnifiedSectionList());
617 SectionList unified_section_list;
618 CreateSections(unified_section_list);
621 return m_sections_ap.get();
625 ObjectFile::GetSymbolTypeFromName(llvm::StringRef name,
626 lldb::SymbolType symbol_type_hint) {
628 if (name.startswith("_OBJC_")) {
630 if (name.startswith("_OBJC_CLASS_$_"))
631 return lldb::eSymbolTypeObjCClass;
632 if (name.startswith("_OBJC_METACLASS_$_"))
633 return lldb::eSymbolTypeObjCMetaClass;
634 if (name.startswith("_OBJC_IVAR_$_"))
635 return lldb::eSymbolTypeObjCIVar;
636 } else if (name.startswith(".objc_class_name_")) {
638 return lldb::eSymbolTypeObjCClass;
641 return symbol_type_hint;
644 ConstString ObjectFile::GetNextSyntheticSymbolName() {
646 ConstString file_name = GetModule()->GetFileSpec().GetFilename();
647 ss.Printf("___lldb_unnamed_symbol%u$$%s", ++m_synthetic_symbol_idx,
648 file_name.GetCString());
649 return ConstString(ss.GetString());