blob: f702bc54d95c10695bab850918e3e03f145a4b45 [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
Michael J. Spencer44fc1c02015-07-29 22:14:50 +000051template <class ELFT>
Michael J. Spencer84487f12015-07-24 21:03:07 +000052void OutputSection::addChunk(Chunk *C) {
53 Chunks.push_back(C);
54 C->setOutputSection(this);
55 uint64_t Off = Header.sh_size;
56 Off = RoundUpToAlignment(Off, C->getAlign());
57 C->setVA(Off);
58 C->setFileOff(Off);
59 Off += C->getSize();
60 Header.sh_size = Off;
Michael J. Spencer44fc1c02015-07-29 22:14:50 +000061 if (auto SC = dyn_cast<SectionChunk<ELFT>>(C)) {
Michael J. Spencer8039dae22015-07-29 00:30:10 +000062 Header.sh_type = SC->getSectionHdr()->sh_type;
Michael J. Spencer44fc1c02015-07-29 22:14:50 +000063 Header.sh_flags |= SC->getSectionHdr()->sh_flags;
64 }
Michael J. Spencer8039dae22015-07-29 00:30:10 +000065}
66
67template <class ELFT>
68void OutputSection::writeHeaderTo(Elf_Shdr_Impl<ELFT> *SHdr) {
69 SHdr->sh_name = Header.sh_name;
70 SHdr->sh_type = Header.sh_type;
71 SHdr->sh_flags = Header.sh_flags;
72 SHdr->sh_addr = Header.sh_addr;
73 SHdr->sh_offset = Header.sh_offset;
74 SHdr->sh_size = Header.sh_size;
75 SHdr->sh_link = Header.sh_link;
76 SHdr->sh_info = Header.sh_info;
77 SHdr->sh_addralign = Header.sh_addralign;
78 SHdr->sh_entsize = Header.sh_entsize;
Michael J. Spencer84487f12015-07-24 21:03:07 +000079}
80
Michael J. Spencer84487f12015-07-24 21:03:07 +000081// Create output section objects and add them to OutputSections.
82template <class ELFT> void Writer<ELFT>::createSections() {
Rui Ueyamae44524d2015-07-28 00:17:25 +000083 SmallDenseMap<StringRef, OutputSection *> Map;
Rui Ueyama06d7bd22015-07-26 00:50:15 +000084 for (Chunk *C : Symtab->getChunks()) {
85 OutputSection *&Sec = Map[C->getSectionName()];
86 if (!Sec) {
Michael J. Spencer84487f12015-07-24 21:03:07 +000087 Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
88 OutputSections.push_back(Sec);
Michael J. Spencer84487f12015-07-24 21:03:07 +000089 }
Michael J. Spencer44fc1c02015-07-29 22:14:50 +000090 Sec->addChunk<ELFT>(C);
Michael J. Spencer84487f12015-07-24 21:03:07 +000091 }
92}
93
94// Visits all sections to assign incremental, non-overlapping RVAs and
95// file offsets.
96template <class ELFT> void Writer<ELFT>::assignAddresses() {
Michael J. Spencer8039dae22015-07-29 00:30:10 +000097 SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize);
Michael J. Spencer84487f12015-07-24 21:03:07 +000098 uint64_t VA = 0x1000; // The first page is kept unmapped.
99 uint64_t FileOff = SizeOfHeaders;
100 for (OutputSection *Sec : OutputSections) {
101 Sec->setVA(VA);
102 Sec->setFileOffset(FileOff);
103 VA += RoundUpToAlignment(Sec->getSize(), PageSize);
104 FileOff += RoundUpToAlignment(Sec->getSize(), 8);
105 }
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000106 // Add space for section headers.
107 SectionHeaderOff = FileOff;
108 FileOff += (OutputSections.size() + 1) * sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000109 FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8);
110}
111
112template <class ELFT> void Writer<ELFT>::writeHeader() {
113 uint8_t *Buf = Buffer->getBufferStart();
114 auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf);
115 EHdr->e_ident[EI_MAG0] = 0x7F;
116 EHdr->e_ident[EI_MAG1] = 0x45;
117 EHdr->e_ident[EI_MAG2] = 0x4C;
118 EHdr->e_ident[EI_MAG3] = 0x46;
119 EHdr->e_ident[EI_CLASS] = ELFCLASS64;
120 EHdr->e_ident[EI_DATA] = ELFDATA2LSB;
121 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
122 EHdr->e_ident[EI_OSABI] = ELFOSABI_GNU;
123
124 EHdr->e_type = ET_EXEC;
125 EHdr->e_machine = EM_X86_64;
126 EHdr->e_version = EV_CURRENT;
127 EHdr->e_entry = 0x401000;
128 EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000129 EHdr->e_shoff = SectionHeaderOff;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000130 EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>);
131 EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>);
132 EHdr->e_phnum = 1;
133 EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000134 EHdr->e_shnum = OutputSections.size() + 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000135 EHdr->e_shstrndx = 0;
136
137 auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff);
138 PHdrs->p_type = PT_LOAD;
139 PHdrs->p_flags = PF_R | PF_X;
140 PHdrs->p_offset = 0x0000;
141 PHdrs->p_vaddr = 0x400000;
142 PHdrs->p_paddr = PHdrs->p_vaddr;
143 PHdrs->p_filesz = FileSize;
144 PHdrs->p_memsz = FileSize;
145 PHdrs->p_align = 0x4000;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000146
147 auto SHdrs = reinterpret_cast<Elf_Shdr_Impl<ELFT> *>(Buf + EHdr->e_shoff);
148 // First entry is null.
149 ++SHdrs;
150 for (OutputSection *Sec : OutputSections)
151 Sec->writeHeaderTo<ELFT>(SHdrs++);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000152}
153
154template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
155 std::error_code EC = FileOutputBuffer::create(Path, FileSize, Buffer,
156 FileOutputBuffer::F_executable);
157 error(EC, Twine("failed to open ") + Path);
158}
159
160// Write section contents to a mmap'ed file.
161template <class ELFT> void Writer<ELFT>::writeSections() {
162 uint8_t *Buf = Buffer->getBufferStart();
163 for (OutputSection *Sec : OutputSections) {
164 for (Chunk *C : Sec->getChunks())
165 C->writeTo(Buf);
166 }
167}
168
169namespace lld {
170namespace elf2 {
171template class Writer<ELF32LE>;
172template class Writer<ELF32BE>;
173template class Writer<ELF64LE>;
174template class Writer<ELF64BE>;
Michael J. Spencer44fc1c02015-07-29 22:14:50 +0000175
176template void OutputSection::addChunk<ELF32LE>(Chunk *);
177template void OutputSection::addChunk<ELF32BE>(Chunk *);
178template void OutputSection::addChunk<ELF64LE>(Chunk *);
179template void OutputSection::addChunk<ELF64BE>(Chunk *);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000180}
181}