[obj2yaml] - Support PPC64 relocation types.
[llvm-complete.git] / lib / ObjectYAML / ELFYAML.cpp
blob5d16f0bd074e6a33ba1197fa7ab5f961a97bd4cd
1 //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines classes for handling the YAML representation of ELF.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/ObjectYAML/ELFYAML.h"
14 #include "llvm/ADT/MapVector.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 "llvm/Support/WithColor.h"
22 #include <cassert>
23 #include <cstdint>
25 namespace llvm {
27 ELFYAML::Section::~Section() = default;
29 namespace yaml {
31 void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration(
32 IO &IO, ELFYAML::ELF_ET &Value) {
33 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
34 ECase(ET_NONE);
35 ECase(ET_REL);
36 ECase(ET_EXEC);
37 ECase(ET_DYN);
38 ECase(ET_CORE);
39 #undef ECase
40 IO.enumFallback<Hex16>(Value);
43 void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration(
44 IO &IO, ELFYAML::ELF_PT &Value) {
45 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
46 ECase(PT_NULL);
47 ECase(PT_LOAD);
48 ECase(PT_DYNAMIC);
49 ECase(PT_INTERP);
50 ECase(PT_NOTE);
51 ECase(PT_SHLIB);
52 ECase(PT_PHDR);
53 ECase(PT_TLS);
54 ECase(PT_GNU_EH_FRAME);
55 ECase(PT_GNU_STACK);
56 ECase(PT_GNU_RELRO);
57 #undef ECase
58 IO.enumFallback<Hex32>(Value);
61 void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration(
62 IO &IO, ELFYAML::ELF_EM &Value) {
63 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
64 ECase(EM_NONE);
65 ECase(EM_M32);
66 ECase(EM_SPARC);
67 ECase(EM_386);
68 ECase(EM_68K);
69 ECase(EM_88K);
70 ECase(EM_IAMCU);
71 ECase(EM_860);
72 ECase(EM_MIPS);
73 ECase(EM_S370);
74 ECase(EM_MIPS_RS3_LE);
75 ECase(EM_PARISC);
76 ECase(EM_VPP500);
77 ECase(EM_SPARC32PLUS);
78 ECase(EM_960);
79 ECase(EM_PPC);
80 ECase(EM_PPC64);
81 ECase(EM_S390);
82 ECase(EM_SPU);
83 ECase(EM_V800);
84 ECase(EM_FR20);
85 ECase(EM_RH32);
86 ECase(EM_RCE);
87 ECase(EM_ARM);
88 ECase(EM_ALPHA);
89 ECase(EM_SH);
90 ECase(EM_SPARCV9);
91 ECase(EM_TRICORE);
92 ECase(EM_ARC);
93 ECase(EM_H8_300);
94 ECase(EM_H8_300H);
95 ECase(EM_H8S);
96 ECase(EM_H8_500);
97 ECase(EM_IA_64);
98 ECase(EM_MIPS_X);
99 ECase(EM_COLDFIRE);
100 ECase(EM_68HC12);
101 ECase(EM_MMA);
102 ECase(EM_PCP);
103 ECase(EM_NCPU);
104 ECase(EM_NDR1);
105 ECase(EM_STARCORE);
106 ECase(EM_ME16);
107 ECase(EM_ST100);
108 ECase(EM_TINYJ);
109 ECase(EM_X86_64);
110 ECase(EM_PDSP);
111 ECase(EM_PDP10);
112 ECase(EM_PDP11);
113 ECase(EM_FX66);
114 ECase(EM_ST9PLUS);
115 ECase(EM_ST7);
116 ECase(EM_68HC16);
117 ECase(EM_68HC11);
118 ECase(EM_68HC08);
119 ECase(EM_68HC05);
120 ECase(EM_SVX);
121 ECase(EM_ST19);
122 ECase(EM_VAX);
123 ECase(EM_CRIS);
124 ECase(EM_JAVELIN);
125 ECase(EM_FIREPATH);
126 ECase(EM_ZSP);
127 ECase(EM_MMIX);
128 ECase(EM_HUANY);
129 ECase(EM_PRISM);
130 ECase(EM_AVR);
131 ECase(EM_FR30);
132 ECase(EM_D10V);
133 ECase(EM_D30V);
134 ECase(EM_V850);
135 ECase(EM_M32R);
136 ECase(EM_MN10300);
137 ECase(EM_MN10200);
138 ECase(EM_PJ);
139 ECase(EM_OPENRISC);
140 ECase(EM_ARC_COMPACT);
141 ECase(EM_XTENSA);
142 ECase(EM_VIDEOCORE);
143 ECase(EM_TMM_GPP);
144 ECase(EM_NS32K);
145 ECase(EM_TPC);
146 ECase(EM_SNP1K);
147 ECase(EM_ST200);
148 ECase(EM_IP2K);
149 ECase(EM_MAX);
150 ECase(EM_CR);
151 ECase(EM_F2MC16);
152 ECase(EM_MSP430);
153 ECase(EM_BLACKFIN);
154 ECase(EM_SE_C33);
155 ECase(EM_SEP);
156 ECase(EM_ARCA);
157 ECase(EM_UNICORE);
158 ECase(EM_EXCESS);
159 ECase(EM_DXP);
160 ECase(EM_ALTERA_NIOS2);
161 ECase(EM_CRX);
162 ECase(EM_XGATE);
163 ECase(EM_C166);
164 ECase(EM_M16C);
165 ECase(EM_DSPIC30F);
166 ECase(EM_CE);
167 ECase(EM_M32C);
168 ECase(EM_TSK3000);
169 ECase(EM_RS08);
170 ECase(EM_SHARC);
171 ECase(EM_ECOG2);
172 ECase(EM_SCORE7);
173 ECase(EM_DSP24);
174 ECase(EM_VIDEOCORE3);
175 ECase(EM_LATTICEMICO32);
176 ECase(EM_SE_C17);
177 ECase(EM_TI_C6000);
178 ECase(EM_TI_C2000);
179 ECase(EM_TI_C5500);
180 ECase(EM_MMDSP_PLUS);
181 ECase(EM_CYPRESS_M8C);
182 ECase(EM_R32C);
183 ECase(EM_TRIMEDIA);
184 ECase(EM_HEXAGON);
185 ECase(EM_8051);
186 ECase(EM_STXP7X);
187 ECase(EM_NDS32);
188 ECase(EM_ECOG1);
189 ECase(EM_ECOG1X);
190 ECase(EM_MAXQ30);
191 ECase(EM_XIMO16);
192 ECase(EM_MANIK);
193 ECase(EM_CRAYNV2);
194 ECase(EM_RX);
195 ECase(EM_METAG);
196 ECase(EM_MCST_ELBRUS);
197 ECase(EM_ECOG16);
198 ECase(EM_CR16);
199 ECase(EM_ETPU);
200 ECase(EM_SLE9X);
201 ECase(EM_L10M);
202 ECase(EM_K10M);
203 ECase(EM_AARCH64);
204 ECase(EM_AVR32);
205 ECase(EM_STM8);
206 ECase(EM_TILE64);
207 ECase(EM_TILEPRO);
208 ECase(EM_CUDA);
209 ECase(EM_TILEGX);
210 ECase(EM_CLOUDSHIELD);
211 ECase(EM_COREA_1ST);
212 ECase(EM_COREA_2ND);
213 ECase(EM_ARC_COMPACT2);
214 ECase(EM_OPEN8);
215 ECase(EM_RL78);
216 ECase(EM_VIDEOCORE5);
217 ECase(EM_78KOR);
218 ECase(EM_56800EX);
219 ECase(EM_AMDGPU);
220 ECase(EM_RISCV);
221 ECase(EM_LANAI);
222 ECase(EM_BPF);
223 #undef ECase
224 IO.enumFallback<Hex16>(Value);
227 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration(
228 IO &IO, ELFYAML::ELF_ELFCLASS &Value) {
229 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
230 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
231 // here.
232 ECase(ELFCLASS32);
233 ECase(ELFCLASS64);
234 #undef ECase
237 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration(
238 IO &IO, ELFYAML::ELF_ELFDATA &Value) {
239 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
240 // ELFDATANONE is an invalid data encoding, but we accept it because
241 // we want to be able to produce invalid binaries for the tests.
242 ECase(ELFDATANONE);
243 ECase(ELFDATA2LSB);
244 ECase(ELFDATA2MSB);
245 #undef ECase
248 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration(
249 IO &IO, ELFYAML::ELF_ELFOSABI &Value) {
250 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
251 ECase(ELFOSABI_NONE);
252 ECase(ELFOSABI_HPUX);
253 ECase(ELFOSABI_NETBSD);
254 ECase(ELFOSABI_GNU);
255 ECase(ELFOSABI_HURD);
256 ECase(ELFOSABI_SOLARIS);
257 ECase(ELFOSABI_AIX);
258 ECase(ELFOSABI_IRIX);
259 ECase(ELFOSABI_FREEBSD);
260 ECase(ELFOSABI_TRU64);
261 ECase(ELFOSABI_MODESTO);
262 ECase(ELFOSABI_OPENBSD);
263 ECase(ELFOSABI_OPENVMS);
264 ECase(ELFOSABI_NSK);
265 ECase(ELFOSABI_AROS);
266 ECase(ELFOSABI_FENIXOS);
267 ECase(ELFOSABI_CLOUDABI);
268 ECase(ELFOSABI_AMDGPU_HSA);
269 ECase(ELFOSABI_AMDGPU_PAL);
270 ECase(ELFOSABI_AMDGPU_MESA3D);
271 ECase(ELFOSABI_ARM);
272 ECase(ELFOSABI_C6000_ELFABI);
273 ECase(ELFOSABI_C6000_LINUX);
274 ECase(ELFOSABI_STANDALONE);
275 #undef ECase
278 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO,
279 ELFYAML::ELF_EF &Value) {
280 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
281 assert(Object && "The IO context is not initialized");
282 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
283 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
284 switch (Object->Header.Machine) {
285 case ELF::EM_ARM:
286 BCase(EF_ARM_SOFT_FLOAT);
287 BCase(EF_ARM_VFP_FLOAT);
288 BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK);
289 BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK);
290 BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK);
291 BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK);
292 BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK);
293 BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK);
294 break;
295 case ELF::EM_MIPS:
296 BCase(EF_MIPS_NOREORDER);
297 BCase(EF_MIPS_PIC);
298 BCase(EF_MIPS_CPIC);
299 BCase(EF_MIPS_ABI2);
300 BCase(EF_MIPS_32BITMODE);
301 BCase(EF_MIPS_FP64);
302 BCase(EF_MIPS_NAN2008);
303 BCase(EF_MIPS_MICROMIPS);
304 BCase(EF_MIPS_ARCH_ASE_M16);
305 BCase(EF_MIPS_ARCH_ASE_MDMX);
306 BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI);
307 BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI);
308 BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI);
309 BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI);
310 BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH);
311 BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH);
312 BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH);
313 BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH);
314 BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH);
315 BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH);
316 BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH);
317 BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH);
318 BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH);
319 BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH);
320 BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH);
321 BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH);
322 BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH);
323 BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH);
324 BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH);
325 BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH);
326 BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH);
327 BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH);
328 BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH);
329 BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH);
330 BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH);
331 BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH);
332 BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH);
333 BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH);
334 BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH);
335 BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH);
336 BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH);
337 BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH);
338 BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH);
339 break;
340 case ELF::EM_HEXAGON:
341 BCase(EF_HEXAGON_MACH_V2);
342 BCase(EF_HEXAGON_MACH_V3);
343 BCase(EF_HEXAGON_MACH_V4);
344 BCase(EF_HEXAGON_MACH_V5);
345 BCase(EF_HEXAGON_MACH_V55);
346 BCase(EF_HEXAGON_MACH_V60);
347 BCase(EF_HEXAGON_MACH_V62);
348 BCase(EF_HEXAGON_MACH_V65);
349 BCase(EF_HEXAGON_ISA_V2);
350 BCase(EF_HEXAGON_ISA_V3);
351 BCase(EF_HEXAGON_ISA_V4);
352 BCase(EF_HEXAGON_ISA_V5);
353 BCase(EF_HEXAGON_ISA_V55);
354 BCase(EF_HEXAGON_ISA_V60);
355 BCase(EF_HEXAGON_ISA_V62);
356 BCase(EF_HEXAGON_ISA_V65);
357 break;
358 case ELF::EM_AVR:
359 BCase(EF_AVR_ARCH_AVR1);
360 BCase(EF_AVR_ARCH_AVR2);
361 BCase(EF_AVR_ARCH_AVR25);
362 BCase(EF_AVR_ARCH_AVR3);
363 BCase(EF_AVR_ARCH_AVR31);
364 BCase(EF_AVR_ARCH_AVR35);
365 BCase(EF_AVR_ARCH_AVR4);
366 BCase(EF_AVR_ARCH_AVR51);
367 BCase(EF_AVR_ARCH_AVR6);
368 BCase(EF_AVR_ARCH_AVRTINY);
369 BCase(EF_AVR_ARCH_XMEGA1);
370 BCase(EF_AVR_ARCH_XMEGA2);
371 BCase(EF_AVR_ARCH_XMEGA3);
372 BCase(EF_AVR_ARCH_XMEGA4);
373 BCase(EF_AVR_ARCH_XMEGA5);
374 BCase(EF_AVR_ARCH_XMEGA6);
375 BCase(EF_AVR_ARCH_XMEGA7);
376 break;
377 case ELF::EM_RISCV:
378 BCase(EF_RISCV_RVC);
379 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI);
380 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI);
381 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI);
382 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI);
383 BCase(EF_RISCV_RVE);
384 break;
385 case ELF::EM_AMDGPU:
386 BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH);
387 BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH);
388 BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH);
389 BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH);
390 BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH);
391 BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH);
392 BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH);
393 BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH);
394 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH);
395 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH);
396 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH);
397 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH);
398 BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH);
399 BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH);
400 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH);
401 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH);
402 BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH);
403 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH);
404 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH);
405 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH);
406 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH);
407 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH);
408 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH);
409 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH);
410 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH);
411 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH);
412 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH);
413 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH);
414 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH);
415 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH);
416 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH);
417 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH);
418 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH);
419 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH);
420 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH);
421 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH);
422 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH);
423 BCase(EF_AMDGPU_XNACK);
424 BCase(EF_AMDGPU_SRAM_ECC);
425 break;
426 case ELF::EM_X86_64:
427 break;
428 default:
429 llvm_unreachable("Unsupported architecture");
431 #undef BCase
432 #undef BCaseMask
435 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration(
436 IO &IO, ELFYAML::ELF_SHT &Value) {
437 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
438 assert(Object && "The IO context is not initialized");
439 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
440 ECase(SHT_NULL);
441 ECase(SHT_PROGBITS);
442 ECase(SHT_SYMTAB);
443 // FIXME: Issue a diagnostic with this information.
444 ECase(SHT_STRTAB);
445 ECase(SHT_RELA);
446 ECase(SHT_HASH);
447 ECase(SHT_DYNAMIC);
448 ECase(SHT_NOTE);
449 ECase(SHT_NOBITS);
450 ECase(SHT_REL);
451 ECase(SHT_SHLIB);
452 ECase(SHT_DYNSYM);
453 ECase(SHT_INIT_ARRAY);
454 ECase(SHT_FINI_ARRAY);
455 ECase(SHT_PREINIT_ARRAY);
456 ECase(SHT_GROUP);
457 ECase(SHT_SYMTAB_SHNDX);
458 ECase(SHT_RELR);
459 ECase(SHT_ANDROID_REL);
460 ECase(SHT_ANDROID_RELA);
461 ECase(SHT_ANDROID_RELR);
462 ECase(SHT_LLVM_ODRTAB);
463 ECase(SHT_LLVM_LINKER_OPTIONS);
464 ECase(SHT_LLVM_CALL_GRAPH_PROFILE);
465 ECase(SHT_LLVM_ADDRSIG);
466 ECase(SHT_LLVM_DEPENDENT_LIBRARIES);
467 ECase(SHT_LLVM_SYMPART);
468 ECase(SHT_LLVM_PART_EHDR);
469 ECase(SHT_LLVM_PART_PHDR);
470 ECase(SHT_GNU_ATTRIBUTES);
471 ECase(SHT_GNU_HASH);
472 ECase(SHT_GNU_verdef);
473 ECase(SHT_GNU_verneed);
474 ECase(SHT_GNU_versym);
475 switch (Object->Header.Machine) {
476 case ELF::EM_ARM:
477 ECase(SHT_ARM_EXIDX);
478 ECase(SHT_ARM_PREEMPTMAP);
479 ECase(SHT_ARM_ATTRIBUTES);
480 ECase(SHT_ARM_DEBUGOVERLAY);
481 ECase(SHT_ARM_OVERLAYSECTION);
482 break;
483 case ELF::EM_HEXAGON:
484 ECase(SHT_HEX_ORDERED);
485 break;
486 case ELF::EM_X86_64:
487 ECase(SHT_X86_64_UNWIND);
488 break;
489 case ELF::EM_MIPS:
490 ECase(SHT_MIPS_REGINFO);
491 ECase(SHT_MIPS_OPTIONS);
492 ECase(SHT_MIPS_DWARF);
493 ECase(SHT_MIPS_ABIFLAGS);
494 break;
495 default:
496 // Nothing to do.
497 break;
499 #undef ECase
500 IO.enumFallback<Hex32>(Value);
503 void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO,
504 ELFYAML::ELF_PF &Value) {
505 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
506 BCase(PF_X);
507 BCase(PF_W);
508 BCase(PF_R);
511 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO,
512 ELFYAML::ELF_SHF &Value) {
513 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
514 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
515 BCase(SHF_WRITE);
516 BCase(SHF_ALLOC);
517 BCase(SHF_EXCLUDE);
518 BCase(SHF_EXECINSTR);
519 BCase(SHF_MERGE);
520 BCase(SHF_STRINGS);
521 BCase(SHF_INFO_LINK);
522 BCase(SHF_LINK_ORDER);
523 BCase(SHF_OS_NONCONFORMING);
524 BCase(SHF_GROUP);
525 BCase(SHF_TLS);
526 BCase(SHF_COMPRESSED);
527 switch (Object->Header.Machine) {
528 case ELF::EM_ARM:
529 BCase(SHF_ARM_PURECODE);
530 break;
531 case ELF::EM_HEXAGON:
532 BCase(SHF_HEX_GPREL);
533 break;
534 case ELF::EM_MIPS:
535 BCase(SHF_MIPS_NODUPES);
536 BCase(SHF_MIPS_NAMES);
537 BCase(SHF_MIPS_LOCAL);
538 BCase(SHF_MIPS_NOSTRIP);
539 BCase(SHF_MIPS_GPREL);
540 BCase(SHF_MIPS_MERGE);
541 BCase(SHF_MIPS_ADDR);
542 BCase(SHF_MIPS_STRING);
543 break;
544 case ELF::EM_X86_64:
545 BCase(SHF_X86_64_LARGE);
546 break;
547 default:
548 // Nothing to do.
549 break;
551 #undef BCase
554 void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration(
555 IO &IO, ELFYAML::ELF_SHN &Value) {
556 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
557 ECase(SHN_UNDEF);
558 ECase(SHN_LORESERVE);
559 ECase(SHN_LOPROC);
560 ECase(SHN_HIPROC);
561 ECase(SHN_LOOS);
562 ECase(SHN_HIOS);
563 ECase(SHN_ABS);
564 ECase(SHN_COMMON);
565 ECase(SHN_XINDEX);
566 ECase(SHN_HIRESERVE);
567 ECase(SHN_AMDGPU_LDS);
568 ECase(SHN_HEXAGON_SCOMMON);
569 ECase(SHN_HEXAGON_SCOMMON_1);
570 ECase(SHN_HEXAGON_SCOMMON_2);
571 ECase(SHN_HEXAGON_SCOMMON_4);
572 ECase(SHN_HEXAGON_SCOMMON_8);
573 #undef ECase
574 IO.enumFallback<Hex16>(Value);
577 void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration(
578 IO &IO, ELFYAML::ELF_STB &Value) {
579 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
580 ECase(STB_LOCAL);
581 ECase(STB_GLOBAL);
582 ECase(STB_WEAK);
583 ECase(STB_GNU_UNIQUE);
584 #undef ECase
585 IO.enumFallback<Hex8>(Value);
588 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration(
589 IO &IO, ELFYAML::ELF_STT &Value) {
590 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
591 ECase(STT_NOTYPE);
592 ECase(STT_OBJECT);
593 ECase(STT_FUNC);
594 ECase(STT_SECTION);
595 ECase(STT_FILE);
596 ECase(STT_COMMON);
597 ECase(STT_TLS);
598 ECase(STT_GNU_IFUNC);
599 #undef ECase
600 IO.enumFallback<Hex8>(Value);
604 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration(
605 IO &IO, ELFYAML::ELF_RSS &Value) {
606 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
607 ECase(RSS_UNDEF);
608 ECase(RSS_GP);
609 ECase(RSS_GP0);
610 ECase(RSS_LOC);
611 #undef ECase
614 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration(
615 IO &IO, ELFYAML::ELF_REL &Value) {
616 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
617 assert(Object && "The IO context is not initialized");
618 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
619 switch (Object->Header.Machine) {
620 case ELF::EM_X86_64:
621 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
622 break;
623 case ELF::EM_MIPS:
624 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
625 break;
626 case ELF::EM_HEXAGON:
627 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
628 break;
629 case ELF::EM_386:
630 case ELF::EM_IAMCU:
631 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
632 break;
633 case ELF::EM_AARCH64:
634 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
635 break;
636 case ELF::EM_ARM:
637 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
638 break;
639 case ELF::EM_ARC:
640 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
641 break;
642 case ELF::EM_RISCV:
643 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
644 break;
645 case ELF::EM_LANAI:
646 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
647 break;
648 case ELF::EM_AMDGPU:
649 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
650 break;
651 case ELF::EM_BPF:
652 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
653 break;
654 case ELF::EM_PPC64:
655 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
656 break;
657 default:
658 llvm_unreachable("Unsupported architecture");
660 #undef ELF_RELOC
661 IO.enumFallback<Hex32>(Value);
664 void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration(
665 IO &IO, ELFYAML::ELF_DYNTAG &Value) {
666 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
667 assert(Object && "The IO context is not initialized");
669 // Disable architecture specific tags by default. We might enable them below.
670 #define AARCH64_DYNAMIC_TAG(name, value)
671 #define MIPS_DYNAMIC_TAG(name, value)
672 #define HEXAGON_DYNAMIC_TAG(name, value)
673 #define PPC_DYNAMIC_TAG(name, value)
674 #define PPC64_DYNAMIC_TAG(name, value)
675 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
676 #define DYNAMIC_TAG_MARKER(name, value)
678 #define STRINGIFY(X) (#X)
679 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
680 switch (Object->Header.Machine) {
681 case ELF::EM_AARCH64:
682 #undef AARCH64_DYNAMIC_TAG
683 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
684 #include "llvm/BinaryFormat/DynamicTags.def"
685 #undef AARCH64_DYNAMIC_TAG
686 #define AARCH64_DYNAMIC_TAG(name, value)
687 break;
688 case ELF::EM_MIPS:
689 #undef MIPS_DYNAMIC_TAG
690 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
691 #include "llvm/BinaryFormat/DynamicTags.def"
692 #undef MIPS_DYNAMIC_TAG
693 #define MIPS_DYNAMIC_TAG(name, value)
694 break;
695 case ELF::EM_HEXAGON:
696 #undef HEXAGON_DYNAMIC_TAG
697 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
698 #include "llvm/BinaryFormat/DynamicTags.def"
699 #undef HEXAGON_DYNAMIC_TAG
700 #define HEXAGON_DYNAMIC_TAG(name, value)
701 break;
702 case ELF::EM_PPC:
703 #undef PPC_DYNAMIC_TAG
704 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
705 #include "llvm/BinaryFormat/DynamicTags.def"
706 #undef PPC_DYNAMIC_TAG
707 #define PPC_DYNAMIC_TAG(name, value)
708 break;
709 case ELF::EM_PPC64:
710 #undef PPC64_DYNAMIC_TAG
711 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
712 #include "llvm/BinaryFormat/DynamicTags.def"
713 #undef PPC64_DYNAMIC_TAG
714 #define PPC64_DYNAMIC_TAG(name, value)
715 break;
716 default:
717 #include "llvm/BinaryFormat/DynamicTags.def"
718 break;
720 #undef AARCH64_DYNAMIC_TAG
721 #undef MIPS_DYNAMIC_TAG
722 #undef HEXAGON_DYNAMIC_TAG
723 #undef PPC_DYNAMIC_TAG
724 #undef PPC64_DYNAMIC_TAG
725 #undef DYNAMIC_TAG_MARKER
726 #undef STRINGIFY
727 #undef DYNAMIC_TAG
729 IO.enumFallback<Hex64>(Value);
732 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration(
733 IO &IO, ELFYAML::MIPS_AFL_REG &Value) {
734 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
735 ECase(REG_NONE);
736 ECase(REG_32);
737 ECase(REG_64);
738 ECase(REG_128);
739 #undef ECase
742 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration(
743 IO &IO, ELFYAML::MIPS_ABI_FP &Value) {
744 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
745 ECase(FP_ANY);
746 ECase(FP_DOUBLE);
747 ECase(FP_SINGLE);
748 ECase(FP_SOFT);
749 ECase(FP_OLD_64);
750 ECase(FP_XX);
751 ECase(FP_64);
752 ECase(FP_64A);
753 #undef ECase
756 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration(
757 IO &IO, ELFYAML::MIPS_AFL_EXT &Value) {
758 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
759 ECase(EXT_NONE);
760 ECase(EXT_XLR);
761 ECase(EXT_OCTEON2);
762 ECase(EXT_OCTEONP);
763 ECase(EXT_LOONGSON_3A);
764 ECase(EXT_OCTEON);
765 ECase(EXT_5900);
766 ECase(EXT_4650);
767 ECase(EXT_4010);
768 ECase(EXT_4100);
769 ECase(EXT_3900);
770 ECase(EXT_10000);
771 ECase(EXT_SB1);
772 ECase(EXT_4111);
773 ECase(EXT_4120);
774 ECase(EXT_5400);
775 ECase(EXT_5500);
776 ECase(EXT_LOONGSON_2E);
777 ECase(EXT_LOONGSON_2F);
778 ECase(EXT_OCTEON3);
779 #undef ECase
782 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration(
783 IO &IO, ELFYAML::MIPS_ISA &Value) {
784 IO.enumCase(Value, "MIPS1", 1);
785 IO.enumCase(Value, "MIPS2", 2);
786 IO.enumCase(Value, "MIPS3", 3);
787 IO.enumCase(Value, "MIPS4", 4);
788 IO.enumCase(Value, "MIPS5", 5);
789 IO.enumCase(Value, "MIPS32", 32);
790 IO.enumCase(Value, "MIPS64", 64);
793 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset(
794 IO &IO, ELFYAML::MIPS_AFL_ASE &Value) {
795 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
796 BCase(DSP);
797 BCase(DSPR2);
798 BCase(EVA);
799 BCase(MCU);
800 BCase(MDMX);
801 BCase(MIPS3D);
802 BCase(MT);
803 BCase(SMARTMIPS);
804 BCase(VIRT);
805 BCase(MSA);
806 BCase(MIPS16);
807 BCase(MICROMIPS);
808 BCase(XPA);
809 #undef BCase
812 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset(
813 IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) {
814 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
815 BCase(ODDSPREG);
816 #undef BCase
819 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO,
820 ELFYAML::FileHeader &FileHdr) {
821 IO.mapRequired("Class", FileHdr.Class);
822 IO.mapRequired("Data", FileHdr.Data);
823 IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0));
824 IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0));
825 IO.mapRequired("Type", FileHdr.Type);
826 IO.mapRequired("Machine", FileHdr.Machine);
827 IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0));
828 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0));
830 IO.mapOptional("SHEntSize", FileHdr.SHEntSize);
831 IO.mapOptional("SHOff", FileHdr.SHOff);
832 IO.mapOptional("SHNum", FileHdr.SHNum);
833 IO.mapOptional("SHStrNdx", FileHdr.SHStrNdx);
836 void MappingTraits<ELFYAML::ProgramHeader>::mapping(
837 IO &IO, ELFYAML::ProgramHeader &Phdr) {
838 IO.mapRequired("Type", Phdr.Type);
839 IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0));
840 IO.mapOptional("Sections", Phdr.Sections);
841 IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0));
842 IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0));
843 IO.mapOptional("Align", Phdr.Align);
844 IO.mapOptional("FileSize", Phdr.FileSize);
845 IO.mapOptional("MemSize", Phdr.MemSize);
846 IO.mapOptional("Offset", Phdr.Offset);
849 LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece)
851 template <> struct ScalarTraits<StOtherPiece> {
852 static void output(const StOtherPiece &Val, void *, raw_ostream &Out) {
853 Out << Val;
855 static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) {
856 Val = Scalar;
857 return {};
859 static QuotingType mustQuote(StringRef) { return QuotingType::None; }
861 template <> struct SequenceElementTraits<StOtherPiece> {
862 static const bool flow = true;
865 namespace {
867 struct NormalizedOther {
868 NormalizedOther(IO &IO) : YamlIO(IO) {}
869 NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) {
870 assert(Original && "This constructor is only used for outputting YAML and "
871 "assumes a non-empty Original");
872 std::vector<StOtherPiece> Ret;
873 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
874 for (std::pair<StringRef, uint8_t> &P :
875 getFlags(Object->Header.Machine).takeVector()) {
876 uint8_t FlagValue = P.second;
877 if ((*Original & FlagValue) != FlagValue)
878 continue;
879 *Original &= ~FlagValue;
880 Ret.push_back({P.first});
883 if (*Original != 0) {
884 UnknownFlagsHolder = std::to_string(*Original);
885 Ret.push_back({UnknownFlagsHolder});
888 if (!Ret.empty())
889 Other = std::move(Ret);
892 uint8_t toValue(StringRef Name) {
893 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext());
894 MapVector<StringRef, uint8_t> Flags = getFlags(Object->Header.Machine);
896 auto It = Flags.find(Name);
897 if (It != Flags.end())
898 return It->second;
900 uint8_t Val;
901 if (to_integer(Name, Val))
902 return Val;
904 YamlIO.setError("an unknown value is used for symbol's 'Other' field: " +
905 Name);
906 return 0;
909 Optional<uint8_t> denormalize(IO &) {
910 if (!Other)
911 return None;
912 uint8_t Ret = 0;
913 for (StOtherPiece &Val : *Other)
914 Ret |= toValue(Val);
915 return Ret;
918 // st_other field is used to encode symbol visibility and platform-dependent
919 // flags and values. This method returns a name to value map that is used for
920 // parsing and encoding this field.
921 MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) {
922 MapVector<StringRef, uint8_t> Map;
923 // STV_* values are just enumeration values. We add them in a reversed order
924 // because when we convert the st_other to named constants when printing
925 // YAML we want to use a maximum number of bits on each step:
926 // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
927 // not as STV_HIDDEN (2) + STV_INTERNAL (1).
928 Map["STV_PROTECTED"] = ELF::STV_PROTECTED;
929 Map["STV_HIDDEN"] = ELF::STV_HIDDEN;
930 Map["STV_INTERNAL"] = ELF::STV_INTERNAL;
931 // STV_DEFAULT is used to represent the default visibility and has a value
932 // 0. We want to be able to read it from YAML documents, but there is no
933 // reason to print it.
934 if (!YamlIO.outputting())
935 Map["STV_DEFAULT"] = ELF::STV_DEFAULT;
937 // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
938 // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
939 // consumed first when we print the output, because we do not want to print
940 // any other flags that have the same bits instead.
941 if (EMachine == ELF::EM_MIPS) {
942 Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16;
943 Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS;
944 Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC;
945 Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT;
946 Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL;
948 return Map;
951 IO &YamlIO;
952 Optional<std::vector<StOtherPiece>> Other;
953 std::string UnknownFlagsHolder;
956 } // end anonymous namespace
958 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) {
959 IO.mapOptional("Name", Symbol.Name, StringRef());
960 IO.mapOptional("NameIndex", Symbol.NameIndex);
961 IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0));
962 IO.mapOptional("Section", Symbol.Section, StringRef());
963 IO.mapOptional("Index", Symbol.Index);
964 IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0));
965 IO.mapOptional("Value", Symbol.Value, Hex64(0));
966 IO.mapOptional("Size", Symbol.Size, Hex64(0));
968 // Symbol's Other field is a bit special. It is usually a field that
969 // represents st_other and holds the symbol visibility. However, on some
970 // platforms, it can contain bit fields and regular values, or even sometimes a
971 // crazy mix of them (see comments for NormalizedOther). Because of this, we
972 // need special handling.
973 MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO,
974 Symbol.Other);
975 IO.mapOptional("Other", Keys->Other);
978 StringRef MappingTraits<ELFYAML::Symbol>::validate(IO &IO,
979 ELFYAML::Symbol &Symbol) {
980 if (Symbol.Index && Symbol.Section.data())
981 return "Index and Section cannot both be specified for Symbol";
982 if (Symbol.NameIndex && !Symbol.Name.empty())
983 return "Name and NameIndex cannot both be specified for Symbol";
984 return StringRef();
987 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) {
988 IO.mapOptional("Name", Section.Name, StringRef());
989 IO.mapRequired("Type", Section.Type);
990 IO.mapOptional("Flags", Section.Flags);
991 IO.mapOptional("Address", Section.Address, Hex64(0));
992 IO.mapOptional("Link", Section.Link, StringRef());
993 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0));
994 IO.mapOptional("EntSize", Section.EntSize);
996 // obj2yaml does not dump these fields. They are expected to be empty when we
997 // are producing YAML, because yaml2obj sets appropriate values for them
998 // automatically when they are not explicitly defined.
999 assert(!IO.outputting() ||
1000 (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue()));
1001 IO.mapOptional("ShName", Section.ShName);
1002 IO.mapOptional("ShOffset", Section.ShOffset);
1003 IO.mapOptional("ShSize", Section.ShSize);
1006 static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) {
1007 commonSectionMapping(IO, Section);
1008 IO.mapOptional("Entries", Section.Entries);
1009 IO.mapOptional("Content", Section.Content);
1012 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) {
1013 commonSectionMapping(IO, Section);
1014 IO.mapOptional("Content", Section.Content);
1015 IO.mapOptional("Size", Section.Size);
1016 IO.mapOptional("Info", Section.Info);
1019 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) {
1020 commonSectionMapping(IO, Section);
1021 IO.mapOptional("Size", Section.Size, Hex64(0));
1024 static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) {
1025 commonSectionMapping(IO, Section);
1026 IO.mapRequired("Info", Section.Info);
1027 IO.mapRequired("Entries", Section.Entries);
1030 static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) {
1031 commonSectionMapping(IO, Section);
1032 IO.mapRequired("Entries", Section.Entries);
1035 static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) {
1036 commonSectionMapping(IO, Section);
1037 IO.mapRequired("Info", Section.Info);
1038 IO.mapRequired("Dependencies", Section.VerneedV);
1041 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) {
1042 commonSectionMapping(IO, Section);
1043 IO.mapOptional("Info", Section.RelocatableSec, StringRef());
1044 IO.mapOptional("Relocations", Section.Relocations);
1047 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) {
1048 commonSectionMapping(IO, Group);
1049 IO.mapOptional("Info", Group.Signature, StringRef());
1050 IO.mapRequired("Members", Group.Members);
1053 static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) {
1054 commonSectionMapping(IO, Section);
1055 IO.mapRequired("Entries", Section.Entries);
1058 void MappingTraits<ELFYAML::SectionOrType>::mapping(
1059 IO &IO, ELFYAML::SectionOrType &sectionOrType) {
1060 IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType);
1063 void MappingTraits<ELFYAML::SectionName>::mapping(
1064 IO &IO, ELFYAML::SectionName &sectionName) {
1065 IO.mapRequired("Section", sectionName.Section);
1068 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) {
1069 commonSectionMapping(IO, Section);
1070 IO.mapOptional("Version", Section.Version, Hex16(0));
1071 IO.mapRequired("ISA", Section.ISALevel);
1072 IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0));
1073 IO.mapOptional("ISAExtension", Section.ISAExtension,
1074 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE));
1075 IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0));
1076 IO.mapOptional("FpABI", Section.FpABI,
1077 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY));
1078 IO.mapOptional("GPRSize", Section.GPRSize,
1079 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1080 IO.mapOptional("CPR1Size", Section.CPR1Size,
1081 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1082 IO.mapOptional("CPR2Size", Section.CPR2Size,
1083 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE));
1084 IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0));
1085 IO.mapOptional("Flags2", Section.Flags2, Hex32(0));
1088 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping(
1089 IO &IO, std::unique_ptr<ELFYAML::Section> &Section) {
1090 ELFYAML::ELF_SHT sectionType;
1091 if (IO.outputting())
1092 sectionType = Section->Type;
1093 else
1094 IO.mapRequired("Type", sectionType);
1096 switch (sectionType) {
1097 case ELF::SHT_DYNAMIC:
1098 if (!IO.outputting())
1099 Section.reset(new ELFYAML::DynamicSection());
1100 sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get()));
1101 break;
1102 case ELF::SHT_REL:
1103 case ELF::SHT_RELA:
1104 if (!IO.outputting())
1105 Section.reset(new ELFYAML::RelocationSection());
1106 sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get()));
1107 break;
1108 case ELF::SHT_GROUP:
1109 if (!IO.outputting())
1110 Section.reset(new ELFYAML::Group());
1111 groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get()));
1112 break;
1113 case ELF::SHT_NOBITS:
1114 if (!IO.outputting())
1115 Section.reset(new ELFYAML::NoBitsSection());
1116 sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get()));
1117 break;
1118 case ELF::SHT_MIPS_ABIFLAGS:
1119 if (!IO.outputting())
1120 Section.reset(new ELFYAML::MipsABIFlags());
1121 sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get()));
1122 break;
1123 case ELF::SHT_GNU_verdef:
1124 if (!IO.outputting())
1125 Section.reset(new ELFYAML::VerdefSection());
1126 sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get()));
1127 break;
1128 case ELF::SHT_GNU_versym:
1129 if (!IO.outputting())
1130 Section.reset(new ELFYAML::SymverSection());
1131 sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get()));
1132 break;
1133 case ELF::SHT_GNU_verneed:
1134 if (!IO.outputting())
1135 Section.reset(new ELFYAML::VerneedSection());
1136 sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get()));
1137 break;
1138 case ELF::SHT_SYMTAB_SHNDX:
1139 if (!IO.outputting())
1140 Section.reset(new ELFYAML::SymtabShndxSection());
1141 sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get()));
1142 break;
1143 default:
1144 if (!IO.outputting())
1145 Section.reset(new ELFYAML::RawContentSection());
1146 sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get()));
1150 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate(
1151 IO &io, std::unique_ptr<ELFYAML::Section> &Section) {
1152 const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get());
1153 if (!RawSection)
1154 return {};
1155 if (RawSection->Size && RawSection->Content &&
1156 (uint64_t)(*RawSection->Size) < RawSection->Content->binary_size())
1157 return "Section size must be greater than or equal to the content size";
1158 return {};
1161 namespace {
1163 struct NormalizedMips64RelType {
1164 NormalizedMips64RelType(IO &)
1165 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1166 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1167 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)),
1168 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {}
1169 NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original)
1170 : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF),
1171 Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {}
1173 ELFYAML::ELF_REL denormalize(IO &) {
1174 ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24;
1175 return Res;
1178 ELFYAML::ELF_REL Type;
1179 ELFYAML::ELF_REL Type2;
1180 ELFYAML::ELF_REL Type3;
1181 ELFYAML::ELF_RSS SpecSym;
1184 } // end anonymous namespace
1186 void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO,
1187 ELFYAML::DynamicEntry &Rel) {
1188 assert(IO.getContext() && "The IO context is not initialized");
1190 IO.mapRequired("Tag", Rel.Tag);
1191 IO.mapRequired("Value", Rel.Val);
1194 void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO,
1195 ELFYAML::VerdefEntry &E) {
1196 assert(IO.getContext() && "The IO context is not initialized");
1198 IO.mapRequired("Version", E.Version);
1199 IO.mapRequired("Flags", E.Flags);
1200 IO.mapRequired("VersionNdx", E.VersionNdx);
1201 IO.mapRequired("Hash", E.Hash);
1202 IO.mapRequired("Names", E.VerNames);
1205 void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO,
1206 ELFYAML::VerneedEntry &E) {
1207 assert(IO.getContext() && "The IO context is not initialized");
1209 IO.mapRequired("Version", E.Version);
1210 IO.mapRequired("File", E.File);
1211 IO.mapRequired("Entries", E.AuxV);
1214 void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO,
1215 ELFYAML::VernauxEntry &E) {
1216 assert(IO.getContext() && "The IO context is not initialized");
1218 IO.mapRequired("Name", E.Name);
1219 IO.mapRequired("Hash", E.Hash);
1220 IO.mapRequired("Flags", E.Flags);
1221 IO.mapRequired("Other", E.Other);
1224 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO,
1225 ELFYAML::Relocation &Rel) {
1226 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext());
1227 assert(Object && "The IO context is not initialized");
1229 IO.mapRequired("Offset", Rel.Offset);
1230 IO.mapOptional("Symbol", Rel.Symbol);
1232 if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) &&
1233 Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) {
1234 MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key(
1235 IO, Rel.Type);
1236 IO.mapRequired("Type", Key->Type);
1237 IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1238 IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE));
1239 IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF));
1240 } else
1241 IO.mapRequired("Type", Rel.Type);
1243 IO.mapOptional("Addend", Rel.Addend, (int64_t)0);
1246 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) {
1247 assert(!IO.getContext() && "The IO context is initialized already");
1248 IO.setContext(&Object);
1249 IO.mapTag("!ELF", true);
1250 IO.mapRequired("FileHeader", Object.Header);
1251 IO.mapOptional("ProgramHeaders", Object.ProgramHeaders);
1252 IO.mapOptional("Sections", Object.Sections);
1253 IO.mapOptional("Symbols", Object.Symbols);
1254 IO.mapOptional("DynamicSymbols", Object.DynamicSymbols);
1255 IO.setContext(nullptr);
1258 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG)
1259 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP)
1260 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT)
1261 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE)
1262 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1)
1264 } // end namespace yaml
1266 } // end namespace llvm