xref: /freebsd/contrib/llvm-project/llvm/lib/BinaryFormat/ELF.cpp (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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       .Default(EM_NONE);
201 }
202 
203 /// Convert an ELF's e_machine value into an architecture name.
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   default:
565     return "None";
566   }
567 }
568