Skip to content

Commit faecfda

Browse files
committed
Reflection: Consolidate some code for passing around reflection sections
I'm about to add a new section, and I'd like to update as few places as possible.
1 parent 9d9c973 commit faecfda

File tree

7 files changed

+70
-178
lines changed

7 files changed

+70
-178
lines changed

include/swift/Reflection/TypeRefBuilder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ using BuiltinTypeSection = ReflectionSection<BuiltinTypeDescriptorIterator>;
7171
using GenericSection = ReflectionSection<const void *>;
7272

7373
struct ReflectionInfo {
74-
std::string ImageName;
7574
FieldSection fieldmd;
7675
AssociatedTypeSection assocty;
7776
BuiltinTypeSection builtin;

include/swift/SwiftRemoteMirror/SwiftRemoteMirror.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ swift_reflection_destroyReflectionContext(SwiftReflectionContextRef Context);
5151
/// Add reflection sections for a loaded Swift image.
5252
void
5353
swift_reflection_addReflectionInfo(SwiftReflectionContextRef ContextRef,
54-
const char *ImageName,
55-
swift_reflection_section_t fieldmd,
56-
swift_reflection_section_t builtin,
57-
swift_reflection_section_t assocty,
58-
swift_reflection_section_t typeref,
59-
swift_reflection_section_t reflstr);
54+
swift_reflection_info_t Info);
6055

6156

