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