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 section_offset *= section->GetTargetByteSize();
518
519 // If some other objectfile owns this data, pass this to them.
520 if (section->GetObjectFile() != this)
521 return section->GetObjectFile()->ReadSectionData(section, section_offset,
522 dst, dst_len);
523
524 if (!section->IsRelocated())
525 RelocateSection(section);
526
527 if (IsInMemory()) {
528 ProcessSP process_sp(m_process_wp.lock());
529 if (process_sp) {
531 const addr_t base_load_addr =
532 section->GetLoadBaseAddress(&process_sp->GetTarget());
533 if (base_load_addr != LLDB_INVALID_ADDRESS)
534 return process_sp->ReadMemory(base_load_addr + section_offset, dst,
535 dst_len, error);
536 }
537 } else {
538 const lldb::offset_t section_file_size = section->GetFileSize();
539 if (section_offset < section_file_size) {
540 const size_t section_bytes_left = section_file_size - section_offset;
541 size_t section_dst_len = dst_len;
542 if (section_dst_len > section_bytes_left)
543 section_dst_len = section_bytes_left;
544 return CopyData(section->GetFileOffset() + section_offset,
545 section_dst_len, dst);
546 } else {
547 if (section->GetType() == eSectionTypeZeroFill) {
548 const uint64_t section_size = section->GetByteSize();
549 const uint64_t section_bytes_left = section_size - section_offset;
550 uint64_t section_dst_len = dst_len;
551 if (section_dst_len > section_bytes_left)
552 section_dst_len = section_bytes_left;
553 memset(dst, 0, section_dst_len);
554 return section_dst_len;
555 }
556 }
557 }
558 return 0;
559}
560
561// Get the section data the file on disk
563 DataExtractor &section_data) {
564 // If some other objectfile owns this data, pass this to them.
565 if (section->GetObjectFile() != this)
566 return section->GetObjectFile()->ReadSectionData(section, section_data);
567
568 if (!section->IsRelocated())
569 RelocateSection(section);
570
571 if (IsInMemory()) {
572 ProcessSP process_sp(m_process_wp.lock());
573 if (process_sp) {
574 const addr_t base_load_addr =
575 section->GetLoadBaseAddress(&process_sp->GetTarget());
576 if (base_load_addr != LLDB_INVALID_ADDRESS) {
577 DataBufferSP data_sp(
578 ReadMemory(process_sp, base_load_addr, section->GetByteSize()));
579 if (data_sp) {
580 section_data.SetData(data_sp, 0, data_sp->GetByteSize());
581 section_data.SetByteOrder(process_sp->GetByteOrder());
582 section_data.SetAddressByteSize(process_sp->GetAddressByteSize());
583 return section_data.GetByteSize();
584 }
585 }
586 }
587 }
588
589 // The object file now contains a full mmap'ed copy of the object file
590 // data, so just use this
591 DataExtractorSP extractor_sp;
592 size_t ret_size = GetData(section->GetFileOffset(),
593 GetSectionDataSize(section), extractor_sp);
594 section_data = *extractor_sp;
595 return ret_size;
596}
597
598bool ObjectFile::SplitArchivePathWithObject(llvm::StringRef path_with_object,
599 FileSpec &archive_file,
600 ConstString &archive_object,
601 bool must_exist) {
602 size_t len = path_with_object.size();
603 if (len < 2 || path_with_object.back() != ')')
604 return false;
605 llvm::StringRef archive = path_with_object.substr(0, path_with_object.rfind('('));
606 if (archive.empty())
607 return false;
608 llvm::StringRef object = path_with_object.substr(archive.size() + 1).drop_back();
609 archive_file.SetFile(archive, FileSpec::Style::native);
610 if (must_exist && !FileSystem::Instance().Exists(archive_file))
611 return false;
612 archive_object.SetString(object);
613 return true;
614}
615
617 ModuleSP module_sp(GetModule());
618 if (module_sp) {
619 Log *log = GetLog(LLDBLog::Object);
620 LLDB_LOGF(log, "%p ObjectFile::ClearSymtab () symtab = %p",
621 static_cast<void *>(this),
622 static_cast<void *>(m_symtab_up.get()));
623 // Since we need to clear the symbol table, we need a new llvm::once_flag
624 // instance so we can safely create another symbol table
625 m_symtab_once_up.reset(new llvm::once_flag());
626 m_symtab_up.reset();
627 }
628}
629
630SectionList *ObjectFile::GetSectionList(bool update_module_section_list) {
631 if (m_sections_up == nullptr) {
632 if (update_module_section_list) {
633 ModuleSP module_sp(GetModule());
634 if (module_sp) {
635 std::lock_guard<std::recursive_mutex> guard(module_sp->GetMutex());
636 CreateSections(*module_sp->GetUnifiedSectionList());
637 }
638 } else {
639 SectionList unified_section_list;
640 CreateSections(unified_section_list);
641 }
642 }
643 return m_sections_up.get();
644}
645
648 lldb::SymbolType symbol_type_hint) {
649 if (!name.empty()) {
650 if (name.starts_with("_OBJC_")) {
651 // ObjC
652 if (name.starts_with("_OBJC_CLASS_$_"))
654 if (name.starts_with("_OBJC_METACLASS_$_"))
656 if (name.starts_with("_OBJC_IVAR_$_"))
658 } else if (name.starts_with(".objc_class_name_")) {
659 // ObjC v1
661 }
662 }
663 return symbol_type_hint;
664}
665
668 return llvm::StringSwitch<SectionType>(name)
669 .Case("abbrev", eSectionTypeDWARFDebugAbbrev)
670 .Case("abbrev.dwo", eSectionTypeDWARFDebugAbbrevDwo)
671 .Case("addr", eSectionTypeDWARFDebugAddr)
672 .Case("aranges", eSectionTypeDWARFDebugAranges)
673 .Case("cu_index", eSectionTypeDWARFDebugCuIndex)
674 .Case("frame", eSectionTypeDWARFDebugFrame)
675 .Case("info", eSectionTypeDWARFDebugInfo)
676 .Case("info.dwo", eSectionTypeDWARFDebugInfoDwo)
677 .Cases({"line", "line.dwo"}, eSectionTypeDWARFDebugLine)
678 .Cases({"line_str", "line_str.dwo"}, eSectionTypeDWARFDebugLineStr)
679 .Case("loc", eSectionTypeDWARFDebugLoc)
680 .Case("loc.dwo", eSectionTypeDWARFDebugLocDwo)
681 .Case("loclists", eSectionTypeDWARFDebugLocLists)
682 .Case("loclists.dwo", eSectionTypeDWARFDebugLocListsDwo)
683 .Case("macinfo", eSectionTypeDWARFDebugMacInfo)
684 .Cases({"macro", "macro.dwo"}, eSectionTypeDWARFDebugMacro)
685 .Case("names", eSectionTypeDWARFDebugNames)
686 .Case("pubnames", eSectionTypeDWARFDebugPubNames)
687 .Case("pubtypes", eSectionTypeDWARFDebugPubTypes)
688 .Case("ranges", eSectionTypeDWARFDebugRanges)
689 .Case("rnglists", eSectionTypeDWARFDebugRngLists)
690 .Case("rnglists.dwo", eSectionTypeDWARFDebugRngListsDwo)
691 .Case("str", eSectionTypeDWARFDebugStr)
692 .Case("str.dwo", eSectionTypeDWARFDebugStrDwo)
693 .Cases({"str_offsets", "str_offs"}, eSectionTypeDWARFDebugStrOffsets)
694 .Case("str_offsets.dwo", eSectionTypeDWARFDebugStrOffsetsDwo)
695 .Case("tu_index", eSectionTypeDWARFDebugTuIndex)
696 .Case("types", eSectionTypeDWARFDebugTypes)
697 .Case("types.dwo", eSectionTypeDWARFDebugTypesDwo)
698 .Default(eSectionTypeOther);
699}
700
701std::vector<ObjectFile::LoadableData>
703 std::vector<LoadableData> loadables;
704 SectionList *section_list = GetSectionList();
705 if (!section_list)
706 return loadables;
707 // Create a list of loadable data from loadable sections
708 size_t section_count = section_list->GetNumSections(0);
709 for (size_t i = 0; i < section_count; ++i) {
710 LoadableData loadable;
711 SectionSP section_sp = section_list->GetSectionAtIndex(i);
712 loadable.Dest = target.GetSectionLoadAddress(section_sp);
713 if (loadable.Dest == LLDB_INVALID_ADDRESS)
714 continue;
715 // We can skip sections like bss
716 if (section_sp->GetFileSize() == 0)
717 continue;
718 DataExtractor section_data;
719 section_sp->GetSectionData(section_data);
720 loadable.Contents = section_data.GetData();
721 loadables.push_back(loadable);
722 }
723 return loadables;
724}
725
726std::unique_ptr<CallFrameInfo> ObjectFile::CreateCallFrameInfo() {
727 return {};
728}
729
733
735 uint64_t Offset) {
736 return FileSystem::Instance().CreateDataBuffer(file.GetPath(), Size, Offset);
737}
738
739void llvm::format_provider<ObjectFile::Type>::format(
740 const ObjectFile::Type &type, raw_ostream &OS, StringRef Style) {
741 switch (type) {
743 OS << "invalid";
744 break;
746 OS << "core file";
747 break;
749 OS << "executable";
750 break;
752 OS << "debug info";
753 break;
755 OS << "dynamic linker";
756 break;
758 OS << "object file";
759 break;
761 OS << "shared library";
762 break;
764 OS << "stub library";
765 break;
767 OS << "jit";
768 break;
770 OS << "unknown";
771 break;
772 }
773}
774
775void llvm::format_provider<ObjectFile::Strata>::format(
776 const ObjectFile::Strata &strata, raw_ostream &OS, StringRef Style) {
777 switch (strata) {
779 OS << "invalid";
780 break;
782 OS << "unknown";
783 break;
785 OS << "user";
786 break;
788 OS << "kernel";
789 break;
791 OS << "raw image";
792 break;
794 OS << "jit";
795 break;
796 }
797}
798
799Symtab *ObjectFile::GetSymtab(bool can_create) {
800 ModuleSP module_sp(GetModule());
801 if (module_sp && can_create) {
802 // We can't take the module lock in ObjectFile::GetSymtab() or we can
803 // deadlock in DWARF indexing when any file asks for the symbol table from
804 // an object file. This currently happens in the preloading of symbols in
805 // SymbolFileDWARF::PreloadSymbols() because the main thread will take the
806 // module lock, and then threads will be spun up to index the DWARF and
807 // any of those threads might end up trying to relocate items in the DWARF
808 // sections which causes ObjectFile::GetSectionData(...) to relocate section
809 // data which requires the symbol table.
810 //
811 // So to work around this, we create the symbol table one time using
812 // llvm::once_flag, lock it, and then set the unique pointer. Any other
813 // thread that gets ahold of the symbol table before parsing is done, will
814 // not be able to access the symbol table contents since all APIs in Symtab
815 // are protected by a mutex in the Symtab object itself.
816 llvm::call_once(*m_symtab_once_up, [&]() {
817 Symtab *symtab = new Symtab(this);
818 std::lock_guard<std::recursive_mutex> symtab_guard(symtab->GetMutex());
819 m_symtab_up.reset(symtab);
820 if (!m_symtab_up->LoadFromCache()) {
821 ElapsedTime elapsed(module_sp->GetSymtabParseTime());
823 m_symtab_up->Finalize();
824 }
825 });
826 }
827 return m_symtab_up.get();
828}
829
831 if (m_cache_hash)
832 return *m_cache_hash;
833 StreamString strm;
834 strm.Format("{0}-{1}-{2}", m_file, GetType(), GetStrata());
835 m_cache_hash = llvm::djbHash(strm.GetString());
836 return *m_cache_hash;
837}
838
839std::string ObjectFile::GetObjectName() const {
840 if (ModuleSP module_sp = GetModule())
841 if (ConstString object_name = module_sp->GetObjectName())
842 return llvm::formatv("{0}({1})", GetFileSpec().GetFilename().GetString(),
843 object_name.GetString())
844 .str();
845 return GetFileSpec().GetFilename().GetString();
846}
847
848namespace llvm {
849namespace json {
850
851bool fromJSON(const llvm::json::Value &value,
852 lldb_private::ObjectFile::Type &type, llvm::json::Path path) {
853 if (auto str = value.getAsString()) {
854 type = llvm::StringSwitch<ObjectFile::Type>(*str)
855 .Case("corefile", ObjectFile::eTypeCoreFile)
856 .Case("executable", ObjectFile::eTypeExecutable)
857 .Case("debuginfo", ObjectFile::eTypeDebugInfo)
858 .Case("dynamiclinker", ObjectFile::eTypeDynamicLinker)
859 .Case("objectfile", ObjectFile::eTypeObjectFile)
860 .Case("sharedlibrary", ObjectFile::eTypeSharedLibrary)
861 .Case("stublibrary", ObjectFile::eTypeStubLibrary)
862 .Case("jit", ObjectFile::eTypeJIT)
863 .Case("unknown", ObjectFile::eTypeUnknown)
864 .Default(ObjectFile::eTypeInvalid);
865
866 if (type == ObjectFile::eTypeInvalid) {
867 path.report("invalid object type");
868 return false;
869 }
870
871 return true;
872 }
873 path.report("expected string");
874 return false;
875}
876} // namespace json
877} // namespace llvm
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:376
static ObjectFileSP CreateObjectFromContainer(const lldb::ModuleSP &module_sp, const FileSpec *file, lldb::offset_t file_offset, lldb::offset_t file_size, DataBufferSP data_sp, lldb::offset_t &data_offset)
static double elapsed(const StatsTimepoint &start, const StatsTimepoint &end)
#define LLDB_SCOPED_TIMERF(...)
Definition Timer.h:86
lldb::SectionSP GetSection() const
Get const accessor for the section.
Definition Address.h:432
An architecture specification class.
Definition ArchSpec.h:31
A uniqued constant string class.
Definition ConstString.h:40
std::string GetString() const
Get the string value as a std::string.
void SetString(llvm::StringRef s)
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
virtual const void * GetData(lldb::offset_t *offset_ptr, lldb::offset_t length) const
Extract length bytes from *offset_ptr.
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint64_t GetByteSize() const
Get the number of bytes contained in this object.
lldb::offset_t SetData(const void *bytes, lldb::offset_t length, lldb::ByteOrder byte_order)
Set data with a buffer that is caller owned.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A class that measures elapsed time in an exception safe way.
Definition Statistics.h:76
A file utility class.
Definition FileSpec.h:57
void SetFile(llvm::StringRef path, Style style)
Change the file specified with a new path.
Definition FileSpec.cpp:174
const ConstString & GetFilename() const
Filename string const get accessor.
Definition FileSpec.h:251
size_t GetPath(char *path, size_t max_path_length, bool denormalize=true) const
Extract the full path to the file.
Definition FileSpec.cpp:374
uint64_t GetByteSize(const FileSpec &file_spec) const
Returns the on-disk size of the given file in bytes.
bool Exists(const FileSpec &file_spec) const
Returns whether the given file exists.
static FileSystem & Instance()
std::shared_ptr< DataBuffer > CreateDataBuffer(const llvm::Twine &path, uint64_t size=0, uint64_t offset=0)
Create memory buffer from path.
ModuleChild(const lldb::ModuleSP &module_sp)
Construct with owning module.
lldb::ModuleSP GetModule() const
Get const accessor for the module pointer.
std::unique_ptr< lldb_private::SectionList > m_sections_up
Definition ObjectFile.h: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:546
lldb::SectionSP GetSectionAtIndex(size_t idx) const
Definition Section.cpp:557
uint32_t GetTargetByteSize() const
Definition Section.h:276
lldb::offset_t GetFileOffset() const
Definition Section.h:183
ObjectFile * GetObjectFile()
Definition Section.h:233
lldb::SectionType GetType() const
Definition Section.h:217
lldb::addr_t GetLoadBaseAddress(Target *target) const
Definition Section.cpp:233
lldb::addr_t GetByteSize() const
Definition Section.h:199
lldb::offset_t GetFileSize() const
Definition Section.h:189
bool IsRelocated() const
Definition Section.h:278
An error handling class.
Definition Status.h:118
llvm::StringRef GetString() const
void Format(const char *format, Args &&... args)
Definition Stream.h:364
bool ValueIsAddress() const
Definition Symbol.cpp:165
Address & GetAddressRef()
Definition Symbol.h:73
lldb::SymbolType GetType() const
Definition Symbol.h:169
Symbol * FindSymbolContainingFileAddress(lldb::addr_t file_addr)
Definition Symtab.cpp:1038
std::recursive_mutex & GetMutex()
Definition Symtab.h:51
lldb::addr_t GetSectionLoadAddress(const lldb::SectionSP &section_sp)
Definition Target.cpp:5335
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:332
size_t(* ObjectFileGetModuleSpecifications)(const FileSpec &file, lldb::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