LLDB mainline
ObjectFile.cpp
Go to the documentation of this file.
1//===-- ObjectFile.cpp ----------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "lldb/Core/Module.h"
13#include "lldb/Core/Section.h"
17#include "lldb/Target/Process.h"
19#include "lldb/Target/Target.h"
23#include "lldb/Utility/Log.h"
24#include "lldb/Utility/Timer.h"
25#include "lldb/lldb-private.h"
26
27#include "llvm/Support/DJB.h"
28
29using namespace lldb;
30using namespace lldb_private;
31
34
35static ObjectFileSP
37 lldb::offset_t file_offset, lldb::offset_t file_size,
38 DataBufferSP data_sp, lldb::offset_t &data_offset) {
40 for (uint32_t idx = 0;
42 idx)) != nullptr;
43 ++idx) {
44 std::unique_ptr<ObjectContainer> object_container_up(callback(
45 module_sp, data_sp, data_offset, file, file_offset, file_size));
46 if (object_container_up)
47 return object_container_up->GetObjectFile(file);
48 }
49 return {};
50}
51
53 const FileSpec *file,
54 lldb::offset_t file_offset,
55 lldb::offset_t file_size,
56 DataExtractorSP extractor_sp,
57 lldb::offset_t &data_offset) {
59 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
60 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
61 module_sp->GetFileSpec().GetPath().c_str(),
62 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
63 static_cast<uint64_t>(file_size));
64
65 if (!module_sp)
66 return {};
67
68 if (!file)
69 return {};
70
71 if (!extractor_sp || !extractor_sp->HasData()) {
72 const bool file_exists = FileSystem::Instance().Exists(*file);
73 // We have an object name which most likely means we have a .o file in
74 // a static archive (.a file). Try and see if we have a cached archive
75 // first without reading any data first
76 if (file_exists && module_sp->GetObjectName()) {
78 module_sp, file, file_offset, file_size, DataBufferSP(), data_offset);
79 if (object_file_sp)
80 return object_file_sp;
81 }
82 // Ok, we didn't find any containers that have a named object, now lets
83 // read the first 512 bytes from the file so the object file and object
84 // container plug-ins can use these bytes to see if they can parse this
85 // file.
86 if (file_size > 0) {
88 file->GetPath(), g_initial_bytes_to_read, file_offset);
89 extractor_sp = std::make_shared<DataExtractor>();
90 extractor_sp->SetData(buffer_sp, data_offset, buffer_sp->GetByteSize());
91 data_offset = 0;
92 }
93 }
94
95 if (!extractor_sp || !extractor_sp->HasData()) {
96 // Check for archive file with format "/path/to/archive.a(object.o)"
97 llvm::SmallString<256> path_with_object;
98 module_sp->GetFileSpec().GetPath(path_with_object);
99
100 FileSpec archive_file;
101 ConstString archive_object;
102 const bool must_exist = true;
103 if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,
104 archive_object, must_exist)) {
105 file_size = FileSystem::Instance().GetByteSize(archive_file);
106 if (file_size > 0) {
107 file = &archive_file;
108 module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
109 // Check if this is a object container by iterating through all
110 // object container plugin instances and then trying to get an
111 // object file from the container plugins since we had a name.
112 // Also, don't read
113 // ANY data in case there is data cached in the container plug-ins
114 // (like BSD archives caching the contained objects within an
115 // file).
117 module_sp, file, file_offset, file_size,
118 extractor_sp->GetSharedDataBuffer(), data_offset);
119 if (object_file_sp)
120 return object_file_sp;
121 // We failed to find any cached object files in the container plug-
122 // ins, so lets read the first 512 bytes and try again below...
124 archive_file.GetPath(), g_initial_bytes_to_read, file_offset);
125 extractor_sp = std::make_shared<DataExtractor>(buffer_sp);
126 }
127 }
128 }
129
130 if (extractor_sp && extractor_sp->HasData()) {
131 // Check if this is a normal object file by iterating through all
132 // object file plugin instances.
134 for (uint32_t idx = 0;
136 nullptr;
137 ++idx) {
138 ObjectFileSP object_file_sp(callback(module_sp, extractor_sp, data_offset,
139 file, file_offset, file_size));
140 if (object_file_sp.get())
141 return object_file_sp;
142 }
143
144 // Check if this is a object container by iterating through all object
145 // container plugin instances and then trying to get an object file
146 // from the container.
147 DataBufferSP buffer_sp = extractor_sp->GetSharedDataBuffer();
149 module_sp, file, file_offset, file_size, buffer_sp, data_offset);
150 if (object_file_sp)
151 return object_file_sp;
152 }
153
154 // We didn't find it, so clear our shared pointer in case it contains
155 // anything and return an empty shared pointer
156 return {};
157}
158
160 const ProcessSP &process_sp,
161 lldb::addr_t header_addr,
162 WritableDataBufferSP data_sp) {
163 ObjectFileSP object_file_sp;
164
165 if (module_sp) {
166 LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "
167 "%s, process = %p, header_addr = "
168 "0x%" PRIx64 ")",
169 module_sp->GetFileSpec().GetPath().c_str(),
170 static_cast<void *>(process_sp.get()), header_addr);
171 uint32_t idx;
172
173 // Check if this is a normal object file by iterating through all object
174 // file plugin instances.
175 ObjectFileCreateMemoryInstance create_callback;
176 for (idx = 0;
177 (create_callback =
179 nullptr;
180 ++idx) {
181 object_file_sp.reset(
182 create_callback(module_sp, data_sp, process_sp, header_addr));
183 if (object_file_sp.get())
184 return object_file_sp;
185 }
186 }
187
188 // We didn't find it, so clear our shared pointer in case it contains
189 // anything and return an empty shared pointer
190 object_file_sp.reset();
191 return object_file_sp;
192}
193
195 DataExtractorSP extractor_sp;
196 offset_t data_offset = 0;
197 ModuleSP module_sp = std::make_shared<Module>(file_spec);
198 return static_cast<bool>(ObjectFile::FindPlugin(
199 module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),
200 extractor_sp, data_offset));
201}
202
204 lldb::offset_t file_offset,
205 lldb::offset_t file_size,
206 ModuleSpecList &specs,
207 DataBufferSP data_sp) {
208 if (!data_sp)
210 file.GetPath(), g_initial_bytes_to_read, file_offset);
211 if (data_sp) {
212 if (file_size == 0) {
213 const lldb::offset_t actual_file_size =
215 if (actual_file_size > file_offset)
216 file_size = actual_file_size - file_offset;
217 }
218 return ObjectFile::GetModuleSpecifications(file, // file spec
219 data_sp, // data bytes
220 0, // data offset
221 file_offset, // file offset
222 file_size, // file length
223 specs);
224 }
225 return 0;
226}
227
229 const lldb_private::FileSpec &file, lldb::DataBufferSP &data_sp,
230 lldb::offset_t data_offset, lldb::offset_t file_offset,
232 const size_t initial_count = specs.GetSize();
234 uint32_t i;
235 // Try the ObjectFile plug-ins
236 for (i = 0;
237 (callback =
239 i)) != nullptr;
240 ++i) {
241 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
242 return specs.GetSize() - initial_count;
243 }
244
245 // Try the ObjectContainer plug-ins
246 for (i = 0;
247 (callback = PluginManager::
249 nullptr;
250 ++i) {
251 if (callback(file, data_sp, data_offset, file_offset, file_size, specs) > 0)
252 return specs.GetSize() - initial_count;
253 }
254 return 0;
255}
256
258 const FileSpec *file_spec_ptr,
259 lldb::offset_t file_offset, lldb::offset_t length,
260 lldb::DataExtractorSP extractor_sp,
261 lldb::offset_t data_offset)
262 : ModuleChild(module_sp),
263 m_file(), // This file could be different from the original module's file
265 m_file_offset(file_offset), m_length(length),
266 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(),
268 m_symtab_once_up(new llvm::once_flag()) {
269 if (file_spec_ptr)
270 m_file = *file_spec_ptr;
271 if (extractor_sp && extractor_sp->HasData()) {
272 m_data_nsp = extractor_sp;
273 // The offset & length fields may be specifying a subset of the
274 // total data buffer.
275 m_data_nsp->SetData(extractor_sp->GetSharedDataBuffer(), data_offset,
276 length);
277 }
278 Log *log = GetLog(LLDBLog::Object);
279 LLDB_LOGF(log,
280 "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
281 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
282 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
283 module_sp->GetSpecificationDescription().c_str(),
284 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
285 m_length);
286}
287
289 const ProcessSP &process_sp, lldb::addr_t header_addr,
290 DataExtractorSP header_extractor_sp)
291 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
293 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(process_sp),
294 m_memory_addr(header_addr), m_sections_up(), m_symtab_up(),
295 m_symtab_once_up(new llvm::once_flag()) {
296 if (header_extractor_sp && header_extractor_sp->HasData())
297 m_data_nsp = header_extractor_sp;
298 Log *log = GetLog(LLDBLog::Object);
299 LLDB_LOGF(log,
300 "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
301 "header_addr = 0x%" PRIx64,
302 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
303 module_sp->GetSpecificationDescription().c_str(),
304 static_cast<void *>(process_sp.get()), m_memory_addr);
305}
306
308 Log *log = GetLog(LLDBLog::Object);
309 LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
310}
311
313 ModuleSP module_sp(GetModule());
314 if (module_sp)
315 return module_sp->SetArchitecture(new_arch);
316 return false;
317}
318
320 Symtab *symtab = GetSymtab();
321 if (symtab) {
322 Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
323 if (symbol) {
324 if (symbol->ValueIsAddress()) {
325 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
326 if (section_sp) {
327 const SectionType section_type = section_sp->GetType();
328 switch (section_type) {
331 case eSectionTypeCode:
332 return AddressClass::eCode;
335 case eSectionTypeData:
347 return AddressClass::eData;
383 case eSectionTypeCTF:
401 // In case of absolute sections decide the address class based on
402 // the symbol type because the section type isn't specify if it is
403 // a code or a data section.
404 break;
405 }
406 }
407 }
408
409 const SymbolType symbol_type = symbol->GetType();
410 switch (symbol_type) {
411 case eSymbolTypeAny:
415 case eSymbolTypeCode:
416 return AddressClass::eCode;
418 return AddressClass::eCode;
420 return AddressClass::eCode;
421 case eSymbolTypeData:
422 return AddressClass::eData;
435 case eSymbolTypeBlock:
437 case eSymbolTypeLocal:
438 return AddressClass::eData;
439 case eSymbolTypeParam:
440 return AddressClass::eData;
442 return AddressClass::eData;
469 }
470 }
471 }
473}
474
476 lldb::addr_t addr,
477 size_t byte_size) {
478 WritableDataBufferSP data_sp;
479 if (process_sp) {
480 std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
482 const size_t bytes_read = process_sp->ReadMemory(
483 addr, data_up->GetBytes(), data_up->GetByteSize(), error);
484 if (bytes_read == byte_size)
485 data_sp.reset(data_up.release());
486 }
487 return data_sp;
488}
489
490size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
491 DataExtractor &data) const {
492 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
493 // there as the back mmap buffer will be shared with shared pointers.
494 return data.SetData(*m_data_nsp.get(), offset, length);
495}
496
497size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
498 void *dst) const {
499 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
500 // there Note that the data remains in target byte order.
501 return m_data_nsp->CopyData(offset, length, dst);
502}
503
505 lldb::offset_t section_offset, void *dst,
506 size_t dst_len) {
507 assert(section);
508 section_offset *= section->GetTargetByteSize();
509
510 // If some other objectfile owns this data, pass this to them.
511 if (section->GetObjectFile() != this)
512 return section->GetObjectFile()->ReadSectionData(section, section_offset,
513 dst, dst_len);
514
515 if (!section->IsRelocated())
516 RelocateSection(section);
517
518 if (IsInMemory()) {
519 ProcessSP process_sp(m_process_wp.lock());
520 if (process_sp) {
522 const addr_t base_load_addr =
523 section->GetLoadBaseAddress(&process_sp->GetTarget());
524 if (base_load_addr != LLDB_INVALID_ADDRESS)
525 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
526 dst_len, error);
527 }
528 } else {
529 const lldb::offset_t section_file_size = section->GetFileSize();
530 if (section_offset < section_file_size) {
531 const size_t section_bytes_left = section_file_size - section_offset;
532 size_t section_dst_len = dst_len;
533 if (section_dst_len > section_bytes_left)
534 section_dst_len = section_bytes_left;
535 return CopyData(section->GetFileOffset() + section_offset,
536 section_dst_len, dst);
537 } else {
538 if (section->GetType() == eSectionTypeZeroFill) {
539 const uint64_t section_size = section->GetByteSize();
540 const uint64_t section_bytes_left = section_size - section_offset;
541 uint64_t section_dst_len = dst_len;
542 if (section_dst_len > section_bytes_left)
543 section_dst_len = section_bytes_left;
544 memset(dst, 0, section_dst_len);
545 return section_dst_len;
546 }
547 }
548 }
549 return 0;
550}
551
552// Get the section data the file on disk
554 DataExtractor &section_data) {
555 // If some other objectfile owns this data, pass this to them.
556 if (section->GetObjectFile() != this)
557 return section->GetObjectFile()->ReadSectionData(section, section_data);
558
559 if (!section->IsRelocated())
560 RelocateSection(section);
561
562 if (IsInMemory()) {
563 ProcessSP process_sp(m_process_wp.lock());
564 if (process_sp) {
565 const addr_t base_load_addr =
566 section->GetLoadBaseAddress(&process_sp->GetTarget());
567 if (base_load_addr != LLDB_INVALID_ADDRESS) {
568 DataBufferSP data_sp(
569 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
570 if (data_sp) {
571 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
572 section_data.SetByteOrder(process_sp->GetByteOrder());
573 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
574 return section_data.GetByteSize();
575 }
576 }
577 }
578 }
579
580 // The object file now contains a full mmap'ed copy of the object file
581 // data, so just use this
582 return GetData(section->GetFileOffset(), GetSectionDataSize(section),
583 section_data);
584}
585
586bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
587 FileSpec &archive_file,
588 ConstString &archive_object,
589 bool must_exist) {
590 size_t len = path_with_object.size();
591 if (len < 2 || path_with_object.back() != ')')
592 return false;
593 llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
594 if (archive.empty())
595 return false;
596 llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
597 archive_file.SetFile(archive, FileSpec::Style::native);
598 if (must_exist && !FileSystem::Instance().Exists(archive_file))
599 return false;
600 archive_object.SetString(object);
601 return true;
602}
603
605 ModuleSP module_sp(GetModule());
606 if (module_sp) {
607 Log *log = GetLog(LLDBLog::Object);
608 LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
609 static_cast<void *>(this),
610 static_cast<void *>(m_symtab_up.get()));
611 // Since we need to clear the symbol table, we need a new llvm::once_flag
612 // instance so we can safely create another symbol table
613 m_symtab_once_up.reset(new llvm::once_flag());
614 m_symtab_up.reset();
615 }
616}
617
618SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
619 if (m_sections_up == nullptr) {
620 if (update_module_section_list) {
621 ModuleSP module_sp(GetModule());
622 if (module_sp) {
623 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
624 CreateSections(*module_sp->GetUnifiedSectionList());
625 }
626 } else {
627 SectionList unified_section_list;
628 CreateSections(unified_section_list);
629 }
630 }
631 return m_sections_up.get();
632}
633
636 lldb::SymbolType symbol_type_hint) {
637 if (!name.empty()) {
638 if (name.starts_with("_OBJC_")) {
639 // ObjC
640 if (name.starts_with("_OBJC_CLASS_$_"))
642 if (name.starts_with("_OBJC_METACLASS_$_"))
644 if (name.starts_with("_OBJC_IVAR_$_"))
646 } else if (name.starts_with(".objc_class_name_")) {
647 // ObjC v1
649 }
650 }
651 return symbol_type_hint;
652}
653
656 return llvm::StringSwitch<SectionType>(name)
657 .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
658 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
659 .Case("addr", eSectionTypeDWARFDebugAddr)
660 .Case("aranges", eSectionTypeDWARFDebugAranges)
661 .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
662 .Case("frame", eSectionTypeDWARFDebugFrame)
663 .Case("info", eSectionTypeDWARFDebugInfo)
664 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
665 .Cases({"line", "line.dwo"}, eSectionTypeDWARFDebugLine)
666 .Cases({"line_str", "line_str.dwo"}, eSectionTypeDWARFDebugLineStr)
667 .Case("loc", eSectionTypeDWARFDebugLoc)
668 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
669 .Case("loclists", eSectionTypeDWARFDebugLocLists)
670 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
671 .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
672 .Cases({"macro", "macro.dwo"}, eSectionTypeDWARFDebugMacro)
673 .Case("names", eSectionTypeDWARFDebugNames)
674 .Case("pubnames", eSectionTypeDWARFDebugPubNames)
675 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
676 .Case("ranges", eSectionTypeDWARFDebugRanges)
677 .Case("rnglists", eSectionTypeDWARFDebugRngLists)
678 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
679 .Case("str", eSectionTypeDWARFDebugStr)
680 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
681 .Cases({"str_offsets", "str_offs"}, eSectionTypeDWARFDebugStrOffsets)
682 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
683 .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
684 .Case("types", eSectionTypeDWARFDebugTypes)
685 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
686 .Default(eSectionTypeOther);
687}
688
689std::vector<ObjectFile::LoadableData>
691 std::vector<LoadableData> loadables;
692 SectionList *section_list = GetSectionList();
693 if (!section_list)
694 return loadables;
695 // Create a list of loadable data from loadable sections
696 size_t section_count = section_list->GetNumSections(0);
697 for (size_t i = 0; i < section_count; ++i) {
698 LoadableData loadable;
699 SectionSP section_sp = section_list->GetSectionAtIndex(i);
700 loadable.Dest = target.GetSectionLoadAddress(section_sp);
701 if (loadable.Dest == LLDB_INVALID_ADDRESS)
702 continue;
703 // We can skip sections like bss
704 if (section_sp->GetFileSize() == 0)
705 continue;
706 DataExtractor section_data;
707 section_sp->GetSectionData(section_data);
708 loadable.Contents = llvm::ArrayRef<uint8_t>(section_data.GetDataStart(),
709 section_data.GetByteSize());
710 loadables.push_back(loadable);
711 }
712 return loadables;
713}
714
715std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
716 return {};
717}
718
722
724 uint64_t Offset) {
725 return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
726}
727
728void llvm::format_provider<ObjectFile::Type>::format(
729 const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
730 switch (type) {
732 OS << "invalid";
733 break;
735 OS << "core file";
736 break;
738 OS << "executable";
739 break;
741 OS << "debug info";
742 break;
744 OS << "dynamic linker";
745 break;
747 OS << "object file";
748 break;
750 OS << "shared library";
751 break;
753 OS << "stub library";
754 break;
756 OS << "jit";
757 break;
759 OS << "unknown";
760 break;
761 }
762}
763
764void llvm::format_provider<ObjectFile::Strata>::format(
765 const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
766 switch (strata) {
768 OS << "invalid";
769 break;
771 OS << "unknown";
772 break;
774 OS << "user";
775 break;
777 OS << "kernel";
778 break;
780 OS << "raw image";
781 break;
783 OS << "jit";
784 break;
785 }
786}
787
788Symtab *ObjectFile::GetSymtab(bool can_create) {
789 ModuleSP module_sp(GetModule());
790 if (module_sp && can_create) {
791 // We can't take the module lock in ObjectFile::GetSymtab() or we can
792 // deadlock in DWARF indexing when any file asks for the symbol table from
793 // an object file. This currently happens in the preloading of symbols in
794 // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
795 // module lock, and then threads will be spun up to index the DWARF and
796 // any of those threads might end up trying to relocate items in the DWARF
797 // sections which causes ObjectFile::GetSectionData(...) to relocate section
798 // data which requires the symbol table.
799 //
800 // So to work around this, we create the symbol table one time using
801 // llvm::once_flag, lock it, and then set the unique pointer. Any other
802 // thread that gets ahold of the symbol table before parsing is done, will
803 // not be able to access the symbol table contents since all APIs in Symtab
804 // are protected by a mutex in the Symtab object itself.
805 llvm::call_once(*m_symtab_once_up, [&]() {
806 Symtab *symtab = new Symtab(this);
807 std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
808 m_symtab_up.reset(symtab);
809 if (!m_symtab_up->LoadFromCache()) {
810 ElapsedTime elapsed(module_sp->GetSymtabParseTime());
812 m_symtab_up->Finalize();
813 }
814 });
815 }
816 return m_symtab_up.get();
817}
818
820 if (m_cache_hash)
821 return *m_cache_hash;
822 StreamString strm;
823 strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
824 m_cache_hash = llvm::djbHash(strm.GetString());
825 return *m_cache_hash;
826}
827
828std::string ObjectFile::GetObjectName() const {
829 if (ModuleSP module_sp = GetModule())
830 if (ConstString object_name = module_sp->GetObjectName())
831 return llvm::formatv("{0}({1})", GetFileSpec().GetFilename().GetString(),
832 object_name.GetString())
833 .str();
834 return GetFileSpec().GetFilename().GetString();
835}
836
837namespace llvm {
838namespace json {
839
840bool fromJSON(const llvm::json::Value &value,
841 lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
842 if (auto str = value.getAsString()) {
843 type = llvm::StringSwitch<ObjectFile::Type>(*str)
844 .Case("corefile", ObjectFile::eTypeCoreFile)
845 .Case("executable", ObjectFile::eTypeExecutable)
846 .Case("debuginfo", ObjectFile::eTypeDebugInfo)
847 .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
848 .Case("objectfile", ObjectFile::eTypeObjectFile)
849 .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
850 .Case("stublibrary", ObjectFile::eTypeStubLibrary)
851 .Case("jit", ObjectFile::eTypeJIT)
852 .Case("unknown", ObjectFile::eTypeUnknown)
853 .Default(ObjectFile::eTypeInvalid);
854
855 if (type == ObjectFile::eTypeInvalid) {
856 path.report("invalid object type");
857 return false;
858 }
859
860 return true;
861 }
862 path.report("expected string");
863 return false;
864}
865} // namespace json
866} // namespace llvm
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:376
static ObjectFileSP CreateObjectFromContainer(const lldb::ModuleSP &module_sp, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t file_size, DataBufferSP data_sp, lldb::offset_t &data_offset)
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
#define LLDB_SCOPED_TIMERF(...)
Definition Timer.h:86
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition Address.h:432
An architecture specification class.
Definition ArchSpec.h:31
A uniqued constant string class.
Definition ConstString.h:40
std::string GetString() const
Get the string value as a std::string.
void SetString(llvm::StringRef s)
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
const uint8_t * GetDataStart() const
Get the data start pointer.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A class that measures elapsed time in an exception safe way.
Definition Statistics.h:76
A file utility class.
Definition FileSpec.h:57
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition FileSpec.cpp:174
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:251
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
ModuleChild(const lldb::ModuleSP &module_sp)
Construct with owning module.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:799
static bool IsObjectFile(lldb_private::FileSpec file_spec)
static lldb::ObjectFileSP FindPlugin(const lldb::ModuleSP &module_sp, const FileSpec *file_spec, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP extractor_sp, lldb::offset_t &data_offset)
Find a ObjectFile plug-in that can parse file_spec.
static lldb::DataBufferSP MapFileData(const FileSpec &file, uint64_t Size, uint64_t Offset)
virtual std::vector< LoadableData > GetLoadableData(Target &target)
Loads this objfile to memory.
~ObjectFile() override
Destructor.
std::unique_ptr< lldb_private::Symtab > m_symtab_up
Definition ObjectFile.h:800
const lldb::addr_t m_memory_addr
Set if the object file only exists in memory.
Definition ObjectFile.h:798
static size_t g_initial_bytes_to_read
The number of bytes to read when going through the plugins.
Definition ObjectFile.h:822
static lldb::SectionType GetDWARFSectionTypeFromName(llvm::StringRef name)
Parses the section type from a section name for DWARF sections.
virtual void ParseSymtab(Symtab &symtab)=0
Parse the symbol table into the provides symbol table object.
virtual AddressClass GetAddressClass(lldb::addr_t file_addr)
Get the address type given a file address in an object file.
Symtab * GetSymtab(bool can_create=true)
Gets the symbol table for the currently selected architecture (and object for archives).
DataExtractorNSP m_data_nsp
The data for this object file so things can be parsed lazily.
Definition ObjectFile.h:792
static lldb::WritableDataBufferSP ReadMemory(const lldb::ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
size_t GetData(lldb::offset_t offset, size_t length, DataExtractor &data) const
@ eTypeExecutable
A normal executable.
Definition ObjectFile.h:55
@ eTypeDebugInfo
An object file that contains only debug information.
Definition ObjectFile.h:57
@ eTypeStubLibrary
A library that can be linked against but not used for execution.
Definition ObjectFile.h:65
@ eTypeObjectFile
An intermediate object file.
Definition ObjectFile.h:61
@ eTypeDynamicLinker
The platform's dynamic linker executable.
Definition ObjectFile.h:59
@ eTypeCoreFile
A core file that has a checkpoint of a program's execution state.
Definition ObjectFile.h:53
@ eTypeSharedLibrary
A shared library that can be used during execution.
Definition ObjectFile.h:63
@ eTypeJIT
JIT code that has symbols, sections and possibly debug info.
Definition ObjectFile.h:67
lldb::addr_t m_file_offset
The offset in bytes into the file, or the address in memory.
Definition ObjectFile.h:787
static lldb::SymbolType GetSymbolTypeFromName(llvm::StringRef name, lldb::SymbolType symbol_type_hint=lldb::eSymbolTypeUndefined)
virtual size_t GetSectionDataSize(Section *section)
Definition ObjectFile.h:705
virtual std::unique_ptr< CallFrameInfo > CreateCallFrameInfo()
Creates a plugin-specific call frame info.
virtual void ClearSymtab()
Frees the symbol table.
bool SetModulesArchitecture(const ArchSpec &new_arch)
Sets the architecture for a module.
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition ObjectFile.h:282
std::string GetObjectName() const
static bool SplitArchivePathWithObject(llvm::StringRef path_with_object, lldb_private::FileSpec &archive_file, lldb_private::ConstString &archive_object, bool must_exist)
Split a path into a file path with object name.
virtual void CreateSections(SectionList &unified_section_list)=0
size_t CopyData(lldb::offset_t offset, size_t length, void *dst) const
virtual void RelocateSection(lldb_private::Section *section)
Perform relocations on the section if necessary.
std::optional< uint32_t > m_cache_hash
Definition ObjectFile.h:807
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
ObjectFile(const lldb::ModuleSP &module_sp, const FileSpec *file_spec_ptr, lldb::offset_t file_offset, lldb::offset_t length, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset)
Construct with a parent module, offset, and header data.
std::unique_ptr< llvm::once_flag > m_symtab_once_up
We need a llvm::once_flag that we can use to avoid locking the module lock and deadlocking LLDB.
Definition ObjectFile.h:806
bool IsInMemory() const
Returns true if the object file exists only in memory.
Definition ObjectFile.h:710
lldb::ProcessWP m_process_wp
Definition ObjectFile.h:796
uint32_t GetCacheHash()
Get a hash that can be used for caching object file releated information.
lldb::addr_t m_length
The length of this object file if it is known (can be zero if length is unknown or can't be determine...
Definition ObjectFile.h:789
virtual size_t ReadSectionData(Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len)
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataBufferSP data_sp=lldb::DataBufferSP())
virtual lldb::addr_t GetByteSize() const
Definition ObjectFile.h:275
static ObjectFileCreateMemoryInstance GetObjectFileCreateMemoryCallbackAtIndex(uint32_t idx)
static ObjectContainerCreateInstance GetObjectContainerCreateCallbackAtIndex(uint32_t idx)
static ObjectFileCreateInstance GetObjectFileCreateCallbackAtIndex(uint32_t idx)
static ObjectFileGetModuleSpecifications GetObjectContainerGetModuleSpecificationsCallbackAtIndex(uint32_t idx)
static ObjectFileGetModuleSpecifications GetObjectFileGetModuleSpecificationsCallbackAtIndex(uint32_t idx)
size_t GetNumSections(uint32_t depth) const
Definition Section.cpp:546
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:557
uint32_t GetTargetByteSize() const
Definition Section.h:276
lldb::offset_t GetFileOffset() const
Definition Section.h:183
ObjectFile * GetObjectFile()
Definition Section.h:233
lldb::SectionType GetType() const
Definition Section.h:217
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:233
lldb::addr_t GetByteSize() const
Definition Section.h:199
lldb::offset_t GetFileSize() const
Definition Section.h:189
bool IsRelocated() const
Definition Section.h:278
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Definition Stream.h:364
bool ValueIsAddress() const
Definition Symbol.cpp:165
Address & GetAddressRef()
Definition Symbol.h:73
lldb::SymbolType GetType() const
Definition Symbol.h:169
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1037
std::recursive_mutex & GetMutex()
Definition Symtab.h:51
lldb::addr_t GetSectionLoadAddress(const lldb::SectionSP &section_sp)
Definition Target.cpp:5335
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
size_t(* ObjectFileGetModuleSpecifications)(const FileSpec &file, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, lldb::offset_t file_offset, lldb::offset_t length, ModuleSpecList &module_specs)
ObjectContainer *(* ObjectContainerCreateInstance)(const lldb::ModuleSP &module_sp, lldb::DataBufferSP &data_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t offset, lldb::offset_t length)
ObjectFile *(* ObjectFileCreateMemoryInstance)(const lldb::ModuleSP &module_sp, lldb::WritableDataBufferSP data_sp, const lldb::ProcessSP &process_sp, lldb::addr_t offset)
bool fromJSON(const llvm::json::Value &value, TraceSupportedResponse &info, llvm::json::Path path)
ObjectFile *(* ObjectFileCreateInstance)(const lldb::ModuleSP &module_sp, lldb::DataExtractorSP extractor_sp, lldb::offset_t data_offset, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t length)
uint64_t offset_t
Definition lldb-types.h:85
std::shared_ptr< lldb_private::ObjectFile > ObjectFileSP
std::shared_ptr< lldb_private::Process > ProcessSP
SymbolType
Symbol types.
@ eSymbolTypeUndefined
@ eSymbolTypeVariableType
@ eSymbolTypeObjCMetaClass
@ eSymbolTypeReExported
@ eSymbolTypeObjCClass
@ eSymbolTypeObjectFile
@ eSymbolTypeTrampoline
@ eSymbolTypeResolver
@ eSymbolTypeSourceFile
@ eSymbolTypeException
@ eSymbolTypeVariable
@ eSymbolTypeAbsolute
@ eSymbolTypeAdditional
When symbols take more than one entry, the extra entries get this type.
@ eSymbolTypeInstrumentation
@ eSymbolTypeHeaderFile
@ eSymbolTypeCommonBlock
@ eSymbolTypeCompiler
@ eSymbolTypeLineHeader
@ eSymbolTypeObjCIVar
@ eSymbolTypeLineEntry
@ eSymbolTypeScopeBegin
@ eSymbolTypeScopeEnd
std::shared_ptr< lldb_private::DataBuffer > DataBufferSP
std::shared_ptr< lldb_private::Section > SectionSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80
@ eSectionTypeDWARFDebugStrOffsets
@ eSectionTypeELFDynamicSymbols
Elf SHT_DYNSYM section.
@ eSectionTypeInvalid
@ eSectionTypeDWARFDebugPubNames
@ eSectionTypeDataObjCCFStrings
Objective-C const CFString/NSString objects.
@ eSectionTypeZeroFill
@ eSectionTypeDWARFDebugLocDwo
@ eSectionTypeDWARFDebugFrame
@ eSectionTypeARMextab
@ eSectionTypeContainer
The section contains child sections.
@ eSectionTypeDWARFDebugLocLists
DWARF v5 .debug_loclists.
@ eSectionTypeDWARFDebugTypes
DWARF .debug_types section.
@ eSectionTypeDataSymbolAddress
Address of a symbol in the symbol table.
@ eSectionTypeELFDynamicLinkInfo
Elf SHT_DYNAMIC section.
@ eSectionTypeDWARFDebugMacInfo
@ eSectionTypeAbsoluteAddress
Dummy section for symbols with absolute address.
@ eSectionTypeCompactUnwind
compact unwind section in Mach-O, __TEXT,__unwind_info
@ eSectionTypeELFRelocationEntries
Elf SHT_REL or SHT_REL section.
@ eSectionTypeDWARFAppleNamespaces
@ eSectionTypeLLDBFormatters
@ eSectionTypeDWARFDebugNames
DWARF v5 .debug_names.
@ eSectionTypeDWARFDebugRngLists
DWARF v5 .debug_rnglists.
@ eSectionTypeEHFrame
@ eSectionTypeDWARFDebugStrOffsetsDwo
@ eSectionTypeDWARFDebugMacro
@ eSectionTypeDWARFAppleTypes
@ eSectionTypeDWARFDebugInfo
@ eSectionTypeDWARFDebugTypesDwo
@ eSectionTypeDWARFDebugRanges
@ eSectionTypeDWARFDebugRngListsDwo
@ eSectionTypeLLDBTypeSummaries
@ eSectionTypeGoSymtab
@ eSectionTypeARMexidx
@ eSectionTypeDWARFDebugLine
@ eSectionTypeDWARFDebugPubTypes
@ eSectionTypeDataObjCMessageRefs
Pointer to function pointer + selector.
@ eSectionTypeDWARFDebugTuIndex
@ eSectionTypeDWARFDebugStr
@ eSectionTypeDWARFDebugLineStr
DWARF v5 .debug_line_str.
@ eSectionTypeDWARFDebugLoc
@ eSectionTypeDWARFAppleNames
@ eSectionTypeDataCStringPointers
Pointers to C string data.
@ eSectionTypeDWARFAppleObjC
@ eSectionTypeSwiftModules
@ eSectionTypeDWARFDebugCuIndex
@ eSectionTypeDWARFDebugAranges
@ eSectionTypeDWARFDebugAbbrevDwo
@ eSectionTypeDWARFGNUDebugAltLink
@ eSectionTypeDWARFDebugStrDwo
@ eSectionTypeDWARFDebugAbbrev
@ eSectionTypeDataPointers
@ eSectionTypeDWARFDebugLocListsDwo
@ eSectionTypeDWARFDebugInfoDwo
@ eSectionTypeDWARFDebugAddr
@ eSectionTypeWasmName
@ eSectionTypeDataCString
Inlined C string data.
@ eSectionTypeELFSymbolTable
Elf SHT_SYMTAB section.
std::shared_ptr< lldb_private::DataExtractor > DataExtractorSP
std::shared_ptr< lldb_private::Module > ModuleSP
llvm::ArrayRef< uint8_t > Contents
Definition ObjectFile.h:98