xref: /freebsd/contrib/llvm-project/llvm/lib/BinaryFormat/ELF.cpp (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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/DenseMap.h"
11 #include "llvm/ADT/StringSwitch.h"
12 #include "llvm/Support/Error.h"
13 
14 using namespace llvm;
15 using namespace ELF;
16 
17 /// Convert an architecture name into ELF's e_machine value.
18 uint16_t ELF::convertArchNameToEMachine(StringRef Arch) {
19   std::string LowerArch = Arch.lower();
20   return StringSwitch<uint16_t>(LowerArch)
21       .Case("none", EM_NONE)
22       .Case("m32", EM_M32)
23       .Case("sparc", EM_SPARC)
24       .Case("386", EM_386)
25       .Case("68k", EM_68K)
26       .Case("88k", EM_88K)
27       .Case("iamcu", EM_IAMCU)
28       .Case("860", EM_860)
29       .Case("mips", EM_MIPS)
30       .Case("s370", EM_S370)
31       .Case("mips_rs3_le", EM_MIPS_RS3_LE)
32       .Case("parisc", EM_PARISC)
33       .Case("vpp500", EM_VPP500)
34       .Case("sparc32plus", EM_SPARC32PLUS)
35       .Case("960", EM_960)
36       .Case("ppc", EM_PPC)
37       .Case("ppc64", EM_PPC64)
38       .Case("s390", EM_S390)
39       .Case("spu", EM_SPU)
40       .Case("v800", EM_V800)
41       .Case("fr20", EM_FR20)
42       .Case("rh32", EM_RH32)
43       .Case("rce", EM_RCE)
44       .Case("arm", EM_ARM)
45       .Case("alpha", EM_ALPHA)
46       .Case("sh", EM_SH)
47       .Case("sparcv9", EM_SPARCV9)
48       .Case("tricore", EM_TRICORE)
49       .Case("arc", EM_ARC)
50       .Case("h8_300", EM_H8_300)
51       .Case("h8_300h", EM_H8_300H)
52       .Case("h8s", EM_H8S)
53       .Case("h8_500", EM_H8_500)
54       .Case("ia_64", EM_IA_64)
55       .Case("mips_x", EM_MIPS_X)
56       .Case("coldfire", EM_COLDFIRE)
57       .Case("68hc12", EM_68HC12)
58       .Case("mma", EM_MMA)
59       .Case("pcp", EM_PCP)
60       .Case("ncpu", EM_NCPU)
61       .Case("ndr1", EM_NDR1)
62       .Case("starcore", EM_STARCORE)
63       .Case("me16", EM_ME16)
64       .Case("st100", EM_ST100)
65       .Case("tinyj", EM_TINYJ)
66       .Case("x86_64", EM_X86_64)
67       .Case("pdsp", EM_PDSP)
68       .Case("pdp10", EM_PDP10)
69       .Case("pdp11", EM_PDP11)
70       .Case("fx66", EM_FX66)
71       .Case("st9plus", EM_ST9PLUS)
72       .Case("st7", EM_ST7)
73       .Case("68hc16", EM_68HC16)
74       .Case("68hc11", EM_68HC11)
75       .Case("68hc08", EM_68HC08)
76       .Case("68hc05", EM_68HC05)
77       .Case("svx", EM_SVX)
78       .Case("st19", EM_ST19)
79       .Case("vax", EM_VAX)
80       .Case("cris", EM_CRIS)
81       .Case("javelin", EM_JAVELIN)
82       .Case("firepath", EM_FIREPATH)
83       .Case("zsp", EM_ZSP)
84       .Case("mmix", EM_MMIX)
85       .Case("huany", EM_HUANY)
86       .Case("prism", EM_PRISM)
87       .Case("avr", EM_AVR)
88       .Case("fr30", EM_FR30)
89       .Case("d10v", EM_D10V)
90       .Case("d30v", EM_D30V)
91       .Case("v850", EM_V850)
92       .Case("m32r", EM_M32R)
93       .Case("mn10300", EM_MN10300)
94       .Case("mn10200", EM_MN10200)
95       .Case("pj", EM_PJ)
96       .Case("openrisc", EM_OPENRISC)
97       .Case("arc_compact", EM_ARC_COMPACT)
98       .Case("xtensa", EM_XTENSA)
99       .Case("videocore", EM_VIDEOCORE)
100       .Case("tmm_gpp", EM_TMM_GPP)
101       .Case("ns32k", EM_NS32K)
102       .Case("tpc", EM_TPC)
103       .Case("snp1k", EM_SNP1K)
104       .Case("st200", EM_ST200)
105       .Case("ip2k", EM_IP2K)
106       .Case("max", EM_MAX)
107       .Case("cr", EM_CR)
108       .Case("f2mc16", EM_F2MC16)
109       .Case("msp430", EM_MSP430)
110       .Case("blackfin", EM_BLACKFIN)
111       .Case("se_c33", EM_SE_C33)
112       .Case("sep", EM_SEP)
113       .Case("arca", EM_ARCA)
114       .Case("unicore", EM_UNICORE)
115       .Case("excess", EM_EXCESS)
116       .Case("dxp", EM_DXP)
117       .Case("altera_nios2", EM_ALTERA_NIOS2)
118       .Case("crx", EM_CRX)
119       .Case("xgate", EM_XGATE)
120       .Case("c166", EM_C166)
121       .Case("m16c", EM_M16C)
122       .Case("dspic30f", EM_DSPIC30F)
123       .Case("ce", EM_CE)
124       .Case("m32c", EM_M32C)
125       .Case("tsk3000", EM_TSK3000)
126       .Case("rs08", EM_RS08)
127       .Case("sharc", EM_SHARC)
128       .Case("ecog2", EM_ECOG2)
129       .Case("score7", EM_SCORE7)
130       .Case("dsp24", EM_DSP24)
131       .Case("videocore3", EM_VIDEOCORE3)
132       .Case("latticemico32", EM_LATTICEMICO32)
133       .Case("se_c17", EM_SE_C17)
134       .Case("ti_c6000", EM_TI_C6000)
135       .Case("ti_c2000", EM_TI_C2000)
136       .Case("ti_c5500", EM_TI_C5500)
137       .Case("mmdsp_plus", EM_MMDSP_PLUS)
138       .Case("cypress_m8c", EM_CYPRESS_M8C)
139       .Case("r32c", EM_R32C)
140       .Case("trimedia", EM_TRIMEDIA)
141       .Case("hexagon", EM_HEXAGON)
142       .Case("8051", EM_8051)
143       .Case("stxp7x", EM_STXP7X)
144       .Case("nds32", EM_NDS32)
145       .Case("ecog1", EM_ECOG1)
146       .Case("ecog1x", EM_ECOG1X)
147       .Case("maxq30", EM_MAXQ30)
148       .Case("ximo16", EM_XIMO16)
149       .Case("manik", EM_MANIK)
150       .Case("craynv2", EM_CRAYNV2)
151       .Case("rx", EM_RX)
152       .Case("metag", EM_METAG)
153       .Case("mcst_elbrus", EM_MCST_ELBRUS)
154       .Case("ecog16", EM_ECOG16)
155       .Case("cr16", EM_CR16)
156       .Case("etpu", EM_ETPU)
157       .Case("sle9x", EM_SLE9X)
158       .Case("l10m", EM_L10M)
159       .Case("k10m", EM_K10M)
160       .Case("aarch64", EM_AARCH64)
161       .Case("avr32", EM_AVR32)
162       .Case("stm8", EM_STM8)
163       .Case("tile64", EM_TILE64)
164       .Case("tilepro", EM_TILEPRO)
165       .Case("cuda", EM_CUDA)
166       .Case("tilegx", EM_TILEGX)
167       .Case("cloudshield", EM_CLOUDSHIELD)
168       .Case("corea_1st", EM_COREA_1ST)
169       .Case("corea_2nd", EM_COREA_2ND)
170       .Case("arc_compact2", EM_ARC_COMPACT2)
171       .Case("open8", EM_OPEN8)
172       .Case("rl78", EM_RL78)
173       .Case("videocore5", EM_VIDEOCORE5)
174       .Case("78kor", EM_78KOR)
175       .Case("56800ex", EM_56800EX)
176       .Case("ba1", EM_BA1)
177       .Case("ba2", EM_BA2)
178       .Case("xcore", EM_XCORE)
179       .Case("mchp_pic", EM_MCHP_PIC)
180       .Case("intel205", EM_INTEL205)
181       .Case("intel206", EM_INTEL206)
182       .Case("intel207", EM_INTEL207)
183       .Case("intel208", EM_INTEL208)
184       .Case("intel209", EM_INTEL209)
185       .Case("km32", EM_KM32)
186       .Case("kmx32", EM_KMX32)
187       .Case("kmx16", EM_KMX16)
188       .Case("kmx8", EM_KMX8)
189       .Case("kvarc", EM_KVARC)
190       .Case("cdp", EM_CDP)
191       .Case("coge", EM_COGE)
192       .Case("cool", EM_COOL)
193       .Case("norc", EM_NORC)
194       .Case("csr_kalimba", EM_CSR_KALIMBA)
195       .Case("amdgpu", EM_AMDGPU)
196       .Case("riscv", EM_RISCV)
197       .Case("lanai", EM_LANAI)
198       .Case("bpf", EM_BPF)
199       .Case("ve", EM_VE)
200       .Case("csky", EM_CSKY)
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   default:
566     return "None";
567   }
568 }
569