[llvm-exegesis] Fix missing std::move.
[llvm-complete.git] / lib / ObjectYAML / ELFYAML.cpp
bloba381a63d600641825a01aae395486e5054390fbd
1 //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
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 // This file defines classes for handling the YAML representation of ELF.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ObjectYAML/ELFYAML.h"
15 #include "llvm/ADT/StringRef.h"
16 #include "llvm/BinaryFormat/ELF.h"
17 #include "llvm/Support/Casting.h"
18 #include "llvm/Support/ErrorHandling.h"
19 #include "llvm/Support/MipsABIFlags.h"
20 #include "llvm/Support/YAMLTraits.h"
21 #include <cassert>
22 #include <cstdint>
24 namespace llvm {
26 ELFYAML::Section::~Section() = default;
28 namespace yaml {
30 void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(
31 IO &IO, ELFYAML::ELF_ET &Value) {
32 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
33 ECase(ET_NONE);
34 ECase(ET_REL);
35 ECase(ET_EXEC);
36 ECase(ET_DYN);
37 ECase(ET_CORE);
38 #undef ECase
39 IO.enumFallback<Hex16>(Value);
42 void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration(
43 IO &IO, ELFYAML::ELF_PT &Value) {
44 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
45 ECase(PT_NULL);
46 ECase(PT_LOAD);
47 ECase(PT_DYNAMIC);
48 ECase(PT_INTERP);
49 ECase(PT_NOTE);
50 ECase(PT_SHLIB);
51 ECase(PT_PHDR);
52 ECase(PT_TLS);
53 ECase(PT_GNU_EH_FRAME);
54 #undef ECase
55 IO.enumFallback<Hex32>(Value);
58 void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
59 IO &IO, ELFYAML::ELF_EM &Value) {
60 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
61 ECase(EM_NONE);
62 ECase(EM_M32);
63 ECase(EM_SPARC);
64 ECase(EM_386);
65 ECase(EM_68K);
66 ECase(EM_88K);
67 ECase(EM_IAMCU);
68 ECase(EM_860);
69 ECase(EM_MIPS);
70 ECase(EM_S370);
71 ECase(EM_MIPS_RS3_LE);
72 ECase(EM_PARISC);
73 ECase(EM_VPP500);
74 ECase(EM_SPARC32PLUS);
75 ECase(EM_960);
76 ECase(EM_PPC);
77 ECase(EM_PPC64);
78 ECase(EM_S390);
79 ECase(EM_SPU);
80 ECase(EM_V800);
81 ECase(EM_FR20);
82 ECase(EM_RH32);
83 ECase(EM_RCE);
84 ECase(EM_ARM);
85 ECase(EM_ALPHA);
86 ECase(EM_SH);
87 ECase(EM_SPARCV9);
88 ECase(EM_TRICORE);
89 ECase(EM_ARC);
90 ECase(EM_H8_300);
91 ECase(EM_H8_300H);
92 ECase(EM_H8S);
93 ECase(EM_H8_500);
94 ECase(EM_IA_64);
95 ECase(EM_MIPS_X);
96 ECase(EM_COLDFIRE);
97 ECase(EM_68HC12);
98 ECase(EM_MMA);
99 ECase(EM_PCP);
100 ECase(EM_NCPU);
101 ECase(EM_NDR1);
102 ECase(EM_STARCORE);
103 ECase(EM_ME16);
104 ECase(EM_ST100);
105 ECase(EM_TINYJ);
106 ECase(EM_X86_64);
107 ECase(EM_PDSP);
108 ECase(EM_PDP10);
109 ECase(EM_PDP11);
110 ECase(EM_FX66);
111 ECase(EM_ST9PLUS);
112 ECase(EM_ST7);
113 ECase(EM_68HC16);
114 ECase(EM_68HC11);
115 ECase(EM_68HC08);
116 ECase(EM_68HC05);
117 ECase(EM_SVX);
118 ECase(EM_ST19);
119 ECase(EM_VAX);
120 ECase(EM_CRIS);
121 ECase(EM_JAVELIN);
122 ECase(EM_FIREPATH);
123 ECase(EM_ZSP);
124 ECase(EM_MMIX);
125 ECase(EM_HUANY);
126 ECase(EM_PRISM);
127 ECase(EM_AVR);
128 ECase(EM_FR30);
129 ECase(EM_D10V);
130 ECase(EM_D30V);
131 ECase(EM_V850);
132 ECase(EM_M32R);
133 ECase(EM_MN10300);
134 ECase(EM_MN10200);
135 ECase(EM_PJ);
136 ECase(EM_OPENRISC);
137 ECase(EM_ARC_COMPACT);
138 ECase(EM_XTENSA);
139 ECase(EM_VIDEOCORE);
140 ECase(EM_TMM_GPP);
141 ECase(EM_NS32K);
142 ECase(EM_TPC);
143 ECase(EM_SNP1K);
144 ECase(EM_ST200);
145 ECase(EM_IP2K);
146 ECase(EM_MAX);
147 ECase(EM_CR);
148 ECase(EM_F2MC16);
149 ECase(EM_MSP430);
150 ECase(EM_BLACKFIN);
151 ECase(EM_SE_C33);
152 ECase(EM_SEP);
153 ECase(EM_ARCA);
154 ECase(EM_UNICORE);
155 ECase(EM_EXCESS);
156 ECase(EM_DXP);
157 ECase(EM_ALTERA_NIOS2);
158 ECase(EM_CRX);
159 ECase(EM_XGATE);
160 ECase(EM_C166);
161 ECase(EM_M16C);
162 ECase(EM_DSPIC30F);
163 ECase(EM_CE);
164 ECase(EM_M32C);
165 ECase(EM_TSK3000);
166 ECase(EM_RS08);
167 ECase(EM_SHARC);
168 ECase(EM_ECOG2);
169 ECase(EM_SCORE7);
170 ECase(EM_DSP24);
171 ECase(EM_VIDEOCORE3);
172 ECase(EM_LATTICEMICO32);
173 ECase(EM_SE_C17);
174 ECase(EM_TI_C6000);
175 ECase(EM_TI_C2000);
176 ECase(EM_TI_C5500);
177 ECase(EM_MMDSP_PLUS);
178 ECase(EM_CYPRESS_M8C);
179 ECase(EM_R32C);
180 ECase(EM_TRIMEDIA);
181 ECase(EM_HEXAGON);
182 ECase(EM_8051);
183 ECase(EM_STXP7X);
184 ECase(EM_NDS32);
185 ECase(EM_ECOG1);
186 ECase(EM_ECOG1X);
187 ECase(EM_MAXQ30);
188 ECase(EM_XIMO16);
189 ECase(EM_MANIK);
190 ECase(EM_CRAYNV2);
191 ECase(EM_RX);
192 ECase(EM_METAG);
193 ECase(EM_MCST_ELBRUS);
194 ECase(EM_ECOG16);
195 ECase(EM_CR16);
196 ECase(EM_ETPU);
197 ECase(EM_SLE9X);
198 ECase(EM_L10M);
199 ECase(EM_K10M);
200 ECase(EM_AARCH64);
201 ECase(EM_AVR32);
202 ECase(EM_STM8);
203 ECase(EM_TILE64);
204 ECase(EM_TILEPRO);
205 ECase(EM_CUDA);
206 ECase(EM_TILEGX);
207 ECase(EM_CLOUDSHIELD);
208 ECase(EM_COREA_1ST);
209 ECase(EM_COREA_2ND);
210 ECase(EM_ARC_COMPACT2);
211 ECase(EM_OPEN8);
212 ECase(EM_RL78);
213 ECase(EM_VIDEOCORE5);
214 ECase(EM_78KOR);
215 ECase(EM_56800EX);
216 ECase(EM_AMDGPU);
217 ECase(EM_RISCV);
218 ECase(EM_LANAI);
219 ECase(EM_BPF);
220 #undef ECase
223 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
224 IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
225 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
226 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
227 // here.
228 ECase(ELFCLASS32);
229 ECase(ELFCLASS64);
230 #undef ECase
233 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
234 IO &IO, ELFYAML::ELF_ELFDATA &Value) {
235 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
236 // Since the semantics of ELFDATANONE is "invalid", just don't accept it
237 // here.
238 ECase(ELFDATA2LSB);
239 ECase(ELFDATA2MSB);
240 #undef ECase
243 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
244 IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
245 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
246 ECase(ELFOSABI_NONE);
247 ECase(ELFOSABI_HPUX);
248 ECase(ELFOSABI_NETBSD);
249 ECase(ELFOSABI_GNU);
250 ECase(ELFOSABI_HURD);
251 ECase(ELFOSABI_SOLARIS);
252 ECase(ELFOSABI_AIX);
253 ECase(ELFOSABI_IRIX);
254 ECase(ELFOSABI_FREEBSD);
255 ECase(ELFOSABI_TRU64);
256 ECase(ELFOSABI_MODESTO);
257 ECase(ELFOSABI_OPENBSD);
258 ECase(ELFOSABI_OPENVMS);
259 ECase(ELFOSABI_NSK);
260 ECase(ELFOSABI_AROS);
261 ECase(ELFOSABI_FENIXOS);
262 ECase(ELFOSABI_CLOUDABI);
263 ECase(ELFOSABI_AMDGPU_HSA);
264 ECase(ELFOSABI_AMDGPU_PAL);
265 ECase(ELFOSABI_AMDGPU_MESA3D);
266 ECase(ELFOSABI_ARM);
267 ECase(ELFOSABI_C6000_ELFABI);
268 ECase(ELFOSABI_C6000_LINUX);
269 ECase(ELFOSABI_STANDALONE);
270 #undef ECase
273 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
274 ELFYAML::ELF_EF &Value) {
275 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
276 assert(Object && "The IO context is not initialized");
277 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
278 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
279 switch (Object->Header.Machine) {
280 case ELF::EM_ARM:
281 BCase(EF_ARM_SOFT_FLOAT);
282 BCase(EF_ARM_VFP_FLOAT);
283 BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK);
284 BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK);
285 BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK);
286 BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK);
287 BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK);
288 BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK);
289 break;
290 case ELF::EM_MIPS:
291 BCase(EF_MIPS_NOREORDER);
292 BCase(EF_MIPS_PIC);
293 BCase(EF_MIPS_CPIC);
294 BCase(EF_MIPS_ABI2);
295 BCase(EF_MIPS_32BITMODE);
296 BCase(EF_MIPS_FP64);
297 BCase(EF_MIPS_NAN2008);
298 BCase(EF_MIPS_MICROMIPS);
299 BCase(EF_MIPS_ARCH_ASE_M16);
300 BCase(EF_MIPS_ARCH_ASE_MDMX);
301 BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI);
302 BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI);
303 BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI);
304 BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI);
305 BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH);
306 BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH);
307 BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH);
308 BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH);
309 BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH);
310 BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH);
311 BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH);
312 BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH);
313 BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH);
314 BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH);
315 BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH);
316 BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH);
317 BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH);
318 BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH);
319 BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH);
320 BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH);
321 BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH);
322 BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH);
323 BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH);
324 BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH);
325 BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH);
326 BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH);
327 BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH);
328 BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH);
329 BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH);
330 BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH);
331 BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH);
332 BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH);
333 BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH);
334 break;
335 case ELF::EM_HEXAGON:
336 BCase(EF_HEXAGON_MACH_V2);
337 BCase(EF_HEXAGON_MACH_V3);
338 BCase(EF_HEXAGON_MACH_V4);
339 BCase(EF_HEXAGON_MACH_V5);
340 BCase(EF_HEXAGON_ISA_V2);
341 BCase(EF_HEXAGON_ISA_V3);
342 BCase(EF_HEXAGON_ISA_V4);
343 BCase(EF_HEXAGON_ISA_V5);
344 break;
345 case ELF::EM_AVR:
346 BCase(EF_AVR_ARCH_AVR1);
347 BCase(EF_AVR_ARCH_AVR2);
348 BCase(EF_AVR_ARCH_AVR25);
349 BCase(EF_AVR_ARCH_AVR3);
350 BCase(EF_AVR_ARCH_AVR31);
351 BCase(EF_AVR_ARCH_AVR35);
352 BCase(EF_AVR_ARCH_AVR4);
353 BCase(EF_AVR_ARCH_AVR51);
354 BCase(EF_AVR_ARCH_AVR6);
355 BCase(EF_AVR_ARCH_AVRTINY);
356 BCase(EF_AVR_ARCH_XMEGA1);
357 BCase(EF_AVR_ARCH_XMEGA2);
358 BCase(EF_AVR_ARCH_XMEGA3);
359 BCase(EF_AVR_ARCH_XMEGA4);
360 BCase(EF_AVR_ARCH_XMEGA5);
361 BCase(EF_AVR_ARCH_XMEGA6);
362 BCase(EF_AVR_ARCH_XMEGA7);
363 break;
364 case ELF::EM_RISCV:
365 BCase(EF_RISCV_RVC);
366 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI);
367 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI);
368 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI);
369 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI);
370 BCase(EF_RISCV_RVE);
371 break;
372 case ELF::EM_AMDGPU:
373 BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH);
374 BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH);
375 BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH);
376 BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH);
377 BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH);
378 BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH);
379 BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH);
380 BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH);
381 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH);
382 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH);
383 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH);
384 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH);
385 BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH);
386 BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH);
387 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH);
388 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH);
389 BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH);
390 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH);
391 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH);
392 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH);
393 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH);
394 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH);
395 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH);
396 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH);
397 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH);
398 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH);
399 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH);
400 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH);
401 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH);
402 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH);
403 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH);
404 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH);
405 BCase(EF_AMDGPU_XNACK);
406 break;
407 case ELF::EM_X86_64:
408 break;
409 default:
410 llvm_unreachable("Unsupported architecture");
412 #undef BCase
413 #undef BCaseMask
416 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
417 IO &IO, ELFYAML::ELF_SHT &Value) {
418 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
419 assert(Object && "The IO context is not initialized");
420 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
421 ECase(SHT_NULL);
422 ECase(SHT_PROGBITS);
423 ECase(SHT_SYMTAB);
424 // FIXME: Issue a diagnostic with this information.
425 ECase(SHT_STRTAB);
426 ECase(SHT_RELA);
427 ECase(SHT_HASH);
428 ECase(SHT_DYNAMIC);
429 ECase(SHT_NOTE);
430 ECase(SHT_NOBITS);
431 ECase(SHT_REL);
432 ECase(SHT_SHLIB);
433 ECase(SHT_DYNSYM);
434 ECase(SHT_INIT_ARRAY);
435 ECase(SHT_FINI_ARRAY);
436 ECase(SHT_PREINIT_ARRAY);
437 ECase(SHT_GROUP);
438 ECase(SHT_SYMTAB_SHNDX);
439 ECase(SHT_RELR);
440 ECase(SHT_LOOS);
441 ECase(SHT_ANDROID_REL);
442 ECase(SHT_ANDROID_RELA);
443 ECase(SHT_ANDROID_RELR);
444 ECase(SHT_LLVM_ODRTAB);
445 ECase(SHT_LLVM_LINKER_OPTIONS);
446 ECase(SHT_LLVM_CALL_GRAPH_PROFILE);
447 ECase(SHT_LLVM_ADDRSIG);
448 ECase(SHT_GNU_ATTRIBUTES);
449 ECase(SHT_GNU_HASH);
450 ECase(SHT_GNU_verdef);
451 ECase(SHT_GNU_verneed);
452 ECase(SHT_GNU_versym);
453 ECase(SHT_HIOS);
454 ECase(SHT_LOPROC);
455 switch (Object->Header.Machine) {
456 case ELF::EM_ARM:
457 ECase(SHT_ARM_EXIDX);
458 ECase(SHT_ARM_PREEMPTMAP);
459 ECase(SHT_ARM_ATTRIBUTES);
460 ECase(SHT_ARM_DEBUGOVERLAY);
461 ECase(SHT_ARM_OVERLAYSECTION);
462 break;
463 case ELF::EM_HEXAGON:
464 ECase(SHT_HEX_ORDERED);
465 break;
466 case ELF::EM_X86_64:
467 ECase(SHT_X86_64_UNWIND);
468 break;
469 case ELF::EM_MIPS:
470 ECase(SHT_MIPS_REGINFO);
471 ECase(SHT_MIPS_OPTIONS);
472 ECase(SHT_MIPS_ABIFLAGS);
473 break;
474 default:
475 // Nothing to do.
476 break;
478 #undef ECase
481 void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO,
482 ELFYAML::ELF_PF &Value) {
483 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
484 BCase(PF_X);
485 BCase(PF_W);
486 BCase(PF_R);
489 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
490 ELFYAML::ELF_SHF &Value) {
491 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
492 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
493 BCase(SHF_WRITE);
494 BCase(SHF_ALLOC);
495 BCase(SHF_EXCLUDE);
496 BCase(SHF_EXECINSTR);
497 BCase(SHF_MERGE);
498 BCase(SHF_STRINGS);
499 BCase(SHF_INFO_LINK);
500 BCase(SHF_LINK_ORDER);
501 BCase(SHF_OS_NONCONFORMING);
502 BCase(SHF_GROUP);
503 BCase(SHF_TLS);
504 BCase(SHF_COMPRESSED);
505 switch (Object->Header.Machine) {
506 case ELF::EM_ARM:
507 BCase(SHF_ARM_PURECODE);
508 break;
509 case ELF::EM_HEXAGON:
510 BCase(SHF_HEX_GPREL);
511 break;
512 case ELF::EM_MIPS:
513 BCase(SHF_MIPS_NODUPES);
514 BCase(SHF_MIPS_NAMES);
515 BCase(SHF_MIPS_LOCAL);
516 BCase(SHF_MIPS_NOSTRIP);
517 BCase(SHF_MIPS_GPREL);
518 BCase(SHF_MIPS_MERGE);
519 BCase(SHF_MIPS_ADDR);
520 BCase(SHF_MIPS_STRING);
521 break;
522 case ELF::EM_X86_64:
523 BCase(SHF_X86_64_LARGE);
524 break;
525 default:
526 // Nothing to do.
527 break;
529 #undef BCase
532 void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration(
533 IO &IO, ELFYAML::ELF_SHN &Value) {
534 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
535 ECase(SHN_UNDEF);
536 ECase(SHN_LORESERVE);
537 ECase(SHN_LOPROC);
538 ECase(SHN_HIPROC);
539 ECase(SHN_LOOS);
540 ECase(SHN_HIOS);
541 ECase(SHN_ABS);
542 ECase(SHN_COMMON);
543 ECase(SHN_XINDEX);
544 ECase(SHN_HIRESERVE);
545 ECase(SHN_HEXAGON_SCOMMON);
546 ECase(SHN_HEXAGON_SCOMMON_1);
547 ECase(SHN_HEXAGON_SCOMMON_2);
548 ECase(SHN_HEXAGON_SCOMMON_4);
549 ECase(SHN_HEXAGON_SCOMMON_8);
550 #undef ECase
551 IO.enumFallback<Hex32>(Value);
554 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
555 IO &IO, ELFYAML::ELF_STT &Value) {
556 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
557 ECase(STT_NOTYPE);
558 ECase(STT_OBJECT);
559 ECase(STT_FUNC);
560 ECase(STT_SECTION);
561 ECase(STT_FILE);
562 ECase(STT_COMMON);
563 ECase(STT_TLS);
564 ECase(STT_GNU_IFUNC);
565 #undef ECase
568 void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration(
569 IO &IO, ELFYAML::ELF_STV &Value) {
570 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
571 ECase(STV_DEFAULT);
572 ECase(STV_INTERNAL);
573 ECase(STV_HIDDEN);
574 ECase(STV_PROTECTED);
575 #undef ECase
578 void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO,
579 ELFYAML::ELF_STO &Value) {
580 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
581 assert(Object && "The IO context is not initialized");
582 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
583 switch (Object->Header.Machine) {
584 case ELF::EM_MIPS:
585 BCase(STO_MIPS_OPTIONAL);
586 BCase(STO_MIPS_PLT);
587 BCase(STO_MIPS_PIC);
588 BCase(STO_MIPS_MICROMIPS);
589 break;
590 default:
591 break; // Nothing to do
593 #undef BCase
594 #undef BCaseMask
597 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
598 IO &IO, ELFYAML::ELF_RSS &Value) {
599 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
600 ECase(RSS_UNDEF);
601 ECase(RSS_GP);
602 ECase(RSS_GP0);
603 ECase(RSS_LOC);
604 #undef ECase
607 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
608 IO &IO, ELFYAML::ELF_REL &Value) {
609 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
610 assert(Object && "The IO context is not initialized");
611 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
612 switch (Object->Header.Machine) {
613 case ELF::EM_X86_64:
614 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
615 break;
616 case ELF::EM_MIPS:
617 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
618 break;
619 case ELF::EM_HEXAGON:
620 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
621 break;
622 case ELF::EM_386:
623 case ELF::EM_IAMCU:
624 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
625 break;
626 case ELF::EM_AARCH64:
627 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
628 break;
629 case ELF::EM_ARM:
630 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
631 break;
632 case ELF::EM_ARC:
633 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
634 break;
635 case ELF::EM_RISCV:
636 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
637 break;
638 case ELF::EM_LANAI:
639 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
640 break;
641 case ELF::EM_AMDGPU:
642 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
643 break;
644 case ELF::EM_BPF:
645 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
646 break;
647 default:
648 llvm_unreachable("Unsupported architecture");
650 #undef ELF_RELOC
651 IO.enumFallback<Hex32>(Value);
654 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
655 IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
656 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
657 ECase(REG_NONE);
658 ECase(REG_32);
659 ECase(REG_64);
660 ECase(REG_128);
661 #undef ECase
664 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
665 IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
666 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
667 ECase(FP_ANY);
668 ECase(FP_DOUBLE);
669 ECase(FP_SINGLE);
670 ECase(FP_SOFT);
671 ECase(FP_OLD_64);
672 ECase(FP_XX);
673 ECase(FP_64);
674 ECase(FP_64A);
675 #undef ECase
678 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
679 IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
680 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
681 ECase(EXT_NONE);
682 ECase(EXT_XLR);
683 ECase(EXT_OCTEON2);
684 ECase(EXT_OCTEONP);
685 ECase(EXT_LOONGSON_3A);
686 ECase(EXT_OCTEON);
687 ECase(EXT_5900);
688 ECase(EXT_4650);
689 ECase(EXT_4010);
690 ECase(EXT_4100);
691 ECase(EXT_3900);
692 ECase(EXT_10000);
693 ECase(EXT_SB1);
694 ECase(EXT_4111);
695 ECase(EXT_4120);
696 ECase(EXT_5400);
697 ECase(EXT_5500);
698 ECase(EXT_LOONGSON_2E);
699 ECase(EXT_LOONGSON_2F);
700 ECase(EXT_OCTEON3);
701 #undef ECase
704 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
705 IO &IO, ELFYAML::MIPS_ISA &Value) {
706 IO.enumCase(Value, "MIPS1", 1);
707 IO.enumCase(Value, "MIPS2", 2);
708 IO.enumCase(Value, "MIPS3", 3);
709 IO.enumCase(Value, "MIPS4", 4);
710 IO.enumCase(Value, "MIPS5", 5);
711 IO.enumCase(Value, "MIPS32", 32);
712 IO.enumCase(Value, "MIPS64", 64);
715 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
716 IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
717 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
718 BCase(DSP);
719 BCase(DSPR2);
720 BCase(EVA);
721 BCase(MCU);
722 BCase(MDMX);
723 BCase(MIPS3D);
724 BCase(MT);
725 BCase(SMARTMIPS);
726 BCase(VIRT);
727 BCase(MSA);
728 BCase(MIPS16);
729 BCase(MICROMIPS);
730 BCase(XPA);
731 #undef BCase
734 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
735 IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
736 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
737 BCase(ODDSPREG);
738 #undef BCase
741 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
742 ELFYAML::FileHeader &FileHdr) {
743 IO.mapRequired("Class", FileHdr.Class);
744 IO.mapRequired("Data", FileHdr.Data);
745 IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
746 IO.mapRequired("Type", FileHdr.Type);
747 IO.mapRequired("Machine", FileHdr.Machine);
748 IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
749 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
752 void MappingTraits<ELFYAML::ProgramHeader>::mapping(
753 IO &IO, ELFYAML::ProgramHeader &Phdr) {
754 IO.mapRequired("Type", Phdr.Type);
755 IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
756 IO.mapOptional("Sections", Phdr.Sections);
757 IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
758 IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
759 IO.mapOptional("Align", Phdr.Align);
762 namespace {
764 struct NormalizedOther {
765 NormalizedOther(IO &)
766 : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {}
767 NormalizedOther(IO &, uint8_t Original)
768 : Visibility(Original & 0x3), Other(Original & ~0x3) {}
770 uint8_t denormalize(IO &) { return Visibility | Other; }
772 ELFYAML::ELF_STV Visibility;
773 ELFYAML::ELF_STO Other;
776 } // end anonymous namespace
778 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
779 IO.mapOptional("Name", Symbol.Name, StringRef());
780 IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
781 IO.mapOptional("Section", Symbol.Section, StringRef());
782 IO.mapOptional("Index", Symbol.Index);
783 IO.mapOptional("Value", Symbol.Value, Hex64(0));
784 IO.mapOptional("Size", Symbol.Size, Hex64(0));
786 MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other);
787 IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0));
788 IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0));
791 StringRef MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
792 ELFYAML::Symbol &Symbol) {
793 if (Symbol.Index && Symbol.Section.data()) {
794 return "Index and Section cannot both be specified for Symbol";
796 if (Symbol.Index && *Symbol.Index == ELFYAML::ELF_SHN(ELF::SHN_XINDEX)) {
797 return "Large indexes are not supported";
799 if (Symbol.Index && *Symbol.Index < ELFYAML::ELF_SHN(ELF::SHN_LORESERVE)) {
800 return "Use a section name to define which section a symbol is defined in";
802 return StringRef();
805 void MappingTraits<ELFYAML::LocalGlobalWeakSymbols>::mapping(
806 IO &IO, ELFYAML::LocalGlobalWeakSymbols &Symbols) {
807 IO.mapOptional("Local", Symbols.Local);
808 IO.mapOptional("Global", Symbols.Global);
809 IO.mapOptional("Weak", Symbols.Weak);
812 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
813 IO.mapOptional("Name", Section.Name, StringRef());
814 IO.mapRequired("Type", Section.Type);
815 IO.mapOptional("Flags", Section.Flags, ELFYAML::ELF_SHF(0));
816 IO.mapOptional("Address", Section.Address, Hex64(0));
817 IO.mapOptional("Link", Section.Link, StringRef());
818 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
819 IO.mapOptional("EntSize", Section.EntSize);
820 IO.mapOptional("Info", Section.Info, StringRef());
823 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
824 commonSectionMapping(IO, Section);
825 IO.mapOptional("Content", Section.Content);
826 IO.mapOptional("Size", Section.Size, Hex64(Section.Content.binary_size()));
829 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
830 commonSectionMapping(IO, Section);
831 IO.mapOptional("Size", Section.Size, Hex64(0));
834 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
835 commonSectionMapping(IO, Section);
836 IO.mapOptional("Relocations", Section.Relocations);
839 static void groupSectionMapping(IO &IO, ELFYAML::Group &group) {
840 commonSectionMapping(IO, group);
841 IO.mapRequired("Members", group.Members);
844 void MappingTraits<ELFYAML::SectionOrType>::mapping(
845 IO &IO, ELFYAML::SectionOrType &sectionOrType) {
846 IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
849 void MappingTraits<ELFYAML::SectionName>::mapping(
850 IO &IO, ELFYAML::SectionName &sectionName) {
851 IO.mapRequired("Section", sectionName.Section);
854 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
855 commonSectionMapping(IO, Section);
856 IO.mapOptional("Version", Section.Version, Hex16(0));
857 IO.mapRequired("ISA", Section.ISALevel);
858 IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
859 IO.mapOptional("ISAExtension", Section.ISAExtension,
860 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
861 IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
862 IO.mapOptional("FpABI", Section.FpABI,
863 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
864 IO.mapOptional("GPRSize", Section.GPRSize,
865 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
866 IO.mapOptional("CPR1Size", Section.CPR1Size,
867 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
868 IO.mapOptional("CPR2Size", Section.CPR2Size,
869 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
870 IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
871 IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
874 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
875 IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
876 ELFYAML::ELF_SHT sectionType;
877 if (IO.outputting())
878 sectionType = Section->Type;
879 else
880 IO.mapRequired("Type", sectionType);
882 switch (sectionType) {
883 case ELF::SHT_REL:
884 case ELF::SHT_RELA:
885 if (!IO.outputting())
886 Section.reset(new ELFYAML::RelocationSection());
887 sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
888 break;
889 case ELF::SHT_GROUP:
890 if (!IO.outputting())
891 Section.reset(new ELFYAML::Group());
892 groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
893 break;
894 case ELF::SHT_NOBITS:
895 if (!IO.outputting())
896 Section.reset(new ELFYAML::NoBitsSection());
897 sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
898 break;
899 case ELF::SHT_MIPS_ABIFLAGS:
900 if (!IO.outputting())
901 Section.reset(new ELFYAML::MipsABIFlags());
902 sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
903 break;
904 default:
905 if (!IO.outputting())
906 Section.reset(new ELFYAML::RawContentSection());
907 sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
911 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
912 IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
913 const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
914 if (!RawSection || RawSection->Size >= RawSection->Content.binary_size())
915 return StringRef();
916 return "Section size must be greater or equal to the content size";
919 namespace {
921 struct NormalizedMips64RelType {
922 NormalizedMips64RelType(IO &)
923 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
924 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
925 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
926 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
927 NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
928 : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
929 Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
931 ELFYAML::ELF_REL denormalize(IO &) {
932 ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
933 return Res;
936 ELFYAML::ELF_REL Type;
937 ELFYAML::ELF_REL Type2;
938 ELFYAML::ELF_REL Type3;
939 ELFYAML::ELF_RSS SpecSym;
942 } // end anonymous namespace
944 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
945 ELFYAML::Relocation &Rel) {
946 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
947 assert(Object && "The IO context is not initialized");
949 IO.mapRequired("Offset", Rel.Offset);
950 IO.mapOptional("Symbol", Rel.Symbol);
952 if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
953 Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
954 MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
955 IO, Rel.Type);
956 IO.mapRequired("Type", Key->Type);
957 IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
958 IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
959 IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
960 } else
961 IO.mapRequired("Type", Rel.Type);
963 IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
966 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
967 assert(!IO.getContext() && "The IO context is initialized already");
968 IO.setContext(&Object);
969 IO.mapTag("!ELF", true);
970 IO.mapRequired("FileHeader", Object.Header);
971 IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
972 IO.mapOptional("Sections", Object.Sections);
973 IO.mapOptional("Symbols", Object.Symbols);
974 IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
975 IO.setContext(nullptr);
978 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
979 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
980 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
981 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
982 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
984 } // end namespace yaml
986 } // end namespace llvm