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