6257
/// Returns a boolean indicating if the isa mask was successfully

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,24 @@ extern "C" {
2626

2727
typedef uintptr_t swift_typeref_t;
2828

29-
/// \brief Represents the __swift{n}_reflect section of an image.
30-
///
31-
/// If this section is virtually mapped, the following corresponding sections
32-
/// should also be mapped into the current address space:
33-
///
34-
/// __swift{n}_typeref
35-
/// --swift{n}_reflstr
36-
///
37-
/// where {n} is SWIFT_REFLECTION_VERSION_MAJOR.
38-
typedef struct swift_reflection_section_t {
29+
/// \brief Represents one of the Swift reflection sections of an image.
30+
typedef struct swift_reflection_section {
3931
void *Begin;
4032
void *End;
4133
} swift_reflection_section_t;
4234

35+
/// \brief Represents the set of Swift reflection sections of an image.
36+
/// Not all sections may be present.
37+
typedef struct swift_reflection_info {
38+
swift_reflection_section_t fieldmd;
39+
swift_reflection_section_t builtin;
40+
swift_reflection_section_t assocty;
41+
swift_reflection_section_t typeref;
42+
swift_reflection_section_t reflstr;
43+
} swift_reflection_info_t;
44+
4345
/// The layout kind of a Swift type.
44-
typedef enum swift_layout_kind_t {
46+
typedef enum swift_layout_kind {
4547
// Nothing is known about the size or contents of this value.
4648
SWIFT_UNKNOWN,
4749

stdlib/private/SwiftReflectionTest/SwiftReflectionTest.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,16 +171,10 @@ internal func sendReflectionInfos() {
171171

172172
var numInfos = infos.count
173173
debugLog("\(numInfos) reflection info bundles.")
174-
sendBytes(from: &numInfos, count: sizeof(UInt.self))
175174
precondition(numInfos >= 1)
175+
sendBytes(from: &numInfos, count: sizeof(UInt.self))
176176
for info in infos {
177177
debugLog("Sending info for \(info.imageName)")
178-
let imageNameBytes = Array(info.imageName.utf8)
179-
var imageNameLength = UInt(imageNameBytes.count)
180-
fwrite(&imageNameLength, sizeof(UInt.self), 1, stdout)
181-
fflush(stdout)
182-
fwrite(imageNameBytes, 1, imageNameBytes.count, stdout)
183-
fflush(stdout)
184178
for section in info {
185179
sendValue(section?.startAddress)
186180
sendValue(section?.size ?? 0)

stdlib/public/SwiftRemoteMirror/SwiftRemoteMirror.cpp

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,9 @@ void swift_reflection_destroyReflectionContext(SwiftReflectionContextRef Context
5151

5252
void
5353
swift_reflection_addReflectionInfo(SwiftReflectionContextRef ContextRef,
54-
const char *ImageName,
55-
swift_reflection_section_t fieldmd,
56-
swift_reflection_section_t assocty,
57-
swift_reflection_section_t builtin,
58-
swift_reflection_section_t typeref,
59-
swift_reflection_section_t reflstr) {
60-
ReflectionInfo Info {
61-
ImageName,
62-
FieldSection(fieldmd.Begin, fieldmd.End),
63-
AssociatedTypeSection(assocty.Begin, assocty.End),
64-
BuiltinTypeSection(builtin.Begin, builtin.End),
65-
GenericSection(typeref.Begin, typeref.End),
66-
GenericSection(reflstr.Begin, reflstr.End)
67-
};
54+
swift_reflection_info_t Info) {
6855
auto Context = reinterpret_cast<NativeReflectionContext *>(ContextRef);
69-
Context->addReflectionInfo(Info);
56+
Context->addReflectionInfo(*reinterpret_cast<ReflectionInfo *>(&Info));
7057
}
7158

7259
int

tools/swift-reflection-dump/swift-reflection-dump.cpp

Lines changed: 45 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ static T unwrap(llvm::ErrorOr<T> value) {
7575
exit(EXIT_FAILURE);
7676
}
7777

78-
static llvm::object::SectionRef
79-
getSectionRef(const ObjectFile *objectFile,
80-
ArrayRef<StringRef> anySectionNames) {
78+
static SectionRef getSectionRef(const ObjectFile *objectFile,
79+
ArrayRef<StringRef> anySectionNames) {
8180
for (auto section : objectFile->sections()) {
8281
StringRef sectionName;
8382
section.getName(sectionName);
@@ -90,25 +89,63 @@ getSectionRef(const ObjectFile *objectFile,
9089
return SectionRef();
9190
}
9291

92+
template<typename Section>
93+
static Section findReflectionSection(const ObjectFile *objectFile,
94+
ArrayRef<StringRef> anySectionNames) {
95+
auto sectionRef = getSectionRef(objectFile, anySectionNames);
96+
97+
if (sectionRef.getObject() == nullptr)
98+
return {nullptr, nullptr};
99+
100+
StringRef sectionContents;
101+
sectionRef.getContents(sectionContents);
102+
103+
return {
104+
reinterpret_cast<const void *>(sectionContents.begin()),
105+
reinterpret_cast<const void *>(sectionContents.end())
106+
};
107+
}
108+
109+
static ReflectionInfo findReflectionInfo(const ObjectFile *objectFile) {
110+
auto fieldSection = findReflectionSection<FieldSection>(
111+
objectFile, {"__swift3_fieldmd", ".swift3_fieldmd"});
112+
auto associatedTypeSection = findReflectionSection<AssociatedTypeSection>(
113+
objectFile, {"__swift3_assocty", ".swift3_assocty"});
114+
auto builtinTypeSection = findReflectionSection<BuiltinTypeSection>(
115+
objectFile, {"__swift3_builtin", ".swift3_builtin"});
116+
auto typeRefSection = findReflectionSection<GenericSection>(
117+
objectFile, {"__swift3_typeref", ".swift3_typeref"});
118+
auto reflectionStringsSection = findReflectionSection<GenericSection>(
119+
objectFile, {"__swift3_reflstr", ".swift3_reflstr"});
120+
121+
return {
122+
fieldSection,
123+
associatedTypeSection,
124+
builtinTypeSection,
125+
typeRefSection,
126+
reflectionStringsSection,
127+
};
128+
}
129+
93130
static int doDumpReflectionSections(ArrayRef<std::string> binaryFilenames,
94131
StringRef arch,
95132
ActionType action,
96133
std::ostream &OS) {
97134
// Note: binaryOrError and objectOrError own the memory for our ObjectFile;
98135
// once they go out of scope, we can no longer do anything.
99136
std::vector<OwningBinary<Binary>> binaryOwners;
100-
std::vector<std::unique_ptr<llvm::object::ObjectFile>> objectOwners;
137+
std::vector<std::unique_ptr<ObjectFile>> objectOwners;
101138

102139
// Construct the TypeRefBuilder
103140
TypeRefBuilder builder;
104141

105142
for (auto binaryFilename : binaryFilenames) {
106-
auto binaryOwner = unwrap(llvm::object::createBinary(binaryFilename));
107-
llvm::object::Binary *binaryFile = binaryOwner.getBinary();
143+
auto binaryOwner = unwrap(createBinary(binaryFilename));
144+
Binary *binaryFile = binaryOwner.getBinary();
108145

109146
// The object file we are doing lookups in -- either the binary itself, or
110147
// a particular slice of a universal binary.
111-
std::unique_ptr<llvm::object::ObjectFile> objectOwner;
148+
std::unique_ptr<ObjectFile> objectOwner;
112149
const ObjectFile *objectFile;
113150

114151
if (auto o = dyn_cast<ObjectFile>(binaryFile)) {
@@ -119,102 +156,7 @@ static int doDumpReflectionSections(ArrayRef<std::string> binaryFilenames,
119156
objectFile = objectOwner.get();
120157
}
121158

122-
// Field descriptor section
123-
auto fieldSectionRef = getSectionRef(objectFile, {
124-
"__swift3_fieldmd", ".swift3_fieldmd"
125-
});
126-
127-
if (fieldSectionRef.getObject() == nullptr) {
128-
OS << binaryFilename;
129-
OS << " doesn't have a field reflection section!\n";
130-
return EXIT_FAILURE;
131-
}
132-
133-
StringRef fieldSectionContents;
134-
fieldSectionRef.getContents(fieldSectionContents);
135-
136-
const FieldSection fieldSection {
137-
reinterpret_cast<const void *>(fieldSectionContents.begin()),
138-
reinterpret_cast<const void *>(fieldSectionContents.end())
139-
};
140-
141-
// Associated type section - optional
142-
AssociatedTypeSection associatedTypeSection {nullptr, nullptr};
143-
144-
auto associatedTypeSectionRef = getSectionRef(objectFile, {
145-
"__swift3_assocty", ".swift3_assocty"
146-
});
147-
148-
if (associatedTypeSectionRef.getObject() != nullptr) {
149-
StringRef associatedTypeSectionContents;
150-
associatedTypeSectionRef.getContents(associatedTypeSectionContents);
151-
associatedTypeSection = {
152-
reinterpret_cast<const void *>(associatedTypeSectionContents.begin()),
153-
reinterpret_cast<const void *>(associatedTypeSectionContents.end()),
154-
};
155-
}
156-
157-
// Builtin types section
158-
BuiltinTypeSection builtinTypeSection {nullptr, nullptr};
159-
160-
auto builtinTypeSectionRef = getSectionRef(objectFile, {
161-
"__swift3_builtin", ".swift3_builtin"
162-
});
163-
164-
if (builtinTypeSectionRef.getObject() != nullptr) {
165-
StringRef builtinTypeSectionContents;
166-
builtinTypeSectionRef.getContents(builtinTypeSectionContents);
167-
168-
builtinTypeSection = {
169-
reinterpret_cast<const void *>(builtinTypeSectionContents.begin()),
170-
reinterpret_cast<const void *>(builtinTypeSectionContents.end())
171-
};
172-
}
173-
174-
// Typeref section
175-
auto typeRefSectionRef = getSectionRef(objectFile, {
176-
"__swift3_typeref", ".swift3_typeref"
177-
});
178-
179-
if (typeRefSectionRef.getObject() == nullptr) {
180-
OS << binaryFilename;
181-
OS << " doesn't have an associated typeref section!\n";
182-
return EXIT_FAILURE;
183-
}
184-
185-
StringRef typeRefSectionContents;
186-
typeRefSectionRef.getContents(typeRefSectionContents);
187-
188-
const GenericSection typeRefSection {
189-
reinterpret_cast<const void *>(typeRefSectionContents.begin()),
190-
reinterpret_cast<const void *>(typeRefSectionContents.end())
191-
};
192-
193-
// Reflection strings section - optional
194-
GenericSection reflectionStringsSection {nullptr, nullptr};
195-
196-
auto reflectionStringsSectionRef = getSectionRef(objectFile, {
197-
"__swift3_reflstr", ".swift3_reflstr"
198-
});
199-
200-
if (reflectionStringsSectionRef.getObject() != nullptr) {
201-
StringRef reflectionStringsSectionContents;
202-
reflectionStringsSectionRef.getContents(reflectionStringsSectionContents);
203-
204-
reflectionStringsSection = {
205-
reinterpret_cast<const void *>(reflectionStringsSectionContents.begin()),
206-
reinterpret_cast<const void *>(reflectionStringsSectionContents.end())
207-
};
208-
}
209-
210-
builder.addReflectionInfo({
211-
binaryFilename,
212-
fieldSection,
213-
associatedTypeSection,
214-
builtinTypeSection,
215-
typeRefSection,
216-
reflectionStringsSection,
217-
});
159+
builder.addReflectionInfo(findReflectionInfo(objectFile));
218160

219161
// Retain the objects that own section memory
220162
binaryOwners.push_back(std::move(binaryOwner));

tools/swift-reflection-test/swift-reflection-test.c

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ typedef struct PipeMemoryReader {
4040
} PipeMemoryReader;
4141

4242
typedef struct RemoteReflectionInfo {
43-
const char *ImageName;
4443
RemoteSection fieldmd;
4544
RemoteSection assocty;
4645
RemoteSection builtin;
@@ -50,15 +49,6 @@ typedef struct RemoteReflectionInfo {
5049
size_t TotalSize;
5150
} RemoteReflectionInfo;
5251

53-
typedef struct LocalReflectionInfo {
54-
const char *ImageName;
55-
swift_reflection_section_t fieldmd;
56-
swift_reflection_section_t assocty;
57-
swift_reflection_section_t builtin;
58-
swift_reflection_section_t typeref;
59-
swift_reflection_section_t reflstr;
60-
} LocalReflectionInfo;
61-
6252
static void errorAndExit(const char *message) {
6353
fprintf(stderr, "%s: %s\n", message, strerror(errno));
6454
abort();
@@ -104,14 +94,12 @@ uintptr_t getEndAddress(const RemoteSection Sections[], size_t Count) {
10494
return End;
10595
}
10696

107-
RemoteReflectionInfo makeRemoteReflectionInfo(const char *ImageName,
108-
RemoteSection fieldmd,
97+
RemoteReflectionInfo makeRemoteReflectionInfo(RemoteSection fieldmd,
10998
RemoteSection assocty,
11099
RemoteSection builtin,
111100
RemoteSection typeref,
112101
RemoteSection reflstr) {
113102
RemoteReflectionInfo Info = {
114-
ImageName,
115103
fieldmd,
116104
assocty,
117105
builtin,
@@ -240,7 +228,7 @@ PipeMemoryReader createPipeMemoryReader() {
240228
return Reader;
241229
}
242230

243-
const LocalReflectionInfo *
231+
const swift_reflection_info_t *
244232
PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
245233
size_t *NumReflectionInfos) {
246234
int WriteFD = PipeMemoryReader_getParentWriteFD(Reader);
@@ -255,12 +243,6 @@ PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
255243
RemoteReflectionInfo *RemoteInfos = (RemoteReflectionInfo*)malloc(Size);
256244

257245
for (size_t i = 0; i < *NumReflectionInfos; ++i) {
258-
size_t ImageNameLength;
259-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)&ImageNameLength,
260-
sizeof(ImageNameLength));
261-
const char *ImageName = (const char *)malloc(ImageNameLength);
262-
PipeMemoryReader_collectBytesFromPipe(Reader, (uint8_t*)ImageName,
263-
ImageNameLength);
264246
uintptr_t fieldmd_start;
265247
size_t fieldmd_size;
266248
uintptr_t assocty_start;
@@ -294,7 +276,6 @@ PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
294276
sizeof(reflstr_size));
295277

296278
RemoteInfos[i] = makeRemoteReflectionInfo(
297-
ImageName,
298279
makeRemoteSection(fieldmd_start, fieldmd_size),
299280
makeRemoteSection(assocty_start, assocty_size),
300281
makeRemoteSection(builtin_start, builtin_size),
@@ -304,8 +285,8 @@ PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
304285

305286
// Now pull in the remote sections into our address space.
306287

307-
LocalReflectionInfo *Infos
308-
= malloc(sizeof(LocalReflectionInfo) * *NumReflectionInfos);
288+
swift_reflection_info_t *Infos
289+
= malloc(sizeof(swift_reflection_info_t) * *NumReflectionInfos);
309290

310291
for (size_t i = 0; i < *NumReflectionInfos; ++i) {
311292
RemoteReflectionInfo RemoteInfo = RemoteInfos[i];
@@ -330,8 +311,7 @@ PipeMemoryReader_receiveReflectionInfo(const PipeMemoryReader *Reader,
330311
uintptr_t reflstr_base
331312
= buffer + RemoteInfo.reflstr.StartAddress - RemoteInfo.StartAddress;
332313

333-
LocalReflectionInfo Info = {
334-
RemoteInfo.ImageName,
314+
swift_reflection_info_t Info = {
335315
makeLocalSection(fieldmd_base, RemoteInfo.fieldmd.Size),
336316
makeLocalSection(assocty_base, RemoteInfo.assocty.Size),
337317
makeLocalSection(builtin_base, RemoteInfo.builtin.Size),
@@ -392,18 +372,11 @@ int doDumpHeapInstance(const char *BinaryFilename) {
392372
instance);
393373

394374
size_t NumReflectionInfos = 0;
395-
const LocalReflectionInfo *Infos
375+
const swift_reflection_info_t *Infos
396376
= PipeMemoryReader_receiveReflectionInfo(&Pipe, &NumReflectionInfos);
397377

398-
for (size_t i = 0; i < NumReflectionInfos; ++i) {
399-
const LocalReflectionInfo Info = Infos[i];
400-
swift_reflection_addReflectionInfo(RC, Info.ImageName,
401-
Info.fieldmd,
402-
Info.assocty,
403-
Info.builtin,
404-
Info.typeref,
405-
Info.reflstr);
406-
}
378+
for (size_t i = 0; i < NumReflectionInfos; ++i)
379+
swift_reflection_addReflectionInfo(RC, Infos[i]);
407380

408381
printf("Type reference:\n");
409382

0 commit comments

Comments
 (0)