ELF.cpp 13 KB

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