Skip to content

Use new libReflection API when reading ELF files #1669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lldb/include/lldb/Symbol/ObjectFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
#include "lldb/lldb-private.h"
#include "llvm/Support/VersionTuple.h"

namespace swift {
class SwiftObjectFileFormat;
enum ReflectionSectionKind : uint8_t;
}
namespace lldb_private {

class ObjectFileJITDelegate {
Expand Down Expand Up @@ -650,6 +654,9 @@ class ObjectFile : public std::enable_shared_from_this<ObjectFile>,
/// Creates a plugin-specific call frame info
virtual std::unique_ptr<CallFrameInfo> CreateCallFrameInfo();

virtual llvm::StringRef
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);

protected:
// Member variables.
FileSpec m_file;
Expand Down
7 changes: 7 additions & 0 deletions lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/MipsABIFlags.h"
#include "swift/ABI/ObjectFile.h"

#define CASE_AND_STREAM(s, def, width) \
case def: \
Expand Down Expand Up @@ -3403,3 +3404,9 @@ ObjectFileELF::GetLoadableData(Target &target) {
}
return loadables;
}

llvm::StringRef ObjectFileELF::GetReflectionSectionIdentifier(
swift::ReflectionSectionKind section) {
swift::SwiftObjectFileFormatELF file_format_elf;
return file_format_elf.getSectionName(section);
}
3 changes: 3 additions & 0 deletions lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,9 @@ class ObjectFileELF : public lldb_private::ObjectFile {
/// .gnu_debugdata section or \c nullptr if an error occured or if there's no
/// section with that name.
std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();

llvm::StringRef
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
};

#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H
7 changes: 7 additions & 0 deletions lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#include "llvm/Support/MemoryBuffer.h"

#include "ObjectFileMachO.h"
#include "swift/ABI/ObjectFile.h"

#if defined(__APPLE__)
#include <TargetConditionals.h>
Expand Down Expand Up @@ -6524,3 +6525,9 @@ bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp,
}
return false;
}

llvm::StringRef ObjectFileMachO::GetReflectionSectionIdentifier(
swift::ReflectionSectionKind section) {
swift::SwiftObjectFileFormatMachO file_format_mach_o;
return file_format_mach_o.getSectionName(section);
}
3 changes: 3 additions & 0 deletions lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ class ObjectFileMachO : public lldb_private::ObjectFile {

bool SectionIsLoadable(const lldb_private::Section *section);

llvm::StringRef
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);

llvm::MachO::mach_header m_header;
static lldb_private::ConstString GetSegmentNameTEXT();
static lldb_private::ConstString GetSegmentNameDATA();
Expand Down
8 changes: 8 additions & 0 deletions lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"

#include "swift/ABI/ObjectFile.h"

#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
#define OPT_HEADER_MAGIC_PE32 0x010b
Expand Down Expand Up @@ -1252,3 +1254,9 @@ ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }

uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }

llvm::StringRef ObjectFilePECOFF::GetReflectionSectionIdentifier(
swift::ReflectionSectionKind section) {
swift::SwiftObjectFileFormatCOFF file_format_coff;
return file_format_coff.getSectionName(section);
}
3 changes: 3 additions & 0 deletions lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@ class ObjectFilePECOFF : public lldb_private::ObjectFile {
static lldb::SectionType GetSectionType(llvm::StringRef sect_name,
const section_header_t &sect);

llvm::StringRef
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);

typedef std::vector<section_header_t> SectionHeaderColl;
typedef SectionHeaderColl::iterator SectionHeaderCollIter;
typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;
Expand Down
6 changes: 6 additions & 0 deletions lldb/source/Symbol/ObjectFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,3 +752,9 @@ void llvm::format_provider<ObjectFile::Strata>::format(
break;
}
}

llvm::StringRef ObjectFile::GetReflectionSectionIdentifier(
swift::ReflectionSectionKind section) {
assert("Base class's GetReflectionSectionIdentifier should not be called");
return "";
}
44 changes: 34 additions & 10 deletions lldb/source/Target/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Memory.h"

// FIXME: we should not need this
#include "Plugins/Language/Swift/SwiftFormatters.h"
Expand Down Expand Up @@ -318,13 +319,26 @@ static bool HasReflectionInfo(ObjectFile *obj_file) {
return false;
};

StringRef field_md = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::fieldmd);
StringRef assocty = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::assocty);
StringRef builtin = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::builtin);
StringRef capture = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::capture);
StringRef typeref = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::typeref);
StringRef reflstr = obj_file->GetReflectionSectionIdentifier(
swift::ReflectionSectionKind::reflstr);

