llvm.org GIT mirror llvm / 5918b7a
[yaml2obj] Initial ELF support. Currently, only emitting the ELF header is supported (no sections or segments). The ELFYAML code organization is broadly similar to the COFFYAML code. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@183711 91177308-0d34-0410-b5e6-96231b3b80d8 Sean Silva 7 years ago
14 changed file(s) with 485 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
0 //===- ELFYAML.h - ELF YAMLIO implementation --------------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief This file declares classes for handling the YAML representation
11 /// of ELF.
12 ///
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_OBJECT_ELFYAML_H
16 #define LLVM_OBJECT_ELFYAML_H
17
18 #include "llvm/Object/YAML.h"
19 #include "llvm/Support/ELF.h"
20
21 namespace llvm {
22 namespace ELFYAML {
23
24 // These types are invariant across 32/64-bit ELF, so for simplicity just
25 // directly give them their exact sizes. We don't need to worry about
26 // endianness because these are just the types in the YAMLIO structures,
27 // and are appropriately converted to the necessary endianness when
28 // reading/generating binary object files.
29 // The naming of these types is intended to be ELF_PREFIX, where PREFIX is
30 // the common prefix of the respective constants. E.g. ELF_EM corresponds
31 // to the `e_machine` constants, like `EM_X86_64`.
32 // In the future, these would probably be better suited by C++11 enum
33 // class's with appropriate fixed underlying type.
34 LLVM_YAML_STRONG_TYPEDEF(uint16_t, ELF_ET);
35 LLVM_YAML_STRONG_TYPEDEF(uint32_t, ELF_EM);
36 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFCLASS);
37 LLVM_YAML_STRONG_TYPEDEF(uint8_t, ELF_ELFDATA);
38
39 // For now, hardcode 64 bits everywhere that 32 or 64 would be needed
40 // since 64-bit can hold 32-bit values too.
41 struct Header {
42 ELF_ELFCLASS Class;
43 ELF_ELFDATA Data;
44 ELF_ET Type;
45 ELF_EM Machine;
46 llvm::yaml::Hex64 Entry;
47 };
48 struct Object {
49 Header Header;
50 };
51
52 } // end namespace ELFYAML
53 } // end namespace llvm
54
55 namespace llvm {
56 namespace yaml {
57
58 template <>
59 struct ScalarEnumerationTraits {
60 static void enumeration(IO &IO, ELFYAML::ELF_ET &Value);
61 };
62
63 template <>
64 struct ScalarEnumerationTraits {
65 static void enumeration(IO &IO, ELFYAML::ELF_EM &Value);
66 };
67
68 template <>
69 struct ScalarEnumerationTraits {
70 static void enumeration(IO &IO, ELFYAML::ELF_ELFCLASS &Value);
71 };
72
73 template <>
74 struct ScalarEnumerationTraits {
75 static void enumeration(IO &IO, ELFYAML::ELF_ELFDATA &Value);
76 };
77
78 template <>
79 struct MappingTraits {
80 static void mapping(IO &IO, ELFYAML::Header &Header);
81 };
82
83 template <>
84 struct MappingTraits {
85 static void mapping(IO &IO, ELFYAML::Object &Object);
86 };
87
88 } // end namespace yaml
89 } // end namespace llvm
90
91 #endif
33 COFFObjectFile.cpp
44 COFFYAML.cpp
55 ELFObjectFile.cpp
6 ELFYAML.cpp
67 Error.cpp
78 MachOObjectFile.cpp
89 Object.cpp
0 //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines classes for handling the YAML representation of ELF.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/Object/ELFYAML.h"
14
15 namespace llvm {
16 namespace yaml {
17
18 void
19 ScalarEnumerationTraits::enumeration(IO &IO,
20 ELFYAML::ELF_ET &Value) {
21 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
22 ECase(ET_NONE)
23 ECase(ET_REL)
24 ECase(ET_EXEC)
25 ECase(ET_DYN)
26 ECase(ET_CORE)
27 #undef ECase
28 }
29
30 void
31 ScalarEnumerationTraits::enumeration(IO &IO,
32 ELFYAML::ELF_EM &Value) {
33 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
34 ECase(EM_NONE)
35 ECase(EM_M32)
36 ECase(EM_SPARC)
37 ECase(EM_386)
38 ECase(EM_68K)
39 ECase(EM_88K)
40 ECase(EM_486)
41 ECase(EM_860)
42 ECase(EM_MIPS)
43 ECase(EM_S370)
44 ECase(EM_MIPS_RS3_LE)
45 ECase(EM_PARISC)
46 ECase(EM_VPP500)
47 ECase(EM_SPARC32PLUS)
48 ECase(EM_960)
49 ECase(EM_PPC)
50 ECase(EM_PPC64)
51 ECase(EM_S390)
52 ECase(EM_SPU)
53 ECase(EM_V800)
54 ECase(EM_FR20)
55 ECase(EM_RH32)
56 ECase(EM_RCE)
57 ECase(EM_ARM)
58 ECase(EM_ALPHA)
59 ECase(EM_SH)
60 ECase(EM_SPARCV9)
61 ECase(EM_TRICORE)
62 ECase(EM_ARC)
63 ECase(EM_H8_300)
64 ECase(EM_H8_300H)
65 ECase(EM_H8S)
66 ECase(EM_H8_500)
67 ECase(EM_IA_64)
68 ECase(EM_MIPS_X)
69 ECase(EM_COLDFIRE)
70 ECase(EM_68HC12)
71 ECase(EM_MMA)
72 ECase(EM_PCP)
73 ECase(EM_NCPU)
74 ECase(EM_NDR1)
75 ECase(EM_STARCORE)
76 ECase(EM_ME16)
77 ECase(EM_ST100)
78 ECase(EM_TINYJ)
79 ECase(EM_X86_64)
80 ECase(EM_PDSP)
81 ECase(EM_PDP10)
82 ECase(EM_PDP11)
83 ECase(EM_FX66)
84 ECase(EM_ST9PLUS)
85 ECase(EM_ST7)
86 ECase(EM_68HC16)
87 ECase(EM_68HC11)
88 ECase(EM_68HC08)
89 ECase(EM_68HC05)
90 ECase(EM_SVX)
91 ECase(EM_ST19)
92 ECase(EM_VAX)
93 ECase(EM_CRIS)
94 ECase(EM_JAVELIN)
95 ECase(EM_FIREPATH)
96 ECase(EM_ZSP)
97 ECase(EM_MMIX)
98 ECase(EM_HUANY)
99 ECase(EM_PRISM)
100 ECase(EM_AVR)
101 ECase(EM_FR30)
102 ECase(EM_D10V)
103 ECase(EM_D30V)
104 ECase(EM_V850)
105 ECase(EM_M32R)
106 ECase(EM_MN10300)
107 ECase(EM_MN10200)
108 ECase(EM_PJ)
109 ECase(EM_OPENRISC)
110 ECase(EM_ARC_COMPACT)
111 ECase(EM_XTENSA)
112 ECase(EM_VIDEOCORE)
113 ECase(EM_TMM_GPP)
114 ECase(EM_NS32K)
115 ECase(EM_TPC)
116 ECase(EM_SNP1K)
117 ECase(EM_ST200)
118 ECase(EM_IP2K)
119 ECase(EM_MAX)
120 ECase(EM_CR)
121 ECase(EM_F2MC16)
122 ECase(EM_MSP430)
123 ECase(EM_BLACKFIN)
124 ECase(EM_SE_C33)
125 ECase(EM_SEP)
126 ECase(EM_ARCA)
127 ECase(EM_UNICORE)
128 ECase(EM_EXCESS)
129 ECase(EM_DXP)
130 ECase(EM_ALTERA_NIOS2)
131 ECase(EM_CRX)
132 ECase(EM_XGATE)
133 ECase(EM_C166)
134 ECase(EM_M16C)
135 ECase(EM_DSPIC30F)
136 ECase(EM_CE)
137 ECase(EM_M32C)
138 ECase(EM_TSK3000)
139 ECase(EM_RS08)
140 ECase(EM_SHARC)
141 ECase(EM_ECOG2)
142 ECase(EM_SCORE7)
143 ECase(EM_DSP24)
144 ECase(EM_VIDEOCORE3)
145 ECase(EM_LATTICEMICO32)
146 ECase(EM_SE_C17)
147 ECase(EM_TI_C6000)
148 ECase(EM_TI_C2000)
149 ECase(EM_TI_C5500)
150 ECase(EM_MMDSP_PLUS)
151 ECase(EM_CYPRESS_M8C)
152 ECase(EM_R32C)
153 ECase(EM_TRIMEDIA)
154 ECase(EM_HEXAGON)
155 ECase(EM_8051)
156 ECase(EM_STXP7X)
157 ECase(EM_NDS32)
158 ECase(EM_ECOG1)
159 ECase(EM_ECOG1X)
160 ECase(EM_MAXQ30)
161 ECase(EM_XIMO16)
162 ECase(EM_MANIK)
163 ECase(EM_CRAYNV2)
164 ECase(EM_RX)
165 ECase(EM_METAG)
166 ECase(EM_MCST_ELBRUS)
167 ECase(EM_ECOG16)
168 ECase(EM_CR16)
169 ECase(EM_ETPU)
170 ECase(EM_SLE9X)
171 ECase(EM_L10M)
172 ECase(EM_K10M)
173 ECase(EM_AARCH64)
174 ECase(EM_AVR32)
175 ECase(EM_STM8)
176 ECase(EM_TILE64)
177 ECase(EM_TILEPRO)
178 ECase(EM_MICROBLAZE)
179 ECase(EM_CUDA)
180 ECase(EM_TILEGX)
181 ECase(EM_CLOUDSHIELD)
182 ECase(EM_COREA_1ST)
183 ECase(EM_COREA_2ND)
184 ECase(EM_ARC_COMPACT2)
185 ECase(EM_OPEN8)
186 ECase(EM_RL78)
187 ECase(EM_VIDEOCORE5)
188 ECase(EM_78KOR)
189 ECase(EM_56800EX)
190 ECase(EM_MBLAZE)
191 #undef ECase
192 }
193
194 void ScalarEnumerationTraits::enumeration(
195 IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
196 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
197 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
198 // here.
199 ECase(ELFCLASS32)
200 ECase(ELFCLASS64)
201 #undef ECase
202 }
203
204 void ScalarEnumerationTraits::enumeration(
205 IO &IO, ELFYAML::ELF_ELFDATA &Value) {
206 #define ECase(X) IO.enumCase(Value, #X, ELF::X);
207 // Since the semantics of ELFDATANONE is "invalid", just don't accept it
208 // here.
209 ECase(ELFDATA2LSB)
210 ECase(ELFDATA2MSB)
211 #undef ECase
212 }
213
214 void MappingTraits::mapping(IO &IO, ELFYAML::Header &Header) {
215 IO.mapRequired("Class", Header.Class);
216 IO.mapRequired("Data", Header.Data);
217 IO.mapRequired("Type", Header.Type);
218 IO.mapRequired("Machine", Header.Machine);
219 IO.mapOptional("Entry", Header.Entry, Hex64(0));
220 }
221
222 void MappingTraits::mapping(IO &IO, ELFYAML::Object &Object) {
223 IO.mapRequired("Header", Object.Header);
224 }
225
226 } // end namespace yaml
227 } // end namespace llvm
0 !ELF
1 Header: !Header
2 Class: ELFCLASS32
3 Data: ELFDATA2MSB
4 Type: ET_EXEC
5 Machine: EM_PPC
0 !ELF
1 Header: !Header
2 Class: ELFCLASS64
3 Data: ELFDATA2MSB
4 Type: ET_EXEC
5 Machine: EM_PPC64
0 !ELF
1 Header: !Header
2 Class: ELFCLASS64
3 Data: ELFDATA2LSB
4 Type: ET_EXEC
5 Machine: EM_X86_64
6 Entry: 0x400000
0 !ELF
1 Header: !Header
2 Class: ELFCLASS32
3 Data: ELFDATA2LSB
4 Type: ET_EXEC
5 Machine: EM_386
0 !ELF
1 Header: !Header
2 Class: ELFCLASS64
3 Data: ELFDATA2LSB
4 Type: ET_EXEC
5 Machine: EM_X86_64
0 RUN: yaml2obj -format=elf %p/Inputs/ELF/Header.yaml | llvm-readobj -file-headers - | FileCheck %s
1
2 // CHECK: File:
3 // CHECK-NEXT: Format: ELF64-x86-64
4 // CHECK-NEXT: Arch: x86_64
5 // CHECK-NEXT: AddressSize: 64bit
6 // CHECK-NEXT: LoadName:
7 // CHECK-NEXT: ElfHeader {
8 // CHECK-NEXT: Ident {
9 // CHECK-NEXT: Magic: (7F 45 4C 46)
10 // CHECK-NEXT: Class: 64-bit (0x2)
11 // CHECK-NEXT: DataEncoding: LittleEndian (0x1)
12 // CHECK-NEXT: FileVersion: 1
13 // CHECK-NEXT: OS/ABI: SystemV (0x0)
14 // CHECK-NEXT: ABIVersion: 0
15 // CHECK-NEXT: Unused: (00 00 00 00 00 00 00)
16 // CHECK-NEXT: }
17 // CHECK-NEXT: Type: Executable (0x2)
18 // CHECK-NEXT: Machine: EM_X86_64 (0x3E)
19 // CHECK-NEXT: Version: 1
20 // CHECK-NEXT: Entry: 0x400000
21 // CHECK-NEXT: ProgramHeaderOffset: 0x0
22 // CHECK-NEXT: SectionHeaderOffset: 0x0
23 // CHECK-NEXT: Flags [ (0x0)
24 // CHECK-NEXT: ]
25 // CHECK-NEXT: HeaderSize: 64
26 // CHECK-NEXT: ProgramHeaderEntrySize: 0
27 // CHECK-NEXT: ProgramHeaderCount: 0
28 // CHECK-NEXT: SectionHeaderEntrySize: 0
29 // CHECK-NEXT: SectionHeaderCount: 0
30 // CHECK-NEXT: StringTableSectionIndex: 0
31 // CHECK-NEXT: }
0 RUN: yaml2obj -format=elf %p/Inputs/ELF/LE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE64
1 RUN: yaml2obj -format=elf %p/Inputs/ELF/BE64.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE64
2 RUN: yaml2obj -format=elf %p/Inputs/ELF/LE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix LE32
3 RUN: yaml2obj -format=elf %p/Inputs/ELF/BE32.yaml | llvm-readobj -file-headers - | FileCheck %s --check-prefix BE32
4
5 LE64: Class: 64-bit (0x2)
6 LE64-NEXT: DataEncoding: LittleEndian (0x1)
7
8 BE64: Class: 64-bit (0x2)
9 BE64-NEXT: DataEncoding: BigEndian (0x2)
10
11 LE32: Class: 32-bit (0x1)
12 LE32-NEXT: DataEncoding: LittleEndian (0x1)
13
14 BE32: Class: 32-bit (0x1)
15 BE32-NEXT: DataEncoding: BigEndian (0x2)
22 add_llvm_utility(yaml2obj
33 yaml2obj.cpp
44 yaml2coff.cpp
5 yaml2elf.cpp
56 )
67
78 target_link_libraries(yaml2obj LLVMSupport)
0 //===- yaml2elf - Convert YAML to a ELF object file -----------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 ///
9 /// \file
10 /// \brief The ELF component of yaml2obj.
11 ///
12 //===----------------------------------------------------------------------===//
13
14 #include "yaml2obj.h"
15 #include "llvm/Object/ELF.h"
16 #include "llvm/Object/ELFYAML.h"
17 #include "llvm/Support/ELF.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/YAMLTraits.h"
20 #include "llvm/Support/raw_ostream.h"
21
22 using namespace llvm;
23
24 template
25 static void writeELF(raw_ostream &OS, const ELFYAML::Object &Doc) {
26 const ELFYAML::Header &Hdr = Doc.Header;
27 using namespace llvm::ELF;
28 using namespace llvm::object;
29 typename ELFObjectFile::Elf_Ehdr Header;
30 memset(&Header, 0, sizeof(Header));
31 Header.e_ident[EI_MAG0] = 0x7f;
32 Header.e_ident[EI_MAG1] = 'E';
33 Header.e_ident[EI_MAG2] = 'L';
34 Header.e_ident[EI_MAG3] = 'F';
35 Header.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
36 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
37 Header.e_ident[EI_DATA] = IsLittleEndian ? ELFDATA2LSB : ELFDATA2MSB;
38
39 Header.e_ident[EI_VERSION] = EV_CURRENT;
40
41 // TODO: Implement ELF_ELFOSABI enum.
42 Header.e_ident[EI_OSABI] = ELFOSABI_NONE;
43 // TODO: Implement ELF_ABIVERSION enum.
44 Header.e_ident[EI_ABIVERSION] = 0;
45 Header.e_type = Hdr.Type;
46 Header.e_machine = Hdr.Machine;
47 Header.e_version = EV_CURRENT;
48 Header.e_entry = Hdr.Entry;
49 Header.e_ehsize = sizeof(Header);
50
51 // TODO: Section headers and program headers.
52
53 OS.write((const char *)&Header, sizeof(Header));
54 }
55
56 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf) {
57 yaml::Input YIn(Buf->getBuffer());
58 ELFYAML::Object Doc;
59 YIn >> Doc;
60 if (YIn.error()) {
61 errs() << "yaml2obj: Failed to parse YAML file!\n";
62 return 1;
63 }
64 if (Doc.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
65 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
66 writeELF >(outs(), Doc);
67 else
68 writeELF >(outs(), Doc);
69 } else {
70 if (Doc.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB))
71 writeELF >(outs(), Doc);
72 else
73 writeELF >(outs(), Doc);
74 }
75
76 return 0;
77 }
3737 // them appropriately requires some work in the YAML parser and the YAMLIO
3838 // library.
3939 enum YAMLObjectFormat {
40 YOF_COFF
40 YOF_COFF,
41 YOF_ELF
4142 };
4243
4344 cl::opt Format(
4546 cl::desc("Interpret input as this type of object file"),
4647 cl::values(
4748 clEnumValN(YOF_COFF, "coff", "COFF object file format"),
49 clEnumValN(YOF_ELF, "elf", "ELF object file format"),
4850 clEnumValEnd));
4951
5052
5961 return 1;
6062 if (Format == YOF_COFF) {
6163 return yaml2coff(outs(), Buf.get());
64 } else if (Format == YOF_ELF) {
65 return yaml2elf(outs(), Buf.get());
6266 } else {
6367 errs() << "Not yet implemented\n";
6468 return 1;
1616 class MemoryBuffer;
1717 }
1818 int yaml2coff(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf);
19 int yaml2elf(llvm::raw_ostream &Out, llvm::MemoryBuffer *Buf);
1920
2021 #endif