TargetLDBackend.h revision 37b74a387bb3993387029859c2d9d051c41c724e
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  /// mergeSection - merge target dependent sections.
111  virtual bool mergeSection(Module& pModule,
112                            const Input& pInputFile,
113                            LDSection& pInputSection) {
114    return true;
115  }
116
117  /// setUpReachedSectionsForGC - set the reference between two sections for
118  /// some special target sections. GC will set up the reference for the Regular
119  /// and BSS sections. Backends can also set up the reference if need.
120  virtual void setUpReachedSectionsForGC(
121      const Module& pModule,
122      GarbageCollection::SectionReachedListMap& pSectReachedListMap) const {}
123
124  /// updateSectionFlags - update pTo's flags when merging pFrom
125  /// update the output section flags based on input section flags.
126  /// FIXME: (Luba) I know ELF need to merge flags, but I'm not sure if
127  /// MachO and COFF also need this.
128  virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom) {
129    return true;
130  }
131
132  /// readSection - read a target dependent section
133  virtual bool readSection(Input& pInput, SectionData& pSD) { return true; }
134
135  /// sizeInterp - compute the size of program interpreter's name
136  /// In ELF executables, this is the length of dynamic linker's path name
137  virtual void sizeInterp() = 0;
138
139  /// getEntry - get the entry point name
140  virtual llvm::StringRef getEntry(const Module& pModule) const = 0;
141
142  // -----  relaxation  ----- //
143  virtual bool initBRIslandFactory() = 0;
144  virtual bool initStubFactory() = 0;
145  virtual bool initTargetStubs() { return true; }
146
147  virtual BranchIslandFactory* getBRIslandFactory() = 0;
148  virtual StubFactory* getStubFactory() = 0;
149
150  /// relax - the relaxation pass
151  virtual bool relax(Module& pModule, IRBuilder& pBuilder) = 0;
152
153  /// mayRelax - return true if the backend needs to do relaxation
154  virtual bool mayRelax() = 0;
155
156  /// commonPageSize - the common page size of the target machine
157  virtual uint64_t commonPageSize() const = 0;
158
159  /// abiPageSize - the abi page size of the target machine
160  virtual uint64_t abiPageSize() const = 0;
161
162  /// sortRelocation - sort the dynamic relocations to let dynamic linker
163  /// process relocations more efficiently
164  virtual void sortRelocation(LDSection& pSection) = 0;
165
166  /// createAndSizeEhFrameHdr - This is seperated since we may add eh_frame
167  /// entry in the middle
168  virtual void createAndSizeEhFrameHdr(Module& pModule) = 0;
169
170  /// isSymbolPreemptible - whether the symbol can be preemted by other link
171  /// units
172  virtual bool isSymbolPreemptible(const ResolveInfo& pSym) const = 0;
173
174  /// mayHaveUnsafeFunctionPointerAccess - check if the section may have unsafe
175  /// function pointer access
176  virtual bool mayHaveUnsafeFunctionPointerAccess(
177      const LDSection& pSection) const = 0;
178
179 protected:
180  const LinkerConfig& config() const { return m_Config; }
181
182 private:
183  const LinkerConfig& m_Config;
184
185 private:
186  DISALLOW_COPY_AND_ASSIGN(TargetLDBackend);
187};
188
189}  // namespace mcld
190
191#endif  // MCLD_TARGET_TARGETLDBACKEND_H_
192