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 std::unique_ptr<ObjectContainer> object_container_up(cbs.create_callback(
41 module_sp, data_sp, data_offset, file, file_offset, file_size));
42 if (object_container_up)
43 return object_container_up->GetObjectFile(file);
44 }
45 return {};
46}
47
49 const FileSpec *file,
50 lldb::offset_t file_offset,
51 lldb::offset_t file_size,
52 DataExtractorSP extractor_sp,
53 lldb::offset_t &data_offset) {
55 "ObjectFile::FindPlugin (module = %s, file = %p, file_offset = "
56 "0x%8.8" PRIx64 ", file_size = 0x%8.8" PRIx64 ")",
57 module_sp->GetFileSpec().GetPath().c_str(),
58 static_cast<const void *>(file), static_cast<uint64_t>(file_offset),
59 static_cast<uint64_t>(file_size));
60
61 if (!module_sp)
62 return {};
63
64 if (!file)
65 return {};
66
67 if (!extractor_sp || !extractor_sp->HasData()) {
68 const bool file_exists = FileSystem::Instance().Exists(*file);
69 const bool module_has_name = !module_sp->GetObjectName().empty();
70 // We have an object name which most likely means we have a .o file in
71 // a static archive (.a file). Try and see if we have a cached archive
72 // first without reading any data first
73 if (file_exists && module_has_name) {
75 module_sp, file, file_offset, file_size, DataBufferSP(), data_offset);
76 if (object_file_sp)
77 return object_file_sp;
78 }
79 // Ok, we didn't find any containers that have a named object, now lets
80 // read the first 512 bytes from the file so the object file and object
81 // container plug-ins can use these bytes to see if they can parse this
82 // file.
83 if (file_size > 0) {
84 // Check that we made a data buffer. For instance, a directory node is
85 // not 0 size, but we can't make a data buffer for it.
86 if (DataBufferSP buffer_sp = FileSystem::Instance().CreateDataBuffer(
87 file->GetPath(), g_initial_bytes_to_read, file_offset)) {
88 extractor_sp = std::make_shared<DataExtractor>(buffer_sp);
89 data_offset = 0;
90 }
91 }
92 }
93
94 if (!extractor_sp || !extractor_sp->HasData()) {
95 // Check for archive file with format "/path/to/archive.a(object.o)"
96 llvm::SmallString<256> path_with_object;
97 module_sp->GetFileSpec().GetPath(path_with_object);
98
99 FileSpec archive_file;
100 llvm::StringRef archive_object;
101 const bool must_exist = true;
102 if (ObjectFile::SplitArchivePathWithObject(path_with_object, archive_file,
103 archive_object, must_exist)) {
104 file_size = FileSystem::Instance().GetByteSize(archive_file);
105 if (file_size > 0) {
106 file = &archive_file;
107 module_sp->SetFileSpecAndObjectName(archive_file, archive_object.str());
108 // Check if this is a object container by iterating through all
109 // object container plugin instances and then trying to get an
110 // object file from the container plugins since we had a name.
111 // Also, don't read
112 // ANY data in case there is data cached in the container plug-ins
113 // (like BSD archives caching the contained objects within an
114 // file).
115 ObjectFileSP object_file_sp =
116 CreateObjectFromContainer(module_sp, file, file_offset, file_size,
117 DataBufferSP(), data_offset);
118 if (object_file_sp)
119 return object_file_sp;
120 // We failed to find any cached object files in the container plug-
121 // ins, so lets read the first 512 bytes and try again below...
123 archive_file.GetPath(), g_initial_bytes_to_read, file_offset);
124 extractor_sp = std::make_shared<DataExtractor>(buffer_sp);
125 }
126 }
127 }
128
129 if (extractor_sp && extractor_sp->HasData()) {
130 // Check if this is a normal object file by iterating through all
131 // object file plugin instances.
132 for (auto &cbs : PluginManager::GetObjectFileCallbacks()) {
133 // Make a copy of the extractor in case any plugin modifies it while
134 // processing.
135 DataExtractorSP extractor_copy_sp = extractor_sp->Clone();
136 ObjectFileSP object_file_sp(
137 cbs.create_callback(module_sp, extractor_copy_sp, data_offset, file,
138 file_offset, file_size));
139 if (object_file_sp.get())
140 return object_file_sp;
141 }
142
143 // Check if this is a object container by iterating through all object
144 // container plugin instances and then trying to get an object file
145 // from the container.
146 DataBufferSP buffer_sp = extractor_sp->GetSharedDataBuffer();
148 module_sp, file, file_offset, file_size, buffer_sp, data_offset);
149 if (object_file_sp)
150 return object_file_sp;
151 }
152
153 // We didn't find it, so clear our shared pointer in case it contains
154 // anything and return an empty shared pointer
155 return {};
156}
157
159 const ProcessSP &process_sp,
160 lldb::addr_t header_addr,
161 WritableDataBufferSP data_sp) {
162 ObjectFileSP object_file_sp;
163
164 if (module_sp) {
165 LLDB_SCOPED_TIMERF("ObjectFile::FindPlugin (module = "
166 "%s, process = %p, header_addr = "
167 "0x%" PRIx64 ")",
168 module_sp->GetFileSpec().GetPath().c_str(),
169 static_cast<void *>(process_sp.get()), header_addr);
170
171 // Check if this is a normal object file by iterating through all object
172 // file plugin instances.
173 for (auto &cbs : PluginManager::GetObjectFileCallbacks()) {
174 if (!cbs.create_memory_callback)
175 continue;
176 object_file_sp.reset(cbs.create_memory_callback(module_sp, data_sp,
177 process_sp, header_addr));
178 if (object_file_sp.get())
179 return object_file_sp;
180 }
181 }
182
183 // We didn't find it, so clear our shared pointer in case it contains
184 // anything and return an empty shared pointer
185 object_file_sp.reset();
186 return object_file_sp;
187}
188
190 DataExtractorSP extractor_sp;
191 offset_t data_offset = 0;
192 ModuleSP module_sp = std::make_shared<Module>(file_spec);
193 return static_cast<bool>(ObjectFile::FindPlugin(
194 module_sp, &file_spec, 0, FileSystem::Instance().GetByteSize(file_spec),
195 extractor_sp, data_offset));
196}
197
199 const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size,
200 DataExtractorSP extractor_sp) {
201 if (!extractor_sp)
202 extractor_sp = std::make_shared<DataExtractor>();
203 if (!extractor_sp->HasData()) {
204 if (DataBufferSP file_data_sp = FileSystem::Instance().CreateDataBuffer(
205 file.GetPath(), g_initial_bytes_to_read, file_offset))
206 extractor_sp->SetData(file_data_sp);
207 }
208 if (extractor_sp->HasData()) {
209 if (file_size == 0) {
210 const lldb::offset_t actual_file_size =
212 if (actual_file_size > file_offset)
213 file_size = actual_file_size - file_offset;
214 }
215 return ObjectFile::GetModuleSpecifications(file, extractor_sp, file_offset,
216 file_size);
217 }
218 return {};
219}
220
222 const lldb_private::FileSpec &file, lldb::DataExtractorSP &extractor_sp,
223 lldb::offset_t file_offset, lldb::offset_t file_size) {
224 ModuleSpecList specs;
225
226 // A container can be embedded in an otherwise valid object file. Preserve
227 // the object file's specifications and also query the container plug-ins.
228
229 // Try the ObjectFile plug-ins
230 for (auto &cbs : PluginManager::GetObjectFileCallbacks()) {
231 ModuleSpecList object_specs = cbs.get_module_specifications(
232 file, extractor_sp, file_offset, file_size);
233 if (object_specs.GetSize() > 0) {
234 specs.Append(object_specs);
235 break;
236 }
237 }
238
239 // Try the ObjectContainer plug-ins
241 ModuleSpecList container_specs = cbs.get_module_specifications(
242 file, extractor_sp, file_offset, file_size);
243 if (container_specs.GetSize() > 0)
244 specs.Append(container_specs);
245 }
246 return specs;
247}
248
250 const FileSpec *file_spec_ptr,
251 lldb::offset_t file_offset, lldb::offset_t length,
252 lldb::DataExtractorSP extractor_sp,
253 lldb::offset_t data_offset)
254 : ModuleChild(module_sp),
255 m_file(), // This file could be different from the original module's file
257 m_file_offset(file_offset), m_length(length),
258 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(),
261 m_symtab_once_up(new llvm::once_flag()) {
262 if (file_spec_ptr)
263 m_file = *file_spec_ptr;
264 if (extractor_sp && extractor_sp->HasData()) {
265 m_data_nsp = extractor_sp;
266 // The offset & length fields may be specifying a subset of the
267 // total data buffer.
268 m_data_nsp->SetData(extractor_sp->GetSharedDataBuffer(), data_offset,
269 length);
270 }
271 Log *log = GetLog(LLDBLog::Object);
272 LLDB_LOGF(log,
273 "%p ObjectFile::ObjectFile() module = %p (%s), file = %s, "
274 "file_offset = 0x%8.8" PRIx64 ", size = %" PRIu64,
275 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
276 module_sp->GetSpecificationDescription().c_str(),
277 m_file ? m_file.GetPath().c_str() : "<NULL>", m_file_offset,
278 m_length);
279}
280
282 const ProcessSP &process_sp, lldb::addr_t header_addr,
283 DataExtractorSP header_extractor_sp)
284 : ModuleChild(module_sp), m_file(), m_type(eTypeInvalid),
286 m_data_nsp(std::make_shared<DataExtractor>()), m_process_wp(process_sp),
288 m_sections_up(), m_symtab_up(), m_symtab_once_up(new llvm::once_flag()) {
289 if (header_extractor_sp && header_extractor_sp->HasData())
290 m_data_nsp = header_extractor_sp;
291 Log *log = GetLog(LLDBLog::Object);
292 LLDB_LOGF(log,
293 "%p ObjectFile::ObjectFile() module = %p (%s), process = %p, "
294 "header_addr = 0x%" PRIx64,
295 static_cast<void *>(this), static_cast<void *>(module_sp.get()),
296 module_sp->GetSpecificationDescription().c_str(),
297 static_cast<void *>(process_sp.get()), m_memory_addr);
298}
299
301 Log *log = GetLog(LLDBLog::Object);
302 LLDB_LOGF(log, "%p ObjectFile::~ObjectFile ()\n", static_cast<void *>(this));
303}
304
306 ModuleSP module_sp(GetModule());
307 if (module_sp)
308 return module_sp->SetArchitecture(new_arch);
309 return false;
310}
311
313 Symtab *symtab = GetSymtab();
314 if (symtab) {
315 const Symbol *symbol = symtab->FindSymbolContainingFileAddress(file_addr);
316 if (symbol) {
317 if (symbol->ValueIsAddress()) {
318 const SectionSP section_sp(symbol->GetAddressRef().GetSection());
319 if (section_sp) {
320 const SectionType section_type = section_sp->GetType();
321 switch (section_type) {
324 case eSectionTypeCode:
325 return AddressClass::eCode;
328 case eSectionTypeData:
340 return AddressClass::eData;
376 case eSectionTypeCTF:
395 // In case of absolute sections decide the address class based on
396 // the symbol type because the section type isn't specify if it is
397 // a code or a data section.
398 break;
399 }
400 }
401 }
402
403 const SymbolType symbol_type = symbol->GetType();
404 switch (symbol_type) {
405 case eSymbolTypeAny:
409 case eSymbolTypeCode:
410 return AddressClass::eCode;
412 return AddressClass::eCode;
414 return AddressClass::eCode;
415 case eSymbolTypeData:
416 return AddressClass::eData;
429 case eSymbolTypeBlock:
431 case eSymbolTypeLocal:
432 return AddressClass::eData;
433 case eSymbolTypeParam:
434 return AddressClass::eData;
436 return AddressClass::eData;
463 }
464 }
465 }
467}
468
470 lldb::addr_t addr,
471 size_t byte_size) {
472 WritableDataBufferSP data_sp;
473 if (process_sp) {
474 std::unique_ptr<DataBufferHeap> data_up(new DataBufferHeap(byte_size, 0));
476 const size_t bytes_read = process_sp->ReadMemory(
477 addr, data_up->GetBytes(), data_up->GetByteSize(), error);
478 if (bytes_read == byte_size)
479 data_sp.reset(data_up.release());
480 }
481 return data_sp;
482}
483
484size_t ObjectFile::GetData(lldb::offset_t offset, size_t length,
485 DataExtractorSP &data_sp) const {
486 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
487 // there as the back mmap buffer will be shared with shared pointers.
488 data_sp = m_data_nsp->GetSubsetExtractorSP(offset, length);
489 return data_sp->GetByteSize();
490}
491
492size_t ObjectFile::CopyData(lldb::offset_t offset, size_t length,
493 void *dst) const {
494 // The entire file has already been mmap'ed into m_data_nsp, so just copy from
495 // there Note that the data remains in target byte order.
496 return m_data_nsp->CopyData(offset, length, dst);
497}
498
500 lldb::offset_t section_offset, void *dst,
501 size_t dst_len) {
502 assert(section);
503
504 // If some other objectfile owns this data, pass this to them.
505 if (section->GetObjectFile() != this)
506 return section->GetObjectFile()->ReadSectionData(section, section_offset,
507 dst, dst_len);
508
509 if (!section->IsRelocated())
510 RelocateSection(section);
511
512 if (IsInMemory()) {
513 ProcessSP process_sp(m_process_wp.lock());
514 if (process_sp) {
516 const addr_t base_load_addr =
517 section->GetLoadBaseAddress(&process_sp->GetTarget());
518 if (base_load_addr != LLDB_INVALID_ADDRESS)
519 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
520 dst_len, error);
521 }
522 } else {
523 const lldb::offset_t section_file_size = section->GetFileSize();
524 if (section_offset < section_file_size) {
525 const size_t section_bytes_left = section_file_size - section_offset;
526 size_t section_dst_len = dst_len;
527 if (section_dst_len > section_bytes_left)
528 section_dst_len = section_bytes_left;
529 return CopyData(section->GetFileOffset() + section_offset,
530 section_dst_len, dst);
531 } else {
532 if (section->GetType() == eSectionTypeZeroFill) {
533 const uint64_t section_size = section->GetByteSize();
534 const uint64_t section_bytes_left = section_size - section_offset;
535 uint64_t section_dst_len = dst_len;
536 if (section_dst_len > section_bytes_left)
537 section_dst_len = section_bytes_left;
538 memset(dst, 0, section_dst_len);
539 return section_dst_len;
540 }
541 }
542 }
543 return 0;
544}
545
546// Get the section data the file on disk
548 DataExtractor &section_data) {
549 // If some other objectfile owns this data, pass this to them.
550 if (section->GetObjectFile() != this)
551 return section->GetObjectFile()->ReadSectionData(section, section_data);
552
553 if (!section->IsRelocated())
554 RelocateSection(section);
555
556 if (IsInMemory()) {
557 ProcessSP process_sp(m_process_wp.lock());
558 if (process_sp) {
559 const addr_t base_load_addr =
560 section->GetLoadBaseAddress(&process_sp->GetTarget());
561 if (base_load_addr != LLDB_INVALID_ADDRESS) {
562 DataBufferSP data_sp(
563 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
564 if (data_sp) {
565 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
566 section_data.SetByteOrder(process_sp->GetByteOrder());
567 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
568 return section_data.GetByteSize();
569 }
570 }
571 }
572 }
573
574 // The object file now contains a full mmap'ed copy of the object file
575 // data, so just use this
576 DataExtractorSP extractor_sp;
577 size_t ret_size = GetData(section->GetFileOffset(),
578 GetSectionDataSize(section), extractor_sp);
579 section_data = *extractor_sp;
580 return ret_size;
581}
582
583bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
584 FileSpec &archive_file,
585 llvm::StringRef &archive_object,
586 bool must_exist) {
587 size_t len = path_with_object.size();
588 if (len < 2 || path_with_object.back() != ')')
589 return false;
590 llvm::StringRef archive =
591 path_with_object.substr(0, path_with_object.rfind('('));
592 if (archive.empty())
593 return false;
594 llvm::StringRef object =
595 path_with_object.substr(archive.size() + 1).drop_back();
596 archive_file.SetFile(archive, FileSpec::Style::native);
597 if (must_exist && !FileSystem::Instance().Exists(archive_file))
598 return false;
599 archive_object = object;
600 return true;
601}
602
604 ModuleSP module_sp(GetModule());
605 if (module_sp) {
606 Log *log = GetLog(LLDBLog::Object);
607 LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
608 static_cast<void *>(this),
609 static_cast<void *>(m_symtab_up.get()));
610 // Since we need to clear the symbol table, we need a new llvm::once_flag
611 // instance so we can safely create another symbol table
612 m_symtab_once_up.reset(new llvm::once_flag());
613 m_symtab_up.reset();
614 }
615}
616
617SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
618 std::lock_guard<std::recursive_mutex> guard(m_sections_mutex);
619 if (m_sections_up)
620 return m_sections_up.get();
621 if (update_module_section_list) {
622 if (ModuleSP module_sp = GetModule()) {
623 std::lock_guard<std::recursive_mutex> guard(module_sp->m_sections_mutex);
624 CreateSections(*module_sp->GetUnifiedSectionList());
625 }
626 } else {
627 SectionList unified_section_list;
628 CreateSections(unified_section_list);
629 }
630 return m_sections_up.get();
631}
632
635 lldb::SymbolType symbol_type_hint) {
636 if (!name.empty()) {
637 if (name.starts_with("_OBJC_")) {
638 // ObjC
639 if (name.starts_with("_OBJC_CLASS_$_"))
641 if (name.starts_with("_OBJC_METACLASS_$_"))
643 if (name.starts_with("_OBJC_IVAR_$_"))
645 } else if (name.starts_with(".objc_class_name_")) {
646 // ObjC v1
648 }
649 }
650 return symbol_type_hint;
651}
652
655 return llvm::StringSwitch<SectionType>(name)
656 .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
657 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
658 .Case("addr", eSectionTypeDWARFDebugAddr)
659 .Case("aranges", eSectionTypeDWARFDebugAranges)
660 .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
661 .Case("frame", eSectionTypeDWARFDebugFrame)
662 .Case("info", eSectionTypeDWARFDebugInfo)
663 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
664 .Cases({"line", "line.dwo"}, eSectionTypeDWARFDebugLine)
665 .Cases({"line_str", "line_str.dwo"}, eSectionTypeDWARFDebugLineStr)
666 .Case("loc", eSectionTypeDWARFDebugLoc)
667 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
668 .Case("loclists", eSectionTypeDWARFDebugLocLists)
669 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
670 .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
671 .Cases({"macro", "macro.dwo"}, eSectionTypeDWARFDebugMacro)
672 .Case("names", eSectionTypeDWARFDebugNames)
673 .Case("pubnames", eSectionTypeDWARFDebugPubNames)
674 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
675 .Case("ranges", eSectionTypeDWARFDebugRanges)
676 .Case("rnglists", eSectionTypeDWARFDebugRngLists)
677 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
678 .Case("str", eSectionTypeDWARFDebugStr)
679 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
680 .Cases({"str_offsets", "str_offs"}, eSectionTypeDWARFDebugStrOffsets)
681 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
682 .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
683 .Case("types", eSectionTypeDWARFDebugTypes)
684 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
685 .Default(eSectionTypeOther);
686}
687
688std::vector<ObjectFile::LoadableData>
690 std::vector<LoadableData> loadables;
691 SectionList *section_list = GetSectionList();
692 if (!section_list)
693 return loadables;
694 // Create a list of loadable data from loadable sections
695 size_t section_count = section_list->GetNumSections(0);
696 for (size_t i = 0; i < section_count; ++i) {
697 LoadableData loadable;
698 SectionSP section_sp = section_list->GetSectionAtIndex(i);
699 loadable.Dest = target.GetSectionLoadAddress(section_sp);
700 if (loadable.Dest == LLDB_INVALID_ADDRESS)
701 continue;
702 // We can skip sections like bss
703 if (section_sp->GetFileSize() == 0)
704 continue;
705 DataExtractor section_data;
706 section_sp->GetSectionData(section_data);
707 loadable.Contents = section_data.GetData();
708 loadables.push_back(loadable);
709 }
710 return loadables;
711}
712
713std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
714 return {};
715}
716
720
722 uint64_t Offset) {
723 return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
724}
725
726void llvm::format_provider<ObjectFile::Type>::format(
727 const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
728 switch (type) {
730 OS << "invalid";
731 break;
733 OS << "core file";
734 break;
736 OS << "executable";
737 break;
739 OS << "debug info";
740 break;
742 OS << "dynamic linker";
743 break;
745 OS << "object file";
746 break;
748 OS << "shared library";
749 break;
751 OS << "stub library";
752 break;
754 OS << "jit";
755 break;
757 OS << "unknown";
758 break;
759 }
760}
761
762void llvm::format_provider<ObjectFile::Strata>::format(
763 const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
764 switch (strata) {
766 OS << "invalid";
767 break;
769 OS << "unknown";
770 break;
772 OS << "user";
773 break;
775 OS << "kernel";
776 break;
778 OS << "raw image";
779 break;
781 OS << "jit";
782 break;
783 }
784}
785
786Symtab *ObjectFile::GetSymtab(bool can_create) {
787 ModuleSP module_sp(GetModule());
788 if (module_sp && can_create) {
789 // We can't take the module lock in ObjectFile::GetSymtab() or we can
790 // deadlock in DWARF indexing when any file asks for the symbol table from
791 // an object file. This currently happens in the preloading of symbols in
792 // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
793 // module lock, and then threads will be spun up to index the DWARF and
794 // any of those threads might end up trying to relocate items in the DWARF
795 // sections which causes ObjectFile::GetSectionData(...) to relocate section
796 // data which requires the symbol table.
797 //
798 // So to work around this, we create the symbol table one time using
799 // llvm::once_flag, lock it, and then set the unique pointer. Any other
800 // thread that gets ahold of the symbol table before parsing is done, will
801 // not be able to access the symbol table contents since all APIs in Symtab
802 // are protected by a mutex in the Symtab object itself.
803 llvm::call_once(*m_symtab_once_up, [&]() {
804 Symtab *symtab = new Symtab(this);
805 std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
806 m_symtab_up.reset(symtab);
807 if (!m_symtab_up->LoadFromCache()) {
808 ElapsedTime elapsed(module_sp->GetSymtabParseTime());
810 m_symtab_up->SetWasParsedFromObjectFile();
811 m_symtab_up->Finalize();
812 }
813 });
814 }
815 return m_symtab_up.get();
816}
817
819 if (m_cache_hash)
820 return *m_cache_hash;
821 StreamString strm;
822 strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
823 m_cache_hash = llvm::djbHash(strm.GetString());
824 return *m_cache_hash;
825}
826
827std::string ObjectFile::GetObjectName() const {
828 if (ModuleSP module_sp = GetModule())
829 if (llvm::StringRef object_name = module_sp->GetObjectName();
830 !object_name.empty())
831 return llvm::formatv("{0}({1})", GetFileSpec().GetFilename(), object_name)
832 .str();
833 return GetFileSpec().GetFilename().str();
834}
835
836namespace llvm {
837namespace json {
838
839bool fromJSON(const llvm::json::Value &value,
840 lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
841 if (auto str = value.getAsString()) {
842 type = llvm::StringSwitch<ObjectFile::Type>(*str)
843 .Case("corefile", ObjectFile::eTypeCoreFile)
844 .Case("executable", ObjectFile::eTypeExecutable)
845 .Case("debuginfo", ObjectFile::eTypeDebugInfo)
846 .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
847 .Case("objectfile", ObjectFile::eTypeObjectFile)
848 .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
849 .Case("stublibrary", ObjectFile::eTypeStubLibrary)
850 .Case("jit", ObjectFile::eTypeJIT)
851 .Case("unknown", ObjectFile::eTypeUnknown)
852 .Default(ObjectFile::eTypeInvalid);
853
854 if (type == ObjectFile::eTypeInvalid) {
855 path.report("invalid object type");
856 return false;
857 }
858
859 return true;
860 }
861 path.report("expected string");
862 return false;
863}
864} // namespace json
865} // namespace llvm
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
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:91
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition Address.h:426
An architecture specification class.
Definition ArchSpec.h:32
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:56
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition FileSpec.cpp:174
llvm::StringRef GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:248
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:380
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.
void Append(const ModuleSpec &spec)
Definition ModuleSpec.h:371
static bool SplitArchivePathWithObject(llvm::StringRef path_with_object, lldb_private::FileSpec &archive_file, llvm::StringRef &archive_object, bool must_exist)
Split a path into a file path with object name.
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h:803
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:806
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:828
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
std::recursive_mutex m_sections_mutex
Definition ObjectFile.h:804
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:787
static lldb::SymbolType GetSymbolTypeFromName(llvm::StringRef name, lldb::SymbolType symbol_type_hint=lldb::eSymbolTypeUndefined)
virtual size_t GetSectionDataSize(Section *section)
Definition ObjectFile.h:690
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.
bool m_disable_memory_symtab_caching
Set if the symbol table for an in-memory object file should not be loaded from the index cache.
Definition ObjectFile.h:801
virtual FileSpec & GetFileSpec()
Get accessor to the object file specification.
Definition ObjectFile.h:284
std::string GetObjectName() const
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:813
virtual SectionList * GetSectionList(bool update_module_section_list=true)
Gets the section list for the currently selected architecture (and object for archives).
static ModuleSpecList GetModuleSpecifications(const FileSpec &file, lldb::offset_t file_offset, lldb::offset_t file_size, lldb::DataExtractorSP=lldb::DataExtractorSP())
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:812
bool IsInMemory() const
Returns true if the object file exists only in memory.
Definition ObjectFile.h:695
lldb::ProcessWP m_process_wp
Definition ObjectFile.h:796
uint32_t GetCacheHash()
Get a hash that can be used for caching object file related 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)
virtual lldb::addr_t GetByteSize() const
Definition ObjectFile.h:277
static llvm::SmallVector< ObjectFileCallbacks > GetObjectFileCallbacks()
static llvm::SmallVector< ObjectContainerCallbacks > GetObjectContainerCallbacks()
size_t GetNumSections(uint32_t depth) const
Definition Section.cpp:544
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:555
lldb::offset_t GetFileOffset() const
Definition Section.h:180
ObjectFile * GetObjectFile()
Definition Section.h:230
lldb::SectionType GetType() const
Definition Section.h:214
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:229
lldb::addr_t GetByteSize() const
Definition Section.h:196
lldb::offset_t GetFileSize() const
Definition Section.h:186
bool IsRelocated() const
Definition Section.h:272
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Forwards the arguments to llvm::formatv and writes to the stream.
Definition Stream.h:370
bool ValueIsAddress() const
Definition Symbol.cpp:192
Address & GetAddressRef()
Definition Symbol.h:78
lldb::SymbolType GetType() const
Definition Symbol.h:197
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1014
std::recursive_mutex & GetMutex()
Definition Symtab.h:51
lldb::addr_t GetSectionLoadAddress(const lldb::SectionSP &section_sp)
Definition Target.cpp:6144
#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:338
bool fromJSON(const llvm::json::Value &value, SymbolValue &data, llvm::json::Path path)
uint64_t offset_t
Definition lldb-types.h:86
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
@ eSectionTypeWasmGlobal
@ 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