]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/ReaderWriter/ELF/ARM/ARMELFFile.h
Vendor import of lld trunk r233088:
[FreeBSD/FreeBSD.git] / lib / ReaderWriter / ELF / ARM / ARMELFFile.h
1 //===--------- lib/ReaderWriter/ELF/ARM/ARMELFFile.h ----------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #ifndef LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
11 #define LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
12
13 #include "ELFReader.h"
14
15 namespace lld {
16 namespace elf {
17
18 class ARMLinkingContext;
19
20 template <class ELFT> class ARMELFDefinedAtom : public ELFDefinedAtom<ELFT> {
21   typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
22   typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
23
24 public:
25   ARMELFDefinedAtom(const ELFFile<ELFT> &file, StringRef symbolName,
26                  StringRef sectionName, const Elf_Sym *symbol,
27                  const Elf_Shdr *section, ArrayRef<uint8_t> contentData,
28                  unsigned int referenceStart, unsigned int referenceEnd,
29                  std::vector<ELFReference<ELFT> *> &referenceList)
30       : ELFDefinedAtom<ELFT>(file, symbolName, sectionName, symbol, section,
31                              contentData, referenceStart, referenceEnd,
32                              referenceList) {}
33
34   bool isThumbFunc(const Elf_Sym *symbol) const {
35     return symbol->getType() == llvm::ELF::STT_FUNC &&
36         (static_cast<uint64_t>(symbol->st_value) & 0x1);
37   }
38
39   /// Correct st_value for symbols addressing Thumb instructions
40   /// by removing its zero bit.
41   uint64_t getSymbolValue(const Elf_Sym *symbol) const override {
42     const auto value = static_cast<uint64_t>(symbol->st_value);
43     return isThumbFunc(symbol) ? value & ~0x1 : value;
44   }
45
46   DefinedAtom::CodeModel codeModel() const override {
47     if (isThumbFunc(this->_symbol))
48       return DefinedAtom::codeARMThumb;
49     return DefinedAtom::codeNA;
50   }
51 };
52
53 template <class ELFT> class ARMELFFile : public ELFFile<ELFT> {
54 public:
55   ARMELFFile(std::unique_ptr<MemoryBuffer> mb, ARMLinkingContext &ctx)
56       : ELFFile<ELFT>(std::move(mb), ctx) {}
57
58   static ErrorOr<std::unique_ptr<ARMELFFile>>
59   create(std::unique_ptr<MemoryBuffer> mb, ARMLinkingContext &ctx) {
60     return std::unique_ptr<ARMELFFile<ELFT>>(
61         new ARMELFFile<ELFT>(std::move(mb), ctx));
62   }
63
64 private:
65   typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
66   typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
67
68   /// Correct st_value for symbols addressing Thumb instructions
69   /// by removing its zero bit.
70   uint64_t getSymbolValue(const Elf_Sym *symbol) const override {
71     const auto value = static_cast<uint64_t>(symbol->st_value);
72     return symbol->getType() == llvm::ELF::STT_FUNC ? value & ~0x1 : value;
73   }
74
75   /// Process the Defined symbol and create an atom for it.
76   ErrorOr<ELFDefinedAtom<ELFT> *> handleDefinedSymbol(StringRef symName,
77           StringRef sectionName,
78           const Elf_Sym *sym, const Elf_Shdr *sectionHdr,
79           ArrayRef<uint8_t> contentData,
80           unsigned int referenceStart, unsigned int referenceEnd,
81           std::vector<ELFReference<ELFT> *> &referenceList) override {
82     return new (this->_readerStorage) ARMELFDefinedAtom<ELFT>(
83         *this, symName, sectionName, sym, sectionHdr, contentData,
84         referenceStart, referenceEnd, referenceList);
85   }
86 };
87
88 template <class ELFT> class ARMDynamicFile : public DynamicFile<ELFT> {
89 public:
90   ARMDynamicFile(const ARMLinkingContext &context, StringRef name)
91       : DynamicFile<ELFT>(context, name) {}
92 };
93
94 } // elf
95 } // lld
96
97 #endif // LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H