Skip to content

Commit 1aa0849

Browse files
Merge pull request #1669 from augusto2112/read-elf-sections-rebranch
Use new libReflection API when reading ELF files
2 parents f740924 + 1849882 commit 1aa0849

File tree

13 files changed

+81
-17
lines changed

13 files changed

+81
-17
lines changed

lldb/include/lldb/Symbol/ObjectFile.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
#include "lldb/lldb-private.h"
2222
#include "llvm/Support/VersionTuple.h"
2323

24+
namespace swift {
25+
class SwiftObjectFileFormat;
26+
enum ReflectionSectionKind : uint8_t;
27+
}
2428
namespace lldb_private {
2529

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

657+
virtual llvm::StringRef
658+
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
659+
653660
protected:
654661
// Member variables.
655662
FileSpec m_file;

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Support/MathExtras.h"
4141
#include "llvm/Support/MemoryBuffer.h"
4242
#include "llvm/Support/MipsABIFlags.h"
43+
#include "swift/ABI/ObjectFile.h"
4344

4445
#define CASE_AND_STREAM(s, def, width) \
4546
case def: \
@@ -3403,3 +3404,9 @@ ObjectFileELF::GetLoadableData(Target &target) {
34033404
}
34043405
return loadables;
34053406
}
3407+
3408+
llvm::StringRef ObjectFileELF::GetReflectionSectionIdentifier(
3409+
swift::ReflectionSectionKind section) {
3410+
swift::SwiftObjectFileFormatELF file_format_elf;
3411+
return file_format_elf.getSectionName(section);
3412+
}

lldb/source/Plugins/ObjectFile/ELF/ObjectFileELF.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,9 @@ class ObjectFileELF : public lldb_private::ObjectFile {
392392
/// .gnu_debugdata section or \c nullptr if an error occured or if there's no
393393
/// section with that name.
394394
std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
395+
396+
llvm::StringRef
397+
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
395398
};
396399

397400
#endif // LLDB_SOURCE_PLUGINS_OBJECTFILE_ELF_OBJECTFILEELF_H

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
#include "llvm/Support/MemoryBuffer.h"
4848

4949
#include "ObjectFileMachO.h"
50+
#include "swift/ABI/ObjectFile.h"
5051

5152
#if defined(__APPLE__)
5253
#include <TargetConditionals.h>
@@ -6524,3 +6525,9 @@ bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp,
65246525
}
65256526
return false;
65266527
}
6528+
6529+
llvm::StringRef ObjectFileMachO::GetReflectionSectionIdentifier(
6530+
swift::ReflectionSectionKind section) {
6531+
swift::SwiftObjectFileFormatMachO file_format_mach_o;
6532+
return file_format_mach_o.getSectionName(section);
6533+
}

lldb/source/Plugins/ObjectFile/Mach-O/ObjectFileMachO.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ class ObjectFileMachO : public lldb_private::ObjectFile {
207207

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

210+
llvm::StringRef
211+
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
212+
210213
llvm::MachO::mach_header m_header;
211214
static lldb_private::ConstString GetSegmentNameTEXT();
212215
static lldb_private::ConstString GetSegmentNameDATA();

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "llvm/Support/Error.h"
3434
#include "llvm/Support/MemoryBuffer.h"
3535

36+
#include "swift/ABI/ObjectFile.h"
37+
3638
#define IMAGE_DOS_SIGNATURE 0x5A4D // MZ
3739
#define IMAGE_NT_SIGNATURE 0x00004550 // PE00
3840
#define OPT_HEADER_MAGIC_PE32 0x010b
@@ -1252,3 +1254,9 @@ ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
12521254
ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
12531255

12541256
uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1257+
1258+
llvm::StringRef ObjectFilePECOFF::GetReflectionSectionIdentifier(
1259+
swift::ReflectionSectionKind section) {
1260+
swift::SwiftObjectFileFormatCOFF file_format_coff;
1261+
return file_format_coff.getSectionName(section);
1262+
}

lldb/source/Plugins/ObjectFile/PECOFF/ObjectFilePECOFF.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,9 @@ class ObjectFilePECOFF : public lldb_private::ObjectFile {
286286
static lldb::SectionType GetSectionType(llvm::StringRef sect_name,
287287
const section_header_t &sect);
288288

289+
llvm::StringRef
290+
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
291+
289292
typedef std::vector<section_header_t> SectionHeaderColl;
290293
typedef SectionHeaderColl::iterator SectionHeaderCollIter;
291294
typedef SectionHeaderColl::const_iterator SectionHeaderCollConstIter;

lldb/source/Symbol/ObjectFile.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -752,3 +752,9 @@ void llvm::format_provider<ObjectFile::Strata>::format(
752752
break;
753753
}
754754
}
755+
756+
llvm::StringRef ObjectFile::GetReflectionSectionIdentifier(
757+
swift::ReflectionSectionKind section) {
758+
assert("Base class's GetReflectionSectionIdentifier should not be called");
759+
return "";
760+
}

lldb/source/Target/SwiftLanguageRuntime.cpp

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242

4343
#include "llvm/ADT/StringRef.h"
4444
#include "llvm/Support/raw_ostream.h"
45+
#include "llvm/Support/Memory.h"
4546

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

322+
StringRef field_md = obj_file->GetReflectionSectionIdentifier(
323+
swift::ReflectionSectionKind::fieldmd);
324+
StringRef assocty = obj_file->GetReflectionSectionIdentifier(
325+
swift::ReflectionSectionKind::assocty);
326+
StringRef builtin = obj_file->GetReflectionSectionIdentifier(
327+
swift::ReflectionSectionKind::builtin);
328+
StringRef capture = obj_file->GetReflectionSectionIdentifier(
329+
swift::ReflectionSectionKind::capture);
330+
StringRef typeref = obj_file->GetReflectionSectionIdentifier(
331+
swift::ReflectionSectionKind::typeref);
332+
StringRef reflstr = obj_file->GetReflectionSectionIdentifier(
333+
swift::ReflectionSectionKind::reflstr);
334+
321335
bool hasReflectionSection = false;
322-
hasReflectionSection |= findSectionInObject("__swift5_fieldmd");
323-
hasReflectionSection |= findSectionInObject("__swift5_assocty");
324-
hasReflectionSection |= findSectionInObject("__swift5_builtin");
325-
hasReflectionSection |= findSectionInObject("__swift5_capture");
326-
hasReflectionSection |= findSectionInObject("__swift5_typeref");
327-
hasReflectionSection |= findSectionInObject("__swift5_reflstr");
336+
hasReflectionSection |= findSectionInObject(field_md);
337+
hasReflectionSection |= findSectionInObject(assocty);
338+
hasReflectionSection |= findSectionInObject(builtin);
339+
hasReflectionSection |= findSectionInObject(capture);
340+
hasReflectionSection |= findSectionInObject(typeref);
341+
hasReflectionSection |= findSectionInObject(reflstr);
328342
return hasReflectionSection;
329343
}
330344

