1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
//===- Writer.cpp ---------------------------------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Config.h"
#include "Writer.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Endian.h"
#include "llvm/Support/FileOutputBuffer.h"
#include "llvm/Support/raw_ostream.h"
#include <algorithm>
#include <cstdio>
#include <functional>
#include <unordered_map>
#include <utility>
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
using namespace llvm::ELF;
using namespace llvm::object;
using namespace llvm::support;
using namespace llvm::support::endian;
static const int PageSize = 4096;
struct SectionTraits {
uint64_t Type;
uint64_t Flags;
StringRef Name;
};
bool operator==(const SectionTraits &A, const SectionTraits &B) {
return A.Type == B.Type && A.Flags == B.Flags && A.Name == B.Name;
}
namespace std {
template <> struct hash<SectionTraits> {
size_t operator()(const SectionTraits &ST) const {
return hash_combine(ST.Type, ST.Flags, ST.Name);
}
};
}
using namespace lld;
using namespace lld::elfv2;
// The main function of the writer.
template <class ELFT>
std::error_code Writer<ELFT>::write(StringRef OutputPath) {
markLive();
createSections();
assignAddresses();
removeEmptySections();
if (auto EC = openFile(OutputPath))
return EC;
writeHeader();
writeSections();
return Buffer->commit();
}
void OutputSection::setVA(uint64_t VA) {
Header.sh_addr = VA;
for (Chunk *C : Chunks)
C->setVA(C->getVA() + VA);
}
void OutputSection::setFileOffset(uint64_t Off) {
if (Header.sh_size == 0)
return;
Header.sh_offset = Off;
for (Chunk *C : Chunks)
C->setFileOff(C->getFileOff() + Off);
}
void OutputSection::addChunk(Chunk *C) {
Chunks.push_back(C);
C->setOutputSection(this);
uint64_t Off = Header.sh_size;
Off = RoundUpToAlignment(Off, C->getAlign());
C->setVA(Off);
C->setFileOff(Off);
Off += C->getSize();
Header.sh_size = Off;
}
void OutputSection::addPermissions(uint32_t C) {
// Header.Characteristics |= C & PermMask;
}
// Write the section header to a given buffer.
void OutputSection::writeHeaderTo(uint8_t *Buf) {}
// Set live bit on for each reachable chunk. Unmarked (unreachable)
// COMDAT chunks will be ignored in the next step, so that they don't
// come to the final output file.
template <class ELFT> void Writer<ELFT>::markLive() {
if (!Config->DoGC)
return;
for (StringRef Name : Config->GCRoots)
cast<Defined>(Symtab->find(Name))->markLive();
for (Chunk *C : Symtab->getChunks())
if (C->isRoot())
C->markLive();
}
static SectionTraits getChunkTraits(Chunk *C) {
return {0, C->getFlags(), C->getSectionName()};
}
// Create output section objects and add them to OutputSections.
template <class ELFT> void Writer<ELFT>::createSections() {
std::unordered_map<SectionTraits, std::vector<Chunk *>> Map;
for (Chunk *C : Symtab->getChunks()) {
if (Config->DoGC && !C->isLive()) {
if (Config->Verbose)
C->printDiscardedMessage();
continue;
}
Map[getChunkTraits(C)].push_back(C);
}
for (auto &P : Map) {
auto Sec = new (CAlloc.Allocate())
OutputSection(P.first.Name, OutputSections.size());
OutputSections.push_back(Sec);
for (Chunk *C : P.second) {
Sec->addChunk(C);
Sec->addPermissions(C->getFlags());
}
}
}
template <class ELFT> void Writer<ELFT>::removeEmptySections() {
auto IsEmpty = [](OutputSection *S) { return S->getSize() == 0; };
OutputSections.erase(
std::remove_if(OutputSections.begin(), OutputSections.end(), IsEmpty),
OutputSections.end());
}
// Visits all sections to assign incremental, non-overlapping RVAs and
// file offsets.
template <class ELFT> void Writer<ELFT>::assignAddresses() {
SizeOfHeaders = RoundUpToAlignment(sizeof(Elf_Ehdr_Impl<ELFT>) +
sizeof(Elf_Shdr_Impl<ELFT>) *
OutputSections.size(),
PageSize);
uint64_t VA = 0x1000; // The first page is kept unmapped.
uint64_t FileOff = SizeOfHeaders;
for (OutputSection *Sec : OutputSections) {
Sec->setVA(VA);
Sec->setFileOffset(FileOff);
VA += RoundUpToAlignment(Sec->getSize(), PageSize);
FileOff += RoundUpToAlignment(Sec->getSize(), 8);
}
SizeOfImage = SizeOfHeaders + RoundUpToAlignment(VA - 0x1000, PageSize);
FileSize = SizeOfHeaders + RoundUpToAlignment(FileOff - SizeOfHeaders, 8);
}
template <class ELFT> void Writer<ELFT>::writeHeader() {
uint8_t *Buf = Buffer->getBufferStart();
auto *EHdr = reinterpret_cast<Elf_Ehdr_Impl<ELFT> *>(Buf);
EHdr->e_ident[EI_MAG0] = 0x7F;
EHdr->e_ident[EI_MAG1] = 0x45;
EHdr->e_ident[EI_MAG2] = 0x4C;
EHdr->e_ident[EI_MAG3] = 0x46;
EHdr->e_ident[EI_CLASS] = ELFCLASS64;
EHdr->e_ident[EI_DATA] = ELFDATA2LSB;
EHdr->e_ident[EI_VERSION] = EV_CURRENT;
EHdr->e_ident[EI_OSABI] = ELFOSABI_GNU;
EHdr->e_type = ET_EXEC;
EHdr->e_machine = EM_X86_64;
EHdr->e_version = EV_CURRENT;
EHdr->e_entry = 0x401000;
EHdr->e_phoff = sizeof(Elf_Ehdr_Impl<ELFT>);
EHdr->e_shoff = 0;
EHdr->e_ehsize = sizeof(Elf_Ehdr_Impl<ELFT>);
EHdr->e_phentsize = sizeof(Elf_Phdr_Impl<ELFT>);
EHdr->e_phnum = 1;
EHdr->e_shentsize = sizeof(Elf_Shdr_Impl<ELFT>);
EHdr->e_shnum = 0;
EHdr->e_shstrndx = 0;
auto PHdrs = reinterpret_cast<Elf_Phdr_Impl<ELFT> *>(Buf + EHdr->e_phoff);
PHdrs->p_type = PT_LOAD;
PHdrs->p_flags = PF_R | PF_X;
PHdrs->p_offset = 0x0000;
PHdrs->p_vaddr = 0x400000;
PHdrs->p_paddr = PHdrs->p_vaddr;
PHdrs->p_filesz = FileSize;
PHdrs->p_memsz = FileSize;
PHdrs->p_align = 0x4000;
}
template <class ELFT> std::error_code Writer<ELFT>::openFile(StringRef Path) {
if (auto EC = FileOutputBuffer::create(Path, FileSize, Buffer,
FileOutputBuffer::F_executable)) {
llvm::errs() << "failed to open " << Path << ": " << EC.message() << "\n";
return EC;
}
return std::error_code();
}
// Write section contents to a mmap'ed file.
template <class ELFT> void Writer<ELFT>::writeSections() {
uint8_t *Buf = Buffer->getBufferStart();
for (OutputSection *Sec : OutputSections) {
// Fill gaps between functions in .text with nop instructions instead of
// leaving as null bytes (which can be interpreted as ADD instructions).
if (Sec->getPermissions() & PF_X)
memset(Buf + Sec->getFileOff(), 0x90, Sec->getSize());
for (Chunk *C : Sec->getChunks())
C->writeTo(Buf);
}
}
template <class ELFT> OutputSection *Writer<ELFT>::findSection(StringRef Name) {
for (OutputSection *Sec : OutputSections)
if (Sec->getName() == Name)
return Sec;
return nullptr;
}
namespace lld {
namespace elfv2 {
template class Writer<ELF32LE>;
template class Writer<ELF32BE>;
template class Writer<ELF64LE>;
template class Writer<ELF64BE>;
}
}
|