1//===- AArch64LDBackend.h -------------------------------------------------===//
2//
3//                     The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef TARGET_AARCH64_AARCH64LDBACKEND_H_
10#define TARGET_AARCH64_AARCH64LDBACKEND_H_
11
12#include "AArch64ELFDynamic.h"
13#include "AArch64GOT.h"
14#include "AArch64PLT.h"
15#include "mcld/LD/LDSection.h"
16#include "mcld/Target/GNULDBackend.h"
17#include "mcld/Target/OutputRelocSection.h"
18
19namespace mcld {
20
21class LinkerConfig;
22class GNUInfo;
23
24//===----------------------------------------------------------------------===//
25/// AArch64GNULDBackend - linker backend of AArch64 target of GNU ELF format
26///
27class AArch64GNULDBackend : public GNULDBackend {
28 public:
29  static const int64_t AARCH64_MAX_FWD_BRANCH_OFFSET = (((1 << 25) - 1) << 2);
30  static const int64_t AARCH64_MAX_BWD_BRANCH_OFFSET = (-((1 << 25) << 2));
31
32 public:
33  AArch64GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
34  ~AArch64GNULDBackend();
35
36 public:
37  /// initTargetSections - initialize target dependent sections in output.
38  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
39
40  /// initTargetSymbols - initialize target dependent symbols in output.
41  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);
42
43  /// initRelocator - create and initialize Relocator.
44  bool initRelocator();
45
46  /// getRelocator - return relocator.
47  const Relocator* getRelocator() const;
48  Relocator* getRelocator();
49
50  /// doPreLayout - Backend can do any needed modification before layout
51  void doPreLayout(IRBuilder& pBuilder);
52
53  /// doPostLayout -Backend can do any needed modification after layout
54  void doPostLayout(Module& pModule, IRBuilder& pBuilder);
55
56  /// dynamic - the dynamic section of the target machine.
57  /// Use co-variant return type to return its own dynamic section.
58  AArch64ELFDynamic& dynamic();
59
60  /// dynamic - the dynamic section of the target machine.
61  /// Use co-variant return type to return its own dynamic section.
62  const AArch64ELFDynamic& dynamic() const;
63
64  /// emitSectionData - write out the section data into the memory region.
65  /// When writers get a LDSection whose kind is LDFileFormat::Target, writers
66  /// call back target backend to emit the data.
67  ///
68  /// Backends handle the target-special tables (plt, gp,...) by themselves.
69  /// Backend can put the data of the tables in SectionData directly
70  ///  - LDSection.getSectionData can get the section data.
71  /// Or, backend can put the data into special data structure
72  ///  - backend can maintain its own map<LDSection, table> to get the table
73  /// from given LDSection.
74  ///
75  /// @param pSection - the given LDSection
76  /// @param pConfig - all options in the command line.
77  /// @param pRegion - the region to write out data
78  /// @return the size of the table in the file.
79  uint64_t emitSectionData(const LDSection& pSection,
80                           MemoryRegion& pRegion) const;
81
82  AArch64GOT& getGOT();
83  const AArch64GOT& getGOT() const;
84
85  AArch64GOT& getGOTPLT();
86  const AArch64GOT& getGOTPLT() const;
87
88  AArch64PLT& getPLT();
89  const AArch64PLT& getPLT() const;
90
91  OutputRelocSection& getRelaDyn();
92  const OutputRelocSection& getRelaDyn() const;
93
94  OutputRelocSection& getRelaPLT();
95  const OutputRelocSection& getRelaPLT() const;
96
97  LDSymbol* getGOTSymbol() { return m_pGOTSymbol; }
98  const LDSymbol* getGOTSymbol() const { return m_pGOTSymbol; }
99
100  /// getTargetSectionOrder - compute the layout order of AArch64 target
101  /// sections
102  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;
103
104  /// finalizeTargetSymbols - finalize the symbol value
105  bool finalizeTargetSymbols();
106
107  /// mergeSection - merge target dependent sections
108  bool mergeSection(Module& pModule, const Input& pInput, LDSection& pSection);
109
110  /// readSection - read target dependent sections
111  bool readSection(Input& pInput, SectionData& pSD);
112
113 private:
114  void defineGOTSymbol(IRBuilder& pBuilder);
115
116  int64_t maxFwdBranchOffset() { return AARCH64_MAX_FWD_BRANCH_OFFSET; }
117  int64_t maxBwdBranchOffset() { return AARCH64_MAX_BWD_BRANCH_OFFSET; }
118
119  /// mayRelax - Backends should override this function if they need relaxation
120  bool mayRelax() { return true; }
121
122  /// doRelax - Backend can orevride this function to add its relaxation
123  /// implementation. Return true if the output (e.g., .text) is "relaxed"
124  /// (i.e. layout is changed), and set pFinished to true if everything is fit,
125  /// otherwise set it to false.
126  bool doRelax(Module& pModule, IRBuilder& pBuilder, bool& pFinished);
127
128  /// initTargetStubs
129  bool initTargetStubs();
130
131  /// getRelEntrySize - the size in BYTE of rel type relocation
132  size_t getRelEntrySize() { return 16; }
133
134  /// getRelEntrySize - the size in BYTE of rela type relocation
135  size_t getRelaEntrySize() { return 24; }
136
137  /// doCreateProgramHdrs - backend can implement this function to create the
138  /// target-dependent segments
139  virtual void doCreateProgramHdrs(Module& pModule);
140
141 private:
142  Relocator* m_pRelocator;
143
144  AArch64GOT* m_pGOT;
145  AArch64GOT* m_pGOTPLT;
146  AArch64PLT* m_pPLT;
147  /// m_RelDyn - dynamic relocation table of .rel.dyn
148  OutputRelocSection* m_pRelaDyn;
149  /// m_RelPLT - dynamic relocation table of .rel.plt
150  OutputRelocSection* m_pRelaPLT;
151
152  /// m_pAttrData - attribute data in public ("aeabi") attribute subsection
153  // AArch64ELFAttributeData* m_pAttrData;
154
155  AArch64ELFDynamic* m_pDynamic;
156  LDSymbol* m_pGOTSymbol;
157
158  //     variable name           :  ELF
159  // LDSection* m_pAttributes;      // .ARM.attributes
160  // LDSection* m_pPreemptMap;      // .AArch64.preemptmap
161  // LDSection* m_pDebugOverlay;    // .AArch64.debug_overlay
162  // LDSection* m_pOverlayTable;    // .AArch64.overlay_table
163};
164
165}  // namespace mcld
166
167#endif  // TARGET_AARCH64_AARCH64LDBACKEND_H_
168