[yaml2obj][ELF] Allow symbols to reference sections.
[oota-llvm.git] / include / llvm / Object / ELFYAML.h
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
16 #ifndef LLVM_OBJECT_ELFYAML_H
17 #define LLVM_OBJECT_ELFYAML_H
18
19 #include "llvm/Object/YAML.h"
20 #include "llvm/Support/ELF.h"
21
22 namespace llvm {
23 namespace ELFYAML {
24
25 // These types are invariant across 32/64-bit ELF, so for simplicity just
26 // directly give them their exact sizes. We don't need to worry about
27 // endianness because these are just the types in the YAMLIO structures,
28 // and are appropriately converted to the necessary endianness when
29 // reading/generating binary object files.
30 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
31 // the common prefix of the respective constants. E.g. ELF_EM corresponds
32 // to the `e_machine` constants, like `EM_X86_64`.
33 // In the future, these would probably be better suited by C++11 enum
34 // class's with appropriate fixed underlying type.
35 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET)
36 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM)
37 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS)
38 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA)
39 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFOSABI)
40 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_SHT)
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_STB)
44 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_STT)
45
46 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
47 // since 64-bit can hold 32-bit values too.
48 struct FileHeader {
49   ELF_ELFCLASS Class;
50   ELF_ELFDATA Data;
51   ELF_ELFOSABI OSABI;
52   ELF_ET Type;
53   ELF_EM Machine;
54   llvm::yaml::Hex64 Entry;
55 };
56 struct Symbol {
57   StringRef Name;
58   ELF_STB Binding;
59   ELF_STT Type;
60   StringRef Section;
61 };
62 struct Section {
63   StringRef Name;
64   ELF_SHT Type;
65   ELF_SHF Flags;
66   llvm::yaml::Hex64 Address;
67   object::yaml::BinaryRef Content;
68   StringRef Link;
69   llvm::yaml::Hex64 AddressAlign;
70   // For SHT_SYMTAB; should be empty otherwise.
71   std::vector<Symbol> Symbols;
72 };
73 struct Object {
74   FileHeader Header;
75   std::vector<Section> Sections;
76 };
77
78 } // end namespace ELFYAML
79 } // end namespace llvm
80
81 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Section)
82 LLVM_YAML_IS_SEQUENCE_VECTOR(llvm::ELFYAML::Symbol)
83
84 namespace llvm {
85 namespace yaml {
86
87 template <>
88 struct ScalarEnumerationTraits<ELFYAML::ELF_ET> {
89   static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
90 };
91
92 template <>
93 struct ScalarEnumerationTraits<ELFYAML::ELF_EM> {
94   static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
95 };
96
97 template <>
98 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS> {
99   static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
100 };
101
102 template <>
103 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA> {
104   static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
105 };
106
107 template <>
108 struct ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI> {
109   static void enumeration(IO &IO, ELFYAML::ELF_ELFOSABI &Value);
110 };
111
112 template <>
113 struct ScalarEnumerationTraits<ELFYAML::ELF_SHT> {
114   static void enumeration(IO &IO, ELFYAML::ELF_SHT &Value);
115 };
116
117 template <>
118 struct ScalarBitSetTraits<ELFYAML::ELF_SHF> {
119   static void bitset(IO &IO, ELFYAML::ELF_SHF &Value);
120 };
121
122 template <>
123 struct ScalarEnumerationTraits<ELFYAML::ELF_STB> {
124   static void enumeration(IO &IO, ELFYAML::ELF_STB &Value);
125 };
126
127 template <>
128 struct ScalarEnumerationTraits<ELFYAML::ELF_STT> {
129   static void enumeration(IO &IO, ELFYAML::ELF_STT &Value);
130 };
131
132 template <>
133 struct MappingTraits<ELFYAML::FileHeader> {
134   static void mapping(IO &IO, ELFYAML::FileHeader &FileHdr);
135 };
136
137 template <>
138 struct MappingTraits<ELFYAML::Symbol> {
139   static void mapping(IO &IO, ELFYAML::Symbol &Symbol);
140 };
141
142 template <>
143 struct MappingTraits<ELFYAML::Section> {
144   static void mapping(IO &IO, ELFYAML::Section &Section);
145 };
146
147 template <>
148 struct MappingTraits<ELFYAML::Object> {
149   static void mapping(IO &IO, ELFYAML::Object &Object);
150 };
151
152 } // end namespace yaml
153 } // end namespace llvm
154
155 #endif