1 //===- Writer.cpp ---------------------------------------------------------===// 2 // 3 // The LLVM Linker 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "Writer.h" 11 #include "Config.h" 12 #include "OutputSections.h" 13 #include "SymbolTable.h" 14 #include "Target.h" 15 16 #include "llvm/Support/FileOutputBuffer.h" 17 18 using namespace llvm; 19 using namespace llvm::ELF; 20 using namespace llvm::object; 21 22 using namespace lld; 23 using namespace lld::elf2; 24 25 static const int PageSize = 4096; 26 27 // On freebsd x86_64 the first page cannot be mmaped. 28 // On linux that is controled by vm.mmap_min_addr. At least on some x86_64 29 // installs that is 65536, so the first 15 pages cannot be used. 30 // Given that, the smallest value that can be used in here is 0x10000. 31 // If using 2MB pages, the smallest page aligned address that works is 32 // 0x200000, but it looks like every OS uses 4k pages for executables. 33 // FIXME: This is architecture and OS dependent. 34 static const int VAStart = 0x10000; 35 36 namespace { 37 38 static uint32_t convertSectionFlagsToPHDRFlags(uint64_t Flags) { 39 uint32_t Ret = PF_R; 40 if (Flags & SHF_WRITE) 41 Ret |= PF_W; 42 43 if (Flags & SHF_EXECINSTR) 44 Ret |= PF_X; 45 46 return Ret; 47 } 48 49 template <bool Is64Bits> 50 class ProgramHeader { 51 public: 52 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; 53 typedef 54 typename std::conditional<Is64Bits, Elf64_Phdr, Elf32_Phdr>::type HeaderT; 55 56 ProgramHeader(uintX_t p_type, uintX_t p_flags) { 57 std::memset(&Header, 0, sizeof(HeaderT)); 58 Header.p_type = p_type; 59 Header.p_flags = p_flags; 60 Header.p_align = PageSize; 61 } 62 63 void setValuesFromSection(OutputSectionBase<Is64Bits> &Sec) { 64 Header.p_flags = convertSectionFlagsToPHDRFlags(Sec.getFlags()); 65 Header.p_offset = Sec.getFileOff(); 66 Header.p_vaddr = Sec.getVA(); 67 Header.p_paddr = Header.p_vaddr; 68 Header.p_filesz = Sec.getSize(); 69 Header.p_memsz = Header.p_filesz; 70 Header.p_align = Sec.getAlign(); 71 } 72 73 template <endianness E> 74 void writeHeaderTo(typename ELFFile<ELFType<E, Is64Bits>>::Elf_Phdr *PHDR) { 75 PHDR->p_type = Header.p_type; 76 PHDR->p_flags = Header.p_flags; 77 PHDR->p_offset = Header.p_offset; 78 PHDR->p_vaddr = Header.p_vaddr; 79 PHDR->p_paddr = Header.p_paddr; 80 PHDR->p_filesz = Header.p_filesz; 81 PHDR->p_memsz = Header.p_memsz; 82 PHDR->p_align = Header.p_align; 83 } 84 85 HeaderT Header; 86 bool Closed = false; 87 }; 88 89 // The writer writes a SymbolTable result to a file. 90 template <class ELFT> class Writer { 91 public: 92 typedef typename ELFFile<ELFT>::uintX_t uintX_t; 93 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr; 94 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr; 95 typedef typename ELFFile<ELFT>::Elf_Phdr Elf_Phdr; 96 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; 97 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; 98 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela; 99 Writer(SymbolTable *T) 100 : SymTabSec(*T, StrTabSec, BssSec), DynSymSec(*T, DynStrSec, BssSec), 101 RelaDynSec(DynSymSec, GotSec, T->shouldUseRela()), PltSec(GotSec), 102 HashSec(DynSymSec), DynamicSec(*T, HashSec, RelaDynSec), 103 BssSec(PltSec, GotSec, BssSec, ".bss", SHT_NOBITS, 104 SHF_ALLOC | SHF_WRITE) {} 105 void run(); 106 107 private: 108 void createSections(); 109 template <bool isRela> 110 void scanRelocs(const InputSection<ELFT> &C, 111 iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels); 112 void scanRelocs(const InputSection<ELFT> &C); 113 void assignAddresses(); 114 void openFile(StringRef OutputPath); 115 void writeHeader(); 116 void writeSections(); 117 bool needsInterpSection() const { 118 return !SymTabSec.getSymTable().getSharedFiles().empty() && 119 !Config->DynamicLinker.empty(); 120 } 121 bool needsDynamicSections() const { 122 return !SymTabSec.getSymTable().getSharedFiles().empty() || Config->Shared; 123 } 124 unsigned getVAStart() const { return Config->Shared ? 0 : VAStart; } 125 126 std::unique_ptr<llvm::FileOutputBuffer> Buffer; 127 128 llvm::SpecificBumpPtrAllocator<OutputSection<ELFT>> CAlloc; 129 std::vector<OutputSectionBase<ELFT::Is64Bits> *> OutputSections; 130 unsigned getNumSections() const { return OutputSections.size() + 1; } 131 132 llvm::BumpPtrAllocator PAlloc; 133 std::vector<ProgramHeader<ELFT::Is64Bits> *> PHDRs; 134 ProgramHeader<ELFT::Is64Bits> FileHeaderPHDR{PT_LOAD, PF_R}; 135 ProgramHeader<ELFT::Is64Bits> InterpPHDR{PT_INTERP, 0}; 136 ProgramHeader<ELFT::Is64Bits> DynamicPHDR{PT_DYNAMIC, 0}; 137 138 uintX_t FileSize; 139 uintX_t ProgramHeaderOff; 140 uintX_t SectionHeaderOff; 141 142 StringTableSection<ELFT::Is64Bits> StrTabSec = { /*dynamic=*/false }; 143 StringTableSection<ELFT::Is64Bits> DynStrSec = { /*dynamic=*/true }; 144 145 lld::elf2::SymbolTableSection<ELFT> SymTabSec; 146 lld::elf2::SymbolTableSection<ELFT> DynSymSec; 147 148 RelocationSection<ELFT> RelaDynSec; 149 150 GotSection<ELFT> GotSec; 151 PltSection<ELFT> PltSec; 152 153 HashTableSection<ELFT> HashSec; 154 155 DynamicSection<ELFT> DynamicSec; 156 157 InterpSection<ELFT::Is64Bits> InterpSec; 158 159 OutputSection<ELFT> BssSec; 160 }; 161 } // anonymous namespace 162 163 namespace lld { 164 namespace elf2 { 165 166 template <class ELFT> 167 void writeResult(SymbolTable *Symtab) { Writer<ELFT>(Symtab).run(); } 168 169 template void writeResult<ELF32LE>(SymbolTable *); 170 template void writeResult<ELF32BE>(SymbolTable *); 171 template void writeResult<ELF64LE>(SymbolTable *); 172 template void writeResult<ELF64BE>(SymbolTable *); 173 174 } // namespace elf2 175 } // namespace lld 176 177 // The main function of the writer. 178 template <class ELFT> void Writer<ELFT>::run() { 179 createSections(); 180 assignAddresses(); 181 openFile(Config->OutputFile); 182 writeHeader(); 183 writeSections(); 184 error(Buffer->commit()); 185 } 186 187 template <class ELFT> 188 static int compareSym(const typename ELFFile<ELFT>::Elf_Sym *A, 189 const typename ELFFile<ELFT>::Elf_Sym *B) { 190 uint32_t AN = A->st_name; 191 uint32_t BN = B->st_name; 192 assert(AN != BN); 193 return AN - BN; 194 } 195 196 namespace { 197 template <bool Is64Bits> struct SectionKey { 198 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t; 199 StringRef Name; 200 uint32_t sh_type; 201 uintX_t sh_flags; 202 }; 203 } 204 namespace llvm { 205 template <bool Is64Bits> struct DenseMapInfo<SectionKey<Is64Bits>> { 206 static SectionKey<Is64Bits> getEmptyKey() { 207 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getEmptyKey(), 0, 0}; 208 } 209 static SectionKey<Is64Bits> getTombstoneKey() { 210 return SectionKey<Is64Bits>{DenseMapInfo<StringRef>::getTombstoneKey(), 0, 211 0}; 212 } 213 static unsigned getHashValue(const SectionKey<Is64Bits> &Val) { 214 return hash_combine(Val.Name, Val.sh_type, Val.sh_flags); 215 } 216 static bool isEqual(const SectionKey<Is64Bits> &LHS, 217 const SectionKey<Is64Bits> &RHS) { 218 return DenseMapInfo<StringRef>::isEqual(LHS.Name, RHS.Name) && 219 LHS.sh_type == RHS.sh_type && LHS.sh_flags == RHS.sh_flags; 220 } 221 }; 222 } 223 224 template <class ELFT> 225 static bool cmpAlign(const DefinedCommon<ELFT> *A, 226 const DefinedCommon<ELFT> *B) { 227 return A->MaxAlignment > B->MaxAlignment; 228 } 229 230 template <bool Is64Bits> 231 static bool compSec(OutputSectionBase<Is64Bits> *A, 232 OutputSectionBase<Is64Bits> *B) { 233 // Place SHF_ALLOC sections first. 234 return (A->getFlags() & SHF_ALLOC) && !(B->getFlags() & SHF_ALLOC); 235 } 236 237 // The reason we have to do this early scan is as follows 238 // * To mmap the output file, we need to know the size 239 // * For that, we need to know how many dynamic relocs we will have. 240 // It might be possible to avoid this by outputting the file with write: 241 // * Write the allocated output sections, computing addresses. 242 // * Apply relocations, recording which ones require a dynamic reloc. 243 // * Write the dynamic relocations. 244 // * Write the rest of the file. 245 template <class ELFT> 246 template <bool isRela> 247 void Writer<ELFT>::scanRelocs( 248 const InputSection<ELFT> &C, 249 iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) { 250 typedef Elf_Rel_Impl<ELFT, isRela> RelType; 251 const ObjectFile<ELFT> &File = *C.getFile(); 252 bool IsMips64EL = File.getObj()->isMips64EL(); 253 for (const RelType &RI : Rels) { 254 uint32_t SymIndex = RI.getSymbol(IsMips64EL); 255 SymbolBody *Body = File.getSymbolBody(SymIndex); 256 if (!Body) 257 continue; 258 auto *S = dyn_cast<SharedSymbol<ELFT>>(Body); 259 if (!S) 260 continue; 261 uint32_t Type = RI.getType(IsMips64EL); 262 if (Target->relocNeedsPlt(Type)) { 263 if (Body->isInPlt()) 264 continue; 265 PltSec.addEntry(Body); 266 } 267 if (Target->relocNeedsGot(Type)) { 268 if (Body->isInGot()) 269 continue; 270 GotSec.addEntry(Body); 271 } 272 S->setUsedInDynamicReloc(); 273 RelaDynSec.addReloc({C, RI}); 274 } 275 } 276 277 template <class ELFT> 278 void Writer<ELFT>::scanRelocs(const InputSection<ELFT> &C) { 279 const ObjectFile<ELFT> *File = C.getFile(); 280 ELFFile<ELFT> *EObj = File->getObj(); 281 282 if (!(C.getSectionHdr()->sh_flags & SHF_ALLOC)) 283 return; 284 285 for (const Elf_Shdr *RelSec : C.RelocSections) { 286 if (RelSec->sh_type == SHT_RELA) 287 scanRelocs(C, EObj->relas(RelSec)); 288 else 289 scanRelocs(C, EObj->rels(RelSec)); 290 } 291 } 292 293 template <class ELFT> 294 static void undefError(const SymbolTable &S, const SymbolBody &Sym) { 295 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym; 296 typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range; 297 298 const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym; 299 ELFFileBase *SymFile = nullptr; 300 301 for (const std::unique_ptr<ObjectFileBase> &F : S.getObjectFiles()) { 302 const auto &File = cast<ObjectFile<ELFT>>(*F); 303 Elf_Sym_Range Syms = File.getObj()->symbols(File.getSymbolTable()); 304 if (&SymE > Syms.begin() && &SymE < Syms.end()) 305 SymFile = F.get(); 306 } 307 if (SymFile) 308 error(Twine("undefined symbol: ") + Sym.getName() + " in " + 309 SymFile->getName()); 310 else 311 error(Twine("undefined symbol: ") + Sym.getName()); 312 } 313 314 // Create output section objects and add them to OutputSections. 315 template <class ELFT> void Writer<ELFT>::createSections() { 316 SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map; 317 318 OutputSections.push_back(&BssSec); 319 Map[{BssSec.getName(), BssSec.getType(), BssSec.getFlags()}] = &BssSec; 320 321 auto getSection = [&](StringRef Name, uint32_t sh_type, 322 uintX_t sh_flags) -> OutputSection<ELFT> * { 323 SectionKey<ELFT::Is64Bits> Key{Name, sh_type, sh_flags}; 324 OutputSection<ELFT> *&Sec = Map[Key]; 325 if (!Sec) { 326 Sec = new (CAlloc.Allocate()) OutputSection<ELFT>( 327 PltSec, GotSec, BssSec, Key.Name, Key.sh_type, Key.sh_flags); 328 OutputSections.push_back(Sec); 329 } 330 return Sec; 331 }; 332 333 const SymbolTable &Symtab = SymTabSec.getSymTable(); 334 for (const std::unique_ptr<ObjectFileBase> &FileB : Symtab.getObjectFiles()) { 335 auto &File = cast<ObjectFile<ELFT>>(*FileB); 336 if (!Config->DiscardAll) { 337 Elf_Sym_Range Syms = File.getLocalSymbols(); 338 for (const Elf_Sym &Sym : Syms) { 339 ErrorOr<StringRef> SymName = Sym.getName(File.getStringTable()); 340 if (SymName && !(Config->DiscardLocals && SymName->startswith(".L"))) 341 SymTabSec.addSymbol(*SymName, true); 342 } 343 } 344 for (InputSection<ELFT> *C : File.getSections()) { 345 if (!C) 346 continue; 347 const Elf_Shdr *H = C->getSectionHdr(); 348 OutputSection<ELFT> *Sec = 349 getSection(C->getSectionName(), H->sh_type, H->sh_flags); 350 Sec->addSection(C); 351 scanRelocs(*C); 352 } 353 } 354 355 // FIXME: Try to avoid the extra walk over all global symbols. 356 std::vector<DefinedCommon<ELFT> *> CommonSymbols; 357 for (auto &P : Symtab.getSymbols()) { 358 StringRef Name = P.first; 359 SymbolBody *Body = P.second->Body; 360 if (Body->isStrongUndefined()) 361 undefError<ELFT>(Symtab, *Body); 362 363 if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body)) 364 CommonSymbols.push_back(C); 365 if (!includeInSymtab(*Body)) 366 continue; 367 SymTabSec.addSymbol(Name); 368 369 if (needsDynamicSections() && includeInDynamicSymtab(*Body)) 370 HashSec.addSymbol(Body); 371 } 372 373 // Sort the common symbols by alignment as an heuristic to pack them better. 374 std::stable_sort(CommonSymbols.begin(), CommonSymbols.end(), cmpAlign<ELFT>); 375 uintX_t Off = BssSec.getSize(); 376 for (DefinedCommon<ELFT> *C : CommonSymbols) { 377 const Elf_Sym &Sym = C->Sym; 378 uintX_t Align = C->MaxAlignment; 379 Off = RoundUpToAlignment(Off, Align); 380 C->OffsetInBSS = Off; 381 Off += Sym.st_size; 382 } 383 384 BssSec.setSize(Off); 385 386 OutputSections.push_back(&SymTabSec); 387 OutputSections.push_back(&StrTabSec); 388 389 if (needsDynamicSections()) { 390 if (needsInterpSection()) 391 OutputSections.push_back(&InterpSec); 392 OutputSections.push_back(&DynSymSec); 393 OutputSections.push_back(&HashSec); 394 OutputSections.push_back(&DynamicSec); 395 OutputSections.push_back(&DynStrSec); 396 if (RelaDynSec.hasRelocs()) 397 OutputSections.push_back(&RelaDynSec); 398 if (!GotSec.empty()) 399 OutputSections.push_back(&GotSec); 400 if (!PltSec.empty()) 401 OutputSections.push_back(&PltSec); 402 } 403 404 std::stable_sort(OutputSections.begin(), OutputSections.end(), 405 compSec<ELFT::Is64Bits>); 406 for (unsigned I = 0, N = OutputSections.size(); I < N; ++I) 407 OutputSections[I]->setSectionIndex(I + 1); 408 } 409 410 template <class ELFT> 411 static bool outputSectionHasPHDR(OutputSectionBase<ELFT::Is64Bits> *Sec) { 412 return Sec->getFlags() & SHF_ALLOC; 413 } 414 415 // Visits all sections to assign incremental, non-overlapping RVAs and 416 // file offsets. 417 template <class ELFT> void Writer<ELFT>::assignAddresses() { 418 assert(!OutputSections.empty() && "No output sections to layout!"); 419 uintX_t VA = getVAStart(); 420 uintX_t FileOff = 0; 421 422 FileOff += sizeof(Elf_Ehdr); 423 VA += sizeof(Elf_Ehdr); 424 425 // Reserve space for PHDRs. 426 ProgramHeaderOff = FileOff; 427 FileOff = RoundUpToAlignment(FileOff, PageSize); 428 VA = RoundUpToAlignment(VA, PageSize); 429 430 if (needsInterpSection()) 431 PHDRs.push_back(&InterpPHDR); 432 433 ProgramHeader<ELFT::Is64Bits> *LastPHDR = &FileHeaderPHDR; 434 // Create a PHDR for the file header. 435 PHDRs.push_back(&FileHeaderPHDR); 436 FileHeaderPHDR.Header.p_vaddr = getVAStart(); 437 FileHeaderPHDR.Header.p_paddr = getVAStart(); 438 FileHeaderPHDR.Header.p_align = PageSize; 439 440 for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { 441 StrTabSec.add(Sec->getName()); 442 Sec->finalize(); 443 444 if (Sec->getSize()) { 445 uintX_t Flags = convertSectionFlagsToPHDRFlags(Sec->getFlags()); 446 if (LastPHDR->Header.p_flags != Flags || 447 !outputSectionHasPHDR<ELFT>(Sec)) { 448 // Flags changed. End current PHDR and potentially create a new one. 449 if (!LastPHDR->Closed) { 450 LastPHDR->Header.p_filesz = FileOff - LastPHDR->Header.p_offset; 451 LastPHDR->Header.p_memsz = VA - LastPHDR->Header.p_vaddr; 452 LastPHDR->Closed = true; 453 } 454 455 if (outputSectionHasPHDR<ELFT>(Sec)) { 456 LastPHDR = new (PAlloc) ProgramHeader<ELFT::Is64Bits>(PT_LOAD, Flags); 457 PHDRs.push_back(LastPHDR); 458 VA = RoundUpToAlignment(VA, PageSize); 459 FileOff = RoundUpToAlignment(FileOff, PageSize); 460 LastPHDR->Header.p_offset = FileOff; 461 LastPHDR->Header.p_vaddr = VA; 462 LastPHDR->Header.p_paddr = VA; 463 } 464 } 465 } 466 467 uintX_t Align = Sec->getAlign(); 468 uintX_t Size = Sec->getSize(); 469 if (Sec->getFlags() & SHF_ALLOC) { 470 VA = RoundUpToAlignment(VA, Align); 471 Sec->setVA(VA); 472 VA += Size; 473 } 474 FileOff = RoundUpToAlignment(FileOff, Align); 475 Sec->setFileOffset(FileOff); 476 if (Sec->getType() != SHT_NOBITS) 477 FileOff += Size; 478 } 479 480 // Add a PHDR for the dynamic table. 481 if (needsDynamicSections()) 482 PHDRs.push_back(&DynamicPHDR); 483 484 FileOff += OffsetToAlignment(FileOff, ELFT::Is64Bits ? 8 : 4); 485 486 // Add space for section headers. 487 SectionHeaderOff = FileOff; 488 FileOff += getNumSections() * sizeof(Elf_Shdr); 489 FileSize = FileOff; 490 } 491 492 template <class ELFT> void Writer<ELFT>::writeHeader() { 493 uint8_t *Buf = Buffer->getBufferStart(); 494 auto *EHdr = reinterpret_cast<Elf_Ehdr *>(Buf); 495 EHdr->e_ident[EI_MAG0] = 0x7F; 496 EHdr->e_ident[EI_MAG1] = 0x45; 497 EHdr->e_ident[EI_MAG2] = 0x4C; 498 EHdr->e_ident[EI_MAG3] = 0x46; 499 EHdr->e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; 500 EHdr->e_ident[EI_DATA] = ELFT::TargetEndianness == llvm::support::little 501 ? ELFDATA2LSB 502 : ELFDATA2MSB; 503 EHdr->e_ident[EI_VERSION] = EV_CURRENT; 504 EHdr->e_ident[EI_OSABI] = ELFOSABI_NONE; 505 506 // FIXME: Generalize the segment construction similar to how we create 507 // output sections. 508 const SymbolTable &Symtab = SymTabSec.getSymTable(); 509 510 EHdr->e_type = Config->Shared ? ET_DYN : ET_EXEC; 511 auto &FirstObj = cast<ObjectFile<ELFT>>(*Symtab.getFirstELF()); 512 EHdr->e_machine = FirstObj.getEMachine(); 513 EHdr->e_version = EV_CURRENT; 514 SymbolBody *Entry = Symtab.getEntrySym(); 515 EHdr->e_entry = Entry ? getSymVA(cast<DefinedRegular<ELFT>>(Entry)) : 0; 516 EHdr->e_phoff = ProgramHeaderOff; 517 EHdr->e_shoff = SectionHeaderOff; 518 EHdr->e_ehsize = sizeof(Elf_Ehdr); 519 EHdr->e_phentsize = sizeof(Elf_Phdr); 520 EHdr->e_phnum = PHDRs.size(); 521 EHdr->e_shentsize = sizeof(Elf_Shdr); 522 EHdr->e_shnum = getNumSections(); 523 EHdr->e_shstrndx = StrTabSec.getSectionIndex(); 524 525 // If nothing was merged into the file header PT_LOAD, set the size correctly. 526 if (FileHeaderPHDR.Header.p_filesz == PageSize) 527 FileHeaderPHDR.Header.p_filesz = FileHeaderPHDR.Header.p_memsz = 528 sizeof(Elf_Ehdr) + sizeof(Elf_Phdr) * PHDRs.size(); 529 530 if (needsInterpSection()) 531 InterpPHDR.setValuesFromSection(InterpSec); 532 if (needsDynamicSections()) 533 DynamicPHDR.setValuesFromSection(DynamicSec); 534 535 auto PHdrs = reinterpret_cast<Elf_Phdr *>(Buf + EHdr->e_phoff); 536 for (ProgramHeader<ELFT::Is64Bits> *PHDR : PHDRs) 537 PHDR->template writeHeaderTo<ELFT::TargetEndianness>(PHdrs++); 538 539 auto SHdrs = reinterpret_cast<Elf_Shdr *>(Buf + EHdr->e_shoff); 540 // First entry is null. 541 ++SHdrs; 542 for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) { 543 Sec->setNameOffset(StrTabSec.getFileOff(Sec->getName())); 544 Sec->template writeHeaderTo<ELFT::TargetEndianness>(SHdrs++); 545 } 546 } 547 548 template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) { 549 ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr = 550 FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable); 551 error(BufferOrErr, Twine("failed to open ") + Path); 552 Buffer = std::move(*BufferOrErr); 553 } 554 555 // Write section contents to a mmap'ed file. 556 template <class ELFT> void Writer<ELFT>::writeSections() { 557 uint8_t *Buf = Buffer->getBufferStart(); 558 for (OutputSectionBase<ELFT::Is64Bits> *Sec : OutputSections) 559 Sec->writeTo(Buf + Sec->getFileOff()); 560 } 561