ELFYAML.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. ///
  10. /// \file
  11. /// \brief This file declares classes for handling the YAML representation
  12. /// of ELF.
  13. ///
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_OBJECT_ELFYAML_H
  16. #define LLVM_OBJECT_ELFYAML_H
  17. #include "llvm/MC/YAML.h"
  18. #include "llvm/Support/ELF.h"
  19. namespace llvm {
  20. namespace ELFYAML {
  21. // These types are invariant across 32/64-bit ELF, so for simplicity just
  22. // directly give them their exact sizes. We don't need to worry about
  23. // endianness because these are just the types in the YAMLIO structures,
  24. // and are appropriately converted to the necessary endianness when
  25. // reading/generating binary object files.
  26. // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
  27. // the common prefix of the respective constants. E.g. ELF_EM corresponds
  28. // to the `e_machine` constants, like `EM_X86_64`.
  29. // In the future, these would probably be better suited by C++11 enum
  30. // class's with appropriate fixed underlying type.
  31. LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
  32. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
  33. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
  34. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
  35. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
  36. // Just use 64, since it can hold 32-bit values too.
  37. LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
  38. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
  39. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
  40. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
  41. // Just use 64, since it can hold 32-bit values too.
  42. LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
  43. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
  44. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STV)
  45. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STO)
  46. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
  47. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
  48. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
  49. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
  50. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
  51. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)
  52. // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
  53. // since 64-bit can hold 32-bit values too.
  54. struct FileHeader {
  55. ELF_ELFCLASS Class;
  56. ELF_ELFDATA Data;
  57. ELF_ELFOSABI OSABI;
  58. ELF_ET Type;
  59. ELF_EM Machine;
  60. ELF_EF Flags;
  61. llvm::yaml::Hex64 Entry;
  62. };
  63. struct Symbol {
  64. StringRef Name;
  65. ELF_STT Type;
  66. StringRef Section;
  67. llvm::yaml::Hex64 Value;
  68. llvm::yaml::Hex64 Size;
  69. uint8_t Other;
  70. };
  71. struct LocalGlobalWeakSymbols {
  72. std::vector<Symbol> Local;
  73. std::vector<Symbol> Global;
  74. std::vector<Symbol> Weak;
  75. };
  76. struct SectionOrType {
  77. StringRef sectionNameOrType;
  78. };
  79. struct Section {
  80. enum class SectionKind {
  81. Group,
  82. RawContent,
  83. Relocation,
  84. NoBits,
  85. MipsABIFlags
  86. };
  87. SectionKind Kind;
  88. StringRef Name;
  89. ELF_SHT Type;
  90. ELF_SHF Flags;
  91. llvm::yaml::Hex64 Address;
  92. StringRef Link;
  93. StringRef Info;
  94. llvm::yaml::Hex64 AddressAlign;
  95. Section(SectionKind Kind) : Kind(Kind) {}
  96. virtual ~Section();
  97. };
  98. struct RawContentSection : Section {
  99. yaml::BinaryRef Content;
  100. llvm::yaml::Hex64 Size;
  101. RawContentSection() : Section(SectionKind::RawContent) {}
  102. static bool classof(const Section *S) {
  103. return S->Kind == SectionKind::RawContent;
  104. }
  105. };
  106. struct NoBitsSection : Section {
  107. llvm::yaml::Hex64 Size;
  108. NoBitsSection() : Section(SectionKind::NoBits) {}
  109. static bool classof(const Section *S) {
  110. return S->Kind == SectionKind::NoBits;
  111. }
  112. };
  113. struct Group : Section {
  114. // Members of a group contain a flag and a list of section indices
  115. // that are part of the group.
  116. std::vector<SectionOrType> Members;
  117. Group() : Section(SectionKind::Group) {}
  118. static bool classof(const Section *S) {
  119. return S->Kind == SectionKind::Group;
  120. }
  121. };
  122. struct Relocation {
  123. llvm::yaml::Hex64 Offset;
  124. int64_t Addend;
  125. ELF_REL Type;
  126. StringRef Symbol;
  127. };
  128. struct RelocationSection : Section {
  129. std::vector<Relocation> Relocations;
  130. RelocationSection() : Section(SectionKind::Relocation) {}
  131. static bool classof(const Section *S) {
  132. return S->Kind == SectionKind::Relocation;
  133. }
  134. };
  135. // Represents .MIPS.abiflags section
  136. struct MipsABIFlags : Section {
  137. llvm::yaml::Hex16 Version;
  138. MIPS_ISA ISALevel;
  139. llvm::yaml::Hex8 ISARevision;
  140. MIPS_AFL_REG GPRSize;
  141. MIPS_AFL_REG CPR1Size;
  142. MIPS_AFL_REG CPR2Size;
  143. MIPS_ABI_FP FpABI;
  144. MIPS_AFL_EXT ISAExtension;
  145. MIPS_AFL_ASE ASEs;
  146. MIPS_AFL_FLAGS1 Flags1;
  147. llvm::yaml::Hex32 Flags2;
  148. MipsABIFlags() : Section(SectionKind::MipsABIFlags) {}
  149. static bool classof(const Section *S) {
  150. return S->Kind == SectionKind::MipsABIFlags;
  151. }
  152. };
  153. struct Object {
  154. FileHeader Header;
  155. std::vector<std::unique_ptr<Section>> Sections;
  156. // Although in reality the symbols reside in a section, it is a lot
  157. // cleaner and nicer if we read them from the YAML as a separate
  158. // top-level key, which automatically ensures that invariants like there
  159. // being a single SHT_SYMTAB section are upheld.
  160. LocalGlobalWeakSymbols Symbols;
  161. };
  162. } // end namespace ELFYAML
  163. } // end namespace llvm
  164. LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Section>)
  165. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
  166. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
  167. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
  168. namespace llvm {
  169. namespace yaml {
  170. template <>
  171. struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
  172. static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
  173. };
  174. template <>
  175. struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
  176. static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
  177. };
  178. template <>
  179. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
  180. static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
  181. };
  182. template <>
  183. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
  184. static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
  185. };
  186. template <>
  187. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
  188. static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
  189. };
  190. template <>
  191. struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
  192. static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
  193. };
  194. template <>
  195. struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
  196. static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
  197. };
  198. template <>
  199. struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
  200. static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
  201. };
  202. template <>
  203. struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
  204. static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
  205. };
  206. template <>
  207. struct ScalarEnumerationTraits<ELFYAML::ELF_STV> {
  208. static void enumeration(IO &IO, ELFYAML::ELF_STV &Value);
  209. };
  210. template <>
  211. struct ScalarBitSetTraits<ELFYAML::ELF_STO> {
  212. static void bitset(IO &IO, ELFYAML::ELF_STO &Value);
  213. };
  214. template <>
  215. struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
  216. static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
  217. };
  218. template <>
  219. struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
  220. static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
  221. };
  222. template <>
  223. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
  224. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
  225. };
  226. template <>
  227. struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
  228. static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
  229. };
  230. template <>
  231. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
  232. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
  233. };
  234. template <>
  235. struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
  236. static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
  237. };
  238. template <>
  239. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
  240. static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
  241. };
  242. template <>
  243. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
  244. static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
  245. };
  246. template <>
  247. struct MappingTraits<ELFYAML::FileHeader> {
  248. static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
  249. };
  250. template <>
  251. struct MappingTraits<ELFYAML::Symbol> {
  252. static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
  253. };
  254. template <>
  255. struct MappingTraits<ELFYAML::LocalGlobalWeakSymbols> {
  256. static void mapping(IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols);
  257. };
  258. template <> struct MappingTraits<ELFYAML::Relocation> {
  259. static void mapping(IO &IO, ELFYAML::Relocation &Rel);
  260. };
  261. template <>
  262. struct MappingTraits<std::unique_ptr<ELFYAML::Section>> {
  263. static void mapping(IO &IO, std::unique_ptr<ELFYAML::Section> &Section);
  264. static StringRef validate(IO &io, std::unique_ptr<ELFYAML::Section> &Section);
  265. };
  266. template <>
  267. struct MappingTraits<ELFYAML::Object> {
  268. static void mapping(IO &IO, ELFYAML::Object &Object);
  269. };
  270. template <> struct MappingTraits<ELFYAML::SectionOrType> {
  271. static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
  272. };
  273. } // end namespace yaml
  274. } // end namespace llvm
  275. #endif