GNULDBackend.h revision 22add6ff3426df1a85089fe6a6e1597ee3b6f300
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/ELFDynObjWriter.h> 26#include <mcld/LD/ELFExecWriter.h> 27#include <mcld/LD/ELFObjectWriter.h> 28#include <mcld/LD/ELFSegment.h> 29#include <mcld/LD/ELFSegmentFactory.h> 30#include <mcld/Target/ELFDynamic.h> 31 32#include <mcld/Support/GCFactory.h> 33#include <mcld/Module.h> 34 35namespace mcld { 36 37class Module; 38class LinkerConfig; 39class Layout; 40class EhFrame; 41class EhFrameHdr; 42class BranchIslandFactory; 43class StubFactory; 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); 53 54public: 55 virtual ~GNULDBackend(); 56 57 // ----- readers/writers ----- // 58 GNUArchiveReader* createArchiveReader(Module& pModule); 59 ELFObjectReader* createObjectReader(FragmentLinker& pLinker); 60 ELFDynObjReader* createDynObjReader(FragmentLinker& pLinker); 61 ELFObjectWriter* createObjectWriter(FragmentLinker& pLinker); 62 ELFDynObjWriter* createDynObjWriter(FragmentLinker& pLinker); 63 ELFExecWriter* createExecWriter(FragmentLinker& pLinker); 64 65 // ----- output sections ----- // 66 /// initStdSections - initialize standard sections of the output file. 67 bool initStdSections(ObjectBuilder& pBuilder); 68 69 /// getOutputFormat - get the sections of the output file. 70 const ELFFileFormat* getOutputFormat() const; 71 ELFFileFormat* getOutputFormat(); 72 73 // ----- target symbols ----- // 74 /// initStandardSymbols - initialize standard symbols. 75 /// Some section symbols is undefined in input object, and linkers must set 76 /// up its value. Take __init_array_begin for example. This symbol is an 77 /// undefined symbol in input objects. FragmentLinker must finalize its value 78 /// to the begin of the .init_array section, then relocation enties to 79 /// __init_array_begin can be applied without emission of "undefined 80 /// reference to `__init_array_begin'". 81 bool initStandardSymbols(FragmentLinker& pLinker, Module& pModule); 82 83 /// finalizeSymbol - Linker checks pSymbol.reserved() if it's not zero, 84 /// then it will ask backend to finalize the symbol value. 85 /// @return ture - if backend set the symbol value sucessfully 86 /// @return false - if backend do not recognize the symbol 87 bool finalizeSymbols(FragmentLinker& pLinker) { 88 return (finalizeStandardSymbols(pLinker) && 89 finalizeTargetSymbols(pLinker)); 90 } 91 92 /// finalizeStandardSymbols - set the value of standard symbols 93 virtual bool finalizeStandardSymbols(FragmentLinker& pLinker); 94 95 /// finalizeTargetSymbols - set the value of target symbols 96 virtual bool finalizeTargetSymbols(FragmentLinker& pLinker) = 0; 97 98 /// finalizeTLSSymbol - set the value of a TLS symbol 99 virtual bool finalizeTLSSymbol(LDSymbol& pSymbol); 100 101 size_t sectionStartOffset() const; 102 103 /// The return value of machine() it the same as e_machine in the ELF header*/ 104 virtual uint32_t machine() const = 0; 105 106 /// ELFVersion - the value of e_ident[EI_VERSION] 107 virtual uint8_t ELFVersion() const 108 { return llvm::ELF::EV_CURRENT; } 109 110 /// OSABI - the value of e_ident[EI_OSABI] 111 virtual uint8_t OSABI() const = 0; 112 113 /// ABIVersion - the value of e_ident[EI_ABIVRESION] 114 virtual uint8_t ABIVersion() const = 0; 115 116 /// flags - the value of ElfXX_Ehdr::e_flags 117 virtual uint64_t flags() const = 0; 118 119 /// entry - the symbol name of the entry point 120 virtual const char* entry() const 121 { return "_start"; } 122 123 /// dyld - the name of the default dynamic linker 124 /// target may override this function if needed. 125 /// @ref gnu ld, bfd/elf32-i386.c:521 126 virtual const char* dyld() const 127 { return "/usr/lib/libc.so.1"; } 128 129 /// defaultTextSegmentAddr - target should specify its own default start address 130 /// of the text segment. esp. for exec. 131 virtual uint64_t defaultTextSegmentAddr() const 132 { return 0x0; } 133 134 bool hasTextRel() const 135 { return m_bHasTextRel; } 136 137 bool hasStaticTLS() const 138 { return m_bHasStaticTLS; } 139 140 /// segmentStartAddr - this function returns the start address of the segment 141 uint64_t segmentStartAddr(const FragmentLinker& pLinker) const; 142 143 /// partialScanRelocation - When doing partial linking, fix the relocation 144 /// offset after section merge 145 void partialScanRelocation(Relocation& pReloc, 146 FragmentLinker& pLinker, 147 Module& pModule, 148 const LDSection& pSection); 149 150 /// sizeNamePools - compute the size of regular name pools 151 /// In ELF executable files, regular name pools are .symtab, .strtab., 152 /// .dynsym, .dynstr, and .hash 153 virtual void sizeNamePools(const Module& pModule, bool pIsStaticLink); 154 155 /// emitSectionData - emit target-dependent section data 156 virtual uint64_t emitSectionData(const LDSection& pSection, 157 MemoryRegion& pRegion) const = 0; 158 159 /// emitRegNamePools - emit regular name pools - .symtab, .strtab 160 virtual void emitRegNamePools(const Module& pModule, 161 MemoryArea& pOutput); 162 163 /// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash 164 virtual void emitDynNamePools(const Module& pModule, 165 MemoryArea& pOutput); 166 167 /// sizeInterp - compute the size of program interpreter's name 168 /// In ELF executables, this is the length of dynamic linker's path name 169 virtual void sizeInterp(); 170 171 /// emitInterp - emit the .interp 172 virtual void emitInterp(MemoryArea& pOutput); 173 174 /// getSectionOrder - compute the layout order of the section 175 /// Layout calls this function to get the default order of the pSectHdr. 176 /// If the pSectHdr.type() is LDFileFormat::Target, then getSectionOrder() 177 /// will call getTargetSectionOrder(). 178 /// 179 /// If targets favors certain order for general sections, please override 180 /// this function. 181 /// 182 /// @see getTargetSectionOrder 183 virtual unsigned int getSectionOrder(const LDSection& pSectHdr) const; 184 185 /// getTargetSectionOrder - compute the layout order of target section 186 /// If the target favors certain order for the given gSectHdr, please 187 /// override this function. 188 /// 189 /// By default, this function returns the maximun order, and pSectHdr 190 /// will be the last section to be laid out. 191 virtual unsigned int getTargetSectionOrder(const LDSection& pSectHdr) const 192 { return (unsigned int)-1; } 193 194 /// numOfSegments - return the number of segments 195 /// if the target favors other ways to emit program header, please override 196 /// this function 197 virtual unsigned int numOfSegments() const 198 { return m_ELFSegmentTable.size(); } 199 200 /// elfSegmentTable - return the reference of the elf segment table 201 ELFSegmentFactory& elfSegmentTable() 202 { return m_ELFSegmentTable; } 203 204 /// elfSegmentTable - return the reference of the elf segment table 205 const ELFSegmentFactory& elfSegmentTable() const 206 { return m_ELFSegmentTable; } 207 208 /// commonPageSize - the common page size of the target machine, and we set it 209 /// to 4K here. If target favors the different size, please override this 210 /// function 211 virtual uint64_t commonPageSize() const; 212 213 /// abiPageSize - the abi page size of the target machine, and we set it to 4K 214 /// here. If target favors the different size, please override this function 215 virtual uint64_t abiPageSize() const; 216 217 /// getSymbolIdx - get the symbol index of ouput symbol table 218 size_t getSymbolIdx(LDSymbol* pSymbol) const; 219 220 /// isDefaultExecStack - target should specify whether the stack is default 221 /// executable. If target favors another choice, please override this function 222 virtual bool isDefaultExecStack() const 223 { return true; } 224 225 /// allocateCommonSymbols - allocate common symbols in the corresponding 226 /// sections. 227 /// Different concrete target backend may overlap this function. 228 virtual bool allocateCommonSymbols(Module& pModule); 229 230 /// isSymbolPreemtible - whether the symbol can be preemted by other 231 /// link unit 232 /// @ref Google gold linker, symtab.h:551 233 bool isSymbolPreemptible(const ResolveInfo& pSym) const; 234 235 /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation 236 /// @ref Google gold linker, symtab.h:645 237 bool symbolNeedsDynRel(const FragmentLinker& pLinker, 238 const ResolveInfo& pSym, 239 bool pSymHasPLT, 240 bool isAbsReloc) const; 241 242 // getTDATASymbol - get section symbol of .tdata 243 LDSymbol& getTDATASymbol(); 244 const LDSymbol& getTDATASymbol() const; 245 246 /// getTBSSSymbol - get section symbol of .tbss 247 LDSymbol& getTBSSSymbol(); 248 const LDSymbol& getTBSSSymbol() const; 249 250 // ----- relaxation ----- // 251 /// initBRIslandFactory - initialize the branch island factory for relaxation 252 bool initBRIslandFactory(); 253 254 /// initStubFactory - initialize the stub factory for relaxation 255 bool initStubFactory(); 256 257 /// getBRIslandFactory 258 BranchIslandFactory* getBRIslandFactory() { return m_pBRIslandFactory; } 259 260 /// getStubFactory 261 StubFactory* getStubFactory() { return m_pStubFactory; } 262 263 /// maxBranchOffset - return the max (forward) branch offset of the backend. 264 /// Target can override this function if needed. 265 virtual uint64_t maxBranchOffset() { return (uint64_t)-1; } 266 267protected: 268 uint64_t getSymbolSize(const LDSymbol& pSymbol) const; 269 270 uint64_t getSymbolInfo(const LDSymbol& pSymbol) const; 271 272 uint64_t getSymbolValue(const LDSymbol& pSymbol) const; 273 274 uint64_t getSymbolShndx(const LDSymbol& pSymbol) const; 275 276 /// getHashBucketCount - calculate hash bucket count. 277 /// @ref Google gold linker, dynobj.cc:791 278 static unsigned getHashBucketCount(unsigned pNumOfSymbols, bool pIsGNUStyle); 279 280 /// isDynamicSymbol 281 /// @ref Google gold linker: symtab.cc:311 282 bool isDynamicSymbol(const LDSymbol& pSymbol); 283 284 /// isDynamicSymbol 285 /// @ref Google gold linker: symtab.cc:311 286 bool isDynamicSymbol(const ResolveInfo& pResolveInfo); 287 288 /// symbolNeedsPLT - return whether the symbol needs a PLT entry 289 /// @ref Google gold linker, symtab.h:596 290 bool symbolNeedsPLT(const FragmentLinker& pLinker, 291 const ResolveInfo& pSym) const; 292 293 /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation 294 bool symbolNeedsCopyReloc(const FragmentLinker& pLinker, 295 const Relocation& pReloc, 296 const ResolveInfo& pSym) const; 297 298 /// symbolHasFinalValue - return true if the symbol's value can be decided at 299 /// link time 300 bool symbolFinalValueIsKnown(const FragmentLinker& pLinker, 301 const ResolveInfo& pSym) const; 302 303 /// emitSymbol32 - emit an ELF32 symbol 304 void emitSymbol32(llvm::ELF::Elf32_Sym& pSym32, 305 LDSymbol& pSymbol, 306 char* pStrtab, 307 size_t pStrtabsize, 308 size_t pSymtabIdx); 309 310 /// emitSymbol64 - emit an ELF64 symbol 311 void emitSymbol64(llvm::ELF::Elf64_Sym& pSym64, 312 LDSymbol& pSymbol, 313 char* pStrtab, 314 size_t pStrtabsize, 315 size_t pSymtabIdx); 316 317 /// checkAndSetHasTextRel - check pSection flag to set HasTextRel 318 void checkAndSetHasTextRel(const LDSection& pSection); 319 320 void setHasStaticTLS(bool pVal = true) 321 { m_bHasStaticTLS = pVal; } 322 323private: 324 /// createProgramHdrs - base on output sections to create the program headers 325 void createProgramHdrs(Module& pModule, const FragmentLinker& pLinker); 326 327 /// doCreateProgramHdrs - backend can implement this function to create the 328 /// target-dependent segments 329 virtual void doCreateProgramHdrs(Module& pModule, 330 const FragmentLinker& pLinker) = 0; 331 332 /// setupProgramHdrs - set up the attributes of segments 333 /// (i.e., offset, addresses, file/mem size, flag, and alignment) 334 void setupProgramHdrs(const FragmentLinker& pLinker); 335 336 /// getSegmentFlag - give a section flag and return the corresponding segment 337 /// flag 338 inline uint32_t getSegmentFlag(const uint32_t pSectionFlag) 339 { 340 uint32_t flag = llvm::ELF::PF_R; 341 if (0 != (pSectionFlag & llvm::ELF::SHF_WRITE)) 342 flag |= llvm::ELF::PF_W; 343 if (0 != (pSectionFlag & llvm::ELF::SHF_EXECINSTR)) 344 flag |= llvm::ELF::PF_X; 345 return flag; 346 } 347 348 /// setupGNUStackInfo - setup the section flag of .note.GNU-stack in output 349 void setupGNUStackInfo(Module& pModule, FragmentLinker& pLinker); 350 351 /// setupRelro - setup the offset constraint of PT_RELRO 352 void setupRelro(Module& pModule); 353 354 /// setOutputSectionOffset - helper function to set a group of output sections' 355 /// offset, and set pSectBegin to pStartOffset if pStartOffset is not -1U. 356 void setOutputSectionOffset(Module& pModule, 357 Module::iterator pSectBegin, 358 Module::iterator pSectEnd, 359 uint64_t pStartOffset = -1U); 360 361 /// setOutputSectionOffset - helper function to set output sections' address. 362 void setOutputSectionAddress(FragmentLinker& pLinker, 363 Module& pModule, 364 Module::iterator pSectBegin, 365 Module::iterator pSectEnd); 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 struct SymCompare 440 { 441 bool operator()(const LDSymbol* X, const LDSymbol* Y) const 442 { return (X==Y); } 443 }; 444 445 struct SymPtrHash 446 { 447 size_t operator()(const LDSymbol* pKey) const 448 { 449 return (unsigned((uintptr_t)pKey) >> 4) ^ 450 (unsigned((uintptr_t)pKey) >> 9); 451 } 452 }; 453 454 typedef HashEntry<LDSymbol*, size_t, SymCompare> SymHashEntryType; 455 typedef HashTable<SymHashEntryType, 456 SymPtrHash, 457 EntryFactory<SymHashEntryType> > HashTableType; 458 459 460protected: 461 ELFObjectReader* m_pObjectReader; 462 463 // ----- file formats ----- // 464 ELFDynObjFileFormat* m_pDynObjFileFormat; 465 ELFExecFileFormat* m_pExecFileFormat; 466 ELFObjectFileFormat* m_pObjectFileFormat; 467 468 // ELF segment factory 469 ELFSegmentFactory m_ELFSegmentTable; 470 471 // branch island factory 472 BranchIslandFactory* m_pBRIslandFactory; 473 474 // stub factory 475 StubFactory* m_pStubFactory; 476 477 // map the LDSymbol to its index in the output symbol table 478 HashTableType* m_pSymIndexMap; 479 480 /// m_pEhFrame - section .eh_frame 481 EhFrame* m_pEhFrame; 482 483 // section .eh_frame_hdr 484 EhFrameHdr* m_pEhFrameHdr; 485 486 // ----- dynamic flags ----- // 487 // DF_TEXTREL of DT_FLAGS 488 bool m_bHasTextRel; 489 490 // DF_STATIC_TLS of DT_FLAGS 491 bool m_bHasStaticTLS; 492 493 // ----- standard symbols ----- // 494 // section symbols 495 LDSymbol* f_pPreInitArrayStart; 496 LDSymbol* f_pPreInitArrayEnd; 497 LDSymbol* f_pInitArrayStart; 498 LDSymbol* f_pInitArrayEnd; 499 LDSymbol* f_pFiniArrayStart; 500 LDSymbol* f_pFiniArrayEnd; 501 LDSymbol* f_pStack; 502 LDSymbol* f_pDynamic; 503 504 // section symbols for .tdata and .tbss 505 LDSymbol* f_pTDATA; 506 LDSymbol* f_pTBSS; 507 508 // segment symbols 509 LDSymbol* f_pExecutableStart; 510 LDSymbol* f_pEText; 511 LDSymbol* f_p_EText; 512 LDSymbol* f_p__EText; 513 LDSymbol* f_pEData; 514 LDSymbol* f_p_EData; 515 LDSymbol* f_pBSSStart; 516 LDSymbol* f_pEnd; 517 LDSymbol* f_p_End; 518}; 519 520} // namespace of mcld 521 522#endif 523 524