Skip to content

Commit 454d680

Browse files
Merge pull request #1608 from augusto2112/read-elf-sections
Use new libReflection API when reading ELF files
2 parents 382bb8b + a046f43 commit 454d680

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: \
@@ -3393,3 +3394,9 @@ ObjectFileELF::GetLoadableData(Target &target) {
33933394
}
33943395
return loadables;
33953396
}
3397+
3398+
llvm::StringRef ObjectFileELF::GetReflectionSectionIdentifier(
3399+
swift::ReflectionSectionKind section) {
3400+
swift::SwiftObjectFileFormatELF file_format_elf;
3401+
return file_format_elf.getSectionName(section);
3402+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,9 @@ class ObjectFileELF : public lldb_private::ObjectFile {
395395
/// .gnu_debugdata section or \c nullptr if an error occured or if there's no
396396
/// section with that name.
397397
std::shared_ptr<ObjectFileELF> GetGnuDebugDataObjectFile();
398+
399+
llvm::StringRef
400+
GetReflectionSectionIdentifier(swift::ReflectionSectionKind section);
398401
};
399402

400403
#endif // liblldb_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
(defined(__arm__) || defined(__arm64__) || defined(__aarch64__))
@@ -6513,3 +6514,9 @@ bool ObjectFileMachO::SaveCore(const lldb::ProcessSP &process_sp,
65136514
}
65146515
return false;
65156516
}
6517+
6518+
llvm::StringRef ObjectFileMachO::GetReflectionSectionIdentifier(
6519+
swift::ReflectionSectionKind section) {
6520+
swift::SwiftObjectFileFormatMachO file_format_mach_o;
6521+
return file_format_mach_o.getSectionName(section);
6522+
}

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
@@ -1264,3 +1266,9 @@ ObjectFile::Strata ObjectFilePECOFF::CalculateStrata() { return eStrataUser; }
12641266
ConstString ObjectFilePECOFF::GetPluginName() { return GetPluginNameStatic(); }
12651267

12661268
uint32_t ObjectFilePECOFF::GetPluginVersion() { return 1; }
1269+
1270+
llvm::StringRef ObjectFilePECOFF::GetReflectionSectionIdentifier(
1271+
swift::ReflectionSectionKind section) {
1272+
swift::SwiftObjectFileFormatCOFF file_format_coff;
1273+
return file_format_coff.getSectionName(section);
1274+
}

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
@@ -747,3 +747,9 @@ void llvm::format_provider<ObjectFile::Strata>::format(
747747
break;
748748
}
749749
}
750+
751+
llvm::StringRef ObjectFile::GetReflectionSectionIdentifier(
752+
swift::ReflectionSectionKind section) {
753+
assert("Base class's GetReflectionSectionIdentifier should not be called");
754+
return "";
755+
}

lldb/source/Target/SwiftLanguageRuntime.cpp

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

4444
#include "llvm/ADT/StringRef.h"
4545
#include "llvm/Support/raw_ostream.h"
46+
#include "llvm/Support/Memory.h"
4647

4748
// FIXME: we should not need this
4849
#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

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

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
@@ -41,7 +41,7 @@ def launch_info(self):
4141
return info
4242

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

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

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)