TargetLDBackend.h revision d0fbbb227051be16931a1aa9b4a7722ac039c698
1//===-- llvm/Target/TargetLDBackend.h - Target LD Backend -----*- C++ -*-===//
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 MCLD_TARGET_TARGETLDBACKEND_H
10#define MCLD_TARGET_TARGETLDBACKEND_H
11
12#include <llvm/Support/DataTypes.h>
13
14namespace mcld {
15
16class Module;
17class LinkerConfig;
18class IRBuilder;
19class FragmentLinker;
20class Relocation;
21class RelocationFactory;
22class Relocator;
23class Layout;
24class ArchiveReader;
25class ObjectReader;
26class DynObjReader;
27class BinaryReader;
28class ObjectWriter;
29class DynObjWriter;
30class ExecWriter;
31class BinaryWriter;
32class LDFileFormat;
33class LDSymbol;
34class LDSection;
35class SectionData;
36class Input;
37class GOT;
38class MemoryArea;
39class MemoryAreaFactory;
40class BranchIslandFactory;
41class StubFactory;
42class ObjectBuilder;
43
44//===----------------------------------------------------------------------===//
45/// TargetLDBackend - Generic interface to target specific assembler backends.
46//===----------------------------------------------------------------------===//
47class TargetLDBackend
48{
49  TargetLDBackend(const TargetLDBackend &);   // DO NOT IMPLEMENT
50  void operator=(const TargetLDBackend &);  // DO NOT IMPLEMENT
51
52protected:
53  TargetLDBackend(const LinkerConfig& pConfig);
54
55public:
56  virtual ~TargetLDBackend();
57
58  // -----  target dependent  ----- //
59  virtual void initTargetSegments(FragmentLinker& pLinker) { }
60  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) { }
61  virtual void initTargetSymbols(FragmentLinker& pLinker) { }
62  virtual void initTargetRelocation(FragmentLinker& pLinker) { }
63  virtual bool initStandardSymbols(FragmentLinker& pLinker, Module& pModule) = 0;
64
65  virtual bool initRelocator(const FragmentLinker& pLinker) = 0;
66
67  virtual Relocator* getRelocator() = 0;
68
69  /// scanRelocation - When read in relocations, backend can do any modification
70  /// to relocation and generate empty entries, such as GOT, dynamic relocation
71  /// entries and other target dependent entries. These entries are generated
72  /// for layout to adjust the ouput offset.
73  /// @param pReloc - a read in relocation entry
74  /// @param pInputSym - the input LDSymbol of relocation target symbol
75  /// @param pSection - the section of relocation applying target
76  virtual void scanRelocation(Relocation& pReloc,
77                              FragmentLinker& pLinker,
78                              Module& pModule,
79                              const LDSection& pSection) = 0;
80
81  /// partialScanRelocation - When doing partial linking, backend can do any
82  /// modification to relocation to fix the relocation offset after section
83  /// merge
84  /// @param pReloc - a read in relocation entry
85  /// @param pInputSym - the input LDSymbol of relocation target symbol
86  /// @param pSection - the section of relocation applying target
87  virtual void partialScanRelocation(Relocation& pReloc,
88                                     FragmentLinker& pLinker,
89                                     Module& pModule,
90                                     const LDSection& pSection) = 0;
91
92  // -----  format dependent  ----- //
93  virtual ArchiveReader* createArchiveReader(Module&) = 0;
94  virtual ObjectReader*  createObjectReader(IRBuilder&) = 0;
95  virtual DynObjReader*  createDynObjReader(IRBuilder&) = 0;
96  virtual BinaryReader*  createBinaryReader(IRBuilder&) = 0;
97  virtual ObjectWriter*  createObjectWriter() = 0;
98  virtual DynObjWriter*  createDynObjWriter() = 0;
99  virtual ExecWriter*    createExecWriter() = 0;
100  virtual BinaryWriter*  createBinaryWriter() = 0;
101
102  virtual bool initStdSections(ObjectBuilder& pBuilder) = 0;
103
104  /// layout - layout method
105  virtual void layout(Module& pModule, FragmentLinker& pLinker) = 0;
106
107  /// preLayout - Backend can do any needed modification before layout
108  virtual void preLayout(Module& pModule, FragmentLinker& pLinker) = 0;
109
110  /// postLayout -Backend can do any needed modification after layout
111  virtual void postLayout(Module& pModule, FragmentLinker& pLinker) = 0;
112
113  /// postProcessing - Backend can do any needed modification in the final stage
114  virtual void postProcessing(FragmentLinker& pLinker,
115                              MemoryArea& pOutput) = 0;
116
117  /// the common page size of the target machine
118  virtual uint64_t commonPageSize() const = 0;
119
120  /// the abi page size of the target machine
121  virtual uint64_t abiPageSize() const = 0;
122
123  /// section start offset in the output file
124  virtual size_t sectionStartOffset() const = 0;
125
126  /// computeSectionOrder - compute the layout order of the given section
127  virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const = 0;
128
129  /// sizeNamePools - compute the size of regular name pools
130  /// In ELF executable files, regular name pools are .symtab, .strtab.,
131  /// .dynsym, .dynstr, and .hash
132  virtual void
133  sizeNamePools(const Module& pModule, bool pIsStaticLink) = 0;
134
135  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
136  /// then it will ask backend to finalize the symbol value.
137  /// @return ture - if backend set the symbol value sucessfully
138  /// @return false - if backend do not recognize the symbol
139  virtual bool finalizeSymbols(FragmentLinker& pLinker) = 0;
140
141  /// finalizeTLSSymbol - Linker asks backend to set the symbol value when it
142  /// meets a TLS symbol
143  virtual bool finalizeTLSSymbol(LDSymbol& pSymbol) = 0;
144
145  /// allocateCommonSymbols - allocate common symbols in the corresponding
146  /// sections.
147  virtual bool allocateCommonSymbols(Module& pModule) = 0;
148
149  /// mergeSection - merge target dependent sections.
150  virtual bool mergeSection(Module& pModule, LDSection& pInputSection)
151  { return true; }
152
153  /// readSection - read a target dependent section
154  virtual bool readSection(Input& pInput, SectionData& pSD)
155  { return true; }
156
157  /// dyld - the name of the default dynamic linker
158  virtual const char* dyld() const = 0;
159
160  /// sizeInterp - compute the size of program interpreter's name
161  /// In ELF executables, this is the length of dynamic linker's path name
162  virtual void sizeInterp() = 0;
163
164  // -----  relaxation  ----- //
165  virtual bool initBRIslandFactory() = 0;
166  virtual bool initStubFactory() = 0;
167  virtual bool initTargetStubs(FragmentLinker& pLinker) { return true; }
168
169  virtual BranchIslandFactory* getBRIslandFactory() = 0;
170  virtual StubFactory*         getStubFactory() = 0;
171
172  /// relax - the relaxation pass
173  virtual bool relax(Module& pModule, FragmentLinker& pLinker) = 0;
174
175  /// mayRelax - return true if the backend needs to do relaxation
176  virtual bool mayRelax() = 0;
177
178protected:
179  const LinkerConfig& config() const { return m_Config; }
180
181private:
182  const LinkerConfig& m_Config;
183};
184
185} // End mcld namespace
186
187#endif
188