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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
//===- lib/ReaderWriter/ELF/WriterELF.cpp ---------------------------------===//
//
// The LLVM Linker
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lld/ReaderWriter/Writer.h"
#include "DefaultLayout.h"
#include "TargetLayout.h"
#include "ExecutableAtoms.h"
#include "lld/ReaderWriter/ELFTargetInfo.h"
using namespace llvm;
using namespace llvm::object;
namespace lld {
namespace elf {
template<class ELFT>
class ExecutableWriter;
//===----------------------------------------------------------------------===//
// ExecutableWriter Class
//===----------------------------------------------------------------------===//
template<class ELFT>
class ExecutableWriter : public ELFWriter {
public:
typedef Elf_Shdr_Impl<ELFT> Elf_Shdr;
typedef Elf_Sym_Impl<ELFT> Elf_Sym;
ExecutableWriter(const ELFTargetInfo &ti);
private:
// build the sections that need to be created
void buildChunks(const File &file);
virtual error_code writeFile(const File &File, StringRef path);
void buildAtomToAddressMap();
void buildSymbolTable ();
void buildSectionHeaderTable();
void assignSectionsWithNoSegments();
void addAbsoluteUndefinedSymbols(const File &File);
void addDefaultAtoms();
void addFiles(InputFiles&);
void finalizeDefaultAtomValues();
uint64_t addressOfAtom(const Atom *atom) {
return _atomToAddressMap[atom];
}
KindHandler *kindHandler() { return _referenceKindHandler.get(); }
void createDefaultSections();
const ELFTargetInfo &_targetInfo;
TargetHandler<ELFT> &_targetHandler;
typedef llvm::DenseMap<const Atom *, uint64_t> AtomToAddress;
std::unique_ptr<KindHandler> _referenceKindHandler;
AtomToAddress _atomToAddressMap;
llvm::BumpPtrAllocator _chunkAllocate;
DefaultLayout<ELFT> *_layout;
Header<ELFT> *_Header;
ProgramHeader<ELFT> *_programHeader;
SymbolTable<ELFT> * _symtab;
StringTable<ELFT> *_strtab;
StringTable<ELFT> *_shstrtab;
SectionHeader<ELFT> *_shdrtab;
CRuntimeFile<ELFT> _runtimeFile;
};
//===----------------------------------------------------------------------===//
// ExecutableWriter
//===----------------------------------------------------------------------===//
template <class ELFT>
ExecutableWriter<ELFT>::ExecutableWriter(const ELFTargetInfo &ti)
: _targetInfo(ti), _targetHandler(ti.getTargetHandler<ELFT>()),
_referenceKindHandler(KindHandler::makeHandler(ti.getTriple().getArch(),
ti.isLittleEndian())),
_runtimeFile(ti) {
_layout = new TargetLayout<ELFT>(_targetInfo);
}
template <class ELFT>
void ExecutableWriter<ELFT>::buildChunks(const File &file) {
for (const DefinedAtom *definedAtom : file.defined() ) {
_layout->addAtom(definedAtom);
}
/// Add all the absolute atoms to the layout
for (const AbsoluteAtom *absoluteAtom : file.absolute()) {
_layout->addAtom(absoluteAtom);
}
}
template<class ELFT>
void ExecutableWriter<ELFT>::buildSymbolTable () {
for (auto sec : _layout->sections())
if (auto section = dyn_cast<Section<ELFT>>(sec))
for (const auto &atom : section->atoms())
_symtab->addSymbol(atom->_atom, section->ordinal(), atom->_virtualAddr);
}
template<class ELFT>
void
ExecutableWriter<ELFT>::addAbsoluteUndefinedSymbols(const File &file) {
// add all the absolute symbols that the layout contains to the output symbol
// table
for (auto &atom : _layout->absoluteAtoms())
_symtab->addSymbol(atom->_atom, ELF::SHN_ABS, atom->_virtualAddr);
for (const UndefinedAtom *a : file.undefined())
_symtab->addSymbol(a, ELF::SHN_UNDEF);
}
template<class ELFT>
void ExecutableWriter<ELFT>::buildAtomToAddressMap () {
for (auto sec : _layout->sections())
if (auto section = dyn_cast<Section<ELFT>>(sec))
for (const auto &atom : section->atoms())
_atomToAddressMap[atom->_atom] = atom->_virtualAddr;
// build the atomToAddressMap that contains absolute symbols too
for (auto &atom : _layout->absoluteAtoms())
_atomToAddressMap[atom->_atom] = atom->_virtualAddr;
}
template<class ELFT>
void ExecutableWriter<ELFT>::buildSectionHeaderTable() {
for (auto mergedSec : _layout->mergedSections()) {
if (mergedSec->kind() != Chunk<ELFT>::K_ELFSection)
continue;
if (mergedSec->hasSegment())
_shdrtab->appendSection(mergedSec);
}
}
template<class ELFT>
void ExecutableWriter<ELFT>::assignSectionsWithNoSegments() {
for (auto mergedSec : _layout->mergedSections()) {
if (mergedSec->kind() != Chunk<ELFT>::K_ELFSection)
continue;
if (!mergedSec->hasSegment())
_shdrtab->appendSection(mergedSec);
}
_layout->assignOffsetsForMiscSections();
for (auto sec : _layout->sections())
if (auto section = dyn_cast<Section<ELFT>>(sec))
if (!DefaultLayout<ELFT>::hasOutputSegment(section))
_shdrtab->updateSection(section);
}
/// \brief Add absolute symbols by default. These are linker added
/// absolute symbols
template<class ELFT>
void ExecutableWriter<ELFT>::addDefaultAtoms() {
_runtimeFile.addUndefinedAtom(_targetInfo.getEntry());
_runtimeFile.addAbsoluteAtom("__bss_start");
_runtimeFile.addAbsoluteAtom("__bss_end");
_runtimeFile.addAbsoluteAtom("_end");
_runtimeFile.addAbsoluteAtom("end");
_runtimeFile.addAbsoluteAtom("__init_array_start");
_runtimeFile.addAbsoluteAtom("__init_array_end");
_runtimeFile.addAbsoluteAtom("__rela_iplt_start");
_runtimeFile.addAbsoluteAtom("__rela_iplt_end");
}
/// \brief Hook in lld to add CRuntime file
template <class ELFT>
void ExecutableWriter<ELFT>::addFiles(InputFiles &inputFiles) {
addDefaultAtoms();
inputFiles.prependFile(_runtimeFile);
// Give a chance for the target to add atoms
_targetHandler.addFiles(inputFiles);
}
/// Finalize the value of all the absolute symbols that we
/// created
template<class ELFT>
void ExecutableWriter<ELFT>::finalizeDefaultAtomValues() {
auto bssStartAtomIter = _layout->findAbsoluteAtom("__bss_start");
auto bssEndAtomIter = _layout->findAbsoluteAtom("__bss_end");
auto underScoreEndAtomIter = _layout->findAbsoluteAtom("_end");
auto endAtomIter = _layout->findAbsoluteAtom("end");
auto initArrayStartIter = _layout->findAbsoluteAtom("__init_array_start");
auto initArrayEndIter = _layout->findAbsoluteAtom("__init_array_end");
auto realIpltStartIter = _layout->findAbsoluteAtom("__rela_iplt_start");
auto realIpltEndIter = _layout->findAbsoluteAtom("__rela_iplt_end");
auto startEnd = [&](typename DefaultLayout<ELFT>::AbsoluteAtomIterT start,
typename DefaultLayout<ELFT>::AbsoluteAtomIterT end,
StringRef sec) -> void {
auto section = _layout->findOutputSection(sec);
if (section) {
(*start)->_virtualAddr = section->virtualAddr();
(*end)->_virtualAddr = section->virtualAddr() + section->memSize();
} else {
(*start)->_virtualAddr = 0;
(*end)->_virtualAddr = 0;
}
};
startEnd(initArrayStartIter, initArrayEndIter, ".init_array");
startEnd(realIpltStartIter, realIpltEndIter, ".rela.plt");
assert(!(bssStartAtomIter == _layout->absoluteAtoms().end() ||
bssEndAtomIter == _layout->absoluteAtoms().end() ||
underScoreEndAtomIter == _layout->absoluteAtoms().end() ||
endAtomIter == _layout->absoluteAtoms().end()) &&
"Unable to find the absolute atoms that have been added by lld");
auto phe = _programHeader->findProgramHeader(
llvm::ELF::PT_LOAD, llvm::ELF::PF_W, llvm::ELF::PF_X);
assert(!(phe == _programHeader->end()) &&
"Can't find a data segment in the program header!");
(*bssStartAtomIter)->_virtualAddr = (*phe)->p_vaddr + (*phe)->p_filesz;
(*bssEndAtomIter)->_virtualAddr = (*phe)->p_vaddr + (*phe)->p_memsz;
(*underScoreEndAtomIter)->_virtualAddr = (*phe)->p_vaddr + (*phe)->p_memsz;
(*endAtomIter)->_virtualAddr = (*phe)->p_vaddr + (*phe)->p_memsz;
}
template<class ELFT>
error_code
ExecutableWriter<ELFT>::writeFile(const File &file, StringRef path) {
buildChunks(file);
// Create the default sections like the symbol table, string table, and the
// section string table
createDefaultSections();
// Set the Layout
_layout->assignSectionsToSegments();
_layout->assignFileOffsets();
_layout->assignVirtualAddress();
// Finalize the default value of symbols that the linker adds
finalizeDefaultAtomValues();
// Build the Atom To Address map for applying relocations
buildAtomToAddressMap();
// Create symbol table and section string table
buildSymbolTable();
// add other symbols
addAbsoluteUndefinedSymbols(file);
// Finalize the layout by calling the finalize() functions
_layout->finalize();
// build Section Header table
buildSectionHeaderTable();
// assign Offsets and virtual addresses
// for sections with no segments
assignSectionsWithNoSegments();
uint64_t totalSize = _shdrtab->fileOffset() + _shdrtab->fileSize();
OwningPtr<FileOutputBuffer> buffer;
error_code ec = FileOutputBuffer::create(path,
totalSize, buffer,
FileOutputBuffer::F_executable);
if (ec)
return ec;
_Header->e_ident(ELF::EI_CLASS, _targetInfo.is64Bits() ? ELF::ELFCLASS64 :
ELF::ELFCLASS32);
_Header->e_ident(ELF::EI_DATA, _targetInfo.isLittleEndian() ?
ELF::ELFDATA2LSB : ELF::ELFDATA2MSB);
_Header->e_type(_targetInfo.getOutputType());
_Header->e_machine(_targetInfo.getOutputMachine());
if (!_targetHandler.doesOverrideHeader()) {
_Header->e_ident(ELF::EI_VERSION, 1);
_Header->e_ident(ELF::EI_OSABI, 0);
_Header->e_version(1);
} else {
// override the contents of the ELF Header
_targetHandler.setHeaderInfo(_Header);
}
_Header->e_phoff(_programHeader->fileOffset());
_Header->e_shoff(_shdrtab->fileOffset());
_Header->e_phentsize(_programHeader->entsize());
_Header->e_phnum(_programHeader->numHeaders());
_Header->e_shentsize(_shdrtab->entsize());
_Header->e_shnum(_shdrtab->numHeaders());
_Header->e_shstrndx(_shstrtab->ordinal());
uint64_t virtualAddr = 0;
_layout->findAtomAddrByName(_targetInfo.getEntry(), virtualAddr);
_Header->e_entry(virtualAddr);
// HACK: We have to write out the header and program header here even though
// they are a member of a segment because only sections are written in the
// following loop.
_Header->write(this, *buffer);
_programHeader->write(this, *buffer);
for (auto section : _layout->sections())
section->write(this, *buffer);
return buffer->commit();
}
template<class ELFT>
void ExecutableWriter<ELFT>::createDefaultSections() {
_Header = new Header<ELFT>(_targetInfo);
_programHeader = new ProgramHeader<ELFT>(_targetInfo);
_layout->setHeader(_Header);
_layout->setProgramHeader(_programHeader);
_symtab = new SymbolTable<
ELFT>(_targetInfo, ".symtab", DefaultLayout<ELFT>::ORDER_SYMBOL_TABLE);
_strtab = new StringTable<
ELFT>(_targetInfo, ".strtab", DefaultLayout<ELFT>::ORDER_STRING_TABLE);
_shstrtab = new StringTable<ELFT>(
_targetInfo, ".shstrtab", DefaultLayout<ELFT>::ORDER_SECTION_STRINGS);
_shdrtab = new SectionHeader<
ELFT>(_targetInfo, DefaultLayout<ELFT>::ORDER_SECTION_HEADERS);
_layout->addSection(_symtab);
_layout->addSection(_strtab);
_layout->addSection(_shstrtab);
_shdrtab->setStringSection(_shstrtab);
_symtab->setStringSection(_strtab);
_layout->addSection(_shdrtab);
// give a chance for the target to add sections
_targetHandler.createDefaultSections();
}
} // namespace elf
std::unique_ptr<Writer> createWriterELF(const ELFTargetInfo &TI) {
using llvm::object::ELFType;
// Set the default layout to be the static executable layout
// We would set the layout to a dynamic executable layout
// if we came across any shared libraries in the process
if (!TI.is64Bits() && TI.isLittleEndian())
return std::unique_ptr<Writer>(new
elf::ExecutableWriter<ELFType<support::little, 4, false>>(TI));
else if (TI.is64Bits() && TI.isLittleEndian())
return std::unique_ptr<Writer>(new
elf::ExecutableWriter<ELFType<support::little, 8, true>>(TI));
else if (!TI.is64Bits() && !TI.isLittleEndian())
return std::unique_ptr<Writer>(new
elf::ExecutableWriter<ELFType<support::big, 4, false>>(TI));
else if (TI.is64Bits() && !TI.isLittleEndian())
return std::unique_ptr<Writer>(new
elf::ExecutableWriter<ELFType<support::big, 8, true>>(TI));
llvm_unreachable("Invalid Options!");
}
} // namespace lld
|