diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index a409cd31af5..a75be0998d2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -69,6 +69,7 @@ set(LLVM_TEST_DEPENDS llvm-exegesis llvm-extract llvm-isel-fuzzer + llvm-ifs llvm-jitlink llvm-lib llvm-link diff --git a/test/lit.cfg.py b/test/lit.cfg.py index 9228200048c..6771b9e7fee 100644 --- a/test/lit.cfg.py +++ b/test/lit.cfg.py @@ -142,7 +142,7 @@ tools.extend([ 'dsymutil', 'lli', 'lli-child-target', 'llvm-ar', 'llvm-as', 'llvm-bcanalyzer', 'llvm-config', 'llvm-cov', 'llvm-cxxdump', 'llvm-cvtres', 'llvm-diff', 'llvm-dis', 'llvm-dwarfdump', 'llvm-exegesis', 'llvm-extract', - 'llvm-isel-fuzzer', 'llvm-jitlink', 'llvm-opt-fuzzer', 'llvm-lib', + 'llvm-isel-fuzzer', 'llvm-ifs', 'llvm-jitlink', 'llvm-opt-fuzzer', 'llvm-lib', 'llvm-link', 'llvm-lto', 'llvm-lto2', 'llvm-mc', 'llvm-mca', 'llvm-modextract', 'llvm-nm', 'llvm-objcopy', 'llvm-objdump', 'llvm-pdbutil', 'llvm-profdata', 'llvm-ranlib', 'llvm-rc', 'llvm-readelf', diff --git a/test/tools/llvm-ifs/conflict-header-format.ifs b/test/tools/llvm-ifs/conflict-header-format.ifs new file mode 100644 index 00000000000..40ae9c0526f --- /dev/null +++ b/test/tools/llvm-ifs/conflict-header-format.ifs @@ -0,0 +1,14 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/object.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# CHECK-IFS: error: Interface Stub: ObjectFileFormat Mismatch. +# CHECK-IFS-NEXT: Filenames: +# CHECK-IFS-NEXT: ObjectFileFormat Values: TBD ELF + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-apple-unknown +ObjectFileFormat: TBD +Symbols: + a: { Type: Func } +... diff --git a/test/tools/llvm-ifs/conflict-header-triple.ifs b/test/tools/llvm-ifs/conflict-header-triple.ifs new file mode 100644 index 00000000000..15bddc6a15f --- /dev/null +++ b/test/tools/llvm-ifs/conflict-header-triple.ifs @@ -0,0 +1,14 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/object.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# CHECK-IFS: error: Interface Stub: Triple Mismatch. +# CHECK-IFS-NEXT: Filenames: +# CHECK-IFS-NEXT: Triple Values: mips-unknown-linux x86_64-unknown-linux-gnu + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: mips-unknown-linux +ObjectFileFormat: ELF +Symbols: + a: { Type: Func } +... diff --git a/test/tools/llvm-ifs/conflict-header-version.ifs b/test/tools/llvm-ifs/conflict-header-version.ifs new file mode 100644 index 00000000000..addf9943441 --- /dev/null +++ b/test/tools/llvm-ifs/conflict-header-version.ifs @@ -0,0 +1,16 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/object.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# RUN: not llvm-ifs -action write-ifs -o - %s 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS2 + +# CHECK-IFS: error: Interface Stub: IfsVersion Mismatch. +# CHECK-IFS2: error: Interface Stub: Bad IfsVersion: 0.0, llvm-ifs supported version: 1.2. + +--- !experimental-ifs-v1 +IfsVersion: 0.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + a: { Type: Func } +... diff --git a/test/tools/llvm-ifs/conflict-size.ifs b/test/tools/llvm-ifs/conflict-size.ifs new file mode 100644 index 00000000000..173ce268c74 --- /dev/null +++ b/test/tools/llvm-ifs/conflict-size.ifs @@ -0,0 +1,16 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/object.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# Here we are testing to see if two symbols with identical names will fail to +# merge in conflict due to mismatching sizes. +# CHECK-IFS: error: Interface Stub: Size Mismatch for b. +# CHECK-IFS-NEXT: Filename: +# CHECK-IFS-NEXT: Size Values: 1 4 + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + b: { Type: Object, Size: 1 } +... diff --git a/test/tools/llvm-ifs/conflict-type.ifs b/test/tools/llvm-ifs/conflict-type.ifs new file mode 100644 index 00000000000..c518be4e141 --- /dev/null +++ b/test/tools/llvm-ifs/conflict-type.ifs @@ -0,0 +1,16 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/func.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# Here we are testing to see if two symbols with identical names will fail to +# merge in conflict due to mismatched types. +# CHECK-IFS: error: Interface Stub: Type Mismatch for a. +# CHECK-IFS-NEXT: Filename: +# CHECK-IFS-NEXT: Type Values: Object Func + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + a: { Type: Object, Size: 1 } +... diff --git a/test/tools/llvm-ifs/conflict-weak.ifs b/test/tools/llvm-ifs/conflict-weak.ifs new file mode 100644 index 00000000000..c7e604e6ad1 --- /dev/null +++ b/test/tools/llvm-ifs/conflict-weak.ifs @@ -0,0 +1,17 @@ +# RUN: not llvm-ifs -action write-ifs -o - %s %S/func.ifs 2>&1 | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# Here we are testing to see if two symbols with identical names will fail to +# merge in conflict due to one being weak and one not. Eventually this will work +# when llvm-ifs has support for resolving these kinds of conflicts. +# CHECK-IFS: error: Interface Stub: Weak Mismatch for a. +# CHECK-IFS-NEXT: Filename: +# CHECK-IFS-NEXT: Weak Values: 1 0 + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + a: { Type: Func, Weak: true } +... diff --git a/test/tools/llvm-ifs/func.ifs b/test/tools/llvm-ifs/func.ifs new file mode 100644 index 00000000000..496e2624192 --- /dev/null +++ b/test/tools/llvm-ifs/func.ifs @@ -0,0 +1,56 @@ +# RUN: llvm-ifs -action write-ifs -o - %s %S/object.ifs | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# RUN: llvm-ifs -action write-bin -o - %s %S/object.ifs | \ +# RUN: llvm-readelf --all | FileCheck %s --check-prefixes=CHECK-ELF + +# RUN: llvm-ifs -action write-bin -force-format TBD -o - %s %S/object.ifs | \ +# RUN: FileCheck %s --check-prefixes=CHECK-DARWIN-TBD3 + +# RUN: llvm-ifs -action write-ifs -o - %s %s | \ +# RUN: FileCheck %s --check-prefixes=CHECK-MERGE-IFS + +# CHECK-IFS: --- !experimental-ifs-v1 +# CHECK-IFS-NEXT: IfsVersion: 1.0 +# CHECK-IFS-NEXT: Triple: x86_64-unknown-linux-gnu +# CHECK-IFS-NEXT: ObjectFileFormat: ELF +# CHECK-IFS-NEXT: Symbols: +# CHECK-IFS-DAG: a: { Type: Func } +# CHECK-IFS-DAG: b: { Type: Object, Size: 4 } +# CHECK-IFS: ... + +# CHECK-ELF: ELF Header: +# CHECK-ELF: Class: ELF64 +# CHECK-ELF: Type: DYN (Shared object file) +# CHECK-ELF: FUNC GLOBAL DEFAULT 1 a +# CHECK-ELF: OBJECT GLOBAL DEFAULT 1 b + +# CHECK-DARWIN-TBD3: --- !tapi-tbd-v3 +# CHECK-DARWIN-TBD3-NEXT: archs: [ x86_64 ] +# CHECK-DARWIN-TBD3-NEXT: platform: macosx +# CHECK-DARWIN-TBD3-NEXT: flags: [ flat_namespace, not_app_extension_safe ] +# CHECK-DARWIN-TBD3-NEXT: install-name: '' +# CHECK-DARWIN-TBD3-NEXT: current-version: 0 +# CHECK-DARWIN-TBD3-NEXT: compatibility-version: 0 +# CHECK-DARWIN-TBD3-NEXT: objc-constraint: none +# CHECK-DARWIN-TBD3-NEXT: exports: +# CHECK-DARWIN-TBD3-NEXT: - archs: [ x86_64 ] +# CHECK-DARWIN-TBD3-NEXT: symbols: [ a, b ] +# CHECK-DARWIN-TBD3-NEXT: ... + +# Here we are testing to see if two identical symbols will merge. +# CHECK-MERGE-IFS: --- !experimental-ifs-v1 +# CHECK-MERGE-IFS-NEXT: IfsVersion: 1.0 +# CHECK-MERGE-IFS-NEXT: Triple: x86_64-unknown-linux-gnu +# CHECK-MERGE-IFS-NEXT: ObjectFileFormat: ELF +# CHECK-MERGE-IFS-NEXT: Symbols: +# CHECK-MERGE-IFS-NEXT: a: { Type: Func } +# CHECK-MERGE-IFS-NEXT: ... + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + a: { Type: Func } +... diff --git a/test/tools/llvm-ifs/object-function-size-weak-combo.ifs b/test/tools/llvm-ifs/object-function-size-weak-combo.ifs new file mode 100644 index 00000000000..b6328fbc58d --- /dev/null +++ b/test/tools/llvm-ifs/object-function-size-weak-combo.ifs @@ -0,0 +1,34 @@ +# RUN: llvm-ifs -action write-ifs -o - %s %S/func.ifs %S/object.ifs %S/weak.ifs | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# RUN: llvm-ifs -action write-bin -o - %s %S/func.ifs %S/object.ifs %S/weak.ifs | \ +# RUN: llvm-readelf --all | FileCheck %s --check-prefixes=CHECK-ELF + +# CHECK-IFS: --- !experimental-ifs-v1 +# CHECK-IFS-NEXT: IfsVersion: 1.0 +# CHECK-IFS-NEXT: Triple: x86_64-unknown-linux-gnu +# CHECK-IFS-NEXT: ObjectFileFormat: ELF +# CHECK-IFS-NEXT: Symbols: +# CHECK-IFS-DAG: e: { Type: Object, Size: 8 } +# CHECK-IFS-DAG: a: { Type: Func } +# CHECK-IFS-DAG: f: { Type: Object, Size: 2 } +# CHECK-IFS-DAG: _Z10strongFuncv: { Type: Func } +# CHECK-IFS-DAG: _Z8weakFuncv: { Type: Func, Weak: true } +# CHECK-IFS-DAG: b: { Type: Object, Size: 4 } +# CHECK-IFS: ... + +# CHECK-ELF: FUNC GLOBAL DEFAULT 1 _Z10strongFuncv +# CHECK-ELF: FUNC WEAK DEFAULT 1 _Z8weakFuncv +# CHECK-ELF: FUNC GLOBAL DEFAULT 1 a +# CHECK-ELF: OBJECT GLOBAL DEFAULT 1 b +# CHECK-ELF: OBJECT GLOBAL DEFAULT 1 e +# CHECK-ELF: OBJECT GLOBAL DEFAULT 1 f + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + e: { Type: Object, Size: 8 } + f: { Type: Object, Size: 2 } +... diff --git a/test/tools/llvm-ifs/object.ifs b/test/tools/llvm-ifs/object.ifs new file mode 100644 index 00000000000..733cc38001d --- /dev/null +++ b/test/tools/llvm-ifs/object.ifs @@ -0,0 +1,28 @@ +# RUN: llvm-ifs -action write-ifs -o - %s | \ +# RUN: FileCheck %s --check-prefixes=CHECK-IFS + +# RUN: llvm-ifs -action write-bin -o - %s | \ +# RUN: llvm-readelf --all | FileCheck %s --check-prefixes=CHECK-ELF + +# CHECK-IFS: --- !experimental-ifs-v1 +# CHECK-IFS-NEXT: IfsVersion: 1.0 +# CHECK-IFS-NEXT: Triple: x86_64-unknown-linux-gnu +# CHECK-IFS-NEXT: ObjectFileFormat: ELF +# CHECK-IFS-NEXT: Symbols: +# CHECK-IFS-NEXT: b: { Type: Object, Size: 4 } +# CHECK-IFS-NEXT: ... + +# CHECK-ELF: ELF Header: +# CHECK-ELF: Class: ELF64 +# CHECK-ELF: Data: 2's complement, little endian +# CHECK-ELF: Type: DYN (Shared object file) +# CHECK-ELF-NOT: FUNC GLOBAL DEFAULT 1 a +# CHECK-ELF: OBJECT GLOBAL DEFAULT 1 b + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + b: { Type: Object, Size: 4 } +... diff --git a/test/tools/llvm-ifs/version-ok.ifs b/test/tools/llvm-ifs/version-ok.ifs new file mode 100644 index 00000000000..fd150ee77d5 --- /dev/null +++ b/test/tools/llvm-ifs/version-ok.ifs @@ -0,0 +1,9 @@ +# RUN: llvm-ifs -action write-ifs -o - %s %S/object.ifs + +--- !experimental-ifs-v1 +IfsVersion: 1.1 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + a: { Type: Func } +... diff --git a/test/tools/llvm-ifs/weak.ifs b/test/tools/llvm-ifs/weak.ifs new file mode 100644 index 00000000000..a7441be1c1f --- /dev/null +++ b/test/tools/llvm-ifs/weak.ifs @@ -0,0 +1,19 @@ +# RUN: llvm-ifs -action write-ifs -o - %s | FileCheck %s --check-prefixes=CHECK-IFS + +# CHECK-IFS: --- !experimental-ifs-v1 +# CHECK-IFS-NEXT: IfsVersion: 1.0 +# CHECK-IFS-NEXT: Triple: x86_64-unknown-linux-gnu +# CHECK-IFS-NEXT: ObjectFileFormat: ELF +# CHECK-IFS-NEXT: Symbols: +# CHECK-IFS-DAG: _Z8weakFuncv: { Type: Func, Weak: true } +# CHECK-IFS-DAG: _Z10strongFuncv: { Type: Func } +# CHECK-IFS: ... + +--- !experimental-ifs-v1 +IfsVersion: 1.0 +Triple: x86_64-unknown-linux-gnu +ObjectFileFormat: ELF +Symbols: + _Z8weakFuncv: { Type: Func, Weak: true } + _Z10strongFuncv: { Type: Func } +... diff --git a/tools/LLVMBuild.txt b/tools/LLVMBuild.txt index b5c14b92edd..8f9aaa5349e 100644 --- a/tools/LLVMBuild.txt +++ b/tools/LLVMBuild.txt @@ -32,6 +32,7 @@ subdirectories = llvm-dwarfdump llvm-dwp llvm-elfabi + llvm-ifs llvm-exegesis llvm-extract llvm-jitlistener diff --git a/tools/llvm-ifs/CMakeLists.txt b/tools/llvm-ifs/CMakeLists.txt new file mode 100644 index 00000000000..544b0e41a5e --- /dev/null +++ b/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/tools/llvm-ifs/LLVMBuild.txt b/tools/llvm-ifs/LLVMBuild.txt new file mode 100644 index 00000000000..10dc6bd8f55 --- /dev/null +++ b/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/tools/llvm-ifs/llvm-ifs.cpp b/tools/llvm-ifs/llvm-ifs.cpp new file mode 100644 index 00000000000..8963a8a1347 --- /dev/null +++ b/tools/llvm-ifs/llvm-ifs.cpp @@ -0,0 +1,520 @@ +//===- 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/Debug.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/VersionTuple.h" +#include "llvm/Support/WithColor.h" +#include "llvm/Support/YAMLTraits.h" +#include "llvm/Support/raw_ostream.h" +#include "llvm/TextAPI/MachO/InterfaceFile.h" +#include "llvm/TextAPI/MachO/TextAPIReader.h" +#include "llvm/TextAPI/MachO/TextAPIWriter.h" +#include +#include + +using namespace llvm; +using namespace llvm::yaml; +using namespace llvm::MachO; + +#define DEBUG_TYPE "llvm-ifs" + +namespace { +const VersionTuple IFSVersionCurrent(1, 2); +}; + +static cl::opt Action("action", cl::desc(""), + cl::value_desc("write-ifs | write-bin"), + cl::init("write-ifs")); + +static cl::opt ForceFormat("force-format", + 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")); + +enum class IFSSymbolType { + NoType = 0, + Object, + Func, + // Type information is 4 bits, so 16 is safely out of range. + Unknown = 16, +}; + +std::string getTypeName(IFSSymbolType Type) { + switch (Type) { + case IFSSymbolType::NoType: + return "NoType"; + case IFSSymbolType::Func: + return "Func"; + case IFSSymbolType::Object: + return "Object"; + case IFSSymbolType::Unknown: + return "Unknown"; + } +} + +struct IFSSymbol { + IFSSymbol(std::string SymbolName) : Name(SymbolName) {} + std::string Name; + uint64_t Size; + IFSSymbolType Type; + bool Weak; + Optional Warning; + bool operator<(const IFSSymbol &RHS) const { return Name < RHS.Name; } +}; + +/// YAML traits for IFSSymbolType. +template <> struct ScalarEnumerationTraits { + static void enumeration(IO &IO, IFSSymbolType &SymbolType) { + IO.enumCase(SymbolType, "NoType", IFSSymbolType::NoType); + IO.enumCase(SymbolType, "Func", IFSSymbolType::Func); + IO.enumCase(SymbolType, "Object", IFSSymbolType::Object); + IO.enumCase(SymbolType, "Unknown", IFSSymbolType::Unknown); + // Treat other symbol types as noise, and map to Unknown. + if (!IO.outputting() && IO.matchEnumFallback()) + SymbolType = IFSSymbolType::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 IFSSymbol. +template <> struct MappingTraits { + static void mapping(IO &IO, IFSSymbol &Symbol) { + IO.mapRequired("Type", Symbol.Type); + // The need for symbol size depends on the symbol type. + if (Symbol.Type == IFSSymbolType::NoType) + IO.mapOptional("Size", Symbol.Size, (uint64_t)0); + else if (Symbol.Type == IFSSymbolType::Func) + Symbol.Size = 0; + else + IO.mapRequired("Size", Symbol.Size); + 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 IFSSymbols. +template <> struct CustomMappingTraits> { + static void inputOne(IO &IO, StringRef Key, std::set &Set) { + std::string Name = Key.str(); + IFSSymbol 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 IFSSymbolType::NoType: + Kind = SymbolKind::GlobalSymbol; + break; + case IFSSymbolType::Object: + Kind = SymbolKind::GlobalSymbol; + break; + case IFSSymbolType::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") + << "\nSections:" + << "\n - Name: .text" + << "\n Type: SHT_PROGBITS" + << "\n - Name: .data" + << "\n Type: SHT_PROGBITS" + << "\n - Name: .rodata" + << "\n Type: SHT_PROGBITS" + << "\nSymbols:\n"; + for (const auto &Symbol : Symbols) { + OS << " - Name: " << Symbol.Name << "\n" + << " Type: STT_"; + switch (Symbol.Type) { + default: + case IFSSymbolType::NoType: + OS << "NOTYPE"; + break; + case IFSSymbolType::Object: + OS << "OBJECT"; + break; + case IFSSymbolType::Func: + OS << "FUNC"; + break; + } + OS << "\n Section: .text" + << "\n Binding: STB_" << (Symbol.Weak ? "WEAK" : "GLOBAL") + << "\n"; + } + OS << "...\n"; + + std::string YamlStr = OS.str(); + + // Only or debugging. Not an offical format. + LLVM_DEBUG({ + if (ForceFormat == "ELFOBJYAML") { + Out << YamlStr; + return 0; + } + }); + + yaml::Input YIn(YamlStr); + if (Error E = convertYAML(YIn, Out)) { + logAllUnhandledErrors(std::move(E), WithColor::error(errs(), "llvm-ifs")); + return 1; + } + + return 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" || ForceFormat == "ELFOBJYAML") + 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; + + std::string PreviousInputFilePath = ""; + 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()) { + PreviousInputFilePath = InputFilePath; + 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) { + if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) { + WithColor::error() + << "Interface Stub: IfsVersion Mismatch." + << "\nFilenames: " << PreviousInputFilePath << " " + << InputFilePath << "\nIfsVersion Values: " << Stub.IfsVersion + << " " << TargetStub->IfsVersion << "\n"; + return -1; + } + if (TargetStub->IfsVersion > Stub.IfsVersion) + Stub.IfsVersion = TargetStub->IfsVersion; + } + if (Stub.ObjectFileFormat != TargetStub->ObjectFileFormat) { + WithColor::error() << "Interface Stub: ObjectFileFormat Mismatch." + << "\nFilenames: " << PreviousInputFilePath << " " + << InputFilePath << "\nObjectFileFormat Values: " + << Stub.ObjectFileFormat << " " + << TargetStub->ObjectFileFormat << "\n"; + return -1; + } + if (Stub.Triple != TargetStub->Triple) { + WithColor::error() << "Interface Stub: Triple Mismatch." + << "\nFilenames: " << PreviousInputFilePath << " " + << InputFilePath + << "\nTriple Values: " << Stub.Triple << " " + << TargetStub->Triple << "\n"; + return -1; + } + if (Stub.SOName != TargetStub->SOName) { + WithColor::error() << "Interface Stub: SOName Mismatch." + << "\nFilenames: " << PreviousInputFilePath << " " + << InputFilePath + << "\nSOName Values: " << Stub.SOName << " " + << TargetStub->SOName << "\n"; + return -1; + } + if (Stub.NeededLibs != TargetStub->NeededLibs) { + WithColor::error() << "Interface Stub: NeededLibs Mismatch." + << "\nFilenames: " << PreviousInputFilePath << " " + << InputFilePath << "\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; + } + + assert(Symbol.Name == SI->second.Name && "Symbol Names Must Match."); + + // Check conflicts: + if (Symbol.Type != SI->second.Type) { + WithColor::error() << "Interface Stub: Type Mismatch for " + << Symbol.Name << ".\nFilename: " << InputFilePath + << "\nType Values: " << getTypeName(SI->second.Type) + << " " << getTypeName(Symbol.Type) << "\n"; + + return -1; + } + if (Symbol.Size != SI->second.Size) { + WithColor::error() << "Interface Stub: Size Mismatch for " + << Symbol.Name << ".\nFilename: " << InputFilePath + << "\nSize Values: " << SI->second.Size << " " + << Symbol.Size << "\n"; + + return -1; + } + if (Symbol.Weak != SI->second.Weak) { + // TODO: Add conflict resolution for Weak vs non-Weak. + WithColor::error() << "Interface Stub: Weak Mismatch for " + << Symbol.Name << ".\nFilename: " << InputFilePath + << "\nWeak Values: " << SI->second.Weak << " " + << Symbol.Weak << "\n"; + + return -1; + } + // TODO: Not checking Warning. Will be dropped. + } + + PreviousInputFilePath = InputFilePath; + } + + if (Stub.IfsVersion != IFSVersionCurrent) + if (Stub.IfsVersion.getMajor() != IFSVersionCurrent.getMajor()) { + WithColor::error() << "Interface Stub: Bad IfsVersion: " + << Stub.IfsVersion << ", llvm-ifs supported version: " + << IFSVersionCurrent << ".\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); +}