summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/ARM/ARMELFFile.h
blob: 5490219fde99b4d2167ed2dc3ee0cf66f35b0514 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
//===--------- lib/ReaderWriter/ELF/ARM/ARMELFFile.h ----------------------===//
//
//                             The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//

#ifndef LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
#define LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H

#include "ELFReader.h"

namespace lld {
namespace elf {

class ARMLinkingContext;

template <class ELFT, DefinedAtom::CodeModel Model>
class ARMELFMappingAtom : public ELFDefinedAtom<ELFT> {
public:
  template<typename... T>
  ARMELFMappingAtom(T&&... args)
      : ELFDefinedAtom<ELFT>(std::forward<T>(args)...) {}

  DefinedAtom::CodeModel codeModel() const override {
    return Model;
  }
};

template <class ELFT> class ARMELFDefinedAtom : public ELFDefinedAtom<ELFT> {
public:
  template<typename... T>
  ARMELFDefinedAtom(T&&... args)
      : ELFDefinedAtom<ELFT>(std::forward<T>(args)...) {}

  bool isThumbFunc() const {
    const auto* symbol = this->_symbol;
    return symbol->getType() == llvm::ELF::STT_FUNC &&
        (static_cast<uint64_t>(symbol->st_value) & 0x1);
  }

  /// Correct st_value for symbols addressing Thumb instructions
  /// by removing its zero bit.
  uint64_t getSymbolValue() const override {
    const auto value = static_cast<uint64_t>(this->_symbol->st_value);
    return isThumbFunc() ? value & ~0x1 : value;
  }

  DefinedAtom::CodeModel codeModel() const override {
    return isThumbFunc() ? DefinedAtom::codeARMThumb : DefinedAtom::codeNA;
  }
};

template <class ELFT> class ARMELFFile : public ELFFile<ELFT> {
  typedef llvm::object::Elf_Rel_Impl<ELFT, false> Elf_Rel;

public:
  ARMELFFile(std::unique_ptr<MemoryBuffer> mb, ARMLinkingContext &ctx)
      : ELFFile<ELFT>(std::move(mb), ctx) {}

protected:
  /// Returns initial addend; for ARM it is 0, because it is read
  /// during the relocations applying
  Reference::Addend getInitialAddend(ArrayRef<uint8_t>,
                                     uint64_t,
                                     const Elf_Rel&) const override {
    return 0;
  }

private:
  typedef llvm::object::Elf_Sym_Impl<ELFT> Elf_Sym;
  typedef llvm::object::Elf_Shdr_Impl<ELFT> Elf_Shdr;

  /// Correct st_value for symbols addressing Thumb instructions
  /// by removing its zero bit.
  uint64_t getSymbolValue(const Elf_Sym *symbol) const override {
    const auto value = static_cast<uint64_t>(symbol->st_value);
    return symbol->getType() == llvm::ELF::STT_FUNC ? value & ~0x1 : value;
  }

  /// Process the Defined symbol and create an atom for it.
  ELFDefinedAtom<ELFT> *
  createDefinedAtom(StringRef symName, StringRef sectionName,
                    const Elf_Sym *sym, const Elf_Shdr *sectionHdr,
                    ArrayRef<uint8_t> contentData, unsigned int referenceStart,
                    unsigned int referenceEnd,
                    std::vector<ELFReference<ELFT> *> &referenceList) override {
    if (symName.size() >= 2 && symName[0] == '$') {
      switch (symName[1]) {
      case 'a':
        return new (this->_readerStorage)
            ARMELFMappingAtom<ELFT, DefinedAtom::codeARM_a>(
                *this, symName, sectionName, sym, sectionHdr, contentData,
                referenceStart, referenceEnd, referenceList);
      case 'd':
        return new (this->_readerStorage)
            ARMELFMappingAtom<ELFT, DefinedAtom::codeARM_d>(
                *this, symName, sectionName, sym, sectionHdr, contentData,
                referenceStart, referenceEnd, referenceList);
      case 't':
        return new (this->_readerStorage)
            ARMELFMappingAtom<ELFT, DefinedAtom::codeARM_t>(
                *this, symName, sectionName, sym, sectionHdr, contentData,
                referenceStart, referenceEnd, referenceList);
      default:
        // Fall through and create regular defined atom.
        break;
      }
    }
    return new (this->_readerStorage) ARMELFDefinedAtom<ELFT>(
        *this, symName, sectionName, sym, sectionHdr, contentData,
        referenceStart, referenceEnd, referenceList);
  }
};

} // elf
} // lld

#endif // LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
OpenPOWER on IntegriCloud