ELFObjectFile.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  1. //===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
  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. // Part of the ELFObjectFile class implementation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/ELFObjectFile.h"
  13. #include "llvm/ADT/Triple.h"
  14. #include "llvm/BinaryFormat/ELF.h"
  15. #include "llvm/MC/MCInstrAnalysis.h"
  16. #include "llvm/MC/SubtargetFeature.h"
  17. #include "llvm/MC/TargetRegistry.h"
  18. #include "llvm/Object/ELF.h"
  19. #include "llvm/Object/ELFTypes.h"
  20. #include "llvm/Object/Error.h"
  21. #include "llvm/Support/ARMAttributeParser.h"
  22. #include "llvm/Support/ARMBuildAttributes.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/RISCVAttributeParser.h"
  26. #include "llvm/Support/RISCVAttributes.h"
  27. #include "llvm/Support/RISCVISAInfo.h"
  28. #include <algorithm>
  29. #include <cstddef>
  30. #include <cstdint>
  31. #include <memory>
  32. #include <optional>
  33. #include <string>
  34. #include <utility>
  35. using namespace llvm;
  36. using namespace object;
  37. const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
  38. {"None", "NOTYPE", ELF::STT_NOTYPE},
  39. {"Object", "OBJECT", ELF::STT_OBJECT},
  40. {"Function", "FUNC", ELF::STT_FUNC},
  41. {"Section", "SECTION", ELF::STT_SECTION},
  42. {"File", "FILE", ELF::STT_FILE},
  43. {"Common", "COMMON", ELF::STT_COMMON},
  44. {"TLS", "TLS", ELF::STT_TLS},
  45. {"Unknown", "<unknown>: 7", 7},
  46. {"Unknown", "<unknown>: 8", 8},
  47. {"Unknown", "<unknown>: 9", 9},
  48. {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
  49. {"OS Specific", "<OS specific>: 11", 11},
  50. {"OS Specific", "<OS specific>: 12", 12},
  51. {"Proc Specific", "<processor specific>: 13", 13},
  52. {"Proc Specific", "<processor specific>: 14", 14},
  53. {"Proc Specific", "<processor specific>: 15", 15}
  54. };
  55. ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
  56. : ObjectFile(Type, Source) {}
  57. template <class ELFT>
  58. static Expected<std::unique_ptr<ELFObjectFile<ELFT>>>
  59. createPtr(MemoryBufferRef Object, bool InitContent) {
  60. auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
  61. if (Error E = Ret.takeError())
  62. return std::move(E);
  63. return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
  64. }
  65. Expected<std::unique_ptr<ObjectFile>>
  66. ObjectFile::createELFObjectFile(MemoryBufferRef Obj, bool InitContent) {
  67. std::pair<unsigned char, unsigned char> Ident =
  68. getElfArchType(Obj.getBuffer());
  69. std::size_t MaxAlignment =
  70. 1ULL << countTrailingZeros(
  71. reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
  72. if (MaxAlignment < 2)
  73. return createError("Insufficient alignment");
  74. if (Ident.first == ELF::ELFCLASS32) {
  75. if (Ident.second == ELF::ELFDATA2LSB)
  76. return createPtr<ELF32LE>(Obj, InitContent);
  77. else if (Ident.second == ELF::ELFDATA2MSB)
  78. return createPtr<ELF32BE>(Obj, InitContent);
  79. else
  80. return createError("Invalid ELF data");
  81. } else if (Ident.first == ELF::ELFCLASS64) {
  82. if (Ident.second == ELF::ELFDATA2LSB)
  83. return createPtr<ELF64LE>(Obj, InitContent);
  84. else if (Ident.second == ELF::ELFDATA2MSB)
  85. return createPtr<ELF64BE>(Obj, InitContent);
  86. else
  87. return createError("Invalid ELF data");
  88. }
  89. return createError("Invalid ELF class");
  90. }
  91. SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
  92. SubtargetFeatures Features;
  93. unsigned PlatformFlags = getPlatformFlags();
  94. switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
  95. case ELF::EF_MIPS_ARCH_1:
  96. break;
  97. case ELF::EF_MIPS_ARCH_2:
  98. Features.AddFeature("mips2");
  99. break;
  100. case ELF::EF_MIPS_ARCH_3:
  101. Features.AddFeature("mips3");
  102. break;
  103. case ELF::EF_MIPS_ARCH_4:
  104. Features.AddFeature("mips4");
  105. break;
  106. case ELF::EF_MIPS_ARCH_5:
  107. Features.AddFeature("mips5");
  108. break;
  109. case ELF::EF_MIPS_ARCH_32:
  110. Features.AddFeature("mips32");
  111. break;
  112. case ELF::EF_MIPS_ARCH_64:
  113. Features.AddFeature("mips64");
  114. break;
  115. case ELF::EF_MIPS_ARCH_32R2:
  116. Features.AddFeature("mips32r2");
  117. break;
  118. case ELF::EF_MIPS_ARCH_64R2:
  119. Features.AddFeature("mips64r2");
  120. break;
  121. case ELF::EF_MIPS_ARCH_32R6:
  122. Features.AddFeature("mips32r6");
  123. break;
  124. case ELF::EF_MIPS_ARCH_64R6:
  125. Features.AddFeature("mips64r6");
  126. break;
  127. default:
  128. llvm_unreachable("Unknown EF_MIPS_ARCH value");
  129. }
  130. switch (PlatformFlags & ELF::EF_MIPS_MACH) {
  131. case ELF::EF_MIPS_MACH_NONE:
  132. // No feature associated with this value.
  133. break;
  134. case ELF::EF_MIPS_MACH_OCTEON:
  135. Features.AddFeature("cnmips");
  136. break;
  137. default:
  138. llvm_unreachable("Unknown EF_MIPS_ARCH value");
  139. }
  140. if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
  141. Features.AddFeature("mips16");
  142. if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
  143. Features.AddFeature("micromips");
  144. return Features;
  145. }
  146. SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
  147. SubtargetFeatures Features;
  148. ARMAttributeParser Attributes;
  149. if (Error E = getBuildAttributes(Attributes)) {
  150. consumeError(std::move(E));
  151. return SubtargetFeatures();
  152. }
  153. // both ARMv7-M and R have to support thumb hardware div
  154. bool isV7 = false;
  155. std::optional<unsigned> Attr =
  156. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
  157. if (Attr)
  158. isV7 = *Attr == ARMBuildAttrs::v7;
  159. Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
  160. if (Attr) {
  161. switch (*Attr) {
  162. case ARMBuildAttrs::ApplicationProfile:
  163. Features.AddFeature("aclass");
  164. break;
  165. case ARMBuildAttrs::RealTimeProfile:
  166. Features.AddFeature("rclass");
  167. if (isV7)
  168. Features.AddFeature("hwdiv");
  169. break;
  170. case ARMBuildAttrs::MicroControllerProfile:
  171. Features.AddFeature("mclass");
  172. if (isV7)
  173. Features.AddFeature("hwdiv");
  174. break;
  175. }
  176. }
  177. Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
  178. if (Attr) {
  179. switch (*Attr) {
  180. default:
  181. break;
  182. case ARMBuildAttrs::Not_Allowed:
  183. Features.AddFeature("thumb", false);
  184. Features.AddFeature("thumb2", false);
  185. break;
  186. case ARMBuildAttrs::AllowThumb32:
  187. Features.AddFeature("thumb2");
  188. break;
  189. }
  190. }
  191. Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
  192. if (Attr) {
  193. switch (*Attr) {
  194. default:
  195. break;
  196. case ARMBuildAttrs::Not_Allowed:
  197. Features.AddFeature("vfp2sp", false);
  198. Features.AddFeature("vfp3d16sp", false);
  199. Features.AddFeature("vfp4d16sp", false);
  200. break;
  201. case ARMBuildAttrs::AllowFPv2:
  202. Features.AddFeature("vfp2");
  203. break;
  204. case ARMBuildAttrs::AllowFPv3A:
  205. case ARMBuildAttrs::AllowFPv3B:
  206. Features.AddFeature("vfp3");
  207. break;
  208. case ARMBuildAttrs::AllowFPv4A:
  209. case ARMBuildAttrs::AllowFPv4B:
  210. Features.AddFeature("vfp4");
  211. break;
  212. }
  213. }
  214. Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
  215. if (Attr) {
  216. switch (*Attr) {
  217. default:
  218. break;
  219. case ARMBuildAttrs::Not_Allowed:
  220. Features.AddFeature("neon", false);
  221. Features.AddFeature("fp16", false);
  222. break;
  223. case ARMBuildAttrs::AllowNeon:
  224. Features.AddFeature("neon");
  225. break;
  226. case ARMBuildAttrs::AllowNeon2:
  227. Features.AddFeature("neon");
  228. Features.AddFeature("fp16");
  229. break;
  230. }
  231. }
  232. Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
  233. if (Attr) {
  234. switch (*Attr) {
  235. default:
  236. break;
  237. case ARMBuildAttrs::Not_Allowed:
  238. Features.AddFeature("mve", false);
  239. Features.AddFeature("mve.fp", false);
  240. break;
  241. case ARMBuildAttrs::AllowMVEInteger:
  242. Features.AddFeature("mve.fp", false);
  243. Features.AddFeature("mve");
  244. break;
  245. case ARMBuildAttrs::AllowMVEIntegerAndFloat:
  246. Features.AddFeature("mve.fp");
  247. break;
  248. }
  249. }
  250. Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
  251. if (Attr) {
  252. switch (*Attr) {
  253. default:
  254. break;
  255. case ARMBuildAttrs::DisallowDIV:
  256. Features.AddFeature("hwdiv", false);
  257. Features.AddFeature("hwdiv-arm", false);
  258. break;
  259. case ARMBuildAttrs::AllowDIVExt:
  260. Features.AddFeature("hwdiv");
  261. Features.AddFeature("hwdiv-arm");
  262. break;
  263. }
  264. }
  265. return Features;
  266. }
  267. Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
  268. SubtargetFeatures Features;
  269. unsigned PlatformFlags = getPlatformFlags();
  270. if (PlatformFlags & ELF::EF_RISCV_RVC) {
  271. Features.AddFeature("c");
  272. }
  273. RISCVAttributeParser Attributes;
  274. if (Error E = getBuildAttributes(Attributes)) {
  275. return std::move(E);
  276. }
  277. std::optional<StringRef> Attr =
  278. Attributes.getAttributeString(RISCVAttrs::ARCH);
  279. if (Attr) {
  280. // Suppress version checking for experimental extensions to prevent erroring
  281. // when getting any unknown version of experimental extension.
  282. auto ParseResult = RISCVISAInfo::parseArchString(
  283. *Attr, /*EnableExperimentalExtension=*/true,
  284. /*ExperimentalExtensionVersionCheck=*/false,
  285. /*IgnoreUnknown=*/true);
  286. if (!ParseResult)
  287. return ParseResult.takeError();
  288. auto &ISAInfo = *ParseResult;
  289. if (ISAInfo->getXLen() == 32)
  290. Features.AddFeature("64bit", false);
  291. else if (ISAInfo->getXLen() == 64)
  292. Features.AddFeature("64bit");
  293. else
  294. llvm_unreachable("XLEN should be 32 or 64.");
  295. Features.addFeaturesVector(ISAInfo->toFeatureVector());
  296. }
  297. return Features;
  298. }
  299. SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
  300. SubtargetFeatures Features;
  301. switch (getPlatformFlags() & ELF::EF_LOONGARCH_ABI_MODIFIER_MASK) {
  302. case ELF::EF_LOONGARCH_ABI_SOFT_FLOAT:
  303. break;
  304. case ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT:
  305. Features.AddFeature("d");
  306. // D implies F according to LoongArch ISA spec.
  307. [[fallthrough]];
  308. case ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT:
  309. Features.AddFeature("f");
  310. break;
  311. }
  312. return Features;
  313. }
  314. Expected<SubtargetFeatures> ELFObjectFileBase::getFeatures() const {
  315. switch (getEMachine()) {
  316. case ELF::EM_MIPS:
  317. return getMIPSFeatures();
  318. case ELF::EM_ARM:
  319. return getARMFeatures();
  320. case ELF::EM_RISCV:
  321. return getRISCVFeatures();
  322. case ELF::EM_LOONGARCH:
  323. return getLoongArchFeatures();
  324. default:
  325. return SubtargetFeatures();
  326. }
  327. }
  328. std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
  329. switch (getEMachine()) {
  330. case ELF::EM_AMDGPU:
  331. return getAMDGPUCPUName();
  332. case ELF::EM_PPC64:
  333. return StringRef("future");
  334. default:
  335. return std::nullopt;
  336. }
  337. }
  338. StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
  339. assert(getEMachine() == ELF::EM_AMDGPU);
  340. unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
  341. switch (CPU) {
  342. // Radeon HD 2000/3000 Series (R600).
  343. case ELF::EF_AMDGPU_MACH_R600_R600:
  344. return "r600";
  345. case ELF::EF_AMDGPU_MACH_R600_R630:
  346. return "r630";
  347. case ELF::EF_AMDGPU_MACH_R600_RS880:
  348. return "rs880";
  349. case ELF::EF_AMDGPU_MACH_R600_RV670:
  350. return "rv670";
  351. // Radeon HD 4000 Series (R700).
  352. case ELF::EF_AMDGPU_MACH_R600_RV710:
  353. return "rv710";
  354. case ELF::EF_AMDGPU_MACH_R600_RV730:
  355. return "rv730";
  356. case ELF::EF_AMDGPU_MACH_R600_RV770:
  357. return "rv770";
  358. // Radeon HD 5000 Series (Evergreen).
  359. case ELF::EF_AMDGPU_MACH_R600_CEDAR:
  360. return "cedar";
  361. case ELF::EF_AMDGPU_MACH_R600_CYPRESS:
  362. return "cypress";
  363. case ELF::EF_AMDGPU_MACH_R600_JUNIPER:
  364. return "juniper";
  365. case ELF::EF_AMDGPU_MACH_R600_REDWOOD:
  366. return "redwood";
  367. case ELF::EF_AMDGPU_MACH_R600_SUMO:
  368. return "sumo";
  369. // Radeon HD 6000 Series (Northern Islands).
  370. case ELF::EF_AMDGPU_MACH_R600_BARTS:
  371. return "barts";
  372. case ELF::EF_AMDGPU_MACH_R600_CAICOS:
  373. return "caicos";
  374. case ELF::EF_AMDGPU_MACH_R600_CAYMAN:
  375. return "cayman";
  376. case ELF::EF_AMDGPU_MACH_R600_TURKS:
  377. return "turks";
  378. // AMDGCN GFX6.
  379. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600:
  380. return "gfx600";
  381. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601:
  382. return "gfx601";
  383. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602:
  384. return "gfx602";
  385. // AMDGCN GFX7.
  386. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700:
  387. return "gfx700";
  388. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701:
  389. return "gfx701";
  390. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702:
  391. return "gfx702";
  392. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703:
  393. return "gfx703";
  394. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704:
  395. return "gfx704";
  396. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705:
  397. return "gfx705";
  398. // AMDGCN GFX8.
  399. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801:
  400. return "gfx801";
  401. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802:
  402. return "gfx802";
  403. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803:
  404. return "gfx803";
  405. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805:
  406. return "gfx805";
  407. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810:
  408. return "gfx810";
  409. // AMDGCN GFX9.
  410. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900:
  411. return "gfx900";
  412. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902:
  413. return "gfx902";
  414. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904:
  415. return "gfx904";
  416. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906:
  417. return "gfx906";
  418. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908:
  419. return "gfx908";
  420. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909:
  421. return "gfx909";
  422. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A:
  423. return "gfx90a";
  424. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C:
  425. return "gfx90c";
  426. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX940:
  427. return "gfx940";
  428. // AMDGCN GFX10.
  429. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010:
  430. return "gfx1010";
  431. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011:
  432. return "gfx1011";
  433. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012:
  434. return "gfx1012";
  435. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013:
  436. return "gfx1013";
  437. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030:
  438. return "gfx1030";
  439. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031:
  440. return "gfx1031";
  441. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032:
  442. return "gfx1032";
  443. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033:
  444. return "gfx1033";
  445. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034:
  446. return "gfx1034";
  447. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035:
  448. return "gfx1035";
  449. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036:
  450. return "gfx1036";
  451. // AMDGCN GFX11.
  452. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100:
  453. return "gfx1100";
  454. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101:
  455. return "gfx1101";
  456. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102:
  457. return "gfx1102";
  458. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103:
  459. return "gfx1103";
  460. default:
  461. llvm_unreachable("Unknown EF_AMDGPU_MACH value");
  462. }
  463. }
  464. // FIXME Encode from a tablegen description or target parser.
  465. void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
  466. if (TheTriple.getSubArch() != Triple::NoSubArch)
  467. return;
  468. ARMAttributeParser Attributes;
  469. if (Error E = getBuildAttributes(Attributes)) {
  470. // TODO Propagate Error.
  471. consumeError(std::move(E));
  472. return;
  473. }
  474. std::string Triple;
  475. // Default to ARM, but use the triple if it's been set.
  476. if (TheTriple.isThumb())
  477. Triple = "thumb";
  478. else
  479. Triple = "arm";
  480. std::optional<unsigned> Attr =
  481. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
  482. if (Attr) {
  483. switch (*Attr) {
  484. case ARMBuildAttrs::v4:
  485. Triple += "v4";
  486. break;
  487. case ARMBuildAttrs::v4T:
  488. Triple += "v4t";
  489. break;
  490. case ARMBuildAttrs::v5T:
  491. Triple += "v5t";
  492. break;
  493. case ARMBuildAttrs::v5TE:
  494. Triple += "v5te";
  495. break;
  496. case ARMBuildAttrs::v5TEJ:
  497. Triple += "v5tej";
  498. break;
  499. case ARMBuildAttrs::v6:
  500. Triple += "v6";
  501. break;
  502. case ARMBuildAttrs::v6KZ:
  503. Triple += "v6kz";
  504. break;
  505. case ARMBuildAttrs::v6T2:
  506. Triple += "v6t2";
  507. break;
  508. case ARMBuildAttrs::v6K:
  509. Triple += "v6k";
  510. break;
  511. case ARMBuildAttrs::v7: {
  512. std::optional<unsigned> ArchProfileAttr =
  513. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
  514. if (ArchProfileAttr &&
  515. *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
  516. Triple += "v7m";
  517. else
  518. Triple += "v7";
  519. break;
  520. }
  521. case ARMBuildAttrs::v6_M:
  522. Triple += "v6m";
  523. break;
  524. case ARMBuildAttrs::v6S_M:
  525. Triple += "v6sm";
  526. break;
  527. case ARMBuildAttrs::v7E_M:
  528. Triple += "v7em";
  529. break;
  530. case ARMBuildAttrs::v8_A:
  531. Triple += "v8a";
  532. break;
  533. case ARMBuildAttrs::v8_R:
  534. Triple += "v8r";
  535. break;
  536. case ARMBuildAttrs::v8_M_Base:
  537. Triple += "v8m.base";
  538. break;
  539. case ARMBuildAttrs::v8_M_Main:
  540. Triple += "v8m.main";
  541. break;
  542. case ARMBuildAttrs::v8_1_M_Main:
  543. Triple += "v8.1m.main";
  544. break;
  545. case ARMBuildAttrs::v9_A:
  546. Triple += "v9a";
  547. break;
  548. }
  549. }
  550. if (!isLittleEndian())
  551. Triple += "eb";
  552. TheTriple.setArchName(Triple);
  553. }
  554. std::vector<std::pair<std::optional<DataRefImpl>, uint64_t>>
  555. ELFObjectFileBase::getPltAddresses() const {
  556. std::string Err;
  557. const auto Triple = makeTriple();
  558. const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
  559. if (!T)
  560. return {};
  561. uint64_t JumpSlotReloc = 0;
  562. switch (Triple.getArch()) {
  563. case Triple::x86:
  564. JumpSlotReloc = ELF::R_386_JUMP_SLOT;
  565. break;
  566. case Triple::x86_64:
  567. JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
  568. break;
  569. case Triple::aarch64:
  570. case Triple::aarch64_be:
  571. JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
  572. break;
  573. default:
  574. return {};
  575. }
  576. std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
  577. std::unique_ptr<const MCInstrAnalysis> MIA(
  578. T->createMCInstrAnalysis(MII.get()));
  579. if (!MIA)
  580. return {};
  581. std::optional<SectionRef> Plt, RelaPlt, GotPlt;
  582. for (const SectionRef &Section : sections()) {
  583. Expected<StringRef> NameOrErr = Section.getName();
  584. if (!NameOrErr) {
  585. consumeError(NameOrErr.takeError());
  586. continue;
  587. }
  588. StringRef Name = *NameOrErr;
  589. if (Name == ".plt")
  590. Plt = Section;
  591. else if (Name == ".rela.plt" || Name == ".rel.plt")
  592. RelaPlt = Section;
  593. else if (Name == ".got.plt")
  594. GotPlt = Section;
  595. }
  596. if (!Plt || !RelaPlt || !GotPlt)
  597. return {};
  598. Expected<StringRef> PltContents = Plt->getContents();
  599. if (!PltContents) {
  600. consumeError(PltContents.takeError());
  601. return {};
  602. }
  603. auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
  604. arrayRefFromStringRef(*PltContents),
  605. GotPlt->getAddress(), Triple);
  606. // Build a map from GOT entry virtual address to PLT entry virtual address.
  607. DenseMap<uint64_t, uint64_t> GotToPlt;
  608. for (const auto &Entry : PltEntries)
  609. GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
  610. // Find the relocations in the dynamic relocation table that point to
  611. // locations in the GOT for which we know the corresponding PLT entry.
  612. std::vector<std::pair<std::optional<DataRefImpl>, uint64_t>> Result;
  613. for (const auto &Relocation : RelaPlt->relocations()) {
  614. if (Relocation.getType() != JumpSlotReloc)
  615. continue;
  616. auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
  617. if (PltEntryIter != GotToPlt.end()) {
  618. symbol_iterator Sym = Relocation.getSymbol();
  619. if (Sym == symbol_end())
  620. Result.emplace_back(std::nullopt, PltEntryIter->second);
  621. else
  622. Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second);
  623. }
  624. }
  625. return Result;
  626. }
  627. template <class ELFT>
  628. Expected<std::vector<BBAddrMap>> static readBBAddrMapImpl(
  629. const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex) {
  630. using Elf_Shdr = typename ELFT::Shdr;
  631. std::vector<BBAddrMap> BBAddrMaps;
  632. const auto &Sections = cantFail(EF.sections());
  633. for (const Elf_Shdr &Sec : Sections) {
  634. if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
  635. Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
  636. continue;
  637. if (TextSectionIndex) {
  638. Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
  639. if (!TextSecOrErr)
  640. return createError("unable to get the linked-to section for " +
  641. describe(EF, Sec) + ": " +
  642. toString(TextSecOrErr.takeError()));
  643. if (*TextSectionIndex != std::distance(Sections.begin(), *TextSecOrErr))
  644. continue;
  645. }
  646. Expected<std::vector<BBAddrMap>> BBAddrMapOrErr = EF.decodeBBAddrMap(Sec);
  647. if (!BBAddrMapOrErr)
  648. return createError("unable to read " + describe(EF, Sec) + ": " +
  649. toString(BBAddrMapOrErr.takeError()));
  650. std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
  651. std::back_inserter(BBAddrMaps));
  652. }
  653. return BBAddrMaps;
  654. }
  655. template <class ELFT>
  656. static Expected<std::vector<VersionEntry>>
  657. readDynsymVersionsImpl(const ELFFile<ELFT> &EF,
  658. ELFObjectFileBase::elf_symbol_iterator_range Symbols) {
  659. using Elf_Shdr = typename ELFT::Shdr;
  660. const Elf_Shdr *VerSec = nullptr;
  661. const Elf_Shdr *VerNeedSec = nullptr;
  662. const Elf_Shdr *VerDefSec = nullptr;
  663. // The user should ensure sections() can't fail here.
  664. for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
  665. if (Sec.sh_type == ELF::SHT_GNU_versym)
  666. VerSec = &Sec;
  667. else if (Sec.sh_type == ELF::SHT_GNU_verdef)
  668. VerDefSec = &Sec;
  669. else if (Sec.sh_type == ELF::SHT_GNU_verneed)
  670. VerNeedSec = &Sec;
  671. }
  672. if (!VerSec)
  673. return std::vector<VersionEntry>();
  674. Expected<SmallVector<std::optional<VersionEntry>, 0>> MapOrErr =
  675. EF.loadVersionMap(VerNeedSec, VerDefSec);
  676. if (!MapOrErr)
  677. return MapOrErr.takeError();
  678. std::vector<VersionEntry> Ret;
  679. size_t I = 0;
  680. for (const ELFSymbolRef &Sym : Symbols) {
  681. ++I;
  682. Expected<const typename ELFT::Versym *> VerEntryOrErr =
  683. EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
  684. if (!VerEntryOrErr)
  685. return createError("unable to read an entry with index " + Twine(I) +
  686. " from " + describe(EF, *VerSec) + ": " +
  687. toString(VerEntryOrErr.takeError()));
  688. Expected<uint32_t> FlagsOrErr = Sym.getFlags();
  689. if (!FlagsOrErr)
  690. return createError("unable to read flags for symbol with index " +
  691. Twine(I) + ": " + toString(FlagsOrErr.takeError()));
  692. bool IsDefault;
  693. Expected<StringRef> VerOrErr = EF.getSymbolVersionByIndex(
  694. (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
  695. (*FlagsOrErr) & SymbolRef::SF_Undefined);
  696. if (!VerOrErr)
  697. return createError("unable to get a version for entry " + Twine(I) +
  698. " of " + describe(EF, *VerSec) + ": " +
  699. toString(VerOrErr.takeError()));
  700. Ret.push_back({(*VerOrErr).str(), IsDefault});
  701. }
  702. return Ret;
  703. }
  704. Expected<std::vector<VersionEntry>>
  705. ELFObjectFileBase::readDynsymVersions() const {
  706. elf_symbol_iterator_range Symbols = getDynamicSymbolIterators();
  707. if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
  708. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  709. if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
  710. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  711. if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
  712. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  713. return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
  714. Symbols);
  715. }
  716. Expected<std::vector<BBAddrMap>> ELFObjectFileBase::readBBAddrMap(
  717. std::optional<unsigned> TextSectionIndex) const {
  718. if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
  719. return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
  720. if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
  721. return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
  722. if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
  723. return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
  724. if (const auto *Obj = cast<ELF64BEObjectFile>(this))
  725. return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
  726. else
  727. llvm_unreachable("Unsupported binary format");
  728. }