Skip to content

Commit 34c55e2

Browse files
committed
Add functionality that returns correct reflection section name for different object files
1 parent 4a6a684 commit 34c55e2

File tree

2 files changed

+142
-21
lines changed

2 files changed

+142
-21
lines changed

include/swift/ABI/ObjectFile.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//===--- ObjectFile.h - Object File Related Information ------*- C++ -*-===//
2+
//
3+
// Object File related data structures.
4+
//
5+
//===----------------------------------------------------------------------===//
6+
7+
#ifndef SWIFT_ABI_OBJECTFILE_H
8+
#define SWIFT_ABI_OBJECTFILE_H
9+
10+
#include "llvm/Support/ErrorHandling.h"
11+
#include "llvm/ADT/StringRef.h"
12+
13+
namespace swift {
14+
15+
/// Represents the six reflection sections used by Swift
16+
enum ReflectionSectionKind : uint8_t {
17+
fieldmd,
18+
assocty,
19+
builtin,
20+
capture,
21+
typeref,
22+
reflstr
23+
};
24+
25+
/// Abstract base class responsible for providing the correct reflection section
26+
/// string identifier for a given object file type (Mach-O, ELF, COFF).
27+
class SwiftObjectFileFormat {
28+
public:
29+
virtual ~SwiftObjectFileFormat() {}
30+
virtual llvm::StringRef getSectionName(ReflectionSectionKind section) = 0;
31+
};
32+
33+
/// Responsible for providing the Mach-O reflection section identifiers.
34+
class SwiftObjectFileFormatMachO : SwiftObjectFileFormat {
35+
public:
36+
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
37+
switch (section) {
38+
case fieldmd:
39+
return "__swift5_fieldmd";
40+
case assocty:
41+
return "__swift5_assocty";
42+
case builtin:
43+
return "__swift5_builtin";
44+
case capture:
45+
return "__swift5_capture";
46+
case typeref:
47+
return "__swift5_typeref";
48+
case reflstr:
49+
return "__swift5_reflstr";
50+
}
51+
llvm_unreachable("Section type not found.");
52+
}
53+
};
54+
55+
/// Responsible for providing the ELF reflection section identifiers.
56+
class SwiftObjectFileFormatELF : SwiftObjectFileFormat {
57+
public:
58+
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
59+
switch (section) {
60+
case fieldmd:
61+
return "swift5_fieldmd";
62+
case assocty:
63+
return "swift5_assocty";
64+
case builtin:
65+
return "swift5_builtin";
66+
case capture:
67+
return "swift5_capture";
68+
case typeref:
69+
return "swift5_typeref";
70+
case reflstr:
71+
return "swift5_reflstr";
72+
}
73+
llvm_unreachable("Section type not found.");
74+
}
75+
};
76+
77+
/// Responsible for providing the COFF reflection section identifiers
78+
class SwiftObjectFileFormatCOFF : SwiftObjectFileFormat {
79+
public:
80+
llvm::StringRef getSectionName(ReflectionSectionKind section) override {
81+
switch (section) {
82+
case fieldmd:
83+
return ".sw5flmd";
84+
case assocty:
85+
return ".sw5asty";
86+
case builtin:
87+
return ".sw5bltn";
88+
case capture:
89+
return ".sw5cptr";
90+
case typeref:
91+
return ".sw5tyrf";
92+
case reflstr:
93+
return ".sw5rfst";
94+
}
95+
llvm_unreachable("Section not found.");
96+
}
97+
};
98+
} // namespace swift
99+
#endif // SWIFT_ABI_OBJECTFILE_H

include/swift/Reflection/ReflectionContext.h

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "llvm/Object/COFF.h"
2525

2626
#include "swift/ABI/Enum.h"
27+
#include "swift/ABI/ObjectFile.h"
2728
#include "swift/Remote/MemoryReader.h"
2829
#include "swift/Remote/MetadataReader.h"
2930
#include "swift/Reflection/Records.h"
@@ -210,12 +211,12 @@ class ReflectionContext
210211
auto SectBuf = this->getReader().readBytes(RemoteAddress(RangeStart),
211212
RangeEnd - RangeStart);
212213

