IFSStub.h 5.0 KB

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