GNULDBackend.h revision 67e37f1be98c926645219cfb47fab9e90d8c725c
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 275 /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation 276 /// @ref Google gold linker, symtab.h:645 277 bool symbolNeedsDynRel(const ResolveInfo& pSym, 278 bool pSymHasPLT, 279 const MCLDInfo& pLDInfo, 280 const Output& pOutput, 281 bool isAbsReloc) const; 282 283protected: 284 uint64_t getSymbolSize(const LDSymbol& pSymbol) const; 285 286 uint64_t getSymbolInfo(const LDSymbol& pSymbol) const; 287 288 uint64_t getSymbolValue(const LDSymbol& pSymbol) const; 289 290 uint64_t getSymbolShndx(const LDSymbol& pSymbol, const Layout& pLayout) const; 291 292 /// getHashBucketCount - calculate hash bucket count. 293 /// @ref Google gold linker, dynobj.cc:791 294 static unsigned getHashBucketCount(unsigned pNumOfSymbols, bool pIsGNUStyle); 295 296 /// isDynamicSymbol 297 /// @ref Google gold linker: symtab.cc:311 298 static bool isDynamicSymbol(const LDSymbol& pSymbol, const Output& pOutput); 299 300 /// isOutputPIC - return whether the output is position-independent 301 bool isOutputPIC(const Output& pOutput, const MCLDInfo& pInfo) const; 302 303 /// isStaticLink - return whether we're doing static link 304 bool isStaticLink(const Output& pOutput, const MCLDInfo& pInfo) const; 305 306 /// symbolNeedsPLT - return whether the symbol needs a PLT entry 307 /// @ref Google gold linker, symtab.h:596 308 bool symbolNeedsPLT(const ResolveInfo& pSym, 309 const MCLDInfo& pLDInfo, 310 const Output& pOutput) const; 311 312 /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation 313 bool symbolNeedsCopyReloc(const Layout& pLayout, 314 const Relocation& pReloc, 315 const ResolveInfo& pSym, 316 const MCLDInfo& pLDInfo, 317 const Output& pOutput) const; 318 319private: 320 /// createProgramHdrs - base on output sections to create the program headers 321 void createProgramHdrs(Output& pOutput, 322 const MCLDInfo& pInfo); 323 324 /// setupProgramHdrs - set up the attributes of segments 325 /// (i.e., offset, addresses, file/mem size, flag, and alignment) 326 void setupProgramHdrs(const Output& pOutput, const MCLDInfo& pInfo); 327 328 /// getSegmentFlag - give a section flag and return the corresponding segment 329 /// flag 330 inline uint32_t getSegmentFlag(const uint32_t pSectionFlag) 331 { 332 uint32_t flag = llvm::ELF::PF_R; 333 if (0 != (pSectionFlag & llvm::ELF::SHF_WRITE)) 334 flag |= llvm::ELF::PF_W; 335 if (0 != (pSectionFlag & llvm::ELF::SHF_EXECINSTR)) 336 flag |= llvm::ELF::PF_X; 337 return flag; 338 } 339 340 /// createGNUStackInfo - create an output GNU stack section or segment if needed 341 void createGNUStackInfo(const Output& pOutput, 342 const MCLDInfo& pInfo, 343 MCLinker& pLinker); 344 345 /// preLayout - Backend can do any needed modification before layout 346 void preLayout(const Output& pOutput, 347 const MCLDInfo& pInfo, 348 MCLinker& pLinker); 349 350 /// postLayout -Backend can do any needed modification after layout 351 void postLayout(const Output& pOutput, 352 const MCLDInfo& pInfo, 353 MCLinker& pLinker); 354 355 /// preLayout - Backend can do any needed modification before layout 356 virtual void doPreLayout(const Output& pOutput, 357 const MCLDInfo& pInfo, 358 MCLinker& pLinker) = 0; 359 360 /// postLayout -Backend can do any needed modification after layout 361 virtual void doPostLayout(const Output& pOutput, 362 const MCLDInfo& pInfo, 363 MCLinker& pLinker) = 0; 364 365 /// postProcessing - Backend can do any needed modification in the final stage 366 void postProcessing(const Output& pOutput, 367 const MCLDInfo& pInfo, 368 MCLinker& pLinker); 369 370 /// dynamic - the dynamic section of the target machine. 371 virtual ELFDynamic& dynamic() = 0; 372 373 /// dynamic - the dynamic section of the target machine. 374 virtual const ELFDynamic& dynamic() const = 0; 375 376protected: 377 // Based on Kind in LDFileFormat to define basic section orders for ELF, and 378 // refer gold linker to add more enumerations to handle Regular and BSS kind 379 enum SectionOrder { 380 SHO_INTERP = 1, // .interp 381 SHO_RO_NOTE, // .note.ABI-tag, .note.gnu.build-id 382 SHO_NAMEPOOL, // *.hash, .dynsym, .dynstr 383 SHO_RELOCATION, // .rel.*, .rela.* 384 SHO_REL_PLT, // .rel.plt should come after other .rel.* 385 SHO_INIT, // .init 386 SHO_PLT, // .plt 387 SHO_TEXT, // .text 388 SHO_FINI, // .fini 389 SHO_RO, // .rodata 390 SHO_EXCEPTION, // .eh_frame_hdr, .eh_frame, .gcc_except_table 391 SHO_TLS_DATA, // .tdata 392 SHO_TLS_BSS, // .tbss 393 SHO_RELRO_LOCAL, // .data.rel.ro.local 394 SHO_RELRO, // .data.rel.ro, 395 SHO_RELRO_LAST, // for x86 to adjust .got if needed 396 SHO_NON_RELRO_FIRST, // for x86 to adjust .got.plt if needed 397 SHO_DATA, // .data 398 SHO_LARGE_DATA, // .ldata 399 SHO_RW_NOTE, // 400 SHO_SMALL_DATA, // .sdata 401 SHO_SMALL_BSS, // .sbss 402 SHO_BSS, // .bss 403 SHO_LARGE_BSS, // .lbss 404 SHO_UNDEFINED = ~(0U) // default order 405 }; 406 407 typedef HashEntry<LDSymbol*, size_t, SymCompare> HashEntryType; 408 typedef HashTable<HashEntryType, PtrHash, EntryFactory<HashEntryType> > HashTableType; 409 410protected: 411 // ----- readers and writers ----- // 412 GNUArchiveReader* m_pArchiveReader; 413 ELFObjectReader* m_pObjectReader; 414 ELFDynObjReader* m_pDynObjReader; 415 ELFObjectWriter* m_pObjectWriter; 416 ELFDynObjWriter* m_pDynObjWriter; 417 ELFExecWriter* m_pExecWriter; 418 419 // ----- file formats ----- // 420 ELFDynObjFileFormat* m_pDynObjFileFormat; 421 ELFExecFileFormat* m_pExecFileFormat; 422 423 // ELF segment factory 424 ELFSegmentFactory m_ELFSegmentTable; 425 426 // map the LDSymbol to its index in the output symbol table 427 HashTableType* m_pSymIndexMap; 428 429 // section .eh_frame_hdr 430 EhFrameHdr* m_pEhFrameHdr; 431 432 // ----- standard symbols ----- // 433 // section symbols 434 LDSymbol* f_pPreInitArrayStart; 435 LDSymbol* f_pPreInitArrayEnd; 436 LDSymbol* f_pInitArrayStart; 437 LDSymbol* f_pInitArrayEnd; 438 LDSymbol* f_pFiniArrayStart; 439 LDSymbol* f_pFiniArrayEnd; 440 LDSymbol* f_pStack; 441 442 // segment symbols 443 LDSymbol* f_pExecutableStart; 444 LDSymbol* f_pEText; 445 LDSymbol* f_p_EText; 446 LDSymbol* f_p__EText; 447 LDSymbol* f_pEData; 448 LDSymbol* f_p_EData; 449 LDSymbol* f_pBSSStart; 450 LDSymbol* f_pEnd; 451 LDSymbol* f_p_End; 452}; 453 454} // namespace of mcld 455 456#endif 457 458