Skip to content

Commit 0a67868

Browse files
committed
Implement ReflectionContext::addImage with section finder
Implement a version of addImage that takes in a closure responsible for finding the sections of interest. This allows for the registration of metadata even in situations where the object file is not complete (for example, when generated by the JIT).
1 parent 7ccc1b0 commit 0a67868

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

include/swift/ABI/ObjectFile.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SwiftObjectFileFormat {
3131
};
3232

3333
/// Responsible for providing the Mach-O reflection section identifiers.
34-
class SwiftObjectFileFormatMachO : SwiftObjectFileFormat {
34+
class SwiftObjectFileFormatMachO : public SwiftObjectFileFormat {
3535
public:
3636
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
3737
switch (section) {
@@ -53,7 +53,7 @@ class SwiftObjectFileFormatMachO : SwiftObjectFileFormat {
5353
};
5454

5555
/// Responsible for providing the ELF reflection section identifiers.
56-
class SwiftObjectFileFormatELF : SwiftObjectFileFormat {
56+
class SwiftObjectFileFormatELF : public SwiftObjectFileFormat {
5757
public:
5858
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
5959
switch (section) {
@@ -75,7 +75,7 @@ class SwiftObjectFileFormatELF : SwiftObjectFileFormat {
7575
};
7676

7777
/// Responsible for providing the COFF reflection section identifiers
78-
class SwiftObjectFileFormatCOFF : SwiftObjectFileFormat {
78+
class SwiftObjectFileFormatCOFF : public SwiftObjectFileFormat {
7979
public:
8080
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
8181
switch (section) {

include/swift/Reflection/ReflectionContext.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "llvm/BinaryFormat/ELF.h"
2424
#include "llvm/Object/COFF.h"
2525
#include "llvm/Support/Memory.h"
26+
#include "llvm/ADT/STLExtras.h"
2627

2728
#include "swift/ABI/Enum.h"
2829
#include "swift/ABI/ObjectFile.h"
@@ -644,6 +645,47 @@ class ReflectionContext
644645
return false;
645646
}
646647

648+
/// Adds an image using the FindSection closure to find the swift metadata
649+
/// sections. \param FindSection
650+
/// Closure that finds sections by name. ReflectionContext is in charge
651+
/// of freeing the memory buffer in the RemoteRef return value.
652+
/// process.
653+
/// \return
654+
/// \b True if any of the reflection sections were registered,
655+
/// \b false otherwise.
656+
bool addImage(llvm::function_ref<
657+
std::pair<RemoteRef<void>, uint64_t>(ReflectionSectionKind)>
658+
FindSection) {
659+
auto Sections = {
660+
ReflectionSectionKind::fieldmd, ReflectionSectionKind::assocty,
661+
ReflectionSectionKind::builtin, ReflectionSectionKind::capture,
662+
ReflectionSectionKind::typeref, ReflectionSectionKind::reflstr};
663+
664+
llvm::SmallVector<std::pair<RemoteRef<void>, uint64_t>, 6> Pairs;
665+
for (auto Section : Sections) {
666+
Pairs.push_back(FindSection(Section));
667+
auto LatestRemoteRef = std::get<RemoteRef<void>>(Pairs.back());
668+
if (LatestRemoteRef) {
669+
MemoryReader::ReadBytesResult Buffer(
670+
LatestRemoteRef.getLocalBuffer(),
671+
[](const void *Ptr) { free(const_cast<void *>(Ptr)); });
672+
673+
savedBuffers.push_back(std::move(Buffer));
674+
}
675+
}
676+
677+
// If we didn't find any sections, return.
678+
if (llvm::all_of(Pairs, [](const auto &Pair) { return !Pair.first; }))
679+
return false;
680+
681+
ReflectionInfo Info = {
682+
{Pairs[0].first, Pairs[0].second}, {Pairs[1].first, Pairs[1].second},
683+
{Pairs[2].first, Pairs[2].second}, {Pairs[3].first, Pairs[3].second},
684+
{Pairs[4].first, Pairs[4].second}, {Pairs[5].first, Pairs[5].second}};
685+
this->addReflectionInfo(Info);
686+
return true;
687+
}
688+
647689
void addReflectionInfo(ReflectionInfo I) {
648690
getBuilder().addReflectionInfo(I);
649691
}

0 commit comments

Comments
 (0)