IFSStub.h 5.0 KB

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