TargetLDBackend.h revision cedee4b38f4786845183be7f5916dd520a170ae0
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 LLVM_TARGET_TARGETLDBACKEND_H
10#define LLVM_TARGET_TARGETLDBACKEND_H
11
12#include <llvm/Support/DataTypes.h>
13#include <mcld/MC/MCLDOutput.h>
14#include <mcld/LD/EhFrame.h>
15
16namespace mcld {
17
18class MCLinker;
19class Relocation;
20class RelocationFactory;
21class Layout;
22class ArchiveReader;
23class ObjectReader;
24class DynObjReader;
25class ObjectWriter;
26class DynObjWriter;
27class ExecWriter;
28class LDContext;
29class SectionMap;
30class Output;
31class MCLDInfo;
32class SymbolCategory;
33class Input;
34class LDFileFormat;
35class GOT;
36class MemoryAreaFactory;
37
38//===----------------------------------------------------------------------===//
39/// TargetLDBackend - Generic interface to target specific assembler backends.
40///
41class TargetLDBackend
42{
43  TargetLDBackend(const TargetLDBackend &);   // DO NOT IMPLEMENT
44  void operator=(const TargetLDBackend &);  // DO NOT IMPLEMENT
45
46protected:
47  TargetLDBackend();
48
49public:
50  virtual ~TargetLDBackend();
51
52  // -----  target dependent  ----- //
53  virtual bool initTargetSectionMap(SectionMap& pSectionMap) { return true;}
54  virtual void initTargetSegments(MCLinker& pLinker) { }
55  virtual void initTargetSections(MCLinker& pLinker) { }
56  virtual void initTargetSymbols(MCLinker& pLinker, const Output& pOutput) { }
57  virtual void initTargetRelocation(MCLinker& pLinker) { }
58  virtual bool initStandardSymbols(MCLinker& pLinker, const Output& pOutput) = 0;
59  virtual bool initRelocFactory(const MCLinker& pLinker) = 0;
60
61  virtual RelocationFactory* getRelocFactory() = 0;
62
63  /// scanRelocation - When read in relocations, backend can do any modification
64  /// to relocation and generate empty entries, such as GOT, dynamic relocation
65  /// entries and other target dependent entries. These entries are generated
66  /// for layout to adjust the ouput offset.
67  /// @param pReloc - a read in relocation entry
68  /// @param pInputSym - the input LDSymbol of relocation target symbol
69  /// @param pOutput - the ouput file
70  virtual void scanRelocation(Relocation& pReloc,
71                              const LDSymbol& pInputSym,
72                              MCLinker& pLinker,
73                              const MCLDInfo& pLDInfo,
74                              const Output& pOutput,
75                              const LDSection& pSection) = 0;
76
77  // -----  format dependent  ----- //
78  virtual bool initArchiveReader(MCLinker&,
79                                 MCLDInfo&,
80                                 MemoryAreaFactory&) = 0;
81  virtual bool initObjectReader(MCLinker&) = 0;
82  virtual bool initDynObjReader(MCLinker&) = 0;
83  virtual bool initObjectWriter(MCLinker&) = 0;
84  virtual bool initDynObjWriter(MCLinker&) = 0;
85  virtual bool initExecWriter(MCLinker&) = 0;
86
87  virtual bool initExecSections(MCLinker&) = 0;
88  virtual bool initDynObjSections(MCLinker&) = 0;
89
90  virtual ArchiveReader *getArchiveReader() = 0;
91  virtual ObjectReader *getObjectReader() = 0;
92  virtual DynObjReader *getDynObjReader() = 0;
93  virtual ObjectWriter *getObjectWriter() = 0;
94  virtual DynObjWriter *getDynObjWriter() = 0;
95  virtual ExecWriter *getExecWriter() = 0;
96
97  virtual LDFileFormat* getDynObjFileFormat() = 0;
98  virtual LDFileFormat* getExecFileFormat() = 0;
99
100  /// preLayout - Backend can do any needed modification before layout
101  virtual void preLayout(const Output& pOutput,
102                         const MCLDInfo& pInfo,
103                         MCLinker& pLinker) = 0;
104
105  /// postLayout -Backend can do any needed modification after layout
106  virtual void postLayout(const Output& pOutput,
107                          const MCLDInfo& pInfo,
108                          MCLinker& pLinker) = 0;
109
110  /// postProcessing - Backend can do any needed modification in the final stage
111  virtual void postProcessing(const Output& pOutput,
112                              const MCLDInfo& pInfo,
113                              MCLinker& pLinker) = 0;
114
115  /// Is the target machine little endian? **/
116  virtual bool isLittleEndian() const = 0;
117
118  /// bit class. the bit length of the target machine, 32 or 64 **/
119  virtual unsigned int bitclass() const = 0;
120
121  /// the common page size of the target machine
122  virtual uint64_t commonPageSize(const MCLDInfo& pInfo) const = 0;
123
124  /// the abi page size of the target machine
125  virtual uint64_t abiPageSize(const MCLDInfo& pInfo) const = 0;
126
127  /// section start offset in the output file
128  virtual size_t sectionStartOffset() const = 0;
129
130  /// computeSectionOrder - compute the layout order of the given section
131  virtual unsigned int getSectionOrder(const Output& pOutput,
132                                       const LDSection& pSectHdr,
133                                       const MCLDInfo& pInfo) const = 0;
134
135  /// sizeNamePools - compute the size of regular name pools
136  /// In ELF executable files, regular name pools are .symtab, .strtab.,
137  /// .dynsym, .dynstr, and .hash
138  virtual void
139  sizeNamePools(const Output& pOutput,
140                const SymbolCategory& pSymbols,
141                const MCLDInfo& pLDInfo) = 0;
142
143  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
144  /// then it will ask backend to finalize the symbol value.
145  /// @return ture - if backend set the symbol value sucessfully
146  /// @return false - if backend do not recognize the symbol
147  virtual bool finalizeSymbols(MCLinker& pLinker, const Output& pOutput) = 0;
148
149  /// allocateCommonSymbols - allocate common symbols in the corresponding
150  /// sections.
151  virtual bool allocateCommonSymbols(const MCLDInfo& pLDInfo, MCLinker& pLinker) const = 0;
152
153  /// readSection - read a target dependent section
154  virtual bool readSection(Input& pInput,
155                           MCLinker& pLinker,
156                           LDSection& pInputSectHdr)
157  { return true; }
158
159  /// dyld - the name of the default dynamic linker
160  virtual const char* dyld() const = 0;
161
162  /// sizeInterp - compute the size of program interpreter's name
163  /// In ELF executables, this is the length of dynamic linker's path name
164  virtual void sizeInterp(const Output& pOutput, const MCLDInfo& pLDInfo) = 0;
165
166public:
167  EhFrame* getEhFrame();
168
169  const EhFrame* getEhFrame() const;
170
171private:
172  /// m_pEhFrame - section .eh_frame
173  EhFrame* m_pEhFrame;
174
175};
176
177} // End mcld namespace
178
179#endif
180