blob: 0f66e403a77dd749259024d3e49e4a8aa78e03a9 [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;
Rafael Espindolab8995142015-08-04 13:39:30 +000084 for (std::unique_ptr<ObjectFile<ELFT>> &File : Symtab->ObjectFiles) {
85 for (Chunk *C : File->getChunks()) {
86 OutputSection *&Sec = Map[C->getSectionName()];
87 if (!Sec) {
88 Sec = new (CAlloc.Allocate()) OutputSection(C->getSectionName());
89 OutputSections.push_back(Sec);
90 }
91 Sec->addChunk<ELFT>(C);
Michael J. Spencer84487f12015-07-24 21:03:07 +000092 }
Michael J. Spencer84487f12015-07-24 21:03:07 +000093 }
94}
95
96// Visits all sections to assign incremental, non-overlapping RVAs and
97// file offsets.
98template <class ELFT> void Writer<ELFT>::assignAddresses() {
Michael J. Spencer8039dae22015-07-29 00:30:10 +000099 SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>), PageSize);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000100 uint64_t VA = 0x1000; // The first page is kept unmapped.
101 uint64_t FileOff = SizeOfHeaders;
102 for (OutputSection *Sec : OutputSections) {
103 Sec->setVA(VA);
104 Sec->setFileOffset(FileOff);
105 VA += RoundUpToAlignment(Sec->getSize(), PageSize);
106 FileOff += RoundUpToAlignment(Sec->getSize(), 8);
107 }
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000108 // Add space for section headers.
109 SectionHeaderOff = FileOff;
110 FileOff += (OutputSections.size() + 1) * sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000111 FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8);
112}
113
114template <class ELFT> void Writer<ELFT>::writeHeader() {
115 uint8_t *Buf = Buffer->getBufferStart();
116 auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf);
117 EHdr->e_ident[EI_MAG0] = 0x7F;
118 EHdr->e_ident[EI_MAG1] = 0x45;
119 EHdr->e_ident[EI_MAG2] = 0x4C;
120 EHdr->e_ident[EI_MAG3] = 0x46;
121 EHdr->e_ident[EI_CLASS] = ELFCLASS64;
122 EHdr->e_ident[EI_DATA] = ELFDATA2LSB;
123 EHdr->e_ident[EI_VERSION] = EV_CURRENT;
124 EHdr->e_ident[EI_OSABI] = ELFOSABI_GNU;
125
126 EHdr->e_type = ET_EXEC;
127 EHdr->e_machine = EM_X86_64;
128 EHdr->e_version = EV_CURRENT;
129 EHdr->e_entry = 0x401000;
130 EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000131 EHdr->e_shoff = SectionHeaderOff;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000132 EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>);
133 EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>);
134 EHdr->e_phnum = 1;
135 EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>);
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000136 EHdr->e_shnum = OutputSections.size() + 1;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000137 EHdr->e_shstrndx = 0;
138
139 auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff);
140 PHdrs->p_type = PT_LOAD;
141 PHdrs->p_flags = PF_R | PF_X;
142 PHdrs->p_offset = 0x0000;
143 PHdrs->p_vaddr = 0x400000;
144 PHdrs->p_paddr = PHdrs->p_vaddr;
145 PHdrs->p_filesz = FileSize;
146 PHdrs->p_memsz = FileSize;
147 PHdrs->p_align = 0x4000;
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000148
149 auto SHdrs = reinterpret_cast<Elf_Shdr_Impl<ELFT> *>(Buf + EHdr->e_shoff);
150 // First entry is null.
151 ++SHdrs;
152 for (OutputSection *Sec : OutputSections)
153 Sec->writeHeaderTo<ELFT>(SHdrs++);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000154}
155
156template <class ELFT> void Writer<ELFT>::openFile(StringRef Path) {
157 std::error_code EC = FileOutputBuffer::create(Path, FileSize, Buffer,
158 FileOutputBuffer::F_executable);
159 error(EC, Twine("failed to open ") + Path);
160}
161
162// Write section contents to a mmap'ed file.
163template <class ELFT> void Writer<ELFT>::writeSections() {
164 uint8_t *Buf = Buffer->getBufferStart();
165 for (OutputSection *Sec : OutputSections) {
166 for (Chunk *C : Sec->getChunks())
167 C->writeTo(Buf);
168 }
169}
170
171namespace lld {
172namespace elf2 {
173template class Writer<ELF32LE>;
174template class Writer<ELF32BE>;
175template class Writer<ELF64LE>;
176template class Writer<ELF64BE>;
Michael J. Spencer44fc1c02015-07-29 22:14:50 +0000177
178template void OutputSection::addChunk<ELF32LE>(Chunk *);
179template void OutputSection::addChunk<ELF32BE>(Chunk *);
180template void OutputSection::addChunk<ELF64LE>(Chunk *);
181template void OutputSection::addChunk<ELF64BE>(Chunk *);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000182}
183}