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