GNULDBackend.h revision 6f75755c9204b1d8817ae5a65a2f7e5af0ec3f70
1//===- GNULDBackend.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 MCLD_TARGET_GNU_LDBACKEND_H
10#define MCLD_TARGET_GNU_LDBACKEND_H
11#ifdef ENABLE_UNITTEST
12#include <gtest.h>
13#endif
14#include <mcld/Target/TargetLDBackend.h>
15
16#include <llvm/Support/ELF.h>
17#include <mcld/ADT/HashTable.h>
18#include <mcld/ADT/HashEntry.h>
19#include <mcld/LD/ELFDynObjFileFormat.h>
20#include <mcld/LD/ELFExecFileFormat.h>
21#include <mcld/LD/ELFObjectFileFormat.h>
22#include <mcld/LD/GNUArchiveReader.h>
23#include <mcld/LD/ELFObjectReader.h>
24#include <mcld/LD/ELFDynObjReader.h>
25#include <mcld/LD/ELFBinaryReader.h>
26#include <mcld/LD/ELFObjectWriter.h>
27#include <mcld/LD/ELFSegment.h>
28#include <mcld/LD/ELFSegmentFactory.h>
29#include <mcld/Target/ELFDynamic.h>
30#include <mcld/Target/GNUInfo.h>
31
32#include <mcld/Support/GCFactory.h>
33#include <mcld/Module.h>
34
35namespace mcld {
36
37class Module;
38class LinkerConfig;
39class IRBuilder;
40class Layout;
41class EhFrameHdr;
42class BranchIslandFactory;
43class StubFactory;
44class GNUInfo;
45
46/** \class GNULDBackend
47 *  \brief GNULDBackend provides a common interface for all GNU Unix-OS
48 *  LDBackend.
49 */
50class GNULDBackend : public TargetLDBackend
51{
52protected:
53  GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo);
54
55public:
56  virtual ~GNULDBackend();
57
58  // -----  readers/writers  ----- //
59  GNUArchiveReader* createArchiveReader(Module& pModule);
60  ELFObjectReader* createObjectReader(IRBuilder& pBuilder);
61  ELFDynObjReader* createDynObjReader(IRBuilder& pBuilder);
62  ELFBinaryReader* createBinaryReader(IRBuilder& pBuilder);
63  ELFObjectWriter* createWriter();
64
65  // -----  output sections  ----- //
66  /// initStdSections - initialize standard sections of the output file.
67  bool initStdSections(ObjectBuilder& pBuilder);
68
69  /// getOutputFormat - get the sections of the output file.
70  const ELFFileFormat* getOutputFormat() const;
71  ELFFileFormat*       getOutputFormat();
72
73  // -----  target symbols ----- //
74  /// initStandardSymbols - initialize standard symbols.
75  /// Some section symbols is undefined in input object, and linkers must set
76  /// up its value. Take __init_array_begin for example. This symbol is an
77  /// undefined symbol in input objects. ObjectLinker must finalize its value
78  /// to the begin of the .init_array section, then relocation enties to
79  /// __init_array_begin can be applied without emission of "undefined
80  /// reference to `__init_array_begin'".
81  bool initStandardSymbols(IRBuilder& pBuilder, Module& pModule);
82
83  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
84  /// then it will ask backend to finalize the symbol value.
85  /// @return ture - if backend set the symbol value sucessfully
86  /// @return false - if backend do not recognize the symbol
87  bool finalizeSymbols() {
88    return (finalizeStandardSymbols() &&
89            finalizeTargetSymbols());
90  }
91
92  /// finalizeStandardSymbols - set the value of standard symbols
93  virtual bool finalizeStandardSymbols();
94
95  /// finalizeTargetSymbols - set the value of target symbols
96  virtual bool finalizeTargetSymbols() = 0;
97
98  /// finalizeTLSSymbol - set the value of a TLS symbol
99  virtual bool finalizeTLSSymbol(LDSymbol& pSymbol);
100
101  size_t sectionStartOffset() const;
102
103  const GNUInfo& getInfo() const { return *m_pInfo; }
104  GNUInfo&       getInfo()       { return *m_pInfo; }
105
106  bool hasTextRel() const { return m_bHasTextRel; }
107
108  bool hasStaticTLS() const { return m_bHasStaticTLS; }
109
110  /// segmentStartAddr - this function returns the start address of the segment
111  uint64_t segmentStartAddr() const;
112
113  /// partialScanRelocation - When doing partial linking, fix the relocation
114  /// offset after section merge
115  void partialScanRelocation(Relocation& pReloc,
116                             Module& pModule,
117                             const LDSection& pSection);
118
119  /// sizeNamePools - compute the size of regular name pools
120  /// In ELF executable files, regular name pools are .symtab, .strtab.,
121  /// .dynsym, .dynstr, and .hash
122  virtual void sizeNamePools(Module& pModule, bool pIsStaticLink);
123
124  /// emitSectionData - emit target-dependent section data
125  virtual uint64_t emitSectionData(const LDSection& pSection,
126                                   MemoryRegion& pRegion) const = 0;
127
128  /// emitRegNamePools - emit regular name pools - .symtab, .strtab
129  virtual void emitRegNamePools(const Module& pModule, MemoryArea& pOutput);
130
131  /// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash
132  virtual void emitDynNamePools(Module& pModule, MemoryArea& pOutput);
133
134  /// emitELFHashTab - emit .hash
135  virtual void emitELFHashTab(const Module::SymbolTable& pSymtab,
136                              MemoryArea& pOutput);
137
138  /// emitGNUHashTab - emit .gnu.hash
139  virtual void emitGNUHashTab(Module::SymbolTable& pSymtab,
140                              MemoryArea& pOutput);
141
142  /// sizeInterp - compute the size of program interpreter's name
143  /// In ELF executables, this is the length of dynamic linker's path name
144  virtual void sizeInterp();
145
146  /// emitInterp - emit the .interp
147  virtual void emitInterp(MemoryArea& pOutput);
148
149  /// getSectionOrder - compute the layout order of the section
150  /// Layout calls this function to get the default order of the pSectHdr.
151  /// If the pSectHdr.type() is LDFileFormat::Target, then getSectionOrder()
152  /// will call getTargetSectionOrder().
153  ///
154  /// If targets favors certain order for general sections, please override
155  /// this function.
156  ///
157  /// @see getTargetSectionOrder
158  virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const;
159
160  /// getTargetSectionOrder - compute the layout order of target section
161  /// If the target favors certain order for the given gSectHdr, please
162  /// override this function.
163  ///
164  /// By default, this function returns the maximun order, and pSectHdr
165  /// will be the last section to be laid out.
166  virtual unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const
167  { return (unsigned int)-1; }
168
169  /// numOfSegments - return the number of segments
170  /// if the target favors other ways to emit program header, please override
171  /// this function
172  size_t numOfSegments() const { return m_ELFSegmentTable.size(); }
173
174  /// elfSegmentTable - return the reference of the elf segment table
175  ELFSegmentFactory&       elfSegmentTable()       { return m_ELFSegmentTable; }
176
177  /// elfSegmentTable - return the reference of the elf segment table
178  const ELFSegmentFactory& elfSegmentTable() const { return m_ELFSegmentTable; }
179
180  /// commonPageSize - the common page size of the target machine
181  uint64_t commonPageSize() const;
182
183  /// abiPageSize - the abi page size of the target machine
184  uint64_t abiPageSize() const;
185
186  /// getSymbolIdx - get the symbol index of ouput symbol table
187  size_t getSymbolIdx(const LDSymbol* pSymbol) const;
188
189  /// allocateCommonSymbols - allocate common symbols in the corresponding
190  /// sections.
191  /// Different concrete target backend may overlap this function.
192  virtual bool allocateCommonSymbols(Module& pModule);
193
194  /// updateSectionFlags - update pTo's flags when merging pFrom
195  /// update the output section flags based on input section flags.
196  virtual bool updateSectionFlags(LDSection& pTo, const LDSection& pFrom);
197
198  /// isSymbolPreemtible - whether the symbol can be preemted by other
199  /// link unit
200  /// @ref Google gold linker, symtab.h:551
201  bool isSymbolPreemptible(const ResolveInfo& pSym) const;
202
203  virtual ResolveInfo::Desc getSymDesc(uint16_t pShndx) const {
204    return ResolveInfo::Define;
205  }
206
207  /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation
208  /// @ref Google gold linker, symtab.h:645
209  bool symbolNeedsDynRel(const ResolveInfo& pSym,
210                         bool pSymHasPLT,
211                         bool isAbsReloc) const;
212
213  // getTDATASymbol - get section symbol of .tdata
214  LDSymbol& getTDATASymbol();
215  const LDSymbol& getTDATASymbol() const;
216
217  /// getTBSSSymbol - get section symbol of .tbss
218  LDSymbol& getTBSSSymbol();
219  const LDSymbol& getTBSSSymbol() const;
220
221  //  -----  relaxation  -----  //
222  /// initBRIslandFactory - initialize the branch island factory for relaxation
223  bool initBRIslandFactory();
224
225  /// initStubFactory - initialize the stub factory for relaxation
226  bool initStubFactory();
227
228  /// getBRIslandFactory
229  BranchIslandFactory* getBRIslandFactory() { return m_pBRIslandFactory; }
230
231  /// getStubFactory
232  StubFactory*         getStubFactory()     { return m_pStubFactory; }
233
234  /// maxBranchOffset - return the max (forward) branch offset of the backend.
235  /// Target can override this function if needed.
236  virtual uint64_t maxBranchOffset() { return (uint64_t)-1; }
237
238protected:
239  uint64_t getSymbolSize(const LDSymbol& pSymbol) const;
240
241  uint64_t getSymbolInfo(const LDSymbol& pSymbol) const;
242
243  uint64_t getSymbolValue(const LDSymbol& pSymbol) const;
244
245  uint64_t getSymbolShndx(const LDSymbol& pSymbol) const;
246
247  /// isTemporary - Whether pSymbol is a local label.
248  virtual bool isTemporary(const LDSymbol& pSymbol) const;
249
250  /// getHashBucketCount - calculate hash bucket count.
251  /// @ref Google gold linker, dynobj.cc:791
252  static unsigned getHashBucketCount(unsigned pNumOfSymbols, bool pIsGNUStyle);
253
254  /// getGNUHashMaskbitslog2 - calculate the number of mask bits in log2
255  /// @ref binutils gold, dynobj.cc:1165
256  unsigned getGNUHashMaskbitslog2(unsigned pNumOfSymbols) const;
257
258  /// isDynamicSymbol
259  /// @ref Google gold linker: symtab.cc:311
260  bool isDynamicSymbol(const LDSymbol& pSymbol);
261
262  /// isDynamicSymbol
263  /// @ref Google gold linker: symtab.cc:311
264  bool isDynamicSymbol(const ResolveInfo& pResolveInfo);
265
266  /// symbolNeedsPLT - return whether the symbol needs a PLT entry
267  /// @ref Google gold linker, symtab.h:596
268  bool symbolNeedsPLT(const ResolveInfo& pSym) const;
269
270  /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation
271  bool symbolNeedsCopyReloc(const Relocation& pReloc,
272                            const ResolveInfo& pSym) const;
273
274  /// symbolHasFinalValue - return true if the symbol's value can be decided at
275  /// link time
276  bool symbolFinalValueIsKnown(const ResolveInfo& pSym) const;
277
278  /// emitSymbol32 - emit an ELF32 symbol
279  void emitSymbol32(llvm::ELF::Elf32_Sym& pSym32,
280                    LDSymbol& pSymbol,
281                    char* pStrtab,
282                    size_t pStrtabsize,
283                    size_t pSymtabIdx);
284
285  /// emitSymbol64 - emit an ELF64 symbol
286  void emitSymbol64(llvm::ELF::Elf64_Sym& pSym64,
287                    LDSymbol& pSymbol,
288                    char* pStrtab,
289                    size_t pStrtabsize,
290                    size_t pSymtabIdx);
291
292  /// checkAndSetHasTextRel - check pSection flag to set HasTextRel
293  void checkAndSetHasTextRel(const LDSection& pSection);
294
295  void setHasStaticTLS(bool pVal = true) { m_bHasStaticTLS = pVal; }
296
297private:
298  /// createProgramHdrs - base on output sections to create the program headers
299  void createProgramHdrs(Module& pModule);
300
301  /// doCreateProgramHdrs - backend can implement this function to create the
302  /// target-dependent segments
303  virtual void doCreateProgramHdrs(Module& pModule) = 0;
304
305  /// setupProgramHdrs - set up the attributes of segments
306  ///  (i.e., offset, addresses, file/mem size, flag,  and alignment)
307  void setupProgramHdrs();
308
309  /// getSegmentFlag - give a section flag and return the corresponding segment
310  /// flag
311  inline uint32_t getSegmentFlag(const uint32_t pSectionFlag)
312  {
313    uint32_t flag = llvm::ELF::PF_R;
314    if (0 != (pSectionFlag & llvm::ELF::SHF_WRITE))
315      flag |= llvm::ELF::PF_W;
316    if (0 != (pSectionFlag & llvm::ELF::SHF_EXECINSTR))
317      flag |= llvm::ELF::PF_X;
318    return flag;
319  }
320
321  /// setupGNUStackInfo - setup the section flag of .note.GNU-stack in output
322  void setupGNUStackInfo(Module& pModule);
323
324  /// setupRelro - setup the offset constraint of PT_RELRO
325  void setupRelro(Module& pModule);
326
327  /// setOutputSectionOffset - helper function to set a group of output sections'
328  /// offset, and set pSectBegin to pStartOffset if pStartOffset is not -1U.
329  void setOutputSectionOffset(Module& pModule,
330                              Module::iterator pSectBegin,
331                              Module::iterator pSectEnd,
332                              uint64_t pStartOffset = -1U);
333
334  /// setOutputSectionOffset - helper function to set output sections' address.
335  void setOutputSectionAddress(Module& pModule,
336                               Module::iterator pSectBegin,
337                               Module::iterator pSectEnd);
338
339  /// layout - layout method
340  void layout(Module& pModule);
341
342  /// preLayout - Backend can do any needed modification before layout
343  void preLayout(Module& pModule, IRBuilder& pBuilder);
344
345  /// postLayout -Backend can do any needed modification after layout
346  void postLayout(Module& pModule, IRBuilder& pBuilder);
347
348  /// preLayout - Backend can do any needed modification before layout
349  virtual void doPreLayout(IRBuilder& pBuilder) = 0;
350
351  /// postLayout -Backend can do any needed modification after layout
352  virtual void doPostLayout(Module& pModule, IRBuilder& pLinker) = 0;
353
354  /// postProcessing - Backend can do any needed modification in the final stage
355  void postProcessing(MemoryArea& pOutput);
356
357  /// dynamic - the dynamic section of the target machine.
358  virtual ELFDynamic& dynamic() = 0;
359
360  /// dynamic - the dynamic section of the target machine.
361  virtual const ELFDynamic& dynamic() const = 0;
362
363  /// relax - the relaxation pass
364  bool relax(Module& pModule, IRBuilder& pBuilder);
365
366  /// mayRelax - Backends should override this function if they need relaxation
367  virtual bool mayRelax() { return false; }
368
369  /// doRelax - Backend can orevride this function to add its relaxation
370  /// implementation. Return true if the output (e.g., .text) is "relaxed"
371  /// (i.e. layout is changed), and set pFinished to true if everything is fit,
372  /// otherwise set it to false.
373  virtual bool doRelax(Module& pModule, IRBuilder& pBuilder, bool& pFinished)
374  { return false; }
375
376  /// getRelEntrySize - the size in BYTE of rel type relocation
377  virtual size_t getRelEntrySize() = 0;
378
379  /// getRelEntrySize - the size in BYTE of rela type relocation
380  virtual size_t getRelaEntrySize() = 0;
381
382protected:
383  // Based on Kind in LDFileFormat to define basic section orders for ELF, and
384  // refer gold linker to add more enumerations to handle Regular and BSS kind
385  enum SectionOrder {
386    SHO_INTERP = 1,          // .interp
387    SHO_RO_NOTE,             // .note.ABI-tag, .note.gnu.build-id
388    SHO_NAMEPOOL,            // *.hash, .dynsym, .dynstr
389    SHO_RELOCATION,          // .rel.*, .rela.*
390    SHO_REL_PLT,             // .rel.plt should come after other .rel.*
391    SHO_INIT,                // .init
392    SHO_PLT,                 // .plt
393    SHO_TEXT,                // .text
394    SHO_FINI,                // .fini
395    SHO_RO,                  // .rodata
396    SHO_EXCEPTION,           // .eh_frame_hdr, .eh_frame, .gcc_except_table
397    SHO_TLS_DATA,            // .tdata
398    SHO_TLS_BSS,             // .tbss
399    SHO_RELRO_LOCAL,         // .data.rel.ro.local
400    SHO_RELRO,               // .data.rel.ro,
401    SHO_RELRO_LAST,          // for x86 to adjust .got if needed
402    SHO_NON_RELRO_FIRST,     // for x86 to adjust .got.plt if needed
403    SHO_DATA,                // .data
404    SHO_LARGE_DATA,          // .ldata
405    SHO_RW_NOTE,             //
406    SHO_SMALL_DATA,          // .sdata
407    SHO_SMALL_BSS,           // .sbss
408    SHO_BSS,                 // .bss
409    SHO_LARGE_BSS,           // .lbss
410    SHO_UNDEFINED,           // default order
411    SHO_STRTAB               // .strtab
412  };
413
414  typedef std::pair<LDSection*, unsigned int> SHOEntry;
415
416  struct SHOCompare
417  {
418    bool operator()(const SHOEntry& X, const SHOEntry& Y) const
419    { return X.second < Y.second; }
420  };
421
422  struct SymCompare
423  {
424    bool operator()(const LDSymbol* X, const LDSymbol* Y) const
425    { return (X==Y); }
426  };
427
428  // for gnu style hash table
429  struct DynsymCompare
430  {
431    bool needGNUHash(const LDSymbol& X) const;
432
433    bool operator()(const LDSymbol* X, const LDSymbol* Y) const;
434  };
435
436  struct SymPtrHash
437  {
438    size_t operator()(const LDSymbol* pKey) const
439    {
440      return (unsigned((uintptr_t)pKey) >> 4) ^
441             (unsigned((uintptr_t)pKey) >> 9);
442    }
443  };
444
445  typedef HashEntry<LDSymbol*, size_t, SymCompare> SymHashEntryType;
446  typedef HashTable<SymHashEntryType,
447                    SymPtrHash,
448                    EntryFactory<SymHashEntryType> > HashTableType;
449
450
451protected:
452  ELFObjectReader* m_pObjectReader;
453
454  // -----  file formats  ----- //
455  ELFDynObjFileFormat* m_pDynObjFileFormat;
456  ELFExecFileFormat*   m_pExecFileFormat;
457  ELFObjectFileFormat* m_pObjectFileFormat;
458
459  // GNUInfo
460  GNUInfo* m_pInfo;
461
462  // ELF segment factory
463  ELFSegmentFactory m_ELFSegmentTable;
464
465  // branch island factory
466  BranchIslandFactory* m_pBRIslandFactory;
467
468  // stub factory
469  StubFactory* m_pStubFactory;
470
471  // map the LDSymbol to its index in the output symbol table
472  HashTableType* m_pSymIndexMap;
473
474  // section .eh_frame_hdr
475  EhFrameHdr* m_pEhFrameHdr;
476
477  // ----- dynamic flags ----- //
478  // DF_TEXTREL of DT_FLAGS
479  bool m_bHasTextRel;
480
481  // DF_STATIC_TLS of DT_FLAGS
482  bool m_bHasStaticTLS;
483
484  // -----  standard symbols  ----- //
485  // section symbols
486  LDSymbol* f_pPreInitArrayStart;
487  LDSymbol* f_pPreInitArrayEnd;
488  LDSymbol* f_pInitArrayStart;
489  LDSymbol* f_pInitArrayEnd;
490  LDSymbol* f_pFiniArrayStart;
491  LDSymbol* f_pFiniArrayEnd;
492  LDSymbol* f_pStack;
493  LDSymbol* f_pDynamic;
494
495  // section symbols for .tdata and .tbss
496  LDSymbol* f_pTDATA;
497  LDSymbol* f_pTBSS;
498
499  // segment symbols
500  LDSymbol* f_pExecutableStart;
501  LDSymbol* f_pEText;
502  LDSymbol* f_p_EText;
503  LDSymbol* f_p__EText;
504  LDSymbol* f_pEData;
505  LDSymbol* f_p_EData;
506  LDSymbol* f_pBSSStart;
507  LDSymbol* f_pEnd;
508  LDSymbol* f_p_End;
509};
510
511} // namespace of mcld
512
513#endif
514
515