1 //===--------- lib/ReaderWriter/ELF/ARM/ARMELFFile.h ----------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
11 #define LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
13 #include "ELFReader.h"
18 class ARMLinkingContext;
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;
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,
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);
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;
46 DefinedAtom::CodeModel codeModel() const override {
47 if (isThumbFunc(this->_symbol))
48 return DefinedAtom::codeARMThumb;
49 return DefinedAtom::codeNA;
53 template <class ELFT> class ARMELFFile : public ELFFile<ELFT> {
55 ARMELFFile(std::unique_ptr<MemoryBuffer> mb, ARMLinkingContext &ctx)
56 : ELFFile<ELFT>(std::move(mb), ctx) {}
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));
65 typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
66 typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;
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;
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);
88 template <class ELFT> class ARMDynamicFile : public DynamicFile<ELFT> {
90 ARMDynamicFile(const ARMLinkingContext &context, StringRef name)
91 : DynamicFile<ELFT>(context, name) {}
97 #endif // LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H