Skip to content

Commit 33eb213

Browse files
committedOct 14, 2022
[swift-reflection-dump] Make ObjC interoperability configurable.
This addresses a FIXME in the code. This allows running swift-reflection-dump on macOS to dump the contents of an ELF binary.
1 parent 5d832bf commit 33eb213

File tree

4 files changed

+52
-25
lines changed

4 files changed

+52
-25
lines changed
 

‎include/swift/StaticMirror/ObjectFileContext.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,12 @@ T unwrap(llvm::Expected<T> value) {
144144
}
145145

146146
std::unique_ptr<ReflectionContextHolder> makeReflectionContextForObjectFiles(
147-
const std::vector<const llvm::object::ObjectFile *> &objectFiles);
147+
const std::vector<const llvm::object::ObjectFile *> &objectFiles,
148+
bool objcInterOp);
148149

149150
std::unique_ptr<ReflectionContextHolder> makeReflectionContextForMetadataReader(
150-
std::shared_ptr<ObjectMemoryReader> reader);
151+
std::shared_ptr<ObjectMemoryReader> reader,
152+
bool objcInterOp);
151153

152154
} // end namespace static_mirror
153155
} // end namespace swift

‎lib/StaticMirror/BinaryScanningTool.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,13 @@ BinaryScanningTool::BinaryScanningTool(
5252
ObjectOwners.push_back(std::move(ObjectOwner));
5353
ObjectFiles.push_back(O);
5454
}
55-
Context = makeReflectionContextForObjectFiles(ObjectFiles);
55+
// FIXME: This could/should be configurable.
56+
#if SWIFT_OBJC_INTEROP
57+
bool ObjCInterop = true;
58+
#else
59+
bool ObjCInterop = false;
60+
#endif
61+
Context = makeReflectionContextForObjectFiles(ObjectFiles, ObjCInterop);
5662
PointerSize = Context->PointerSize;
5763
}
5864

‎lib/StaticMirror/ObjectFileContext.cpp

+21-17
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ std::unique_ptr<ReflectionContextHolder> makeReflectionContextForMetadataReader(
521521
}
522522

523523
std::unique_ptr<ReflectionContextHolder> makeReflectionContextForObjectFiles(
524-
const std::vector<const ObjectFile *> &objectFiles) {
524+
const std::vector<const ObjectFile *> &objectFiles, bool ObjCInterop) {
525525
auto Reader = std::make_shared<ObjectMemoryReader>(objectFiles);
526526

527527
uint8_t pointerSize;
@@ -530,23 +530,27 @@ std::unique_ptr<ReflectionContextHolder> makeReflectionContextForObjectFiles(
530530

531531
switch (pointerSize) {
532532
case 4:
533-
return makeReflectionContextForMetadataReader<
534-
// FIXME: This could be configurable.
535-
#if SWIFT_OBJC_INTEROP
536-
External<WithObjCInterop<RuntimeTarget<4>>>
537-
#else
538-
External<NoObjCInterop<RuntimeTarget<4>>>
539-
#endif
540-
>(std::move(Reader), pointerSize);
533+
#define MAKE_CONTEXT(INTEROP, PTRSIZE) \
534+
makeReflectionContextForMetadataReader< \
535+
External<INTEROP<RuntimeTarget<PTRSIZE>>>>(std::move(Reader), \
536+
pointerSize)
537+
#if SWIFT_OBJC_INTEROP
538+
if (ObjCInterop)
539+
return MAKE_CONTEXT(WithObjCInterop, 4);
540+
else
541+
return MAKE_CONTEXT(NoObjCInterop, 4);
542+
#else
543+
return MAKE_CONTEXT(NoObjCInterop, 4);
544+
#endif
541545
case 8:
542-
return makeReflectionContextForMetadataReader<
543-
// FIXME: This could be configurable.
544-
#if SWIFT_OBJC_INTEROP
545-
External<WithObjCInterop<RuntimeTarget<8>>>
546-
#else
547-
External<NoObjCInterop<RuntimeTarget<8>>>
548-
#endif
549-
>(std::move(Reader), pointerSize);
546+
#if SWIFT_OBJC_INTEROP
547+
if (ObjCInterop)
548+
return MAKE_CONTEXT(WithObjCInterop, 8);
549+
else
550+
return MAKE_CONTEXT(NoObjCInterop, 8);
551+
#else
552+
return MAKE_CONTEXT(NoObjCInterop, 8);
553+
#endif
550554
default:
551555
fputs("unsupported word size in object file\n", stderr);
552556
abort();

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

+20-5
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,12 @@ static llvm::cl::opt<std::string>
7878
Architecture("arch",
7979
llvm::cl::desc("Architecture to inspect in the binary"),
8080
llvm::cl::Required);
81+
82+
#if SWIFT_OBJC_INTEROP
83+
static llvm::cl::opt<bool> DisableObjCInterop(
84+
"no-objc-interop",
85+
llvm::cl::desc("Disable Objective-C interoperability support"));
86+
#endif
8187
} // end namespace options
8288

8389
static int doDumpReflectionSections(ArrayRef<std::string> BinaryFilenames,
@@ -109,25 +115,34 @@ static int doDumpReflectionSections(ArrayRef<std::string> BinaryFilenames,
109115
ObjectFiles.push_back(O);
110116
}
111117

112-
auto context = makeReflectionContextForObjectFiles(ObjectFiles);
118+
#if SWIFT_OBJC_INTEROP
119+
bool ObjCInterop = !options::DisableObjCInterop;
120+
#else
121+
bool ObjCInterop = false;
122+
#endif
123+
auto context = makeReflectionContextForObjectFiles(ObjectFiles, ObjCInterop);
113124
auto &builder = context->Builder;
114125

115126
switch (Action) {
116127
case ActionType::DumpReflectionSections:
117128
// Dump everything
118129
switch (context->PointerSize) {
119130
case 4:
120-
// FIXME: This could/should be configurable.
121131
#if SWIFT_OBJC_INTEROP
122-
builder.dumpAllSections<WithObjCInterop, 4>(stream);
132+
if (!options::DisableObjCInterop)
133+
builder.dumpAllSections<WithObjCInterop, 4>(stream);
134+
else
135+
builder.dumpAllSections<NoObjCInterop, 4>(stream);
123136
#else
124137
builder.dumpAllSections<NoObjCInterop, 4>(stream);
125138
#endif
126139
break;
127140
case 8:
128-
// FIXME: This could/should be configurable.
129141
#if SWIFT_OBJC_INTEROP
130-
builder.dumpAllSections<WithObjCInterop, 8>(stream);
142+
if (!options::DisableObjCInterop)
143+
builder.dumpAllSections<WithObjCInterop, 8>(stream);
144+
else
145+
builder.dumpAllSections<NoObjCInterop, 8>(stream);
131146
#else
132147
builder.dumpAllSections<NoObjCInterop, 8>(stream);
133148
#endif

0 commit comments

Comments
 (0)