summaryrefslogtreecommitdiff
path: root/lib/Target/AArch64/AArch64LDBackend.h
blob: a8cd52ac4cf138ae7cab2fd4a0d2638f7ac8ffe1 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//===- AArch64LDBackend.h -------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef TARGET_AARCH64_AARCH64LDBACKEND_H_
#define TARGET_AARCH64_AARCH64LDBACKEND_H_

#include "AArch64ELFDynamic.h"
#include "AArch64GOT.h"
#include "AArch64PLT.h"
#include "mcld/LD/LDSection.h"
#include "mcld/Target/GNULDBackend.h"
#include "mcld/Target/OutputRelocSection.h"

namespace mcld {

class LinkerConfig;
class GNUInfo;

//===----------------------------------------------------------------------===//
/// AArch64GNULDBackend - linker backend of AArch64 target of GNU ELF format
///
class AArch64GNULDBackend : public GNULDBackend {
 public:
  static constexpr int64_t MAX_FWD_BRANCH_OFFSET = (((1 << 25) - 1) << 2);
  static constexpr int64_t MAX_BWD_BRANCH_OFFSET = (-((1 << 25) << 2));

  static constexpr int64_t MAX_ADRP_IMM = (1 << 20) - 1;
  static constexpr int64_t MIN_ADRP_IMM = -(1 << 20);

 public:
  AArch64GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
  ~AArch64GNULDBackend();

 public:
  /// initTargetSections - initialize target dependent sections in output.
  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);

  /// initTargetSymbols - initialize target dependent symbols in output.
  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);

  /// initRelocator - create and initialize Relocator.
  bool initRelocator();

  /// getRelocator - return relocator.
  const Relocator* getRelocator() const;
  Relocator* getRelocator();

  /// doPreLayout - Backend can do any needed modification before layout
  void doPreLayout(IRBuilder& pBuilder);

  /// doPostLayout -Backend can do any needed modification after layout
  void doPostLayout(Module& pModule, IRBuilder& pBuilder);

  /// dynamic - the dynamic section of the target machine.
  /// Use co-variant return type to return its own dynamic section.
  AArch64ELFDynamic& dynamic();

  /// dynamic - the dynamic section of the target machine.
  /// Use co-variant return type to return its own dynamic section.
  const AArch64ELFDynamic& dynamic() const;

  /// emitSectionData - write out the section data into the memory region.
  /// When writers get a LDSection whose kind is LDFileFormat::Target, writers
  /// call back target backend to emit the data.
  ///
  /// Backends handle the target-special tables (plt, gp,...) by themselves.
  /// Backend can put the data of the tables in SectionData directly
  ///  - LDSection.getSectionData can get the section data.
  /// Or, backend can put the data into special data structure
  ///  - backend can maintain its own map<LDSection, table> to get the table
  /// from given LDSection.
  ///
  /// @param pSection - the given LDSection
  /// @param pConfig - all options in the command line.
  /// @param pRegion - the region to write out data
  /// @return the size of the table in the file.
  uint64_t emitSectionData(const LDSection& pSection,
                           MemoryRegion& pRegion) const;

  AArch64GOT& getGOT();
  const AArch64GOT& getGOT() const;

  AArch64GOT& getGOTPLT();
  const AArch64GOT& getGOTPLT() const;

  AArch64PLT& getPLT();
  const AArch64PLT& getPLT() const;

  OutputRelocSection& getRelaDyn();
  const OutputRelocSection& getRelaDyn() const;

  OutputRelocSection& getRelaPLT();
  const OutputRelocSection& getRelaPLT() const;

  LDSymbol* getGOTSymbol() { return m_pGOTSymbol; }
  const LDSymbol* getGOTSymbol() const { return m_pGOTSymbol; }

  /// getTargetSectionOrder - compute the layout order of AArch64 target
  /// sections
  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;

  /// finalizeTargetSymbols - finalize the symbol value
  bool finalizeTargetSymbols();

  /// mergeSection - merge target dependent sections
  bool mergeSection(Module& pModule, const Input& pInput, LDSection& pSection);

  /// readSection - read target dependent sections
  bool readSection(Input& pInput, SectionData& pSD);

 private:
  void defineGOTSymbol(IRBuilder& pBuilder);

  int64_t maxFwdBranchOffset() const { return MAX_FWD_BRANCH_OFFSET; }
  int64_t maxBwdBranchOffset() const { return MAX_BWD_BRANCH_OFFSET; }

  void scanErrata(Module& pModule,
                  IRBuilder& pBuilder,
                  size_t& num_new_stubs,
                  size_t& stubs_strlen);

  /// mayRelax - Backends should override this function if they need relaxation
  bool mayRelax() { return true; }

  /// doRelax - Backend can orevride this function to add its relaxation
  /// implementation. Return true if the output (e.g., .text) is "relaxed"
  /// (i.e. layout is changed), and set pFinished to true if everything is fit,
  /// otherwise set it to false.
  bool doRelax(Module& pModule, IRBuilder& pBuilder, bool& pFinished);

  /// initTargetStubs
  bool initTargetStubs();

  /// getRelEntrySize - the size in BYTE of rel type relocation
  size_t getRelEntrySize() { return 16; }

  /// getRelEntrySize - the size in BYTE of rela type relocation
  size_t getRelaEntrySize() { return 24; }

  /// doCreateProgramHdrs - backend can implement this function to create the
  /// target-dependent segments
  virtual void doCreateProgramHdrs(Module& pModule);

 private:
  Relocator* m_pRelocator;

  AArch64GOT* m_pGOT;
  AArch64GOT* m_pGOTPLT;
  AArch64PLT* m_pPLT;
  /// m_RelDyn - dynamic relocation table of .rel.dyn
  OutputRelocSection* m_pRelaDyn;
  /// m_RelPLT - dynamic relocation table of .rel.plt
  OutputRelocSection* m_pRelaPLT;

  /// m_pAttrData - attribute data in public ("aeabi") attribute subsection
  // AArch64ELFAttributeData* m_pAttrData;

  AArch64ELFDynamic* m_pDynamic;
  LDSymbol* m_pGOTSymbol;

  //     variable name           :  ELF
  // LDSection* m_pAttributes;      // .ARM.attributes
  // LDSection* m_pPreemptMap;      // .AArch64.preemptmap
  // LDSection* m_pDebugOverlay;    // .AArch64.debug_overlay
  // LDSection* m_pOverlayTable;    // .AArch64.overlay_table
};

}  // namespace mcld

#endif  // TARGET_AARCH64_AARCH64LDBACKEND_H_