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