1 //===- ELFYAML.cpp - ELF YAMLIO implementation ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
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"
27 ELFYAML::Chunk::~Chunk() = default;
31 void ScalarEnumerationTraits
<ELFYAML::ELF_ET
>::enumeration(
32 IO
&IO
, ELFYAML::ELF_ET
&Value
) {
33 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
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)
54 ECase(PT_GNU_EH_FRAME
);
57 ECase(PT_GNU_PROPERTY
);
59 IO
.enumFallback
<Hex32
>(Value
);
62 void ScalarEnumerationTraits
<ELFYAML::ELF_EM
>::enumeration(
63 IO
&IO
, ELFYAML::ELF_EM
&Value
) {
64 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
75 ECase(EM_MIPS_RS3_LE
);
78 ECase(EM_SPARC32PLUS
);
141 ECase(EM_ARC_COMPACT
);
161 ECase(EM_ALTERA_NIOS2
);
175 ECase(EM_VIDEOCORE3
);
176 ECase(EM_LATTICEMICO32
);
181 ECase(EM_MMDSP_PLUS
);
182 ECase(EM_CYPRESS_M8C
);
197 ECase(EM_MCST_ELBRUS
);
211 ECase(EM_CLOUDSHIELD
);
214 ECase(EM_ARC_COMPACT2
);
217 ECase(EM_VIDEOCORE5
);
225 IO
.enumFallback
<Hex16
>(Value
);
228 void ScalarEnumerationTraits
<ELFYAML::ELF_ELFCLASS
>::enumeration(
229 IO
&IO
, ELFYAML::ELF_ELFCLASS
&Value
) {
230 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
231 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it
238 void ScalarEnumerationTraits
<ELFYAML::ELF_ELFDATA
>::enumeration(
239 IO
&IO
, ELFYAML::ELF_ELFDATA
&Value
) {
240 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
241 // ELFDATANONE is an invalid data encoding, but we accept it because
242 // we want to be able to produce invalid binaries for the tests.
249 void ScalarEnumerationTraits
<ELFYAML::ELF_ELFOSABI
>::enumeration(
250 IO
&IO
, ELFYAML::ELF_ELFOSABI
&Value
) {
251 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
252 ECase(ELFOSABI_NONE
);
253 ECase(ELFOSABI_HPUX
);
254 ECase(ELFOSABI_NETBSD
);
256 ECase(ELFOSABI_LINUX
);
257 ECase(ELFOSABI_HURD
);
258 ECase(ELFOSABI_SOLARIS
);
260 ECase(ELFOSABI_IRIX
);
261 ECase(ELFOSABI_FREEBSD
);
262 ECase(ELFOSABI_TRU64
);
263 ECase(ELFOSABI_MODESTO
);
264 ECase(ELFOSABI_OPENBSD
);
265 ECase(ELFOSABI_OPENVMS
);
267 ECase(ELFOSABI_AROS
);
268 ECase(ELFOSABI_FENIXOS
);
269 ECase(ELFOSABI_CLOUDABI
);
270 ECase(ELFOSABI_AMDGPU_HSA
);
271 ECase(ELFOSABI_AMDGPU_PAL
);
272 ECase(ELFOSABI_AMDGPU_MESA3D
);
274 ECase(ELFOSABI_C6000_ELFABI
);
275 ECase(ELFOSABI_C6000_LINUX
);
276 ECase(ELFOSABI_STANDALONE
);
278 IO
.enumFallback
<Hex8
>(Value
);
281 void ScalarBitSetTraits
<ELFYAML::ELF_EF
>::bitset(IO
&IO
,
282 ELFYAML::ELF_EF
&Value
) {
283 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
284 assert(Object
&& "The IO context is not initialized");
285 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
286 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M)
287 switch (Object
->Header
.Machine
) {
289 BCase(EF_ARM_SOFT_FLOAT
);
290 BCase(EF_ARM_VFP_FLOAT
);
291 BCaseMask(EF_ARM_EABI_UNKNOWN
, EF_ARM_EABIMASK
);
292 BCaseMask(EF_ARM_EABI_VER1
, EF_ARM_EABIMASK
);
293 BCaseMask(EF_ARM_EABI_VER2
, EF_ARM_EABIMASK
);
294 BCaseMask(EF_ARM_EABI_VER3
, EF_ARM_EABIMASK
);
295 BCaseMask(EF_ARM_EABI_VER4
, EF_ARM_EABIMASK
);
296 BCaseMask(EF_ARM_EABI_VER5
, EF_ARM_EABIMASK
);
299 BCase(EF_MIPS_NOREORDER
);
303 BCase(EF_MIPS_32BITMODE
);
305 BCase(EF_MIPS_NAN2008
);
306 BCase(EF_MIPS_MICROMIPS
);
307 BCase(EF_MIPS_ARCH_ASE_M16
);
308 BCase(EF_MIPS_ARCH_ASE_MDMX
);
309 BCaseMask(EF_MIPS_ABI_O32
, EF_MIPS_ABI
);
310 BCaseMask(EF_MIPS_ABI_O64
, EF_MIPS_ABI
);
311 BCaseMask(EF_MIPS_ABI_EABI32
, EF_MIPS_ABI
);
312 BCaseMask(EF_MIPS_ABI_EABI64
, EF_MIPS_ABI
);
313 BCaseMask(EF_MIPS_MACH_3900
, EF_MIPS_MACH
);
314 BCaseMask(EF_MIPS_MACH_4010
, EF_MIPS_MACH
);
315 BCaseMask(EF_MIPS_MACH_4100
, EF_MIPS_MACH
);
316 BCaseMask(EF_MIPS_MACH_4650
, EF_MIPS_MACH
);
317 BCaseMask(EF_MIPS_MACH_4120
, EF_MIPS_MACH
);
318 BCaseMask(EF_MIPS_MACH_4111
, EF_MIPS_MACH
);
319 BCaseMask(EF_MIPS_MACH_SB1
, EF_MIPS_MACH
);
320 BCaseMask(EF_MIPS_MACH_OCTEON
, EF_MIPS_MACH
);
321 BCaseMask(EF_MIPS_MACH_XLR
, EF_MIPS_MACH
);
322 BCaseMask(EF_MIPS_MACH_OCTEON2
, EF_MIPS_MACH
);
323 BCaseMask(EF_MIPS_MACH_OCTEON3
, EF_MIPS_MACH
);
324 BCaseMask(EF_MIPS_MACH_5400
, EF_MIPS_MACH
);
325 BCaseMask(EF_MIPS_MACH_5900
, EF_MIPS_MACH
);
326 BCaseMask(EF_MIPS_MACH_5500
, EF_MIPS_MACH
);
327 BCaseMask(EF_MIPS_MACH_9000
, EF_MIPS_MACH
);
328 BCaseMask(EF_MIPS_MACH_LS2E
, EF_MIPS_MACH
);
329 BCaseMask(EF_MIPS_MACH_LS2F
, EF_MIPS_MACH
);
330 BCaseMask(EF_MIPS_MACH_LS3A
, EF_MIPS_MACH
);
331 BCaseMask(EF_MIPS_ARCH_1
, EF_MIPS_ARCH
);
332 BCaseMask(EF_MIPS_ARCH_2
, EF_MIPS_ARCH
);
333 BCaseMask(EF_MIPS_ARCH_3
, EF_MIPS_ARCH
);
334 BCaseMask(EF_MIPS_ARCH_4
, EF_MIPS_ARCH
);
335 BCaseMask(EF_MIPS_ARCH_5
, EF_MIPS_ARCH
);
336 BCaseMask(EF_MIPS_ARCH_32
, EF_MIPS_ARCH
);
337 BCaseMask(EF_MIPS_ARCH_64
, EF_MIPS_ARCH
);
338 BCaseMask(EF_MIPS_ARCH_32R2
, EF_MIPS_ARCH
);
339 BCaseMask(EF_MIPS_ARCH_64R2
, EF_MIPS_ARCH
);
340 BCaseMask(EF_MIPS_ARCH_32R6
, EF_MIPS_ARCH
);
341 BCaseMask(EF_MIPS_ARCH_64R6
, EF_MIPS_ARCH
);
343 case ELF::EM_HEXAGON
:
344 BCase(EF_HEXAGON_MACH_V2
);
345 BCase(EF_HEXAGON_MACH_V3
);
346 BCase(EF_HEXAGON_MACH_V4
);
347 BCase(EF_HEXAGON_MACH_V5
);
348 BCase(EF_HEXAGON_MACH_V55
);
349 BCase(EF_HEXAGON_MACH_V60
);
350 BCase(EF_HEXAGON_MACH_V62
);
351 BCase(EF_HEXAGON_MACH_V65
);
352 BCase(EF_HEXAGON_MACH_V66
);
353 BCase(EF_HEXAGON_MACH_V67
);
354 BCase(EF_HEXAGON_MACH_V67T
);
355 BCase(EF_HEXAGON_ISA_V2
);
356 BCase(EF_HEXAGON_ISA_V3
);
357 BCase(EF_HEXAGON_ISA_V4
);
358 BCase(EF_HEXAGON_ISA_V5
);
359 BCase(EF_HEXAGON_ISA_V55
);
360 BCase(EF_HEXAGON_ISA_V60
);
361 BCase(EF_HEXAGON_ISA_V62
);
362 BCase(EF_HEXAGON_ISA_V65
);
363 BCase(EF_HEXAGON_ISA_V66
);
364 BCase(EF_HEXAGON_ISA_V67
);
367 BCase(EF_AVR_ARCH_AVR1
);
368 BCase(EF_AVR_ARCH_AVR2
);
369 BCase(EF_AVR_ARCH_AVR25
);
370 BCase(EF_AVR_ARCH_AVR3
);
371 BCase(EF_AVR_ARCH_AVR31
);
372 BCase(EF_AVR_ARCH_AVR35
);
373 BCase(EF_AVR_ARCH_AVR4
);
374 BCase(EF_AVR_ARCH_AVR51
);
375 BCase(EF_AVR_ARCH_AVR6
);
376 BCase(EF_AVR_ARCH_AVRTINY
);
377 BCase(EF_AVR_ARCH_XMEGA1
);
378 BCase(EF_AVR_ARCH_XMEGA2
);
379 BCase(EF_AVR_ARCH_XMEGA3
);
380 BCase(EF_AVR_ARCH_XMEGA4
);
381 BCase(EF_AVR_ARCH_XMEGA5
);
382 BCase(EF_AVR_ARCH_XMEGA6
);
383 BCase(EF_AVR_ARCH_XMEGA7
);
387 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT
, EF_RISCV_FLOAT_ABI
);
388 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE
, EF_RISCV_FLOAT_ABI
);
389 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE
, EF_RISCV_FLOAT_ABI
);
390 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD
, EF_RISCV_FLOAT_ABI
);
394 BCaseMask(EF_AMDGPU_MACH_NONE
, EF_AMDGPU_MACH
);
395 BCaseMask(EF_AMDGPU_MACH_R600_R600
, EF_AMDGPU_MACH
);
396 BCaseMask(EF_AMDGPU_MACH_R600_R630
, EF_AMDGPU_MACH
);
397 BCaseMask(EF_AMDGPU_MACH_R600_RS880
, EF_AMDGPU_MACH
);
398 BCaseMask(EF_AMDGPU_MACH_R600_RV670
, EF_AMDGPU_MACH
);
399 BCaseMask(EF_AMDGPU_MACH_R600_RV710
, EF_AMDGPU_MACH
);
400 BCaseMask(EF_AMDGPU_MACH_R600_RV730
, EF_AMDGPU_MACH
);
401 BCaseMask(EF_AMDGPU_MACH_R600_RV770
, EF_AMDGPU_MACH
);
402 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR
, EF_AMDGPU_MACH
);
403 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS
, EF_AMDGPU_MACH
);
404 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER
, EF_AMDGPU_MACH
);
405 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD
, EF_AMDGPU_MACH
);
406 BCaseMask(EF_AMDGPU_MACH_R600_SUMO
, EF_AMDGPU_MACH
);
407 BCaseMask(EF_AMDGPU_MACH_R600_BARTS
, EF_AMDGPU_MACH
);
408 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS
, EF_AMDGPU_MACH
);
409 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN
, EF_AMDGPU_MACH
);
410 BCaseMask(EF_AMDGPU_MACH_R600_TURKS
, EF_AMDGPU_MACH
);
411 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600
, EF_AMDGPU_MACH
);
412 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601
, EF_AMDGPU_MACH
);
413 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700
, EF_AMDGPU_MACH
);
414 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701
, EF_AMDGPU_MACH
);
415 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702
, EF_AMDGPU_MACH
);
416 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703
, EF_AMDGPU_MACH
);
417 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704
, EF_AMDGPU_MACH
);
418 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801
, EF_AMDGPU_MACH
);
419 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802
, EF_AMDGPU_MACH
);
420 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803
, EF_AMDGPU_MACH
);
421 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810
, EF_AMDGPU_MACH
);
422 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900
, EF_AMDGPU_MACH
);
423 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902
, EF_AMDGPU_MACH
);
424 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904
, EF_AMDGPU_MACH
);
425 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906
, EF_AMDGPU_MACH
);
426 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908
, EF_AMDGPU_MACH
);
427 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909
, EF_AMDGPU_MACH
);
428 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010
, EF_AMDGPU_MACH
);
429 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011
, EF_AMDGPU_MACH
);
430 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012
, EF_AMDGPU_MACH
);
431 BCase(EF_AMDGPU_XNACK
);
432 BCase(EF_AMDGPU_SRAM_ECC
);
437 llvm_unreachable("Unsupported architecture");
443 void ScalarEnumerationTraits
<ELFYAML::ELF_SHT
>::enumeration(
444 IO
&IO
, ELFYAML::ELF_SHT
&Value
) {
445 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
446 assert(Object
&& "The IO context is not initialized");
447 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
451 // FIXME: Issue a diagnostic with this information.
461 ECase(SHT_INIT_ARRAY
);
462 ECase(SHT_FINI_ARRAY
);
463 ECase(SHT_PREINIT_ARRAY
);
465 ECase(SHT_SYMTAB_SHNDX
);
467 ECase(SHT_ANDROID_REL
);
468 ECase(SHT_ANDROID_RELA
);
469 ECase(SHT_ANDROID_RELR
);
470 ECase(SHT_LLVM_ODRTAB
);
471 ECase(SHT_LLVM_LINKER_OPTIONS
);
472 ECase(SHT_LLVM_CALL_GRAPH_PROFILE
);
473 ECase(SHT_LLVM_ADDRSIG
);
474 ECase(SHT_LLVM_DEPENDENT_LIBRARIES
);
475 ECase(SHT_LLVM_SYMPART
);
476 ECase(SHT_LLVM_PART_EHDR
);
477 ECase(SHT_LLVM_PART_PHDR
);
478 ECase(SHT_GNU_ATTRIBUTES
);
480 ECase(SHT_GNU_verdef
);
481 ECase(SHT_GNU_verneed
);
482 ECase(SHT_GNU_versym
);
483 switch (Object
->Header
.Machine
) {
485 ECase(SHT_ARM_EXIDX
);
486 ECase(SHT_ARM_PREEMPTMAP
);
487 ECase(SHT_ARM_ATTRIBUTES
);
488 ECase(SHT_ARM_DEBUGOVERLAY
);
489 ECase(SHT_ARM_OVERLAYSECTION
);
491 case ELF::EM_HEXAGON
:
492 ECase(SHT_HEX_ORDERED
);
495 ECase(SHT_X86_64_UNWIND
);
498 ECase(SHT_MIPS_REGINFO
);
499 ECase(SHT_MIPS_OPTIONS
);
500 ECase(SHT_MIPS_DWARF
);
501 ECase(SHT_MIPS_ABIFLAGS
);
508 IO
.enumFallback
<Hex32
>(Value
);
511 void ScalarBitSetTraits
<ELFYAML::ELF_PF
>::bitset(IO
&IO
,
512 ELFYAML::ELF_PF
&Value
) {
513 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
519 void ScalarBitSetTraits
<ELFYAML::ELF_SHF
>::bitset(IO
&IO
,
520 ELFYAML::ELF_SHF
&Value
) {
521 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
522 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X)
526 BCase(SHF_EXECINSTR
);
529 BCase(SHF_INFO_LINK
);
530 BCase(SHF_LINK_ORDER
);
531 BCase(SHF_OS_NONCONFORMING
);
534 BCase(SHF_COMPRESSED
);
535 switch (Object
->Header
.Machine
) {
537 BCase(SHF_ARM_PURECODE
);
539 case ELF::EM_HEXAGON
:
540 BCase(SHF_HEX_GPREL
);
543 BCase(SHF_MIPS_NODUPES
);
544 BCase(SHF_MIPS_NAMES
);
545 BCase(SHF_MIPS_LOCAL
);
546 BCase(SHF_MIPS_NOSTRIP
);
547 BCase(SHF_MIPS_GPREL
);
548 BCase(SHF_MIPS_MERGE
);
549 BCase(SHF_MIPS_ADDR
);
550 BCase(SHF_MIPS_STRING
);
553 BCase(SHF_X86_64_LARGE
);
562 void ScalarEnumerationTraits
<ELFYAML::ELF_SHN
>::enumeration(
563 IO
&IO
, ELFYAML::ELF_SHN
&Value
) {
564 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
566 ECase(SHN_LORESERVE
);
574 ECase(SHN_HIRESERVE
);
575 ECase(SHN_AMDGPU_LDS
);
576 ECase(SHN_HEXAGON_SCOMMON
);
577 ECase(SHN_HEXAGON_SCOMMON_1
);
578 ECase(SHN_HEXAGON_SCOMMON_2
);
579 ECase(SHN_HEXAGON_SCOMMON_4
);
580 ECase(SHN_HEXAGON_SCOMMON_8
);
582 IO
.enumFallback
<Hex16
>(Value
);
585 void ScalarEnumerationTraits
<ELFYAML::ELF_STB
>::enumeration(
586 IO
&IO
, ELFYAML::ELF_STB
&Value
) {
587 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
591 ECase(STB_GNU_UNIQUE
);
593 IO
.enumFallback
<Hex8
>(Value
);
596 void ScalarEnumerationTraits
<ELFYAML::ELF_STT
>::enumeration(
597 IO
&IO
, ELFYAML::ELF_STT
&Value
) {
598 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
606 ECase(STT_GNU_IFUNC
);
608 IO
.enumFallback
<Hex8
>(Value
);
612 void ScalarEnumerationTraits
<ELFYAML::ELF_RSS
>::enumeration(
613 IO
&IO
, ELFYAML::ELF_RSS
&Value
) {
614 #define ECase(X) IO.enumCase(Value, #X, ELF::X)
622 void ScalarEnumerationTraits
<ELFYAML::ELF_REL
>::enumeration(
623 IO
&IO
, ELFYAML::ELF_REL
&Value
) {
624 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
625 assert(Object
&& "The IO context is not initialized");
626 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X);
627 switch (Object
->Header
.Machine
) {
629 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
632 #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
634 case ELF::EM_HEXAGON
:
635 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
639 #include "llvm/BinaryFormat/ELFRelocs/i386.def"
641 case ELF::EM_AARCH64
:
642 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
645 #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
648 #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
651 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
654 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
657 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
660 #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
663 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
670 IO
.enumFallback
<Hex32
>(Value
);
673 void ScalarEnumerationTraits
<ELFYAML::ELF_DYNTAG
>::enumeration(
674 IO
&IO
, ELFYAML::ELF_DYNTAG
&Value
) {
675 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
676 assert(Object
&& "The IO context is not initialized");
678 // Disable architecture specific tags by default. We might enable them below.
679 #define AARCH64_DYNAMIC_TAG(name, value)
680 #define MIPS_DYNAMIC_TAG(name, value)
681 #define HEXAGON_DYNAMIC_TAG(name, value)
682 #define PPC_DYNAMIC_TAG(name, value)
683 #define PPC64_DYNAMIC_TAG(name, value)
684 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
685 #define DYNAMIC_TAG_MARKER(name, value)
687 #define STRINGIFY(X) (#X)
688 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X);
689 switch (Object
->Header
.Machine
) {
690 case ELF::EM_AARCH64
:
691 #undef AARCH64_DYNAMIC_TAG
692 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
693 #include "llvm/BinaryFormat/DynamicTags.def"
694 #undef AARCH64_DYNAMIC_TAG
695 #define AARCH64_DYNAMIC_TAG(name, value)
698 #undef MIPS_DYNAMIC_TAG
699 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
700 #include "llvm/BinaryFormat/DynamicTags.def"
701 #undef MIPS_DYNAMIC_TAG
702 #define MIPS_DYNAMIC_TAG(name, value)
704 case ELF::EM_HEXAGON
:
705 #undef HEXAGON_DYNAMIC_TAG
706 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
707 #include "llvm/BinaryFormat/DynamicTags.def"
708 #undef HEXAGON_DYNAMIC_TAG
709 #define HEXAGON_DYNAMIC_TAG(name, value)
712 #undef PPC_DYNAMIC_TAG
713 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
714 #include "llvm/BinaryFormat/DynamicTags.def"
715 #undef PPC_DYNAMIC_TAG
716 #define PPC_DYNAMIC_TAG(name, value)
719 #undef PPC64_DYNAMIC_TAG
720 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value)
721 #include "llvm/BinaryFormat/DynamicTags.def"
722 #undef PPC64_DYNAMIC_TAG
723 #define PPC64_DYNAMIC_TAG(name, value)
726 #include "llvm/BinaryFormat/DynamicTags.def"
729 #undef AARCH64_DYNAMIC_TAG
730 #undef MIPS_DYNAMIC_TAG
731 #undef HEXAGON_DYNAMIC_TAG
732 #undef PPC_DYNAMIC_TAG
733 #undef PPC64_DYNAMIC_TAG
734 #undef DYNAMIC_TAG_MARKER
738 IO
.enumFallback
<Hex64
>(Value
);
741 void ScalarEnumerationTraits
<ELFYAML::MIPS_AFL_REG
>::enumeration(
742 IO
&IO
, ELFYAML::MIPS_AFL_REG
&Value
) {
743 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
751 void ScalarEnumerationTraits
<ELFYAML::MIPS_ABI_FP
>::enumeration(
752 IO
&IO
, ELFYAML::MIPS_ABI_FP
&Value
) {
753 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X)
765 void ScalarEnumerationTraits
<ELFYAML::MIPS_AFL_EXT
>::enumeration(
766 IO
&IO
, ELFYAML::MIPS_AFL_EXT
&Value
) {
767 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X)
772 ECase(EXT_LOONGSON_3A
);
785 ECase(EXT_LOONGSON_2E
);
786 ECase(EXT_LOONGSON_2F
);
791 void ScalarEnumerationTraits
<ELFYAML::MIPS_ISA
>::enumeration(
792 IO
&IO
, ELFYAML::MIPS_ISA
&Value
) {
793 IO
.enumCase(Value
, "MIPS1", 1);
794 IO
.enumCase(Value
, "MIPS2", 2);
795 IO
.enumCase(Value
, "MIPS3", 3);
796 IO
.enumCase(Value
, "MIPS4", 4);
797 IO
.enumCase(Value
, "MIPS5", 5);
798 IO
.enumCase(Value
, "MIPS32", 32);
799 IO
.enumCase(Value
, "MIPS64", 64);
802 void ScalarBitSetTraits
<ELFYAML::MIPS_AFL_ASE
>::bitset(
803 IO
&IO
, ELFYAML::MIPS_AFL_ASE
&Value
) {
804 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X)
821 void ScalarBitSetTraits
<ELFYAML::MIPS_AFL_FLAGS1
>::bitset(
822 IO
&IO
, ELFYAML::MIPS_AFL_FLAGS1
&Value
) {
823 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X)
828 void MappingTraits
<ELFYAML::FileHeader
>::mapping(IO
&IO
,
829 ELFYAML::FileHeader
&FileHdr
) {
830 IO
.mapRequired("Class", FileHdr
.Class
);
831 IO
.mapRequired("Data", FileHdr
.Data
);
832 IO
.mapOptional("OSABI", FileHdr
.OSABI
, ELFYAML::ELF_ELFOSABI(0));
833 IO
.mapOptional("ABIVersion", FileHdr
.ABIVersion
, Hex8(0));
834 IO
.mapRequired("Type", FileHdr
.Type
);
835 IO
.mapRequired("Machine", FileHdr
.Machine
);
836 IO
.mapOptional("Flags", FileHdr
.Flags
, ELFYAML::ELF_EF(0));
837 IO
.mapOptional("Entry", FileHdr
.Entry
, Hex64(0));
839 IO
.mapOptional("SHEntSize", FileHdr
.SHEntSize
);
840 IO
.mapOptional("SHOff", FileHdr
.SHOff
);
841 IO
.mapOptional("SHNum", FileHdr
.SHNum
);
842 IO
.mapOptional("SHStrNdx", FileHdr
.SHStrNdx
);
845 void MappingTraits
<ELFYAML::ProgramHeader
>::mapping(
846 IO
&IO
, ELFYAML::ProgramHeader
&Phdr
) {
847 IO
.mapRequired("Type", Phdr
.Type
);
848 IO
.mapOptional("Flags", Phdr
.Flags
, ELFYAML::ELF_PF(0));
849 IO
.mapOptional("Sections", Phdr
.Sections
);
850 IO
.mapOptional("VAddr", Phdr
.VAddr
, Hex64(0));
851 IO
.mapOptional("PAddr", Phdr
.PAddr
, Hex64(0));
852 IO
.mapOptional("Align", Phdr
.Align
);
853 IO
.mapOptional("FileSize", Phdr
.FileSize
);
854 IO
.mapOptional("MemSize", Phdr
.MemSize
);
855 IO
.mapOptional("Offset", Phdr
.Offset
);
858 LLVM_YAML_STRONG_TYPEDEF(StringRef
, StOtherPiece
)
860 template <> struct ScalarTraits
<StOtherPiece
> {
861 static void output(const StOtherPiece
&Val
, void *, raw_ostream
&Out
) {
864 static StringRef
input(StringRef Scalar
, void *, StOtherPiece
&Val
) {
868 static QuotingType
mustQuote(StringRef
) { return QuotingType::None
; }
870 template <> struct SequenceElementTraits
<StOtherPiece
> {
871 static const bool flow
= true;
874 template <> struct ScalarTraits
<ELFYAML::YAMLFlowString
> {
875 static void output(const ELFYAML::YAMLFlowString
&Val
, void *,
879 static StringRef
input(StringRef Scalar
, void *,
880 ELFYAML::YAMLFlowString
&Val
) {
884 static QuotingType
mustQuote(StringRef S
) {
885 return ScalarTraits
<StringRef
>::mustQuote(S
);
888 template <> struct SequenceElementTraits
<ELFYAML::YAMLFlowString
> {
889 static const bool flow
= true;
894 struct NormalizedOther
{
895 NormalizedOther(IO
&IO
) : YamlIO(IO
) {}
896 NormalizedOther(IO
&IO
, Optional
<uint8_t> Original
) : YamlIO(IO
) {
897 assert(Original
&& "This constructor is only used for outputting YAML and "
898 "assumes a non-empty Original");
899 std::vector
<StOtherPiece
> Ret
;
900 const auto *Object
= static_cast<ELFYAML::Object
*>(YamlIO
.getContext());
901 for (std::pair
<StringRef
, uint8_t> &P
:
902 getFlags(Object
->Header
.Machine
).takeVector()) {
903 uint8_t FlagValue
= P
.second
;
904 if ((*Original
& FlagValue
) != FlagValue
)
906 *Original
&= ~FlagValue
;
907 Ret
.push_back({P
.first
});
910 if (*Original
!= 0) {
911 UnknownFlagsHolder
= std::to_string(*Original
);
912 Ret
.push_back({UnknownFlagsHolder
});
916 Other
= std::move(Ret
);
919 uint8_t toValue(StringRef Name
) {
920 const auto *Object
= static_cast<ELFYAML::Object
*>(YamlIO
.getContext());
921 MapVector
<StringRef
, uint8_t> Flags
= getFlags(Object
->Header
.Machine
);
923 auto It
= Flags
.find(Name
);
924 if (It
!= Flags
.end())
928 if (to_integer(Name
, Val
))
931 YamlIO
.setError("an unknown value is used for symbol's 'Other' field: " +
936 Optional
<uint8_t> denormalize(IO
&) {
940 for (StOtherPiece
&Val
: *Other
)
945 // st_other field is used to encode symbol visibility and platform-dependent
946 // flags and values. This method returns a name to value map that is used for
947 // parsing and encoding this field.
948 MapVector
<StringRef
, uint8_t> getFlags(unsigned EMachine
) {
949 MapVector
<StringRef
, uint8_t> Map
;
950 // STV_* values are just enumeration values. We add them in a reversed order
951 // because when we convert the st_other to named constants when printing
952 // YAML we want to use a maximum number of bits on each step:
953 // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but
954 // not as STV_HIDDEN (2) + STV_INTERNAL (1).
955 Map
["STV_PROTECTED"] = ELF::STV_PROTECTED
;
956 Map
["STV_HIDDEN"] = ELF::STV_HIDDEN
;
957 Map
["STV_INTERNAL"] = ELF::STV_INTERNAL
;
958 // STV_DEFAULT is used to represent the default visibility and has a value
959 // 0. We want to be able to read it from YAML documents, but there is no
960 // reason to print it.
961 if (!YamlIO
.outputting())
962 Map
["STV_DEFAULT"] = ELF::STV_DEFAULT
;
964 // MIPS is not consistent. All of the STO_MIPS_* values are bit flags,
965 // except STO_MIPS_MIPS16 which overlaps them. It should be checked and
966 // consumed first when we print the output, because we do not want to print
967 // any other flags that have the same bits instead.
968 if (EMachine
== ELF::EM_MIPS
) {
969 Map
["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16
;
970 Map
["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS
;
971 Map
["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC
;
972 Map
["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT
;
973 Map
["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL
;
979 Optional
<std::vector
<StOtherPiece
>> Other
;
980 std::string UnknownFlagsHolder
;
983 } // end anonymous namespace
985 void MappingTraits
<ELFYAML::Symbol
>::mapping(IO
&IO
, ELFYAML::Symbol
&Symbol
) {
986 IO
.mapOptional("Name", Symbol
.Name
, StringRef());
987 IO
.mapOptional("NameIndex", Symbol
.NameIndex
);
988 IO
.mapOptional("Type", Symbol
.Type
, ELFYAML::ELF_STT(0));
989 IO
.mapOptional("Section", Symbol
.Section
, StringRef());
990 IO
.mapOptional("Index", Symbol
.Index
);
991 IO
.mapOptional("Binding", Symbol
.Binding
, ELFYAML::ELF_STB(0));
992 IO
.mapOptional("Value", Symbol
.Value
, Hex64(0));
993 IO
.mapOptional("Size", Symbol
.Size
, Hex64(0));
995 // Symbol's Other field is a bit special. It is usually a field that
996 // represents st_other and holds the symbol visibility. However, on some
997 // platforms, it can contain bit fields and regular values, or even sometimes a
998 // crazy mix of them (see comments for NormalizedOther). Because of this, we
999 // need special handling.
1000 MappingNormalization
<NormalizedOther
, Optional
<uint8_t>> Keys(IO
,
1002 IO
.mapOptional("Other", Keys
->Other
);
1005 StringRef MappingTraits
<ELFYAML::Symbol
>::validate(IO
&IO
,
1006 ELFYAML::Symbol
&Symbol
) {
1007 if (Symbol
.Index
&& Symbol
.Section
.data())
1008 return "Index and Section cannot both be specified for Symbol";
1009 if (Symbol
.NameIndex
&& !Symbol
.Name
.empty())
1010 return "Name and NameIndex cannot both be specified for Symbol";
1014 static void commonSectionMapping(IO
&IO
, ELFYAML::Section
&Section
) {
1015 IO
.mapOptional("Name", Section
.Name
, StringRef());
1016 IO
.mapRequired("Type", Section
.Type
);
1017 IO
.mapOptional("Flags", Section
.Flags
);
1018 IO
.mapOptional("Address", Section
.Address
, Hex64(0));
1019 IO
.mapOptional("Link", Section
.Link
, StringRef());
1020 IO
.mapOptional("AddressAlign", Section
.AddressAlign
, Hex64(0));
1021 IO
.mapOptional("EntSize", Section
.EntSize
);
1023 // obj2yaml does not dump these fields. They are expected to be empty when we
1024 // are producing YAML, because yaml2obj sets appropriate values for them
1025 // automatically when they are not explicitly defined.
1026 assert(!IO
.outputting() ||
1027 (!Section
.ShOffset
.hasValue() && !Section
.ShSize
.hasValue() &&
1028 !Section
.ShName
.hasValue() && !Section
.ShFlags
.hasValue()));
1029 IO
.mapOptional("ShName", Section
.ShName
);
1030 IO
.mapOptional("ShOffset", Section
.ShOffset
);
1031 IO
.mapOptional("ShSize", Section
.ShSize
);
1032 IO
.mapOptional("ShFlags", Section
.ShFlags
);
1035 static void sectionMapping(IO
&IO
, ELFYAML::DynamicSection
&Section
) {
1036 commonSectionMapping(IO
, Section
);
1037 IO
.mapOptional("Entries", Section
.Entries
);
1038 IO
.mapOptional("Content", Section
.Content
);
1041 static void sectionMapping(IO
&IO
, ELFYAML::RawContentSection
&Section
) {
1042 commonSectionMapping(IO
, Section
);
1043 IO
.mapOptional("Content", Section
.Content
);
1044 IO
.mapOptional("Size", Section
.Size
);
1045 IO
.mapOptional("Info", Section
.Info
);
1048 static void sectionMapping(IO
&IO
, ELFYAML::StackSizesSection
&Section
) {
1049 commonSectionMapping(IO
, Section
);
1050 IO
.mapOptional("Content", Section
.Content
);
1051 IO
.mapOptional("Size", Section
.Size
);
1052 IO
.mapOptional("Entries", Section
.Entries
);
1055 static void sectionMapping(IO
&IO
, ELFYAML::HashSection
&Section
) {
1056 commonSectionMapping(IO
, Section
);
1057 IO
.mapOptional("Content", Section
.Content
);
1058 IO
.mapOptional("Bucket", Section
.Bucket
);
1059 IO
.mapOptional("Chain", Section
.Chain
);
1060 IO
.mapOptional("Size", Section
.Size
);
1063 static void sectionMapping(IO
&IO
, ELFYAML::NoteSection
&Section
) {
1064 commonSectionMapping(IO
, Section
);
1065 IO
.mapOptional("Content", Section
.Content
);
1066 IO
.mapOptional("Size", Section
.Size
);
1067 IO
.mapOptional("Notes", Section
.Notes
);
1071 static void sectionMapping(IO
&IO
, ELFYAML::GnuHashSection
&Section
) {
1072 commonSectionMapping(IO
, Section
);
1073 IO
.mapOptional("Content", Section
.Content
);
1074 IO
.mapOptional("Header", Section
.Header
);
1075 IO
.mapOptional("BloomFilter", Section
.BloomFilter
);
1076 IO
.mapOptional("HashBuckets", Section
.HashBuckets
);
1077 IO
.mapOptional("HashValues", Section
.HashValues
);
1079 static void sectionMapping(IO
&IO
, ELFYAML::NoBitsSection
&Section
) {
1080 commonSectionMapping(IO
, Section
);
1081 IO
.mapOptional("Size", Section
.Size
, Hex64(0));
1084 static void sectionMapping(IO
&IO
, ELFYAML::VerdefSection
&Section
) {
1085 commonSectionMapping(IO
, Section
);
1086 IO
.mapRequired("Info", Section
.Info
);
1087 IO
.mapOptional("Entries", Section
.Entries
);
1088 IO
.mapOptional("Content", Section
.Content
);
1091 static void sectionMapping(IO
&IO
, ELFYAML::SymverSection
&Section
) {
1092 commonSectionMapping(IO
, Section
);
1093 IO
.mapRequired("Entries", Section
.Entries
);
1096 static void sectionMapping(IO
&IO
, ELFYAML::VerneedSection
&Section
) {
1097 commonSectionMapping(IO
, Section
);
1098 IO
.mapRequired("Info", Section
.Info
);
1099 IO
.mapOptional("Dependencies", Section
.VerneedV
);
1100 IO
.mapOptional("Content", Section
.Content
);
1103 static void sectionMapping(IO
&IO
, ELFYAML::RelocationSection
&Section
) {
1104 commonSectionMapping(IO
, Section
);
1105 IO
.mapOptional("Info", Section
.RelocatableSec
, StringRef());
1106 IO
.mapOptional("Relocations", Section
.Relocations
);
1109 static void sectionMapping(IO
&IO
, ELFYAML::RelrSection
&Section
) {
1110 commonSectionMapping(IO
, Section
);
1111 IO
.mapOptional("Entries", Section
.Entries
);
1112 IO
.mapOptional("Content", Section
.Content
);
1115 static void groupSectionMapping(IO
&IO
, ELFYAML::Group
&Group
) {
1116 commonSectionMapping(IO
, Group
);
1117 IO
.mapOptional("Info", Group
.Signature
);
1118 IO
.mapRequired("Members", Group
.Members
);
1121 static void sectionMapping(IO
&IO
, ELFYAML::SymtabShndxSection
&Section
) {
1122 commonSectionMapping(IO
, Section
);
1123 IO
.mapRequired("Entries", Section
.Entries
);
1126 static void sectionMapping(IO
&IO
, ELFYAML::AddrsigSection
&Section
) {
1127 commonSectionMapping(IO
, Section
);
1128 IO
.mapOptional("Content", Section
.Content
);
1129 IO
.mapOptional("Size", Section
.Size
);
1130 IO
.mapOptional("Symbols", Section
.Symbols
);
1133 static void fillMapping(IO
&IO
, ELFYAML::Fill
&Fill
) {
1134 IO
.mapOptional("Name", Fill
.Name
, StringRef());
1135 IO
.mapOptional("Pattern", Fill
.Pattern
);
1136 IO
.mapRequired("Size", Fill
.Size
);
1139 static void sectionMapping(IO
&IO
, ELFYAML::LinkerOptionsSection
&Section
) {
1140 commonSectionMapping(IO
, Section
);
1141 IO
.mapOptional("Options", Section
.Options
);
1142 IO
.mapOptional("Content", Section
.Content
);
1145 static void sectionMapping(IO
&IO
,
1146 ELFYAML::DependentLibrariesSection
&Section
) {
1147 commonSectionMapping(IO
, Section
);
1148 IO
.mapOptional("Libraries", Section
.Libs
);
1149 IO
.mapOptional("Content", Section
.Content
);
1152 void MappingTraits
<ELFYAML::SectionOrType
>::mapping(
1153 IO
&IO
, ELFYAML::SectionOrType
§ionOrType
) {
1154 IO
.mapRequired("SectionOrType", sectionOrType
.sectionNameOrType
);
1157 void MappingTraits
<ELFYAML::SectionName
>::mapping(
1158 IO
&IO
, ELFYAML::SectionName
§ionName
) {
1159 IO
.mapRequired("Section", sectionName
.Section
);
1162 static void sectionMapping(IO
&IO
, ELFYAML::MipsABIFlags
&Section
) {
1163 commonSectionMapping(IO
, Section
);
1164 IO
.mapOptional("Version", Section
.Version
, Hex16(0));
1165 IO
.mapRequired("ISA", Section
.ISALevel
);
1166 IO
.mapOptional("ISARevision", Section
.ISARevision
, Hex8(0));
1167 IO
.mapOptional("ISAExtension", Section
.ISAExtension
,
1168 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE
));
1169 IO
.mapOptional("ASEs", Section
.ASEs
, ELFYAML::MIPS_AFL_ASE(0));
1170 IO
.mapOptional("FpABI", Section
.FpABI
,
1171 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY
));
1172 IO
.mapOptional("GPRSize", Section
.GPRSize
,
1173 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE
));
1174 IO
.mapOptional("CPR1Size", Section
.CPR1Size
,
1175 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE
));
1176 IO
.mapOptional("CPR2Size", Section
.CPR2Size
,
1177 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE
));
1178 IO
.mapOptional("Flags1", Section
.Flags1
, ELFYAML::MIPS_AFL_FLAGS1(0));
1179 IO
.mapOptional("Flags2", Section
.Flags2
, Hex32(0));
1182 void MappingTraits
<std::unique_ptr
<ELFYAML::Chunk
>>::mapping(
1183 IO
&IO
, std::unique_ptr
<ELFYAML::Chunk
> &Section
) {
1184 ELFYAML::ELF_SHT Type
;
1185 if (IO
.outputting()) {
1186 Type
= cast
<ELFYAML::Section
>(Section
.get())->Type
;
1188 // When the Type string does not have a "SHT_" prefix, we know it is not a
1189 // description of a regular ELF output section. Currently, we have one
1190 // special type named "Fill". See comments for Fill.
1192 IO
.mapRequired("Type", StrType
);
1193 if (StrType
== "Fill") {
1194 Section
.reset(new ELFYAML::Fill());
1195 fillMapping(IO
, *cast
<ELFYAML::Fill
>(Section
.get()));
1199 IO
.mapRequired("Type", Type
);
1203 case ELF::SHT_DYNAMIC
:
1204 if (!IO
.outputting())
1205 Section
.reset(new ELFYAML::DynamicSection());
1206 sectionMapping(IO
, *cast
<ELFYAML::DynamicSection
>(Section
.get()));
1210 if (!IO
.outputting())
1211 Section
.reset(new ELFYAML::RelocationSection());
1212 sectionMapping(IO
, *cast
<ELFYAML::RelocationSection
>(Section
.get()));
1215 if (!IO
.outputting())
1216 Section
.reset(new ELFYAML::RelrSection());
1217 sectionMapping(IO
, *cast
<ELFYAML::RelrSection
>(Section
.get()));
1219 case ELF::SHT_GROUP
:
1220 if (!IO
.outputting())
1221 Section
.reset(new ELFYAML::Group());
1222 groupSectionMapping(IO
, *cast
<ELFYAML::Group
>(Section
.get()));
1224 case ELF::SHT_NOBITS
:
1225 if (!IO
.outputting())
1226 Section
.reset(new ELFYAML::NoBitsSection());
1227 sectionMapping(IO
, *cast
<ELFYAML::NoBitsSection
>(Section
.get()));
1230 if (!IO
.outputting())
1231 Section
.reset(new ELFYAML::HashSection());
1232 sectionMapping(IO
, *cast
<ELFYAML::HashSection
>(Section
.get()));
1235 if (!IO
.outputting())
1236 Section
.reset(new ELFYAML::NoteSection());
1237 sectionMapping(IO
, *cast
<ELFYAML::NoteSection
>(Section
.get()));
1239 case ELF::SHT_GNU_HASH
:
1240 if (!IO
.outputting())
1241 Section
.reset(new ELFYAML::GnuHashSection());
1242 sectionMapping(IO
, *cast
<ELFYAML::GnuHashSection
>(Section
.get()));
1244 case ELF::SHT_MIPS_ABIFLAGS
:
1245 if (!IO
.outputting())
1246 Section
.reset(new ELFYAML::MipsABIFlags());
1247 sectionMapping(IO
, *cast
<ELFYAML::MipsABIFlags
>(Section
.get()));
1249 case ELF::SHT_GNU_verdef
:
1250 if (!IO
.outputting())
1251 Section
.reset(new ELFYAML::VerdefSection());
1252 sectionMapping(IO
, *cast
<ELFYAML::VerdefSection
>(Section
.get()));
1254 case ELF::SHT_GNU_versym
:
1255 if (!IO
.outputting())
1256 Section
.reset(new ELFYAML::SymverSection());
1257 sectionMapping(IO
, *cast
<ELFYAML::SymverSection
>(Section
.get()));
1259 case ELF::SHT_GNU_verneed
:
1260 if (!IO
.outputting())
1261 Section
.reset(new ELFYAML::VerneedSection());
1262 sectionMapping(IO
, *cast
<ELFYAML::VerneedSection
>(Section
.get()));
1264 case ELF::SHT_SYMTAB_SHNDX
:
1265 if (!IO
.outputting())
1266 Section
.reset(new ELFYAML::SymtabShndxSection());
1267 sectionMapping(IO
, *cast
<ELFYAML::SymtabShndxSection
>(Section
.get()));
1269 case ELF::SHT_LLVM_ADDRSIG
:
1270 if (!IO
.outputting())
1271 Section
.reset(new ELFYAML::AddrsigSection());
1272 sectionMapping(IO
, *cast
<ELFYAML::AddrsigSection
>(Section
.get()));
1274 case ELF::SHT_LLVM_LINKER_OPTIONS
:
1275 if (!IO
.outputting())
1276 Section
.reset(new ELFYAML::LinkerOptionsSection());
1277 sectionMapping(IO
, *cast
<ELFYAML::LinkerOptionsSection
>(Section
.get()));
1279 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES
:
1280 if (!IO
.outputting())
1281 Section
.reset(new ELFYAML::DependentLibrariesSection());
1283 *cast
<ELFYAML::DependentLibrariesSection
>(Section
.get()));
1286 if (!IO
.outputting()) {
1288 IO
.mapOptional("Name", Name
, StringRef());
1289 Name
= ELFYAML::dropUniqueSuffix(Name
);
1291 if (ELFYAML::StackSizesSection::nameMatches(Name
))
1292 Section
= std::make_unique
<ELFYAML::StackSizesSection
>();
1294 Section
= std::make_unique
<ELFYAML::RawContentSection
>();
1297 if (auto S
= dyn_cast
<ELFYAML::RawContentSection
>(Section
.get()))
1298 sectionMapping(IO
, *S
);
1300 sectionMapping(IO
, *cast
<ELFYAML::StackSizesSection
>(Section
.get()));
1304 StringRef MappingTraits
<std::unique_ptr
<ELFYAML::Chunk
>>::validate(
1305 IO
&io
, std::unique_ptr
<ELFYAML::Chunk
> &C
) {
1306 if (const auto *RawSection
= dyn_cast
<ELFYAML::RawContentSection
>(C
.get())) {
1307 if (RawSection
->Size
&& RawSection
->Content
&&
1308 (uint64_t)(*RawSection
->Size
) < RawSection
->Content
->binary_size())
1309 return "Section size must be greater than or equal to the content size";
1310 if (RawSection
->Flags
&& RawSection
->ShFlags
)
1311 return "ShFlags and Flags cannot be used together";
1315 if (const auto *SS
= dyn_cast
<ELFYAML::StackSizesSection
>(C
.get())) {
1316 if (!SS
->Entries
&& !SS
->Content
&& !SS
->Size
)
1317 return ".stack_sizes: one of Content, Entries and Size must be specified";
1319 if (SS
->Size
&& SS
->Content
&&
1320 (uint64_t)(*SS
->Size
) < SS
->Content
->binary_size())
1321 return ".stack_sizes: Size must be greater than or equal to the content "
1324 // We accept Content, Size or both together when there are no Entries.
1329 return ".stack_sizes: Size and Entries cannot be used together";
1331 return ".stack_sizes: Content and Entries cannot be used together";
1335 if (const auto *HS
= dyn_cast
<ELFYAML::HashSection
>(C
.get())) {
1336 if (!HS
->Content
&& !HS
->Bucket
&& !HS
->Chain
&& !HS
->Size
)
1337 return "one of \"Content\", \"Size\", \"Bucket\" or \"Chain\" must be "
1340 if (HS
->Content
|| HS
->Size
) {
1341 if (HS
->Size
&& HS
->Content
&&
1342 (uint64_t)*HS
->Size
< HS
->Content
->binary_size())
1343 return "\"Size\" must be greater than or equal to the content "
1347 return "\"Bucket\" cannot be used with \"Content\" or \"Size\"";
1349 return "\"Chain\" cannot be used with \"Content\" or \"Size\"";
1353 if ((HS
->Bucket
&& !HS
->Chain
) || (!HS
->Bucket
&& HS
->Chain
))
1354 return "\"Bucket\" and \"Chain\" must be used together";
1358 if (const auto *Sec
= dyn_cast
<ELFYAML::AddrsigSection
>(C
.get())) {
1359 if (!Sec
->Symbols
&& !Sec
->Content
&& !Sec
->Size
)
1360 return "one of \"Content\", \"Size\" or \"Symbols\" must be specified";
1362 if (Sec
->Content
|| Sec
->Size
) {
1363 if (Sec
->Size
&& Sec
->Content
&&
1364 (uint64_t)*Sec
->Size
< Sec
->Content
->binary_size())
1365 return "\"Size\" must be greater than or equal to the content "
1369 return "\"Symbols\" cannot be used with \"Content\" or \"Size\"";
1376 for (const ELFYAML::AddrsigSymbol
&AS
: *Sec
->Symbols
)
1377 if (AS
.Index
&& AS
.Name
)
1378 return "\"Index\" and \"Name\" cannot be used together when defining a "
1383 if (const auto *NS
= dyn_cast
<ELFYAML::NoteSection
>(C
.get())) {
1384 if (!NS
->Content
&& !NS
->Size
&& !NS
->Notes
)
1385 return "one of \"Content\", \"Size\" or \"Notes\" must be "
1388 if (!NS
->Content
&& !NS
->Size
)
1391 if (NS
->Size
&& NS
->Content
&&
1392 (uint64_t)*NS
->Size
< NS
->Content
->binary_size())
1393 return "\"Size\" must be greater than or equal to the content "
1397 return "\"Notes\" cannot be used with \"Content\" or \"Size\"";
1401 if (const auto *Sec
= dyn_cast
<ELFYAML::GnuHashSection
>(C
.get())) {
1402 if (!Sec
->Content
&& !Sec
->Header
&& !Sec
->BloomFilter
&&
1403 !Sec
->HashBuckets
&& !Sec
->HashValues
)
1404 return "either \"Content\" or \"Header\", \"BloomFilter\", "
1405 "\"HashBuckets\" and \"HashBuckets\" must be specified";
1407 if (Sec
->Header
|| Sec
->BloomFilter
|| Sec
->HashBuckets
||
1409 if (!Sec
->Header
|| !Sec
->BloomFilter
|| !Sec
->HashBuckets
||
1411 return "\"Header\", \"BloomFilter\", "
1412 "\"HashBuckets\" and \"HashValues\" must be used together";
1414 return "\"Header\", \"BloomFilter\", "
1415 "\"HashBuckets\" and \"HashValues\" can't be used together with "
1420 // Only Content is specified.
1424 if (const auto *Sec
= dyn_cast
<ELFYAML::LinkerOptionsSection
>(C
.get())) {
1425 if (Sec
->Options
&& Sec
->Content
)
1426 return "\"Options\" and \"Content\" can't be used together";
1430 if (const auto *Sec
= dyn_cast
<ELFYAML::DependentLibrariesSection
>(C
.get())) {
1431 if (Sec
->Libs
&& Sec
->Content
)
1432 return "SHT_LLVM_DEPENDENT_LIBRARIES: \"Libraries\" and \"Content\" "
1438 if (const auto *F
= dyn_cast
<ELFYAML::Fill
>(C
.get())) {
1441 if (F
->Pattern
->binary_size() != 0 && !F
->Size
)
1442 return "\"Size\" can't be 0 when \"Pattern\" is not empty";
1446 if (const auto *VD
= dyn_cast
<ELFYAML::VerdefSection
>(C
.get())) {
1447 if (VD
->Entries
&& VD
->Content
)
1448 return "SHT_GNU_verdef: \"Entries\" and \"Content\" can't be used "
1453 if (const auto *VD
= dyn_cast
<ELFYAML::VerneedSection
>(C
.get())) {
1454 if (VD
->VerneedV
&& VD
->Content
)
1455 return "SHT_GNU_verneed: \"Dependencies\" and \"Content\" can't be used "
1460 if (const auto *RS
= dyn_cast
<ELFYAML::RelrSection
>(C
.get())) {
1461 if (RS
->Entries
&& RS
->Content
)
1462 return "\"Entries\" and \"Content\" can't be used together";
1471 struct NormalizedMips64RelType
{
1472 NormalizedMips64RelType(IO
&)
1473 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE
)),
1474 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE
)),
1475 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE
)),
1476 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF
)) {}
1477 NormalizedMips64RelType(IO
&, ELFYAML::ELF_REL Original
)
1478 : Type(Original
& 0xFF), Type2(Original
>> 8 & 0xFF),
1479 Type3(Original
>> 16 & 0xFF), SpecSym(Original
>> 24 & 0xFF) {}
1481 ELFYAML::ELF_REL
denormalize(IO
&) {
1482 ELFYAML::ELF_REL Res
= Type
| Type2
<< 8 | Type3
<< 16 | SpecSym
<< 24;
1486 ELFYAML::ELF_REL Type
;
1487 ELFYAML::ELF_REL Type2
;
1488 ELFYAML::ELF_REL Type3
;
1489 ELFYAML::ELF_RSS SpecSym
;
1492 } // end anonymous namespace
1494 void MappingTraits
<ELFYAML::StackSizeEntry
>::mapping(
1495 IO
&IO
, ELFYAML::StackSizeEntry
&E
) {
1496 assert(IO
.getContext() && "The IO context is not initialized");
1497 IO
.mapOptional("Address", E
.Address
, Hex64(0));
1498 IO
.mapRequired("Size", E
.Size
);
1501 void MappingTraits
<ELFYAML::GnuHashHeader
>::mapping(IO
&IO
,
1502 ELFYAML::GnuHashHeader
&E
) {
1503 assert(IO
.getContext() && "The IO context is not initialized");
1504 IO
.mapOptional("NBuckets", E
.NBuckets
);
1505 IO
.mapRequired("SymNdx", E
.SymNdx
);
1506 IO
.mapOptional("MaskWords", E
.MaskWords
);
1507 IO
.mapRequired("Shift2", E
.Shift2
);
1510 void MappingTraits
<ELFYAML::DynamicEntry
>::mapping(IO
&IO
,
1511 ELFYAML::DynamicEntry
&Rel
) {
1512 assert(IO
.getContext() && "The IO context is not initialized");
1514 IO
.mapRequired("Tag", Rel
.Tag
);
1515 IO
.mapRequired("Value", Rel
.Val
);
1518 void MappingTraits
<ELFYAML::NoteEntry
>::mapping(IO
&IO
, ELFYAML::NoteEntry
&N
) {
1519 assert(IO
.getContext() && "The IO context is not initialized");
1521 IO
.mapOptional("Name", N
.Name
);
1522 IO
.mapOptional("Desc", N
.Desc
);
1523 IO
.mapRequired("Type", N
.Type
);
1526 void MappingTraits
<ELFYAML::VerdefEntry
>::mapping(IO
&IO
,
1527 ELFYAML::VerdefEntry
&E
) {
1528 assert(IO
.getContext() && "The IO context is not initialized");
1530 IO
.mapRequired("Version", E
.Version
);
1531 IO
.mapRequired("Flags", E
.Flags
);
1532 IO
.mapRequired("VersionNdx", E
.VersionNdx
);
1533 IO
.mapRequired("Hash", E
.Hash
);
1534 IO
.mapRequired("Names", E
.VerNames
);
1537 void MappingTraits
<ELFYAML::VerneedEntry
>::mapping(IO
&IO
,
1538 ELFYAML::VerneedEntry
&E
) {
1539 assert(IO
.getContext() && "The IO context is not initialized");
1541 IO
.mapRequired("Version", E
.Version
);
1542 IO
.mapRequired("File", E
.File
);
1543 IO
.mapRequired("Entries", E
.AuxV
);
1546 void MappingTraits
<ELFYAML::VernauxEntry
>::mapping(IO
&IO
,
1547 ELFYAML::VernauxEntry
&E
) {
1548 assert(IO
.getContext() && "The IO context is not initialized");
1550 IO
.mapRequired("Name", E
.Name
);
1551 IO
.mapRequired("Hash", E
.Hash
);
1552 IO
.mapRequired("Flags", E
.Flags
);
1553 IO
.mapRequired("Other", E
.Other
);
1556 void MappingTraits
<ELFYAML::Relocation
>::mapping(IO
&IO
,
1557 ELFYAML::Relocation
&Rel
) {
1558 const auto *Object
= static_cast<ELFYAML::Object
*>(IO
.getContext());
1559 assert(Object
&& "The IO context is not initialized");
1561 IO
.mapRequired("Offset", Rel
.Offset
);
1562 IO
.mapOptional("Symbol", Rel
.Symbol
);
1564 if (Object
->Header
.Machine
== ELFYAML::ELF_EM(ELF::EM_MIPS
) &&
1565 Object
->Header
.Class
== ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64
)) {
1566 MappingNormalization
<NormalizedMips64RelType
, ELFYAML::ELF_REL
> Key(
1568 IO
.mapRequired("Type", Key
->Type
);
1569 IO
.mapOptional("Type2", Key
->Type2
, ELFYAML::ELF_REL(ELF::R_MIPS_NONE
));
1570 IO
.mapOptional("Type3", Key
->Type3
, ELFYAML::ELF_REL(ELF::R_MIPS_NONE
));
1571 IO
.mapOptional("SpecSym", Key
->SpecSym
, ELFYAML::ELF_RSS(ELF::RSS_UNDEF
));
1573 IO
.mapRequired("Type", Rel
.Type
);
1575 IO
.mapOptional("Addend", Rel
.Addend
, (int64_t)0);
1578 void MappingTraits
<ELFYAML::Object
>::mapping(IO
&IO
, ELFYAML::Object
&Object
) {
1579 assert(!IO
.getContext() && "The IO context is initialized already");
1580 IO
.setContext(&Object
);
1581 IO
.mapTag("!ELF", true);
1582 IO
.mapRequired("FileHeader", Object
.Header
);
1583 IO
.mapOptional("ProgramHeaders", Object
.ProgramHeaders
);
1584 IO
.mapOptional("Sections", Object
.Chunks
);
1585 IO
.mapOptional("Symbols", Object
.Symbols
);
1586 IO
.mapOptional("DynamicSymbols", Object
.DynamicSymbols
);
1587 IO
.setContext(nullptr);
1590 void MappingTraits
<ELFYAML::AddrsigSymbol
>::mapping(IO
&IO
, ELFYAML::AddrsigSymbol
&Sym
) {
1591 assert(IO
.getContext() && "The IO context is not initialized");
1592 IO
.mapOptional("Name", Sym
.Name
);
1593 IO
.mapOptional("Index", Sym
.Index
);
1596 void MappingTraits
<ELFYAML::LinkerOption
>::mapping(IO
&IO
,
1597 ELFYAML::LinkerOption
&Opt
) {
1598 assert(IO
.getContext() && "The IO context is not initialized");
1599 IO
.mapRequired("Name", Opt
.Key
);
1600 IO
.mapRequired("Value", Opt
.Value
);
1603 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG
)
1604 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP
)
1605 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT
)
1606 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE
)
1607 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1
)
1609 } // end namespace yaml
1611 } // end namespace llvm