diff --git a/llvm/tools/LLVMBuild.txt b/llvm/tools/LLVMBuild.txt --- a/llvm/tools/LLVMBuild.txt +++ b/llvm/tools/LLVMBuild.txt @@ -32,6 +32,7 @@ llvm-dwarfdump llvm-dwp llvm-elfabi + llvm-ifs llvm-exegesis llvm-extract llvm-jitlistener diff --git a/llvm/tools/llvm-ifs/CMakeLists.txt b/llvm/tools/llvm-ifs/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-ifs/CMakeLists.txt @@ -0,0 +1,10 @@ +set(LLVM_LINK_COMPONENTS + Object + Support + TextAPI + ObjectYAML + ) + +add_llvm_tool(llvm-ifs + llvm-ifs.cpp + ) diff --git a/llvm/tools/llvm-ifs/LLVMBuild.txt b/llvm/tools/llvm-ifs/LLVMBuild.txt new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-ifs/LLVMBuild.txt @@ -0,0 +1,21 @@ +;===- ./tools/llvm-ifs/LLVMBuild.txt ---------------------------*- Conf -*--===; +; +; Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +; See https://llvm.org/LICENSE.txt for license information. +; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +; +;===------------------------------------------------------------------------===; +; +; This is an LLVMBuild description file for the components in this subdirectory. +; +; For more information on the LLVMBuild system, please see: +; +; http://llvm.org/docs/LLVMBuild.html +; +;===------------------------------------------------------------------------===; + +[component_0] +type = Tool +name = llvm-ifs +parent = Tools +required_libraries = Object Support TextAPI diff --git a/llvm/tools/llvm-ifs/llvm-ifs.cpp b/llvm/tools/llvm-ifs/llvm-ifs.cpp new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-ifs/llvm-ifs.cpp @@ -0,0 +1,429 @@ +//===- llvm-ifs.cpp -------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===-----------------------------------------------------------------------===/ + +#include "llvm/ADT/StringRef.h" +#include "llvm/ADT/StringSwitch.h" +#include "llvm/ADT/Triple.h" +#include "llvm/ObjectYAML/yaml2obj.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/Errc.h" +#include "llvm/Support/Error.h" +#include "llvm/Support/FileOutputBuffer.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" +#include "llvm/Support/WithColor.h" +#include "llvm/Support/YAMLTraits.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/TextAPI/ELF/ELFStub.h" +#include "llvm/TextAPI/MachO/InterfaceFile.h" +#include "llvm/TextAPI/MachO/TextAPIReader.h" +#include "llvm/TextAPI/MachO/TextAPIWriter.h" +#include + +using namespace llvm; +using namespace llvm::yaml; +using namespace llvm::elfabi; +using namespace llvm::MachO; + +namespace { +const VersionTuple IFSVersionCurrent(1, 0); +}; + +static cl::opt Action("a", cl::desc(""), + cl::value_desc("write-ifs | write-bin"), + cl::init("write-ifs")); + +static cl::opt ForceFormat("f", cl::desc(""), + cl::value_desc("ELF | TBD"), + cl::init("")); + +static cl::list InputFilenames(cl::Positional, + cl::desc(""), + cl::ZeroOrMore); + +static cl::opt OutputFilename("o", cl::desc(""), + cl::value_desc("path")); + +/// YAML traits for ELFSymbolType. +template <> struct ScalarEnumerationTraits { + static void enumeration(IO &IO, ELFSymbolType &SymbolType) { + IO.enumCase(SymbolType, "NoType", ELFSymbolType::NoType); + IO.enumCase(SymbolType, "Func", ELFSymbolType::Func); + IO.enumCase(SymbolType, "Object", ELFSymbolType::Object); + IO.enumCase(SymbolType, "TLS", ELFSymbolType::TLS); + IO.enumCase(SymbolType, "Unknown", ELFSymbolType::Unknown); + // Treat other symbol types as noise, and map to Unknown. + if (!IO.outputting() && IO.matchEnumFallback()) + SymbolType = ELFSymbolType::Unknown; + } +}; + +template <> struct ScalarTraits { + static void output(const VersionTuple &Value, void *, + llvm::raw_ostream &Out) { + Out << Value.getAsString(); + } + + static StringRef input(StringRef Scalar, void *, VersionTuple &Value) { + if (Value.tryParse(Scalar)) + return StringRef("Can't parse version: invalid version format."); + + if (Value > IFSVersionCurrent) + return StringRef("Unsupported IFS version."); + + // Returning empty StringRef indicates successful parse. + return StringRef(); + } + + // Don't place quotation marks around version value. + static QuotingType mustQuote(StringRef) { return QuotingType::None; } +}; + +/// YAML traits for ELFSymbol. +template <> struct MappingTraits { + static void mapping(IO &IO, ELFSymbol &Symbol) { + IO.mapRequired("Type", Symbol.Type); + // The need for symbol size depends on the symbol type. + if (Symbol.Type == ELFSymbolType::NoType) + IO.mapOptional("Size", Symbol.Size, (uint64_t)0); + else if (Symbol.Type == ELFSymbolType::Func) + Symbol.Size = 0; + else + IO.mapRequired("Size", Symbol.Size); + IO.mapOptional("Undefined", Symbol.Undefined, false); + IO.mapOptional("Weak", Symbol.Weak, false); + IO.mapOptional("Warning", Symbol.Warning); + } + + // Compacts symbol information into a single line. + static const bool flow = true; +}; + +/// YAML traits for set of ELFSymbols. +template <> struct CustomMappingTraits> { + static void inputOne(IO &IO, StringRef Key, std::set &Set) { + std::string Name = Key.str(); + ELFSymbol Sym(Name); + IO.mapRequired(Name.c_str(), Sym); + Set.insert(Sym); + } + + static void output(IO &IO, std::set &Set) { + for (auto &Sym : Set) + IO.mapRequired(Sym.Name.c_str(), const_cast(Sym)); + } +}; + +// A cumulative representation of ELF stubs. +// Both textual and binary stubs will read into and write from this object. +class IFSStub { + // TODO: Add support for symbol versioning. +public: + VersionTuple IfsVersion; + std::string Triple; + std::string ObjectFileFormat; + Optional SOName; + std::vector NeededLibs; + std::set Symbols; + + IFSStub() = default; + IFSStub(const IFSStub &Stub) + : IfsVersion(Stub.IfsVersion), Triple(Stub.Triple), + ObjectFileFormat(Stub.ObjectFileFormat), SOName(Stub.SOName), + NeededLibs(Stub.NeededLibs), Symbols(Stub.Symbols) {} + IFSStub(IFSStub &&Stub) + : IfsVersion(std::move(Stub.IfsVersion)), Triple(std::move(Stub.Triple)), + ObjectFileFormat(std::move(Stub.ObjectFileFormat)), + SOName(std::move(Stub.SOName)), NeededLibs(std::move(Stub.NeededLibs)), + Symbols(std::move(Stub.Symbols)) {} +}; + +/// YAML traits for IFSStub objects. +template <> struct MappingTraits { + static void mapping(IO &IO, IFSStub &Stub) { + if (!IO.mapTag("!experimental-ifs-v1", true)) + IO.setError("Not a .ifs YAML file."); + IO.mapRequired("IfsVersion", Stub.IfsVersion); + IO.mapOptional("Triple", Stub.Triple); + IO.mapOptional("ObjectFileFormat", Stub.ObjectFileFormat); + IO.mapOptional("SOName", Stub.SOName); + IO.mapOptional("NeededLibs", Stub.NeededLibs); + IO.mapRequired("Symbols", Stub.Symbols); + } +}; + +static Expected> readInputFile(StringRef FilePath) { + // Read in file. + ErrorOr> BufOrError = + MemoryBuffer::getFileOrSTDIN(FilePath); + if (!BufOrError) + return createStringError(BufOrError.getError(), "Could not open `%s`", + FilePath.data()); + + std::unique_ptr FileReadBuffer = std::move(*BufOrError); + yaml::Input YamlIn(FileReadBuffer->getBuffer()); + std::unique_ptr Stub(new IFSStub()); + YamlIn >> *Stub; + + if (std::error_code Err = YamlIn.error()) + return createStringError(Err, "Failed reading Interface Stub File."); + + return std::move(Stub); +} + +int writeTbdStub(const llvm::Triple &T, const std::set &Symbols, + const StringRef Format, raw_ostream &Out) { + auto ArchOrError = + [](const llvm::Triple &T) -> llvm::Expected { + switch (T.getArch()) { + default: + return createStringError(errc::not_supported, "Invalid Architecture."); + case llvm::Triple::ArchType::x86: + return AK_i386; + case llvm::Triple::ArchType::x86_64: + return AK_x86_64; + case llvm::Triple::ArchType::arm: + return AK_armv7; + case llvm::Triple::ArchType::aarch64: + return AK_arm64; + } + }(T); + + if (!ArchOrError) + return -1; + + Architecture Arch = ArchOrError.get(); + + InterfaceFile File; + File.setFileType(FileType::TBD_V3); + File.setArchitectures(Arch); + File.setPlatform(PlatformKind::macOS); + + for (const auto &Symbol : Symbols) { + auto Name = Symbol.Name; + auto Kind = SymbolKind::GlobalSymbol; + switch (Symbol.Type) { + default: + case llvm::elfabi::ELFSymbolType::NoType: + Kind = SymbolKind::GlobalSymbol; + break; + case llvm::elfabi::ELFSymbolType::Object: + Kind = SymbolKind::GlobalSymbol; + break; + case llvm::elfabi::ELFSymbolType::Func: + Kind = SymbolKind::GlobalSymbol; + break; + } + if (Symbol.Weak) + File.addSymbol(Kind, Name, Arch, SymbolFlags::WeakDefined); + else + File.addSymbol(Kind, Name, Arch); + } + + SmallString<4096> Buffer; + raw_svector_ostream OS(Buffer); + if (Error Result = TextAPIWriter::writeToStream(OS, File)) + return -1; + Out << OS.str(); + return 0; +} + +int writeElfStub(const llvm::Triple &T, const std::set &Symbols, + const StringRef Format, raw_ostream &Out) { + SmallString<0> Storage; + Storage.clear(); + raw_svector_ostream OS(Storage); + + OS << "--- !ELF\n"; + OS << "FileHeader:\n"; + OS << " Class: ELFCLASS"; + OS << (T.isArch64Bit() ? "64" : "32"); + OS << "\n"; + OS << " Data: ELFDATA2"; + OS << (T.isLittleEndian() ? "LSB" : "MSB"); + OS << "\n"; + OS << " Type: ET_DYN\n"; + OS << " Machine: " + << llvm::StringSwitch(T.getArchName()) + .Case("x86_64", "EM_X86_64") + .Case("i386", "EM_386") + .Case("i686", "EM_386") + .Case("aarch64", "EM_AARCH64") + .Case("amdgcn", "EM_AMDGPU") + .Case("r600", "EM_AMDGPU") + .Case("arm", "EM_ARM") + .Case("thumb", "EM_ARM") + .Case("avr", "EM_AVR") + .Case("mips", "EM_MIPS") + .Case("mipsel", "EM_MIPS") + .Case("mips64", "EM_MIPS") + .Case("mips64el", "EM_MIPS") + .Case("msp430", "EM_MSP430") + .Case("ppc", "EM_PPC") + .Case("ppc64", "EM_PPC64") + .Case("ppc64le", "EM_PPC64") + .Case("x86", T.isOSIAMCU() ? "EM_IAMCU" : "EM_386") + .Case("x86_64", "EM_X86_64") + .Default("EM_NONE") + << "\nSymbols:\n"; + for (const auto &Symbol : Symbols) { + OS << " - Name: " << Symbol.Name << "\n" + << " Type: STT_"; + switch (Symbol.Type) { + default: + case llvm::elfabi::ELFSymbolType::NoType: + OS << "NOTYPE"; + break; + case llvm::elfabi::ELFSymbolType::Object: + OS << "OBJECT"; + break; + case llvm::elfabi::ELFSymbolType::Func: + OS << "FUNC"; + break; + } + OS << "\n Binding: STB_" << (Symbol.Weak ? "WEAK" : "GLOBAL") + << "\n"; + } + OS << "...\n"; + + yaml::Input YIn(OS.str()); + Error E = convertYAML(YIn, Out); + return !E ? -1 : 0; +} + +int writeIfso(const IFSStub &Stub, bool IsWriteIfs, raw_ostream &Out) { + if (IsWriteIfs) { + yaml::Output YamlOut(Out, NULL, /*WrapColumn =*/0); + YamlOut << const_cast(Stub); + return 0; + } + + std::string ObjectFileFormat = + ForceFormat.empty() ? Stub.ObjectFileFormat : ForceFormat; + + if (ObjectFileFormat == "ELF") + return writeElfStub(llvm::Triple(Stub.Triple), Stub.Symbols, + Stub.ObjectFileFormat, Out); + if (ObjectFileFormat == "TBD") + return writeTbdStub(llvm::Triple(Stub.Triple), Stub.Symbols, + Stub.ObjectFileFormat, Out); + + WithColor::error() + << "Invalid ObjectFileFormat: Only ELF and TBD are supported.\n"; + return -1; +} + +// New Interface Stubs Yaml Format: +// --- !experimental-ifs-v1 +// IfsVersion: 1.0 +// Triple: +// ObjectFileFormat: +// Symbols: +// _ZSymbolName: { Type: } +// ... + +int main(int argc, char *argv[]) { + // Parse arguments. + cl::ParseCommandLineOptions(argc, argv); + + if (InputFilenames.empty()) + InputFilenames.push_back("-"); + + IFSStub Stub; + std::map SymbolMap; + + for (const std::string &InputFilePath : InputFilenames) { + Expected> StubOrErr = readInputFile(InputFilePath); + if (!StubOrErr) { + WithColor::error() << StubOrErr.takeError() << "\n"; + return -1; + } + std::unique_ptr TargetStub = std::move(StubOrErr.get()); + + if (Stub.Triple.empty()) { + Stub.IfsVersion = TargetStub->IfsVersion; + Stub.Triple = TargetStub->Triple; + Stub.ObjectFileFormat = TargetStub->ObjectFileFormat; + Stub.SOName = TargetStub->SOName; + Stub.NeededLibs = TargetStub->NeededLibs; + } else { + if (Stub.IfsVersion != TargetStub->IfsVersion) { + WithColor::error() << "Interface Stub: IfsVersion Mismatch.\n"; + return -1; + } + if (Stub.Triple != TargetStub->Triple) { + WithColor::error() << "Interface Stub: Triple Mismatch.\n"; + return -1; + } + if (Stub.ObjectFileFormat != TargetStub->ObjectFileFormat) { + WithColor::error() << "Interface Stub: ObjectFileFormat Mismatch.\n"; + return -1; + } + if (Stub.SOName != TargetStub->SOName) { + WithColor::error() << "Interface Stub: SOName Mismatch.\n"; + return -1; + } + if (Stub.NeededLibs != TargetStub->NeededLibs) { + WithColor::error() << "Interface Stub: NeededLibs Mismatch.\n"; + return -1; + } + } + + for (auto Symbol : TargetStub->Symbols) { + auto SI = SymbolMap.find(Symbol.Name); + if (SI == SymbolMap.end()) { + SymbolMap.insert( + std::pair(Symbol.Name, Symbol)); + continue; + } + + // Check conflicts: + // Symbol.Name + if (Symbol.Size != SI->second.Size) { + WithColor::error() << "Interface Stub: Size Mismatch for" + + Symbol.Name + ".\n"; + return -1; + } + if (Symbol.Type != SI->second.Type) { + WithColor::error() << "Interface Stub: Type Mismatch for" + + Symbol.Name + ".\n"; + return -1; + } + if (Symbol.Undefined != SI->second.Undefined) { + WithColor::error() << "Interface Stub: Undefined Mismatch for" + + Symbol.Name + ".\n"; + return -1; + } + if (Symbol.Warning != SI->second.Warning) { + WithColor::error() << "Interface Stub: Warning Mismatch for" + + Symbol.Name + ".\n"; + return -1; + } + if (Symbol.Weak != SI->second.Weak) { + WithColor::error() << "Interface Stub: Weak Mismatch for" + + Symbol.Name + ".\n"; + return -1; + } + } + } + + for (auto &Entry : SymbolMap) + Stub.Symbols.insert(Entry.second); + + std::error_code SysErr; + + // Open file for writing. + raw_fd_ostream Out(OutputFilename, SysErr); + if (SysErr) { + WithColor::error() << "Couldn't open " << OutputFilename + << " for writing.\n"; + return -1; + } + + return writeIfso(Stub, (Action == "write-ifs"), Out); +}