TargetLDBackend.h revision a6c24dff8b7fa2551a3a885e77a2e814f5b764a2
1//===-- 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#include "mcld/LD/GarbageCollection.h"
12#include "mcld/Support/Compiler.h"
13
14#include <llvm/ADT/StringRef.h>
15#include <llvm/Support/DataTypes.h>
16
17namespace mcld {
18
19class ArchiveReader;
20class BranchIslandFactory;
21class BinaryReader;
22class BinaryWriter;
23class DynObjReader;
24class DynObjWriter;
25class ExecWriter;
26class FileOutputBuffer;
27class IRBuilder;
28class Input;
29class LDSection;
30class LDSymbol;
31class LinkerConfig;
32class Module;
33class ObjectBuilder;
34class ObjectReader;
35class ObjectWriter;
36class Relocator;
37class ResolveInfo;
38class SectionData;
39class SectionReachedListMap;
40class StubFactory;
41
42//===----------------------------------------------------------------------===//
43/// TargetLDBackend - Generic interface to target specific assembler backends.
44//===----------------------------------------------------------------------===//
45class TargetLDBackend {
46 protected:
47  explicit TargetLDBackend(const LinkerConfig& pConfig);
48
49 public:
50  virtual ~TargetLDBackend();
51
52  // -----  target dependent  ----- //
53  virtual void initTargetSegments(IRBuilder& pBuilder) {}
54  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) {}
55  virtual void initTargetSymbols(IRBuilder& pBuilder, Module& pModule) {}
56  virtual void initTargetRelocation(IRBuilder& pBuilder) {}
57  virtual bool initStandardSymbols(IRBuilder& pBuilder, Module& pModule) = 0;
58
59  virtual bool initRelocator() = 0;
60
61  virtual Relocator* getRelocator() = 0;
62  virtual const Relocator* getRelocator() const = 0;
63
64  // -----  format dependent  ----- //
65  virtual ArchiveReader* createArchiveReader(Module&) = 0;
66  virtual ObjectReader* createObjectReader(IRBuilder&) = 0;
67  virtual DynObjReader* createDynObjReader(IRBuilder&) = 0;
68  virtual BinaryReader* createBinaryReader(IRBuilder&) = 0;
69  virtual ObjectWriter* createWriter() = 0;
70
71  virtual bool initStdSections(ObjectBuilder& pBuilder) = 0;
72
73  /// layout - layout method
74  virtual void layout(Module& pModule) = 0;
75
76  /// preLayout - Backend can do any needed modification before layout
77  virtual void preLayout(Module& pModule, IRBuilder& pBuilder) = 0;
78
79  /// postLayout - Backend can do any needed modification after layout
80  virtual void postLayout(Module& pModule, IRBuilder& pBuilder) = 0;
81
82  /// postProcessing - Backend can do any needed modification in the final stage
83  virtual void postProcessing(FileOutputBuffer& pOutput) = 0;
84
85  /// section start offset in the output file
86  virtual size_t sectionStartOffset() const = 0;
87
88  /// computeSectionOrder - compute the layout order of the given section
89  virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const = 0;
90
91  /// sizeNamePools - compute the size of regular name pools
92  /// In ELF executable files, regular name pools are .symtab, .strtab.,
93  /// .dynsym, .dynstr, and .hash
94  virtual void sizeNamePools(Module& pModule) = 0;
95
96  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
97  /// then it will ask backend to finalize the symbol value.
98  /// @return ture - if backend set the symbol value sucessfully
99  /// @return false - if backend do not recognize the symbol
100  virtual bool finalizeSymbols() = 0;
101
102  /// finalizeTLSSymbol - Linker asks backend to set the symbol value when it
103  /// meets a TLS symbol
104  virtual bool finalizeTLSSymbol(LDSymbol& pSymbol) = 0;
105
106  /// allocateCommonSymbols - allocate common symbols in the corresponding
107  /// sections.
108  virtual bool allocateCommonSymbols(Module& pModule) = 0;
109
110  /// preMergeSections - hooks to be executed before merging sections
111  virtual void preMergeSections(Module& pModule) { }
112
113  /// postMergeSections - hooks to be executed after merging sections
114  virtual void postMergeSections(Module& pModule) { }
115
116  /// mergeSection - merge target dependent sections.
117  virtual bool mergeSection(Module& pModule,
118                            const Input& pInputFile,
119                            LDSection& pInputSection) {
120    return true;
121  }
122
123  /// setUpReachedSectionsForGC - set the reference between two sections for
124  /// some special target sections. GC will set up the reference for the Regular
125  /// and BSS sections. Backends can also set up the reference if need.
126  virtual void setUpReachedSectionsForGC(
127      const Module& pModule,
128      GarbageCollection::SectionReachedListMap& pSectReachedListMap) const {}
129
130  /// updateSectionFlags - update pTo's flags when merging pFrom
131  /// update the output section flags based on input section flags.
132  /// FIXME: (Luba) I know ELF need to merge flags, but I'm not sure if
133  /// MachO and COFF also need this.
134  virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom) {
135    return true;
136  }
137
138  /// readSection - read a target dependent section
139  virtual bool readSection(Input& pInput, SectionData& pSD) { return true; }
140
141  /// sizeInterp - compute the size of program interpreter's name
142  /// In ELF executables, this is the length of dynamic linker's path name
143  virtual void sizeInterp() = 0;
144
145  /// getEntry - get the entry point name
146  virtual llvm::StringRef getEntry(const Module& pModule) const = 0;
147
148  // -----  relaxation  ----- //
149  virtual bool initBRIslandFactory() = 0;
150  virtual bool initStubFactory() = 0;
151  virtual bool initTargetStubs() { return true; }
152
153  virtual BranchIslandFactory* getBRIslandFactory() = 0;
154  virtual StubFactory* getStubFactory() = 0;
155
156  /// relax - the relaxation pass
157  virtual bool relax(Module& pModule, IRBuilder& pBuilder) = 0;
158
159  /// mayRelax - return true if the backend needs to do relaxation
160  virtual bool mayRelax() = 0;
161
162  /// commonPageSize - the common page size of the target machine
163  virtual uint64_t commonPageSize() const = 0;
164
165  /// abiPageSize - the abi page size of the target machine
166  virtual uint64_t abiPageSize() const = 0;
167
168  /// sortRelocation - sort the dynamic relocations to let dynamic linker
169  /// process relocations more efficiently
170  virtual void sortRelocation(LDSection& pSection) = 0;
171
172  /// createAndSizeEhFrameHdr - This is seperated since we may add eh_frame
173  /// entry in the middle
174  virtual void createAndSizeEhFrameHdr(Module& pModule) = 0;
175
176  /// isSymbolPreemptible - whether the symbol can be preemted by other link
177  /// units
178  virtual bool isSymbolPreemptible(const ResolveInfo& pSym) const = 0;
179
180  /// mayHaveUnsafeFunctionPointerAccess - check if the section may have unsafe
181  /// function pointer access
182  virtual bool mayHaveUnsafeFunctionPointerAccess(
183      const LDSection& pSection) const = 0;
184
185 protected:
186  const LinkerConfig& config() const { return m_Config; }
187
188 private:
189  const LinkerConfig& m_Config;
190
191 private:
192  DISALLOW_COPY_AND_ASSIGN(TargetLDBackend);
193};
194
195}  // namespace mcld
196
197#endif  // MCLD_TARGET_TARGETLDBACKEND_H_
198