aboutsummaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/ELF/ARM/ARMExecutableWriter.h
blob: 19311d516e4dedbd56b484979e13ae06e8e5f24e (plain) (blame)
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/ARMExecutableWriter.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_EXECUTABLE_WRITER_H
#define LLD_READER_WRITER_ELF_ARM_ARM_EXECUTABLE_WRITER_H

#include "ExecutableWriter.h"
#include "ARMLinkingContext.h"
#include "ARMTargetHandler.h"
#include "ARMSymbolTable.h"

namespace {
const char *gotSymbol = "_GLOBAL_OFFSET_TABLE_";
}

namespace lld {
namespace elf {

template <class ELFT>
class ARMExecutableWriter : public ExecutableWriter<ELFT> {
public:
  ARMExecutableWriter(ARMLinkingContext &context,
                      ARMTargetLayout<ELFT> &layout);

protected:
  // Add any runtime files and their atoms to the output
  bool createImplicitFiles(std::vector<std::unique_ptr<File>> &) override;

  void finalizeDefaultAtomValues() override;

  void addDefaultAtoms() override {
    ExecutableWriter<ELFT>::addDefaultAtoms();
  }

  /// \brief Create symbol table.
  unique_bump_ptr<SymbolTable<ELFT>> createSymbolTable() override;

  void processUndefinedSymbol(StringRef symName,
                              RuntimeFile<ELFT> &file) const override;

  // Setup the ELF header.
  std::error_code setELFHeader() override;

private:
  ARMLinkingContext &_context;
  ARMTargetLayout<ELFT> &_armLayout;
};

template <class ELFT>
ARMExecutableWriter<ELFT>::ARMExecutableWriter(ARMLinkingContext &context,
                                               ARMTargetLayout<ELFT> &layout)
    : ExecutableWriter<ELFT>(context, layout), _context(context),
      _armLayout(layout) {}

template <class ELFT>
bool ARMExecutableWriter<ELFT>::createImplicitFiles(
    std::vector<std::unique_ptr<File>> &result) {
  ExecutableWriter<ELFT>::createImplicitFiles(result);
  return true;
}

template <class ELFT>
void ARMExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
  // Finalize the atom values that are part of the parent.
  ExecutableWriter<ELFT>::finalizeDefaultAtomValues();
  auto gotAtomIter = _armLayout.findAbsoluteAtom(gotSymbol);
  if (gotAtomIter != _armLayout.absoluteAtoms().end()) {
    auto *gotAtom = *gotAtomIter;
    if (auto gotpltSection = _armLayout.findOutputSection(".got.plt"))
      gotAtom->_virtualAddr = gotpltSection->virtualAddr();
    else if (auto gotSection = _armLayout.findOutputSection(".got"))
      gotAtom->_virtualAddr = gotSection->virtualAddr();
    else
      gotAtom->_virtualAddr = 0;
  }
  // TODO: resolve addresses of __exidx_start/_end atoms
}

template <class ELFT>
unique_bump_ptr<SymbolTable<ELFT>>
    ARMExecutableWriter<ELFT>::createSymbolTable() {
  return unique_bump_ptr<SymbolTable<ELFT>>(
      new (this->_alloc) ARMSymbolTable<ELFT>(this->_context));
}

template <class ELFT>
void ARMExecutableWriter<ELFT>::processUndefinedSymbol(
    StringRef symName, RuntimeFile<ELFT> &file) const {
  if (symName == gotSymbol) {
    file.addAbsoluteAtom(gotSymbol);
  } else if (symName.startswith("__exidx")) {
    file.addAbsoluteAtom("__exidx_start");
    file.addAbsoluteAtom("__exidx_end");
  }
}

template <class ELFT>
std::error_code ARMExecutableWriter<ELFT>::setELFHeader() {
  if (std::error_code ec = ExecutableWriter<ELFT>::setELFHeader())
    return ec;

  // Fixup entry point for Thumb code.
  StringRef entryName = _context.entrySymbolName();
  if (const AtomLayout *al = _armLayout.findAtomLayoutByName(entryName)) {
    const auto *ea = dyn_cast<DefinedAtom>(al->_atom);
    if (ea && ea->codeModel() == DefinedAtom::codeARMThumb)
      this->_elfHeader->e_entry(al->_virtualAddr | 0x1);
  }

  return std::error_code();
}

} // namespace elf
} // namespace lld

#endif // LLD_READER_WRITER_ELF_ARM_ARM_EXECUTABLE_WRITER_H