TargetLDBackend.h revision 87f34658dec9097d987d254a990ea7f311bfc95f
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
12#include <llvm/ADT/StringRef.h>
13#include <llvm/Support/DataTypes.h>
14#include <mcld/LD/GarbageCollection.h>
15
16namespace mcld {
17
18class ArchiveReader;
19class BinaryReader;
20class BinaryWriter;
21class BranchIslandFactory;
22class DynObjReader;
23class DynObjWriter;
24class ExecWriter;
25class FileOutputBuffer;
26class SectionReachedListMap;
27class IRBuilder;
28class Input;
29class LDSection;
30class LDSymbol;
31class Layout;
32class LinkerConfig;
33class Module;
34class ObjectBuilder;
35class ObjectReader;
36class ObjectWriter;
37class Relocator;
38class SectionData;
39class StubFactory;
40
41//===----------------------------------------------------------------------===//
42/// TargetLDBackend - Generic interface to target specific assembler backends.
43//===----------------------------------------------------------------------===//
44class TargetLDBackend
45{
46  TargetLDBackend(const TargetLDBackend &);   // DO NOT IMPLEMENT
47  void operator=(const TargetLDBackend &);  // DO NOT IMPLEMENT
48
49protected:
50  TargetLDBackend(const LinkerConfig& pConfig);
51
52public:
53  virtual ~TargetLDBackend();
54
55  // -----  target dependent  ----- //
56  virtual void initTargetSegments(IRBuilder& pBuilder) { }
57  virtual void initTargetSections(Module& pModule, ObjectBuilder& pBuilder) { }
58  virtual void initTargetSymbols(IRBuilder& pBuilder, Module& pModule) { }
59  virtual void initTargetRelocation(IRBuilder& pBuilder) { }
60  virtual bool initStandardSymbols(IRBuilder& pBuilder, Module& pModule) = 0;
61
62  virtual bool initRelocator() = 0;
63
64  virtual Relocator* getRelocator() = 0;
65
66  // -----  format dependent  ----- //
67  virtual ArchiveReader* createArchiveReader(Module&) = 0;
68  virtual ObjectReader*  createObjectReader(IRBuilder&) = 0;
69  virtual DynObjReader*  createDynObjReader(IRBuilder&) = 0;
70  virtual BinaryReader*  createBinaryReader(IRBuilder&) = 0;
71  virtual ObjectWriter*  createWriter() = 0;
72
73  virtual bool initStdSections(ObjectBuilder& pBuilder) = 0;
74
75  /// layout - layout method
76  virtual void layout(Module& pModule) = 0;
77
78  /// preLayout - Backend can do any needed modification before layout
79  virtual void preLayout(Module& pModule, IRBuilder& pBuilder) = 0;
80
81  /// postLayout - Backend can do any needed modification after layout
82  virtual void postLayout(Module& pModule, IRBuilder& pBuilder) = 0;
83
84  /// postProcessing - Backend can do any needed modification in the final stage
85  virtual void postProcessing(FileOutputBuffer& pOutput) = 0;
86
87  /// section start offset in the output file
88  virtual size_t sectionStartOffset() const = 0;
89
90  /// computeSectionOrder - compute the layout order of the given section
91  virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const = 0;
92
93  /// sizeNamePools - compute the size of regular name pools
94  /// In ELF executable files, regular name pools are .symtab, .strtab.,
95  /// .dynsym, .dynstr, and .hash
96  virtual void sizeNamePools(Module& pModule) = 0;
97
98  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
99  /// then it will ask backend to finalize the symbol value.
100  /// @return ture - if backend set the symbol value sucessfully
101  /// @return false - if backend do not recognize the symbol
102  virtual bool finalizeSymbols() = 0;
103
104  /// finalizeTLSSymbol - Linker asks backend to set the symbol value when it
105  /// meets a TLS symbol
106  virtual bool finalizeTLSSymbol(LDSymbol& pSymbol) = 0;
107
108  /// allocateCommonSymbols - allocate common symbols in the corresponding
109  /// sections.
110  virtual bool allocateCommonSymbols(Module& pModule) = 0;
111
112  /// mergeSection - merge target dependent sections.
113  virtual bool mergeSection(Module& pModule,
114                            const Input& pInputFile,
115                            LDSection& pInputSection)
116  { return true; }
117
118  /// setUpReachedSectionsForGC - set the reference between two sections for
119  /// some special target sections. GC will set up the reference for the Regular
120  /// and BSS sections. Backends can also set up the reference if need.
121  virtual void setUpReachedSectionsForGC(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  /// readSection - read a target dependent section
132  virtual bool readSection(Input& pInput, SectionData& pSD)
133  { 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
170protected:
171  const LinkerConfig& config() const { return m_Config; }
172
173private:
174  const LinkerConfig& m_Config;
175};
176
177} // End mcld namespace
178
179#endif
180