LLVM  10.0.0svn
TBEHandler.cpp
Go to the documentation of this file.
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 
10 #include "llvm/ADT/StringSwitch.h"
11 #include "llvm/ADT/StringRef.h"
12 #include "llvm/Support/Error.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> {
26  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> {
40  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;
48  Out << "AArch64";
49  break;
50  case (ELFArch)ELF::EM_NONE:
51  default:
52  Out << "Unknown";
53  }
54  }
55 
56  static StringRef input(StringRef Scalar, void *, ELFArchMapper &Value) {
57  // Map from architecture string to integer.
59  .Case("x86_64", ELF::EM_X86_64)
60  .Case("AArch64", ELF::EM_AARCH64)
61  .Case("Unknown", 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.
69  static QuotingType mustQuote(StringRef) { return QuotingType::None; }
70 };
71 
72 /// YAML traits for TbeVersion.
73 template <> struct ScalarTraits<VersionTuple> {
74  static void output(const VersionTuple &Value, void *,
75  llvm::raw_ostream &Out) {
76  Out << Value.getAsString();
77  }
78 
80  if (Value.tryParse(Scalar))
81  return StringRef("Can't parse version: invalid version format.");
82 
83  if (Value > TBEVersionCurrent)
84  return StringRef("Unsupported TBE version.");
85 
86  // Returning empty StringRef indicates successful parse.
87  return StringRef();
88  }
89 
90  // Don't place quotation marks around version value.
91  static QuotingType mustQuote(StringRef) { return QuotingType::None; }
92 };
93 
94 /// YAML traits for ELFSymbol.
95 template <> struct MappingTraits<ELFSymbol> {
96  static void mapping(IO &IO, ELFSymbol &Symbol) {
97  IO.mapRequired("Type", Symbol.Type);
98  // The need for symbol size depends on the symbol type.
99  if (Symbol.Type == ELFSymbolType::NoType) {
100  IO.mapOptional("Size", Symbol.Size, (uint64_t)0);
101  } else if (Symbol.Type == ELFSymbolType::Func) {
102  Symbol.Size = 0;
103  } else {
104  IO.mapRequired("Size", Symbol.Size);
105  }
106  IO.mapOptional("Undefined", Symbol.Undefined, false);
107  IO.mapOptional("Weak", Symbol.Weak, false);
108  IO.mapOptional("Warning", Symbol.Warning);
109  }
110 
111  // Compacts symbol information into a single line.
112  static const bool flow = true;
113 };
114 
115 /// YAML traits for set of ELFSymbols.
116 template <> struct CustomMappingTraits<std::set<ELFSymbol>> {
117  static void inputOne(IO &IO, StringRef Key, std::set<ELFSymbol> &Set) {
118  ELFSymbol Sym(Key.str());
119  IO.mapRequired(Key.str().c_str(), Sym);
120  Set.insert(Sym);
121  }
122 
123  static void output(IO &IO, std::set<ELFSymbol> &Set) {
124  for (auto &Sym : Set)
125  IO.mapRequired(Sym.Name.c_str(), const_cast<ELFSymbol &>(Sym));
126  }
127 };
128 
129 /// YAML traits for ELFStub objects.
130 template <> struct MappingTraits<ELFStub> {
131  static void mapping(IO &IO, ELFStub &Stub) {
132  if (!IO.mapTag("!tapi-tbe", true))
133  IO.setError("Not a .tbe YAML file.");
134  IO.mapRequired("TbeVersion", Stub.TbeVersion);
135  IO.mapOptional("SoName", Stub.SoName);
136  IO.mapRequired("Arch", (ELFArchMapper &)Stub.Arch);
137  IO.mapOptional("NeededLibs", Stub.NeededLibs);
138  IO.mapRequired("Symbols", Stub.Symbols);
139  }
140 };
141 
142 } // end namespace yaml
143 } // end namespace llvm
144 
146  yaml::Input YamlIn(Buf);
147  std::unique_ptr<ELFStub> Stub(new ELFStub());
148  YamlIn >> *Stub;
149  if (std::error_code Err = YamlIn.error())
150  return createStringError(Err, "YAML failed reading as TBE");
151 
152  return std::move(Stub);
153 }
154 
156  yaml::Output YamlOut(OS, NULL, /*WrapColumn =*/0);
157 
158  YamlOut << const_cast<ELFStub &>(Stub);
159  return Error::success();
160 }
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:218
static void output(const ELFArchMapper &Value, void *, llvm::raw_ostream &Out)
Definition: TBEHandler.cpp:40
std::set< ELFSymbol > Symbols
Definition: ELFStub.h:59
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static void mapping(IO &IO, ELFStub &Stub)
Definition: TBEHandler.cpp:131
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
Definition: BitVector.h:937
Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub)
Attempts to write an ELF interface file to a raw_ostream.
Definition: TBEHandler.cpp:155
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
Optional< std::string > Warning
Definition: ELFStub.h:44
static QuotingType mustQuote(StringRef)
Definition: TBEHandler.cpp:69
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
static void mapping(IO &IO, ELFSymbol &Symbol)
Definition: TBEHandler.cpp:96
Key
PAL metadata keys.
Optional< std::string > SoName
Definition: ELFStub.h:56
static QuotingType mustQuote(StringRef)
Definition: TBEHandler.cpp:91
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
static void inputOne(IO &IO, StringRef Key, std::set< ELFSymbol > &Set)
Definition: TBEHandler.cpp:117
static StringRef input(StringRef Scalar, void *, VersionTuple &Value)
Definition: TBEHandler.cpp:79
std::vector< std::string > NeededLibs
Definition: ELFStub.h:58
ELFSymbolType Type
Definition: ELFStub.h:41
const VersionTuple TBEVersionCurrent(1, 0)
uint16_t ELFArch
Definition: ELFStub.h:25
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
bool tryParse(StringRef string)
Try to parse the given string as a version number.
static void output(const VersionTuple &Value, void *, llvm::raw_ostream &Out)
Definition: TBEHandler.cpp:74
This file declares an interface for reading and writing .tbe (text-based ELF) files.
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:26
std::string getAsString() const
Retrieve a string representation of the version number.
This file defines an internal representation of an ELF stub.
static void enumeration(IO &IO, ELFSymbolType &SymbolType)
Definition: TBEHandler.cpp:26
static void output(IO &IO, std::set< ELFSymbol > &Set)
Definition: TBEHandler.cpp:123
LLVM Value Representation.
Definition: Value.h:72
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
static StringRef input(StringRef Scalar, void *, ELFArchMapper &Value)
Definition: TBEHandler.cpp:56
Expected< std::unique_ptr< ELFStub > > readTBEFromBuffer(StringRef Buf)
Attempts to read an ELF interface file from a StringRef buffer.
Definition: TBEHandler.cpp:145
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1163
VersionTuple TbeVersion
Definition: ELFStub.h:55