1 //===- llvm/BinaryFormat/ELF.cpp - The ELF format ---------------*- C++ -*-===// 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 #include "llvm/BinaryFormat/ELF.h" 10 #include "llvm/ADT/DenseMapInfo.h" 11 #include "llvm/ADT/StringSwitch.h" 12 13 using namespace llvm; 14 using namespace ELF; 15 16 /// Convert an architecture name into ELF's e_machine value. 17 uint16_t ELF::convertArchNameToEMachine(StringRef Arch) { 18 std::string LowerArch = Arch.lower(); 19 return StringSwitch<uint16_t>(LowerArch) 20 .Case("none", EM_NONE) 21 .Case("m32", EM_M32) 22 .Case("sparc", EM_SPARC) 23 .Case("386", EM_386) 24 .Case("68k", EM_68K) 25 .Case("88k", EM_88K) 26 .Case("iamcu", EM_IAMCU) 27 .Case("860", EM_860) 28 .Case("mips", EM_MIPS) 29 .Case("s370", EM_S370) 30 .Case("mips_rs3_le", EM_MIPS_RS3_LE) 31 .Case("parisc", EM_PARISC) 32 .Case("vpp500", EM_VPP500) 33 .Case("sparc32plus", EM_SPARC32PLUS) 34 .Case("960", EM_960) 35 .Case("ppc", EM_PPC) 36 .Case("ppc64", EM_PPC64) 37 .Case("s390", EM_S390) 38 .Case("spu", EM_SPU) 39 .Case("v800", EM_V800) 40 .Case("fr20", EM_FR20) 41 .Case("rh32", EM_RH32) 42 .Case("rce", EM_RCE) 43 .Case("arm", EM_ARM) 44 .Case("alpha", EM_ALPHA) 45 .Case("sh", EM_SH) 46 .Case("sparcv9", EM_SPARCV9) 47 .Case("tricore", EM_TRICORE) 48 .Case("arc", EM_ARC) 49 .Case("h8_300", EM_H8_300) 50 .Case("h8_300h", EM_H8_300H) 51 .Case("h8s", EM_H8S) 52 .Case("h8_500", EM_H8_500) 53 .Case("ia_64", EM_IA_64) 54 .Case("mips_x", EM_MIPS_X) 55 .Case("coldfire", EM_COLDFIRE) 56 .Case("68hc12", EM_68HC12) 57 .Case("mma", EM_MMA) 58 .Case("pcp", EM_PCP) 59 .Case("ncpu", EM_NCPU) 60 .Case("ndr1", EM_NDR1) 61 .Case("starcore", EM_STARCORE) 62 .Case("me16", EM_ME16) 63 .Case("st100", EM_ST100) 64 .Case("tinyj", EM_TINYJ) 65 .Case("x86_64", EM_X86_64) 66 .Case("pdsp", EM_PDSP) 67 .Case("pdp10", EM_PDP10) 68 .Case("pdp11", EM_PDP11) 69 .Case("fx66", EM_FX66) 70 .Case("st9plus", EM_ST9PLUS) 71 .Case("st7", EM_ST7) 72 .Case("68hc16", EM_68HC16) 73 .Case("68hc11", EM_68HC11) 74 .Case("68hc08", EM_68HC08) 75 .Case("68hc05", EM_68HC05) 76 .Case("svx", EM_SVX) 77 .Case("st19", EM_ST19) 78 .Case("vax", EM_VAX) 79 .Case("cris", EM_CRIS) 80 .Case("javelin", EM_JAVELIN) 81 .Case("firepath", EM_FIREPATH) 82 .Case("zsp", EM_ZSP) 83 .Case("mmix", EM_MMIX) 84 .Case("huany", EM_HUANY) 85 .Case("prism", EM_PRISM) 86 .Case("avr", EM_AVR) 87 .Case("fr30", EM_FR30) 88 .Case("d10v", EM_D10V) 89 .Case("d30v", EM_D30V) 90 .Case("v850", EM_V850) 91 .Case("m32r", EM_M32R) 92 .Case("mn10300", EM_MN10300) 93 .Case("mn10200", EM_MN10200) 94 .Case("pj", EM_PJ) 95 .Case("openrisc", EM_OPENRISC) 96 .Case("arc_compact", EM_ARC_COMPACT) 97 .Case("xtensa", EM_XTENSA) 98 .Case("videocore", EM_VIDEOCORE) 99 .Case("tmm_gpp", EM_TMM_GPP) 100 .Case("ns32k", EM_NS32K) 101 .Case("tpc", EM_TPC) 102 .Case("snp1k", EM_SNP1K) 103 .Case("st200", EM_ST200) 104 .Case("ip2k", EM_IP2K) 105 .Case("max", EM_MAX) 106 .Case("cr", EM_CR) 107 .Case("f2mc16", EM_F2MC16) 108 .Case("msp430", EM_MSP430) 109 .Case("blackfin", EM_BLACKFIN) 110 .Case("se_c33", EM_SE_C33) 111 .Case("sep", EM_SEP) 112 .Case("arca", EM_ARCA) 113 .Case("unicore", EM_UNICORE) 114 .Case("excess", EM_EXCESS) 115 .Case("dxp", EM_DXP) 116 .Case("altera_nios2", EM_ALTERA_NIOS2) 117 .Case("crx", EM_CRX) 118 .Case("xgate", EM_XGATE) 119 .Case("c166", EM_C166) 120 .Case("m16c", EM_M16C) 121 .Case("dspic30f", EM_DSPIC30F) 122 .Case("ce", EM_CE) 123 .Case("m32c", EM_M32C) 124 .Case("tsk3000", EM_TSK3000) 125 .Case("rs08", EM_RS08) 126 .Case("sharc", EM_SHARC) 127 .Case("ecog2", EM_ECOG2) 128 .Case("score7", EM_SCORE7) 129 .Case("dsp24", EM_DSP24) 130 .Case("videocore3", EM_VIDEOCORE3) 131 .Case("latticemico32", EM_LATTICEMICO32) 132 .Case("se_c17", EM_SE_C17) 133 .Case("ti_c6000", EM_TI_C6000) 134 .Case("ti_c2000", EM_TI_C2000) 135 .Case("ti_c5500", EM_TI_C5500) 136 .Case("mmdsp_plus", EM_MMDSP_PLUS) 137 .Case("cypress_m8c", EM_CYPRESS_M8C) 138 .Case("r32c", EM_R32C) 139 .Case("trimedia", EM_TRIMEDIA) 140 .Case("hexagon", EM_HEXAGON) 141 .Case("8051", EM_8051) 142 .Case("stxp7x", EM_STXP7X) 143 .Case("nds32", EM_NDS32) 144 .Case("ecog1", EM_ECOG1) 145 .Case("ecog1x", EM_ECOG1X) 146 .Case("maxq30", EM_MAXQ30) 147 .Case("ximo16", EM_XIMO16) 148 .Case("manik", EM_MANIK) 149 .Case("craynv2", EM_CRAYNV2) 150 .Case("rx", EM_RX) 151 .Case("metag", EM_METAG) 152 .Case("mcst_elbrus", EM_MCST_ELBRUS) 153 .Case("ecog16", EM_ECOG16) 154 .Case("cr16", EM_CR16) 155 .Case("etpu", EM_ETPU) 156 .Case("sle9x", EM_SLE9X) 157 .Case("l10m", EM_L10M) 158 .Case("k10m", EM_K10M) 159 .Case("aarch64", EM_AARCH64) 160 .Case("avr32", EM_AVR32) 161 .Case("stm8", EM_STM8) 162 .Case("tile64", EM_TILE64) 163 .Case("tilepro", EM_TILEPRO) 164 .Case("cuda", EM_CUDA) 165 .Case("tilegx", EM_TILEGX) 166 .Case("cloudshield", EM_CLOUDSHIELD) 167 .Case("corea_1st", EM_COREA_1ST) 168 .Case("corea_2nd", EM_COREA_2ND) 169 .Case("arc_compact2", EM_ARC_COMPACT2) 170 .Case("open8", EM_OPEN8) 171 .Case("rl78", EM_RL78) 172 .Case("videocore5", EM_VIDEOCORE5) 173 .Case("78kor", EM_78KOR) 174 .Case("56800ex", EM_56800EX) 175 .Case("ba1", EM_BA1) 176 .Case("ba2", EM_BA2) 177 .Case("xcore", EM_XCORE) 178 .Case("mchp_pic", EM_MCHP_PIC) 179 .Case("intel205", EM_INTEL205) 180 .Case("intel206", EM_INTEL206) 181 .Case("intel207", EM_INTEL207) 182 .Case("intel208", EM_INTEL208) 183 .Case("intel209", EM_INTEL209) 184 .Case("km32", EM_KM32) 185 .Case("kmx32", EM_KMX32) 186 .Case("kmx16", EM_KMX16) 187 .Case("kmx8", EM_KMX8) 188 .Case("kvarc", EM_KVARC) 189 .Case("cdp", EM_CDP) 190 .Case("coge", EM_COGE) 191 .Case("cool", EM_COOL) 192 .Case("norc", EM_NORC) 193 .Case("csr_kalimba", EM_CSR_KALIMBA) 194 .Case("amdgpu", EM_AMDGPU) 195 .Case("riscv", EM_RISCV) 196 .Case("lanai", EM_LANAI) 197 .Case("bpf", EM_BPF) 198 .Case("ve", EM_VE) 199 .Case("csky", EM_CSKY) 200 .Case("loongarch", EM_LOONGARCH) 201 .Default(EM_NONE); 202 } 203 204 /// Convert an ELF's e_machine value into an architecture name. 205 StringRef ELF::convertEMachineToArchName(uint16_t EMachine) { 206 switch (EMachine) { 207 case EM_NONE: 208 return "None"; 209 case EM_M32: 210 return "m32"; 211 case EM_SPARC: 212 return "sparc"; 213 case EM_386: 214 return "386"; 215 case EM_68K: 216 return "68k"; 217 case EM_88K: 218 return "88k"; 219 case EM_IAMCU: 220 return "iamcu"; 221 case EM_860: 222 return "860"; 223 case EM_MIPS: 224 return "mips"; 225 case EM_S370: 226 return "s370"; 227 case EM_MIPS_RS3_LE: 228 return "mips_rs3_le"; 229 case EM_PARISC: 230 return "parisc"; 231 case EM_VPP500: 232 return "vpp500"; 233 case EM_SPARC32PLUS: 234 return "sparc32plus"; 235 case EM_960: 236 return "960"; 237 case EM_PPC: 238 return "ppc"; 239 case EM_PPC64: 240 return "ppc64"; 241 case EM_S390: 242 return "s390"; 243 case EM_SPU: 244 return "spu"; 245 case EM_V800: 246 return "v800"; 247 case EM_FR20: 248 return "fr20"; 249 case EM_RH32: 250 return "rh32"; 251 case EM_RCE: 252 return "rce"; 253 case EM_ARM: 254 return "arm"; 255 case EM_ALPHA: 256 return "alpha"; 257 case EM_SH: 258 return "sh"; 259 case EM_SPARCV9: 260 return "sparcv9"; 261 case EM_TRICORE: 262 return "tricore"; 263 case EM_ARC: 264 return "arc"; 265 case EM_H8_300: 266 return "h8_300"; 267 case EM_H8_300H: 268 return "h8_300h"; 269 case EM_H8S: 270 return "h8s"; 271 case EM_H8_500: 272 return "h8_500"; 273 case EM_IA_64: 274 return "ia_64"; 275 case EM_MIPS_X: 276 return "mips_x"; 277 case EM_COLDFIRE: 278 return "coldfire"; 279 case EM_68HC12: 280 return "68hc12"; 281 case EM_MMA: 282 return "mma"; 283 case EM_PCP: 284 return "pcp"; 285 case EM_NCPU: 286 return "ncpu"; 287 case EM_NDR1: 288 return "ndr1"; 289 case EM_STARCORE: 290 return "starcore"; 291 case EM_ME16: 292 return "me16"; 293 case EM_ST100: 294 return "st100"; 295 case EM_TINYJ: 296 return "tinyj"; 297 case EM_X86_64: 298 return "x86_64"; 299 case EM_PDSP: 300 return "pdsp"; 301 case EM_PDP10: 302 return "pdp10"; 303 case EM_PDP11: 304 return "pdp11"; 305 case EM_FX66: 306 return "fx66"; 307 case EM_ST9PLUS: 308 return "st9plus"; 309 case EM_ST7: 310 return "st7"; 311 case EM_68HC16: 312 return "68hc16"; 313 case EM_68HC11: 314 return "68hc11"; 315 case EM_68HC08: 316 return "68hc08"; 317 case EM_68HC05: 318 return "68hc05"; 319 case EM_SVX: 320 return "svx"; 321 case EM_ST19: 322 return "st19"; 323 case EM_VAX: 324 return "vax"; 325 case EM_CRIS: 326 return "cris"; 327 case EM_JAVELIN: 328 return "javelin"; 329 case EM_FIREPATH: 330 return "firepath"; 331 case EM_ZSP: 332 return "zsp"; 333 case EM_MMIX: 334 return "mmix"; 335 case EM_HUANY: 336 return "huany"; 337 case EM_PRISM: 338 return "prism"; 339 case EM_AVR: 340 return "avr"; 341 case EM_FR30: 342 return "fr30"; 343 case EM_D10V: 344 return "d10v"; 345 case EM_D30V: 346 return "d30v"; 347 case EM_V850: 348 return "v850"; 349 case EM_M32R: 350 return "m32r"; 351 case EM_MN10300: 352 return "mn10300"; 353 case EM_MN10200: 354 return "mn10200"; 355 case EM_PJ: 356 return "pj"; 357 case EM_OPENRISC: 358 return "openrisc"; 359 case EM_ARC_COMPACT: 360 return "arc_compact"; 361 case EM_XTENSA: 362 return "xtensa"; 363 case EM_VIDEOCORE: 364 return "videocore"; 365 case EM_TMM_GPP: 366 return "tmm_gpp"; 367 case EM_NS32K: 368 return "ns32k"; 369 case EM_TPC: 370 return "tpc"; 371 case EM_SNP1K: 372 return "snp1k"; 373 case EM_ST200: 374 return "st200"; 375 case EM_IP2K: 376 return "ip2k"; 377 case EM_MAX: 378 return "max"; 379 case EM_CR: 380 return "cr"; 381 case EM_F2MC16: 382 return "f2mc16"; 383 case EM_MSP430: 384 return "msp430"; 385 case EM_BLACKFIN: 386 return "blackfin"; 387 case EM_SE_C33: 388 return "se_c33"; 389 case EM_SEP: 390 return "sep"; 391 case EM_ARCA: 392 return "arca"; 393 case EM_UNICORE: 394 return "unicore"; 395 case EM_EXCESS: 396 return "excess"; 397 case EM_DXP: 398 return "dxp"; 399 case EM_ALTERA_NIOS2: 400 return "altera_nios2"; 401 case EM_CRX: 402 return "crx"; 403 case EM_XGATE: 404 return "xgate"; 405 case EM_C166: 406 return "c166"; 407 case EM_M16C: 408 return "m16c"; 409 case EM_DSPIC30F: 410 return "dspic30f"; 411 case EM_CE: 412 return "ce"; 413 case EM_M32C: 414 return "m32c"; 415 case EM_TSK3000: 416 return "tsk3000"; 417 case EM_RS08: 418 return "rs08"; 419 case EM_SHARC: 420 return "sharc"; 421 case EM_ECOG2: 422 return "ecog2"; 423 case EM_SCORE7: 424 return "score7"; 425 case EM_DSP24: 426 return "dsp24"; 427 case EM_VIDEOCORE3: 428 return "videocore3"; 429 case EM_LATTICEMICO32: 430 return "latticemico32"; 431 case EM_SE_C17: 432 return "se_c17"; 433 case EM_TI_C6000: 434 return "ti_c6000"; 435 case EM_TI_C2000: 436 return "ti_c2000"; 437 case EM_TI_C5500: 438 return "ti_c5500"; 439 case EM_MMDSP_PLUS: 440 return "mmdsp_plus"; 441 case EM_CYPRESS_M8C: 442 return "cypress_m8c"; 443 case EM_R32C: 444 return "r32c"; 445 case EM_TRIMEDIA: 446 return "trimedia"; 447 case EM_HEXAGON: 448 return "hexagon"; 449 case EM_8051: 450 return "8051"; 451 case EM_STXP7X: 452 return "stxp7x"; 453 case EM_NDS32: 454 return "nds32"; 455 case EM_ECOG1: 456 return "ecog1"; 457 case EM_MAXQ30: 458 return "maxq30"; 459 case EM_XIMO16: 460 return "ximo16"; 461 case EM_MANIK: 462 return "manik"; 463 case EM_CRAYNV2: 464 return "craynv2"; 465 case EM_RX: 466 return "rx"; 467 case EM_METAG: 468 return "metag"; 469 case EM_MCST_ELBRUS: 470 return "mcst_elbrus"; 471 case EM_ECOG16: 472 return "ecog16"; 473 case EM_CR16: 474 return "cr16"; 475 case EM_ETPU: 476 return "etpu"; 477 case EM_SLE9X: 478 return "sle9x"; 479 case EM_L10M: 480 return "l10m"; 481 case EM_K10M: 482 return "k10m"; 483 case EM_AARCH64: 484 return "AArch64"; 485 case EM_AVR32: 486 return "avr32"; 487 case EM_STM8: 488 return "stm8"; 489 case EM_TILE64: 490 return "tile64"; 491 case EM_TILEPRO: 492 return "tilepro"; 493 case EM_CUDA: 494 return "cuda"; 495 case EM_TILEGX: 496 return "tilegx"; 497 case EM_CLOUDSHIELD: 498 return "cloudshield"; 499 case EM_COREA_1ST: 500 return "corea_1st"; 501 case EM_COREA_2ND: 502 return "corea_2nd"; 503 case EM_ARC_COMPACT2: 504 return "arc_compact2"; 505 case EM_OPEN8: 506 return "open8"; 507 case EM_RL78: 508 return "rl78"; 509 case EM_VIDEOCORE5: 510 return "videocore5"; 511 case EM_78KOR: 512 return "78kor"; 513 case EM_56800EX: 514 return "56800ex"; 515 case EM_BA1: 516 return "ba1"; 517 case EM_BA2: 518 return "ba2"; 519 case EM_XCORE: 520 return "xcore"; 521 case EM_MCHP_PIC: 522 return "mchp_pic"; 523 case EM_INTEL205: 524 return "intel205"; 525 case EM_INTEL206: 526 return "intel206"; 527 case EM_INTEL207: 528 return "intel207"; 529 case EM_INTEL208: 530 return "intel208"; 531 case EM_INTEL209: 532 return "intel209"; 533 case EM_KM32: 534 return "km32"; 535 case EM_KMX32: 536 return "kmx32"; 537 case EM_KMX16: 538 return "kmx16"; 539 case EM_KMX8: 540 return "kmx8"; 541 case EM_KVARC: 542 return "kvarc"; 543 case EM_CDP: 544 return "cdp"; 545 case EM_COGE: 546 return "coge"; 547 case EM_COOL: 548 return "cool"; 549 case EM_NORC: 550 return "norc"; 551 case EM_CSR_KALIMBA: 552 return "csr_kalimba"; 553 case EM_AMDGPU: 554 return "amdgpu"; 555 case EM_RISCV: 556 return "riscv"; 557 case EM_LANAI: 558 return "lanai"; 559 case EM_BPF: 560 return "bpf"; 561 case EM_VE: 562 return "ve"; 563 case EM_CSKY: 564 return "csky"; 565 case EM_LOONGARCH: 566 return "loongarch"; 567 default: 568 return "None"; 569 } 570 } 571