ELFYAML.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  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(uint32_t, ELF_NT)
  64. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
  65. LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
  66. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
  67. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
  68. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
  69. LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_ISA)
  70. LLVM_YAML_STRONG_TYPEDEF(StringRef, YAMLFlowString)
  71. LLVM_YAML_STRONG_TYPEDEF(int64_t, YAMLIntUInt)
  72. template <class ELFT>
  73. unsigned getDefaultShEntSize(unsigned EMachine, ELF_SHT SecType,
  74. StringRef SecName) {
  75. if (EMachine == ELF::EM_MIPS && SecType == ELF::SHT_MIPS_ABIFLAGS)
  76. return sizeof(object::Elf_Mips_ABIFlags<ELFT>);
  77. switch (SecType) {
  78. case ELF::SHT_SYMTAB:
  79. case ELF::SHT_DYNSYM:
  80. return sizeof(typename ELFT::Sym);
  81. case ELF::SHT_GROUP:
  82. return sizeof(typename ELFT::Word);
  83. case ELF::SHT_REL:
  84. return sizeof(typename ELFT::Rel);
  85. case ELF::SHT_RELA:
  86. return sizeof(typename ELFT::Rela);
  87. case ELF::SHT_RELR:
  88. return sizeof(typename ELFT::Relr);
  89. case ELF::SHT_DYNAMIC:
  90. return sizeof(typename ELFT::Dyn);
  91. case ELF::SHT_HASH:
  92. return sizeof(typename ELFT::Word);
  93. case ELF::SHT_SYMTAB_SHNDX:
  94. return sizeof(typename ELFT::Word);
  95. case ELF::SHT_GNU_versym:
  96. return sizeof(typename ELFT::Half);
  97. case ELF::SHT_LLVM_CALL_GRAPH_PROFILE:
  98. return sizeof(object::Elf_CGProfile_Impl<ELFT>);
  99. default:
  100. if (SecName == ".debug_str")
  101. return 1;
  102. return 0;
  103. }
  104. }
  105. // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
  106. // since 64-bit can hold 32-bit values too.
  107. struct FileHeader {
  108. ELF_ELFCLASS Class;
  109. ELF_ELFDATA Data;
  110. ELF_ELFOSABI OSABI;
  111. llvm::yaml::Hex8 ABIVersion;
  112. ELF_ET Type;
  113. Optional<ELF_EM> Machine;
  114. ELF_EF Flags;
  115. llvm::yaml::Hex64 Entry;
  116. Optional<StringRef> SectionHeaderStringTable;
  117. Optional<llvm::yaml::Hex64> EPhOff;
  118. Optional<llvm::yaml::Hex16> EPhEntSize;
  119. Optional<llvm::yaml::Hex16> EPhNum;
  120. Optional<llvm::yaml::Hex16> EShEntSize;
  121. Optional<llvm::yaml::Hex64> EShOff;
  122. Optional<llvm::yaml::Hex16> EShNum;
  123. Optional<llvm::yaml::Hex16> EShStrNdx;
  124. };
  125. struct SectionHeader {
  126. StringRef Name;
  127. };
  128. struct Symbol {
  129. StringRef Name;
  130. ELF_STT Type;
  131. Optional<StringRef> Section;
  132. Optional<ELF_SHN> Index;
  133. ELF_STB Binding;
  134. Optional<llvm::yaml::Hex64> Value;
  135. Optional<llvm::yaml::Hex64> Size;
  136. Optional<uint8_t> Other;
  137. Optional<uint32_t> StName;
  138. };
  139. struct SectionOrType {
  140. StringRef sectionNameOrType;
  141. };
  142. struct DynamicEntry {
  143. ELF_DYNTAG Tag;
  144. llvm::yaml::Hex64 Val;
  145. };
  146. struct BBAddrMapEntry {
  147. struct BBEntry {
  148. llvm::yaml::Hex64 AddressOffset;
  149. llvm::yaml::Hex64 Size;
  150. llvm::yaml::Hex64 Metadata;
  151. };
  152. llvm::yaml::Hex64 Address;
  153. Optional<uint64_t> NumBlocks;
  154. Optional<std::vector<BBEntry>> BBEntries;
  155. };
  156. struct StackSizeEntry {
  157. llvm::yaml::Hex64 Address;
  158. llvm::yaml::Hex64 Size;
  159. };
  160. struct NoteEntry {
  161. StringRef Name;
  162. yaml::BinaryRef Desc;
  163. ELF_NT Type;
  164. };
  165. struct Chunk {
  166. enum class ChunkKind {
  167. Dynamic,
  168. Group,
  169. RawContent,
  170. Relocation,
  171. Relr,
  172. NoBits,
  173. Note,
  174. Hash,
  175. GnuHash,
  176. Verdef,
  177. Verneed,
  178. StackSizes,
  179. SymtabShndxSection,
  180. Symver,
  181. ARMIndexTable,
  182. MipsABIFlags,
  183. Addrsig,
  184. LinkerOptions,
  185. DependentLibraries,
  186. CallGraphProfile,
  187. BBAddrMap,
  188. // Special chunks.
  189. SpecialChunksStart,
  190. Fill = SpecialChunksStart,
  191. SectionHeaderTable,
  192. };
  193. ChunkKind Kind;
  194. StringRef Name;
  195. Optional<llvm::yaml::Hex64> Offset;
  196. // Usually chunks are not created implicitly, but rather loaded from YAML.
  197. // This flag is used to signal whether this is the case or not.
  198. bool IsImplicit;
  199. Chunk(ChunkKind K, bool Implicit) : Kind(K), IsImplicit(Implicit) {}
  200. virtual ~Chunk();
  201. };
  202. struct Section : public Chunk {
  203. ELF_SHT Type;
  204. Optional<ELF_SHF> Flags;
  205. Optional<llvm::yaml::Hex64> Address;
  206. Optional<StringRef> Link;
  207. llvm::yaml::Hex64 AddressAlign;
  208. Optional<llvm::yaml::Hex64> EntSize;
  209. Optional<yaml::BinaryRef> Content;
  210. Optional<llvm::yaml::Hex64> Size;
  211. // Holds the original section index.
  212. unsigned OriginalSecNdx;
  213. Section(ChunkKind Kind, bool IsImplicit = false) : Chunk(Kind, IsImplicit) {}
  214. static bool classof(const Chunk *S) {
  215. return S->Kind < ChunkKind::SpecialChunksStart;
  216. }
  217. // Some derived sections might have their own special entries. This method
  218. // returns a vector of <entry name, is used> pairs. It is used for section
  219. // validation.
  220. virtual std::vector<std::pair<StringRef, bool>> getEntries() const {
  221. return {};
  222. };
  223. // The following members are used to override section fields which is
  224. // useful for creating invalid objects.
  225. // This can be used to override the sh_addralign field.
  226. Optional<llvm::yaml::Hex64> ShAddrAlign;
  227. // This can be used to override the offset stored in the sh_name field.
  228. // It does not affect the name stored in the string table.
  229. Optional<llvm::yaml::Hex64> ShName;
  230. // This can be used to override the sh_offset field. It does not place the
  231. // section data at the offset specified.
  232. Optional<llvm::yaml::Hex64> ShOffset;
  233. // This can be used to override the sh_size field. It does not affect the
  234. // content written.
  235. Optional<llvm::yaml::Hex64> ShSize;
  236. // This can be used to override the sh_flags field.
  237. Optional<llvm::yaml::Hex64> ShFlags;
  238. // This can be used to override the sh_type field. It is useful when we
  239. // want to use specific YAML keys for a section of a particular type to
  240. // describe the content, but still want to have a different final type
  241. // for the section.
  242. Optional<ELF_SHT> ShType;
  243. };
  244. // Fill is a block of data which is placed outside of sections. It is
  245. // not present in the sections header table, but it might affect the output file
  246. // size and program headers produced.
  247. struct Fill : Chunk {
  248. Optional<yaml::BinaryRef> Pattern;
  249. llvm::yaml::Hex64 Size;
  250. Fill() : Chunk(ChunkKind::Fill, /*Implicit=*/false) {}
  251. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Fill; }
  252. };
  253. struct SectionHeaderTable : Chunk {
  254. SectionHeaderTable(bool IsImplicit)
  255. : Chunk(ChunkKind::SectionHeaderTable, IsImplicit) {}
  256. static bool classof(const Chunk *S) {
  257. return S->Kind == ChunkKind::SectionHeaderTable;
  258. }
  259. Optional<std::vector<SectionHeader>> Sections;
  260. Optional<std::vector<SectionHeader>> Excluded;
  261. Optional<bool> NoHeaders;
  262. size_t getNumHeaders(size_t SectionsNum) const {
  263. if (IsImplicit || isDefault())
  264. return SectionsNum;
  265. if (NoHeaders)
  266. return (*NoHeaders) ? 0 : SectionsNum;
  267. return (Sections ? Sections->size() : 0) + /*Null section*/ 1;
  268. }
  269. bool isDefault() const { return !Sections && !Excluded && !NoHeaders; }
  270. static constexpr StringRef TypeStr = "SectionHeaderTable";
  271. };
  272. struct BBAddrMapSection : Section {
  273. Optional<std::vector<BBAddrMapEntry>> Entries;
  274. BBAddrMapSection() : Section(ChunkKind::BBAddrMap) {}
  275. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  276. return {{"Entries", Entries.hasValue()}};
  277. };
  278. static bool classof(const Chunk *S) {
  279. return S->Kind == ChunkKind::BBAddrMap;
  280. }
  281. };
  282. struct StackSizesSection : Section {
  283. Optional<std::vector<StackSizeEntry>> Entries;
  284. StackSizesSection() : Section(ChunkKind::StackSizes) {}
  285. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  286. return {{"Entries", Entries.hasValue()}};
  287. };
  288. static bool classof(const Chunk *S) {
  289. return S->Kind == ChunkKind::StackSizes;
  290. }
  291. static bool nameMatches(StringRef Name) {
  292. return Name == ".stack_sizes";
  293. }
  294. };
  295. struct DynamicSection : Section {
  296. Optional<std::vector<DynamicEntry>> Entries;
  297. DynamicSection() : Section(ChunkKind::Dynamic) {}
  298. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  299. return {{"Entries", Entries.hasValue()}};
  300. };
  301. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Dynamic; }
  302. };
  303. struct RawContentSection : Section {
  304. Optional<llvm::yaml::Hex64> Info;
  305. RawContentSection() : Section(ChunkKind::RawContent) {}
  306. static bool classof(const Chunk *S) {
  307. return S->Kind == ChunkKind::RawContent;
  308. }
  309. // Is used when a content is read as an array of bytes.
  310. Optional<std::vector<uint8_t>> ContentBuf;
  311. };
  312. struct NoBitsSection : Section {
  313. NoBitsSection() : Section(ChunkKind::NoBits) {}
  314. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::NoBits; }
  315. };
  316. struct NoteSection : Section {
  317. Optional<std::vector<ELFYAML::NoteEntry>> Notes;
  318. NoteSection() : Section(ChunkKind::Note) {}
  319. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  320. return {{"Notes", Notes.hasValue()}};
  321. };
  322. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Note; }
  323. };
  324. struct HashSection : Section {
  325. Optional<std::vector<uint32_t>> Bucket;
  326. Optional<std::vector<uint32_t>> Chain;
  327. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  328. return {{"Bucket", Bucket.hasValue()}, {"Chain", Chain.hasValue()}};
  329. };
  330. // The following members are used to override section fields.
  331. // This is useful for creating invalid objects.
  332. Optional<llvm::yaml::Hex64> NBucket;
  333. Optional<llvm::yaml::Hex64> NChain;
  334. HashSection() : Section(ChunkKind::Hash) {}
  335. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Hash; }
  336. };
  337. struct GnuHashHeader {
  338. // The number of hash buckets.
  339. // Not used when dumping the object, but can be used to override
  340. // the real number of buckets when emiting an object from a YAML document.
  341. Optional<llvm::yaml::Hex32> NBuckets;
  342. // Index of the first symbol in the dynamic symbol table
  343. // included in the hash table.
  344. llvm::yaml::Hex32 SymNdx;
  345. // The number of words in the Bloom filter.
  346. // Not used when dumping the object, but can be used to override the real
  347. // number of words in the Bloom filter when emiting an object from a YAML
  348. // document.
  349. Optional<llvm::yaml::Hex32> MaskWords;
  350. // A shift constant used by the Bloom filter.
  351. llvm::yaml::Hex32 Shift2;
  352. };
  353. struct GnuHashSection : Section {
  354. Optional<GnuHashHeader> Header;
  355. Optional<std::vector<llvm::yaml::Hex64>> BloomFilter;
  356. Optional<std::vector<llvm::yaml::Hex32>> HashBuckets;
  357. Optional<std::vector<llvm::yaml::Hex32>> HashValues;
  358. GnuHashSection() : Section(ChunkKind::GnuHash) {}
  359. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  360. return {{"Header", Header.hasValue()},
  361. {"BloomFilter", BloomFilter.hasValue()},
  362. {"HashBuckets", HashBuckets.hasValue()},
  363. {"HashValues", HashValues.hasValue()}};
  364. };
  365. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::GnuHash; }
  366. };
  367. struct VernauxEntry {
  368. uint32_t Hash;
  369. uint16_t Flags;
  370. uint16_t Other;
  371. StringRef Name;
  372. };
  373. struct VerneedEntry {
  374. uint16_t Version;
  375. StringRef File;
  376. std::vector<VernauxEntry> AuxV;
  377. };
  378. struct VerneedSection : Section {
  379. Optional<std::vector<VerneedEntry>> VerneedV;
  380. Optional<llvm::yaml::Hex64> Info;
  381. VerneedSection() : Section(ChunkKind::Verneed) {}
  382. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  383. return {{"Dependencies", VerneedV.hasValue()}};
  384. };
  385. static bool classof(const Chunk *S) {
  386. return S->Kind == ChunkKind::Verneed;
  387. }
  388. };
  389. struct AddrsigSection : Section {
  390. Optional<std::vector<YAMLFlowString>> Symbols;
  391. AddrsigSection() : Section(ChunkKind::Addrsig) {}
  392. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  393. return {{"Symbols", Symbols.hasValue()}};
  394. };
  395. static bool classof(const Chunk *S) { return S->Kind == ChunkKind::Addrsig; }
  396. };
  397. struct LinkerOption {
  398. StringRef Key;
  399. StringRef Value;
  400. };
  401. struct LinkerOptionsSection : Section {
  402. Optional<std::vector<LinkerOption>> Options;
  403. LinkerOptionsSection() : Section(ChunkKind::LinkerOptions) {}
  404. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  405. return {{"Options", Options.hasValue()}};
  406. };
  407. static bool classof(const Chunk *S) {
  408. return S->Kind == ChunkKind::LinkerOptions;
  409. }
  410. };
  411. struct DependentLibrariesSection : Section {
  412. Optional<std::vector<YAMLFlowString>> Libs;
  413. DependentLibrariesSection() : Section(ChunkKind::DependentLibraries) {}
  414. std::vector<std::pair<StringRef, bool>> getEntries() const override {
  415. return {{"Libraries", Libs.hasValue()}};
  416. };
  417. static bool classof(const Chunk *S) {
  418. return S->Kind == ChunkKind::DependentLibraries;
  419. }
  420. };
  421. // Represents the call graph profile section entry.
  422. struct CallGraphEntryWeight {
  423. // The weight of the edge.
  424. uint64_t Weight;
  425. };
  426. struct CallGraphProfileSection : Section {
  427. Optional<std::vector<CallGraphEntryWeight>> 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::CallGraphEntryWeight)
  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 <> struct ScalarEnumerationTraits<ELFYAML::ELF_NT> {
  620. static void enumeration(IO &IO, ELFYAML::ELF_NT &Value);
  621. };
  622. template <>
  623. struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
  624. static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
  625. };
  626. template <>
  627. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
  628. static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
  629. };
  630. template <>
  631. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
  632. static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
  633. };
  634. template <>
  635. struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
  636. static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
  637. };
  638. template <>
  639. struct ScalarBitSetTraits<ELFYAML::ELF_EF> {
  640. static void bitset(IO &IO, ELFYAML::ELF_EF &Value);
  641. };
  642. template <> struct ScalarBitSetTraits<ELFYAML::ELF_PF> {
  643. static void bitset(IO &IO, ELFYAML::ELF_PF &Value);
  644. };
  645. template <>
  646. struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
  647. static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
  648. };
  649. template <>
  650. struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
  651. static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
  652. };
  653. template <> struct ScalarEnumerationTraits<ELFYAML::ELF_SHN> {
  654. static void enumeration(IO &IO, ELFYAML::ELF_SHN &Value);
  655. };
  656. template <> struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
  657. static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
  658. };
  659. template <>
  660. struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
  661. static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
  662. };
  663. template <>
  664. struct ScalarEnumerationTraits<ELFYAML::ELF_REL> {
  665. static void enumeration(IO &IO, ELFYAML::ELF_REL &Value);
  666. };
  667. template <>
  668. struct ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG> {
  669. static void enumeration(IO &IO, ELFYAML::ELF_DYNTAG &Value);
  670. };
  671. template <>
  672. struct ScalarEnumerationTraits<ELFYAML::ELF_RSS> {
  673. static void enumeration(IO &IO, ELFYAML::ELF_RSS &Value);
  674. };
  675. template <>
  676. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG> {
  677. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_REG &Value);
  678. };
  679. template <>
  680. struct ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP> {
  681. static void enumeration(IO &IO, ELFYAML::MIPS_ABI_FP &Value);
  682. };
  683. template <>
  684. struct ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT> {
  685. static void enumeration(IO &IO, ELFYAML::MIPS_AFL_EXT &Value);
  686. };
  687. template <>
  688. struct ScalarEnumerationTraits<ELFYAML::MIPS_ISA> {
  689. static void enumeration(IO &IO, ELFYAML::MIPS_ISA &Value);
  690. };
  691. template <>
  692. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE> {
  693. static void bitset(IO &IO, ELFYAML::MIPS_AFL_ASE &Value);
  694. };
  695. template <>
  696. struct ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1> {
  697. static void bitset(IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value);
  698. };
  699. template <>
  700. struct MappingTraits<ELFYAML::FileHeader> {
  701. static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
  702. };
  703. template <> struct MappingTraits<ELFYAML::SectionHeader> {
  704. static void mapping(IO &IO, ELFYAML::SectionHeader &SHdr);
  705. };
  706. template <> struct MappingTraits<ELFYAML::ProgramHeader> {
  707. static void mapping(IO &IO, ELFYAML::ProgramHeader &FileHdr);
  708. static std::string validate(IO &IO, ELFYAML::ProgramHeader &FileHdr);
  709. };
  710. template <>
  711. struct MappingTraits<ELFYAML::Symbol> {
  712. static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
  713. static std::string validate(IO &IO, ELFYAML::Symbol &Symbol);
  714. };
  715. template <> struct MappingTraits<ELFYAML::StackSizeEntry> {
  716. static void mapping(IO &IO, ELFYAML::StackSizeEntry &Rel);
  717. };
  718. template <> struct MappingTraits<ELFYAML::BBAddrMapEntry> {
  719. static void mapping(IO &IO, ELFYAML::BBAddrMapEntry &Rel);
  720. };
  721. template <> struct MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry> {
  722. static void mapping(IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &Rel);
  723. };
  724. template <> struct MappingTraits<ELFYAML::GnuHashHeader> {
  725. static void mapping(IO &IO, ELFYAML::GnuHashHeader &Rel);
  726. };
  727. template <> struct MappingTraits<ELFYAML::DynamicEntry> {
  728. static void mapping(IO &IO, ELFYAML::DynamicEntry &Rel);
  729. };
  730. template <> struct MappingTraits<ELFYAML::NoteEntry> {
  731. static void mapping(IO &IO, ELFYAML::NoteEntry &N);
  732. };
  733. template <> struct MappingTraits<ELFYAML::VerdefEntry> {
  734. static void mapping(IO &IO, ELFYAML::VerdefEntry &E);
  735. };
  736. template <> struct MappingTraits<ELFYAML::VerneedEntry> {
  737. static void mapping(IO &IO, ELFYAML::VerneedEntry &E);
  738. };
  739. template <> struct MappingTraits<ELFYAML::VernauxEntry> {
  740. static void mapping(IO &IO, ELFYAML::VernauxEntry &E);
  741. };
  742. template <> struct MappingTraits<ELFYAML::LinkerOption> {
  743. static void mapping(IO &IO, ELFYAML::LinkerOption &Sym);
  744. };
  745. template <> struct MappingTraits<ELFYAML::CallGraphEntryWeight> {
  746. static void mapping(IO &IO, ELFYAML::CallGraphEntryWeight &E);
  747. };
  748. template <> struct MappingTraits<ELFYAML::Relocation> {
  749. static void mapping(IO &IO, ELFYAML::Relocation &Rel);
  750. };
  751. template <> struct MappingTraits<ELFYAML::ARMIndexTableEntry> {
  752. static void mapping(IO &IO, ELFYAML::ARMIndexTableEntry &E);
  753. };
  754. template <> struct MappingTraits<std::unique_ptr<ELFYAML::Chunk>> {
  755. static void mapping(IO &IO, std::unique_ptr<ELFYAML::Chunk> &C);
  756. static std::string validate(IO &io, std::unique_ptr<ELFYAML::Chunk> &C);
  757. };
  758. template <>
  759. struct MappingTraits<ELFYAML::Object> {
  760. static void mapping(IO &IO, ELFYAML::Object &Object);
  761. };
  762. template <> struct MappingTraits<ELFYAML::SectionOrType> {
  763. static void mapping(IO &IO, ELFYAML::SectionOrType &sectionOrType);
  764. };
  765. } // end namespace yaml
  766. } // end namespace llvm
  767. #endif // LLVM_OBJECTYAML_ELFYAML_H
  768. #ifdef __GNUC__
  769. #pragma GCC diagnostic pop
  770. #endif