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/MapVector.h" 15 #include "llvm/ADT/StringRef.h" 16 #include "llvm/BinaryFormat/ELF.h" 17 #include "llvm/Support/ARMEHABI.h" 18 #include "llvm/Support/Casting.h" 19 #include "llvm/Support/ErrorHandling.h" 20 #include "llvm/Support/MipsABIFlags.h" 21 #include "llvm/Support/YAMLTraits.h" 22 #include "llvm/Support/WithColor.h" 23 #include <cassert> 24 #include <cstdint> 25 26 namespace llvm { 27 28 ELFYAML::Chunk::~Chunk() = default; 29 30 namespace ELFYAML { 31 unsigned Object::getMachine() const { 32 if (Header.Machine) 33 return *Header.Machine; 34 return llvm::ELF::EM_NONE; 35 } 36 37 constexpr StringRef SectionHeaderTable::TypeStr; 38 } // namespace ELFYAML 39 40 namespace yaml { 41 42 void ScalarEnumerationTraits<ELFYAML::ELF_ET>::enumeration( 43 IO &IO, ELFYAML::ELF_ET &Value) { 44 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 45 ECase(ET_NONE); 46 ECase(ET_REL); 47 ECase(ET_EXEC); 48 ECase(ET_DYN); 49 ECase(ET_CORE); 50 #undef ECase 51 IO.enumFallback<Hex16>(Value); 52 } 53 54 void ScalarEnumerationTraits<ELFYAML::ELF_PT>::enumeration( 55 IO &IO, ELFYAML::ELF_PT &Value) { 56 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 57 ECase(PT_NULL); 58 ECase(PT_LOAD); 59 ECase(PT_DYNAMIC); 60 ECase(PT_INTERP); 61 ECase(PT_NOTE); 62 ECase(PT_SHLIB); 63 ECase(PT_PHDR); 64 ECase(PT_TLS); 65 ECase(PT_GNU_EH_FRAME); 66 ECase(PT_GNU_STACK); 67 ECase(PT_GNU_RELRO); 68 ECase(PT_GNU_PROPERTY); 69 #undef ECase 70 IO.enumFallback<Hex32>(Value); 71 } 72 73 void ScalarEnumerationTraits<ELFYAML::ELF_EM>::enumeration( 74 IO &IO, ELFYAML::ELF_EM &Value) { 75 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 76 ECase(EM_NONE); 77 ECase(EM_M32); 78 ECase(EM_SPARC); 79 ECase(EM_386); 80 ECase(EM_68K); 81 ECase(EM_88K); 82 ECase(EM_IAMCU); 83 ECase(EM_860); 84 ECase(EM_MIPS); 85 ECase(EM_S370); 86 ECase(EM_MIPS_RS3_LE); 87 ECase(EM_PARISC); 88 ECase(EM_VPP500); 89 ECase(EM_SPARC32PLUS); 90 ECase(EM_960); 91 ECase(EM_PPC); 92 ECase(EM_PPC64); 93 ECase(EM_S390); 94 ECase(EM_SPU); 95 ECase(EM_V800); 96 ECase(EM_FR20); 97 ECase(EM_RH32); 98 ECase(EM_RCE); 99 ECase(EM_ARM); 100 ECase(EM_ALPHA); 101 ECase(EM_SH); 102 ECase(EM_SPARCV9); 103 ECase(EM_TRICORE); 104 ECase(EM_ARC); 105 ECase(EM_H8_300); 106 ECase(EM_H8_300H); 107 ECase(EM_H8S); 108 ECase(EM_H8_500); 109 ECase(EM_IA_64); 110 ECase(EM_MIPS_X); 111 ECase(EM_COLDFIRE); 112 ECase(EM_68HC12); 113 ECase(EM_MMA); 114 ECase(EM_PCP); 115 ECase(EM_NCPU); 116 ECase(EM_NDR1); 117 ECase(EM_STARCORE); 118 ECase(EM_ME16); 119 ECase(EM_ST100); 120 ECase(EM_TINYJ); 121 ECase(EM_X86_64); 122 ECase(EM_PDSP); 123 ECase(EM_PDP10); 124 ECase(EM_PDP11); 125 ECase(EM_FX66); 126 ECase(EM_ST9PLUS); 127 ECase(EM_ST7); 128 ECase(EM_68HC16); 129 ECase(EM_68HC11); 130 ECase(EM_68HC08); 131 ECase(EM_68HC05); 132 ECase(EM_SVX); 133 ECase(EM_ST19); 134 ECase(EM_VAX); 135 ECase(EM_CRIS); 136 ECase(EM_JAVELIN); 137 ECase(EM_FIREPATH); 138 ECase(EM_ZSP); 139 ECase(EM_MMIX); 140 ECase(EM_HUANY); 141 ECase(EM_PRISM); 142 ECase(EM_AVR); 143 ECase(EM_FR30); 144 ECase(EM_D10V); 145 ECase(EM_D30V); 146 ECase(EM_V850); 147 ECase(EM_M32R); 148 ECase(EM_MN10300); 149 ECase(EM_MN10200); 150 ECase(EM_PJ); 151 ECase(EM_OPENRISC); 152 ECase(EM_ARC_COMPACT); 153 ECase(EM_XTENSA); 154 ECase(EM_VIDEOCORE); 155 ECase(EM_TMM_GPP); 156 ECase(EM_NS32K); 157 ECase(EM_TPC); 158 ECase(EM_SNP1K); 159 ECase(EM_ST200); 160 ECase(EM_IP2K); 161 ECase(EM_MAX); 162 ECase(EM_CR); 163 ECase(EM_F2MC16); 164 ECase(EM_MSP430); 165 ECase(EM_BLACKFIN); 166 ECase(EM_SE_C33); 167 ECase(EM_SEP); 168 ECase(EM_ARCA); 169 ECase(EM_UNICORE); 170 ECase(EM_EXCESS); 171 ECase(EM_DXP); 172 ECase(EM_ALTERA_NIOS2); 173 ECase(EM_CRX); 174 ECase(EM_XGATE); 175 ECase(EM_C166); 176 ECase(EM_M16C); 177 ECase(EM_DSPIC30F); 178 ECase(EM_CE); 179 ECase(EM_M32C); 180 ECase(EM_TSK3000); 181 ECase(EM_RS08); 182 ECase(EM_SHARC); 183 ECase(EM_ECOG2); 184 ECase(EM_SCORE7); 185 ECase(EM_DSP24); 186 ECase(EM_VIDEOCORE3); 187 ECase(EM_LATTICEMICO32); 188 ECase(EM_SE_C17); 189 ECase(EM_TI_C6000); 190 ECase(EM_TI_C2000); 191 ECase(EM_TI_C5500); 192 ECase(EM_MMDSP_PLUS); 193 ECase(EM_CYPRESS_M8C); 194 ECase(EM_R32C); 195 ECase(EM_TRIMEDIA); 196 ECase(EM_HEXAGON); 197 ECase(EM_8051); 198 ECase(EM_STXP7X); 199 ECase(EM_NDS32); 200 ECase(EM_ECOG1); 201 ECase(EM_ECOG1X); 202 ECase(EM_MAXQ30); 203 ECase(EM_XIMO16); 204 ECase(EM_MANIK); 205 ECase(EM_CRAYNV2); 206 ECase(EM_RX); 207 ECase(EM_METAG); 208 ECase(EM_MCST_ELBRUS); 209 ECase(EM_ECOG16); 210 ECase(EM_CR16); 211 ECase(EM_ETPU); 212 ECase(EM_SLE9X); 213 ECase(EM_L10M); 214 ECase(EM_K10M); 215 ECase(EM_AARCH64); 216 ECase(EM_AVR32); 217 ECase(EM_STM8); 218 ECase(EM_TILE64); 219 ECase(EM_TILEPRO); 220 ECase(EM_CUDA); 221 ECase(EM_TILEGX); 222 ECase(EM_CLOUDSHIELD); 223 ECase(EM_COREA_1ST); 224 ECase(EM_COREA_2ND); 225 ECase(EM_ARC_COMPACT2); 226 ECase(EM_OPEN8); 227 ECase(EM_RL78); 228 ECase(EM_VIDEOCORE5); 229 ECase(EM_78KOR); 230 ECase(EM_56800EX); 231 ECase(EM_AMDGPU); 232 ECase(EM_RISCV); 233 ECase(EM_LANAI); 234 ECase(EM_BPF); 235 ECase(EM_VE); 236 ECase(EM_CSKY); 237 #undef ECase 238 IO.enumFallback<Hex16>(Value); 239 } 240 241 void ScalarEnumerationTraits<ELFYAML::ELF_ELFCLASS>::enumeration( 242 IO &IO, ELFYAML::ELF_ELFCLASS &Value) { 243 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 244 // Since the semantics of ELFCLASSNONE is "invalid", just don't accept it 245 // here. 246 ECase(ELFCLASS32); 247 ECase(ELFCLASS64); 248 #undef ECase 249 } 250 251 void ScalarEnumerationTraits<ELFYAML::ELF_ELFDATA>::enumeration( 252 IO &IO, ELFYAML::ELF_ELFDATA &Value) { 253 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 254 // ELFDATANONE is an invalid data encoding, but we accept it because 255 // we want to be able to produce invalid binaries for the tests. 256 ECase(ELFDATANONE); 257 ECase(ELFDATA2LSB); 258 ECase(ELFDATA2MSB); 259 #undef ECase 260 } 261 262 void ScalarEnumerationTraits<ELFYAML::ELF_ELFOSABI>::enumeration( 263 IO &IO, ELFYAML::ELF_ELFOSABI &Value) { 264 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 265 ECase(ELFOSABI_NONE); 266 ECase(ELFOSABI_HPUX); 267 ECase(ELFOSABI_NETBSD); 268 ECase(ELFOSABI_GNU); 269 ECase(ELFOSABI_LINUX); 270 ECase(ELFOSABI_HURD); 271 ECase(ELFOSABI_SOLARIS); 272 ECase(ELFOSABI_AIX); 273 ECase(ELFOSABI_IRIX); 274 ECase(ELFOSABI_FREEBSD); 275 ECase(ELFOSABI_TRU64); 276 ECase(ELFOSABI_MODESTO); 277 ECase(ELFOSABI_OPENBSD); 278 ECase(ELFOSABI_OPENVMS); 279 ECase(ELFOSABI_NSK); 280 ECase(ELFOSABI_AROS); 281 ECase(ELFOSABI_FENIXOS); 282 ECase(ELFOSABI_CLOUDABI); 283 ECase(ELFOSABI_AMDGPU_HSA); 284 ECase(ELFOSABI_AMDGPU_PAL); 285 ECase(ELFOSABI_AMDGPU_MESA3D); 286 ECase(ELFOSABI_ARM); 287 ECase(ELFOSABI_C6000_ELFABI); 288 ECase(ELFOSABI_C6000_LINUX); 289 ECase(ELFOSABI_STANDALONE); 290 #undef ECase 291 IO.enumFallback<Hex8>(Value); 292 } 293 294 void ScalarBitSetTraits<ELFYAML::ELF_EF>::bitset(IO &IO, 295 ELFYAML::ELF_EF &Value) { 296 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 297 assert(Object && "The IO context is not initialized"); 298 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 299 #define BCaseMask(X, M) IO.maskedBitSetCase(Value, #X, ELF::X, ELF::M) 300 switch (Object->getMachine()) { 301 case ELF::EM_ARM: 302 BCase(EF_ARM_SOFT_FLOAT); 303 BCase(EF_ARM_VFP_FLOAT); 304 BCaseMask(EF_ARM_EABI_UNKNOWN, EF_ARM_EABIMASK); 305 BCaseMask(EF_ARM_EABI_VER1, EF_ARM_EABIMASK); 306 BCaseMask(EF_ARM_EABI_VER2, EF_ARM_EABIMASK); 307 BCaseMask(EF_ARM_EABI_VER3, EF_ARM_EABIMASK); 308 BCaseMask(EF_ARM_EABI_VER4, EF_ARM_EABIMASK); 309 BCaseMask(EF_ARM_EABI_VER5, EF_ARM_EABIMASK); 310 break; 311 case ELF::EM_MIPS: 312 BCase(EF_MIPS_NOREORDER); 313 BCase(EF_MIPS_PIC); 314 BCase(EF_MIPS_CPIC); 315 BCase(EF_MIPS_ABI2); 316 BCase(EF_MIPS_32BITMODE); 317 BCase(EF_MIPS_FP64); 318 BCase(EF_MIPS_NAN2008); 319 BCase(EF_MIPS_MICROMIPS); 320 BCase(EF_MIPS_ARCH_ASE_M16); 321 BCase(EF_MIPS_ARCH_ASE_MDMX); 322 BCaseMask(EF_MIPS_ABI_O32, EF_MIPS_ABI); 323 BCaseMask(EF_MIPS_ABI_O64, EF_MIPS_ABI); 324 BCaseMask(EF_MIPS_ABI_EABI32, EF_MIPS_ABI); 325 BCaseMask(EF_MIPS_ABI_EABI64, EF_MIPS_ABI); 326 BCaseMask(EF_MIPS_MACH_3900, EF_MIPS_MACH); 327 BCaseMask(EF_MIPS_MACH_4010, EF_MIPS_MACH); 328 BCaseMask(EF_MIPS_MACH_4100, EF_MIPS_MACH); 329 BCaseMask(EF_MIPS_MACH_4650, EF_MIPS_MACH); 330 BCaseMask(EF_MIPS_MACH_4120, EF_MIPS_MACH); 331 BCaseMask(EF_MIPS_MACH_4111, EF_MIPS_MACH); 332 BCaseMask(EF_MIPS_MACH_SB1, EF_MIPS_MACH); 333 BCaseMask(EF_MIPS_MACH_OCTEON, EF_MIPS_MACH); 334 BCaseMask(EF_MIPS_MACH_XLR, EF_MIPS_MACH); 335 BCaseMask(EF_MIPS_MACH_OCTEON2, EF_MIPS_MACH); 336 BCaseMask(EF_MIPS_MACH_OCTEON3, EF_MIPS_MACH); 337 BCaseMask(EF_MIPS_MACH_5400, EF_MIPS_MACH); 338 BCaseMask(EF_MIPS_MACH_5900, EF_MIPS_MACH); 339 BCaseMask(EF_MIPS_MACH_5500, EF_MIPS_MACH); 340 BCaseMask(EF_MIPS_MACH_9000, EF_MIPS_MACH); 341 BCaseMask(EF_MIPS_MACH_LS2E, EF_MIPS_MACH); 342 BCaseMask(EF_MIPS_MACH_LS2F, EF_MIPS_MACH); 343 BCaseMask(EF_MIPS_MACH_LS3A, EF_MIPS_MACH); 344 BCaseMask(EF_MIPS_ARCH_1, EF_MIPS_ARCH); 345 BCaseMask(EF_MIPS_ARCH_2, EF_MIPS_ARCH); 346 BCaseMask(EF_MIPS_ARCH_3, EF_MIPS_ARCH); 347 BCaseMask(EF_MIPS_ARCH_4, EF_MIPS_ARCH); 348 BCaseMask(EF_MIPS_ARCH_5, EF_MIPS_ARCH); 349 BCaseMask(EF_MIPS_ARCH_32, EF_MIPS_ARCH); 350 BCaseMask(EF_MIPS_ARCH_64, EF_MIPS_ARCH); 351 BCaseMask(EF_MIPS_ARCH_32R2, EF_MIPS_ARCH); 352 BCaseMask(EF_MIPS_ARCH_64R2, EF_MIPS_ARCH); 353 BCaseMask(EF_MIPS_ARCH_32R6, EF_MIPS_ARCH); 354 BCaseMask(EF_MIPS_ARCH_64R6, EF_MIPS_ARCH); 355 break; 356 case ELF::EM_HEXAGON: 357 BCase(EF_HEXAGON_MACH_V2); 358 BCase(EF_HEXAGON_MACH_V3); 359 BCase(EF_HEXAGON_MACH_V4); 360 BCase(EF_HEXAGON_MACH_V5); 361 BCase(EF_HEXAGON_MACH_V55); 362 BCase(EF_HEXAGON_MACH_V60); 363 BCase(EF_HEXAGON_MACH_V62); 364 BCase(EF_HEXAGON_MACH_V65); 365 BCase(EF_HEXAGON_MACH_V66); 366 BCase(EF_HEXAGON_MACH_V67); 367 BCase(EF_HEXAGON_MACH_V67T); 368 BCase(EF_HEXAGON_ISA_V2); 369 BCase(EF_HEXAGON_ISA_V3); 370 BCase(EF_HEXAGON_ISA_V4); 371 BCase(EF_HEXAGON_ISA_V5); 372 BCase(EF_HEXAGON_ISA_V55); 373 BCase(EF_HEXAGON_ISA_V60); 374 BCase(EF_HEXAGON_ISA_V62); 375 BCase(EF_HEXAGON_ISA_V65); 376 BCase(EF_HEXAGON_ISA_V66); 377 BCase(EF_HEXAGON_ISA_V67); 378 break; 379 case ELF::EM_AVR: 380 BCase(EF_AVR_ARCH_AVR1); 381 BCase(EF_AVR_ARCH_AVR2); 382 BCase(EF_AVR_ARCH_AVR25); 383 BCase(EF_AVR_ARCH_AVR3); 384 BCase(EF_AVR_ARCH_AVR31); 385 BCase(EF_AVR_ARCH_AVR35); 386 BCase(EF_AVR_ARCH_AVR4); 387 BCase(EF_AVR_ARCH_AVR51); 388 BCase(EF_AVR_ARCH_AVR6); 389 BCase(EF_AVR_ARCH_AVRTINY); 390 BCase(EF_AVR_ARCH_XMEGA1); 391 BCase(EF_AVR_ARCH_XMEGA2); 392 BCase(EF_AVR_ARCH_XMEGA3); 393 BCase(EF_AVR_ARCH_XMEGA4); 394 BCase(EF_AVR_ARCH_XMEGA5); 395 BCase(EF_AVR_ARCH_XMEGA6); 396 BCase(EF_AVR_ARCH_XMEGA7); 397 break; 398 case ELF::EM_RISCV: 399 BCase(EF_RISCV_RVC); 400 BCaseMask(EF_RISCV_FLOAT_ABI_SOFT, EF_RISCV_FLOAT_ABI); 401 BCaseMask(EF_RISCV_FLOAT_ABI_SINGLE, EF_RISCV_FLOAT_ABI); 402 BCaseMask(EF_RISCV_FLOAT_ABI_DOUBLE, EF_RISCV_FLOAT_ABI); 403 BCaseMask(EF_RISCV_FLOAT_ABI_QUAD, EF_RISCV_FLOAT_ABI); 404 BCase(EF_RISCV_RVE); 405 break; 406 case ELF::EM_AMDGPU: 407 BCaseMask(EF_AMDGPU_MACH_NONE, EF_AMDGPU_MACH); 408 BCaseMask(EF_AMDGPU_MACH_R600_R600, EF_AMDGPU_MACH); 409 BCaseMask(EF_AMDGPU_MACH_R600_R630, EF_AMDGPU_MACH); 410 BCaseMask(EF_AMDGPU_MACH_R600_RS880, EF_AMDGPU_MACH); 411 BCaseMask(EF_AMDGPU_MACH_R600_RV670, EF_AMDGPU_MACH); 412 BCaseMask(EF_AMDGPU_MACH_R600_RV710, EF_AMDGPU_MACH); 413 BCaseMask(EF_AMDGPU_MACH_R600_RV730, EF_AMDGPU_MACH); 414 BCaseMask(EF_AMDGPU_MACH_R600_RV770, EF_AMDGPU_MACH); 415 BCaseMask(EF_AMDGPU_MACH_R600_CEDAR, EF_AMDGPU_MACH); 416 BCaseMask(EF_AMDGPU_MACH_R600_CYPRESS, EF_AMDGPU_MACH); 417 BCaseMask(EF_AMDGPU_MACH_R600_JUNIPER, EF_AMDGPU_MACH); 418 BCaseMask(EF_AMDGPU_MACH_R600_REDWOOD, EF_AMDGPU_MACH); 419 BCaseMask(EF_AMDGPU_MACH_R600_SUMO, EF_AMDGPU_MACH); 420 BCaseMask(EF_AMDGPU_MACH_R600_BARTS, EF_AMDGPU_MACH); 421 BCaseMask(EF_AMDGPU_MACH_R600_CAICOS, EF_AMDGPU_MACH); 422 BCaseMask(EF_AMDGPU_MACH_R600_CAYMAN, EF_AMDGPU_MACH); 423 BCaseMask(EF_AMDGPU_MACH_R600_TURKS, EF_AMDGPU_MACH); 424 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX600, EF_AMDGPU_MACH); 425 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX601, EF_AMDGPU_MACH); 426 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX602, EF_AMDGPU_MACH); 427 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX700, EF_AMDGPU_MACH); 428 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX701, EF_AMDGPU_MACH); 429 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX702, EF_AMDGPU_MACH); 430 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX703, EF_AMDGPU_MACH); 431 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX704, EF_AMDGPU_MACH); 432 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX705, EF_AMDGPU_MACH); 433 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX801, EF_AMDGPU_MACH); 434 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX802, EF_AMDGPU_MACH); 435 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX803, EF_AMDGPU_MACH); 436 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX805, EF_AMDGPU_MACH); 437 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX810, EF_AMDGPU_MACH); 438 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX900, EF_AMDGPU_MACH); 439 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX902, EF_AMDGPU_MACH); 440 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX904, EF_AMDGPU_MACH); 441 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX906, EF_AMDGPU_MACH); 442 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX908, EF_AMDGPU_MACH); 443 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX909, EF_AMDGPU_MACH); 444 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX90C, EF_AMDGPU_MACH); 445 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1010, EF_AMDGPU_MACH); 446 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1011, EF_AMDGPU_MACH); 447 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1012, EF_AMDGPU_MACH); 448 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1030, EF_AMDGPU_MACH); 449 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1031, EF_AMDGPU_MACH); 450 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1032, EF_AMDGPU_MACH); 451 BCaseMask(EF_AMDGPU_MACH_AMDGCN_GFX1033, EF_AMDGPU_MACH); 452 BCase(EF_AMDGPU_XNACK); 453 BCase(EF_AMDGPU_SRAM_ECC); 454 break; 455 default: 456 break; 457 } 458 #undef BCase 459 #undef BCaseMask 460 } 461 462 void ScalarEnumerationTraits<ELFYAML::ELF_SHT>::enumeration( 463 IO &IO, ELFYAML::ELF_SHT &Value) { 464 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 465 assert(Object && "The IO context is not initialized"); 466 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 467 ECase(SHT_NULL); 468 ECase(SHT_PROGBITS); 469 ECase(SHT_SYMTAB); 470 // FIXME: Issue a diagnostic with this information. 471 ECase(SHT_STRTAB); 472 ECase(SHT_RELA); 473 ECase(SHT_HASH); 474 ECase(SHT_DYNAMIC); 475 ECase(SHT_NOTE); 476 ECase(SHT_NOBITS); 477 ECase(SHT_REL); 478 ECase(SHT_SHLIB); 479 ECase(SHT_DYNSYM); 480 ECase(SHT_INIT_ARRAY); 481 ECase(SHT_FINI_ARRAY); 482 ECase(SHT_PREINIT_ARRAY); 483 ECase(SHT_GROUP); 484 ECase(SHT_SYMTAB_SHNDX); 485 ECase(SHT_RELR); 486 ECase(SHT_ANDROID_REL); 487 ECase(SHT_ANDROID_RELA); 488 ECase(SHT_ANDROID_RELR); 489 ECase(SHT_LLVM_ODRTAB); 490 ECase(SHT_LLVM_LINKER_OPTIONS); 491 ECase(SHT_LLVM_CALL_GRAPH_PROFILE); 492 ECase(SHT_LLVM_ADDRSIG); 493 ECase(SHT_LLVM_DEPENDENT_LIBRARIES); 494 ECase(SHT_LLVM_SYMPART); 495 ECase(SHT_LLVM_PART_EHDR); 496 ECase(SHT_LLVM_PART_PHDR); 497 ECase(SHT_LLVM_BB_ADDR_MAP); 498 ECase(SHT_GNU_ATTRIBUTES); 499 ECase(SHT_GNU_HASH); 500 ECase(SHT_GNU_verdef); 501 ECase(SHT_GNU_verneed); 502 ECase(SHT_GNU_versym); 503 switch (Object->getMachine()) { 504 case ELF::EM_ARM: 505 ECase(SHT_ARM_EXIDX); 506 ECase(SHT_ARM_PREEMPTMAP); 507 ECase(SHT_ARM_ATTRIBUTES); 508 ECase(SHT_ARM_DEBUGOVERLAY); 509 ECase(SHT_ARM_OVERLAYSECTION); 510 break; 511 case ELF::EM_HEXAGON: 512 ECase(SHT_HEX_ORDERED); 513 break; 514 case ELF::EM_X86_64: 515 ECase(SHT_X86_64_UNWIND); 516 break; 517 case ELF::EM_MIPS: 518 ECase(SHT_MIPS_REGINFO); 519 ECase(SHT_MIPS_OPTIONS); 520 ECase(SHT_MIPS_DWARF); 521 ECase(SHT_MIPS_ABIFLAGS); 522 break; 523 case ELF::EM_RISCV: 524 ECase(SHT_RISCV_ATTRIBUTES); 525 break; 526 default: 527 // Nothing to do. 528 break; 529 } 530 #undef ECase 531 IO.enumFallback<Hex32>(Value); 532 } 533 534 void ScalarBitSetTraits<ELFYAML::ELF_PF>::bitset(IO &IO, 535 ELFYAML::ELF_PF &Value) { 536 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 537 BCase(PF_X); 538 BCase(PF_W); 539 BCase(PF_R); 540 } 541 542 void ScalarBitSetTraits<ELFYAML::ELF_SHF>::bitset(IO &IO, 543 ELFYAML::ELF_SHF &Value) { 544 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 545 #define BCase(X) IO.bitSetCase(Value, #X, ELF::X) 546 BCase(SHF_WRITE); 547 BCase(SHF_ALLOC); 548 BCase(SHF_EXCLUDE); 549 BCase(SHF_EXECINSTR); 550 BCase(SHF_MERGE); 551 BCase(SHF_STRINGS); 552 BCase(SHF_INFO_LINK); 553 BCase(SHF_LINK_ORDER); 554 BCase(SHF_OS_NONCONFORMING); 555 BCase(SHF_GROUP); 556 BCase(SHF_TLS); 557 BCase(SHF_COMPRESSED); 558 switch (Object->getMachine()) { 559 case ELF::EM_ARM: 560 BCase(SHF_ARM_PURECODE); 561 break; 562 case ELF::EM_HEXAGON: 563 BCase(SHF_HEX_GPREL); 564 break; 565 case ELF::EM_MIPS: 566 BCase(SHF_MIPS_NODUPES); 567 BCase(SHF_MIPS_NAMES); 568 BCase(SHF_MIPS_LOCAL); 569 BCase(SHF_MIPS_NOSTRIP); 570 BCase(SHF_MIPS_GPREL); 571 BCase(SHF_MIPS_MERGE); 572 BCase(SHF_MIPS_ADDR); 573 BCase(SHF_MIPS_STRING); 574 break; 575 case ELF::EM_X86_64: 576 BCase(SHF_X86_64_LARGE); 577 break; 578 default: 579 // Nothing to do. 580 break; 581 } 582 #undef BCase 583 } 584 585 void ScalarEnumerationTraits<ELFYAML::ELF_SHN>::enumeration( 586 IO &IO, ELFYAML::ELF_SHN &Value) { 587 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 588 ECase(SHN_UNDEF); 589 ECase(SHN_LORESERVE); 590 ECase(SHN_LOPROC); 591 ECase(SHN_HIPROC); 592 ECase(SHN_LOOS); 593 ECase(SHN_HIOS); 594 ECase(SHN_ABS); 595 ECase(SHN_COMMON); 596 ECase(SHN_XINDEX); 597 ECase(SHN_HIRESERVE); 598 ECase(SHN_AMDGPU_LDS); 599 ECase(SHN_HEXAGON_SCOMMON); 600 ECase(SHN_HEXAGON_SCOMMON_1); 601 ECase(SHN_HEXAGON_SCOMMON_2); 602 ECase(SHN_HEXAGON_SCOMMON_4); 603 ECase(SHN_HEXAGON_SCOMMON_8); 604 #undef ECase 605 IO.enumFallback<Hex16>(Value); 606 } 607 608 void ScalarEnumerationTraits<ELFYAML::ELF_STB>::enumeration( 609 IO &IO, ELFYAML::ELF_STB &Value) { 610 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 611 ECase(STB_LOCAL); 612 ECase(STB_GLOBAL); 613 ECase(STB_WEAK); 614 ECase(STB_GNU_UNIQUE); 615 #undef ECase 616 IO.enumFallback<Hex8>(Value); 617 } 618 619 void ScalarEnumerationTraits<ELFYAML::ELF_STT>::enumeration( 620 IO &IO, ELFYAML::ELF_STT &Value) { 621 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 622 ECase(STT_NOTYPE); 623 ECase(STT_OBJECT); 624 ECase(STT_FUNC); 625 ECase(STT_SECTION); 626 ECase(STT_FILE); 627 ECase(STT_COMMON); 628 ECase(STT_TLS); 629 ECase(STT_GNU_IFUNC); 630 #undef ECase 631 IO.enumFallback<Hex8>(Value); 632 } 633 634 635 void ScalarEnumerationTraits<ELFYAML::ELF_RSS>::enumeration( 636 IO &IO, ELFYAML::ELF_RSS &Value) { 637 #define ECase(X) IO.enumCase(Value, #X, ELF::X) 638 ECase(RSS_UNDEF); 639 ECase(RSS_GP); 640 ECase(RSS_GP0); 641 ECase(RSS_LOC); 642 #undef ECase 643 } 644 645 void ScalarEnumerationTraits<ELFYAML::ELF_REL>::enumeration( 646 IO &IO, ELFYAML::ELF_REL &Value) { 647 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 648 assert(Object && "The IO context is not initialized"); 649 #define ELF_RELOC(X, Y) IO.enumCase(Value, #X, ELF::X); 650 switch (Object->getMachine()) { 651 case ELF::EM_X86_64: 652 #include "llvm/BinaryFormat/ELFRelocs/x86_64.def" 653 break; 654 case ELF::EM_MIPS: 655 #include "llvm/BinaryFormat/ELFRelocs/Mips.def" 656 break; 657 case ELF::EM_HEXAGON: 658 #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def" 659 break; 660 case ELF::EM_386: 661 case ELF::EM_IAMCU: 662 #include "llvm/BinaryFormat/ELFRelocs/i386.def" 663 break; 664 case ELF::EM_AARCH64: 665 #include "llvm/BinaryFormat/ELFRelocs/AArch64.def" 666 break; 667 case ELF::EM_ARM: 668 #include "llvm/BinaryFormat/ELFRelocs/ARM.def" 669 break; 670 case ELF::EM_ARC: 671 #include "llvm/BinaryFormat/ELFRelocs/ARC.def" 672 break; 673 case ELF::EM_RISCV: 674 #include "llvm/BinaryFormat/ELFRelocs/RISCV.def" 675 break; 676 case ELF::EM_LANAI: 677 #include "llvm/BinaryFormat/ELFRelocs/Lanai.def" 678 break; 679 case ELF::EM_AMDGPU: 680 #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def" 681 break; 682 case ELF::EM_BPF: 683 #include "llvm/BinaryFormat/ELFRelocs/BPF.def" 684 break; 685 case ELF::EM_VE: 686 #include "llvm/BinaryFormat/ELFRelocs/VE.def" 687 break; 688 case ELF::EM_CSKY: 689 #include "llvm/BinaryFormat/ELFRelocs/CSKY.def" 690 break; 691 case ELF::EM_PPC64: 692 #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def" 693 break; 694 default: 695 // Nothing to do. 696 break; 697 } 698 #undef ELF_RELOC 699 IO.enumFallback<Hex32>(Value); 700 } 701 702 void ScalarEnumerationTraits<ELFYAML::ELF_DYNTAG>::enumeration( 703 IO &IO, ELFYAML::ELF_DYNTAG &Value) { 704 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 705 assert(Object && "The IO context is not initialized"); 706 707 // Disable architecture specific tags by default. We might enable them below. 708 #define AARCH64_DYNAMIC_TAG(name, value) 709 #define MIPS_DYNAMIC_TAG(name, value) 710 #define HEXAGON_DYNAMIC_TAG(name, value) 711 #define PPC_DYNAMIC_TAG(name, value) 712 #define PPC64_DYNAMIC_TAG(name, value) 713 // Ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc. 714 #define DYNAMIC_TAG_MARKER(name, value) 715 716 #define STRINGIFY(X) (#X) 717 #define DYNAMIC_TAG(X, Y) IO.enumCase(Value, STRINGIFY(DT_##X), ELF::DT_##X); 718 switch (Object->getMachine()) { 719 case ELF::EM_AARCH64: 720 #undef AARCH64_DYNAMIC_TAG 721 #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 722 #include "llvm/BinaryFormat/DynamicTags.def" 723 #undef AARCH64_DYNAMIC_TAG 724 #define AARCH64_DYNAMIC_TAG(name, value) 725 break; 726 case ELF::EM_MIPS: 727 #undef MIPS_DYNAMIC_TAG 728 #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 729 #include "llvm/BinaryFormat/DynamicTags.def" 730 #undef MIPS_DYNAMIC_TAG 731 #define MIPS_DYNAMIC_TAG(name, value) 732 break; 733 case ELF::EM_HEXAGON: 734 #undef HEXAGON_DYNAMIC_TAG 735 #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 736 #include "llvm/BinaryFormat/DynamicTags.def" 737 #undef HEXAGON_DYNAMIC_TAG 738 #define HEXAGON_DYNAMIC_TAG(name, value) 739 break; 740 case ELF::EM_PPC: 741 #undef PPC_DYNAMIC_TAG 742 #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 743 #include "llvm/BinaryFormat/DynamicTags.def" 744 #undef PPC_DYNAMIC_TAG 745 #define PPC_DYNAMIC_TAG(name, value) 746 break; 747 case ELF::EM_PPC64: 748 #undef PPC64_DYNAMIC_TAG 749 #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_TAG(name, value) 750 #include "llvm/BinaryFormat/DynamicTags.def" 751 #undef PPC64_DYNAMIC_TAG 752 #define PPC64_DYNAMIC_TAG(name, value) 753 break; 754 default: 755 #include "llvm/BinaryFormat/DynamicTags.def" 756 break; 757 } 758 #undef AARCH64_DYNAMIC_TAG 759 #undef MIPS_DYNAMIC_TAG 760 #undef HEXAGON_DYNAMIC_TAG 761 #undef PPC_DYNAMIC_TAG 762 #undef PPC64_DYNAMIC_TAG 763 #undef DYNAMIC_TAG_MARKER 764 #undef STRINGIFY 765 #undef DYNAMIC_TAG 766 767 IO.enumFallback<Hex64>(Value); 768 } 769 770 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_REG>::enumeration( 771 IO &IO, ELFYAML::MIPS_AFL_REG &Value) { 772 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) 773 ECase(REG_NONE); 774 ECase(REG_32); 775 ECase(REG_64); 776 ECase(REG_128); 777 #undef ECase 778 } 779 780 void ScalarEnumerationTraits<ELFYAML::MIPS_ABI_FP>::enumeration( 781 IO &IO, ELFYAML::MIPS_ABI_FP &Value) { 782 #define ECase(X) IO.enumCase(Value, #X, Mips::Val_GNU_MIPS_ABI_##X) 783 ECase(FP_ANY); 784 ECase(FP_DOUBLE); 785 ECase(FP_SINGLE); 786 ECase(FP_SOFT); 787 ECase(FP_OLD_64); 788 ECase(FP_XX); 789 ECase(FP_64); 790 ECase(FP_64A); 791 #undef ECase 792 } 793 794 void ScalarEnumerationTraits<ELFYAML::MIPS_AFL_EXT>::enumeration( 795 IO &IO, ELFYAML::MIPS_AFL_EXT &Value) { 796 #define ECase(X) IO.enumCase(Value, #X, Mips::AFL_##X) 797 ECase(EXT_NONE); 798 ECase(EXT_XLR); 799 ECase(EXT_OCTEON2); 800 ECase(EXT_OCTEONP); 801 ECase(EXT_LOONGSON_3A); 802 ECase(EXT_OCTEON); 803 ECase(EXT_5900); 804 ECase(EXT_4650); 805 ECase(EXT_4010); 806 ECase(EXT_4100); 807 ECase(EXT_3900); 808 ECase(EXT_10000); 809 ECase(EXT_SB1); 810 ECase(EXT_4111); 811 ECase(EXT_4120); 812 ECase(EXT_5400); 813 ECase(EXT_5500); 814 ECase(EXT_LOONGSON_2E); 815 ECase(EXT_LOONGSON_2F); 816 ECase(EXT_OCTEON3); 817 #undef ECase 818 } 819 820 void ScalarEnumerationTraits<ELFYAML::MIPS_ISA>::enumeration( 821 IO &IO, ELFYAML::MIPS_ISA &Value) { 822 IO.enumCase(Value, "MIPS1", 1); 823 IO.enumCase(Value, "MIPS2", 2); 824 IO.enumCase(Value, "MIPS3", 3); 825 IO.enumCase(Value, "MIPS4", 4); 826 IO.enumCase(Value, "MIPS5", 5); 827 IO.enumCase(Value, "MIPS32", 32); 828 IO.enumCase(Value, "MIPS64", 64); 829 IO.enumFallback<Hex32>(Value); 830 } 831 832 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_ASE>::bitset( 833 IO &IO, ELFYAML::MIPS_AFL_ASE &Value) { 834 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_ASE_##X) 835 BCase(DSP); 836 BCase(DSPR2); 837 BCase(EVA); 838 BCase(MCU); 839 BCase(MDMX); 840 BCase(MIPS3D); 841 BCase(MT); 842 BCase(SMARTMIPS); 843 BCase(VIRT); 844 BCase(MSA); 845 BCase(MIPS16); 846 BCase(MICROMIPS); 847 BCase(XPA); 848 BCase(CRC); 849 BCase(GINV); 850 #undef BCase 851 } 852 853 void ScalarBitSetTraits<ELFYAML::MIPS_AFL_FLAGS1>::bitset( 854 IO &IO, ELFYAML::MIPS_AFL_FLAGS1 &Value) { 855 #define BCase(X) IO.bitSetCase(Value, #X, Mips::AFL_FLAGS1_##X) 856 BCase(ODDSPREG); 857 #undef BCase 858 } 859 860 void MappingTraits<ELFYAML::SectionHeader>::mapping( 861 IO &IO, ELFYAML::SectionHeader &SHdr) { 862 IO.mapRequired("Name", SHdr.Name); 863 } 864 865 void MappingTraits<ELFYAML::FileHeader>::mapping(IO &IO, 866 ELFYAML::FileHeader &FileHdr) { 867 IO.mapRequired("Class", FileHdr.Class); 868 IO.mapRequired("Data", FileHdr.Data); 869 IO.mapOptional("OSABI", FileHdr.OSABI, ELFYAML::ELF_ELFOSABI(0)); 870 IO.mapOptional("ABIVersion", FileHdr.ABIVersion, Hex8(0)); 871 IO.mapRequired("Type", FileHdr.Type); 872 IO.mapOptional("Machine", FileHdr.Machine); 873 IO.mapOptional("Flags", FileHdr.Flags, ELFYAML::ELF_EF(0)); 874 IO.mapOptional("Entry", FileHdr.Entry, Hex64(0)); 875 876 // obj2yaml does not dump these fields. 877 assert(!IO.outputting() || 878 (!FileHdr.EPhOff && !FileHdr.EPhEntSize && !FileHdr.EPhNum)); 879 IO.mapOptional("EPhOff", FileHdr.EPhOff); 880 IO.mapOptional("EPhEntSize", FileHdr.EPhEntSize); 881 IO.mapOptional("EPhNum", FileHdr.EPhNum); 882 IO.mapOptional("EShEntSize", FileHdr.EShEntSize); 883 IO.mapOptional("EShOff", FileHdr.EShOff); 884 IO.mapOptional("EShNum", FileHdr.EShNum); 885 IO.mapOptional("EShStrNdx", FileHdr.EShStrNdx); 886 } 887 888 void MappingTraits<ELFYAML::ProgramHeader>::mapping( 889 IO &IO, ELFYAML::ProgramHeader &Phdr) { 890 IO.mapRequired("Type", Phdr.Type); 891 IO.mapOptional("Flags", Phdr.Flags, ELFYAML::ELF_PF(0)); 892 IO.mapOptional("FirstSec", Phdr.FirstSec); 893 IO.mapOptional("LastSec", Phdr.LastSec); 894 IO.mapOptional("VAddr", Phdr.VAddr, Hex64(0)); 895 IO.mapOptional("PAddr", Phdr.PAddr, Phdr.VAddr); 896 IO.mapOptional("Align", Phdr.Align); 897 IO.mapOptional("FileSize", Phdr.FileSize); 898 IO.mapOptional("MemSize", Phdr.MemSize); 899 IO.mapOptional("Offset", Phdr.Offset); 900 } 901 902 std::string MappingTraits<ELFYAML::ProgramHeader>::validate( 903 IO &IO, ELFYAML::ProgramHeader &FileHdr) { 904 if (!FileHdr.FirstSec && FileHdr.LastSec) 905 return "the \"LastSec\" key can't be used without the \"FirstSec\" key"; 906 if (FileHdr.FirstSec && !FileHdr.LastSec) 907 return "the \"FirstSec\" key can't be used without the \"LastSec\" key"; 908 return ""; 909 } 910 911 LLVM_YAML_STRONG_TYPEDEF(StringRef, StOtherPiece) 912 913 template <> struct ScalarTraits<StOtherPiece> { 914 static void output(const StOtherPiece &Val, void *, raw_ostream &Out) { 915 Out << Val; 916 } 917 static StringRef input(StringRef Scalar, void *, StOtherPiece &Val) { 918 Val = Scalar; 919 return {}; 920 } 921 static QuotingType mustQuote(StringRef) { return QuotingType::None; } 922 }; 923 template <> struct SequenceElementTraits<StOtherPiece> { 924 static const bool flow = true; 925 }; 926 927 template <> struct ScalarTraits<ELFYAML::YAMLFlowString> { 928 static void output(const ELFYAML::YAMLFlowString &Val, void *, 929 raw_ostream &Out) { 930 Out << Val; 931 } 932 static StringRef input(StringRef Scalar, void *, 933 ELFYAML::YAMLFlowString &Val) { 934 Val = Scalar; 935 return {}; 936 } 937 static QuotingType mustQuote(StringRef S) { 938 return ScalarTraits<StringRef>::mustQuote(S); 939 } 940 }; 941 template <> struct SequenceElementTraits<ELFYAML::YAMLFlowString> { 942 static const bool flow = true; 943 }; 944 945 namespace { 946 947 struct NormalizedOther { 948 NormalizedOther(IO &IO) : YamlIO(IO) {} 949 NormalizedOther(IO &IO, Optional<uint8_t> Original) : YamlIO(IO) { 950 assert(Original && "This constructor is only used for outputting YAML and " 951 "assumes a non-empty Original"); 952 std::vector<StOtherPiece> Ret; 953 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext()); 954 for (std::pair<StringRef, uint8_t> &P : 955 getFlags(Object->getMachine()).takeVector()) { 956 uint8_t FlagValue = P.second; 957 if ((*Original & FlagValue) != FlagValue) 958 continue; 959 *Original &= ~FlagValue; 960 Ret.push_back({P.first}); 961 } 962 963 if (*Original != 0) { 964 UnknownFlagsHolder = std::to_string(*Original); 965 Ret.push_back({UnknownFlagsHolder}); 966 } 967 968 if (!Ret.empty()) 969 Other = std::move(Ret); 970 } 971 972 uint8_t toValue(StringRef Name) { 973 const auto *Object = static_cast<ELFYAML::Object *>(YamlIO.getContext()); 974 MapVector<StringRef, uint8_t> Flags = getFlags(Object->getMachine()); 975 976 auto It = Flags.find(Name); 977 if (It != Flags.end()) 978 return It->second; 979 980 uint8_t Val; 981 if (to_integer(Name, Val)) 982 return Val; 983 984 YamlIO.setError("an unknown value is used for symbol's 'Other' field: " + 985 Name); 986 return 0; 987 } 988 989 Optional<uint8_t> denormalize(IO &) { 990 if (!Other) 991 return None; 992 uint8_t Ret = 0; 993 for (StOtherPiece &Val : *Other) 994 Ret |= toValue(Val); 995 return Ret; 996 } 997 998 // st_other field is used to encode symbol visibility and platform-dependent 999 // flags and values. This method returns a name to value map that is used for 1000 // parsing and encoding this field. 1001 MapVector<StringRef, uint8_t> getFlags(unsigned EMachine) { 1002 MapVector<StringRef, uint8_t> Map; 1003 // STV_* values are just enumeration values. We add them in a reversed order 1004 // because when we convert the st_other to named constants when printing 1005 // YAML we want to use a maximum number of bits on each step: 1006 // when we have st_other == 3, we want to print it as STV_PROTECTED (3), but 1007 // not as STV_HIDDEN (2) + STV_INTERNAL (1). 1008 Map["STV_PROTECTED"] = ELF::STV_PROTECTED; 1009 Map["STV_HIDDEN"] = ELF::STV_HIDDEN; 1010 Map["STV_INTERNAL"] = ELF::STV_INTERNAL; 1011 // STV_DEFAULT is used to represent the default visibility and has a value 1012 // 0. We want to be able to read it from YAML documents, but there is no 1013 // reason to print it. 1014 if (!YamlIO.outputting()) 1015 Map["STV_DEFAULT"] = ELF::STV_DEFAULT; 1016 1017 // MIPS is not consistent. All of the STO_MIPS_* values are bit flags, 1018 // except STO_MIPS_MIPS16 which overlaps them. It should be checked and 1019 // consumed first when we print the output, because we do not want to print 1020 // any other flags that have the same bits instead. 1021 if (EMachine == ELF::EM_MIPS) { 1022 Map["STO_MIPS_MIPS16"] = ELF::STO_MIPS_MIPS16; 1023 Map["STO_MIPS_MICROMIPS"] = ELF::STO_MIPS_MICROMIPS; 1024 Map["STO_MIPS_PIC"] = ELF::STO_MIPS_PIC; 1025 Map["STO_MIPS_PLT"] = ELF::STO_MIPS_PLT; 1026 Map["STO_MIPS_OPTIONAL"] = ELF::STO_MIPS_OPTIONAL; 1027 } 1028 1029 if (EMachine == ELF::EM_AARCH64) 1030 Map["STO_AARCH64_VARIANT_PCS"] = ELF::STO_AARCH64_VARIANT_PCS; 1031 return Map; 1032 } 1033 1034 IO &YamlIO; 1035 Optional<std::vector<StOtherPiece>> Other; 1036 std::string UnknownFlagsHolder; 1037 }; 1038 1039 } // end anonymous namespace 1040 1041 void ScalarTraits<ELFYAML::YAMLIntUInt>::output(const ELFYAML::YAMLIntUInt &Val, 1042 void *Ctx, raw_ostream &Out) { 1043 Out << Val; 1044 } 1045 1046 StringRef ScalarTraits<ELFYAML::YAMLIntUInt>::input(StringRef Scalar, void *Ctx, 1047 ELFYAML::YAMLIntUInt &Val) { 1048 const bool Is64 = static_cast<ELFYAML::Object *>(Ctx)->Header.Class == 1049 ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); 1050 StringRef ErrMsg = "invalid number"; 1051 // We do not accept negative hex numbers because their meaning is ambiguous. 1052 // For example, would -0xfffffffff mean 1 or INT32_MIN? 1053 if (Scalar.empty() || Scalar.startswith("-0x")) 1054 return ErrMsg; 1055 1056 if (Scalar.startswith("-")) { 1057 const int64_t MinVal = Is64 ? INT64_MIN : INT32_MIN; 1058 long long Int; 1059 if (getAsSignedInteger(Scalar, /*Radix=*/0, Int) || (Int < MinVal)) 1060 return ErrMsg; 1061 Val = Int; 1062 return ""; 1063 } 1064 1065 const uint64_t MaxVal = Is64 ? UINT64_MAX : UINT32_MAX; 1066 unsigned long long UInt; 1067 if (getAsUnsignedInteger(Scalar, /*Radix=*/0, UInt) || (UInt > MaxVal)) 1068 return ErrMsg; 1069 Val = UInt; 1070 return ""; 1071 } 1072 1073 void MappingTraits<ELFYAML::Symbol>::mapping(IO &IO, ELFYAML::Symbol &Symbol) { 1074 IO.mapOptional("Name", Symbol.Name, StringRef()); 1075 IO.mapOptional("StName", Symbol.StName); 1076 IO.mapOptional("Type", Symbol.Type, ELFYAML::ELF_STT(0)); 1077 IO.mapOptional("Section", Symbol.Section); 1078 IO.mapOptional("Index", Symbol.Index); 1079 IO.mapOptional("Binding", Symbol.Binding, ELFYAML::ELF_STB(0)); 1080 IO.mapOptional("Value", Symbol.Value); 1081 IO.mapOptional("Size", Symbol.Size); 1082 1083 // Symbol's Other field is a bit special. It is usually a field that 1084 // represents st_other and holds the symbol visibility. However, on some 1085 // platforms, it can contain bit fields and regular values, or even sometimes a 1086 // crazy mix of them (see comments for NormalizedOther). Because of this, we 1087 // need special handling. 1088 MappingNormalization<NormalizedOther, Optional<uint8_t>> Keys(IO, 1089 Symbol.Other); 1090 IO.mapOptional("Other", Keys->Other); 1091 } 1092 1093 std::string MappingTraits<ELFYAML::Symbol>::validate(IO &IO, 1094 ELFYAML::Symbol &Symbol) { 1095 if (Symbol.Index && Symbol.Section) 1096 return "Index and Section cannot both be specified for Symbol"; 1097 return ""; 1098 } 1099 1100 static void commonSectionMapping(IO &IO, ELFYAML::Section &Section) { 1101 IO.mapOptional("Name", Section.Name, StringRef()); 1102 IO.mapRequired("Type", Section.Type); 1103 IO.mapOptional("Flags", Section.Flags); 1104 IO.mapOptional("Address", Section.Address); 1105 IO.mapOptional("Link", Section.Link); 1106 IO.mapOptional("AddressAlign", Section.AddressAlign, Hex64(0)); 1107 IO.mapOptional("EntSize", Section.EntSize); 1108 IO.mapOptional("Offset", Section.Offset); 1109 1110 IO.mapOptional("Content", Section.Content); 1111 IO.mapOptional("Size", Section.Size); 1112 1113 // obj2yaml does not dump these fields. They are expected to be empty when we 1114 // are producing YAML, because yaml2obj sets appropriate values for them 1115 // automatically when they are not explicitly defined. 1116 assert(!IO.outputting() || 1117 (!Section.ShOffset && !Section.ShSize && !Section.ShName && 1118 !Section.ShFlags && !Section.ShType && !Section.ShAddrAlign)); 1119 IO.mapOptional("ShAddrAlign", Section.ShAddrAlign); 1120 IO.mapOptional("ShName", Section.ShName); 1121 IO.mapOptional("ShOffset", Section.ShOffset); 1122 IO.mapOptional("ShSize", Section.ShSize); 1123 IO.mapOptional("ShFlags", Section.ShFlags); 1124 IO.mapOptional("ShType", Section.ShType); 1125 } 1126 1127 static void sectionMapping(IO &IO, ELFYAML::DynamicSection &Section) { 1128 commonSectionMapping(IO, Section); 1129 IO.mapOptional("Entries", Section.Entries); 1130 } 1131 1132 static void sectionMapping(IO &IO, ELFYAML::RawContentSection &Section) { 1133 commonSectionMapping(IO, Section); 1134 1135 // We also support reading a content as array of bytes using the ContentArray 1136 // key. obj2yaml never prints this field. 1137 assert(!IO.outputting() || !Section.ContentBuf.hasValue()); 1138 IO.mapOptional("ContentArray", Section.ContentBuf); 1139 if (Section.ContentBuf) { 1140 if (Section.Content) 1141 IO.setError("Content and ContentArray can't be used together"); 1142 Section.Content = yaml::BinaryRef(*Section.ContentBuf); 1143 } 1144 1145 IO.mapOptional("Info", Section.Info); 1146 } 1147 1148 static void sectionMapping(IO &IO, ELFYAML::BBAddrMapSection &Section) { 1149 commonSectionMapping(IO, Section); 1150 IO.mapOptional("Content", Section.Content); 1151 IO.mapOptional("Entries", Section.Entries); 1152 } 1153 1154 static void sectionMapping(IO &IO, ELFYAML::StackSizesSection &Section) { 1155 commonSectionMapping(IO, Section); 1156 IO.mapOptional("Entries", Section.Entries); 1157 } 1158 1159 static void sectionMapping(IO &IO, ELFYAML::HashSection &Section) { 1160 commonSectionMapping(IO, Section); 1161 IO.mapOptional("Bucket", Section.Bucket); 1162 IO.mapOptional("Chain", Section.Chain); 1163 1164 // obj2yaml does not dump these fields. They can be used to override nchain 1165 // and nbucket values for creating broken sections. 1166 assert(!IO.outputting() || 1167 (!Section.NBucket.hasValue() && !Section.NChain.hasValue())); 1168 IO.mapOptional("NChain", Section.NChain); 1169 IO.mapOptional("NBucket", Section.NBucket); 1170 } 1171 1172 static void sectionMapping(IO &IO, ELFYAML::NoteSection &Section) { 1173 commonSectionMapping(IO, Section); 1174 IO.mapOptional("Notes", Section.Notes); 1175 } 1176 1177 1178 static void sectionMapping(IO &IO, ELFYAML::GnuHashSection &Section) { 1179 commonSectionMapping(IO, Section); 1180 IO.mapOptional("Header", Section.Header); 1181 IO.mapOptional("BloomFilter", Section.BloomFilter); 1182 IO.mapOptional("HashBuckets", Section.HashBuckets); 1183 IO.mapOptional("HashValues", Section.HashValues); 1184 } 1185 static void sectionMapping(IO &IO, ELFYAML::NoBitsSection &Section) { 1186 commonSectionMapping(IO, Section); 1187 } 1188 1189 static void sectionMapping(IO &IO, ELFYAML::VerdefSection &Section) { 1190 commonSectionMapping(IO, Section); 1191 IO.mapOptional("Info", Section.Info); 1192 IO.mapOptional("Entries", Section.Entries); 1193 } 1194 1195 static void sectionMapping(IO &IO, ELFYAML::SymverSection &Section) { 1196 commonSectionMapping(IO, Section); 1197 IO.mapOptional("Entries", Section.Entries); 1198 } 1199 1200 static void sectionMapping(IO &IO, ELFYAML::VerneedSection &Section) { 1201 commonSectionMapping(IO, Section); 1202 IO.mapOptional("Info", Section.Info); 1203 IO.mapOptional("Dependencies", Section.VerneedV); 1204 } 1205 1206 static void sectionMapping(IO &IO, ELFYAML::RelocationSection &Section) { 1207 commonSectionMapping(IO, Section); 1208 IO.mapOptional("Info", Section.RelocatableSec, StringRef()); 1209 IO.mapOptional("Relocations", Section.Relocations); 1210 } 1211 1212 static void sectionMapping(IO &IO, ELFYAML::RelrSection &Section) { 1213 commonSectionMapping(IO, Section); 1214 IO.mapOptional("Entries", Section.Entries); 1215 } 1216 1217 static void groupSectionMapping(IO &IO, ELFYAML::GroupSection &Group) { 1218 commonSectionMapping(IO, Group); 1219 IO.mapOptional("Info", Group.Signature); 1220 IO.mapOptional("Members", Group.Members); 1221 } 1222 1223 static void sectionMapping(IO &IO, ELFYAML::SymtabShndxSection &Section) { 1224 commonSectionMapping(IO, Section); 1225 IO.mapOptional("Entries", Section.Entries); 1226 } 1227 1228 static void sectionMapping(IO &IO, ELFYAML::AddrsigSection &Section) { 1229 commonSectionMapping(IO, Section); 1230 IO.mapOptional("Symbols", Section.Symbols); 1231 } 1232 1233 static void fillMapping(IO &IO, ELFYAML::Fill &Fill) { 1234 IO.mapOptional("Name", Fill.Name, StringRef()); 1235 IO.mapOptional("Pattern", Fill.Pattern); 1236 IO.mapOptional("Offset", Fill.Offset); 1237 IO.mapRequired("Size", Fill.Size); 1238 } 1239 1240 static void sectionHeaderTableMapping(IO &IO, 1241 ELFYAML::SectionHeaderTable &SHT) { 1242 IO.mapOptional("Offset", SHT.Offset); 1243 IO.mapOptional("Sections", SHT.Sections); 1244 IO.mapOptional("Excluded", SHT.Excluded); 1245 IO.mapOptional("NoHeaders", SHT.NoHeaders); 1246 } 1247 1248 static void sectionMapping(IO &IO, ELFYAML::LinkerOptionsSection &Section) { 1249 commonSectionMapping(IO, Section); 1250 IO.mapOptional("Options", Section.Options); 1251 } 1252 1253 static void sectionMapping(IO &IO, 1254 ELFYAML::DependentLibrariesSection &Section) { 1255 commonSectionMapping(IO, Section); 1256 IO.mapOptional("Libraries", Section.Libs); 1257 } 1258 1259 static void sectionMapping(IO &IO, ELFYAML::CallGraphProfileSection &Section) { 1260 commonSectionMapping(IO, Section); 1261 IO.mapOptional("Entries", Section.Entries); 1262 } 1263 1264 void MappingTraits<ELFYAML::SectionOrType>::mapping( 1265 IO &IO, ELFYAML::SectionOrType §ionOrType) { 1266 IO.mapRequired("SectionOrType", sectionOrType.sectionNameOrType); 1267 } 1268 1269 static void sectionMapping(IO &IO, ELFYAML::ARMIndexTableSection &Section) { 1270 commonSectionMapping(IO, Section); 1271 IO.mapOptional("Entries", Section.Entries); 1272 } 1273 1274 static void sectionMapping(IO &IO, ELFYAML::MipsABIFlags &Section) { 1275 commonSectionMapping(IO, Section); 1276 IO.mapOptional("Version", Section.Version, Hex16(0)); 1277 IO.mapRequired("ISA", Section.ISALevel); 1278 IO.mapOptional("ISARevision", Section.ISARevision, Hex8(0)); 1279 IO.mapOptional("ISAExtension", Section.ISAExtension, 1280 ELFYAML::MIPS_AFL_EXT(Mips::AFL_EXT_NONE)); 1281 IO.mapOptional("ASEs", Section.ASEs, ELFYAML::MIPS_AFL_ASE(0)); 1282 IO.mapOptional("FpABI", Section.FpABI, 1283 ELFYAML::MIPS_ABI_FP(Mips::Val_GNU_MIPS_ABI_FP_ANY)); 1284 IO.mapOptional("GPRSize", Section.GPRSize, 1285 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 1286 IO.mapOptional("CPR1Size", Section.CPR1Size, 1287 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 1288 IO.mapOptional("CPR2Size", Section.CPR2Size, 1289 ELFYAML::MIPS_AFL_REG(Mips::AFL_REG_NONE)); 1290 IO.mapOptional("Flags1", Section.Flags1, ELFYAML::MIPS_AFL_FLAGS1(0)); 1291 IO.mapOptional("Flags2", Section.Flags2, Hex32(0)); 1292 } 1293 1294 static StringRef getStringValue(IO &IO, const char *Key) { 1295 StringRef Val; 1296 IO.mapRequired(Key, Val); 1297 return Val; 1298 } 1299 1300 static void setStringValue(IO &IO, const char *Key, StringRef Val) { 1301 IO.mapRequired(Key, Val); 1302 } 1303 1304 static bool isInteger(StringRef Val) { 1305 APInt Tmp; 1306 return !Val.getAsInteger(0, Tmp); 1307 } 1308 1309 void MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::mapping( 1310 IO &IO, std::unique_ptr<ELFYAML::Chunk> &Section) { 1311 ELFYAML::ELF_SHT Type; 1312 StringRef TypeStr; 1313 if (IO.outputting()) { 1314 if (auto *S = dyn_cast<ELFYAML::Section>(Section.get())) 1315 Type = S->Type; 1316 else if (auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(Section.get())) 1317 TypeStr = SHT->TypeStr; 1318 } else { 1319 // When the Type string does not have a "SHT_" prefix, we know it is not a 1320 // description of a regular ELF output section. 1321 TypeStr = getStringValue(IO, "Type"); 1322 if (TypeStr.startswith("SHT_") || isInteger(TypeStr)) 1323 IO.mapRequired("Type", Type); 1324 } 1325 1326 if (TypeStr == "Fill") { 1327 assert(!IO.outputting()); // We don't dump fills currently. 1328 Section.reset(new ELFYAML::Fill()); 1329 fillMapping(IO, *cast<ELFYAML::Fill>(Section.get())); 1330 return; 1331 } 1332 1333 if (TypeStr == ELFYAML::SectionHeaderTable::TypeStr) { 1334 if (IO.outputting()) 1335 setStringValue(IO, "Type", TypeStr); 1336 else 1337 Section.reset(new ELFYAML::SectionHeaderTable(/*IsImplicit=*/false)); 1338 1339 sectionHeaderTableMapping( 1340 IO, *cast<ELFYAML::SectionHeaderTable>(Section.get())); 1341 return; 1342 } 1343 1344 const auto &Obj = *static_cast<ELFYAML::Object *>(IO.getContext()); 1345 if (Obj.getMachine() == ELF::EM_MIPS && Type == ELF::SHT_MIPS_ABIFLAGS) { 1346 if (!IO.outputting()) 1347 Section.reset(new ELFYAML::MipsABIFlags()); 1348 sectionMapping(IO, *cast<ELFYAML::MipsABIFlags>(Section.get())); 1349 return; 1350 } 1351 1352 if (Obj.getMachine() == ELF::EM_ARM && Type == ELF::SHT_ARM_EXIDX) { 1353 if (!IO.outputting()) 1354 Section.reset(new ELFYAML::ARMIndexTableSection()); 1355 sectionMapping(IO, *cast<ELFYAML::ARMIndexTableSection>(Section.get())); 1356 return; 1357 } 1358 1359 switch (Type) { 1360 case ELF::SHT_DYNAMIC: 1361 if (!IO.outputting()) 1362 Section.reset(new ELFYAML::DynamicSection()); 1363 sectionMapping(IO, *cast<ELFYAML::DynamicSection>(Section.get())); 1364 break; 1365 case ELF::SHT_REL: 1366 case ELF::SHT_RELA: 1367 if (!IO.outputting()) 1368 Section.reset(new ELFYAML::RelocationSection()); 1369 sectionMapping(IO, *cast<ELFYAML::RelocationSection>(Section.get())); 1370 break; 1371 case ELF::SHT_RELR: 1372 if (!IO.outputting()) 1373 Section.reset(new ELFYAML::RelrSection()); 1374 sectionMapping(IO, *cast<ELFYAML::RelrSection>(Section.get())); 1375 break; 1376 case ELF::SHT_GROUP: 1377 if (!IO.outputting()) 1378 Section.reset(new ELFYAML::GroupSection()); 1379 groupSectionMapping(IO, *cast<ELFYAML::GroupSection>(Section.get())); 1380 break; 1381 case ELF::SHT_NOBITS: 1382 if (!IO.outputting()) 1383 Section.reset(new ELFYAML::NoBitsSection()); 1384 sectionMapping(IO, *cast<ELFYAML::NoBitsSection>(Section.get())); 1385 break; 1386 case ELF::SHT_HASH: 1387 if (!IO.outputting()) 1388 Section.reset(new ELFYAML::HashSection()); 1389 sectionMapping(IO, *cast<ELFYAML::HashSection>(Section.get())); 1390 break; 1391 case ELF::SHT_NOTE: 1392 if (!IO.outputting()) 1393 Section.reset(new ELFYAML::NoteSection()); 1394 sectionMapping(IO, *cast<ELFYAML::NoteSection>(Section.get())); 1395 break; 1396 case ELF::SHT_GNU_HASH: 1397 if (!IO.outputting()) 1398 Section.reset(new ELFYAML::GnuHashSection()); 1399 sectionMapping(IO, *cast<ELFYAML::GnuHashSection>(Section.get())); 1400 break; 1401 case ELF::SHT_GNU_verdef: 1402 if (!IO.outputting()) 1403 Section.reset(new ELFYAML::VerdefSection()); 1404 sectionMapping(IO, *cast<ELFYAML::VerdefSection>(Section.get())); 1405 break; 1406 case ELF::SHT_GNU_versym: 1407 if (!IO.outputting()) 1408 Section.reset(new ELFYAML::SymverSection()); 1409 sectionMapping(IO, *cast<ELFYAML::SymverSection>(Section.get())); 1410 break; 1411 case ELF::SHT_GNU_verneed: 1412 if (!IO.outputting()) 1413 Section.reset(new ELFYAML::VerneedSection()); 1414 sectionMapping(IO, *cast<ELFYAML::VerneedSection>(Section.get())); 1415 break; 1416 case ELF::SHT_SYMTAB_SHNDX: 1417 if (!IO.outputting()) 1418 Section.reset(new ELFYAML::SymtabShndxSection()); 1419 sectionMapping(IO, *cast<ELFYAML::SymtabShndxSection>(Section.get())); 1420 break; 1421 case ELF::SHT_LLVM_ADDRSIG: 1422 if (!IO.outputting()) 1423 Section.reset(new ELFYAML::AddrsigSection()); 1424 sectionMapping(IO, *cast<ELFYAML::AddrsigSection>(Section.get())); 1425 break; 1426 case ELF::SHT_LLVM_LINKER_OPTIONS: 1427 if (!IO.outputting()) 1428 Section.reset(new ELFYAML::LinkerOptionsSection()); 1429 sectionMapping(IO, *cast<ELFYAML::LinkerOptionsSection>(Section.get())); 1430 break; 1431 case ELF::SHT_LLVM_DEPENDENT_LIBRARIES: 1432 if (!IO.outputting()) 1433 Section.reset(new ELFYAML::DependentLibrariesSection()); 1434 sectionMapping(IO, 1435 *cast<ELFYAML::DependentLibrariesSection>(Section.get())); 1436 break; 1437 case ELF::SHT_LLVM_CALL_GRAPH_PROFILE: 1438 if (!IO.outputting()) 1439 Section.reset(new ELFYAML::CallGraphProfileSection()); 1440 sectionMapping(IO, *cast<ELFYAML::CallGraphProfileSection>(Section.get())); 1441 break; 1442 case ELF::SHT_LLVM_BB_ADDR_MAP: 1443 if (!IO.outputting()) 1444 Section.reset(new ELFYAML::BBAddrMapSection()); 1445 sectionMapping(IO, *cast<ELFYAML::BBAddrMapSection>(Section.get())); 1446 break; 1447 default: 1448 if (!IO.outputting()) { 1449 StringRef Name; 1450 IO.mapOptional("Name", Name, StringRef()); 1451 Name = ELFYAML::dropUniqueSuffix(Name); 1452 1453 if (ELFYAML::StackSizesSection::nameMatches(Name)) 1454 Section = std::make_unique<ELFYAML::StackSizesSection>(); 1455 else 1456 Section = std::make_unique<ELFYAML::RawContentSection>(); 1457 } 1458 1459 if (auto S = dyn_cast<ELFYAML::RawContentSection>(Section.get())) 1460 sectionMapping(IO, *S); 1461 else 1462 sectionMapping(IO, *cast<ELFYAML::StackSizesSection>(Section.get())); 1463 } 1464 } 1465 1466 std::string MappingTraits<std::unique_ptr<ELFYAML::Chunk>>::validate( 1467 IO &io, std::unique_ptr<ELFYAML::Chunk> &C) { 1468 if (const auto *F = dyn_cast<ELFYAML::Fill>(C.get())) { 1469 if (F->Pattern && F->Pattern->binary_size() != 0 && !F->Size) 1470 return "\"Size\" can't be 0 when \"Pattern\" is not empty"; 1471 return ""; 1472 } 1473 1474 if (const auto *SHT = dyn_cast<ELFYAML::SectionHeaderTable>(C.get())) { 1475 if (SHT->NoHeaders && (SHT->Sections || SHT->Excluded || SHT->Offset)) 1476 return "NoHeaders can't be used together with Offset/Sections/Excluded"; 1477 if (!SHT->NoHeaders && !SHT->Sections && !SHT->Excluded) 1478 return "SectionHeaderTable can't be empty. Use 'NoHeaders' key to drop " 1479 "the section header table"; 1480 return ""; 1481 } 1482 1483 const ELFYAML::Section &Sec = *cast<ELFYAML::Section>(C.get()); 1484 if (Sec.Size && Sec.Content && 1485 (uint64_t)(*Sec.Size) < Sec.Content->binary_size()) 1486 return "Section size must be greater than or equal to the content size"; 1487 1488 auto BuildErrPrefix = [](ArrayRef<std::pair<StringRef, bool>> EntV) { 1489 std::string Msg; 1490 for (size_t I = 0, E = EntV.size(); I != E; ++I) { 1491 StringRef Name = EntV[I].first; 1492 if (I == 0) { 1493 Msg = "\"" + Name.str() + "\""; 1494 continue; 1495 } 1496 if (I != EntV.size() - 1) 1497 Msg += ", \"" + Name.str() + "\""; 1498 else 1499 Msg += " and \"" + Name.str() + "\""; 1500 } 1501 return Msg; 1502 }; 1503 1504 std::vector<std::pair<StringRef, bool>> Entries = Sec.getEntries(); 1505 const size_t NumUsedEntries = llvm::count_if( 1506 Entries, [](const std::pair<StringRef, bool> &P) { return P.second; }); 1507 1508 if ((Sec.Size || Sec.Content) && NumUsedEntries > 0) 1509 return BuildErrPrefix(Entries) + 1510 " cannot be used with \"Content\" or \"Size\""; 1511 1512 if (NumUsedEntries > 0 && Entries.size() != NumUsedEntries) 1513 return BuildErrPrefix(Entries) + " must be used together"; 1514 1515 if (const auto *RawSection = dyn_cast<ELFYAML::RawContentSection>(C.get())) { 1516 if (RawSection->Flags && RawSection->ShFlags) 1517 return "ShFlags and Flags cannot be used together"; 1518 return ""; 1519 } 1520 1521 if (const auto *NB = dyn_cast<ELFYAML::NoBitsSection>(C.get())) { 1522 if (NB->Content) 1523 return "SHT_NOBITS section cannot have \"Content\""; 1524 return ""; 1525 } 1526 1527 if (const auto *MF = dyn_cast<ELFYAML::MipsABIFlags>(C.get())) { 1528 if (MF->Content) 1529 return "\"Content\" key is not implemented for SHT_MIPS_ABIFLAGS " 1530 "sections"; 1531 if (MF->Size) 1532 return "\"Size\" key is not implemented for SHT_MIPS_ABIFLAGS sections"; 1533 return ""; 1534 } 1535 1536 return ""; 1537 } 1538 1539 namespace { 1540 1541 struct NormalizedMips64RelType { 1542 NormalizedMips64RelType(IO &) 1543 : Type(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1544 Type2(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1545 Type3(ELFYAML::ELF_REL(ELF::R_MIPS_NONE)), 1546 SpecSym(ELFYAML::ELF_REL(ELF::RSS_UNDEF)) {} 1547 NormalizedMips64RelType(IO &, ELFYAML::ELF_REL Original) 1548 : Type(Original & 0xFF), Type2(Original >> 8 & 0xFF), 1549 Type3(Original >> 16 & 0xFF), SpecSym(Original >> 24 & 0xFF) {} 1550 1551 ELFYAML::ELF_REL denormalize(IO &) { 1552 ELFYAML::ELF_REL Res = Type | Type2 << 8 | Type3 << 16 | SpecSym << 24; 1553 return Res; 1554 } 1555 1556 ELFYAML::ELF_REL Type; 1557 ELFYAML::ELF_REL Type2; 1558 ELFYAML::ELF_REL Type3; 1559 ELFYAML::ELF_RSS SpecSym; 1560 }; 1561 1562 } // end anonymous namespace 1563 1564 void MappingTraits<ELFYAML::StackSizeEntry>::mapping( 1565 IO &IO, ELFYAML::StackSizeEntry &E) { 1566 assert(IO.getContext() && "The IO context is not initialized"); 1567 IO.mapOptional("Address", E.Address, Hex64(0)); 1568 IO.mapRequired("Size", E.Size); 1569 } 1570 1571 void MappingTraits<ELFYAML::BBAddrMapEntry>::mapping( 1572 IO &IO, ELFYAML::BBAddrMapEntry &E) { 1573 assert(IO.getContext() && "The IO context is not initialized"); 1574 IO.mapOptional("Address", E.Address, Hex64(0)); 1575 IO.mapOptional("BBEntries", E.BBEntries); 1576 } 1577 1578 void MappingTraits<ELFYAML::BBAddrMapEntry::BBEntry>::mapping( 1579 IO &IO, ELFYAML::BBAddrMapEntry::BBEntry &E) { 1580 assert(IO.getContext() && "The IO context is not initialized"); 1581 IO.mapRequired("AddressOffset", E.AddressOffset); 1582 IO.mapRequired("Size", E.Size); 1583 IO.mapRequired("Metadata", E.Metadata); 1584 } 1585 1586 void MappingTraits<ELFYAML::GnuHashHeader>::mapping(IO &IO, 1587 ELFYAML::GnuHashHeader &E) { 1588 assert(IO.getContext() && "The IO context is not initialized"); 1589 IO.mapOptional("NBuckets", E.NBuckets); 1590 IO.mapRequired("SymNdx", E.SymNdx); 1591 IO.mapOptional("MaskWords", E.MaskWords); 1592 IO.mapRequired("Shift2", E.Shift2); 1593 } 1594 1595 void MappingTraits<ELFYAML::DynamicEntry>::mapping(IO &IO, 1596 ELFYAML::DynamicEntry &Rel) { 1597 assert(IO.getContext() && "The IO context is not initialized"); 1598 1599 IO.mapRequired("Tag", Rel.Tag); 1600 IO.mapRequired("Value", Rel.Val); 1601 } 1602 1603 void MappingTraits<ELFYAML::NoteEntry>::mapping(IO &IO, ELFYAML::NoteEntry &N) { 1604 assert(IO.getContext() && "The IO context is not initialized"); 1605 1606 IO.mapOptional("Name", N.Name); 1607 IO.mapOptional("Desc", N.Desc); 1608 IO.mapRequired("Type", N.Type); 1609 } 1610 1611 void MappingTraits<ELFYAML::VerdefEntry>::mapping(IO &IO, 1612 ELFYAML::VerdefEntry &E) { 1613 assert(IO.getContext() && "The IO context is not initialized"); 1614 1615 IO.mapOptional("Version", E.Version); 1616 IO.mapOptional("Flags", E.Flags); 1617 IO.mapOptional("VersionNdx", E.VersionNdx); 1618 IO.mapOptional("Hash", E.Hash); 1619 IO.mapRequired("Names", E.VerNames); 1620 } 1621 1622 void MappingTraits<ELFYAML::VerneedEntry>::mapping(IO &IO, 1623 ELFYAML::VerneedEntry &E) { 1624 assert(IO.getContext() && "The IO context is not initialized"); 1625 1626 IO.mapRequired("Version", E.Version); 1627 IO.mapRequired("File", E.File); 1628 IO.mapRequired("Entries", E.AuxV); 1629 } 1630 1631 void MappingTraits<ELFYAML::VernauxEntry>::mapping(IO &IO, 1632 ELFYAML::VernauxEntry &E) { 1633 assert(IO.getContext() && "The IO context is not initialized"); 1634 1635 IO.mapRequired("Name", E.Name); 1636 IO.mapRequired("Hash", E.Hash); 1637 IO.mapRequired("Flags", E.Flags); 1638 IO.mapRequired("Other", E.Other); 1639 } 1640 1641 void MappingTraits<ELFYAML::Relocation>::mapping(IO &IO, 1642 ELFYAML::Relocation &Rel) { 1643 const auto *Object = static_cast<ELFYAML::Object *>(IO.getContext()); 1644 assert(Object && "The IO context is not initialized"); 1645 1646 IO.mapOptional("Offset", Rel.Offset, (Hex64)0); 1647 IO.mapOptional("Symbol", Rel.Symbol); 1648 1649 if (Object->getMachine() == ELFYAML::ELF_EM(ELF::EM_MIPS) && 1650 Object->Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64)) { 1651 MappingNormalization<NormalizedMips64RelType, ELFYAML::ELF_REL> Key( 1652 IO, Rel.Type); 1653 IO.mapRequired("Type", Key->Type); 1654 IO.mapOptional("Type2", Key->Type2, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); 1655 IO.mapOptional("Type3", Key->Type3, ELFYAML::ELF_REL(ELF::R_MIPS_NONE)); 1656 IO.mapOptional("SpecSym", Key->SpecSym, ELFYAML::ELF_RSS(ELF::RSS_UNDEF)); 1657 } else 1658 IO.mapRequired("Type", Rel.Type); 1659 1660 IO.mapOptional("Addend", Rel.Addend, (ELFYAML::YAMLIntUInt)0); 1661 } 1662 1663 void MappingTraits<ELFYAML::ARMIndexTableEntry>::mapping( 1664 IO &IO, ELFYAML::ARMIndexTableEntry &E) { 1665 assert(IO.getContext() && "The IO context is not initialized"); 1666 IO.mapRequired("Offset", E.Offset); 1667 1668 StringRef CantUnwind = "EXIDX_CANTUNWIND"; 1669 if (IO.outputting() && (uint32_t)E.Value == ARM::EHABI::EXIDX_CANTUNWIND) 1670 IO.mapRequired("Value", CantUnwind); 1671 else if (!IO.outputting() && getStringValue(IO, "Value") == CantUnwind) 1672 E.Value = ARM::EHABI::EXIDX_CANTUNWIND; 1673 else 1674 IO.mapRequired("Value", E.Value); 1675 } 1676 1677 void MappingTraits<ELFYAML::Object>::mapping(IO &IO, ELFYAML::Object &Object) { 1678 assert(!IO.getContext() && "The IO context is initialized already"); 1679 IO.setContext(&Object); 1680 IO.mapTag("!ELF", true); 1681 IO.mapRequired("FileHeader", Object.Header); 1682 IO.mapOptional("ProgramHeaders", Object.ProgramHeaders); 1683 IO.mapOptional("Sections", Object.Chunks); 1684 IO.mapOptional("Symbols", Object.Symbols); 1685 IO.mapOptional("DynamicSymbols", Object.DynamicSymbols); 1686 IO.mapOptional("DWARF", Object.DWARF); 1687 if (Object.DWARF) { 1688 Object.DWARF->IsLittleEndian = 1689 Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); 1690 Object.DWARF->Is64BitAddrSize = 1691 Object.Header.Class == ELFYAML::ELF_ELFCLASS(ELF::ELFCLASS64); 1692 } 1693 IO.setContext(nullptr); 1694 } 1695 1696 void MappingTraits<ELFYAML::LinkerOption>::mapping(IO &IO, 1697 ELFYAML::LinkerOption &Opt) { 1698 assert(IO.getContext() && "The IO context is not initialized"); 1699 IO.mapRequired("Name", Opt.Key); 1700 IO.mapRequired("Value", Opt.Value); 1701 } 1702 1703 void MappingTraits<ELFYAML::CallGraphEntry>::mapping( 1704 IO &IO, ELFYAML::CallGraphEntry &E) { 1705 assert(IO.getContext() && "The IO context is not initialized"); 1706 IO.mapRequired("From", E.From); 1707 IO.mapRequired("To", E.To); 1708 IO.mapRequired("Weight", E.Weight); 1709 } 1710 1711 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_AFL_REG) 1712 LLVM_YAML_STRONG_TYPEDEF(uint8_t, MIPS_ABI_FP) 1713 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_EXT) 1714 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_ASE) 1715 LLVM_YAML_STRONG_TYPEDEF(uint32_t, MIPS_AFL_FLAGS1) 1716 1717 } // end namespace yaml 1718 1719 } // end namespace llvm 1720