Skip to content

Commit 5931c1e

Browse files
committed
[lldb] Pass likely module names to reflection parsing machinery
rdar://87889973
1 parent 116acf4 commit 5931c1e

File tree

3 files changed

+63
-24
lines changed

3 files changed

+63
-24
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -623,8 +623,25 @@ GetObjectFileFormat(llvm::Triple::ObjectFormatType obj_format_type) {
623623
return obj_file_format;
624624
}
625625

626+
static llvm::SmallVector<llvm::StringRef, 1>
627+
GetLikelySwiftImageNamesForModule(ModuleSP module) {
628+
if (!module || !module->GetFileSpec())
629+
return {};
630+
631+
auto name =
632+
module->GetFileSpec().GetFileNameStrippingExtension().GetStringRef();
633+
if (name == "libswiftCore")
634+
name = "Swift";
635+
if (name.startswith("libswift"))
636+
name = name.drop_front(8);
637+
if (name.startswith("lib"))
638+
name = name.drop_front(3);
639+
return {name};
640+
}
641+
626642
bool SwiftLanguageRuntimeImpl::AddJitObjectFileToReflectionContext(
627-
ObjectFile &obj_file, llvm::Triple::ObjectFormatType obj_format_type) {
643+
ObjectFile &obj_file, llvm::Triple::ObjectFormatType obj_format_type,
644+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) {
628645
assert(obj_file.GetType() == ObjectFile::eTypeJIT &&
629646
"Not a JIT object file!");
630647
auto obj_file_format = GetObjectFileFormat(obj_format_type);
@@ -635,8 +652,7 @@ bool SwiftLanguageRuntimeImpl::AddJitObjectFileToReflectionContext(
635652
return m_reflection_ctx->addImage(
636653
[&](swift::ReflectionSectionKind section_kind)
637654
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
638-
auto section_name =
639-
obj_file_format->getSectionName(section_kind);
655+
auto section_name = obj_file_format->getSectionName(section_kind);
640656
for (auto section : *obj_file.GetSectionList()) {
641657
JITSection *jit_section = llvm::dyn_cast<JITSection>(section.get());
642658
if (jit_section && section->GetName().AsCString() == section_name) {
@@ -658,11 +674,13 @@ bool SwiftLanguageRuntimeImpl::AddJitObjectFileToReflectionContext(
658674
}
659675
}
660676
return {};
661-
});
677+
},
678+
likely_module_names);
662679
}
663680

664681
bool SwiftLanguageRuntimeImpl::AddObjectFileToReflectionContext(
665-
ModuleSP module) {
682+
ModuleSP module,
683+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) {
666684
auto obj_format_type =
667685
module->GetArchitecture().GetTriple().getObjectFormat();
668686

@@ -787,14 +805,16 @@ bool SwiftLanguageRuntimeImpl::AddObjectFileToReflectionContext(
787805
}
788806
return {};
789807
};
808+
790809
return m_reflection_ctx->addImage(
791810
[&](swift::ReflectionSectionKind section_kind)
792811
-> std::pair<swift::remote::RemoteRef<void>, uint64_t> {
793812
auto pair = find_section_with_kind(segment, section_kind);
794813
if (pair.first)
795814
return pair;
796815
return find_section_with_kind(maybe_secondary_segment, section_kind);
797-
});
816+
},
817+
likely_module_names);
798818
}
799819

800820
bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
@@ -813,10 +833,12 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
813833
Address start_address = obj_file->GetBaseAddress();
814834
auto load_ptr = static_cast<uintptr_t>(
815835
start_address.GetLoadAddress(&target));
836+
auto likely_module_names = GetLikelySwiftImageNamesForModule(module_sp);
816837
if (obj_file->GetType() == ObjectFile::eTypeJIT) {
817838
auto object_format_type =
818839
module_sp->GetArchitecture().GetTriple().getObjectFormat();
819-
return AddJitObjectFileToReflectionContext(*obj_file, object_format_type);
840+
return AddJitObjectFileToReflectionContext(*obj_file, object_format_type,
841+
likely_module_names);
820842
}
821843

