MipsLDBackend.h revision f7ac0f19a1c8d0ad14bcf6456ce368b830fea886
1//===- MipsLDBackend.h ----------------------------------------------------===//
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 MIPS_LDBACKEND_H
10#define MIPS_LDBACKEND_H
11#include <mcld/Target/GNULDBackend.h>
12#include "MipsELFDynamic.h"
13#include "MipsGOT.h"
14
15namespace mcld {
16
17class LinkerConfig;
18class OutputRelocSection;
19class SectionMap;
20class MemoryArea;
21class MipsGNUInfo;
22
23//===----------------------------------------------------------------------===//
24/// MipsGNULDBackend - linker backend of Mips target of GNU ELF format
25///
26class MipsGNULDBackend : public GNULDBackend
27{
28public:
29  typedef std::vector<LDSymbol*> SymbolListType;
30
31public:
32  MipsGNULDBackend(const LinkerConfig& pConfig, MipsGNUInfo* pInfo);
33  ~MipsGNULDBackend();
34
35public:
36  /// initTargetSections - initialize target dependent sections in output
37  void initTargetSections(Module& pModule, ObjectBuilder& pBuilder);
38
39  /// initTargetSymbols - initialize target dependent symbols in output.
40  void initTargetSymbols(IRBuilder& pBuilder, Module& pModule);
41
42  /// initRelocator - create and initialize Relocator.
43  bool initRelocator();
44
45  /// getRelocator - return relocator.
46  Relocator* getRelocator();
47
48  /// preLayout - Backend can do any needed modification before layout
49  void doPreLayout(IRBuilder& pBuilder);
50
51  /// postLayout - Backend can do any needed modification after layout
52  void doPostLayout(Module& pModule, IRBuilder& pBuilder);
53
54  /// dynamic - the dynamic section of the target machine.
55  /// Use co-variant return type to return its own dynamic section.
56  MipsELFDynamic& dynamic();
57
58  /// dynamic - the dynamic section of the target machine.
59  /// Use co-variant return type to return its own dynamic section.
60  const MipsELFDynamic& dynamic() const;
61
62  /// emitSectionData - write out the section data into the memory region.
63  /// When writers get a LDSection whose kind is LDFileFormat::Target, writers
64  /// call back target backend to emit the data.
65  ///
66  /// Backends handle the target-special tables (plt, gp,...) by themselves.
67  /// Backend can put the data of the tables in SectionData directly
68  ///  - LDSection.getSectionData can get the section data.
69  /// Or, backend can put the data into special data structure
70  ///  - backend can maintain its own map<LDSection, table> to get the table
71  /// from given LDSection.
72  ///
73  /// @param pSection - the given LDSection
74  /// @param pRegion - the region to write out data
75  /// @return the size of the table in the file.
76  uint64_t emitSectionData(const LDSection& pSection,
77                           MemoryRegion& pRegion) const;
78
79  /// hasEntryInStrTab - symbol has an entry in a .strtab
80  bool hasEntryInStrTab(const LDSymbol& pSym) const;
81
82  /// orderSymbolTable - order symbol table before emitting
83  void orderSymbolTable(Module& pModule);
84
85  MipsGOT& getGOT();
86  const MipsGOT& getGOT() const;
87
88  OutputRelocSection& getRelDyn();
89  const OutputRelocSection& getRelDyn() const;
90
91  LDSymbol*             getGOTSymbo()            { return m_pGOTSymbol;    }
92  const LDSymbol*       getGOTSymbo() const      { return m_pGOTSymbol;    }
93
94  LDSymbol*             getGpDispSymbol()        { return m_pGpDispSymbol; }
95  const LDSymbol*       getGpDispSymbol() const  { return m_pGpDispSymbol; }
96
97  SymbolListType&       getGlobalGOTSyms()       { return m_GlobalGOTSyms; }
98  const SymbolListType& getGlobalGOTSyms() const { return m_GlobalGOTSyms; }
99
100  /// getTargetSectionOrder - compute the layout order of ARM target sections
101  unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const;
102
103  /// finalizeSymbol - finalize the symbol value
104  bool finalizeTargetSymbols();
105
106  /// allocateCommonSymbols - allocate common symbols in the corresponding
107  /// sections.
108  bool allocateCommonSymbols(Module& pModule);
109
110private:
111  void defineGOTSymbol(IRBuilder& pBuilder);
112
113  /// emitSymbol32 - emit an ELF32 symbol, override parent's function
114  void emitSymbol32(llvm::ELF::Elf32_Sym& pSym32,
115                    LDSymbol& pSymbol,
116                    char* pStrtab,
117                    size_t pStrtabsize,
118                    size_t pSymtabIdx);
119
120  /// getRelEntrySize - the size in BYTE of rel type relocation
121  size_t getRelEntrySize()
122  { return 8; }
123
124  /// getRelEntrySize - the size in BYTE of rela type relocation
125  size_t getRelaEntrySize()
126  { return 12; }
127
128  /// doCreateProgramHdrs - backend can implement this function to create the
129  /// target-dependent segments
130  void doCreateProgramHdrs(Module& pModule);
131
132private:
133  Relocator* m_pRelocator;
134
135  MipsGOT* m_pGOT;                      // .got
136  OutputRelocSection* m_pRelDyn;        // .rel.dyn
137
138  MipsELFDynamic* m_pDynamic;
139  LDSymbol* m_pGOTSymbol;
140  LDSymbol* m_pGpDispSymbol;
141
142  SymbolListType m_GlobalGOTSyms;
143};
144
145} // namespace of mcld
146
147#endif
148
149