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