822844
if (load_ptr == 0 || load_ptr == LLDB_INVALID_ADDRESS) {
@@ -843,13 +865,17 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
843865
llvm::sys::MemoryBlock file_buffer((void *)file_data, size);
844866
m_reflection_ctx->readELF(
845867
swift::remote::RemoteAddress(load_ptr),
846-
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer));
868+
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer),
869+
likely_module_names);
847870
} else if (read_from_file_cache &&
848871
obj_file->GetPluginName().equals("mach-o")) {
849-
if (!AddObjectFileToReflectionContext(module_sp))
850-
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
872+
if (!AddObjectFileToReflectionContext(module_sp, likely_module_names)) {
873+
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
874+
likely_module_names);
875+
}
851876
} else {
852-
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
877+
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr),
878+
likely_module_names);
853879
}
854880
return true;
855881
}

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -247,17 +247,23 @@ class TargetReflectionContext
247247
bool addImage(
248248
llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>(
249249
swift::ReflectionSectionKind)>
250-
find_section) override {
251-
return m_reflection_ctx.addImage(find_section);
250+
find_section,
251+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
252+
return m_reflection_ctx.addImage(find_section, likely_module_names);
252253
}
253254

254-
bool addImage(swift::remote::RemoteAddress image_start) override {
255-
return m_reflection_ctx.addImage(image_start);
255+
bool
256+
addImage(swift::remote::RemoteAddress image_start,
257+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names) override {
258+
return m_reflection_ctx.addImage(image_start, likely_module_names);
256259
}
257260

258-
bool readELF(swift::remote::RemoteAddress ImageStart,
259-
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer) override {
260-
return m_reflection_ctx.readELF(ImageStart, FileBuffer);
261+
bool readELF(
262+
swift::remote::RemoteAddress ImageStart,
263+
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer,
264+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) override {
265+
return m_reflection_ctx.readELF(ImageStart, FileBuffer,
266+
likely_module_names);
261267
}
262268

263269
const swift::reflection::TypeInfo *

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,15 @@ class SwiftLanguageRuntimeImpl {
213213
virtual bool addImage(
214214
llvm::function_ref<std::pair<swift::remote::RemoteRef<void>, uint64_t>(
215215
swift::ReflectionSectionKind)>
216-
find_section) = 0;
217-
virtual bool addImage(swift::remote::RemoteAddress image_start) = 0;
218-
virtual bool readELF(swift::remote::RemoteAddress ImageStart,
219-
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer) = 0;
216+
find_section,
217+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0;
218+
virtual bool addImage(
219+
swift::remote::RemoteAddress image_start,
220+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0;
221+
virtual bool
222+
readELF(swift::remote::RemoteAddress ImageStart,
223+
llvm::Optional<llvm::sys::MemoryBlock> FileBuffer,
224+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names = {}) = 0;
220225
virtual const swift::reflection::TypeInfo *
221226
getTypeInfo(const swift::reflection::TypeRef *type_ref,
222227
swift::remote::TypeInfoProvider *provider) = 0;
@@ -388,13 +393,15 @@ class SwiftLanguageRuntimeImpl {
388393
/// Add the contents of the object file to the reflection context.
389394
/// \return true on success.
390395
bool AddJitObjectFileToReflectionContext(
391-
ObjectFile &obj_file, llvm::Triple::ObjectFormatType obj_format_type);
396+
ObjectFile &obj_file, llvm::Triple::ObjectFormatType obj_format_type,
397+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names);
392398

393399
/// Add the reflections sections to the reflection context by extracting
394400
/// the directly from the object file.
395401
/// \return true on success.
396402
bool AddObjectFileToReflectionContext(
397-
lldb::ModuleSP module);
403+
lldb::ModuleSP module,
404+
llvm::SmallVector<llvm::StringRef, 1> likely_module_names);
398405

399406
/// Cache for the debug-info-originating type infos.
400407
/// \{

0 commit comments

Comments
 (0)