summaryrefslogtreecommitdiff
path: root/lib/Target/Hexagon/HexagonLDBackend.h
blob: e2bc2bb63fb015b9e2d472b255e9e84e7756c5f8 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//===- HexagonLDBackend.h -------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef HEXAGON_LDBACKEND_H
#define HEXAGON_LDBACKEND_H

#include "HexagonELFDynamic.h"
#include "HexagonGOT.h"
#include "HexagonPLT.h"
#include "HexagonGOTPLT.h"
#include <mcld/IRBuilder.h>
#include <mcld/LinkerConfig.h>
#include <mcld/LD/LDSection.h>
#include <mcld/Object/ObjectBuilder.h>
#include <mcld/Target/GNULDBackend.h>
#include <mcld/Target/OutputRelocSection.h>

namespace mcld {

class LinkerConfig;
class HexagonGNUInfo;

//===----------------------------------------------------------------------===//
/// HexagonLDBackend - linker backend of Hexagon target of GNU ELF format
///
class HexagonLDBackend : public GNULDBackend
{
public:
  HexagonLDBackend(const LinkerConfig& pConfig, HexagonGNUInfo* pInfo);

  ~HexagonLDBackend();

  uint32_t machine() const;

  HexagonGOT& getGOT();

  const HexagonGOT& getGOT() const;

  HexagonPLT& getPLT();

  const HexagonPLT& getPLT() const;

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

  bool allocateCommonSymbols(Module& pModule);

  /// postLayout - 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.
  HexagonELFDynamic& dynamic();

  /// dynamic - the dynamic section of the target machine.
  /// Use co-variant return type to return its own dynamic section.
  const HexagonELFDynamic& 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 MCSectionData 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 pLayout - for comouting the size of fragment
  /// @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;

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

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

  ResolveInfo::Desc getSymDesc(uint16_t shndx) const
  {
    if (shndx >= llvm::ELF::SHN_HEXAGON_SCOMMON &&
        shndx <= llvm::ELF::SHN_HEXAGON_SCOMMON_8)
      return ResolveInfo::Common;
    return ResolveInfo::NoneDesc;
  }

  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);

  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);

  bool initBRIslandFactory();

  bool initStubFactory();

  bool mayRelax() { return true; }

  bool doRelax(Module& pModule, IRBuilder& pBuilder, bool& pFinished);

  bool initTargetStubs();

  OutputRelocSection& getRelaDyn();

  const OutputRelocSection& getRelaDyn() const;

  HexagonGOTPLT& getGOTPLT();

  const HexagonGOTPLT& getGOTPLT() const;

  OutputRelocSection& getRelaPLT();

  const OutputRelocSection& getRelaPLT() const;

  /// getTargetSectionOrder - compute the layout order of Hexagon target section
  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);

  bool MoveCommonData(SectionData &pFrom, SectionData &pTo);

  bool MoveSectionDataAndSort(SectionData& pFrom, SectionData& pTo);

  bool SetSDataSection();

  uint32_t getGP() { return m_psdata->addr(); }

  Relocation::Type getCopyRelType()    const { return m_CopyRel;    }

  virtual uint32_t getGOTSymbolAddr() {
    return m_pGOTSymbol->value();
  }


protected:
  void defineGOTSymbol(IRBuilder& pBuilder, Fragment&);

private:
  /// getRelEntrySize - the size in BYTE of rela type relocation
  size_t getRelEntrySize()
  { return 0; }

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

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

  uint64_t maxBranchOffset() { return ~(~0 << 6); }

  virtual void setGOTSectionSize(IRBuilder& pBuilder);

  virtual uint64_t emitGOTSectionData(MemoryRegion& pRegion) const;

  virtual uint64_t emitGOTPLTSectionData(MemoryRegion& pRegion,
					 const ELFFileFormat* FileFormat) const;

  virtual void setRelaDynSize();
  virtual void setRelaPLTSize();

private:
  Relocator* m_pRelocator;
  HexagonGOT* m_pGOT;
  HexagonGOTPLT* m_pGOTPLT;
  HexagonPLT* m_pPLT;
  /// m_RelaDyn - dynamic relocation table of .rela.dyn
  OutputRelocSection* m_pRelaDyn;
  /// m_RelaPLT - dynamic relocation table of .rela.plt
  OutputRelocSection* m_pRelaPLT;

  HexagonELFDynamic* m_pDynamic;

  LDSection* m_psdata;
  LDSection* m_pscommon_1;
  LDSection* m_pscommon_2;
  LDSection* m_pscommon_4;
  LDSection* m_pscommon_8;
  LDSection* m_pstart;
  LDSymbol* m_psdabase;

  LDSymbol* m_pGOTSymbol;
  LDSymbol* m_pBSSEnd;
  Relocation::Type m_CopyRel;
};
} // namespace of mcld

#endif