|
| 1 | +//===--- swift-reflection-test.cpp - Reflection testing application -------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// This is a host-side tool to dump remote reflection sections in swift |
| 13 | +// binaries. |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "swift/ABI/MetadataValues.h" |
| 17 | +#include "swift/Basic/Demangle.h" |
| 18 | +#include "swift/Basic/LLVMInitialize.h" |
| 19 | +#include "swift/Reflection/ReflectionContext.h" |
| 20 | +#include "swift/Reflection/TypeRef.h" |
| 21 | +#include "llvm/Object/Archive.h" |
| 22 | +#include "llvm/Object/MachO.h" |
| 23 | +#include "llvm/Object/MachOUniversal.h" |
| 24 | +#include "llvm/Object/ELF.h" |
| 25 | +#include "llvm/Support/CommandLine.h" |
| 26 | + |
| 27 | +#include <unistd.h> |
| 28 | + |
| 29 | +#include <algorithm> |
| 30 | +#include <iostream> |
| 31 | +#include <csignal> |
| 32 | + |
| 33 | +using llvm::dyn_cast; |
| 34 | +using llvm::StringRef; |
| 35 | +using llvm::ArrayRef; |
| 36 | +using namespace llvm::object; |
| 37 | + |
| 38 | +using namespace swift; |
| 39 | +using namespace reflection; |
| 40 | +using namespace Demangle; |
| 41 | + |
| 42 | +enum class ActionType { |
| 43 | + None, |
| 44 | + DumpReflectionSections, |
| 45 | + DumpHeapInstance |
| 46 | +}; |
| 47 | + |
| 48 | +namespace options { |
| 49 | +static llvm::cl::opt<ActionType> |
| 50 | +Action(llvm::cl::desc("Mode:"), |
| 51 | + llvm::cl::values( |
| 52 | + clEnumValN(ActionType::DumpReflectionSections, |
| 53 | + "dump-reflection-sections", |
| 54 | + "Dump the field reflection section"), |
| 55 | + clEnumValN(ActionType::DumpHeapInstance, |
| 56 | + "dump-heap-instance", |
| 57 | + "Dump the field layout for a heap instance by running " |
| 58 | + "a Swift executable"), |
| 59 | + clEnumValEnd)); |
| 60 | + |
| 61 | +static llvm::cl::opt<std::string> |
| 62 | +BinaryFilename("binary-filename", llvm::cl::desc("Filename of the binary file"), |
| 63 | + llvm::cl::Required); |
| 64 | + |
| 65 | +static llvm::cl::opt<std::string> |
| 66 | +Architecture("arch", llvm::cl::desc("Architecture to inspect in the binary"), |
| 67 | + llvm::cl::Required); |
| 68 | +} // end namespace options |
| 69 | + |
| 70 | +static void guardError(std::error_code error) { |
| 71 | + if (!error) return; |
| 72 | + std::cerr << "swift-reflection-test error: " << error.message() << "\n"; |
| 73 | + exit(EXIT_FAILURE); |
| 74 | +} |
| 75 | + |
| 76 | +static llvm::object::SectionRef |
| 77 | +getSectionRef(const ObjectFile *objectFile, |
| 78 | + ArrayRef<StringRef> anySectionNames) { |
| 79 | + for (auto section : objectFile->sections()) { |
| 80 | + StringRef sectionName; |
| 81 | + section.getName(sectionName); |
| 82 | + for (auto desiredName : anySectionNames) { |
| 83 | + if (sectionName.equals(desiredName)) { |
| 84 | + return section; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + return llvm::object::SectionRef(); |
| 89 | +} |
| 90 | + |
| 91 | +static llvm::object::SectionRef |
| 92 | +getSectionRef(const Binary *binaryFile, StringRef arch, |
| 93 | + ArrayRef<StringRef> anySectionNames) { |
| 94 | + if (auto objectFile = dyn_cast<ObjectFile>(binaryFile)) |
| 95 | + return getSectionRef(objectFile, anySectionNames); |
| 96 | + if (auto machoUniversal = dyn_cast<MachOUniversalBinary>(binaryFile)) { |
| 97 | + const auto objectOrError = machoUniversal->getObjectForArch(arch); |
| 98 | + guardError(objectOrError.getError()); |
| 99 | + return getSectionRef(objectOrError.get().get(), anySectionNames); |
| 100 | + } |
| 101 | + return SectionRef(); |
| 102 | +} |
| 103 | + |
| 104 | +static int doDumpReflectionSections(std::string BinaryFilename, |
| 105 | + StringRef arch) { |
| 106 | + auto binaryOrError = llvm::object::createBinary(BinaryFilename); |
| 107 | + guardError(binaryOrError.getError()); |
| 108 | + |
| 109 | + const auto binary = binaryOrError.get().getBinary(); |
| 110 | + |
| 111 | + auto FieldSectionRef = getSectionRef(binary, arch, { |
| 112 | + "__swift3_fieldmd", ".swift3_fieldmd" |
| 113 | + }); |
| 114 | + |
| 115 | + if (FieldSectionRef.getObject() == nullptr) { |
| 116 | + std::cerr << BinaryFilename; |
| 117 | + std::cerr << " doesn't have a field reflection section!\n"; |
| 118 | + return EXIT_FAILURE; |
| 119 | + } |
| 120 | + |
| 121 | + auto AssociatedTypeSectionRef = getSectionRef(binary, arch, { |
| 122 | + "__swift3_assocty", ".swift3_assocty" |
| 123 | + }); |
| 124 | + |
| 125 | + if (AssociatedTypeSectionRef.getObject() == nullptr) { |
| 126 | + std::cerr << BinaryFilename; |
| 127 | + std::cerr << " doesn't have an associated type reflection section!\n"; |
| 128 | + return EXIT_FAILURE; |
| 129 | + } |
| 130 | + |
| 131 | + auto ReflectionStringsSectionRef = getSectionRef(binary, arch, { |
| 132 | + "__swift3_reflstr", ".swift3_reflstr" |
| 133 | + }); |
| 134 | + |
| 135 | + if (ReflectionStringsSectionRef.getObject() == nullptr) { |
| 136 | + std::cerr << BinaryFilename; |
| 137 | + std::cerr << " doesn't have an associated reflection strings section!\n"; |
| 138 | + return EXIT_FAILURE; |
| 139 | + } |
| 140 | + |
| 141 | + auto TypeRefSectionRef = getSectionRef(binary, arch, { |
| 142 | + "__swift3_typeref", ".swift3_typeref" |
| 143 | + }); |
| 144 | + |
| 145 | + if (TypeRefSectionRef.getObject() == nullptr) { |
| 146 | + std::cerr << BinaryFilename; |
| 147 | + std::cerr << " doesn't have an associated typeref section!\n"; |
| 148 | + return EXIT_FAILURE; |
| 149 | + } |
| 150 | + |
| 151 | + StringRef FieldSectionContents; |
| 152 | + FieldSectionRef.getContents(FieldSectionContents); |
| 153 | + |
| 154 | + const FieldSection fieldSection { |
| 155 | + reinterpret_cast<const void *>(FieldSectionContents.begin()), |
| 156 | + reinterpret_cast<const void *>(FieldSectionContents.end()) |
| 157 | + }; |
| 158 | + |
| 159 | + StringRef AssociatedTypeSectionContents; |
| 160 | + AssociatedTypeSectionRef.getContents(AssociatedTypeSectionContents); |
| 161 | + |
| 162 | + const AssociatedTypeSection associatedTypeSection { |
| 163 | + reinterpret_cast<const void *>(AssociatedTypeSectionContents.begin()), |
| 164 | + reinterpret_cast<const void *>(AssociatedTypeSectionContents.end()) |
| 165 | + }; |
| 166 | + |
| 167 | + StringRef ReflectionStringsSectionContents; |
| 168 | + ReflectionStringsSectionRef.getContents(ReflectionStringsSectionContents); |
| 169 | + |
| 170 | + const GenericSection ReflectionStringsSection { |
| 171 | + reinterpret_cast<const void *>(ReflectionStringsSectionContents.begin()), |
| 172 | + reinterpret_cast<const void *>(ReflectionStringsSectionContents.end()) |
| 173 | + }; |
| 174 | + |
| 175 | + StringRef TypeRefSectionContents; |
| 176 | + AssociatedTypeSectionRef.getContents(TypeRefSectionContents); |
| 177 | + |
| 178 | + const GenericSection TypeRefSection { |
| 179 | + reinterpret_cast<const void *>(TypeRefSectionContents.begin()), |
| 180 | + reinterpret_cast<const void *>(TypeRefSectionContents.end()) |
| 181 | + }; |
| 182 | + |
| 183 | + InProcessMemoryReader Reader; |
| 184 | + ReflectionContext<External<RuntimeTarget<8>>> RC(Reader); |
| 185 | + RC.addReflectionInfo({ |
| 186 | + BinaryFilename, |
| 187 | + fieldSection, |
| 188 | + associatedTypeSection, |
| 189 | + ReflectionStringsSection, |
| 190 | + TypeRefSection, |
| 191 | + }); |
| 192 | + RC.dumpAllSections(std::cout); |
| 193 | + |
| 194 | + return EXIT_SUCCESS; |
| 195 | +} |
| 196 | + |
| 197 | +int main(int argc, char *argv[]) { |
| 198 | + llvm::cl::ParseCommandLineOptions(argc, argv, "Swift Reflection Dump\n"); |
| 199 | + return doDumpReflectionSections(options::BinaryFilename, |
| 200 | + options::Architecture); |
| 201 | +} |
| 202 | + |
0 commit comments