@@ -461,8 +475,6 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
461475
auto *obj_file = module_sp->GetObjectFile();
462476
if (!obj_file)
463477
return false;
464-
if (obj_file->GetPluginName().GetStringRef().equals("elf"))
465-
return false;
466478
Address start_address = obj_file->GetBaseAddress();
467479
auto load_ptr = static_cast<uintptr_t>(
468480
start_address.GetLoadAddress(&(m_process.GetTarget())));
@@ -473,8 +485,20 @@ bool SwiftLanguageRuntimeImpl::AddModuleToReflectionContext(
473485
obj_file->GetFileSpec().GetFilename().GetCString());
474486
return false;
475487
}
476-
if (HasReflectionInfo(obj_file))
477-
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
488+
if (HasReflectionInfo(obj_file)) {
489+
// When dealing with ELF, we need to pass in the contents of the on-disk
490+
// file, since the Section Header Table is not present in the child process
491+
if (obj_file->GetPluginName().GetStringRef().equals("elf")) {
492+
DataExtractor extractor;
493+
auto size = obj_file->GetData(0, obj_file->GetByteSize(), extractor);
494+
const uint8_t *file_data = extractor.GetDataStart();
495+
llvm::sys::MemoryBlock file_buffer((void *)file_data, size);
496+
m_reflection_ctx->readELF(swift::remote::RemoteAddress(load_ptr),
497+
llvm::Optional<llvm::sys::MemoryBlock>(file_buffer));
498+
} else {
499+
m_reflection_ctx->addImage(swift::remote::RemoteAddress(load_ptr));
500+
}
501+
}
478502
return true;
479503
}
480504

lldb/test/API/lang/swift/implementation_only_imports/library_resilient/TestLibraryResilient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def launch_info(self):
4242
return info
4343

4444
@swiftTest
45-
@expectedFailureOS(no_match(["macosx"])) # Requires Remote Mirrors support
45+
@expectedFailureAll(oslist=["windows"]) # Requires Remote Mirrors support
4646
def test_implementation_only_import_library(self):
4747
"""Test `@_implementationOnly import` in a resilient library used by the main executable
4848
@@ -64,7 +64,7 @@ def cleanup():
6464
self.expect("e container.wrapped", substrs=["(SomeLibraryCore.TwoInts)", "(first = 2, second = 3)"])
6565

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

lldb/test/API/lang/swift/implementation_only_imports/main_executable/TestMainExecutable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def cleanup():
102102
self.runCmd("settings set symbols.use-swift-dwarfimporter true")
103103

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

lldb/test/API/lang/swift/parseable_interfaces/shared/TestSwiftInterfaceNoDebugInfo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ class TestSwiftInterfaceNoDebugInfo(TestBase):
3030
mydir = TestBase.compute_mydir(__file__)
3131

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

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

lldb/test/API/lang/swift/parseable_interfaces/static/TestSwiftInterfaceStaticNoDebugInfo.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ class TestSwiftInterfaceStaticNoDebugInfo(TestBase):
3030
mydir = TestBase.compute_mydir(__file__)
3131

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

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

0 commit comments

Comments
 (0)