LLVM  13.0.0git
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/StringRef.h"
11 #include "llvm/ADT/StringSwitch.h"
13 #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())
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 ELFSymbol.
73 template <> struct MappingTraits<ELFSymbol> {
74  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>> {
95  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 
101  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> {
109  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 
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 
139  yaml::Output YamlOut(OS, NULL, /*WrapColumn =*/0);
140 
141  YamlOut << const_cast<ELFStub &>(Stub);
142  return Error::success();
143 }
llvm::StringSwitch::Case
StringSwitch & Case(StringLiteral S, T Value)
Definition: StringSwitch.h:67
set
We currently generate a but we really shouldn eax ecx xorl edx divl ecx eax divl ecx movl eax ret A similar code sequence works for division We currently compile i32 v2 eax eax jo LBB1_2 atomic and others It is also currently not done for read modify write instructions It is also current not done if the OF or CF flags are needed The shift operators have the complication that when the shift count is EFLAGS is not set
Definition: README.txt:1277
llvm::elfabi::ELFStub::NeededLibs
std::vector< std::string > NeededLibs
Definition: ELFStub.h:56
TBEHandler.h
llvm::elfabi::ELFSymbolType
ELFSymbolType
Definition: ELFStub.h:27
llvm::yaml::MappingTraits< ELFStub >::mapping
static void mapping(IO &IO, ELFStub &Stub)
Definition: TBEHandler.cpp:109
llvm::elfabi::ELFSymbol
Definition: ELFStub.h:37
llvm
Definition: AllocatorList.h:23
llvm::elfabi
Definition: ELFObjHandler.h:25
llvm::yaml::CustomMappingTraits< std::set< ELFSymbol > >::inputOne
static void inputOne(IO &IO, StringRef Key, std::set< ELFSymbol > &Set)
Definition: TBEHandler.cpp:95
StringRef.h
llvm::StringSwitch::Default
LLVM_NODISCARD R Default(T Value)
Definition: StringSwitch.h:181
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:332
llvm::XCOFF::SymbolType
SymbolType
Definition: XCOFF.h:177
llvm::yaml::ScalarEnumerationTraits< ELFSymbolType >::enumeration
static void enumeration(IO &IO, ELFSymbolType &SymbolType)
Definition: TBEHandler.cpp:26
Error.h
llvm::elfabi::TBEVersionCurrent
const VersionTuple TBEVersionCurrent(1, 0)
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::yaml::MappingTraits
Definition: ModuleSummaryIndex.h:50
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:481
llvm::yaml::CustomMappingTraits< std::set< ELFSymbol > >::output
static void output(IO &IO, std::set< ELFSymbol > &Set)
Definition: TBEHandler.cpp:101
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:50
llvm::elfabi::ELFStub
Definition: ELFStub.h:50
llvm::None
const NoneType None
Definition: None.h:23
llvm::yaml::MappingTraits< ELFSymbol >::mapping
static void mapping(IO &IO, ELFSymbol &Symbol)
Definition: TBEHandler.cpp:74
llvm::ELF::EM_AARCH64
@ EM_AARCH64
Definition: ELF.h:279
llvm::elfabi::readTBEFromBuffer
Expected< std::unique_ptr< ELFStub > > readTBEFromBuffer(StringRef Buf)
Attempts to read an ELF interface file from a StringRef buffer.
Definition: TBEHandler.cpp:123
llvm::yaml::ScalarTraits< ELFArchMapper >::input
static StringRef input(StringRef Scalar, void *, ELFArchMapper &Value)
Definition: TBEHandler.cpp:56
llvm::elfabi::ELFStub::TbeVersion
VersionTuple TbeVersion
Definition: ELFStub.h:53
llvm::elfabi::ELFSymbolType::Object
@ Object
llvm::yaml::ScalarTraits< ELFArchMapper >::output
static void output(const ELFArchMapper &Value, void *, llvm::raw_ostream &Out)
Definition: TBEHandler.cpp:40
LLVM_YAML_STRONG_TYPEDEF
#define LLVM_YAML_STRONG_TYPEDEF(_base, _type)
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::ELF::EM_X86_64
@ EM_X86_64
Definition: ELF.h:177
llvm::elfabi::ELFStub::Arch
ELFArch Arch
Definition: ELFStub.h:55
llvm::elfabi::ELFSymbolType::Func
@ Func
llvm::elfabi::ELFSymbolType::NoType
@ NoType
llvm::elfabi::ELFSymbolType::Unknown
@ Unknown
YAMLTraits.h
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::make_error_code
std::error_code make_error_code(BitcodeError E)
Definition: BitcodeReader.h:270
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1202
std
Definition: BitVector.h:838
llvm::yaml::ScalarTraits< ELFArchMapper >::mustQuote
static QuotingType mustQuote(StringRef)
Definition: TBEHandler.cpp:69
uint16_t
llvm::elfabi::ELFStub::Symbols
std::set< ELFSymbol > Symbols
Definition: ELFStub.h:57
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
StringSwitch.h
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::elfabi::writeTBEToOutputStream
Error writeTBEToOutputStream(raw_ostream &OS, const ELFStub &Stub)
Attempts to write an ELF interface file to a raw_ostream.
Definition: TBEHandler.cpp:138
llvm::StringSwitch
A switch()-like statement whose cases are string literals.
Definition: StringSwitch.h:42
llvm::ELF::EM_NONE
@ EM_NONE
Definition: ELF.h:132
ELFStub.h
llvm::elfabi::ELFStub::SoName
Optional< std::string > SoName
Definition: ELFStub.h:54
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
llvm::elfabi::ELFSymbolType::TLS
@ TLS