GNULDBackend.h revision affc150dc44fab1911775a49636d0ce85333b634
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
15#include <llvm/Support/ELF.h>
16#include <mcld/ADT/HashTable.h>
17#include <mcld/ADT/HashEntry.h>
18#include <mcld/LD/EhFrameHdr.h>
19#include <mcld/LD/ELFDynObjFileFormat.h>
20#include <mcld/LD/ELFDynObjReader.h>
21#include <mcld/LD/ELFDynObjWriter.h>
22#include <mcld/LD/ELFExecFileFormat.h>
23#include <mcld/LD/ELFExecWriter.h>
24#include <mcld/LD/ELFObjectReader.h>
25#include <mcld/LD/ELFObjectWriter.h>
26#include <mcld/LD/ELFSegment.h>
27#include <mcld/LD/ELFSegmentFactory.h>
28#include <mcld/LD/GNUArchiveReader.h>
29#include <mcld/Support/GCFactory.h>
30#include <mcld/Target/ELFDynamic.h>
31#include <mcld/Target/TargetLDBackend.h>
32
33namespace mcld
34{
35
36struct SymCompare
37{
38  bool operator()(const LDSymbol* X, const LDSymbol* Y) const
39  { return (X==Y); }
40};
41
42struct PtrHash
43{
44  size_t operator()(const LDSymbol* pKey) const
45  {
46    return (unsigned((uintptr_t)pKey) >> 4) ^
47           (unsigned((uintptr_t)pKey) >> 9);
48  }
49};
50
51class MCLDInfo;
52class Layout;
53class SymbolCategory;
54
55/** \class GNULDBackend
56 *  \brief GNULDBackend provides a common interface for all GNU Unix-OS
57 *  LDBackend.
58 */
59class GNULDBackend : public TargetLDBackend
60{
61protected:
62  GNULDBackend();
63
64public:
65  virtual ~GNULDBackend();
66
67  // -----  readers/writers  ----- //
68  bool initArchiveReader(MCLinker& pLinker, MCLDInfo& pInfo);
69  bool initObjectReader(MCLinker& pLinker);
70  bool initDynObjReader(MCLinker& pLinker);
71  bool initObjectWriter(MCLinker& pLinker);
72  bool initDynObjWriter(MCLinker& pLinker);
73  bool initExecWriter(MCLinker& pLinker);
74
75  GNUArchiveReader *getArchiveReader();
76  const GNUArchiveReader *getArchiveReader() const;
77
78  ELFObjectReader *getObjectReader();
79  const ELFObjectReader *getObjectReader() const;
80
81  ELFDynObjReader *getDynObjReader();
82  const ELFDynObjReader *getDynObjReader() const;
83
84  ELFObjectWriter *getObjectWriter();
85  const ELFObjectWriter *getObjectWriter() const;
86
87  ELFDynObjWriter *getDynObjWriter();
88  const ELFDynObjWriter *getDynObjWriter() const;
89
90  ELFExecWriter *getExecWriter();
91  const ELFExecWriter *getExecWriter() const;
92
93  // -----  output sections  ----- //
94  /// initExecSections - initialize sections of the output executable file.
95  bool initExecSections(MCLinker& pMCLinker);
96
97  /// initDynObjSections - initialize sections of the output shared object.
98  bool initDynObjSections(MCLinker& pMCLinker);
99
100  /// getOutputFormat - get the sections of the output file.
101  ELFFileFormat* getOutputFormat(const Output& pOutput);
102  const ELFFileFormat* getOutputFormat(const Output& pOutput) const;
103
104  ELFDynObjFileFormat* getDynObjFileFormat();
105  const ELFDynObjFileFormat* getDynObjFileFormat() const;
106
107  ELFExecFileFormat* getExecFileFormat();
108  const ELFExecFileFormat* getExecFileFormat() const;
109
110  // -----  target symbols ----- //
111  /// initStandardSymbols - initialize standard symbols.
112  /// Some section symbols is undefined in input object, and linkers must set
113  /// up its value. Take __init_array_begin for example. This symbol is an
114  /// undefined symbol in input objects. MCLinker must finalize its value
115  /// to the begin of the .init_array section, then relocation enties to
116  /// __init_array_begin can be applied without emission of "undefined
117  /// reference to `__init_array_begin'".
118  bool initStandardSymbols(MCLinker& pLinker, const Output& pOutput);
119
120  /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero,
121  /// then it will ask backend to finalize the symbol value.
122  /// @return ture - if backend set the symbol value sucessfully
123  /// @return false - if backend do not recognize the symbol
124  bool finalizeSymbols(MCLinker& pLinker, const Output& pOutput) {
125    return (finalizeStandardSymbols(pLinker, pOutput) &&
126            finalizeTargetSymbols(pLinker, pOutput));
127  }
128
129  /// finalizeStandardSymbols - set the value of standard symbols
130  virtual bool finalizeStandardSymbols(MCLinker& pLinker,
131                                       const Output& pOutput);
132
133  /// finalizeTargetSymbols - set the value of target symbols
134  virtual bool finalizeTargetSymbols(MCLinker& pLinker,
135                                     const Output& pOutput) = 0;
136
137  size_t sectionStartOffset() const;
138
139  /// The return value of machine() it the same as e_machine in the ELF header*/
140  virtual uint32_t machine() const = 0;
141
142  /// ELFVersion - the value of e_ident[EI_VERSION]
143  virtual uint8_t ELFVersion() const
144  { return llvm::ELF::EV_CURRENT; }
145
146  /// OSABI - the value of e_ident[EI_OSABI]
147  virtual uint8_t OSABI() const = 0;
148
149  /// ABIVersion - the value of e_ident[EI_ABIVRESION]
150  virtual uint8_t ABIVersion() const = 0;
151
152  /// flags - the value of ElfXX_Ehdr::e_flags
153  virtual uint64_t flags() const = 0;
154
155  /// entry - the symbol name of the entry point
156  virtual const char* entry() const
157  { return "_start"; }
158
159  /// dyld - the name of the default dynamic linker
160  /// target may override this function if needed.
161  /// @ref gnu ld, bfd/elf32-i386.c:521
162  virtual const char* dyld() const
163  { return "/usr/lib/libc.so.1"; }
164
165  /// defaultTextSegmentAddr - target should specify its own default start address
166  /// of the text segment. esp. for exec.
167  virtual uint64_t defaultTextSegmentAddr() const
168  { return 0x0; }
169
170  /// segmentStartAddr - this function returns the start address of the segment
171  uint64_t segmentStartAddr(const Output& pOutput,
172                            const MCLDInfo& pInfo) const;
173
174  /// sizeNamePools - compute the size of regular name pools
175  /// In ELF executable files, regular name pools are .symtab, .strtab.,
176  /// .dynsym, .dynstr, and .hash
177  virtual void sizeNamePools(const Output& pOutput,
178                             const SymbolCategory& pSymbols,
179                             const MCLDInfo& pLDInfo);
180
181  /// emitSectionData - emit target-dependent section data
182  virtual uint64_t emitSectionData(const Output& pOutput,
183                                   const LDSection& pSection,
184                                   const MCLDInfo& pInfo,
185                                   const Layout& pLayout,
186                                   MemoryRegion& pRegion) const = 0;
187
188  /// emitRegNamePools - emit regular name pools - .symtab, .strtab
189  virtual void emitRegNamePools(Output& pOutput,
190                                SymbolCategory& pSymbols,
191                                const Layout& pLayout,
192                                const MCLDInfo& pLDInfo);
193
194  /// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash
195  virtual void emitDynNamePools(Output& pOutput,
196                                SymbolCategory& pSymbols,
197                                const Layout& pLayout,
198                                const MCLDInfo& pLDInfo);
199
200  /// sizeInterp - compute the size of program interpreter's name
201  /// In ELF executables, this is the length of dynamic linker's path name
202  virtual void sizeInterp(const Output& pOutput, const MCLDInfo& pLDInfo);
203
204  /// emitInterp - emit the .interp
205  virtual void emitInterp(Output& pOutput, const MCLDInfo& pLDInfo);
206
207  /// getSectionOrder - compute the layout order of the section
208  /// Layout calls this function to get the default order of the pSectHdr.
209  /// If the pSectHdr.type() is LDFileFormat::Target, then getSectionOrder()
210  /// will call getTargetSectionOrder().
211  ///
212  /// If targets favors certain order for general sections, please override
213  /// this function.
214  ///
215  /// @see getTargetSectionOrder
216  virtual unsigned int getSectionOrder(const Output& pOutput,
217                                       const LDSection& pSectHdr,
218                                       const MCLDInfo& pInfo) const;
219
220  /// getTargetSectionOrder - compute the layout order of target section
221  /// If the target favors certain order for the given gSectHdr, please
222  /// override this function.
223  ///
224  /// By default, this function returns the maximun order, and pSectHdr
225  /// will be the last section to be laid out.
226  virtual unsigned int
227  getTargetSectionOrder(const Output& pOutput,
228                        const LDSection& pSectHdr,
229                        const MCLDInfo& pInfo) const
230  { return (unsigned int)-1; }
231
232  /// numOfSegments - return the number of segments
233  /// if the target favors other ways to emit program header, please override
234  /// this function
235  virtual unsigned int numOfSegments() const
236  { return m_ELFSegmentTable.size(); }
237
238  /// elfSegmentTable - return the reference of the elf segment table
239  ELFSegmentFactory& elfSegmentTable()
240  { return m_ELFSegmentTable; }
241
242  /// elfSegmentTable - return the reference of the elf segment table
243  const ELFSegmentFactory& elfSegmentTable() const
244  { return m_ELFSegmentTable; }
245
246  /// commonPageSize - the common page size of the target machine, and we set it
247  /// to 4K here. If target favors the different size, please override this
248  /// function
249  virtual uint64_t commonPageSize(const MCLDInfo& pInfo) const;
250
251  /// abiPageSize - the abi page size of the target machine, and we set it to 4K
252  /// here. If target favors the different size, please override this function
253  virtual uint64_t abiPageSize(const MCLDInfo& pInfo) const;
254
255  /// getSymbolIdx - get the symbol index of ouput symbol table
256  size_t getSymbolIdx(LDSymbol* pSymbol) const;
257
258  /// isDefaultExecStack - target should specify whether the stack is default
259  /// executable. If target favors another choice, please override this function
260  virtual bool isDefaultExecStack() const
261  { return true; }
262
263  /// allocateCommonSymbols - allocate common symbols in the corresponding
264  /// sections.
265  /// Different concrete target backend may overlap this function.
266  virtual bool allocateCommonSymbols(const MCLDInfo& pLDInfo, MCLinker& pLinker) const;
267
268  /// isSymbolPreemtible - whether the symbol can be preemted by other
269  /// link unit
270  /// @ref Google gold linker, symtab.h:551
271  bool isSymbolPreemptible(const ResolveInfo& pSym,
272                           const MCLDInfo& pLDInfo,
273                           const Output& pOutput) const;
274
275protected:
276  uint64_t getSymbolSize(const LDSymbol& pSymbol) const;
277
278  uint64_t getSymbolInfo(const LDSymbol& pSymbol) const;
279
280  uint64_t getSymbolValue(const LDSymbol& pSymbol) const;
281
282  uint64_t getSymbolShndx(const LDSymbol& pSymbol, const Layout& pLayout) const;
283
284  /// getHashBucketCount - calculate hash bucket count.
285  /// @ref Google gold linker, dynobj.cc:791
286  static unsigned getHashBucketCount(unsigned pNumOfSymbols, bool pIsGNUStyle);
287
288  /// isDynamicSymbol
289  /// @ref Google gold linker: symtab.cc:311
290  static bool isDynamicSymbol(const LDSymbol& pSymbol, const Output& pOutput);
291
292  /// isOutputPIC - return whether the output is position-independent
293  bool isOutputPIC(const Output& pOutput, const MCLDInfo& pInfo) const;
294
295  /// isStaticLink - return whether we're doing static link
296  bool isStaticLink(const Output& pOutput, const MCLDInfo& pInfo) const;
297
298public:
299  /// symbolNeedsPLT - return whether the symbol needs a PLT entry
300  /// @ref Google gold linker, symtab.h:596
301  bool symbolNeedsPLT(const ResolveInfo& pSym,
302                      const MCLDInfo& pLDInfo,
303                      const Output& pOutput) const;
304
305  /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation
306  /// @ref Google gold linker, symtab.h:645
307  bool symbolNeedsDynRel(const ResolveInfo& pSym,
308                         bool pSymHasPLT,
309                         const MCLDInfo& pLDInfo,
310                         const Output& pOutput,
311                         bool isAbsReloc) const;
312
313  /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation
314  bool symbolNeedsCopyReloc(const Layout& pLayout,
315                            const Relocation& pReloc,
316                            const ResolveInfo& pSym,
317                            const MCLDInfo& pLDInfo,
318                            const Output& pOutput) const;
319
320private:
321  /// createProgramHdrs - base on output sections to create the program headers
322  void createProgramHdrs(Output& pOutput,
323                         const MCLDInfo& pInfo);
324
325  /// setupProgramHdrs - set up the attributes of segments
326  ///  (i.e., offset, addresses, file/mem size, flag,  and alignment)
327  void setupProgramHdrs(const Output& pOutput, const MCLDInfo& pInfo);
328
329  /// getSegmentFlag - give a section flag and return the corresponding segment
330  /// flag
331  inline uint32_t getSegmentFlag(const uint32_t pSectionFlag)
332  {
333    uint32_t flag = llvm::ELF::PF_R;
334    if (0 != (pSectionFlag & llvm::ELF::SHF_WRITE))
335      flag |= llvm::ELF::PF_W;
336    if (0 != (pSectionFlag & llvm::ELF::SHF_EXECINSTR))
337      flag |= llvm::ELF::PF_X;
338    return flag;
339  }
340
341  /// createGNUStackInfo - create an output GNU stack section or segment if needed
342  void createGNUStackInfo(const Output& pOutput,
343                          const MCLDInfo& pInfo,
344                          MCLinker& pLinker);
345
346  /// preLayout - Backend can do any needed modification before layout
347  void preLayout(const Output& pOutput,
348                 const MCLDInfo& pInfo,
349                 MCLinker& pLinker);
350
351  /// postLayout -Backend can do any needed modification after layout
352  void postLayout(const Output& pOutput,
353                 const MCLDInfo& pInfo,
354                 MCLinker& pLinker);
355
356  /// preLayout - Backend can do any needed modification before layout
357  virtual void doPreLayout(const Output& pOutput,
358                         const MCLDInfo& pInfo,
359                         MCLinker& pLinker) = 0;
360
361  /// postLayout -Backend can do any needed modification after layout
362  virtual void doPostLayout(const Output& pOutput,
363                          const MCLDInfo& pInfo,
364                          MCLinker& pLinker) = 0;
365
366  /// postProcessing - Backend can do any needed modification in the final stage
367  void postProcessing(const Output& pOutput,
368                      const MCLDInfo& pInfo,
369                      MCLinker& pLinker);
370
371  /// dynamic - the dynamic section of the target machine.
372  virtual ELFDynamic& dynamic() = 0;
373
374  /// dynamic - the dynamic section of the target machine.
375  virtual const ELFDynamic& dynamic() const = 0;
376
377protected:
378  // Based on Kind in LDFileFormat to define basic section orders for ELF, and
379  // refer gold linker to add more enumerations to handle Regular and BSS kind
380  enum SectionOrder {
381    SHO_INTERP = 1,          // .interp
382    SHO_RO_NOTE,             // .note.ABI-tag, .note.gnu.build-id
383    SHO_NAMEPOOL,            // *.hash, .dynsym, .dynstr
384    SHO_RELOCATION,          // .rel.*, .rela.*
385    SHO_REL_PLT,             // .rel.plt should come after other .rel.*
386    SHO_INIT,                // .init
387    SHO_PLT,                 // .plt
388    SHO_TEXT,                // .text
389    SHO_FINI,                // .fini
390    SHO_RO,                  // .rodata
391    SHO_EXCEPTION,           // .eh_frame_hdr, .eh_frame, .gcc_except_table
392    SHO_TLS_DATA,            // .tdata
393    SHO_TLS_BSS,             // .tbss
394    SHO_RELRO_LOCAL,         // .data.rel.ro.local
395    SHO_RELRO,               // .data.rel.ro,
396    SHO_RELRO_LAST,          // for x86 to adjust .got if needed
397    SHO_NON_RELRO_FIRST,     // for x86 to adjust .got.plt if needed
398    SHO_DATA,                // .data
399    SHO_LARGE_DATA,          // .ldata
400    SHO_RW_NOTE,             //
401    SHO_SMALL_DATA,          // .sdata
402    SHO_SMALL_BSS,           // .sbss
403    SHO_BSS,                 // .bss
404    SHO_LARGE_BSS,           // .lbss
405    SHO_UNDEFINED = ~(0U)    // default order
406  };
407
408  typedef HashEntry<LDSymbol*, size_t, SymCompare> HashEntryType;
409  typedef HashTable<HashEntryType, PtrHash, EntryFactory<HashEntryType> > HashTableType;
410
411protected:
412  // ----- readers and writers ----- //
413  GNUArchiveReader* m_pArchiveReader;
414  ELFObjectReader* m_pObjectReader;
415  ELFDynObjReader* m_pDynObjReader;
416  ELFObjectWriter* m_pObjectWriter;
417  ELFDynObjWriter* m_pDynObjWriter;
418  ELFExecWriter*   m_pExecWriter;
419
420  // -----  file formats  ----- //
421  ELFDynObjFileFormat* m_pDynObjFileFormat;
422  ELFExecFileFormat* m_pExecFileFormat;
423
424  // ELF segment factory
425  ELFSegmentFactory m_ELFSegmentTable;
426
427  // map the LDSymbol to its index in the output symbol table
428  HashTableType* m_pSymIndexMap;
429
430  // section .eh_frame_hdr
431  EhFrameHdr* m_pEhFrameHdr;
432
433  // -----  standard symbols  ----- //
434  // section symbols
435  LDSymbol* f_pPreInitArrayStart;
436  LDSymbol* f_pPreInitArrayEnd;
437  LDSymbol* f_pInitArrayStart;
438  LDSymbol* f_pInitArrayEnd;
439  LDSymbol* f_pFiniArrayStart;
440  LDSymbol* f_pFiniArrayEnd;
441  LDSymbol* f_pStack;
442
443  // segment symbols
444  LDSymbol* f_pExecutableStart;
445  LDSymbol* f_pEText;
446  LDSymbol* f_p_EText;
447  LDSymbol* f_p__EText;
448  LDSymbol* f_pEData;
449  LDSymbol* f_p_EData;
450  LDSymbol* f_pBSSStart;
451  LDSymbol* f_pEnd;
452  LDSymbol* f_p_End;
453};
454
455} // namespace of mcld
456
457#endif
458
459