ELFObjectFile.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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/Endian.h"
  24. #include "llvm/Support/ErrorHandling.h"
  25. #include "llvm/Support/MathExtras.h"
  26. #include "llvm/Support/RISCVAttributeParser.h"
  27. #include "llvm/Support/RISCVAttributes.h"
  28. #include <algorithm>
  29. #include <cstddef>
  30. #include <cstdint>
  31. #include <memory>
  32. #include <string>
  33. #include <system_error>
  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. Optional<unsigned> Attr =
  156. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
  157. if (Attr.hasValue())
  158. isV7 = Attr.getValue() == ARMBuildAttrs::v7;
  159. Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
  160. if (Attr.hasValue()) {
  161. switch (Attr.getValue()) {
  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.hasValue()) {
  179. switch (Attr.getValue()) {
  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.hasValue()) {
  193. switch (Attr.getValue()) {
  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.hasValue()) {
  216. switch (Attr.getValue()) {
  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.hasValue()) {
  234. switch (Attr.getValue()) {
  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.hasValue()) {
  252. switch (Attr.getValue()) {
  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. SubtargetFeatures ELFObjectFileBase::getRISCVFeatures() const {
  268. SubtargetFeatures Features;
  269. unsigned PlatformFlags = getPlatformFlags();
  270. if (PlatformFlags & ELF::EF_RISCV_RVC) {
  271. Features.AddFeature("c");
  272. }
  273. // Add features according to the ELF attribute section.
  274. // If there are any unrecognized features, ignore them.
  275. RISCVAttributeParser Attributes;
  276. if (Error E = getBuildAttributes(Attributes)) {
  277. // TODO Propagate Error.
  278. consumeError(std::move(E));
  279. return Features; // Keep "c" feature if there is one in PlatformFlags.
  280. }
  281. Optional<StringRef> Attr = Attributes.getAttributeString(RISCVAttrs::ARCH);
  282. if (Attr.hasValue()) {
  283. // The Arch pattern is [rv32|rv64][i|e]version(_[m|a|f|d|c]version)*
  284. // Version string pattern is (major)p(minor). Major and minor are optional.
  285. // For example, a version number could be 2p0, 2, or p92.
  286. StringRef Arch = Attr.getValue();
  287. if (Arch.consume_front("rv32"))
  288. Features.AddFeature("64bit", false);
  289. else if (Arch.consume_front("rv64"))
  290. Features.AddFeature("64bit");
  291. while (!Arch.empty()) {
  292. switch (Arch[0]) {
  293. default:
  294. break; // Ignore unexpected features.
  295. case 'i':
  296. Features.AddFeature("e", false);
  297. break;
  298. case 'd':
  299. Features.AddFeature("f"); // D-ext will imply F-ext.
  300. LLVM_FALLTHROUGH;
  301. case 'e':
  302. case 'm':
  303. case 'a':
  304. case 'f':
  305. case 'c':
  306. Features.AddFeature(Arch.take_front());
  307. break;
  308. }
  309. // FIXME: Handle version numbers.
  310. Arch = Arch.drop_until([](char c) { return c == '_' || c == '\0'; });
  311. Arch = Arch.drop_while([](char c) { return c == '_'; });
  312. }
  313. }
  314. return Features;
  315. }
  316. SubtargetFeatures ELFObjectFileBase::getFeatures() const {
  317. switch (getEMachine()) {
  318. case ELF::EM_MIPS:
  319. return getMIPSFeatures();
  320. case ELF::EM_ARM:
  321. return getARMFeatures();
  322. case ELF::EM_RISCV:
  323. return getRISCVFeatures();
  324. default:
  325. return SubtargetFeatures();
  326. }
  327. }
  328. Optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
  329. switch (getEMachine()) {
  330. case ELF::EM_AMDGPU:
  331. return getAMDGPUCPUName();
  332. default:
  333. return None;
  334. }
  335. }
  336. StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
  337. assert(getEMachine() == ELF::EM_AMDGPU);
  338. unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
  339. switch (CPU) {
  340. // Radeon HD 2000/3000 Series (R600).
  341. case ELF::EF_AMDGPU_MACH_R600_R600:
  342. return "r600";
  343. case ELF::EF_AMDGPU_MACH_R600_R630:
  344. return "r630";
  345. case ELF::EF_AMDGPU_MACH_R600_RS880:
  346. return "rs880";
  347. case ELF::EF_AMDGPU_MACH_R600_RV670:
  348. return "rv670";
  349. // Radeon HD 4000 Series (R700).
  350. case ELF::EF_AMDGPU_MACH_R600_RV710:
  351. return "rv710";
  352. case ELF::EF_AMDGPU_MACH_R600_RV730:
  353. return "rv730";
  354. case ELF::EF_AMDGPU_MACH_R600_RV770:
  355. return "rv770";
  356. // Radeon HD 5000 Series (Evergreen).
  357. case ELF::EF_AMDGPU_MACH_R600_CEDAR:
  358. return "cedar";
  359. case ELF::EF_AMDGPU_MACH_R600_CYPRESS:
  360. return "cypress";
  361. case ELF::EF_AMDGPU_MACH_R600_JUNIPER:
  362. return "juniper";
  363. case ELF::EF_AMDGPU_MACH_R600_REDWOOD:
  364. return "redwood";
  365. case ELF::EF_AMDGPU_MACH_R600_SUMO:
  366. return "sumo";
  367. // Radeon HD 6000 Series (Northern Islands).
  368. case ELF::EF_AMDGPU_MACH_R600_BARTS:
  369. return "barts";
  370. case ELF::EF_AMDGPU_MACH_R600_CAICOS:
  371. return "caicos";
  372. case ELF::EF_AMDGPU_MACH_R600_CAYMAN:
  373. return "cayman";
  374. case ELF::EF_AMDGPU_MACH_R600_TURKS:
  375. return "turks";
  376. // AMDGCN GFX6.
  377. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600:
  378. return "gfx600";
  379. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601:
  380. return "gfx601";
  381. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602:
  382. return "gfx602";
  383. // AMDGCN GFX7.
  384. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700:
  385. return "gfx700";
  386. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701:
  387. return "gfx701";
  388. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702:
  389. return "gfx702";
  390. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703:
  391. return "gfx703";
  392. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704:
  393. return "gfx704";
  394. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705:
  395. return "gfx705";
  396. // AMDGCN GFX8.
  397. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801:
  398. return "gfx801";
  399. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802:
  400. return "gfx802";
  401. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803:
  402. return "gfx803";
  403. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805:
  404. return "gfx805";
  405. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810:
  406. return "gfx810";
  407. // AMDGCN GFX9.
  408. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900:
  409. return "gfx900";
  410. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902:
  411. return "gfx902";
  412. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904:
  413. return "gfx904";
  414. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906:
  415. return "gfx906";
  416. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908:
  417. return "gfx908";
  418. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909:
  419. return "gfx909";
  420. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A:
  421. return "gfx90a";
  422. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C:
  423. return "gfx90c";
  424. // AMDGCN GFX10.
  425. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010:
  426. return "gfx1010";
  427. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011:
  428. return "gfx1011";
  429. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012:
  430. return "gfx1012";
  431. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013:
  432. return "gfx1013";
  433. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030:
  434. return "gfx1030";
  435. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031:
  436. return "gfx1031";
  437. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032:
  438. return "gfx1032";
  439. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033:
  440. return "gfx1033";
  441. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034:
  442. return "gfx1034";
  443. case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035:
  444. return "gfx1035";
  445. default:
  446. llvm_unreachable("Unknown EF_AMDGPU_MACH value");
  447. }
  448. }
  449. // FIXME Encode from a tablegen description or target parser.
  450. void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
  451. if (TheTriple.getSubArch() != Triple::NoSubArch)
  452. return;
  453. ARMAttributeParser Attributes;
  454. if (Error E = getBuildAttributes(Attributes)) {
  455. // TODO Propagate Error.
  456. consumeError(std::move(E));
  457. return;
  458. }
  459. std::string Triple;
  460. // Default to ARM, but use the triple if it's been set.
  461. if (TheTriple.isThumb())
  462. Triple = "thumb";
  463. else
  464. Triple = "arm";
  465. Optional<unsigned> Attr =
  466. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
  467. if (Attr.hasValue()) {
  468. switch (Attr.getValue()) {
  469. case ARMBuildAttrs::v4:
  470. Triple += "v4";
  471. break;
  472. case ARMBuildAttrs::v4T:
  473. Triple += "v4t";
  474. break;
  475. case ARMBuildAttrs::v5T:
  476. Triple += "v5t";
  477. break;
  478. case ARMBuildAttrs::v5TE:
  479. Triple += "v5te";
  480. break;
  481. case ARMBuildAttrs::v5TEJ:
  482. Triple += "v5tej";
  483. break;
  484. case ARMBuildAttrs::v6:
  485. Triple += "v6";
  486. break;
  487. case ARMBuildAttrs::v6KZ:
  488. Triple += "v6kz";
  489. break;
  490. case ARMBuildAttrs::v6T2:
  491. Triple += "v6t2";
  492. break;
  493. case ARMBuildAttrs::v6K:
  494. Triple += "v6k";
  495. break;
  496. case ARMBuildAttrs::v7: {
  497. Optional<unsigned> ArchProfileAttr =
  498. Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
  499. if (ArchProfileAttr.hasValue() &&
  500. ArchProfileAttr.getValue() == ARMBuildAttrs::MicroControllerProfile)
  501. Triple += "v7m";
  502. else
  503. Triple += "v7";
  504. break;
  505. }
  506. case ARMBuildAttrs::v6_M:
  507. Triple += "v6m";
  508. break;
  509. case ARMBuildAttrs::v6S_M:
  510. Triple += "v6sm";
  511. break;
  512. case ARMBuildAttrs::v7E_M:
  513. Triple += "v7em";
  514. break;
  515. case ARMBuildAttrs::v8_A:
  516. Triple += "v8a";
  517. break;
  518. case ARMBuildAttrs::v8_R:
  519. Triple += "v8r";
  520. break;
  521. case ARMBuildAttrs::v8_M_Base:
  522. Triple += "v8m.base";
  523. break;
  524. case ARMBuildAttrs::v8_M_Main:
  525. Triple += "v8m.main";
  526. break;
  527. case ARMBuildAttrs::v8_1_M_Main:
  528. Triple += "v8.1m.main";
  529. break;
  530. }
  531. }
  532. if (!isLittleEndian())
  533. Triple += "eb";
  534. TheTriple.setArchName(Triple);
  535. }
  536. std::vector<std::pair<Optional<DataRefImpl>, uint64_t>>
  537. ELFObjectFileBase::getPltAddresses() const {
  538. std::string Err;
  539. const auto Triple = makeTriple();
  540. const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
  541. if (!T)
  542. return {};
  543. uint64_t JumpSlotReloc = 0;
  544. switch (Triple.getArch()) {
  545. case Triple::x86:
  546. JumpSlotReloc = ELF::R_386_JUMP_SLOT;
  547. break;
  548. case Triple::x86_64:
  549. JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
  550. break;
  551. case Triple::aarch64:
  552. case Triple::aarch64_be:
  553. JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
  554. break;
  555. default:
  556. return {};
  557. }
  558. std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
  559. std::unique_ptr<const MCInstrAnalysis> MIA(
  560. T->createMCInstrAnalysis(MII.get()));
  561. if (!MIA)
  562. return {};
  563. Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None;
  564. for (const SectionRef &Section : sections()) {
  565. Expected<StringRef> NameOrErr = Section.getName();
  566. if (!NameOrErr) {
  567. consumeError(NameOrErr.takeError());
  568. continue;
  569. }
  570. StringRef Name = *NameOrErr;
  571. if (Name == ".plt")
  572. Plt = Section;
  573. else if (Name == ".rela.plt" || Name == ".rel.plt")
  574. RelaPlt = Section;
  575. else if (Name == ".got.plt")
  576. GotPlt = Section;
  577. }
  578. if (!Plt || !RelaPlt || !GotPlt)
  579. return {};
  580. Expected<StringRef> PltContents = Plt->getContents();
  581. if (!PltContents) {
  582. consumeError(PltContents.takeError());
  583. return {};
  584. }
  585. auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
  586. arrayRefFromStringRef(*PltContents),
  587. GotPlt->getAddress(), Triple);
  588. // Build a map from GOT entry virtual address to PLT entry virtual address.
  589. DenseMap<uint64_t, uint64_t> GotToPlt;
  590. for (const auto &Entry : PltEntries)
  591. GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
  592. // Find the relocations in the dynamic relocation table that point to
  593. // locations in the GOT for which we know the corresponding PLT entry.
  594. std::vector<std::pair<Optional<DataRefImpl>, uint64_t>> Result;
  595. for (const auto &Relocation : RelaPlt->relocations()) {
  596. if (Relocation.getType() != JumpSlotReloc)
  597. continue;
  598. auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
  599. if (PltEntryIter != GotToPlt.end()) {
  600. symbol_iterator Sym = Relocation.getSymbol();
  601. if (Sym == symbol_end())
  602. Result.emplace_back(None, PltEntryIter->second);
  603. else
  604. Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second);
  605. }
  606. }
  607. return Result;
  608. }
  609. template <class ELFT>
  610. static Expected<std::vector<VersionEntry>>
  611. readDynsymVersionsImpl(const ELFFile<ELFT> &EF,
  612. ELFObjectFileBase::elf_symbol_iterator_range Symbols) {
  613. using Elf_Shdr = typename ELFT::Shdr;
  614. const Elf_Shdr *VerSec = nullptr;
  615. const Elf_Shdr *VerNeedSec = nullptr;
  616. const Elf_Shdr *VerDefSec = nullptr;
  617. // The user should ensure sections() can't fail here.
  618. for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
  619. if (Sec.sh_type == ELF::SHT_GNU_versym)
  620. VerSec = &Sec;
  621. else if (Sec.sh_type == ELF::SHT_GNU_verdef)
  622. VerDefSec = &Sec;
  623. else if (Sec.sh_type == ELF::SHT_GNU_verneed)
  624. VerNeedSec = &Sec;
  625. }
  626. if (!VerSec)
  627. return std::vector<VersionEntry>();
  628. Expected<SmallVector<Optional<VersionEntry>, 0>> MapOrErr =
  629. EF.loadVersionMap(VerNeedSec, VerDefSec);
  630. if (!MapOrErr)
  631. return MapOrErr.takeError();
  632. std::vector<VersionEntry> Ret;
  633. size_t I = 0;
  634. for (const ELFSymbolRef &Sym : Symbols) {
  635. ++I;
  636. Expected<const typename ELFT::Versym *> VerEntryOrErr =
  637. EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
  638. if (!VerEntryOrErr)
  639. return createError("unable to read an entry with index " + Twine(I) +
  640. " from " + describe(EF, *VerSec) + ": " +
  641. toString(VerEntryOrErr.takeError()));
  642. Expected<uint32_t> FlagsOrErr = Sym.getFlags();
  643. if (!FlagsOrErr)
  644. return createError("unable to read flags for symbol with index " +
  645. Twine(I) + ": " + toString(FlagsOrErr.takeError()));
  646. bool IsDefault;
  647. Expected<StringRef> VerOrErr = EF.getSymbolVersionByIndex(
  648. (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
  649. (*FlagsOrErr) & SymbolRef::SF_Undefined);
  650. if (!VerOrErr)
  651. return createError("unable to get a version for entry " + Twine(I) +
  652. " of " + describe(EF, *VerSec) + ": " +
  653. toString(VerOrErr.takeError()));
  654. Ret.push_back({(*VerOrErr).str(), IsDefault});
  655. }
  656. return Ret;
  657. }
  658. Expected<std::vector<VersionEntry>>
  659. ELFObjectFileBase::readDynsymVersions() const {
  660. elf_symbol_iterator_range Symbols = getDynamicSymbolIterators();
  661. if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
  662. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  663. if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
  664. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  665. if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
  666. return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
  667. return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
  668. Symbols);
  669. }