Skip to content

Commit d6d4216

Browse files
committed
Add Conformance section to ReflectionInfo read from object files
1 parent da64dc4 commit d6d4216

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

include/swift/ABI/Metadata.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2840,6 +2840,10 @@ using TargetProtocolConformanceRecord =
28402840

28412841
using ProtocolConformanceRecord = TargetProtocolConformanceRecord<InProcess>;
28422842

2843+
using ExternalProtocolConformanceDescriptor
2844+
= TargetProtocolConformanceDescriptor<External<RuntimeTarget<8>>>;
2845+
using ExternalProtocolConformanceRecord = TargetProtocolConformanceRecord<External<RuntimeTarget<8>>>;
2846+
28432847
template<typename Runtime>
28442848
struct TargetGenericContext;
28452849

include/swift/ABI/ObjectFile.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212

1313
namespace swift {
1414

15-
/// Represents the six reflection sections used by Swift
15+
/// Represents the seven reflection sections used by Swift
1616
enum ReflectionSectionKind : uint8_t {
1717
fieldmd,
1818
assocty,
1919
builtin,
2020
capture,
2121
typeref,
22-
reflstr
22+
reflstr,
23+
conform
2324
};
2425

2526
/// Abstract base class responsible for providing the correct reflection section
@@ -47,6 +48,8 @@ class SwiftObjectFileFormatMachO : public SwiftObjectFileFormat {
4748
return "__swift5_typeref";
4849
case reflstr:
4950
return "__swift5_reflstr";
51+
case conform:
52+
return "__swift5_proto";
5053
}
5154
llvm_unreachable("Section type not found.");
5255
}
@@ -69,6 +72,8 @@ class SwiftObjectFileFormatELF : public SwiftObjectFileFormat {
6972
return "swift5_typeref";
7073
case reflstr:
7174
return "swift5_reflstr";
75+
case conform:
76+
return "swift5_proto";
7277
}
7378
llvm_unreachable("Section type not found.");
7479
}
@@ -91,6 +96,8 @@ class SwiftObjectFileFormatCOFF : public SwiftObjectFileFormat {
9196
return ".sw5tyrf";
9297
case reflstr:
9398
return ".sw5rfst";
99+
case conform:
100+
return ".sw5cnfrm"; // TODO: Figure out the real name
94101
}
95102
llvm_unreachable("Section not found.");
96103
}

include/swift/Reflection/ReflectionContext.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,16 @@ class ReflectionContext
247247
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
248248
auto ReflStrMdSec = findMachOSectionByName(
249249
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
250+
auto ConformMdSec = findMachOSectionByName(
251+
ObjectFileFormat.getSectionName(ReflectionSectionKind::conform));
250252

251253
if (FieldMdSec.first == nullptr &&
252254
AssocTySec.first == nullptr &&
253255
BuiltinTySec.first == nullptr &&
254256
CaptureSec.first == nullptr &&
255257
TypeRefMdSec.first == nullptr &&
256-
ReflStrMdSec.first == nullptr)
258+
ReflStrMdSec.first == nullptr &&
259+
ConformMdSec.first == nullptr)
257260
return false;
258261

259262
ReflectionInfo info = {
@@ -262,7 +265,8 @@ class ReflectionContext
262265
{BuiltinTySec.first, BuiltinTySec.second},
263266
{CaptureSec.first, CaptureSec.second},
264267
{TypeRefMdSec.first, TypeRefMdSec.second},
265-
{ReflStrMdSec.first, ReflStrMdSec.second}};
268+
{ReflStrMdSec.first, ReflStrMdSec.second},
269+
{ConformMdSec.first, ConformMdSec.second}};
266270

267271
this->addReflectionInfo(info);
268272

@@ -365,13 +369,16 @@ class ReflectionContext
365369
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
366370
auto ReflStrMdSec = findCOFFSectionByName(
367371
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
372+
auto ConformMdSec = findCOFFSectionByName(
373+
ObjectFileFormat.getSectionName(ReflectionSectionKind::conform));
368374

369375
if (FieldMdSec.first == nullptr &&
370376
AssocTySec.first == nullptr &&
371377
BuiltinTySec.first == nullptr &&
372378
CaptureSec.first == nullptr &&
373379
TypeRefMdSec.first == nullptr &&
374-
ReflStrMdSec.first == nullptr)
380+
ReflStrMdSec.first == nullptr &&
381+
ConformMdSec.first == nullptr)
375382
return false;
376383

377384
ReflectionInfo Info = {
@@ -380,7 +387,8 @@ class ReflectionContext
380387
{BuiltinTySec.first, BuiltinTySec.second},
381388
{CaptureSec.first, CaptureSec.second},
382389
{TypeRefMdSec.first, TypeRefMdSec.second},
383-
{ReflStrMdSec.first, ReflStrMdSec.second}};
390+
{ReflStrMdSec.first, ReflStrMdSec.second},
391+
{ConformMdSec.first, ConformMdSec.second}};
384392
this->addReflectionInfo(Info);
385393
return true;
386394
}
@@ -544,6 +552,8 @@ class ReflectionContext
544552
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
545553
auto ReflStrMdSec = findELFSectionByName(
546554
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
555+
auto ConformMdSec = findELFSectionByName(
556+
ObjectFileFormat.getSectionName(ReflectionSectionKind::conform));
547557

548558
if (Error)
549559
return false;
@@ -555,7 +565,8 @@ class ReflectionContext
555565
BuiltinTySec.first == nullptr &&
556566
CaptureSec.first == nullptr &&
557567
TypeRefMdSec.first == nullptr &&
558-
ReflStrMdSec.first == nullptr)
568+
ReflStrMdSec.first == nullptr &&
569+
ConformMdSec.first == nullptr)
559570
return false;
560571

