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