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. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "llvm/ObjectYAML/ELFYAML.h" 14 #include "llvm/ADT/StringRef.h" 15 #include "llvm/BinaryFormat/ELF.h" 16 #include "llvm/Support/Casting.h" 17 #include "llvm/Support/ErrorHandling.h" 18 #include "llvm/Support/MipsABIFlags.h" 19 #include "llvm/Support/YAMLTraits.h" 20 #include <cassert> 21 #include <cstdint> 22 23 namespace llvm { 24 25 ELFYAML::Section::~Section() = default; 26 27 namespace yaml { 28 29 void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration( 30 IO &IO, ELFYAML::ELF_ET &Value) { 31 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 32 ECase(ET_NONE); 33 ECase(ET_REL); 34 ECase(ET_EXEC); 35 ECase(ET_DYN); 36 ECase(ET_CORE); 37 #undef ECase 38 IO.enumFallback<Hex16>(Value); 39 } 40 41 void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration( 42 IO &IO, ELFYAML::ELF_PT &Value) { 43 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 44 ECase(PT_NULL); 45 ECase(PT_LOAD); 46 ECase(PT_DYNAMIC); 47 ECase(PT_INTERP); 48 ECase(PT_NOTE); 49 ECase(PT_SHLIB); 50 ECase(PT_PHDR); 51 ECase(PT_TLS); 52 ECase(PT_GNU_EH_FRAME); 53 #undef ECase 54 IO.enumFallback<Hex32>(Value); 55 } 56 57 void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration( 58 IO &IO, ELFYAML::ELF_EM &Value) { 59 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 60 ECase(EM_NONE); 61 ECase(EM_M32); 62 ECase(EM_SPARC); 63 ECase(EM_386); 64 ECase(EM_68K); 65 ECase(EM_88K); 66 ECase(EM_IAMCU); 67 ECase(EM_860); 68 ECase(EM_MIPS); 69 ECase(EM_S370); 70 ECase(EM_MIPS_RS3_LE); 71 ECase(EM_PARISC); 72 ECase(EM_VPP500); 73 ECase(EM_SPARC32PLUS); 74 ECase(EM_960); 75 ECase(EM_PPC); 76 ECase(EM_PPC64); 77 ECase(EM_S390); 78 ECase(EM_SPU); 79 ECase(EM_V800); 80 ECase(EM_FR20); 81 ECase(EM_RH32); 82 ECase(EM_RCE); 83 ECase(EM_ARM); 84 ECase(EM_ALPHA); 85 ECase(EM_SH); 86 ECase(EM_SPARCV9); 87 ECase(EM_TRICORE); 88 ECase(EM_ARC); 89 ECase(EM_H8_300); 90 ECase(EM_H8_300H); 91 ECase(EM_H8S); 92 ECase(EM_H8_500); 93 ECase(EM_IA_64); 94 ECase(EM_MIPS_X); 95 ECase(EM_COLDFIRE); 96 ECase(EM_68HC12); 97 ECase(EM_MMA); 98 ECase(EM_PCP); 99 ECase(EM_NCPU); 100 ECase(EM_NDR1); 101 ECase(EM_STARCORE); 102 ECase(EM_ME16); 103 ECase(EM_ST100); 104 ECase(EM_TINYJ); 105 ECase(EM_X86_64); 106 ECase(EM_PDSP); 107 ECase(EM_PDP10); 108 ECase(EM_PDP11); 109 ECase(EM_FX66); 110 ECase(EM_ST9PLUS); 111 ECase(EM_ST7); 112 ECase(EM_68HC16); 113 ECase(EM_68HC11); 114 ECase(EM_68HC08); 115 ECase(EM_68HC05); 116 ECase(EM_SVX); 117 ECase(EM_ST19); 118 ECase(EM_VAX); 119 ECase(EM_CRIS); 120 ECase(EM_JAVELIN); 121 ECase(EM_FIREPATH); 122 ECase(EM_ZSP); 123 ECase(EM_MMIX); 124 ECase(EM_HUANY); 125 ECase(EM_PRISM); 126 ECase(EM_AVR); 127 ECase(EM_FR30); 128 ECase(EM_D10V); 129 ECase(EM_D30V); 130 ECase(EM_V850); 131 ECase(EM_M32R); 132 ECase(EM_MN10300); 133 ECase(EM_MN10200); 134 ECase(EM_PJ); 135 ECase(EM_OPENRISC); 136 ECase(EM_ARC_COMPACT); 137 ECase(EM_XTENSA); 138 ECase(EM_VIDEOCORE); 139 ECase(EM_TMM_GPP); 140 ECase(EM_NS32K); 141 ECase(EM_TPC); 142 ECase(EM_SNP1K); 143 ECase(EM_ST200); 144 ECase(EM_IP2K); 145 ECase(EM_MAX); 146 ECase(EM_CR); 147 ECase(EM_F2MC16); 148 ECase(EM_MSP430); 149 ECase(EM_BLACKFIN); 150 ECase(EM_SE_C33); 151 ECase(EM_SEP); 152 ECase(EM_ARCA); 153 ECase(EM_UNICORE); 154 ECase(EM_EXCESS); 155 ECase(EM_DXP); 156 ECase(EM_ALTERA_NIOS2); 157 ECase(EM_CRX); 158 ECase(EM_XGATE); 159 ECase(EM_C166); 160 ECase(EM_M16C); 161 ECase(EM_DSPIC30F); 162 ECase(EM_CE); 163 ECase(EM_M32C); 164 ECase(EM_TSK3000); 165 ECase(EM_RS08); 166 ECase(EM_SHARC); 167 ECase(EM_ECOG2); 168 ECase(EM_SCORE7); 169 ECase(EM_DSP24); 170 ECase(EM_VIDEOCORE3); 171 ECase(EM_LATTICEMICO32); 172 ECase(EM_SE_C17); 173 ECase(EM_TI_C6000); 174 ECase(EM_TI_C2000); 175 ECase(EM_TI_C5500); 176 ECase(EM_MMDSP_PLUS); 177 ECase(EM_CYPRESS_M8C); 178 ECase(EM_R32C); 179 ECase(EM_TRIMEDIA); 180 ECase(EM_HEXAGON); 181 ECase(EM_8051); 182 ECase(EM_STXP7X); 183 ECase(EM_NDS32); 184 ECase(EM_ECOG1); 185 ECase(EM_ECOG1X); 186 ECase(EM_MAXQ30); 187 ECase(EM_XIMO16); 188 ECase(EM_MANIK); 189 ECase(EM_CRAYNV2); 190 ECase(EM_RX); 191 ECase(EM_METAG); 192 ECase(EM_MCST_ELBRUS); 193 ECase(EM_ECOG16); 194 ECase(EM_CR16); 195 ECase(EM_ETPU); 196 ECase(EM_SLE9X); 197 ECase(EM_L10M); 198 ECase(EM_K10M); 199 ECase(EM_AARCH64); 200 ECase(EM_AVR32); 201 ECase(EM_STM8); 202 ECase(EM_TILE64); 203 ECase(EM_TILEPRO); 204 ECase(EM_CUDA); 205 ECase(EM_TILEGX); 206 ECase(EM_CLOUDSHIELD); 207 ECase(EM_COREA_1ST); 208 ECase(EM_COREA_2ND); 209 ECase(EM_ARC_COMPACT2); 210 ECase(EM_OPEN8); 211 ECase(EM_RL78); 212 ECase(EM_VIDEOCORE5); 213 ECase(EM_78KOR); 214 ECase(EM_56800EX); 215 ECase(EM_AMDGPU); 216 ECase(EM_RISCV); 217 ECase(EM_LANAI); 218 ECase(EM_BPF); 219 #undef ECase 220 } 221 222 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration( 223 IO &IO, ELFYAML::ELF_ELFCLASS &Value) { 224 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 225 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it 226 // here. 227 ECase(ELFCLASS32); 228 ECase(ELFCLASS64); 229 #undef ECase 230 } 231 232 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration( 233 IO &IO, ELFYAML::ELF_ELFDATA &Value) { 234 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 235 // ELFDATANONE is an invalid data encoding, but we accept it because 236 // we want to be able to produce invalid binaries for the tests. 237 ECase(ELFDATANONE); 238 ECase(ELFDATA2LSB); 239 ECase(ELFDATA2MSB); 240 #undef ECase 241 } 242 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 271 } 272 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_MACH_V55); 341 BCase(EF_HEXAGON_MACH_V60); 342 BCase(EF_HEXAGON_MACH_V62); 343 BCase(EF_HEXAGON_MACH_V65); 344 BCase(EF_HEXAGON_ISA_V2); 345 BCase(EF_HEXAGON_ISA_V3); 346 BCase(EF_HEXAGON_ISA_V4); 347 BCase(EF_HEXAGON_ISA_V5); 348 BCase(EF_HEXAGON_ISA_V55); 349 BCase(EF_HEXAGON_ISA_V60); 350 BCase(EF_HEXAGON_ISA_V62); 351 BCase(EF_HEXAGON_ISA_V65); 352 break; 353 case ELF::EM_AVR: 354 BCase(EF_AVR_ARCH_AVR1); 355 BCase(EF_AVR_ARCH_AVR2); 356 BCase(EF_AVR_ARCH_AVR25); 357 BCase(EF_AVR_ARCH_AVR3); 358 BCase(EF_AVR_ARCH_AVR31); 359 BCase(EF_AVR_ARCH_AVR35); 360 BCase(EF_AVR_ARCH_AVR4); 361 BCase(EF_AVR_ARCH_AVR51); 362 BCase(EF_AVR_ARCH_AVR6); 363 BCase(EF_AVR_ARCH_AVRTINY); 364 BCase(EF_AVR_ARCH_XMEGA1); 365 BCase(EF_AVR_ARCH_XMEGA2); 366 BCase(EF_AVR_ARCH_XMEGA3); 367 BCase(EF_AVR_ARCH_XMEGA4); 368 BCase(EF_AVR_ARCH_XMEGA5); 369 BCase(EF_AVR_ARCH_XMEGA6); 370 BCase(EF_AVR_ARCH_XMEGA7); 371 break; 372 case ELF::EM_RISCV: 373 BCase(EF_RISCV_RVC); 374 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI); 375 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI); 376 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI); 377 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI); 378 BCase(EF_RISCV_RVE); 379 break; 380 case ELF::EM_AMDGPU: 381 BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH); 382 BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH); 383 BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH); 384 BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH); 385 BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH); 386 BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH); 387 BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH); 388 BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH); 389 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH); 390 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH); 391 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH); 392 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH); 393 BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH); 394 BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH); 395 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH); 396 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH); 397 BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH); 398 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH); 399 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH); 400 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH); 401 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH); 402 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH); 403 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH); 404 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH); 405 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH); 406 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH); 407 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH); 408 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH); 409 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH); 410 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH); 411 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH); 412 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH); 413 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH); 414 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH); 415 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH); 416 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH); 417 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH); 418 BCase(EF_AMDGPU_XNACK); 419 BCase(EF_AMDGPU_SRAM_ECC); 420 break; 421 case ELF::EM_X86_64: 422 break; 423 default: 424 llvm_unreachable("Unsupported architecture"); 425 } 426 #undef BCase 427 #undef BCaseMask 428 } 429 430 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration( 431 IO &IO, ELFYAML::ELF_SHT &Value) { 432 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 433 assert(Object && "The IO context is not initialized"); 434 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 435 ECase(SHT_NULL); 436 ECase(SHT_PROGBITS); 437 ECase(SHT_SYMTAB); 438 // FIXME: Issue a diagnostic with this information. 439 ECase(SHT_STRTAB); 440 ECase(SHT_RELA); 441 ECase(SHT_HASH); 442 ECase(SHT_DYNAMIC); 443 ECase(SHT_NOTE); 444 ECase(SHT_NOBITS); 445 ECase(SHT_REL); 446 ECase(SHT_SHLIB); 447 ECase(SHT_DYNSYM); 448 ECase(SHT_INIT_ARRAY); 449 ECase(SHT_FINI_ARRAY); 450 ECase(SHT_PREINIT_ARRAY); 451 ECase(SHT_GROUP); 452 ECase(SHT_SYMTAB_SHNDX); 453 ECase(SHT_RELR); 454 ECase(SHT_ANDROID_REL); 455 ECase(SHT_ANDROID_RELA); 456 ECase(SHT_ANDROID_RELR); 457 ECase(SHT_LLVM_ODRTAB); 458 ECase(SHT_LLVM_LINKER_OPTIONS); 459 ECase(SHT_LLVM_CALL_GRAPH_PROFILE); 460 ECase(SHT_LLVM_ADDRSIG); 461 ECase(SHT_LLVM_DEPENDENT_LIBRARIES); 462 ECase(SHT_GNU_ATTRIBUTES); 463 ECase(SHT_GNU_HASH); 464 ECase(SHT_GNU_verdef); 465 ECase(SHT_GNU_verneed); 466 ECase(SHT_GNU_versym); 467 switch (Object->Header.Machine) { 468 case ELF::EM_ARM: 469 ECase(SHT_ARM_EXIDX); 470 ECase(SHT_ARM_PREEMPTMAP); 471 ECase(SHT_ARM_ATTRIBUTES); 472 ECase(SHT_ARM_DEBUGOVERLAY); 473 ECase(SHT_ARM_OVERLAYSECTION); 474 break; 475 case ELF::EM_HEXAGON: 476 ECase(SHT_HEX_ORDERED); 477 break; 478 case ELF::EM_X86_64: 479 ECase(SHT_X86_64_UNWIND); 480 break; 481 case ELF::EM_MIPS: 482 ECase(SHT_MIPS_REGINFO); 483 ECase(SHT_MIPS_OPTIONS); 484 ECase(SHT_MIPS_DWARF); 485 ECase(SHT_MIPS_ABIFLAGS); 486 break; 487 default: 488 // Nothing to do. 489 break; 490 } 491 #undef ECase 492 IO.enumFallback<Hex32>(Value); 493 } 494 495 void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO, 496 ELFYAML::ELF_PF &Value) { 497 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 498 BCase(PF_X); 499 BCase(PF_W); 500 BCase(PF_R); 501 } 502 503 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO, 504 ELFYAML::ELF_SHF &Value) { 505 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 506 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 507 BCase(SHF_WRITE); 508 BCase(SHF_ALLOC); 509 BCase(SHF_EXCLUDE); 510 BCase(SHF_EXECINSTR); 511 BCase(SHF_MERGE); 512 BCase(SHF_STRINGS); 513 BCase(SHF_INFO_LINK); 514 BCase(SHF_LINK_ORDER); 515 BCase(SHF_OS_NONCONFORMING); 516 BCase(SHF_GROUP); 517 BCase(SHF_TLS); 518 BCase(SHF_COMPRESSED); 519 switch (Object->Header.Machine) { 520 case ELF::EM_ARM: 521 BCase(SHF_ARM_PURECODE); 522 break; 523 case ELF::EM_HEXAGON: 524 BCase(SHF_HEX_GPREL); 525 break; 526 case ELF::EM_MIPS: 527 BCase(SHF_MIPS_NODUPES); 528 BCase(SHF_MIPS_NAMES); 529 BCase(SHF_MIPS_LOCAL); 530 BCase(SHF_MIPS_NOSTRIP); 531 BCase(SHF_MIPS_GPREL); 532 BCase(SHF_MIPS_MERGE); 533 BCase(SHF_MIPS_ADDR); 534 BCase(SHF_MIPS_STRING); 535 break; 536 case ELF::EM_X86_64: 537 BCase(SHF_X86_64_LARGE); 538 break; 539 default: 540 // Nothing to do. 541 break; 542 } 543 #undef BCase 544 } 545 546 void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration( 547 IO &IO, ELFYAML::ELF_SHN &Value) { 548 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 549 ECase(SHN_UNDEF); 550 ECase(SHN_LORESERVE); 551 ECase(SHN_LOPROC); 552 ECase(SHN_HIPROC); 553 ECase(SHN_LOOS); 554 ECase(SHN_HIOS); 555 ECase(SHN_ABS); 556 ECase(SHN_COMMON); 557 ECase(SHN_XINDEX); 558 ECase(SHN_HIRESERVE); 559 ECase(SHN_AMDGPU_LDS); 560 ECase(SHN_HEXAGON_SCOMMON); 561 ECase(SHN_HEXAGON_SCOMMON_1); 562 ECase(SHN_HEXAGON_SCOMMON_2); 563 ECase(SHN_HEXAGON_SCOMMON_4); 564 ECase(SHN_HEXAGON_SCOMMON_8); 565 #undef ECase 566 IO.enumFallback<Hex32>(Value); 567 } 568 569 void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration( 570 IO &IO, ELFYAML::ELF_STB &Value) { 571 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 572 ECase(STB_LOCAL); 573 ECase(STB_GLOBAL); 574 ECase(STB_WEAK); 575 ECase(STB_GNU_UNIQUE); 576 #undef ECase 577 IO.enumFallback<Hex8>(Value); 578 } 579 580 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration( 581 IO &IO, ELFYAML::ELF_STT &Value) { 582 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 583 ECase(STT_NOTYPE); 584 ECase(STT_OBJECT); 585 ECase(STT_FUNC); 586 ECase(STT_SECTION); 587 ECase(STT_FILE); 588 ECase(STT_COMMON); 589 ECase(STT_TLS); 590 ECase(STT_GNU_IFUNC); 591 #undef ECase 592 IO.enumFallback<Hex8>(Value); 593 } 594 595 void ScalarEnumerationTraits<ELFYAML::ELF_STV>::enumeration( 596 IO &IO, ELFYAML::ELF_STV &Value) { 597 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 598 ECase(STV_DEFAULT); 599 ECase(STV_INTERNAL); 600 ECase(STV_HIDDEN); 601 ECase(STV_PROTECTED); 602 #undef ECase 603 } 604 605 void ScalarBitSetTraits<ELFYAML::ELF_STO>::bitset(IO &IO, 606 ELFYAML::ELF_STO &Value) { 607 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 608 assert(Object && "The IO context is not initialized"); 609 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 610 switch (Object->Header.Machine) { 611 case ELF::EM_MIPS: 612 BCase(STO_MIPS_OPTIONAL); 613 BCase(STO_MIPS_PLT); 614 BCase(STO_MIPS_PIC); 615 BCase(STO_MIPS_MICROMIPS); 616 break; 617 default: 618 break; // Nothing to do 619 } 620 #undef BCase 621 #undef BCaseMask 622 } 623 624 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration( 625 IO &IO, ELFYAML::ELF_RSS &Value) { 626 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 627 ECase(RSS_UNDEF); 628 ECase(RSS_GP); 629 ECase(RSS_GP0); 630 ECase(RSS_LOC); 631 #undef ECase 632 } 633 634 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration( 635 IO &IO, ELFYAML::ELF_REL &Value) { 636 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 637 assert(Object && "The IO context is not initialized"); 638 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X); 639 switch (Object->Header.Machine) { 640 case ELF::EM_X86_64: 641 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 642 break; 643 case ELF::EM_MIPS: 644 #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 645 break; 646 case ELF::EM_HEXAGON: 647 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 648 break; 649 case ELF::EM_386: 650 case ELF::EM_IAMCU: 651 #include "llvm/BinaryFormat/ELFRelocs/i386.def" 652 break; 653 case ELF::EM_AARCH64: 654 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 655 break; 656 case ELF::EM_ARM: 657 #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 658 break; 659 case ELF::EM_ARC: 660 #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 661 break; 662 case ELF::EM_RISCV: 663 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 664 break; 665 case ELF::EM_LANAI: 666 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 667 break; 668 case ELF::EM_AMDGPU: 669 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 670 break; 671 case ELF::EM_BPF: 672 #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 673 break; 674 default: 675 llvm_unreachable("Unsupported architecture"); 676 } 677 #undef ELF_RELOC 678 IO.enumFallback<Hex32>(Value); 679 } 680 681 void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration( 682 IO &IO, ELFYAML::ELF_DYNTAG &Value) { 683 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 684 assert(Object && "The IO context is not initialized"); 685 686 // Disable architecture specific tags by default. We might enable them below. 687 #define AARCH64_DYNAMIC_TAG(name, value) 688 #define MIPS_DYNAMIC_TAG(name, value) 689 #define HEXAGON_DYNAMIC_TAG(name, value) 690 #define PPC_DYNAMIC_TAG(name, value) 691 #define PPC64_DYNAMIC_TAG(name, value) 692 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 693 #define DYNAMIC_TAG_MARKER(name, value) 694 695 #define STRINGIFY(X) (#X) 696 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X); 697 switch (Object->Header.Machine) { 698 case ELF::EM_AARCH64: 699 #undef AARCH64_DYNAMIC_TAG 700 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 701 #include "llvm/BinaryFormat/DynamicTags.def" 702 #undef AARCH64_DYNAMIC_TAG 703 #define AARCH64_DYNAMIC_TAG(name, value) 704 break; 705 case ELF::EM_MIPS: 706 #undef MIPS_DYNAMIC_TAG 707 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 708 #include "llvm/BinaryFormat/DynamicTags.def" 709 #undef MIPS_DYNAMIC_TAG 710 #define MIPS_DYNAMIC_TAG(name, value) 711 break; 712 case ELF::EM_HEXAGON: 713 #undef HEXAGON_DYNAMIC_TAG 714 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 715 #include "llvm/BinaryFormat/DynamicTags.def" 716 #undef HEXAGON_DYNAMIC_TAG 717 #define HEXAGON_DYNAMIC_TAG(name, value) 718 break; 719 case ELF::EM_PPC: 720 #undef PPC_DYNAMIC_TAG 721 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 722 #include "llvm/BinaryFormat/DynamicTags.def" 723 #undef PPC_DYNAMIC_TAG 724 #define PPC_DYNAMIC_TAG(name, value) 725 break; 726 case ELF::EM_PPC64: 727 #undef PPC64_DYNAMIC_TAG 728 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 729 #include "llvm/BinaryFormat/DynamicTags.def" 730 #undef PPC64_DYNAMIC_TAG 731 #define PPC64_DYNAMIC_TAG(name, value) 732 break; 733 default: 734 #include "llvm/BinaryFormat/DynamicTags.def" 735 break; 736 } 737 #undef AARCH64_DYNAMIC_TAG 738 #undef MIPS_DYNAMIC_TAG 739 #undef HEXAGON_DYNAMIC_TAG 740 #undef PPC_DYNAMIC_TAG 741 #undef PPC64_DYNAMIC_TAG 742 #undef DYNAMIC_TAG_MARKER 743 #undef STRINGIFY 744 #undef DYNAMIC_TAG 745 746 IO.enumFallback<Hex64>(Value); 747 } 748 749 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration( 750 IO &IO, ELFYAML::MIPS_AFL_REG &Value) { 751 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) 752 ECase(REG_NONE); 753 ECase(REG_32); 754 ECase(REG_64); 755 ECase(REG_128); 756 #undef ECase 757 } 758 759 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration( 760 IO &IO, ELFYAML::MIPS_ABI_FP &Value) { 761 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X) 762 ECase(FP_ANY); 763 ECase(FP_DOUBLE); 764 ECase(FP_SINGLE); 765 ECase(FP_SOFT); 766 ECase(FP_OLD_64); 767 ECase(FP_XX); 768 ECase(FP_64); 769 ECase(FP_64A); 770 #undef ECase 771 } 772 773 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration( 774 IO &IO, ELFYAML::MIPS_AFL_EXT &Value) { 775 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) 776 ECase(EXT_NONE); 777 ECase(EXT_XLR); 778 ECase(EXT_OCTEON2); 779 ECase(EXT_OCTEONP); 780 ECase(EXT_LOONGSON_3A); 781 ECase(EXT_OCTEON); 782 ECase(EXT_5900); 783 ECase(EXT_4650); 784 ECase(EXT_4010); 785 ECase(EXT_4100); 786 ECase(EXT_3900); 787 ECase(EXT_10000); 788 ECase(EXT_SB1); 789 ECase(EXT_4111); 790 ECase(EXT_4120); 791 ECase(EXT_5400); 792 ECase(EXT_5500); 793 ECase(EXT_LOONGSON_2E); 794 ECase(EXT_LOONGSON_2F); 795 ECase(EXT_OCTEON3); 796 #undef ECase 797 } 798 799 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration( 800 IO &IO, ELFYAML::MIPS_ISA &Value) { 801 IO.enumCase(Value, "MIPS1", 1); 802 IO.enumCase(Value, "MIPS2", 2); 803 IO.enumCase(Value, "MIPS3", 3); 804 IO.enumCase(Value, "MIPS4", 4); 805 IO.enumCase(Value, "MIPS5", 5); 806 IO.enumCase(Value, "MIPS32", 32); 807 IO.enumCase(Value, "MIPS64", 64); 808 } 809 810 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset( 811 IO &IO, ELFYAML::MIPS_AFL_ASE &Value) { 812 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X) 813 BCase(DSP); 814 BCase(DSPR2); 815 BCase(EVA); 816 BCase(MCU); 817 BCase(MDMX); 818 BCase(MIPS3D); 819 BCase(MT); 820 BCase(SMARTMIPS); 821 BCase(VIRT); 822 BCase(MSA); 823 BCase(MIPS16); 824 BCase(MICROMIPS); 825 BCase(XPA); 826 #undef BCase 827 } 828 829 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset( 830 IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) { 831 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X) 832 BCase(ODDSPREG); 833 #undef BCase 834 } 835 836 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO, 837 ELFYAML::FileHeader &FileHdr) { 838 IO.mapRequired("Class", FileHdr.Class); 839 IO.mapRequired("Data", FileHdr.Data); 840 IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); 841 IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); 842 IO.mapRequired("Type", FileHdr.Type); 843 IO.mapRequired("Machine", FileHdr.Machine); 844 IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); 845 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0)); 846 847 IO.mapOptional("SHEntSize", FileHdr.SHEntSize); 848 IO.mapOptional("SHOffset", FileHdr.SHOffset); 849 IO.mapOptional("SHNum", FileHdr.SHNum); 850 IO.mapOptional("SHStrNdx", FileHdr.SHStrNdx); 851 } 852 853 void MappingTraits<ELFYAML::ProgramHeader>::mapping( 854 IO &IO, ELFYAML::ProgramHeader &Phdr) { 855 IO.mapRequired("Type", Phdr.Type); 856 IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0)); 857 IO.mapOptional("Sections", Phdr.Sections); 858 IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0)); 859 IO.mapOptional("PAddr", Phdr.PAddr, Hex64(0)); 860 IO.mapOptional("Align", Phdr.Align); 861 IO.mapOptional("FileSize", Phdr.FileSize); 862 IO.mapOptional("MemSize", Phdr.MemSize); 863 IO.mapOptional("Offset", Phdr.Offset); 864 } 865 866 namespace { 867 868 struct NormalizedOther { 869 NormalizedOther(IO &) 870 : Visibility(ELFYAML::ELF_STV(0)), Other(ELFYAML::ELF_STO(0)) {} 871 NormalizedOther(IO &, uint8_t Original) 872 : Visibility(Original & 0x3), Other(Original & ~0x3) {} 873 874 uint8_t denormalize(IO &) { return Visibility | Other; } 875 876 ELFYAML::ELF_STV Visibility; 877 ELFYAML::ELF_STO Other; 878 }; 879 880 } // end anonymous namespace 881 882 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) { 883 IO.mapOptional("Name", Symbol.Name, StringRef()); 884 IO.mapOptional("NameIndex", Symbol.NameIndex); 885 IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0)); 886 IO.mapOptional("Section", Symbol.Section, StringRef()); 887 IO.mapOptional("Index", Symbol.Index); 888 IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0)); 889 IO.mapOptional("Value", Symbol.Value, Hex64(0)); 890 IO.mapOptional("Size", Symbol.Size, Hex64(0)); 891 MappingNormalization<NormalizedOther, uint8_t> Keys(IO, Symbol.Other); 892 IO.mapOptional("Visibility", Keys->Visibility, ELFYAML::ELF_STV(0)); 893 IO.mapOptional("Other", Keys->Other, ELFYAML::ELF_STO(0)); 894 } 895 896 StringRef MappingTraits<ELFYAML::Symbol>::validate(IO &IO, 897 ELFYAML::Symbol &Symbol) { 898 if (Symbol.Index && Symbol.Section.data()) 899 return "Index and Section cannot both be specified for Symbol"; 900 if (Symbol.Index && *Symbol.Index == ELFYAML::ELF_SHN(ELF::SHN_XINDEX)) 901 return "Large indexes are not supported"; 902 if (Symbol.NameIndex && !Symbol.Name.empty()) 903 return "Name and NameIndex cannot both be specified for Symbol"; 904 return StringRef(); 905 } 906 907 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) { 908 IO.mapOptional("Name", Section.Name, StringRef()); 909 IO.mapRequired("Type", Section.Type); 910 IO.mapOptional("Flags", Section.Flags); 911 IO.mapOptional("Address", Section.Address, Hex64(0)); 912 IO.mapOptional("Link", Section.Link, StringRef()); 913 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0)); 914 IO.mapOptional("EntSize", Section.EntSize); 915 916 // obj2yaml does not dump these fields. They are expected to be empty when we 917 // are producing YAML, because yaml2obj sets appropriate values for sh_offset 918 // and sh_size automatically when they are not explicitly defined. 919 assert(!IO.outputting() || 920 (!Section.ShOffset.hasValue() && !Section.ShSize.hasValue())); 921 IO.mapOptional("ShOffset", Section.ShOffset); 922 IO.mapOptional("ShSize", Section.ShSize); 923 } 924 925 static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) { 926 commonSectionMapping(IO, Section); 927 IO.mapOptional("Entries", Section.Entries); 928 IO.mapOptional("Content", Section.Content); 929 } 930 931 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { 932 commonSectionMapping(IO, Section); 933 IO.mapOptional("Content", Section.Content); 934 IO.mapOptional("Size", Section.Size); 935 IO.mapOptional("Info", Section.Info); 936 } 937 938 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) { 939 commonSectionMapping(IO, Section); 940 IO.mapOptional("Size", Section.Size, Hex64(0)); 941 } 942 943 static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) { 944 commonSectionMapping(IO, Section); 945 IO.mapRequired("Info", Section.Info); 946 IO.mapRequired("Entries", Section.Entries); 947 } 948 949 static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) { 950 commonSectionMapping(IO, Section); 951 IO.mapRequired("Entries", Section.Entries); 952 } 953 954 static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) { 955 commonSectionMapping(IO, Section); 956 IO.mapRequired("Info", Section.Info); 957 IO.mapRequired("Dependencies", Section.VerneedV); 958 } 959 960 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { 961 commonSectionMapping(IO, Section); 962 IO.mapOptional("Info", Section.RelocatableSec, StringRef()); 963 IO.mapOptional("Relocations", Section.Relocations); 964 } 965 966 static void groupSectionMapping(IO &IO, ELFYAML::Group &Group) { 967 commonSectionMapping(IO, Group); 968 IO.mapOptional("Info", Group.Signature, StringRef()); 969 IO.mapRequired("Members", Group.Members); 970 } 971 972 void MappingTraits<ELFYAML::SectionOrType>::mapping( 973 IO &IO, ELFYAML::SectionOrType §ionOrType) { 974 IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType); 975 } 976 977 void MappingTraits<ELFYAML::SectionName>::mapping( 978 IO &IO, ELFYAML::SectionName §ionName) { 979 IO.mapRequired("Section", sectionName.Section); 980 } 981 982 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) { 983 commonSectionMapping(IO, Section); 984 IO.mapOptional("Version", Section.Version, Hex16(0)); 985 IO.mapRequired("ISA", Section.ISALevel); 986 IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0)); 987 IO.mapOptional("ISAExtension", Section.ISAExtension, 988 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE)); 989 IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0)); 990 IO.mapOptional("FpABI", Section.FpABI, 991 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY)); 992 IO.mapOptional("GPRSize", Section.GPRSize, 993 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 994 IO.mapOptional("CPR1Size", Section.CPR1Size, 995 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 996 IO.mapOptional("CPR2Size", Section.CPR2Size, 997 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 998 IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0)); 999 IO.mapOptional("Flags2", Section.Flags2, Hex32(0)); 1000 } 1001 1002 void MappingTraits<std::unique_ptr<ELFYAML::Section>>::mapping( 1003 IO &IO, std::unique_ptr<ELFYAML::Section> &Section) { 1004 ELFYAML::ELF_SHT sectionType; 1005 if (IO.outputting()) 1006 sectionType = Section->Type; 1007 else 1008 IO.mapRequired("Type", sectionType); 1009 1010 switch (sectionType) { 1011 case ELF::SHT_DYNAMIC: 1012 if (!IO.outputting()) 1013 Section.reset(new ELFYAML::DynamicSection()); 1014 sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get())); 1015 break; 1016 case ELF::SHT_REL: 1017 case ELF::SHT_RELA: 1018 if (!IO.outputting()) 1019 Section.reset(new ELFYAML::RelocationSection()); 1020 sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get())); 1021 break; 1022 case ELF::SHT_GROUP: 1023 if (!IO.outputting()) 1024 Section.reset(new ELFYAML::Group()); 1025 groupSectionMapping(IO, *cast<ELFYAML::Group>(Section.get())); 1026 break; 1027 case ELF::SHT_NOBITS: 1028 if (!IO.outputting()) 1029 Section.reset(new ELFYAML::NoBitsSection()); 1030 sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get())); 1031 break; 1032 case ELF::SHT_MIPS_ABIFLAGS: 1033 if (!IO.outputting()) 1034 Section.reset(new ELFYAML::MipsABIFlags()); 1035 sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get())); 1036 break; 1037 case ELF::SHT_GNU_verdef: 1038 if (!IO.outputting()) 1039 Section.reset(new ELFYAML::VerdefSection()); 1040 sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get())); 1041 break; 1042 case ELF::SHT_GNU_versym: 1043 if (!IO.outputting()) 1044 Section.reset(new ELFYAML::SymverSection()); 1045 sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get())); 1046 break; 1047 case ELF::SHT_GNU_verneed: 1048 if (!IO.outputting()) 1049 Section.reset(new ELFYAML::VerneedSection()); 1050 sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get())); 1051 break; 1052 default: 1053 if (!IO.outputting()) 1054 Section.reset(new ELFYAML::RawContentSection()); 1055 sectionMapping(IO, *cast<ELFYAML::RawContentSection>(Section.get())); 1056 } 1057 } 1058 1059 StringRef MappingTraits<std::unique_ptr<ELFYAML::Section>>::validate( 1060 IO &io, std::unique_ptr<ELFYAML::Section> &Section) { 1061 const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(Section.get()); 1062 if (!RawSection) 1063 return {}; 1064 if (RawSection->Size && RawSection->Content && 1065 (uint64_t)(*RawSection->Size) < RawSection->Content->binary_size()) 1066 return "Section size must be greater than or equal to the content size"; 1067 return {}; 1068 } 1069 1070 namespace { 1071 1072 struct NormalizedMips64RelType { 1073 NormalizedMips64RelType(IO &) 1074 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1075 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1076 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1077 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {} 1078 NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original) 1079 : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF), 1080 Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {} 1081 1082 ELFYAML::ELF_REL denormalize(IO &) { 1083 ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24; 1084 return Res; 1085 } 1086 1087 ELFYAML::ELF_REL Type; 1088 ELFYAML::ELF_REL Type2; 1089 ELFYAML::ELF_REL Type3; 1090 ELFYAML::ELF_RSS SpecSym; 1091 }; 1092 1093 } // end anonymous namespace 1094 1095 void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO, 1096 ELFYAML::DynamicEntry &Rel) { 1097 assert(IO.getContext() && "The IO context is not initialized"); 1098 1099 IO.mapRequired("Tag", Rel.Tag); 1100 IO.mapRequired("Value", Rel.Val); 1101 } 1102 1103 void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO, 1104 ELFYAML::VerdefEntry &E) { 1105 assert(IO.getContext() && "The IO context is not initialized"); 1106 1107 IO.mapRequired("Version", E.Version); 1108 IO.mapRequired("Flags", E.Flags); 1109 IO.mapRequired("VersionNdx", E.VersionNdx); 1110 IO.mapRequired("Hash", E.Hash); 1111 IO.mapRequired("Names", E.VerNames); 1112 } 1113 1114 void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO, 1115 ELFYAML::VerneedEntry &E) { 1116 assert(IO.getContext() && "The IO context is not initialized"); 1117 1118 IO.mapRequired("Version", E.Version); 1119 IO.mapRequired("File", E.File); 1120 IO.mapRequired("Entries", E.AuxV); 1121 } 1122 1123 void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO, 1124 ELFYAML::VernauxEntry &E) { 1125 assert(IO.getContext() && "The IO context is not initialized"); 1126 1127 IO.mapRequired("Name", E.Name); 1128 IO.mapRequired("Hash", E.Hash); 1129 IO.mapRequired("Flags", E.Flags); 1130 IO.mapRequired("Other", E.Other); 1131 } 1132 1133 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO, 1134 ELFYAML::Relocation &Rel) { 1135 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 1136 assert(Object && "The IO context is not initialized"); 1137 1138 IO.mapRequired("Offset", Rel.Offset); 1139 IO.mapOptional("Symbol", Rel.Symbol); 1140 1141 if (Object->Header.Machine == ELFYAML::ELF_EM(ELF::EM_MIPS) && 1142 Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { 1143 MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key( 1144 IO, Rel.Type); 1145 IO.mapRequired("Type", Key->Type); 1146 IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); 1147 IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); 1148 IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF)); 1149 } else 1150 IO.mapRequired("Type", Rel.Type); 1151 1152 IO.mapOptional("Addend", Rel.Addend, (int64_t)0); 1153 } 1154 1155 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) { 1156 assert(!IO.getContext() && "The IO context is initialized already"); 1157 IO.setContext(&Object); 1158 IO.mapTag("!ELF", true); 1159 IO.mapRequired("FileHeader", Object.Header); 1160 IO.mapOptional("ProgramHeaders", Object.ProgramHeaders); 1161 IO.mapOptional("Sections", Object.Sections); 1162 IO.mapOptional("Symbols", Object.Symbols); 1163 IO.mapOptional("DynamicSymbols", Object.DynamicSymbols); 1164 IO.setContext(nullptr); 1165 } 1166 1167 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG) 1168 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP) 1169 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT) 1170 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE) 1171 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1) 1172 1173 } // end namespace yaml 1174 1175 } // end namespace llvm 1176