ELF.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  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. .Default(EM_NONE);
  198. }
  199. /// Convert an ELF's e_machine value into an architecture name.
  200. StringRef ELF::convertEMachineToArchName(uint16_t EMachine) {
  201. switch (EMachine) {
  202. case EM_NONE:
  203. return "None";
  204. case EM_M32:
  205. return "m32";
  206. case EM_SPARC:
  207. return "sparc";
  208. case EM_386:
  209. return "386";
  210. case EM_68K:
  211. return "68k";
  212. case EM_88K:
  213. return "88k";
  214. case EM_IAMCU:
  215. return "iamcu";
  216. case EM_860:
  217. return "860";
  218. case EM_MIPS:
  219. return "mips";
  220. case EM_S370:
  221. return "s370";
  222. case EM_MIPS_RS3_LE:
  223. return "mips_rs3_le";
  224. case EM_PARISC:
  225. return "parisc";
  226. case EM_VPP500:
  227. return "vpp500";
  228. case EM_SPARC32PLUS:
  229. return "sparc32plus";
  230. case EM_960:
  231. return "960";
  232. case EM_PPC:
  233. return "ppc";
  234. case EM_PPC64:
  235. return "ppc64";
  236. case EM_S390:
  237. return "s390";
  238. case EM_SPU:
  239. return "spu";
  240. case EM_V800:
  241. return "v800";
  242. case EM_FR20:
  243. return "fr20";
  244. case EM_RH32:
  245. return "rh32";
  246. case EM_RCE:
  247. return "rce";
  248. case EM_ARM:
  249. return "arm";
  250. case EM_ALPHA:
  251. return "alpha";
  252. case EM_SH:
  253. return "sh";
  254. case EM_SPARCV9:
  255. return "sparcv9";
  256. case EM_TRICORE:
  257. return "tricore";
  258. case EM_ARC:
  259. return "arc";
  260. case EM_H8_300:
  261. return "h8_300";
  262. case EM_H8_300H:
  263. return "h8_300h";
  264. case EM_H8S:
  265. return "h8s";
  266. case EM_H8_500:
  267. return "h8_500";
  268. case EM_IA_64:
  269. return "ia_64";
  270. case EM_MIPS_X:
  271. return "mips_x";
  272. case EM_COLDFIRE:
  273. return "coldfire";
  274. case EM_68HC12:
  275. return "68hc12";
  276. case EM_MMA:
  277. return "mma";
  278. case EM_PCP:
  279. return "pcp";
  280. case EM_NCPU:
  281. return "ncpu";
  282. case EM_NDR1:
  283. return "ndr1";
  284. case EM_STARCORE:
  285. return "starcore";
  286. case EM_ME16:
  287. return "me16";
  288. case EM_ST100:
  289. return "st100";
  290. case EM_TINYJ:
  291. return "tinyj";
  292. case EM_X86_64:
  293. return "x86_64";
  294. case EM_PDSP:
  295. return "pdsp";
  296. case EM_PDP10:
  297. return "pdp10";
  298. case EM_PDP11:
  299. return "pdp11";
  300. case EM_FX66:
  301. return "fx66";
  302. case EM_ST9PLUS:
  303. return "st9plus";
  304. case EM_ST7:
  305. return "st7";
  306. case EM_68HC16:
  307. return "68hc16";
  308. case EM_68HC11:
  309. return "68hc11";
  310. case EM_68HC08:
  311. return "68hc08";
  312. case EM_68HC05:
  313. return "68hc05";
  314. case EM_SVX:
  315. return "svx";
  316. case EM_ST19:
  317. return "st19";
  318. case EM_VAX:
  319. return "vax";
  320. case EM_CRIS:
  321. return "cris";
  322. case EM_JAVELIN:
  323. return "javelin";
  324. case EM_FIREPATH:
  325. return "firepath";
  326. case EM_ZSP:
  327. return "zsp";
  328. case EM_MMIX:
  329. return "mmix";
  330. case EM_HUANY:
  331. return "huany";
  332. case EM_PRISM:
  333. return "prism";
  334. case EM_AVR:
  335. return "avr";
  336. case EM_FR30:
  337. return "fr30";
  338. case EM_D10V:
  339. return "d10v";
  340. case EM_D30V:
  341. return "d30v";
  342. case EM_V850:
  343. return "v850";
  344. case EM_M32R:
  345. return "m32r";
  346. case EM_MN10300:
  347. return "mn10300";
  348. case EM_MN10200:
  349. return "mn10200";
  350. case EM_PJ:
  351. return "pj";
  352. case EM_OPENRISC:
  353. return "openrisc";
  354. case EM_ARC_COMPACT:
  355. return "arc_compact";
  356. case EM_XTENSA:
  357. return "xtensa";
  358. case EM_VIDEOCORE:
  359. return "videocore";
  360. case EM_TMM_GPP:
  361. return "tmm_gpp";
  362. case EM_NS32K:
  363. return "ns32k";
  364. case EM_TPC:
  365. return "tpc";
  366. case EM_SNP1K:
  367. return "snp1k";
  368. case EM_ST200:
  369. return "st200";
  370. case EM_IP2K:
  371. return "ip2k";
  372. case EM_MAX:
  373. return "max";
  374. case EM_CR:
  375. return "cr";
  376. case EM_F2MC16:
  377. return "f2mc16";
  378. case EM_MSP430:
  379. return "msp430";
  380. case EM_BLACKFIN:
  381. return "blackfin";
  382. case EM_SE_C33:
  383. return "se_c33";
  384. case EM_SEP:
  385. return "sep";
  386. case EM_ARCA:
  387. return "arca";
  388. case EM_UNICORE:
  389. return "unicore";
  390. case EM_EXCESS:
  391. return "excess";
  392. case EM_DXP:
  393. return "dxp";
  394. case EM_ALTERA_NIOS2:
  395. return "altera_nios2";
  396. case EM_CRX:
  397. return "crx";
  398. case EM_XGATE:
  399. return "xgate";
  400. case EM_C166:
  401. return "c166";
  402. case EM_M16C:
  403. return "m16c";
  404. case EM_DSPIC30F:
  405. return "dspic30f";
  406. case EM_CE:
  407. return "ce";
  408. case EM_M32C:
  409. return "m32c";
  410. case EM_TSK3000:
  411. return "tsk3000";
  412. case EM_RS08:
  413. return "rs08";
  414. case EM_SHARC:
  415. return "sharc";
  416. case EM_ECOG2:
  417. return "ecog2";
  418. case EM_SCORE7:
  419. return "score7";
  420. case EM_DSP24:
  421. return "dsp24";
  422. case EM_VIDEOCORE3:
  423. return "videocore3";
  424. case EM_LATTICEMICO32:
  425. return "latticemico32";
  426. case EM_SE_C17:
  427. return "se_c17";
  428. case EM_TI_C6000:
  429. return "ti_c6000";
  430. case EM_TI_C2000:
  431. return "ti_c2000";
  432. case EM_TI_C5500:
  433. return "ti_c5500";
  434. case EM_MMDSP_PLUS:
  435. return "mmdsp_plus";
  436. case EM_CYPRESS_M8C:
  437. return "cypress_m8c";
  438. case EM_R32C:
  439. return "r32c";
  440. case EM_TRIMEDIA:
  441. return "trimedia";
  442. case EM_HEXAGON:
  443. return "hexagon";
  444. case EM_8051:
  445. return "8051";
  446. case EM_STXP7X:
  447. return "stxp7x";
  448. case EM_NDS32:
  449. return "nds32";
  450. case EM_ECOG1:
  451. return "ecog1";
  452. case EM_MAXQ30:
  453. return "maxq30";
  454. case EM_XIMO16:
  455. return "ximo16";
  456. case EM_MANIK:
  457. return "manik";
  458. case EM_CRAYNV2:
  459. return "craynv2";
  460. case EM_RX:
  461. return "rx";
  462. case EM_METAG:
  463. return "metag";
  464. case EM_MCST_ELBRUS:
  465. return "mcst_elbrus";
  466. case EM_ECOG16:
  467. return "ecog16";
  468. case EM_CR16:
  469. return "cr16";
  470. case EM_ETPU:
  471. return "etpu";
  472. case EM_SLE9X:
  473. return "sle9x";
  474. case EM_L10M:
  475. return "l10m";
  476. case EM_K10M:
  477. return "k10m";
  478. case EM_AARCH64:
  479. return "AArch64";
  480. case EM_AVR32:
  481. return "avr32";
  482. case EM_STM8:
  483. return "stm8";
  484. case EM_TILE64:
  485. return "tile64";
  486. case EM_TILEPRO:
  487. return "tilepro";
  488. case EM_CUDA:
  489. return "cuda";
  490. case EM_TILEGX:
  491. return "tilegx";
  492. case EM_CLOUDSHIELD:
  493. return "cloudshield";
  494. case EM_COREA_1ST:
  495. return "corea_1st";
  496. case EM_COREA_2ND:
  497. return "corea_2nd";
  498. case EM_ARC_COMPACT2:
  499. return "arc_compact2";
  500. case EM_OPEN8:
  501. return "open8";
  502. case EM_RL78:
  503. return "rl78";
  504. case EM_VIDEOCORE5:
  505. return "videocore5";
  506. case EM_78KOR:
  507. return "78kor";
  508. case EM_56800EX:
  509. return "56800ex";
  510. case EM_BA1:
  511. return "ba1";
  512. case EM_BA2:
  513. return "ba2";
  514. case EM_XCORE:
  515. return "xcore";
  516. case EM_MCHP_PIC:
  517. return "mchp_pic";
  518. case EM_INTEL205:
  519. return "intel205";
  520. case EM_INTEL206:
  521. return "intel206";
  522. case EM_INTEL207:
  523. return "intel207";
  524. case EM_INTEL208:
  525. return "intel208";
  526. case EM_INTEL209:
  527. return "intel209";
  528. case EM_KM32:
  529. return "km32";
  530. case EM_KMX32:
  531. return "kmx32";
  532. case EM_KMX16:
  533. return "kmx16";
  534. case EM_KMX8:
  535. return "kmx8";
  536. case EM_KVARC:
  537. return "kvarc";
  538. case EM_CDP:
  539. return "cdp";
  540. case EM_COGE:
  541. return "coge";
  542. case EM_COOL:
  543. return "cool";
  544. case EM_NORC:
  545. return "norc";
  546. case EM_CSR_KALIMBA:
  547. return "csr_kalimba";
  548. case EM_AMDGPU:
  549. return "amdgpu";
  550. case EM_RISCV:
  551. return "riscv";
  552. case EM_LANAI:
  553. return "lanai";
  554. case EM_BPF:
  555. return "bpf";
  556. case EM_VE:
  557. return "ve";
  558. case EM_CSKY:
  559. return "csky";
  560. default:
  561. return "None";
  562. }
  563. }