bool hasReflectionSection = false;
hasReflectionSection |= findSectionInObject("__swift5_fieldmd");
hasReflectionSection |= findSectionInObject("__swift5_assocty");
hasReflectionSection |= findSectionInObject("__swift5_builtin");
hasReflectionSection |= findSectionInObject("__swift5_capture");
hasReflectionSection |= findSectionInObject("__swift5_typeref");
hasReflectionSection |= findSectionInObject("__swift5_reflstr");
hasReflectionSection |= findSectionInObject(field_md);
hasReflectionSection |= findSectionInObject(assocty);
hasReflectionSection |= findSectionInObject(builtin);
hasReflectionSection |= findSectionInObject(capture);
hasReflectionSection |= findSectionInObject(typeref);
hasReflectionSection |= findSectionInObject(reflstr);
return hasReflectionSection;
}

Expand Down Expand Up @@ -461,8 +475,6 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
auto *obj_file = module_sp->GetObjectFile();
if (!obj_file)
return false;
if (obj_file->GetPluginName().GetStringRef().equals("elf"))
return false;
Address start_address = obj_file->GetBaseAddress();
auto load_ptr = static_cast<uintptr_t>(
start_address.GetLoadAddress(&(m_process.GetTarget())));
Expand All @@ -473,8 +485,20 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
obj_file->GetFileSpec().GetFilename().GetCString());
return false;
}
if (HasReflectionInfo(obj_file))
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
if (HasReflectionInfo(obj_file)) {
// When dealing with ELF, we need to pass in the contents of the on-disk
// file, since the Section Header Table is not present in the child process
if (obj_file->GetPluginName().GetStringRef().equals("elf")) {
DataExtractor extractor;
auto size = obj_file->GetData(0, obj_file->GetByteSize(), extractor);
const uint8_t *file_data = extractor.GetDataStart();
llvm::sys::MemoryBlock file_buffer((void *)file_data, size);
m_reflection_ctx->readELF(swift::remote::RemoteAddress(load_ptr),
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer));
} else {
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
}
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def launch_info(self):
return info

@swiftTest
@expectedFailureOS(no_match(["macosx"])) # Requires Remote Mirrors support
@expectedFailureAll(oslist=["windows"]) # Requires Remote Mirrors support
def test_implementation_only_import_library(self):
"""Test `@_implementationOnly import` in a resilient library used by the main executable

Expand All @@ -64,7 +64,7 @@ def cleanup():
self.expect("e container.wrapped", substrs=["(SomeLibraryCore.TwoInts)", "(first = 2, second = 3)"])

@swiftTest
@expectedFailureOS(no_match(["macosx"])) # Requires Remote Mirrors support
@expectedFailureAll(oslist=["windows"]) # Requires Remote Mirrors support
def test_implementation_only_import_library_no_library_module(self):
"""Test `@_implementationOnly import` in a resilient library used by the main executable, after removing the implementation-only library's swiftmodule

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def cleanup():
self.runCmd("settings set symbols.use-swift-dwarfimporter true")

@swiftTest
@expectedFailureOS(no_match(["macosx"])) # Requires Remote Mirrors support
@expectedFailureAll(oslist=["windows"])
def test_implementation_only_import_main_executable_resilient(self):
"""Test `@_implementationOnly import` in the main executable with a resilient library

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ class TestSwiftInterfaceNoDebugInfo(TestBase):
mydir = TestBase.compute_mydir(__file__)

@swiftTest
@expectedFailureAll(oslist=["linux"],bugnumber="rdar://49662256")
def test_swift_interface(self):
"""Test that we load and handle modules that only have textual .swiftinterface files"""
self.build()
self.do_test()

@swiftTest
@expectedFailureAll(oslist=["linux"],bugnumber="rdar://49662256")
def test_swift_interface_fallback(self):
"""Test that we fall back to load from the .swiftinterface file if the .swiftmodule is invalid"""
self.build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ class TestSwiftInterfaceStaticNoDebugInfo(TestBase):
mydir = TestBase.compute_mydir(__file__)

@swiftTest
@expectedFailureAll(oslist=["linux"],bugnumber="rdar://49662256")
def test_swift_interface(self):
"""Test that we load and handle modules that only have textual .swiftinterface files"""
self.build()
self.do_test()

@swiftTest
@expectedFailureAll(oslist=["linux"],bugnumber="rdar://49662256")
def test_swift_interface_fallback(self):
"""Test that we fall back to load from the .swiftinterface file if the .swiftmodule is invalid"""
self.build()
Expand Down