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) {
87 // Check that we made a data buffer. For instance, a directory node is
88 // not 0 size, but we can't make a data buffer for it.
89 if (DataBufferSP buffer_sp = FileSystem::Instance().CreateDataBuffer(
90 file->GetPath(), g_initial_bytes_to_read, file_offset)) {
91 extractor_sp = std::make_shared<DataExtractor>(buffer_sp);
92 data_offset = 0;
93 }
94 }
95 }
96
97 if (!extractor_sp || !extractor_sp->HasData()) {
98 // Check for archive file with format "/path/to/archive.a(object.o)"
99 llvm::SmallString<256> path_with_object;
100 module_sp->GetFileSpec().GetPath(path_with_object);
101
102 FileSpec archive_file;
103 ConstString archive_object;
104 const bool must_exist = true;
105 if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,
106 archive_object, must_exist)) {
107 file_size = FileSystem::Instance().GetByteSize(archive_file);
108 if (file_size > 0) {
109 file = &archive_file;
110 module_sp->SetFileSpecAndObjectName(archive_file, archive_object);
111 // Check if this is a object container by iterating through all
112 // object container plugin instances and then trying to get an
113 // object file from the container plugins since we had a name.
114 // Also, don't read
115 // ANY data in case there is data cached in the container plug-ins
116 // (like BSD archives caching the contained objects within an
117 // file).
119 module_sp, file, file_offset, file_size,
120 extractor_sp->GetSharedDataBuffer(), data_offset);
121 if (object_file_sp)
122 return object_file_sp;
123 // We failed to find any cached object files in the container plug-
124 // ins, so lets read the first 512 bytes and try again below...
126 archive_file.GetPath(), g_initial_bytes_to_read, file_offset);
127 extractor_sp = std::make_shared<DataExtractor>(buffer_sp);
128 }
129 }
130 }
131
132 if (extractor_sp && extractor_sp->HasData()) {
133 // Check if this is a normal object file by iterating through all
134 // object file plugin instances.
136 for (uint32_t idx = 0;
138 nullptr;
139 ++idx) {
140 ObjectFileSP object_file_sp(callback(module_sp, extractor_sp, data_offset,
141 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 all object
147 // container plugin instances and then trying to get an object file
148 // from the container.
149 DataBufferSP buffer_sp = extractor_sp->GetSharedDataBuffer();
151 module_sp, file, file_offset, file_size, buffer_sp, data_offset);
152 if (object_file_sp)
153 return object_file_sp;
154 }
155
156 // We didn't find it, so clear our shared pointer in case it contains
157 // anything and return an empty shared pointer
158 return {};
159}
160
162 const ProcessSP &process_sp,
163 lldb::addr_t header_addr,
164 WritableDataBufferSP data_sp) {
165 ObjectFileSP object_file_sp;
166
167 if (module_sp) {
168 LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "
169 "%s, process = %p, header_addr = "
170 "0x%" PRIx64 ")",
171 module_sp->GetFileSpec().GetPath().c_str(),
172 static_cast<void *>(process_sp.get()), header_addr);
173 uint32_t idx;
174
175 // Check if this is a normal object file by iterating through all object
176 // file plugin instances.
177 ObjectFileCreateMemoryInstance create_callback;
178 for (idx = 0;
179 (create_callback =
181 nullptr;
182 ++idx) {
183 object_file_sp.reset(
184 create_callback(module_sp, data_sp, process_sp, header_addr));
185 if (object_file_sp.get())
186 return object_file_sp;
187 }
188 }
189
190 // We didn't find it, so clear our shared pointer in case it contains
191 // anything and return an empty shared pointer
192 object_file_sp.reset();
193 return object_file_sp;
194}
195
197 DataExtractorSP extractor_sp;
198 offset_t data_offset = 0;
199 ModuleSP module_sp = std::make_shared<Module>(file_spec);
200 return static_cast<bool>(ObjectFile::FindPlugin(
201 module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),
202 extractor_sp, data_offset));
203}
204
206 lldb::offset_t file_offset,
207 lldb::offset_t file_size,
208 ModuleSpecList &specs,
209 DataExtractorSP extractor_sp) {
210 if (!extractor_sp)
211 extractor_sp = std::make_shared<DataExtractor>();
212 if (!extractor_sp->HasData()) {
213 if (DataBufferSP file_data_sp = FileSystem::Instance().CreateDataBuffer(
214 file.GetPath(), g_initial_bytes_to_read, file_offset))
215 extractor_sp->SetData(file_data_sp);
216 }
217 if (extractor_sp->HasData()) {
218 if (file_size == 0) {
219 const lldb::offset_t actual_file_size =
221 if (actual_file_size > file_offset)
222 file_size = actual_file_size - file_offset;
223 }
224 return ObjectFile::GetModuleSpecifications(file, // file spec
225 extractor_sp, // data bytes
226 0, // data offset
227 file_offset, // file offset
228 file_size, // file length
229 specs);
230 }
231 return 0;
232}
233
235 const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp,
236 lldb::offset_t data_offset, lldb::offset_t file_offset,
238 const size_t initial_count = specs.GetSize();
240 uint32_t i;
241 // Try the ObjectFile plug-ins
242 for (i = 0;
243 (callback =
245 i)) != nullptr;
246 ++i) {
247 if (callback(file, extractor_sp, data_offset, file_offset, file_size,
248 specs) > 0)
249 return specs.GetSize() - initial_count;
250 }
251
252 // Try the ObjectContainer plug-ins
253 for (i = 0;
254 (callback = PluginManager::
256 nullptr;
257 ++i) {
258 if (callback(file, extractor_sp, data_offset, file_offset, file_size,
259 specs) > 0)
260 return specs.GetSize() - initial_count;
261 }
262 return 0;
263}
264
266 const FileSpec *file_spec_ptr,
267 lldb::offset_t file_offset, lldb::offset_t length,
268 lldb::DataExtractorSP extractor_sp,
269 lldb::offset_t data_offset)
270 : ModuleChild(module_sp),
271 m_file(), // This file could be different from the original module's file
273 m_file_offset(file_offset), m_length(length),
274 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(),
276 m_symtab_once_up(new llvm::once_flag()) {
277 if (file_spec_ptr)
278 m_file = *file_spec_ptr;
279 if (extractor_sp && extractor_sp->HasData()) {
280 m_data_nsp = extractor_sp;
281 // The offset & length fields may be specifying a subset of the
282 // total data buffer.
283 m_data_nsp->SetData(extractor_sp->GetSharedDataBuffer(), data_offset,
284 length);
285 }
286 Log *log = GetLog(LLDBLog::Object);
287 LLDB_LOGF(log,
288 "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
289 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
290 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
291 module_sp->GetSpecificationDescription().c_str(),
292 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
293 m_length);
294}
295
297 const ProcessSP &process_sp, lldb::addr_t header_addr,
298 DataExtractorSP header_extractor_sp)
299 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
301 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(process_sp),
302 m_memory_addr(header_addr), m_sections_up(), m_symtab_up(),
303 m_symtab_once_up(new llvm::once_flag()) {
304 if (header_extractor_sp && header_extractor_sp->HasData())
305 m_data_nsp = header_extractor_sp;
306 Log *log = GetLog(LLDBLog::Object);
307 LLDB_LOGF(log,
308 "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
309 "header_addr = 0x%" PRIx64,
310 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
311 module_sp->GetSpecificationDescription().c_str(),
312 static_cast<void *>(process_sp.get()), m_memory_addr);
313}
314
316 Log *log = GetLog(LLDBLog::Object);
317 LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
318}
319
321 ModuleSP module_sp(GetModule());
322 if (module_sp)
323 return module_sp->SetArchitecture(new_arch);
324 return false;
325}
326
328 Symtab *symtab = GetSymtab();
329 if (symtab) {
330 const Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
331 if (symbol) {
332 if (symbol->ValueIsAddress()) {
333 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
334 if (section_sp) {
335 const SectionType section_type = section_sp->GetType();
336 switch (section_type) {
339 case eSectionTypeCode:
340 return AddressClass::eCode;
343 case eSectionTypeData:
355 return AddressClass::eData;
391 case eSectionTypeCTF:
409 // In case of absolute sections decide the address class based on
410 // the symbol type because the section type isn't specify if it is
411 // a code or a data section.
412 break;
413 }
414 }
415 }
416
417 const SymbolType symbol_type = symbol->GetType();
418 switch (symbol_type) {
419 case eSymbolTypeAny:
423 case eSymbolTypeCode:
424 return AddressClass::eCode;
426 return AddressClass::eCode;
428 return AddressClass::eCode;
429 case eSymbolTypeData:
430 return AddressClass::eData;
443 case eSymbolTypeBlock:
445 case eSymbolTypeLocal:
446 return AddressClass::eData;
447 case eSymbolTypeParam:
448 return AddressClass::eData;
450 return AddressClass::eData;
477 }
478 }
479 }
481}
482
484 lldb::addr_t addr,
485 size_t byte_size) {
486 WritableDataBufferSP data_sp;
487 if (process_sp) {
488 std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
490 const size_t bytes_read = process_sp->ReadMemory(
491 addr, data_up->GetBytes(), data_up->GetByteSize(), error);
492 if (bytes_read == byte_size)
493 data_sp.reset(data_up.release());
494 }
495 return data_sp;
496}
497
498size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
499 DataExtractorSP &data_sp) const {
500 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
501 // there as the back mmap buffer will be shared with shared pointers.
502 data_sp = m_data_nsp->GetSubsetExtractorSP(offset, length);
503 return data_sp->GetByteSize();
504}
505
506size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
507 void *dst) const {
508 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
509 // there Note that the data remains in target byte order.
510 return m_data_nsp->CopyData(offset, length, dst);
511}
512
514 lldb::offset_t section_offset, void *dst,
515 size_t dst_len) {
516 assert(section);
517
518 // If some other objectfile owns this data, pass this to them.
519 if (section->GetObjectFile() != this)
520 return section->GetObjectFile()->ReadSectionData(section, section_offset,
521 dst, dst_len);
522
523 if (!section->IsRelocated())
524 RelocateSection(section);
525
526 if (IsInMemory()) {
527 ProcessSP process_sp(m_process_wp.lock());
528 if (process_sp) {
530 const addr_t base_load_addr =
531 section->GetLoadBaseAddress(&process_sp->GetTarget());
532 if (base_load_addr != LLDB_INVALID_ADDRESS)
533 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
534 dst_len, error);
535 }
536 } else {
537 const lldb::offset_t section_file_size = section->GetFileSize();
538 if (section_offset < section_file_size) {
539 const size_t section_bytes_left = section_file_size - section_offset;
540 size_t section_dst_len = dst_len;
541 if (section_dst_len > section_bytes_left)
542 section_dst_len = section_bytes_left;
543 return CopyData(section->GetFileOffset() + section_offset,
544 section_dst_len, dst);
545 } else {
546 if (section->GetType() == eSectionTypeZeroFill) {
547 const uint64_t section_size = section->GetByteSize();
548 const uint64_t section_bytes_left = section_size - section_offset;
549 uint64_t section_dst_len = dst_len;
550 if (section_dst_len > section_bytes_left)
551 section_dst_len = section_bytes_left;
552 memset(dst, 0, section_dst_len);
553 return section_dst_len;
554 }
555 }
556 }
557 return 0;
558}
559
560// Get the section data the file on disk
562 DataExtractor &section_data) {
563 // If some other objectfile owns this data, pass this to them.
564 if (section->GetObjectFile() != this)
565 return section->GetObjectFile()->ReadSectionData(section, section_data);
566
567 if (!section->IsRelocated())
568 RelocateSection(section);
569
570 if (IsInMemory()) {
571 ProcessSP process_sp(m_process_wp.lock());
572 if (process_sp) {
573 const addr_t base_load_addr =
574 section->GetLoadBaseAddress(&process_sp->GetTarget());
575 if (base_load_addr != LLDB_INVALID_ADDRESS) {
576 DataBufferSP data_sp(
577 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
578 if (data_sp) {
579 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
580 section_data.SetByteOrder(process_sp->GetByteOrder());
581 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
582 return section_data.GetByteSize();
583 }
584 }
585 }
586 }
587
588 // The object file now contains a full mmap'ed copy of the object file
589 // data, so just use this
590 DataExtractorSP extractor_sp;
591 size_t ret_size = GetData(section->GetFileOffset(),
592 GetSectionDataSize(section), extractor_sp);
593 section_data = *extractor_sp;
594 return ret_size;
595}
596
597bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
598 FileSpec &archive_file,
599 ConstString &archive_object,
600 bool must_exist) {
601 size_t len = path_with_object.size();
602 if (len < 2 || path_with_object.back() != ')')
603 return false;
604 llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
605 if (archive.empty())
606 return false;
607 llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
608 archive_file.SetFile(archive, FileSpec::Style::native);
609 if (must_exist && !FileSystem::Instance().Exists(archive_file))
610 return false;
611 archive_object.SetString(object);
612 return true;
613}
614
616 ModuleSP module_sp(GetModule());
617 if (module_sp) {
618 Log *log = GetLog(LLDBLog::Object);
619 LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
620 static_cast<void *>(this),
621 static_cast<void *>(m_symtab_up.get()));
622 // Since we need to clear the symbol table, we need a new llvm::once_flag
623 // instance so we can safely create another symbol table
624 m_symtab_once_up.reset(new llvm::once_flag());
625 m_symtab_up.reset();
626 }
627}
628
629SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
630 if (m_sections_up == nullptr) {
631 if (update_module_section_list) {
632 ModuleSP module_sp(GetModule());
633 if (module_sp) {
634 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
635 CreateSections(*module_sp->GetUnifiedSectionList());
636 }
637 } else {
638 SectionList unified_section_list;
639 CreateSections(unified_section_list);
640 }
641 }
642 return m_sections_up.get();
643}
644
647 lldb::SymbolType symbol_type_hint) {
648 if (!name.empty()) {
649 if (name.starts_with("_OBJC_")) {
650 // ObjC
651 if (name.starts_with("_OBJC_CLASS_$_"))
653 if (name.starts_with("_OBJC_METACLASS_$_"))
655 if (name.starts_with("_OBJC_IVAR_$_"))
657 } else if (name.starts_with(".objc_class_name_")) {
658 // ObjC v1
660 }
661 }
662 return symbol_type_hint;
663}
664
667 return llvm::StringSwitch<SectionType>(name)
668 .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
669 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
670 .Case("addr", eSectionTypeDWARFDebugAddr)
671 .Case("aranges", eSectionTypeDWARFDebugAranges)
672 .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
673 .Case("frame", eSectionTypeDWARFDebugFrame)
674 .Case("info", eSectionTypeDWARFDebugInfo)
675 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
676 .Cases({"line", "line.dwo"}, eSectionTypeDWARFDebugLine)
677 .Cases({"line_str", "line_str.dwo"}, eSectionTypeDWARFDebugLineStr)
678 .Case("loc", eSectionTypeDWARFDebugLoc)
679 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
680 .Case("loclists", eSectionTypeDWARFDebugLocLists)
681 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
682 .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
683 .Cases({"macro", "macro.dwo"}, eSectionTypeDWARFDebugMacro)
684 .Case("names", eSectionTypeDWARFDebugNames)
685 .Case("pubnames", eSectionTypeDWARFDebugPubNames)
686 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
687 .Case("ranges", eSectionTypeDWARFDebugRanges)
688 .Case("rnglists", eSectionTypeDWARFDebugRngLists)
689 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
690 .Case("str", eSectionTypeDWARFDebugStr)
691 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
692 .Cases({"str_offsets", "str_offs"}, eSectionTypeDWARFDebugStrOffsets)
693 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
694 .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
695 .Case("types", eSectionTypeDWARFDebugTypes)
696 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
697 .Default(eSectionTypeOther);
698}
699
700std::vector<ObjectFile::LoadableData>
702 std::vector<LoadableData> loadables;
703 SectionList *section_list = GetSectionList();
704 if (!section_list)
705 return loadables;
706 // Create a list of loadable data from loadable sections
707 size_t section_count = section_list->GetNumSections(0);
708 for (size_t i = 0; i < section_count; ++i) {
709 LoadableData loadable;
710 SectionSP section_sp = section_list->GetSectionAtIndex(i);
711 loadable.Dest = target.GetSectionLoadAddress(section_sp);
712 if (loadable.Dest == LLDB_INVALID_ADDRESS)
713 continue;
714 // We can skip sections like bss
715 if (section_sp->GetFileSize() == 0)
716 continue;
717 DataExtractor section_data;
718 section_sp->GetSectionData(section_data);
719 loadable.Contents = section_data.GetData();
720 loadables.push_back(loadable);
721 }
722 return loadables;
723}
724
725std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
726 return {};
727}
728
732
734 uint64_t Offset) {
735 return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
736}
737
738void llvm::format_provider<ObjectFile::Type>::format(
739 const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
740 switch (type) {
742 OS << "invalid";
743 break;
745 OS << "core file";
746 break;
748 OS << "executable";
749 break;
751 OS << "debug info";
752 break;
754 OS << "dynamic linker";
755 break;
757 OS << "object file";
758 break;
760 OS << "shared library";
761 break;
763 OS << "stub library";
764 break;
766 OS << "jit";
767 break;
769 OS << "unknown";
770 break;
771 }
772}
773
774void llvm::format_provider<ObjectFile::Strata>::format(
775 const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
776 switch (strata) {
778 OS << "invalid";
779 break;
781 OS << "unknown";
782 break;
784 OS << "user";
785 break;
787 OS << "kernel";
788 break;
790 OS << "raw image";
791 break;
793 OS << "jit";
794 break;
795 }
796}
797
798Symtab *ObjectFile::GetSymtab(bool can_create) {
799 ModuleSP module_sp(GetModule());
800 if (module_sp && can_create) {
801 // We can't take the module lock in ObjectFile::GetSymtab() or we can
802 // deadlock in DWARF indexing when any file asks for the symbol table from
803 // an object file. This currently happens in the preloading of symbols in
804 // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
805 // module lock, and then threads will be spun up to index the DWARF and
806 // any of those threads might end up trying to relocate items in the DWARF
807 // sections which causes ObjectFile::GetSectionData(...) to relocate section
808 // data which requires the symbol table.
809 //
810 // So to work around this, we create the symbol table one time using
811 // llvm::once_flag, lock it, and then set the unique pointer. Any other
812 // thread that gets ahold of the symbol table before parsing is done, will
813 // not be able to access the symbol table contents since all APIs in Symtab
814 // are protected by a mutex in the Symtab object itself.
815 llvm::call_once(*m_symtab_once_up, [&]() {
816 Symtab *symtab = new Symtab(this);
817 std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
818 m_symtab_up.reset(symtab);
819 if (!m_symtab_up->LoadFromCache()) {
820 ElapsedTime elapsed(module_sp->GetSymtabParseTime());
822 m_symtab_up->Finalize();
823 }
824 });
825 }
826 return m_symtab_up.get();
827}
828
830 if (m_cache_hash)
831 return *m_cache_hash;
832 StreamString strm;
833 strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
834 m_cache_hash = llvm::djbHash(strm.GetString());
835 return *m_cache_hash;
836}
837
838std::string ObjectFile::GetObjectName() const {
839 if (ModuleSP module_sp = GetModule())
840 if (ConstString object_name = module_sp->GetObjectName())
841 return llvm::formatv("{0}({1})", GetFileSpec().GetFilename().GetString(),
842 object_name.GetString())
843 .str();
844 return GetFileSpec().GetFilename().GetString();
845}
846
847namespace llvm {
848namespace json {
849
850bool fromJSON(const llvm::json::Value &value,
851 lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
852 if (auto str = value.getAsString()) {
853 type = llvm::StringSwitch<ObjectFile::Type>(*str)
854 .Case("corefile", ObjectFile::eTypeCoreFile)
855 .Case("executable", ObjectFile::eTypeExecutable)
856 .Case("debuginfo", ObjectFile::eTypeDebugInfo)
857 .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
858 .Case("objectfile", ObjectFile::eTypeObjectFile)
859 .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
860 .Case("stublibrary", ObjectFile::eTypeStubLibrary)
861 .Case("jit", ObjectFile::eTypeJIT)
862 .Case("unknown", ObjectFile::eTypeUnknown)
863 .Default(ObjectFile::eTypeInvalid);
864
865 if (type == ObjectFile::eTypeInvalid) {
866 path.report("invalid object type");
867 return false;
868 }
869
870 return true;
871 }
872 path.report("expected string");
873 return false;
874}
875} // namespace json
876} // 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:32
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.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
virtual uint64_t GetByteSize() const
Get the number of bytes contained in this object.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
virtual 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:250
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:776
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:777
const lldb::addr_t m_memory_addr
Set if the object file only exists in memory.
Definition ObjectFile.h:775
static size_t g_initial_bytes_to_read
The number of bytes to read when going through the plugins.
Definition ObjectFile.h:799
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:769
static lldb::WritableDataBufferSP ReadMemory(const lldb::ProcessSP &process_sp, lldb::addr_t addr, size_t byte_size)
@ 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:764
static lldb::SymbolType GetSymbolTypeFromName(llvm::StringRef name, lldb::SymbolType symbol_type_hint=lldb::eSymbolTypeUndefined)
virtual size_t GetSectionDataSize(Section *section)
Definition ObjectFile.h:682
static size_t GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, ModuleSpecList &specs, lldb::DataExtractorSP=lldb::DataExtractorSP())
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 GetData(lldb::offset_t offset, size_t length, lldb::DataExtractorSP &data_sp) const
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:784
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:783
bool IsInMemory() const
Returns true if the object file exists only in memory.
Definition ObjectFile.h:687
lldb::ProcessWP m_process_wp
Definition ObjectFile.h:773
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:766
virtual size_t ReadSectionData(Section *section, lldb::offset_t section_offset, void *dst, size_t dst_len)
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:542
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:553
lldb::offset_t GetFileOffset() const
Definition Section.h:181
ObjectFile * GetObjectFile()
Definition Section.h:231
lldb::SectionType GetType() const
Definition Section.h:215
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:229
lldb::addr_t GetByteSize() const
Definition Section.h:197
lldb::offset_t GetFileSize() const
Definition Section.h:187
bool IsRelocated() const
Definition Section.h:273
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:1038
std::recursive_mutex & GetMutex()
Definition Symtab.h:51
lldb::addr_t GetSectionLoadAddress(const lldb::SectionSP &section_sp)
Definition Target.cpp:5350
#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::DataExtractorSP &extractor_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