ELFYAML.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- 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 declares classes for handling the YAML representation
  16. /// of ELF.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_OBJECTYAML_ELFYAML_H
  20. #define LLVM_OBJECTYAML_ELFYAML_H
  21. #include "llvm/ADT/StringRef.h"
  22. #include "llvm/BinaryFormat/ELF.h"
  23. #include "llvm/Object/ELFTypes.h"
  24. #include "llvm/ObjectYAML/DWARFYAML.h"
  25. #include "llvm/ObjectYAML/YAML.h"
  26. #include "llvm/Support/YAMLTraits.h"
  27. #include <cstdint>
  28. #include <memory>
  29. #include <vector>
  30. namespace llvm {
  31. namespace ELFYAML {
  32. StringRef dropUniqueSuffix(StringRef S);
  33. std::string appendUniqueSuffix(StringRef Name, const Twine& Msg);
  34. // These types are invariant across 32/64-bit ELF, so for simplicity just
  35. // directly give them their exact sizes. We don't need to worry about
  36. // endianness because these are just the types in the YAMLIO structures,
  37. // and are appropriately converted to the necessary endianness when
  38. // reading/generating binary object files.
  39. // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
  40. // the common prefix of the respective constants. E.g. ELF_EM corresponds
  41. // to the `e_machine` constants, like `EM_X86_64`.
  42. // In the future, these would probably be better suited by C++11 enum
  43. // class's with appropriate fixed underlying type.
  44. LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
  45. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PT)
  46. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
  47. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
  48. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
  49. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
  50. // Just use 64, since it can hold 32-bit values too.
  51. LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_EF)
  52. // Just use 64, since it can hold 32-bit values too.
  53. LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_DYNTAG)
  54. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_PF)
  55. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
  56. LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_REL)
  57. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_RSS)
  58. // Just use 64, since it can hold 32-bit values too.
  59. LLVM_YAML_STRONG_TYPEDEF(uint64_t, ELF_SHF)
  60. LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_SHN)
  61. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STB)
  62. LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
  63. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
  64. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
  65. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
  66. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
  67. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
  68. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)
  69. LLVM_YAML_STRONG_TYPEDEF(StringRef, YAMLFlowString)
  70. LLVM_YAML_STRONG_TYPEDEF(int64_t, YAMLIntUInt)
  71. template <class ELFT>
  72. unsigned getDefaultShEntSize(unsigned EMachine, ELF_SHT SecType,
  73. StringRef SecName) {
  74. if (EMachine == ELF::EM_MIPS && SecType == ELF::SHT_MIPS_ABIFLAGS)
  75. return sizeof(object::Elf_Mips_ABIFlags<ELFT>);
  76. switch (SecType) {
  77. case ELF::SHT_SYMTAB:
  78. case ELF::SHT_DYNSYM:
  79. return sizeof(typename ELFT::Sym);
  80. case ELF::SHT_GROUP:
  81. return sizeof(typename ELFT::Word);
  82. case ELF::SHT_REL:
  83. return sizeof(typename ELFT::Rel);
  84. case ELF::SHT_RELA:
  85. return sizeof(typename ELFT::Rela);
  86. case ELF::SHT_RELR:
  87. return sizeof(typename ELFT::Relr);
  88. case ELF::SHT_DYNAMIC:
  89. return sizeof(typename ELFT::Dyn);
  90. case ELF::SHT_HASH:
  91. return sizeof(typename ELFT::Word);
  92. case ELF::SHT_SYMTAB_SHNDX:
  93. return sizeof(typename ELFT::Word);
  94. case ELF::SHT_GNU_versym:
  95. return sizeof(typename ELFT::Half);
  96. case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
  97. return sizeof(object::Elf_CGProfile_Impl<ELFT>);
  98. default:
  99. if (SecName == ".debug_str")
  100. return 1;
  101. return 0;
  102. }
  103. }
  104. // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
  105. // since 64-bit can hold 32-bit values too.
  106. struct FileHeader {
  107. ELF_ELFCLASS Class;
  108. ELF_ELFDATA Data;
  109. ELF_ELFOSABI OSABI;
  110. llvm::yaml::Hex8 ABIVersion;
  111. ELF_ET Type;
  112. Optional<ELF_EM> Machine;
  113. ELF_EF Flags;
  114. llvm::yaml::Hex64 Entry;
  115. Optional<llvm::yaml::Hex64> EPhOff;
  116. Optional<llvm::yaml::Hex16> EPhEntSize;
  117. Optional<llvm::yaml::Hex16> EPhNum;
  118. Optional<llvm::yaml::Hex16> EShEntSize;
  119. Optional<llvm::yaml::Hex64> EShOff;
  120. Optional<llvm::yaml::Hex16> EShNum;
  121. Optional<llvm::yaml::Hex16> EShStrNdx;
  122. };
  123. struct SectionHeader {
  124. StringRef Name;
  125. };
  126. struct Symbol {
  127. StringRef Name;
  128. ELF_STT Type;
  129. Optional<StringRef> Section;
  130. Optional<ELF_SHN> Index;
  131. ELF_STB Binding;
  132. Optional<llvm::yaml::Hex64> Value;
  133. Optional<llvm::yaml::Hex64> Size;
  134. Optional<uint8_t> Other;
  135. Optional<uint32_t> StName;
  136. };
  137. struct SectionOrType {
  138. StringRef sectionNameOrType;
  139. };
  140. struct DynamicEntry {
  141. ELF_DYNTAG Tag;
  142. llvm::yaml::Hex64 Val;
  143. };
  144. struct BBAddrMapEntry {
  145. struct BBEntry {
  146. llvm::yaml::Hex32 AddressOffset;
  147. llvm::yaml::Hex32 Size;
  148. llvm::yaml::Hex32 Metadata;
  149. };
  150. llvm::yaml::Hex64 Address;
  151. Optional<std::vector<BBEntry>> BBEntries;
  152. };
  153. struct StackSizeEntry {
  154. llvm::yaml::Hex64 Address;
  155. llvm::yaml::Hex64 Size;
  156. };
  157. struct NoteEntry {
  158. StringRef Name;
  159. yaml::BinaryRef Desc;
  160. llvm::yaml::Hex32 Type;
  161. };
  162. struct Chunk {
  163. enum class ChunkKind {
  164. Dynamic,
  165. Group,
  166. RawContent,
  167. Relocation,
  168. Relr,
  169. NoBits,
  170. Note,
  171. Hash,
  172. GnuHash,
  173. Verdef,
  174. Verneed,
  175. StackSizes,
  176. SymtabShndxSection,
  177. Symver,
  178. ARMIndexTable,
  179. MipsABIFlags,
  180. Addrsig,
  181. LinkerOptions,
  182. DependentLibraries,
  183. CallGraphProfile,
  184. BBAddrMap,
  185. // Special chunks.
  186. SpecialChunksStart,
  187. Fill = SpecialChunksStart,
  188. SectionHeaderTable,
  189. };
  190. ChunkKind Kind;
  191. StringRef Name;
  192. Optional<llvm::yaml::Hex64> Offset;
  193. // Usually chunks are not created implicitly, but rather loaded from YAML.
  194. // This flag is used to signal whether this is the case or not.
  195. bool IsImplicit;
  196. Chunk(ChunkKind K, bool Implicit) : Kind(K), IsImplicit(Implicit) {}
  197. virtual ~Chunk();
  198. };
  199. struct Section : public Chunk {
  200. ELF_SHT Type;
  201. Optional<ELF_SHF> Flags;
  202. Optional<llvm::yaml::Hex64> Address;
  203. Optional<StringRef> Link;
  204. llvm::yaml::Hex64 AddressAlign;
  205. Optional<llvm::yaml::Hex64> EntSize;
  206. Optional<yaml::BinaryRef> Content;
  207. Optional<llvm::yaml::Hex64> Size;
  208. // Holds the original section index.
  209. unsigned OriginalSecNdx;
  210. Section(ChunkKind Kind, bool IsImplicit = false) : Chunk(Kind, IsImplicit) {}
  211. static bool classof(const Chunk *S) {
  212. return S->Kind < ChunkKind::SpecialChunksStart;
  213. }
  214. // Some derived sections might have their own special entries. This method
  215. // returns a vector of <entry name, is used> pairs. It is used for section
  216. // validation.
  217. virtual std::vector<std::pair<StringRef, bool>> getEntries() const {
  218. return {};
  219. };
  220. // The following members are used to override section fields which is
  221. // useful for creating invalid objects.
  222. // This can be used to override the sh_addralign field.
  223. Optional<llvm::yaml::Hex64> ShAddrAlign;
  224. // This can be used to override the offset stored in the sh_name field.
  225. // It does not affect the name stored in the string table.
  226. Optional<llvm::yaml::Hex64> ShName;
  227. // This can be used to override the sh_offset field. It does not place the
  228. // section data at the offset specified.
  229. Optional<llvm::yaml::Hex64> ShOffset;
  230. // This can be used to override the sh_size field. It does not affect the
  231. // content written.
  232. Optional<llvm::yaml::Hex64> ShSize;
  233. // This can be used to override the sh_flags field.
  234. Optional<llvm::yaml::Hex64> ShFlags;
  235. // This can be used to override the sh_type field. It is useful when we
  236. // want to use specific YAML keys for a section of a particular type to
  237. // describe the content, but still want to have a different final type
  238. // for the section.
  239. Optional<ELF_SHT> ShType;
  240. };
  241. // Fill is a block of data which is placed outside of sections. It is
  242. // not present in the sections header table, but it might affect the output file
  243. // size and program headers produced.
  244. struct Fill : Chunk {
  245. Optional<yaml::BinaryRef> Pattern;
  246. llvm::yaml::Hex64 Size;
  247. Fill() : Chunk(ChunkKind::Fill, /*Implicit=*/false) {}
  248. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Fill; }
  249. };
  250. struct SectionHeaderTable : Chunk {
  251. SectionHeaderTable(bool IsImplicit)
  252. : Chunk(ChunkKind::SectionHeaderTable, IsImplicit) {}
  253. static bool classof(const Chunk *S) {
  254. return S->Kind == ChunkKind::SectionHeaderTable;
  255. }
  256. Optional<std::vector<SectionHeader>> Sections;
  257. Optional<std::vector<SectionHeader>> Excluded;
  258. Optional<bool> NoHeaders;
  259. size_t getNumHeaders(size_t SectionsNum) const {
  260. if (IsImplicit)
  261. return SectionsNum;
  262. if (NoHeaders)
  263. return (*NoHeaders) ? 0 : SectionsNum;
  264. return (Sections ? Sections->size() : 0) + /*Null section*/ 1;
  265. }
  266. static constexpr StringRef TypeStr = "SectionHeaderTable";
  267. };
  268. struct BBAddrMapSection : Section {
  269. Optional<std::vector<BBAddrMapEntry>> Entries;
  270. BBAddrMapSection() : Section(ChunkKind::BBAddrMap) {}
  271. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  272. return {{"Entries", Entries.hasValue()}};
  273. };
  274. static bool classof(const Chunk *S) {
  275. return S->Kind == ChunkKind::BBAddrMap;
  276. }
  277. };
  278. struct StackSizesSection : Section {
  279. Optional<std::vector<StackSizeEntry>> Entries;
  280. StackSizesSection() : Section(ChunkKind::StackSizes) {}
  281. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  282. return {{"Entries", Entries.hasValue()}};
  283. };
  284. static bool classof(const Chunk *S) {
  285. return S->Kind == ChunkKind::StackSizes;
  286. }
  287. static bool nameMatches(StringRef Name) {
  288. return Name == ".stack_sizes";
  289. }
  290. };
  291. struct DynamicSection : Section {
  292. Optional<std::vector<DynamicEntry>> Entries;
  293. DynamicSection() : Section(ChunkKind::Dynamic) {}
  294. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  295. return {{"Entries", Entries.hasValue()}};
  296. };
  297. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Dynamic; }
  298. };
  299. struct RawContentSection : Section {
  300. Optional<llvm::yaml::Hex64> Info;
  301. RawContentSection() : Section(ChunkKind::RawContent) {}
  302. static bool classof(const Chunk *S) {
  303. return S->Kind == ChunkKind::RawContent;
  304. }
  305. // Is used when a content is read as an array of bytes.
  306. Optional<std::vector<uint8_t>> ContentBuf;
  307. };
  308. struct NoBitsSection : Section {
  309. NoBitsSection() : Section(ChunkKind::NoBits) {}
  310. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::NoBits; }
  311. };
  312. struct NoteSection : Section {
  313. Optional<std::vector<ELFYAML::NoteEntry>> Notes;
  314. NoteSection() : Section(ChunkKind::Note) {}
  315. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  316. return {{"Notes", Notes.hasValue()}};
  317. };
  318. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Note; }
  319. };
  320. struct HashSection : Section {
  321. Optional<std::vector<uint32_t>> Bucket;
  322. Optional<std::vector<uint32_t>> Chain;
  323. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  324. return {{"Bucket", Bucket.hasValue()}, {"Chain", Chain.hasValue()}};
  325. };
  326. // The following members are used to override section fields.
  327. // This is useful for creating invalid objects.
  328. Optional<llvm::yaml::Hex64> NBucket;
  329. Optional<llvm::yaml::Hex64> NChain;
  330. HashSection() : Section(ChunkKind::Hash) {}
  331. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Hash; }
  332. };
  333. struct GnuHashHeader {
  334. // The number of hash buckets.
  335. // Not used when dumping the object, but can be used to override
  336. // the real number of buckets when emiting an object from a YAML document.
  337. Optional<llvm::yaml::Hex32> NBuckets;
  338. // Index of the first symbol in the dynamic symbol table
  339. // included in the hash table.
  340. llvm::yaml::Hex32 SymNdx;
  341. // The number of words in the Bloom filter.
  342. // Not used when dumping the object, but can be used to override the real
  343. // number of words in the Bloom filter when emiting an object from a YAML
  344. // document.
  345. Optional<llvm::yaml::Hex32> MaskWords;
  346. // A shift constant used by the Bloom filter.
  347. llvm::yaml::Hex32 Shift2;
  348. };
  349. struct GnuHashSection : Section {
  350. Optional<GnuHashHeader> Header;
  351. Optional<std::vector<llvm::yaml::Hex64>> BloomFilter;
  352. Optional<std::vector<llvm::yaml::Hex32>> HashBuckets;
  353. Optional<std::vector<llvm::yaml::Hex32>> HashValues;
  354. GnuHashSection() : Section(ChunkKind::GnuHash) {}
  355. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  356. return {{"Header", Header.hasValue()},
  357. {"BloomFilter", BloomFilter.hasValue()},
  358. {"HashBuckets", HashBuckets.hasValue()},
  359. {"HashValues", HashValues.hasValue()}};
  360. };
  361. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::GnuHash; }
  362. };
  363. struct VernauxEntry {
  364. uint32_t Hash;
  365. uint16_t Flags;
  366. uint16_t Other;
  367. StringRef Name;
  368. };
  369. struct VerneedEntry {
  370. uint16_t Version;
  371. StringRef File;
  372. std::vector<VernauxEntry> AuxV;
  373. };
  374. struct VerneedSection : Section {
  375. Optional<std::vector<VerneedEntry>> VerneedV;
  376. Optional<llvm::yaml::Hex64> Info;
  377. VerneedSection() : Section(ChunkKind::Verneed) {}
  378. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  379. return {{"Dependencies", VerneedV.hasValue()}};
  380. };
  381. static bool classof(const Chunk *S) {
  382. return S->Kind == ChunkKind::Verneed;
  383. }
  384. };
  385. struct AddrsigSection : Section {
  386. Optional<std::vector<YAMLFlowString>> Symbols;
  387. AddrsigSection() : Section(ChunkKind::Addrsig) {}
  388. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  389. return {{"Symbols", Symbols.hasValue()}};
  390. };
  391. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Addrsig; }
  392. };
  393. struct LinkerOption {
  394. StringRef Key;
  395. StringRef Value;
  396. };
  397. struct LinkerOptionsSection : Section {
  398. Optional<std::vector<LinkerOption>> Options;
  399. LinkerOptionsSection() : Section(ChunkKind::LinkerOptions) {}
  400. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  401. return {{"Options", Options.hasValue()}};
  402. };
  403. static bool classof(const Chunk *S) {
  404. return S->Kind == ChunkKind::LinkerOptions;
  405. }
  406. };
  407. struct DependentLibrariesSection : Section {
  408. Optional<std::vector<YAMLFlowString>> Libs;
  409. DependentLibrariesSection() : Section(ChunkKind::DependentLibraries) {}
  410. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  411. return {{"Libraries", Libs.hasValue()}};
  412. };
  413. static bool classof(const Chunk *S) {
  414. return S->Kind == ChunkKind::DependentLibraries;
  415. }
  416. };
  417. // Represents the call graph profile section entry.
  418. struct CallGraphEntry {
  419. // The symbol of the source of the edge.
  420. StringRef From;
  421. // The symbol index of the destination of the edge.
  422. StringRef To;
  423. // The weight of the edge.
  424. uint64_t Weight;
  425. };
  426. struct CallGraphProfileSection : Section {
  427. Optional<std::vector<CallGraphEntry>> Entries;
  428. CallGraphProfileSection() : Section(ChunkKind::CallGraphProfile) {}
  429. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  430. return {{"Entries", Entries.hasValue()}};
  431. };
  432. static bool classof(const Chunk *S) {
  433. return S->Kind == ChunkKind::CallGraphProfile;
  434. }
  435. };
  436. struct SymverSection : Section {
  437. Optional<std::vector<uint16_t>> Entries;
  438. SymverSection() : Section(ChunkKind::Symver) {}
  439. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  440. return {{"Entries", Entries.hasValue()}};
  441. };
  442. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Symver; }
  443. };
  444. struct VerdefEntry {
  445. Optional<uint16_t> Version;
  446. Optional<uint16_t> Flags;
  447. Optional<uint16_t> VersionNdx;
  448. Optional<uint32_t> Hash;
  449. std::vector<StringRef> VerNames;
  450. };
  451. struct VerdefSection : Section {
  452. Optional<std::vector<VerdefEntry>> Entries;
  453. Optional<llvm::yaml::Hex64> Info;
  454. VerdefSection() : Section(ChunkKind::Verdef) {}
  455. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  456. return {{"Entries", Entries.hasValue()}};
  457. };
  458. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Verdef; }
  459. };
  460. struct GroupSection : Section {
  461. // Members of a group contain a flag and a list of section indices
  462. // that are part of the group.
  463. Optional<std::vector<SectionOrType>> Members;
  464. Optional<StringRef> Signature; /* Info */
  465. GroupSection() : Section(ChunkKind::Group) {}
  466. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  467. return {{"Members", Members.hasValue()}};
  468. };
  469. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Group; }
  470. };
  471. struct Relocation {
  472. llvm::yaml::Hex64 Offset;
  473. YAMLIntUInt Addend;
  474. ELF_REL Type;
  475. Optional<StringRef> Symbol;
  476. };
  477. struct RelocationSection : Section {
  478. Optional<std::vector<Relocation>> Relocations;
  479. StringRef RelocatableSec; /* Info */
  480. RelocationSection() : Section(ChunkKind::Relocation) {}
  481. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  482. return {{"Relocations", Relocations.hasValue()}};
  483. };
  484. static bool classof(const Chunk *S) {
  485. return S->Kind == ChunkKind::Relocation;
  486. }
  487. };
  488. struct RelrSection : Section {
  489. Optional<std::vector<llvm::yaml::Hex64>> Entries;
  490. RelrSection() : Section(ChunkKind::Relr) {}
  491. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  492. return {{"Entries", Entries.hasValue()}};
  493. };
  494. static bool classof(const Chunk *S) {
  495. return S->Kind == ChunkKind::Relr;
  496. }
  497. };
  498. struct SymtabShndxSection : Section {
  499. Optional<std::vector<uint32_t>> Entries;
  500. SymtabShndxSection() : Section(ChunkKind::SymtabShndxSection) {}
  501. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  502. return {{"Entries", Entries.hasValue()}};
  503. };
  504. static bool classof(const Chunk *S) {
  505. return S->Kind == ChunkKind::SymtabShndxSection;
  506. }
  507. };
  508. struct ARMIndexTableEntry {
  509. llvm::yaml::Hex32 Offset;
  510. llvm::yaml::Hex32 Value;
  511. };
  512. struct ARMIndexTableSection : Section {
  513. Optional<std::vector<ARMIndexTableEntry>> Entries;
  514. ARMIndexTableSection() : Section(ChunkKind::ARMIndexTable) {}
  515. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  516. return {{"Entries", Entries.hasValue()}};
  517. };
  518. static bool classof(const Chunk *S) {
  519. return S->Kind == ChunkKind::ARMIndexTable;
  520. }
  521. };
  522. // Represents .MIPS.abiflags section
  523. struct MipsABIFlags : Section {
  524. llvm::yaml::Hex16 Version;
  525. MIPS_ISA ISALevel;
  526. llvm::yaml::Hex8 ISARevision;
  527. MIPS_AFL_REG GPRSize;
  528. MIPS_AFL_REG CPR1Size;
  529. MIPS_AFL_REG CPR2Size;
  530. MIPS_ABI_FP FpABI;
  531. MIPS_AFL_EXT ISAExtension;
  532. MIPS_AFL_ASE ASEs;
  533. MIPS_AFL_FLAGS1 Flags1;
  534. llvm::yaml::Hex32 Flags2;
  535. MipsABIFlags() : Section(ChunkKind::MipsABIFlags) {}
  536. static bool classof(const Chunk *S) {
  537. return S->Kind == ChunkKind::MipsABIFlags;
  538. }
  539. };
  540. struct ProgramHeader {
  541. ELF_PT Type;
  542. ELF_PF Flags;
  543. llvm::yaml::Hex64 VAddr;
  544. llvm::yaml::Hex64 PAddr;
  545. Optional<llvm::yaml::Hex64> Align;
  546. Optional<llvm::yaml::Hex64> FileSize;
  547. Optional<llvm::yaml::Hex64> MemSize;
  548. Optional<llvm::yaml::Hex64> Offset;
  549. Optional<StringRef> FirstSec;
  550. Optional<StringRef> LastSec;
  551. // This vector contains all chunks from [FirstSec, LastSec].
  552. std::vector<Chunk *> Chunks;
  553. };
  554. struct Object {
  555. FileHeader Header;
  556. std::vector<ProgramHeader> ProgramHeaders;
  557. // An object might contain output section descriptions as well as
  558. // custom data that does not belong to any section.
  559. std::vector<std::unique_ptr<Chunk>> Chunks;
  560. // Although in reality the symbols reside in a section, it is a lot
  561. // cleaner and nicer if we read them from the YAML as a separate
  562. // top-level key, which automatically ensures that invariants like there
  563. // being a single SHT_SYMTAB section are upheld.
  564. Optional<std::vector<Symbol>> Symbols;
  565. Optional<std::vector<Symbol>> DynamicSymbols;
  566. Optional<DWARFYAML::Data> DWARF;
  567. std::vector<Section *> getSections() {
  568. std::vector<Section *> Ret;
  569. for (const std::unique_ptr<Chunk> &Sec : Chunks)
  570. if (auto S = dyn_cast<ELFYAML::Section>(Sec.get()))
  571. Ret.push_back(S);
  572. return Ret;
  573. }
  574. const SectionHeaderTable &getSectionHeaderTable() const {
  575. for (const std::unique_ptr<Chunk> &C : Chunks)
  576. if (auto *S = dyn_cast<ELFYAML::SectionHeaderTable>(C.get()))
  577. return *S;
  578. llvm_unreachable("the section header table chunk must always be present");
  579. }
  580. unsigned getMachine() const;
  581. };
  582. bool shouldAllocateFileSpace(ArrayRef<ProgramHeader> Phdrs,
  583. const NoBitsSection &S);
  584. } // end namespace ELFYAML
  585. } // end namespace llvm
  586. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::StackSizeEntry)
  587. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry)
  588. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::BBAddrMapEntry::BBEntry)
  589. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::DynamicEntry)
  590. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::LinkerOption)
  591. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::CallGraphEntry)
  592. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::NoteEntry)
  593. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ProgramHeader)
  594. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionHeader)
  595. LLVM_YAML_IS_SEQUENCE_VECTOR(std::unique_ptr<llvm::ELFYAML::Chunk>)
  596. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
  597. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerdefEntry)
  598. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VernauxEntry)
  599. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::VerneedEntry)
  600. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Relocation)
  601. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::SectionOrType)
  602. LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::ARMIndexTableEntry)
  603. namespace llvm {
  604. namespace yaml {
  605. template <> struct ScalarTraits<ELFYAML::YAMLIntUInt> {
  606. static void output(const ELFYAML::YAMLIntUInt &Val, void *Ctx,
  607. raw_ostream &Out);
  608. static StringRef input(StringRef Scalar, void *Ctx,
  609. ELFYAML::YAMLIntUInt &Val);
  610. static QuotingType mustQuote(StringRef) { return QuotingType::None; }
  611. };
  612. template <>
  613. struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
  614. static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
  615. };
  616. template <> struct ScalarEnumerationTraits<ELFYAML::ELF_PT> {
  617. static void enumeration(IO &IO, ELFYAML::ELF_PT &Value);
  618. };
  619. template <>
  620. struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
  621. static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
  622. };
  623. template <>
  624. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
  625. static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
  626. };
  627. template <>
  628. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
  629. static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
  630. };
  631. template <>
  632. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
  633. static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
  634. };
  635. template <>
  636. struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
  637. static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
  638. };
  639. template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
  640. static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
  641. };
  642. template <>
  643. struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
  644. static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
  645. };
  646. template <>
  647. struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
  648. static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
  649. };
  650. template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
  651. static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
  652. };
  653. template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
  654. static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
  655. };
  656. template <>
  657. struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
  658. static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
  659. };
  660. template <>
  661. struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
  662. static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
  663. };
  664. template <>
  665. struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
  666. static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
  667. };
  668. template <>
  669. struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
  670. static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
  671. };
  672. template <>
  673. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
  674. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
  675. };
  676. template <>
  677. struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
  678. static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
  679. };
  680. template <>
  681. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
  682. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
  683. };
  684. template <>
  685. struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
  686. static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
  687. };
  688. template <>
  689. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
  690. static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
  691. };
  692. template <>
  693. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
  694. static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
  695. };
  696. template <>
  697. struct MappingTraits<ELFYAML::FileHeader> {
  698. static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
  699. };
  700. template <> struct MappingTraits<ELFYAML::SectionHeader> {
  701. static void mapping(IO &IO, ELFYAML::SectionHeader &SHdr);
  702. };
  703. template <> struct MappingTraits<ELFYAML::ProgramHeader> {
  704. static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
  705. static std::string validate(IO &IO, ELFYAML::ProgramHeader &FileHdr);
  706. };
  707. template <>
  708. struct MappingTraits<ELFYAML::Symbol> {
  709. static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
  710. static std::string validate(IO &IO, ELFYAML::Symbol &Symbol);
  711. };
  712. template <> struct MappingTraits<ELFYAML::StackSizeEntry> {
  713. static void mapping(IO &IO, ELFYAML::StackSizeEntry &Rel);
  714. };
  715. template <> struct MappingTraits<ELFYAML::BBAddrMapEntry> {
  716. static void mapping(IO &IO, ELFYAML::BBAddrMapEntry &Rel);
  717. };
  718. template <> struct MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry> {
  719. static void mapping(IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &Rel);
  720. };
  721. template <> struct MappingTraits<ELFYAML::GnuHashHeader> {
  722. static void mapping(IO &IO, ELFYAML::GnuHashHeader &Rel);
  723. };
  724. template <> struct MappingTraits<ELFYAML::DynamicEntry> {
  725. static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
  726. };
  727. template <> struct MappingTraits<ELFYAML::NoteEntry> {
  728. static void mapping(IO &IO, ELFYAML::NoteEntry &N);
  729. };
  730. template <> struct MappingTraits<ELFYAML::VerdefEntry> {
  731. static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
  732. };
  733. template <> struct MappingTraits<ELFYAML::VerneedEntry> {
  734. static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
  735. };
  736. template <> struct MappingTraits<ELFYAML::VernauxEntry> {
  737. static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
  738. };
  739. template <> struct MappingTraits<ELFYAML::LinkerOption> {
  740. static void mapping(IO &IO, ELFYAML::LinkerOption &Sym);
  741. };
  742. template <> struct MappingTraits<ELFYAML::CallGraphEntry> {
  743. static void mapping(IO &IO, ELFYAML::CallGraphEntry &E);
  744. };
  745. template <> struct MappingTraits<ELFYAML::Relocation> {
  746. static void mapping(IO &IO, ELFYAML::Relocation &Rel);
  747. };
  748. template <> struct MappingTraits<ELFYAML::ARMIndexTableEntry> {
  749. static void mapping(IO &IO, ELFYAML::ARMIndexTableEntry &E);
  750. };
  751. template <> struct MappingTraits<std::unique_ptr<ELFYAML::Chunk>> {
  752. static void mapping(IO &IO, std::unique_ptr<ELFYAML::Chunk> &C);
  753. static std::string validate(IO &io, std::unique_ptr<ELFYAML::Chunk> &C);
  754. };
  755. template <>
  756. struct MappingTraits<ELFYAML::Object> {
  757. static void mapping(IO &IO, ELFYAML::Object &Object);
  758. };
  759. template <> struct MappingTraits<ELFYAML::SectionOrType> {
  760. static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
  761. };
  762. } // end namespace yaml
  763. } // end namespace llvm
  764. #endif // LLVM_OBJECTYAML_ELFYAML_H
  765. #ifdef __GNUC__
  766. #pragma GCC diagnostic pop
  767. #endif