213-
auto findMachOSectionByName = [&](std::string Name)
214+
auto findMachOSectionByName = [&](llvm::StringRef Name)
214215
-> std::pair<RemoteRef<void>, uint64_t> {
215216
for (unsigned I = 0; I < NumSect; ++I) {
216217
auto S = reinterpret_cast<typename T::Section *>(
217218
SectionsBuf + (I * sizeof(typename T::Section)));
218-
if (strncmp(S->sectname, Name.c_str(), strlen(Name.c_str())) != 0)
219+
if (strncmp(S->sectname, Name.data(), strlen(Name.data())) != 0)
219220
continue;
220221
auto RemoteSecStart = S->addr + Slide;
221222
auto SectBufData = reinterpret_cast<const char *>(SectBuf.get());
@@ -228,12 +229,19 @@ class ReflectionContext
228229
return {nullptr, 0};
229230
};
230231

231-
auto FieldMdSec = findMachOSectionByName("__swift5_fieldmd");
232-
auto AssocTySec = findMachOSectionByName("__swift5_assocty");
233-
auto BuiltinTySec = findMachOSectionByName("__swift5_builtin");
234-
auto CaptureSec = findMachOSectionByName("__swift5_capture");
235-
auto TypeRefMdSec = findMachOSectionByName("__swift5_typeref");
236-
auto ReflStrMdSec = findMachOSectionByName("__swift5_reflstr");
232+
SwiftObjectFileFormatMachO ObjectFileFormat;
233+
auto FieldMdSec = findMachOSectionByName(
234+
ObjectFileFormat.getSectionName(ReflectionSectionKind::fieldmd));
235+
auto AssocTySec = findMachOSectionByName(
236+
ObjectFileFormat.getSectionName(ReflectionSectionKind::assocty));
237+
auto BuiltinTySec = findMachOSectionByName(
238+
ObjectFileFormat.getSectionName(ReflectionSectionKind::builtin));
239+
auto CaptureSec = findMachOSectionByName(
240+
ObjectFileFormat.getSectionName(ReflectionSectionKind::capture));
241+
auto TypeRefMdSec = findMachOSectionByName(
242+
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
243+
auto ReflStrMdSec = findMachOSectionByName(
244+
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
237245

238246
if (FieldMdSec.first == nullptr &&
239247
AssocTySec.first == nullptr &&
@@ -330,12 +338,19 @@ class ReflectionContext
330338
return {nullptr, 0};
331339
};
332340

333-
auto CaptureSec = findCOFFSectionByName(".sw5cptr");
334-
auto TypeRefMdSec = findCOFFSectionByName(".sw5tyrf");
335-
auto FieldMdSec = findCOFFSectionByName(".sw5flmd");
336-
auto AssocTySec = findCOFFSectionByName(".sw5asty");
337-
auto BuiltinTySec = findCOFFSectionByName(".sw5bltn");
338-
auto ReflStrMdSec = findCOFFSectionByName(".sw5rfst");
341+
SwiftObjectFileFormatCOFF ObjectFileFormat;
342+
auto FieldMdSec = findCOFFSectionByName(
343+
ObjectFileFormat.getSectionName(ReflectionSectionKind::fieldmd));
344+
auto AssocTySec = findCOFFSectionByName(
345+
ObjectFileFormat.getSectionName(ReflectionSectionKind::assocty));
346+
auto BuiltinTySec = findCOFFSectionByName(
347+
ObjectFileFormat.getSectionName(ReflectionSectionKind::builtin));
348+
auto CaptureSec = findCOFFSectionByName(
349+
ObjectFileFormat.getSectionName(ReflectionSectionKind::capture));
350+
auto TypeRefMdSec = findCOFFSectionByName(
351+
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
352+
auto ReflStrMdSec = findCOFFSectionByName(
353+
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
339354

340355
if (FieldMdSec.first == nullptr &&
341356
AssocTySec.first == nullptr &&
@@ -424,7 +439,7 @@ class ReflectionContext
424439
auto StrTabBuf = this->getReader().readBytes(StrTabStart, StrTabSize);
425440
auto StrTab = reinterpret_cast<const char *>(StrTabBuf.get());
426441

427-
auto findELFSectionByName = [&](std::string Name)
442+
auto findELFSectionByName = [&](llvm::StringRef Name)
428443
-> std::pair<RemoteRef<void>, uint64_t> {
429444
// Now for all the sections, find their name.
430445
for (const typename T::Section *Hdr : SecHdrVec) {
@@ -444,12 +459,19 @@ class ReflectionContext
444459
return {nullptr, 0};
445460
};
446461

447-
auto FieldMdSec = findELFSectionByName("swift5_fieldmd");
448-
auto AssocTySec = findELFSectionByName("swift5_assocty");
449-
auto BuiltinTySec = findELFSectionByName("swift5_builtin");
450-
auto CaptureSec = findELFSectionByName("swift5_capture");
451-
auto TypeRefMdSec = findELFSectionByName("swift5_typeref");
452-
auto ReflStrMdSec = findELFSectionByName("swift5_reflstr");
462+
SwiftObjectFileFormatELF ObjectFileFormat;
463+
auto FieldMdSec = findELFSectionByName(
464+
ObjectFileFormat.getSectionName(ReflectionSectionKind::fieldmd));
465+
auto AssocTySec = findELFSectionByName(
466+
ObjectFileFormat.getSectionName(ReflectionSectionKind::assocty));
467+
auto BuiltinTySec = findELFSectionByName(
468+
ObjectFileFormat.getSectionName(ReflectionSectionKind::builtin));
469+
auto CaptureSec = findELFSectionByName(
470+
ObjectFileFormat.getSectionName(ReflectionSectionKind::capture));
471+
auto TypeRefMdSec = findELFSectionByName(
472+
ObjectFileFormat.getSectionName(ReflectionSectionKind::typeref));
473+
auto ReflStrMdSec = findELFSectionByName(
474+
ObjectFileFormat.getSectionName(ReflectionSectionKind::reflstr));
453475

454476
// We succeed if at least one of the sections is present in the
455477
// ELF executable.

0 commit comments

Comments
 (0)