1 //===- IFSStub.h ------------------------------------------------*- C++ -*-===//
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 /// \file
10 /// This file defines an internal representation of an InterFace Stub.
11 ///
12 //===-----------------------------------------------------------------------===/
13 
14 #ifndef LLVM_INTERFACESTUB_IFSSTUB_H
15 #define LLVM_INTERFACESTUB_IFSSTUB_H
16 
17 #include "llvm/ADT/Optional.h"
18 #include "llvm/Support/VersionTuple.h"
19 #include <vector>
20 
21 namespace llvm {
22 namespace ifs {
23 
24 typedef uint16_t IFSArch;
25 
26 enum class IFSSymbolType {
27   NoType,
28   Object,
29   Func,
30   TLS,
31 
32   // Type information is 4 bits, so 16 is safely out of range.
33   Unknown = 16,
34 };
35 
36 enum class IFSEndiannessType {
37   Little,
38   Big,
39 
40   // Endianness info is 1 bytes, 256 is safely out of range.
41   Unknown = 256,
42 };
43 
44 enum class IFSBitWidthType {
45   IFS32,
46   IFS64,
47 
48   // Bit width info is 1 bytes, 256 is safely out of range.
49   Unknown = 256,
50 };
51 
52 struct IFSSymbol {
53   IFSSymbol() = default;
54   explicit IFSSymbol(std::string SymbolName) : Name(std::move(SymbolName)) {}
55   std::string Name;
56   Optional<uint64_t> Size;
57   IFSSymbolType Type;
58   bool Undefined;
59   bool Weak;
60   Optional<std::string> Warning;
61   bool operator<(const IFSSymbol &RHS) const { return Name < RHS.Name; }
62 };
63 
64 struct IFSTarget {
65   Optional<std::string> Triple;
66   Optional<std::string> ObjectFormat;
67   Optional<IFSArch> Arch;
68   Optional<std::string> ArchString;
69   Optional<IFSEndiannessType> Endianness;
70   Optional<IFSBitWidthType> BitWidth;
71 
72   bool empty();
73 };
74 
75 inline bool operator==(const IFSTarget &Lhs, const IFSTarget &Rhs) {
76   if (Lhs.Arch != Rhs.Arch || Lhs.BitWidth != Rhs.BitWidth ||
77       Lhs.Endianness != Rhs.Endianness ||
78       Lhs.ObjectFormat != Rhs.ObjectFormat || Lhs.Triple != Rhs.Triple)
79     return false;
80   return true;
81 }
82 
83 inline bool operator!=(const IFSTarget &Lhs, const IFSTarget &Rhs) {
84   return !(Lhs == Rhs);
85 }
86 
87 // A cumulative representation of InterFace stubs.
88 // Both textual and binary stubs will read into and write from this object.
89 struct IFSStub {
90   // TODO: Add support for symbol versioning.
91   VersionTuple IfsVersion;
92   Optional<std::string> SoName;
93   IFSTarget Target;
94   std::vector<std::string> NeededLibs;
95   std::vector<IFSSymbol> Symbols;
96 
97   IFSStub() = default;
98   IFSStub(const IFSStub &Stub);
99   IFSStub(IFSStub &&Stub);
100 };
101 
102 // Create a alias class for IFSStub.
103 // LLVM's YAML library does not allow mapping a class with 2 traits,
104 // which prevents us using 'Target:' field with different definitions.
105 // This class makes it possible to map a second traits so the same data
106 // structure can be used for 2 different yaml schema.
107 struct IFSStubTriple : IFSStub {
108   IFSStubTriple() = default;
109   IFSStubTriple(const IFSStub &Stub);
110   IFSStubTriple(const IFSStubTriple &Stub);
111   IFSStubTriple(IFSStubTriple &&Stub);
112 };
113 
114 /// This function convert bit width type from IFS enum to ELF format
115 /// Currently, ELFCLASS32 and ELFCLASS64 are supported.
116 ///
117 /// @param BitWidth IFS bit width type.
118 uint8_t convertIFSBitWidthToELF(IFSBitWidthType BitWidth);
119 
120 /// This function convert endianness type from IFS enum to ELF format
121 /// Currently, ELFDATA2LSB and ELFDATA2MSB are supported.
122 ///
123 /// @param Endianness IFS endianness type.
124 uint8_t convertIFSEndiannessToELF(IFSEndiannessType Endianness);
125 
126 /// This function convert symbol type from IFS enum to ELF format
127 /// Currently, STT_NOTYPE, STT_OBJECT, STT_FUNC, and STT_TLS are supported.
128 ///
129 /// @param SymbolType IFS symbol type.
130 uint8_t convertIFSSymbolTypeToELF(IFSSymbolType SymbolType);
131 
132 /// This function extracts ELF bit width from e_ident[EI_CLASS] of an ELF file
133 /// Currently, ELFCLASS32 and ELFCLASS64 are supported.
134 /// Other endianness types are mapped to IFSBitWidthType::Unknown.
135 ///
136 /// @param BitWidth e_ident[EI_CLASS] value to extract bit width from.
137 IFSBitWidthType convertELFBitWidthToIFS(uint8_t BitWidth);
138 
139 /// This function extracts ELF endianness from e_ident[EI_DATA] of an ELF file
140 /// Currently, ELFDATA2LSB and ELFDATA2MSB are supported.
141 /// Other endianness types are mapped to IFSEndiannessType::Unknown.
142 ///
143 /// @param Endianness e_ident[EI_DATA] value to extract endianness type from.
144 IFSEndiannessType convertELFEndiannessToIFS(uint8_t Endianness);
145 
146 /// This function extracts symbol type from a symbol's st_info member and
147 /// maps it to an IFSSymbolType enum.
148 /// Currently, STT_NOTYPE, STT_OBJECT, STT_FUNC, and STT_TLS are supported.
149 /// Other symbol types are mapped to IFSSymbolType::Unknown.
150 ///
151 /// @param SymbolType Binary symbol st_info to extract symbol type from.
152 IFSSymbolType convertELFSymbolTypeToIFS(uint8_t SymbolType);
153 } // namespace ifs
154 } // end namespace llvm
155 
156 #endif // LLVM_INTERFACESTUB_IFSSTUB_H
157