ELFObjectFile.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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/Object/ELF.h"
  18. #include "llvm/Object/ELFTypes.h"
  19. #include "llvm/Object/Error.h"
  20. #include "llvm/Support/ARMAttributeParser.h"
  21. #include "llvm/Support/ARMBuildAttributes.h"
  22. #include "llvm/Support/Endian.h"
  23. #include "llvm/Support/ErrorHandling.h"
  24. #include "llvm/Support/MathExtras.h"
  25. #include "llvm/Support/TargetRegistry.h"
  26. #include <algorithm>
  27. #include <cstddef>
  28. #include <cstdint>
  29. #include <memory>
  30. #include <string>
  31. #include <system_error>
  32. #include <utility>
  33. using namespace llvm;
  34. using namespace object;
  35. const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
  36. {"None", "NOTYPE", ELF::STT_NOTYPE},
  37. {"Object", "OBJECT", ELF::STT_OBJECT},
  38. {"Function", "FUNC", ELF::STT_FUNC},
  39. {"Section", "SECTION", ELF::STT_SECTION},
  40. {"File", "FILE", ELF::STT_FILE},
  41. {"Common", "COMMON", ELF::STT_COMMON},
  42. {"TLS", "TLS", ELF::STT_TLS},
  43. {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC}};
  44. ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
  45. : ObjectFile(Type, Source) {}
  46. template <class ELFT>
  47. static Expected<std::unique_ptr<ELFObjectFile<ELFT>>>
  48. createPtr(MemoryBufferRef Object) {
  49. auto Ret = ELFObjectFile<ELFT>::create(Object);
  50. if (Error E = Ret.takeError())
  51. return std::move(E);
  52. return make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
  53. }
  54. Expected<std::unique_ptr<ObjectFile>>
  55. ObjectFile::createELFObjectFile(MemoryBufferRef Obj) {
  56. std::pair<unsigned char, unsigned char> Ident =
  57. getElfArchType(Obj.getBuffer());
  58. std::size_t MaxAlignment =
  59. 1ULL << countTrailingZeros(uintptr_t(Obj.getBufferStart()));
  60. if (MaxAlignment < 2)
  61. return createError("Insufficient alignment");
  62. if (Ident.first == ELF::ELFCLASS32) {
  63. if (Ident.second == ELF::ELFDATA2LSB)
  64. return createPtr<ELF32LE>(Obj);
  65. else if (Ident.second == ELF::ELFDATA2MSB)
  66. return createPtr<ELF32BE>(Obj);
  67. else
  68. return createError("Invalid ELF data");
  69. } else if (Ident.first == ELF::ELFCLASS64) {
  70. if (Ident.second == ELF::ELFDATA2LSB)
  71. return createPtr<ELF64LE>(Obj);
  72. else if (Ident.second == ELF::ELFDATA2MSB)
  73. return createPtr<ELF64BE>(Obj);
  74. else
  75. return createError("Invalid ELF data");
  76. }
  77. return createError("Invalid ELF class");
  78. }
  79. SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
  80. SubtargetFeatures Features;
  81. unsigned PlatformFlags = getPlatformFlags();
  82. switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
  83. case ELF::EF_MIPS_ARCH_1:
  84. break;
  85. case ELF::EF_MIPS_ARCH_2:
  86. Features.AddFeature("mips2");
  87. break;
  88. case ELF::EF_MIPS_ARCH_3:
  89. Features.AddFeature("mips3");
  90. break;
  91. case ELF::EF_MIPS_ARCH_4:
  92. Features.AddFeature("mips4");
  93. break;
  94. case ELF::EF_MIPS_ARCH_5:
  95. Features.AddFeature("mips5");
  96. break;
  97. case ELF::EF_MIPS_ARCH_32:
  98. Features.AddFeature("mips32");
  99. break;
  100. case ELF::EF_MIPS_ARCH_64:
  101. Features.AddFeature("mips64");
  102. break;
  103. case ELF::EF_MIPS_ARCH_32R2:
  104. Features.AddFeature("mips32r2");
  105. break;
  106. case ELF::EF_MIPS_ARCH_64R2:
  107. Features.AddFeature("mips64r2");
  108. break;
  109. case ELF::EF_MIPS_ARCH_32R6:
  110. Features.AddFeature("mips32r6");
  111. break;
  112. case ELF::EF_MIPS_ARCH_64R6:
  113. Features.AddFeature("mips64r6");
  114. break;
  115. default:
  116. llvm_unreachable("Unknown EF_MIPS_ARCH value");
  117. }
  118. switch (PlatformFlags & ELF::EF_MIPS_MACH) {
  119. case ELF::EF_MIPS_MACH_NONE:
  120. // No feature associated with this value.
  121. break;
  122. case ELF::EF_MIPS_MACH_OCTEON:
  123. Features.AddFeature("cnmips");
  124. break;
  125. default:
  126. llvm_unreachable("Unknown EF_MIPS_ARCH value");
  127. }
  128. if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
  129. Features.AddFeature("mips16");
  130. if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
  131. Features.AddFeature("micromips");
  132. return Features;
  133. }
  134. SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
  135. SubtargetFeatures Features;
  136. ARMAttributeParser Attributes;
  137. if (Error E = getBuildAttributes(Attributes))
  138. return SubtargetFeatures();
  139. // both ARMv7-M and R have to support thumb hardware div
  140. bool isV7 = false;
  141. if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch))
  142. isV7 = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch)
  143. == ARMBuildAttrs::v7;
  144. if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch_profile)) {
  145. switch(Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile)) {
  146. case ARMBuildAttrs::ApplicationProfile:
  147. Features.AddFeature("aclass");
  148. break;
  149. case ARMBuildAttrs::RealTimeProfile:
  150. Features.AddFeature("rclass");
  151. if (isV7)
  152. Features.AddFeature("hwdiv");
  153. break;
  154. case ARMBuildAttrs::MicroControllerProfile:
  155. Features.AddFeature("mclass");
  156. if (isV7)
  157. Features.AddFeature("hwdiv");
  158. break;
  159. }
  160. }
  161. if (Attributes.hasAttribute(ARMBuildAttrs::THUMB_ISA_use)) {
  162. switch(Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use)) {
  163. default:
  164. break;
  165. case ARMBuildAttrs::Not_Allowed:
  166. Features.AddFeature("thumb", false);
  167. Features.AddFeature("thumb2", false);
  168. break;
  169. case ARMBuildAttrs::AllowThumb32:
  170. Features.AddFeature("thumb2");
  171. break;
  172. }
  173. }
  174. if (Attributes.hasAttribute(ARMBuildAttrs::FP_arch)) {
  175. switch(Attributes.getAttributeValue(ARMBuildAttrs::FP_arch)) {
  176. default:
  177. break;
  178. case ARMBuildAttrs::Not_Allowed:
  179. Features.AddFeature("vfp2d16sp", false);
  180. Features.AddFeature("vfp3d16sp", false);
  181. Features.AddFeature("vfp4d16sp", false);
  182. break;
  183. case ARMBuildAttrs::AllowFPv2:
  184. Features.AddFeature("vfp2");
  185. break;
  186. case ARMBuildAttrs::AllowFPv3A:
  187. case ARMBuildAttrs::AllowFPv3B:
  188. Features.AddFeature("vfp3");
  189. break;
  190. case ARMBuildAttrs::AllowFPv4A:
  191. case ARMBuildAttrs::AllowFPv4B:
  192. Features.AddFeature("vfp4");
  193. break;
  194. }
  195. }
  196. if (Attributes.hasAttribute(ARMBuildAttrs::Advanced_SIMD_arch)) {
  197. switch(Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch)) {
  198. default:
  199. break;
  200. case ARMBuildAttrs::Not_Allowed:
  201. Features.AddFeature("neon", false);
  202. Features.AddFeature("fp16", false);
  203. break;
  204. case ARMBuildAttrs::AllowNeon:
  205. Features.AddFeature("neon");
  206. break;
  207. case ARMBuildAttrs::AllowNeon2:
  208. Features.AddFeature("neon");
  209. Features.AddFeature("fp16");
  210. break;
  211. }
  212. }
  213. if (Attributes.hasAttribute(ARMBuildAttrs::MVE_arch)) {
  214. switch(Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch)) {
  215. default:
  216. break;
  217. case ARMBuildAttrs::Not_Allowed:
  218. Features.AddFeature("mve", false);
  219. Features.AddFeature("mve.fp", false);
  220. break;
  221. case ARMBuildAttrs::AllowMVEInteger:
  222. Features.AddFeature("mve.fp", false);
  223. Features.AddFeature("mve");
  224. break;
  225. case ARMBuildAttrs::AllowMVEIntegerAndFloat:
  226. Features.AddFeature("mve.fp");
  227. break;
  228. }
  229. }
  230. if (Attributes.hasAttribute(ARMBuildAttrs::DIV_use)) {
  231. switch(Attributes.getAttributeValue(ARMBuildAttrs::DIV_use)) {
  232. default:
  233. break;
  234. case ARMBuildAttrs::DisallowDIV:
  235. Features.AddFeature("hwdiv", false);
  236. Features.AddFeature("hwdiv-arm", false);
  237. break;
  238. case ARMBuildAttrs::AllowDIVExt:
  239. Features.AddFeature("hwdiv");
  240. Features.AddFeature("hwdiv-arm");
  241. break;
  242. }
  243. }
  244. return Features;
  245. }
  246. SubtargetFeatures ELFObjectFileBase::getRISCVFeatures() const {
  247. SubtargetFeatures Features;
  248. unsigned PlatformFlags = getPlatformFlags();
  249. if (PlatformFlags & ELF::EF_RISCV_RVC) {
  250. Features.AddFeature("c");
  251. }
  252. return Features;
  253. }
  254. SubtargetFeatures ELFObjectFileBase::getFeatures() const {
  255. switch (getEMachine()) {
  256. case ELF::EM_MIPS:
  257. return getMIPSFeatures();
  258. case ELF::EM_ARM:
  259. return getARMFeatures();
  260. case ELF::EM_RISCV:
  261. return getRISCVFeatures();
  262. default:
  263. return SubtargetFeatures();
  264. }
  265. }
  266. // FIXME Encode from a tablegen description or target parser.
  267. void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
  268. if (TheTriple.getSubArch() != Triple::NoSubArch)
  269. return;
  270. ARMAttributeParser Attributes;
  271. if (Error E = getBuildAttributes(Attributes))
  272. return;
  273. std::string Triple;
  274. // Default to ARM, but use the triple if it's been set.
  275. if (TheTriple.isThumb())
  276. Triple = "thumb";
  277. else
  278. Triple = "arm";
  279. if (Attributes.hasAttribute(ARMBuildAttrs::CPU_arch)) {
  280. switch(Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch)) {
  281. case ARMBuildAttrs::v4:
  282. Triple += "v4";
  283. break;
  284. case ARMBuildAttrs::v4T:
  285. Triple += "v4t";
  286. break;
  287. case ARMBuildAttrs::v5T:
  288. Triple += "v5t";
  289. break;
  290. case ARMBuildAttrs::v5TE:
  291. Triple += "v5te";
  292. break;
  293. case ARMBuildAttrs::v5TEJ:
  294. Triple += "v5tej";
  295. break;
  296. case ARMBuildAttrs::v6:
  297. Triple += "v6";
  298. break;
  299. case ARMBuildAttrs::v6KZ:
  300. Triple += "v6kz";
  301. break;
  302. case ARMBuildAttrs::v6T2:
  303. Triple += "v6t2";
  304. break;
  305. case ARMBuildAttrs::v6K:
  306. Triple += "v6k";
  307. break;
  308. case ARMBuildAttrs::v7:
  309. Triple += "v7";
  310. break;
  311. case ARMBuildAttrs::v6_M:
  312. Triple += "v6m";
  313. break;
  314. case ARMBuildAttrs::v6S_M:
  315. Triple += "v6sm";
  316. break;
  317. case ARMBuildAttrs::v7E_M:
  318. Triple += "v7em";
  319. break;
  320. }
  321. }
  322. if (!isLittleEndian())
  323. Triple += "eb";
  324. TheTriple.setArchName(Triple);
  325. }
  326. std::vector<std::pair<DataRefImpl, uint64_t>>
  327. ELFObjectFileBase::getPltAddresses() const {
  328. std::string Err;
  329. const auto Triple = makeTriple();
  330. const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
  331. if (!T)
  332. return {};
  333. uint64_t JumpSlotReloc = 0;
  334. switch (Triple.getArch()) {
  335. case Triple::x86:
  336. JumpSlotReloc = ELF::R_386_JUMP_SLOT;
  337. break;
  338. case Triple::x86_64:
  339. JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
  340. break;
  341. case Triple::aarch64:
  342. JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
  343. break;
  344. default:
  345. return {};
  346. }
  347. std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
  348. std::unique_ptr<const MCInstrAnalysis> MIA(
  349. T->createMCInstrAnalysis(MII.get()));
  350. if (!MIA)
  351. return {};
  352. Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None;
  353. for (const SectionRef &Section : sections()) {
  354. StringRef Name;
  355. if (Section.getName(Name))
  356. continue;
  357. if (Name == ".plt")
  358. Plt = Section;
  359. else if (Name == ".rela.plt" || Name == ".rel.plt")
  360. RelaPlt = Section;
  361. else if (Name == ".got.plt")
  362. GotPlt = Section;
  363. }
  364. if (!Plt || !RelaPlt || !GotPlt)
  365. return {};
  366. Expected<StringRef> PltContents = Plt->getContents();
  367. if (!PltContents) {
  368. consumeError(PltContents.takeError());
  369. return {};
  370. }
  371. auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
  372. arrayRefFromStringRef(*PltContents),
  373. GotPlt->getAddress(), Triple);
  374. // Build a map from GOT entry virtual address to PLT entry virtual address.
  375. DenseMap<uint64_t, uint64_t> GotToPlt;
  376. for (const auto &Entry : PltEntries)
  377. GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
  378. // Find the relocations in the dynamic relocation table that point to
  379. // locations in the GOT for which we know the corresponding PLT entry.
  380. std::vector<std::pair<DataRefImpl, uint64_t>> Result;
  381. for (const auto &Relocation : RelaPlt->relocations()) {
  382. if (Relocation.getType() != JumpSlotReloc)
  383. continue;
  384. auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
  385. if (PltEntryIter != GotToPlt.end())
  386. Result.push_back(std::make_pair(
  387. Relocation.getSymbol()->getRawDataRefImpl(), PltEntryIter->second));
  388. }
  389. return Result;
  390. }