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