Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame^] | 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 "Chunks.h" |
| 12 | #include "Driver.h" |
| 13 | |
| 14 | using namespace llvm; |
| 15 | using namespace llvm::ELF; |
| 16 | using namespace llvm::object; |
| 17 | |
| 18 | using namespace lld; |
| 19 | using namespace lld::elf2; |
| 20 | |
| 21 | static const int PageSize = 4096; |
| 22 | |
| 23 | template <class ELFT> Writer<ELFT>::Writer(SymbolTable<ELFT> *T) : Symtab(T) {} |
| 24 | template <class ELFT> Writer<ELFT>::~Writer() {} |
| 25 | |
| 26 | // The main function of the writer. |
| 27 | template <class ELFT> void Writer<ELFT>::write(StringRef OutputPath) { |
| 28 | createSections(); |
| 29 | assignAddresses(); |
| 30 | openFile(OutputPath); |
| 31 | writeHeader(); |
| 32 | writeSections(); |
| 33 | error(Buffer->commit()); |
| 34 | } |
| 35 | |
| 36 | void OutputSection::setVA(uint64_t VA) { |
| 37 | Header.sh_addr = VA; |
| 38 | for (Chunk *C : Chunks) |
| 39 | C->setVA(C->getVA() + VA); |
| 40 | } |
| 41 | |
| 42 | void OutputSection::setFileOffset(uint64_t Off) { |
| 43 | if (Header.sh_size == 0) |
| 44 | return; |
| 45 | Header.sh_offset = Off; |
| 46 | for (Chunk *C : Chunks) |
| 47 | C->setFileOff(C->getFileOff() + Off); |
| 48 | } |
| 49 | |
| 50 | void OutputSection::addChunk(Chunk *C) { |
| 51 | Chunks.push_back(C); |
| 52 | C->setOutputSection(this); |
| 53 | uint64_t Off = Header.sh_size; |
| 54 | Off = RoundUpToAlignment(Off, C->getAlign()); |
| 55 | C->setVA(Off); |
| 56 | C->setFileOff(Off); |
| 57 | Off += C->getSize(); |
| 58 | Header.sh_size = Off; |
| 59 | } |
| 60 | |
| 61 | static int compare(const Chunk *A, const Chunk *B) { |
| 62 | return A->getSectionName() < B->getSectionName(); |
| 63 | } |
| 64 | |
| 65 | // Create output section objects and add them to OutputSections. |
| 66 | template <class ELFT> void Writer<ELFT>::createSections() { |
| 67 | std::vector<Chunk *> Chunks = Symtab->getChunks(); |
| 68 | if (Chunks.empty()) |
| 69 | return; |
| 70 | std::sort(Chunks.begin(), Chunks.end(), compare); |
| 71 | |
| 72 | Chunk *Prev = nullptr; |
| 73 | OutputSection *Sec = nullptr; |
| 74 | for (Chunk *C : Chunks) { |
| 75 | if (Prev == nullptr || Prev->getSectionName() != C->getSectionName()) { |
| 76 | Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName()); |
| 77 | OutputSections.push_back(Sec); |
| 78 | Prev = C; |
| 79 | } |
| 80 | Sec->addChunk(C); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | // Visits all sections to assign incremental, non-overlapping RVAs and |
| 85 | // file offsets. |
| 86 | template <class ELFT> void Writer<ELFT>::assignAddresses() { |
| 87 | SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>) + |
| 88 | sizeof(Elf_Shdr_Impl<ELFT>) * |
| 89 | OutputSections.size(), |
| 90 | PageSize); |
| 91 | uint64_t VA = 0x1000; // The first page is kept unmapped. |
| 92 | uint64_t FileOff = SizeOfHeaders; |
| 93 | for (OutputSection *Sec : OutputSections) { |
| 94 | Sec->setVA(VA); |
| 95 | Sec->setFileOffset(FileOff); |
| 96 | VA += RoundUpToAlignment(Sec->getSize(), PageSize); |
| 97 | FileOff += RoundUpToAlignment(Sec->getSize(), 8); |
| 98 | } |
| 99 | SizeOfImage = SizeOfHeaders + RoundUpToAlignment(VA - 0x1000, PageSize); |
| 100 | FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8); |
| 101 | } |
| 102 | |
| 103 | template <class ELFT> void Writer<ELFT>::writeHeader() { |
| 104 | uint8_t *Buf = Buffer->getBufferStart(); |
| 105 | auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf); |
| 106 | EHdr->e_ident[EI_MAG0] = 0x7F; |
| 107 | EHdr->e_ident[EI_MAG1] = 0x45; |
| 108 | EHdr->e_ident[EI_MAG2] = 0x4C; |
| 109 | EHdr->e_ident[EI_MAG3] = 0x46; |
| 110 | EHdr->e_ident[EI_CLASS] = ELFCLASS64; |
| 111 | EHdr->e_ident[EI_DATA] = ELFDATA2LSB; |
| 112 | EHdr->e_ident[EI_VERSION] = EV_CURRENT; |
| 113 | EHdr->e_ident[EI_OSABI] = ELFOSABI_GNU; |
| 114 | |
| 115 | EHdr->e_type = ET_EXEC; |
| 116 | EHdr->e_machine = EM_X86_64; |
| 117 | EHdr->e_version = EV_CURRENT; |
| 118 | EHdr->e_entry = 0x401000; |
| 119 | EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>); |
| 120 | EHdr->e_shoff = 0; |
| 121 | EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>); |
| 122 | EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>); |
| 123 | EHdr->e_phnum = 1; |
| 124 | EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>); |
| 125 | EHdr->e_shnum = 0; |
| 126 | EHdr->e_shstrndx = 0; |
| 127 | |
| 128 | auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff); |
| 129 | PHdrs->p_type = PT_LOAD; |
| 130 | PHdrs->p_flags = PF_R | PF_X; |
| 131 | PHdrs->p_offset = 0x0000; |
| 132 | PHdrs->p_vaddr = 0x400000; |
| 133 | PHdrs->p_paddr = PHdrs->p_vaddr; |
| 134 | PHdrs->p_filesz = FileSize; |
| 135 | PHdrs->p_memsz = FileSize; |
| 136 | PHdrs->p_align = 0x4000; |
| 137 | } |
| 138 | |
| 139 | template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) { |
| 140 | std::error_code EC = FileOutputBuffer::create(Path, FileSize, Buffer, |
| 141 | FileOutputBuffer::F_executable); |
| 142 | error(EC, Twine("failed to open ") + Path); |
| 143 | } |
| 144 | |
| 145 | // Write section contents to a mmap'ed file. |
| 146 | template <class ELFT> void Writer<ELFT>::writeSections() { |
| 147 | uint8_t *Buf = Buffer->getBufferStart(); |
| 148 | for (OutputSection *Sec : OutputSections) { |
| 149 | for (Chunk *C : Sec->getChunks()) |
| 150 | C->writeTo(Buf); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | namespace lld { |
| 155 | namespace elf2 { |
| 156 | template class Writer<ELF32LE>; |
| 157 | template class Writer<ELF32BE>; |
| 158 | template class Writer<ELF64LE>; |
| 159 | template class Writer<ELF64BE>; |
| 160 | } |
| 161 | } |