summaryrefslogtreecommitdiffstats
path: root/lld/lib/ReaderWriter/ELF/ARM/ARMELFFile.h
blob: 76d8477850a3432a125d6cd717f831797e15d8a8 (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
//===--------- 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;

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

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

private:
  DefinedAtom::CodeModel _model;
};

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

  bool isThumbFunc() const {
    const auto *symbol = _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>(_symbol->st_value);
    return isThumbFunc() ? value & ~0x1 : value;
  }

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

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

public:
  ARMELFFile(std::unique_ptr<MemoryBuffer> mb, ELFLinkingContext &ctx)
      : ELFFile<ELF32LE>(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<ELF32LE> Elf_Sym;
  typedef llvm::object::Elf_Shdr_Impl<ELF32LE> 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<ELF32LE> *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<ELF32LE> *> &referenceList) override {
    if (symName.size() >= 2 && symName[0] == '$') {
      switch (symName[1]) {
      case 'a':
        return new (_readerStorage)
            ARMELFMappingAtom(DefinedAtom::codeARM_a, *this, symName,
                              sectionName, sym, sectionHdr, contentData,
                              referenceStart, referenceEnd, referenceList);
      case 'd':
        return new (_readerStorage)
            ARMELFMappingAtom(DefinedAtom::codeARM_d, *this, symName,
                              sectionName, sym, sectionHdr, contentData,
                              referenceStart, referenceEnd, referenceList);
      case 't':
        return new (_readerStorage)
            ARMELFMappingAtom(DefinedAtom::codeARM_t, *this, symName,
                              sectionName, sym, sectionHdr, contentData,
                              referenceStart, referenceEnd, referenceList);
      default:
        // Fall through and create regular defined atom.
        break;
      }
    }
    return new (_readerStorage) ARMELFDefinedAtom(
        *this, symName, sectionName, sym, sectionHdr, contentData,
        referenceStart, referenceEnd, referenceList);
  }
};

} // elf
} // lld

#endif // LLD_READER_WRITER_ELF_ARM_ARM_ELF_FILE_H
OpenPOWER on IntegriCloud