1 //===- TBEHandler.cpp -----------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===-----------------------------------------------------------------------===/
8
9 #include "llvm/InterfaceStub/TBEHandler.h"
10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/InterfaceStub/ELFStub.h"
13 #include "llvm/Support/Error.h"
14 #include "llvm/Support/YAMLTraits.h"
15
16 using namespace llvm;
17 using namespace llvm::elfabi;
18
19 LLVM_YAML_STRONG_TYPEDEF(ELFArch, ELFArchMapper)
20
21 namespace llvm {
22 namespace yaml {
23
24 /// YAML traits for ELFSymbolType.
25 template <> struct ScalarEnumerationTraits<ELFSymbolType> {
enumerationllvm::yaml::ScalarEnumerationTraits26 static void enumeration(IO &IO, ELFSymbolType &SymbolType) {
27 IO.enumCase(SymbolType, "NoType", ELFSymbolType::NoType);
28 IO.enumCase(SymbolType, "Func", ELFSymbolType::Func);
29 IO.enumCase(SymbolType, "Object", ELFSymbolType::Object);
30 IO.enumCase(SymbolType, "TLS", ELFSymbolType::TLS);
31 IO.enumCase(SymbolType, "Unknown", ELFSymbolType::Unknown);
32 // Treat other symbol types as noise, and map to Unknown.
33 if (!IO.outputting() && IO.matchEnumFallback())
34 SymbolType = ELFSymbolType::Unknown;
35 }
36 };
37
38 /// YAML traits for ELFArch.
39 template <> struct ScalarTraits<ELFArchMapper> {
outputllvm::yaml::ScalarTraits40 static void output(const ELFArchMapper &Value, void *,
41 llvm::raw_ostream &Out) {
42 // Map from integer to architecture string.
43 switch (Value) {
44 case (ELFArch)ELF::EM_X86_64:
45 Out << "x86_64";
46 break;
47 case (ELFArch)ELF::EM_AARCH64:
48 Out << "AArch64";
49 break;
50 case (ELFArch)ELF::EM_NONE:
51 default:
52 Out << "Unknown";
53 }
54 }
55
inputllvm::yaml::ScalarTraits56 static StringRef input(StringRef Scalar, void *, ELFArchMapper &Value) {
57 // Map from architecture string to integer.
58 Value = StringSwitch<ELFArch>(Scalar)
59 .Case("x86_64", ELF::EM_X86_64)
60 .Case("AArch64", ELF::EM_AARCH64)
61 .Case("Unknown", ELF::EM_NONE)
62 .Default(ELF::EM_NONE);
63
64 // Returning empty StringRef indicates successful parse.
65 return StringRef();
66 }
67
68 // Don't place quotation marks around architecture value.
mustQuotellvm::yaml::ScalarTraits69 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
70 };
71
72 /// YAML traits for ELFSymbol.
73 template <> struct MappingTraits<ELFSymbol> {
mappingllvm::yaml::MappingTraits74 static void mapping(IO &IO, ELFSymbol &Symbol) {
75 IO.mapRequired("Type", Symbol.Type);
76 // The need for symbol size depends on the symbol type.
77 if (Symbol.Type == ELFSymbolType::NoType) {
78 IO.mapOptional("Size", Symbol.Size, (uint64_t)0);
79 } else if (Symbol.Type == ELFSymbolType::Func) {
80 Symbol.Size = 0;
81 } else {
82 IO.mapRequired("Size", Symbol.Size);
83 }
84 IO.mapOptional("Undefined", Symbol.Undefined, false);
85 IO.mapOptional("Weak", Symbol.Weak, false);
86 IO.mapOptional("Warning", Symbol.Warning);
87 }
88
89 // Compacts symbol information into a single line.
90 static const bool flow = true;
91 };
92
93 /// YAML traits for set of ELFSymbols.
94 template <> struct CustomMappingTraits<std::set<ELFSymbol>> {
inputOnellvm::yaml::CustomMappingTraits95 static void inputOne(IO &IO, StringRef Key, std::set<ELFSymbol> &Set) {
96 ELFSymbol Sym(Key.str());
97 IO.mapRequired(Key.str().c_str(), Sym);
98 Set.insert(Sym);
99 }
100
outputllvm::yaml::CustomMappingTraits101 static void output(IO &IO, std::set<ELFSymbol> &Set) {
102 for (auto &Sym : Set)
103 IO.mapRequired(Sym.Name.c_str(), const_cast<ELFSymbol &>(Sym));
104 }
105 };
106
107 /// YAML traits for ELFStub objects.
108 template <> struct MappingTraits<ELFStub> {
mappingllvm::yaml::MappingTraits109 static void mapping(IO &IO, ELFStub &Stub) {
110 if (!IO.mapTag("!tapi-tbe", true))
111 IO.setError("Not a .tbe YAML file.");
112 IO.mapRequired("TbeVersion", Stub.TbeVersion);
113 IO.mapOptional("SoName", Stub.SoName);
114 IO.mapRequired("Arch", (ELFArchMapper &)Stub.Arch);
115 IO.mapOptional("NeededLibs", Stub.NeededLibs);
116 IO.mapRequired("Symbols", Stub.Symbols);
117 }
118 };
119
120 } // end namespace yaml
121 } // end namespace llvm
122
readTBEFromBuffer(StringRef Buf)123 Expected<std::unique_ptr<ELFStub>> elfabi::readTBEFromBuffer(StringRef Buf) {
124 yaml::Input YamlIn(Buf);
125 std::unique_ptr<ELFStub> Stub(new ELFStub());
126 YamlIn >> *Stub;
127 if (std::error_code Err = YamlIn.error())
128 return createStringError(Err, "YAML failed reading as TBE");
129
130 if (Stub->TbeVersion > elfabi::TBEVersionCurrent)
131 return make_error<StringError>(
132 "TBE version " + Stub->TbeVersion.getAsString() + " is unsupported.",
133 std::make_error_code(std::errc::invalid_argument));
134
135 return std::move(Stub);
136 }
137
writeTBEToOutputStream(raw_ostream & OS,const ELFStub & Stub)138 Error elfabi::writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub) {
139 yaml::Output YamlOut(OS, NULL, /*WrapColumn =*/0);
140
141 YamlOut << const_cast<ELFStub &>(Stub);
142 return Error::success();
143 }
144