blob: 615a5eff48f10f5236c582c6f79bc8f96e2b4fce [file] [log] [blame]
Michael J. Spencer84487f12015-07-24 21:03:07 +00001//===- 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"
Rui Ueyamae44524d2015-07-28 00:17:25 +000013#include "llvm/ADT/DenseMap.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014
15using namespace llvm;
16using namespace llvm::ELF;
17using namespace llvm::object;
18
19using namespace lld;
20using namespace lld::elf2;
21
22static const int PageSize = 4096;
23
24template <class ELFT> Writer<ELFT>::Writer(SymbolTable<ELFT> *T) : Symtab(T) {}
25template <class ELFT> Writer<ELFT>::~Writer() {}
26
27// The main function of the writer.
28template <class ELFT> void Writer<ELFT>::write(StringRef OutputPath) {
29 createSections();
30 assignAddresses();
31 openFile(OutputPath);
32 writeHeader();
33 writeSections();
34 error(Buffer->commit());
35}
36
37void OutputSection::setVA(uint64_t VA) {
38 Header.sh_addr = VA;
39 for (Chunk *C : Chunks)
40 C->setVA(C->getVA() + VA);
41}
42
43void OutputSection::setFileOffset(uint64_t Off) {
44 if (Header.sh_size == 0)
45 return;
46 Header.sh_offset = Off;
47 for (Chunk *C : Chunks)
48 C->setFileOff(C->getFileOff() + Off);
49}
50
51void OutputSection::addChunk(Chunk *C) {
52 Chunks.push_back(C);
53 C->setOutputSection(this);
54 uint64_t Off = Header.sh_size;
55 Off = RoundUpToAlignment(Off, C->getAlign());
56 C->setVA(Off);
57 C->setFileOff(Off);
58 Off += C->getSize();
59 Header.sh_size = Off;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000060 if (auto SC = dyn_cast<SectionChunk<ELF64LE>>(C))
61 Header.sh_type = SC->getSectionHdr()->sh_type;
62}
63
64template <class ELFT>
65void OutputSection::writeHeaderTo(Elf_Shdr_Impl<ELFT> *SHdr) {
66 SHdr->sh_name = Header.sh_name;
67 SHdr->sh_type = Header.sh_type;
68 SHdr->sh_flags = Header.sh_flags;
69 SHdr->sh_addr = Header.sh_addr;
70 SHdr->sh_offset = Header.sh_offset;
71 SHdr->sh_size = Header.sh_size;
72 SHdr->sh_link = Header.sh_link;
73 SHdr->sh_info = Header.sh_info;
74 SHdr->sh_addralign = Header.sh_addralign;
75 SHdr->sh_entsize = Header.sh_entsize;
Michael J. Spencer84487f12015-07-24 21:03:07 +000076}
77
Michael J. Spencer84487f12015-07-24 21:03:07 +000078// Create output section objects and add them to OutputSections.
79template <class ELFT> void Writer<ELFT>::createSections() {
Rui Ueyamae44524d2015-07-28 00:17:25 +000080 SmallDenseMap<StringRef, OutputSection *> Map;
Rui Ueyama06d7bd22015-07-26 00:50:15 +000081 for (Chunk *C : Symtab->getChunks()) {
82 OutputSection *&Sec = Map[C->getSectionName()];
83 if (!Sec) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000084 Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
85 OutputSections.push_back(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +000086 }
87 Sec->addChunk(C);
88 }
89}
90
91// Visits all sections to assign incremental, non-overlapping RVAs and
92// file offsets.
93template <class ELFT> void Writer<ELFT>::assignAddresses() {
Michael J. Spencer8039dae22015-07-29 00:30:10 +000094 SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize);
Michael J. Spencer84487f12015-07-24 21:03:07 +000095 uint64_t VA = 0x1000; // The first page is kept unmapped.
96 uint64_t FileOff = SizeOfHeaders;
97 for (OutputSection *Sec : OutputSections) {
98 Sec->setVA(VA);
99 Sec->setFileOffset(FileOff);
100 VA += RoundUpToAlignment(Sec->getSize(), PageSize);
101 FileOff += RoundUpToAlignment(Sec->getSize(), 8);
102 }
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000103 // Add space for section headers.
104 SectionHeaderOff = FileOff;
105 FileOff += (OutputSections.size() + 1) * sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000106 FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8);
107}
108
109template <class ELFT> void Writer<ELFT>::writeHeader() {
110 uint8_t *Buf = Buffer->getBufferStart();
111 auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf);
112 EHdr->e_ident[EI_MAG0] = 0x7F;
113 EHdr->e_ident[EI_MAG1] = 0x45;
114 EHdr->e_ident[EI_MAG2] = 0x4C;
115 EHdr->e_ident[EI_MAG3] = 0x46;
116 EHdr->e_ident[EI_CLASS] = ELFCLASS64;
117 EHdr->e_ident[EI_DATA] = ELFDATA2LSB;
118 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
119 EHdr->e_ident[EI_OSABI] = ELFOSABI_GNU;
120
121 EHdr->e_type = ET_EXEC;
122 EHdr->e_machine = EM_X86_64;
123 EHdr->e_version = EV_CURRENT;
124 EHdr->e_entry = 0x401000;
125 EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000126 EHdr->e_shoff = SectionHeaderOff;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000127 EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>);
128 EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>);
129 EHdr->e_phnum = 1;
130 EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000131 EHdr->e_shnum = OutputSections.size() + 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000132 EHdr->e_shstrndx = 0;
133
134 auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff);
135 PHdrs->p_type = PT_LOAD;
136 PHdrs->p_flags = PF_R | PF_X;
137 PHdrs->p_offset = 0x0000;
138 PHdrs->p_vaddr = 0x400000;
139 PHdrs->p_paddr = PHdrs->p_vaddr;
140 PHdrs->p_filesz = FileSize;
141 PHdrs->p_memsz = FileSize;
142 PHdrs->p_align = 0x4000;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000143
144 auto SHdrs = reinterpret_cast<Elf_Shdr_Impl<ELFT> *>(Buf + EHdr->e_shoff);
145 // First entry is null.
146 ++SHdrs;
147 for (OutputSection *Sec : OutputSections)
148 Sec->writeHeaderTo<ELFT>(SHdrs++);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000149}
150
151template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
152 std::error_code EC = FileOutputBuffer::create(Path, FileSize, Buffer,
153 FileOutputBuffer::F_executable);
154 error(EC, Twine("failed to open ") + Path);
155}
156
157// Write section contents to a mmap'ed file.
158template <class ELFT> void Writer<ELFT>::writeSections() {
159 uint8_t *Buf = Buffer->getBufferStart();
160 for (OutputSection *Sec : OutputSections) {
161 for (Chunk *C : Sec->getChunks())
162 C->writeTo(Buf);
163 }
164}
165
166namespace lld {
167namespace elf2 {
168template class Writer<ELF32LE>;
169template class Writer<ELF32BE>;
170template class Writer<ELF64LE>;
171template class Writer<ELF64BE>;
172}
173}