|
23 | 23 | #include "llvm/BinaryFormat/ELF.h"
|
24 | 24 | #include "llvm/Object/COFF.h"
|
25 | 25 | #include "llvm/Support/Memory.h"
|
| 26 | +#include "llvm/ADT/STLExtras.h" |
26 | 27 |
|
27 | 28 | #include "swift/ABI/Enum.h"
|
28 | 29 | #include "swift/ABI/ObjectFile.h"
|
@@ -644,6 +645,47 @@ class ReflectionContext
|
644 | 645 | return false;
|
645 | 646 | }
|
646 | 647 |
|
| 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 | + |
647 | 689 | void addReflectionInfo(ReflectionInfo I) {
|
648 | 690 | getBuilder().addReflectionInfo(I);
|
649 | 691 | }
|
|
0 commit comments