561572
ReflectionInfo info = {
@@ -564,7 +575,8 @@ class ReflectionContext
564575
{BuiltinTySec.first, BuiltinTySec.second},
565576
{CaptureSec.first, CaptureSec.second},
566577
{TypeRefMdSec.first, TypeRefMdSec.second},
567-
{ReflStrMdSec.first, ReflStrMdSec.second}};
578+
{ReflStrMdSec.first, ReflStrMdSec.second},
579+
{ConformMdSec.first, ConformMdSec.second}};
568580

569581
this->addReflectionInfo(info);
570582
return true;
@@ -665,7 +677,9 @@ class ReflectionContext
665677
auto Sections = {
666678
ReflectionSectionKind::fieldmd, ReflectionSectionKind::assocty,
667679
ReflectionSectionKind::builtin, ReflectionSectionKind::capture,
668-
ReflectionSectionKind::typeref, ReflectionSectionKind::reflstr};
680+
ReflectionSectionKind::typeref, ReflectionSectionKind::reflstr,
681+
ReflectionSectionKind::conform
682+
};
669683

670684
llvm::SmallVector<std::pair<RemoteRef<void>, uint64_t>, 6> Pairs;
671685
for (auto Section : Sections) {
@@ -687,7 +701,8 @@ class ReflectionContext
687701
ReflectionInfo Info = {
688702
{Pairs[0].first, Pairs[0].second}, {Pairs[1].first, Pairs[1].second},
689703
{Pairs[2].first, Pairs[2].second}, {Pairs[3].first, Pairs[3].second},
690-
{Pairs[4].first, Pairs[4].second}, {Pairs[5].first, Pairs[5].second}};
704+
{Pairs[4].first, Pairs[4].second}, {Pairs[5].first, Pairs[5].second},
705+
{Pairs[6].first, Pairs[6].second}};
691706
this->addReflectionInfo(Info);
692707
return true;
693708
}

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,23 @@ class CaptureDescriptorIterator
192192
+ CR->NumMetadataSources * sizeof(MetadataSourceRecord);
193193
}
194194
};
195+
196+
class ConformanceRecordIterator
197+
: public ReflectionSectionIteratorBase<ConformanceRecordIterator,
198+
ExternalProtocolConformanceRecord> {
199+
public:
200+
ConformanceRecordIterator(RemoteRef<void> Cur, uint64_t Size)
201+
: ReflectionSectionIteratorBase(Cur, Size)
202+
{}
203+
204+
static uint64_t getCurrentRecordSize(RemoteRef<ExternalProtocolConformanceRecord> CD){
205+
return sizeof(ExternalProtocolConformanceRecord);
206+
}
207+
};
208+
195209
using CaptureSection = ReflectionSection<CaptureDescriptorIterator>;
196210
using GenericSection = ReflectionSection<const void *>;
211+
using ConformanceSection = ReflectionSection<ConformanceRecordIterator>;
197212

198213
struct ReflectionInfo {
199214
FieldSection Field;
@@ -202,6 +217,7 @@ struct ReflectionInfo {
202217
CaptureSection Capture;
203218
GenericSection TypeReference;
204219
GenericSection ReflectionString;
220+
ConformanceSection Conformance;
205221
};
206222

207223
struct ClosureContextInfo {
@@ -739,6 +755,7 @@ class TypeRefBuilder {
739755
void dumpAssociatedTypeSection(std::ostream &stream);
740756
void dumpBuiltinTypeSection(std::ostream &stream);
741757
void dumpCaptureSection(std::ostream &stream);
758+
void dumpConformanceSection(std::ostream &stream);
742759
void dumpAllSections(std::ostream &stream);
743760
};
744761

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,41 @@ void TypeRefBuilder::dumpCaptureSection(std::ostream &stream) {
496496
}
497497
}
498498

499+
/// Given the address of a conformance descriptor, attempt to read it.
500+
static void readConformanceDescriptor(
501+
const ExternalProtocolConformanceRecord &record) {
502+
503+
// Read the flags to figure out how much space we should read.
504+
ContextDescriptorFlags flags;
505+
if (!Reader->readBytes(RemoteAddress(address), (uint8_t*)&flags,
506+
sizeof(flags)))
507+
return nullptr;
508+
}
509+
510+
void TypeRefBuilder::dumpConformanceSection(std::ostream &stream) {
511+
for (const auto &section : ReflectionInfos) {
512+
for (const auto conformanceRecord : section.Conformance) {
513+
stream << "dummy\n";
514+
readConformanceDescriptor(conformanceRecord->get());
515+
516+
// switch (auto kind = conformance->getTypeKind()) {
517+
// case TypeReferenceKind::DirectObjCClassName:
518+
// stream << "DirectObjCClassName\n";
519+
// break;
520+
//
521+
// case TypeReferenceKind::IndirectObjCClass:
522+
// stream << "IndirectObjCClass\n";
523+
// break;
524+
//
525+
// case TypeReferenceKind::DirectTypeDescriptor:
526+
// case TypeReferenceKind::IndirectTypeDescriptor:
527+
// stream << "TypeDescriptor\n";
528+
// break;
529+
// }
530+
}
531+
}
532+
}
533+
499534
void TypeRefBuilder::dumpAllSections(std::ostream &stream) {
500535
stream << "FIELDS:\n";
501536
stream << "=======\n";
@@ -513,6 +548,10 @@ void TypeRefBuilder::dumpAllSections(std::ostream &stream) {
513548
stream << "====================\n";
514549
dumpCaptureSection(stream);
515550
stream << "\n";
551+
stream << "CONFORMANCES:\n";
552+
stream << "=============\n";
553+
dumpConformanceSection(stream);
554+
stream << "\n";
516555
}
517556

518557
#endif

0 commit comments

Comments
 (0)