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