blob: 2ff24ce9a25ba5fdbb2efce54e2e1d2edbec8ebc [file] [log] [blame]
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001//===- Object.cpp ---------------------------------------------------------===//
Petr Hosek05a04cb2017-08-01 00:33:58 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00009
Petr Hosek05a04cb2017-08-01 00:33:58 +000010#include "Object.h"
11#include "llvm-objcopy.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000012#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/STLExtras.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16#include "llvm/ADT/iterator_range.h"
17#include "llvm/BinaryFormat/ELF.h"
18#include "llvm/Object/ELFObjectFile.h"
19#include "llvm/Support/ErrorHandling.h"
20#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000021#include "llvm/Support/Path.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000022#include <algorithm>
23#include <cstddef>
24#include <cstdint>
25#include <iterator>
26#include <utility>
27#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000028
29using namespace llvm;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000030using namespace llvm::objcopy;
Petr Hosek05a04cb2017-08-01 00:33:58 +000031using namespace object;
32using namespace ELF;
33
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000034Buffer::~Buffer() {}
35
36void FileBuffer::allocate(size_t Size) {
37 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
38 FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
39 handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) {
40 error("failed to open " + getName() + ": " + E.message());
41 });
42 Buf = std::move(*BufferOrErr);
43}
44
45Error FileBuffer::commit() { return Buf->commit(); }
46
47uint8_t *FileBuffer::getBufferStart() {
48 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
49}
50
51void MemBuffer::allocate(size_t Size) {
52 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
53}
54
55Error MemBuffer::commit() { return Error::success(); }
56
57uint8_t *MemBuffer::getBufferStart() {
58 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
59}
60
61std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
62 return std::move(Buf);
63}
64
Jake Ehrlich76e91102018-01-25 22:46:17 +000065template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000066 uint8_t *B = Buf.getBufferStart();
67 B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
68 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000069 Phdr.p_type = Seg.Type;
70 Phdr.p_flags = Seg.Flags;
71 Phdr.p_offset = Seg.Offset;
72 Phdr.p_vaddr = Seg.VAddr;
73 Phdr.p_paddr = Seg.PAddr;
74 Phdr.p_filesz = Seg.FileSize;
75 Phdr.p_memsz = Seg.MemSize;
76 Phdr.p_align = Seg.Align;
Petr Hosekc4df10e2017-08-04 21:09:26 +000077}
78
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000079void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
Paul Semel4246a462018-05-09 21:36:54 +000080void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
Jake Ehrlichf5a43772017-09-25 20:37:28 +000081void SectionBase::initialize(SectionTableRef SecTable) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000082void SectionBase::finalize() {}
Paul Semel99dda0b2018-05-25 11:01:25 +000083void SectionBase::markSymbols() {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000084
Jake Ehrlich76e91102018-01-25 22:46:17 +000085template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000086 uint8_t *B = Buf.getBufferStart();
87 B += Sec.HeaderOffset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +000088 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000089 Shdr.sh_name = Sec.NameIndex;
90 Shdr.sh_type = Sec.Type;
91 Shdr.sh_flags = Sec.Flags;
92 Shdr.sh_addr = Sec.Addr;
93 Shdr.sh_offset = Sec.Offset;
94 Shdr.sh_size = Sec.Size;
95 Shdr.sh_link = Sec.Link;
96 Shdr.sh_info = Sec.Info;
97 Shdr.sh_addralign = Sec.Align;
98 Shdr.sh_entsize = Sec.EntrySize;
Petr Hosek05a04cb2017-08-01 00:33:58 +000099}
100
Jake Ehrlich76e91102018-01-25 22:46:17 +0000101SectionVisitor::~SectionVisitor() {}
102
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000103void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
104 error("Cannot write symbol section index table '" + Sec.Name + "' ");
105}
106
Jake Ehrlich76e91102018-01-25 22:46:17 +0000107void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
108 error("Cannot write symbol table '" + Sec.Name + "' out to binary");
109}
110
111void BinarySectionWriter::visit(const RelocationSection &Sec) {
112 error("Cannot write relocation section '" + Sec.Name + "' out to binary");
113}
114
115void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000116 error("Cannot write '" + Sec.Name + "' out to binary");
117}
118
119void BinarySectionWriter::visit(const GroupSection &Sec) {
120 error("Cannot write '" + Sec.Name + "' out to binary");
Jake Ehrlich76e91102018-01-25 22:46:17 +0000121}
122
123void SectionWriter::visit(const Section &Sec) {
124 if (Sec.Type == SHT_NOBITS)
Petr Hosek05a04cb2017-08-01 00:33:58 +0000125 return;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000126 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
127 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000128}
129
Jake Ehrlich76e91102018-01-25 22:46:17 +0000130void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
131
132void SectionWriter::visit(const OwnedDataSection &Sec) {
133 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
134 std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
135}
136
137void OwnedDataSection::accept(SectionVisitor &Visitor) const {
138 Visitor.visit(*this);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000139}
140
Petr Hosek05a04cb2017-08-01 00:33:58 +0000141void StringTableSection::addString(StringRef Name) {
142 StrTabBuilder.add(Name);
143 Size = StrTabBuilder.getSize();
144}
145
146uint32_t StringTableSection::findIndex(StringRef Name) const {
147 return StrTabBuilder.getOffset(Name);
148}
149
150void StringTableSection::finalize() { StrTabBuilder.finalize(); }
151
Jake Ehrlich76e91102018-01-25 22:46:17 +0000152void SectionWriter::visit(const StringTableSection &Sec) {
153 Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
154}
155
156void StringTableSection::accept(SectionVisitor &Visitor) const {
157 Visitor.visit(*this);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000158}
159
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000160template <class ELFT>
161void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
162 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000163 auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000164 std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer);
165}
166
167void SectionIndexSection::initialize(SectionTableRef SecTable) {
168 Size = 0;
169 setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
170 Link,
171 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
172 "Link field value " + Twine(Link) + " in section " + Name +
173 " is not a symbol table"));
174 Symbols->setShndxTable(this);
175}
176
177void SectionIndexSection::finalize() { Link = Symbols->Index; }
178
179void SectionIndexSection::accept(SectionVisitor &Visitor) const {
180 Visitor.visit(*this);
181}
182
Petr Hosekc1135772017-09-13 03:04:50 +0000183static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000184 switch (Index) {
185 case SHN_ABS:
186 case SHN_COMMON:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000187 return true;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000188 }
Petr Hosekc1135772017-09-13 03:04:50 +0000189 if (Machine == EM_HEXAGON) {
190 switch (Index) {
191 case SHN_HEXAGON_SCOMMON:
192 case SHN_HEXAGON_SCOMMON_2:
193 case SHN_HEXAGON_SCOMMON_4:
194 case SHN_HEXAGON_SCOMMON_8:
195 return true;
196 }
197 }
198 return false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000199}
200
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000201// Large indexes force us to clarify exactly what this function should do. This
202// function should return the value that will appear in st_shndx when written
203// out.
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000204uint16_t Symbol::getShndx() const {
205 if (DefinedIn != nullptr) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000206 if (DefinedIn->Index >= SHN_LORESERVE)
207 return SHN_XINDEX;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000208 return DefinedIn->Index;
209 }
210 switch (ShndxType) {
211 // This means that we don't have a defined section but we do need to
212 // output a legitimate section index.
213 case SYMBOL_SIMPLE_INDEX:
214 return SHN_UNDEF;
215 case SYMBOL_ABS:
216 case SYMBOL_COMMON:
217 case SYMBOL_HEXAGON_SCOMMON:
218 case SYMBOL_HEXAGON_SCOMMON_2:
219 case SYMBOL_HEXAGON_SCOMMON_4:
220 case SYMBOL_HEXAGON_SCOMMON_8:
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000221 case SYMBOL_XINDEX:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000222 return static_cast<uint16_t>(ShndxType);
223 }
224 llvm_unreachable("Symbol with invalid ShndxType encountered");
225}
226
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000227void SymbolTableSection::assignIndices() {
228 uint32_t Index = 0;
229 for (auto &Sym : Symbols)
230 Sym->Index = Index++;
231}
232
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000233void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
Petr Hosek79cee9e2017-08-29 02:12:03 +0000234 SectionBase *DefinedIn, uint64_t Value,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000235 uint8_t Visibility, uint16_t Shndx,
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000236 uint64_t Size) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000237 Symbol Sym;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000238 Sym.Name = Name.str();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000239 Sym.Binding = Bind;
240 Sym.Type = Type;
241 Sym.DefinedIn = DefinedIn;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000242 if (DefinedIn != nullptr)
243 DefinedIn->HasSymbol = true;
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000244 if (DefinedIn == nullptr) {
245 if (Shndx >= SHN_LORESERVE)
246 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
247 else
248 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
249 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000250 Sym.Value = Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000251 Sym.Visibility = Visibility;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000252 Sym.Size = Size;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000253 Sym.Index = Symbols.size();
254 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
255 Size += this->EntrySize;
256}
257
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000258void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000259 if (SectionIndexTable == Sec)
260 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000261 if (SymbolNames == Sec) {
262 error("String table " + SymbolNames->Name +
263 " cannot be removed because it is referenced by the symbol table " +
264 this->Name);
265 }
Paul Semel41695f82018-05-02 20:19:22 +0000266 removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000267}
268
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000269void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
Paul Semel46201fb2018-06-01 16:19:46 +0000270 std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
271 [Callable](SymPtr &Sym) { Callable(*Sym); });
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000272 std::stable_partition(
273 std::begin(Symbols), std::end(Symbols),
274 [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000275 assignIndices();
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000276}
277
Paul Semel4246a462018-05-09 21:36:54 +0000278void SymbolTableSection::removeSymbols(
279 function_ref<bool(const Symbol &)> ToRemove) {
Paul Semel41695f82018-05-02 20:19:22 +0000280 Symbols.erase(
Paul Semel46201fb2018-06-01 16:19:46 +0000281 std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
Paul Semel41695f82018-05-02 20:19:22 +0000282 [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
283 std::end(Symbols));
284 Size = Symbols.size() * EntrySize;
285 assignIndices();
286}
287
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000288void SymbolTableSection::initialize(SectionTableRef SecTable) {
289 Size = 0;
290 setStrTab(SecTable.getSectionOfType<StringTableSection>(
291 Link,
292 "Symbol table has link index of " + Twine(Link) +
293 " which is not a valid index",
294 "Symbol table has link index of " + Twine(Link) +
295 " which is not a string table"));
296}
297
Petr Hosek79cee9e2017-08-29 02:12:03 +0000298void SymbolTableSection::finalize() {
299 // Make sure SymbolNames is finalized before getting name indexes.
300 SymbolNames->finalize();
301
302 uint32_t MaxLocalIndex = 0;
303 for (auto &Sym : Symbols) {
304 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
305 if (Sym->Binding == STB_LOCAL)
306 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
307 }
308 // Now we need to set the Link and Info fields.
309 Link = SymbolNames->Index;
310 Info = MaxLocalIndex + 1;
311}
312
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000313void SymbolTableSection::prepareForLayout() {
314 // Add all potential section indexes before file layout so that the section
315 // index section has the approprite size.
316 if (SectionIndexTable != nullptr) {
317 for (const auto &Sym : Symbols) {
318 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
319 SectionIndexTable->addIndex(Sym->DefinedIn->Index);
320 else
321 SectionIndexTable->addIndex(SHN_UNDEF);
322 }
323 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000324 // Add all of our strings to SymbolNames so that SymbolNames has the right
325 // size before layout is decided.
326 for (auto &Sym : Symbols)
327 SymbolNames->addString(Sym->Name);
328}
329
330const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
331 if (Symbols.size() <= Index)
332 error("Invalid symbol index: " + Twine(Index));
333 return Symbols[Index].get();
334}
335
Paul Semel99dda0b2018-05-25 11:01:25 +0000336Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
337 return const_cast<Symbol *>(
338 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
339}
340
Petr Hosek79cee9e2017-08-29 02:12:03 +0000341template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000342void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000343 uint8_t *Buf = Out.getBufferStart();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000344 Buf += Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000345 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000346 // Loop though symbols setting each entry of the symbol table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000347 for (auto &Symbol : Sec.Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000348 Sym->st_name = Symbol->NameIndex;
349 Sym->st_value = Symbol->Value;
350 Sym->st_size = Symbol->Size;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000351 Sym->st_other = Symbol->Visibility;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000352 Sym->setBinding(Symbol->Binding);
353 Sym->setType(Symbol->Type);
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000354 Sym->st_shndx = Symbol->getShndx();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000355 ++Sym;
356 }
357}
358
Jake Ehrlich76e91102018-01-25 22:46:17 +0000359void SymbolTableSection::accept(SectionVisitor &Visitor) const {
360 Visitor.visit(*this);
361}
362
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000363template <class SymTabType>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000364void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
365 const SectionBase *Sec) {
366 if (Symbols == Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000367 error("Symbol table " + Symbols->Name +
368 " cannot be removed because it is "
369 "referenced by the relocation "
370 "section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000371 this->Name);
372 }
373}
374
375template <class SymTabType>
376void RelocSectionWithSymtabBase<SymTabType>::initialize(
377 SectionTableRef SecTable) {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000378 if (Link != SHN_UNDEF)
379 setSymTab(SecTable.getSectionOfType<SymTabType>(
380 Link,
381 "Link field value " + Twine(Link) + " in section " + Name +
382 " is invalid",
383 "Link field value " + Twine(Link) + " in section " + Name +
384 " is not a symbol table"));
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000385
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000386 if (Info != SHN_UNDEF)
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000387 setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
388 " in section " + Name +
389 " is invalid"));
James Y Knight2ea995a2017-09-26 22:44:01 +0000390 else
391 setSection(nullptr);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000392}
393
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000394template <class SymTabType>
395void RelocSectionWithSymtabBase<SymTabType>::finalize() {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000396 this->Link = Symbols ? Symbols->Index : 0;
397
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000398 if (SecToApplyRel != nullptr)
399 this->Info = SecToApplyRel->Index;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000400}
401
402template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000403static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
Petr Hosekd7df9b22017-09-06 23:41:02 +0000404
405template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000406static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000407 Rela.r_addend = Addend;
408}
409
Jake Ehrlich76e91102018-01-25 22:46:17 +0000410template <class RelRange, class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000411static void writeRel(const RelRange &Relocations, T *Buf) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000412 for (const auto &Reloc : Relocations) {
413 Buf->r_offset = Reloc.Offset;
414 setAddend(*Buf, Reloc.Addend);
415 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
416 ++Buf;
417 }
418}
419
420template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000421void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
422 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
423 if (Sec.Type == SHT_REL)
424 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000425 else
Jake Ehrlich76e91102018-01-25 22:46:17 +0000426 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000427}
428
Jake Ehrlich76e91102018-01-25 22:46:17 +0000429void RelocationSection::accept(SectionVisitor &Visitor) const {
430 Visitor.visit(*this);
431}
432
Paul Semel4246a462018-05-09 21:36:54 +0000433void RelocationSection::removeSymbols(
434 function_ref<bool(const Symbol &)> ToRemove) {
435 for (const Relocation &Reloc : Relocations)
436 if (ToRemove(*Reloc.RelocSymbol))
Jordan Rupprecht88ed5e52018-08-09 22:52:03 +0000437 error("not stripping symbol '" + Reloc.RelocSymbol->Name +
Paul Semel4246a462018-05-09 21:36:54 +0000438 "' because it is named in a relocation");
439}
440
Paul Semel99dda0b2018-05-25 11:01:25 +0000441void RelocationSection::markSymbols() {
442 for (const Relocation &Reloc : Relocations)
443 Reloc.RelocSymbol->Referenced = true;
444}
445
Jake Ehrlich76e91102018-01-25 22:46:17 +0000446void SectionWriter::visit(const DynamicRelocationSection &Sec) {
447 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
448 Out.getBufferStart() + Sec.Offset);
449}
450
451void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
452 Visitor.visit(*this);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000453}
454
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000455void Section::removeSectionReferences(const SectionBase *Sec) {
456 if (LinkSection == Sec) {
457 error("Section " + LinkSection->Name +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000458 " cannot be removed because it is "
459 "referenced by the section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000460 this->Name);
461 }
462}
463
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000464void GroupSection::finalize() {
465 this->Info = Sym->Index;
466 this->Link = SymTab->Index;
467}
468
Paul Semel4246a462018-05-09 21:36:54 +0000469void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
470 if (ToRemove(*Sym)) {
471 error("Symbol " + Sym->Name +
472 " cannot be removed because it is "
473 "referenced by the section " +
474 this->Name + "[" + Twine(this->Index) + "]");
475 }
476}
477
Paul Semel99dda0b2018-05-25 11:01:25 +0000478void GroupSection::markSymbols() {
479 if (Sym)
480 Sym->Referenced = true;
481}
482
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000483void Section::initialize(SectionTableRef SecTable) {
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000484 if (Link != ELF::SHN_UNDEF) {
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000485 LinkSection =
486 SecTable.getSection(Link, "Link field value " + Twine(Link) +
487 " in section " + Name + " is invalid");
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000488 if (LinkSection->Type == ELF::SHT_SYMTAB)
489 LinkSection = nullptr;
490 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000491}
492
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000493void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000494
Jake Ehrlich76e91102018-01-25 22:46:17 +0000495void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
Alexander Richardson6c859922018-02-19 19:53:44 +0000496 FileName = sys::path::filename(File);
497 // The format for the .gnu_debuglink starts with the file name and is
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000498 // followed by a null terminator and then the CRC32 of the file. The CRC32
499 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
500 // byte, and then finally push the size to alignment and add 4.
501 Size = alignTo(FileName.size() + 1, 4) + 4;
502 // The CRC32 will only be aligned if we align the whole section.
503 Align = 4;
504 Type = ELF::SHT_PROGBITS;
505 Name = ".gnu_debuglink";
506 // For sections not found in segments, OriginalOffset is only used to
507 // establish the order that sections should go in. By using the maximum
508 // possible offset we cause this section to wind up at the end.
509 OriginalOffset = std::numeric_limits<uint64_t>::max();
510 JamCRC crc;
511 crc.update(ArrayRef<char>(Data.data(), Data.size()));
512 // The CRC32 value needs to be complemented because the JamCRC dosn't
513 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
514 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
515 CRC32 = ~crc.getCRC();
516}
517
Jake Ehrlich76e91102018-01-25 22:46:17 +0000518GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000519 // Read in the file to compute the CRC of it.
520 auto DebugOrErr = MemoryBuffer::getFile(File);
521 if (!DebugOrErr)
522 error("'" + File + "': " + DebugOrErr.getError().message());
523 auto Debug = std::move(*DebugOrErr);
524 init(File, Debug->getBuffer());
525}
526
527template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000528void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
529 auto Buf = Out.getBufferStart() + Sec.Offset;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000530 char *File = reinterpret_cast<char *>(Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000531 Elf_Word *CRC =
532 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
533 *CRC = Sec.CRC32;
534 std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
535}
536
537void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
538 Visitor.visit(*this);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000539}
540
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000541template <class ELFT>
542void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
543 ELF::Elf32_Word *Buf =
544 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
545 *Buf++ = Sec.FlagWord;
546 for (const auto *S : Sec.GroupMembers)
547 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
548}
549
550void GroupSection::accept(SectionVisitor &Visitor) const {
551 Visitor.visit(*this);
552}
553
Petr Hosek05a04cb2017-08-01 00:33:58 +0000554// Returns true IFF a section is wholly inside the range of a segment
555static bool sectionWithinSegment(const SectionBase &Section,
556 const Segment &Segment) {
557 // If a section is empty it should be treated like it has a size of 1. This is
558 // to clarify the case when an empty section lies on a boundary between two
559 // segments and ensures that the section "belongs" to the second segment and
560 // not the first.
561 uint64_t SecSize = Section.Size ? Section.Size : 1;
562 return Segment.Offset <= Section.OriginalOffset &&
563 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
564}
565
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000566// Returns true IFF a segment's original offset is inside of another segment's
567// range.
568static bool segmentOverlapsSegment(const Segment &Child,
569 const Segment &Parent) {
570
571 return Parent.OriginalOffset <= Child.OriginalOffset &&
572 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
573}
574
Jake Ehrlich46814be2018-01-22 19:27:30 +0000575static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +0000576 // Any segment without a parent segment should come before a segment
577 // that has a parent segment.
578 if (A->OriginalOffset < B->OriginalOffset)
579 return true;
580 if (A->OriginalOffset > B->OriginalOffset)
581 return false;
582 return A->Index < B->Index;
583}
584
Jake Ehrlich46814be2018-01-22 19:27:30 +0000585static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
586 if (A->PAddr < B->PAddr)
587 return true;
588 if (A->PAddr > B->PAddr)
589 return false;
590 return A->Index < B->Index;
591}
592
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000593template <class ELFT> void BinaryELFBuilder<ELFT>::initFileHeader() {
594 Obj->Flags = 0x0;
595 Obj->Type = ET_REL;
596 Obj->Entry = 0x0;
597 Obj->Machine = EMachine;
598 Obj->Version = 1;
599}
600
601template <class ELFT> void BinaryELFBuilder<ELFT>::initHeaderSegment() {
602 Obj->ElfHdrSegment.Index = 0;
603}
604
605template <class ELFT> StringTableSection *BinaryELFBuilder<ELFT>::addStrTab() {
606 auto &StrTab = Obj->addSection<StringTableSection>();
607 StrTab.Name = ".strtab";
608
609 Obj->SectionNames = &StrTab;
610 return &StrTab;
611}
612
613template <class ELFT>
614SymbolTableSection *
615BinaryELFBuilder<ELFT>::addSymTab(StringTableSection *StrTab) {
616 auto &SymTab = Obj->addSection<SymbolTableSection>();
617
618 SymTab.Name = ".symtab";
619 SymTab.Link = StrTab->Index;
620 // TODO: Factor out dependence on ElfType here.
621 SymTab.EntrySize = sizeof(Elf_Sym);
622
623 // The symbol table always needs a null symbol
624 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
625
626 Obj->SymbolTable = &SymTab;
627 return &SymTab;
628}
629
630template <class ELFT>
631void BinaryELFBuilder<ELFT>::addData(SymbolTableSection *SymTab) {
632 auto Data = ArrayRef<uint8_t>(
633 reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
634 MemBuf->getBufferSize());
635 auto &DataSection = Obj->addSection<Section>(Data);
636 DataSection.Name = ".data";
637 DataSection.Type = ELF::SHT_PROGBITS;
638 DataSection.Size = Data.size();
639 DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
640
641 std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
642 std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
643 [](char c) { return !isalnum(c); }, '_');
644 Twine Prefix = Twine("_binary_") + SanitizedFilename;
645
646 SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
647 /*Value=*/0, STV_DEFAULT, 0, 0);
648 SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
649 /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
650 SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
651 /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
652}
653
654template <class ELFT> void BinaryELFBuilder<ELFT>::initSections() {
655 for (auto &Section : Obj->sections()) {
656 Section.initialize(Obj->sections());
657 }
658}
659
660template <class ELFT> std::unique_ptr<Object> BinaryELFBuilder<ELFT>::build() {
661 initFileHeader();
662 initHeaderSegment();
663 StringTableSection *StrTab = addStrTab();
664 SymbolTableSection *SymTab = addSymTab(StrTab);
665 initSections();
666 addData(SymTab);
667
668 return std::move(Obj);
669}
670
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000671template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
Jake Ehrlich6452b112018-02-14 23:31:33 +0000672 for (auto &Parent : Obj.segments()) {
673 // Every segment will overlap with itself but we don't want a segment to
674 // be it's own parent so we avoid that situation.
675 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
676 // We want a canonical "most parental" segment but this requires
677 // inspecting the ParentSegment.
678 if (compareSegmentsByOffset(&Parent, &Child))
679 if (Child.ParentSegment == nullptr ||
680 compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
681 Child.ParentSegment = &Parent;
682 }
683 }
684 }
685}
686
Jake Ehrlich76e91102018-01-25 22:46:17 +0000687template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000688 uint32_t Index = 0;
689 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000690 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
691 (size_t)Phdr.p_filesz};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000692 Segment &Seg = Obj.addSegment(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000693 Seg.Type = Phdr.p_type;
694 Seg.Flags = Phdr.p_flags;
Petr Hosek3f383832017-08-26 01:32:20 +0000695 Seg.OriginalOffset = Phdr.p_offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000696 Seg.Offset = Phdr.p_offset;
697 Seg.VAddr = Phdr.p_vaddr;
698 Seg.PAddr = Phdr.p_paddr;
699 Seg.FileSize = Phdr.p_filesz;
700 Seg.MemSize = Phdr.p_memsz;
701 Seg.Align = Phdr.p_align;
702 Seg.Index = Index++;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000703 for (auto &Section : Obj.sections()) {
704 if (sectionWithinSegment(Section, Seg)) {
705 Seg.addSection(&Section);
706 if (!Section.ParentSegment ||
707 Section.ParentSegment->Offset > Seg.Offset) {
708 Section.ParentSegment = &Seg;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000709 }
710 }
711 }
712 }
Jake Ehrlich6452b112018-02-14 23:31:33 +0000713
714 auto &ElfHdr = Obj.ElfHdrSegment;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000715 ElfHdr.Index = Index++;
716
717 const auto &Ehdr = *ElfFile.getHeader();
718 auto &PrHdr = Obj.ProgramHdrSegment;
719 PrHdr.Type = PT_PHDR;
720 PrHdr.Flags = 0;
721 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
722 // Whereas this works automatically for ElfHdr, here OriginalOffset is
723 // always non-zero and to ensure the equation we assign the same value to
724 // VAddr as well.
725 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
726 PrHdr.PAddr = 0;
727 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000728 // The spec requires us to naturally align all the fields.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000729 PrHdr.Align = sizeof(Elf_Addr);
730 PrHdr.Index = Index++;
731
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000732 // Now we do an O(n^2) loop through the segments in order to match up
733 // segments.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000734 for (auto &Child : Obj.segments())
735 setParentSegment(Child);
736 setParentSegment(ElfHdr);
737 setParentSegment(PrHdr);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000738}
739
740template <class ELFT>
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000741void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
742 auto SecTable = Obj.sections();
743 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
744 GroupSec->Link,
745 "Link field value " + Twine(GroupSec->Link) + " in section " +
746 GroupSec->Name + " is invalid",
747 "Link field value " + Twine(GroupSec->Link) + " in section " +
748 GroupSec->Name + " is not a symbol table");
749 auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
750 if (!Sym)
751 error("Info field value " + Twine(GroupSec->Info) + " in section " +
752 GroupSec->Name + " is not a valid symbol index");
753 GroupSec->setSymTab(SymTab);
754 GroupSec->setSymbol(Sym);
755 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
756 GroupSec->Contents.empty())
757 error("The content of the section " + GroupSec->Name + " is malformed");
758 const ELF::Elf32_Word *Word =
759 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
760 const ELF::Elf32_Word *End =
761 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
762 GroupSec->setFlagWord(*Word++);
763 for (; Word != End; ++Word) {
764 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
765 GroupSec->addMember(SecTable.getSection(
766 Index, "Group member index " + Twine(Index) + " in section " +
767 GroupSec->Name + " is invalid"));
768 }
769}
770
771template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000772void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000773 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
774 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000775 ArrayRef<Elf_Word> ShndxData;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000776
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000777 auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
778 for (const auto &Sym : Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000779 SectionBase *DefSection = nullptr;
780 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000781
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000782 if (Sym.st_shndx == SHN_XINDEX) {
783 if (SymTab->getShndxTable() == nullptr)
784 error("Symbol '" + Name +
785 "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
786 if (ShndxData.data() == nullptr) {
787 const Elf_Shdr &ShndxSec =
788 *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
789 ShndxData = unwrapOrError(
790 ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
791 if (ShndxData.size() != Symbols.size())
792 error("Symbol section index table does not have the same number of "
793 "entries as the symbol table.");
794 }
795 Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
796 DefSection = Obj.sections().getSection(
797 Index,
Puyan Lotfi97604b42018-08-02 18:16:52 +0000798 "Symbol '" + Name + "' has invalid section index " + Twine(Index));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000799 } else if (Sym.st_shndx >= SHN_LORESERVE) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000800 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000801 error(
802 "Symbol '" + Name +
803 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
804 Twine(Sym.st_shndx));
805 }
806 } else if (Sym.st_shndx != SHN_UNDEF) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000807 DefSection = Obj.sections().getSection(
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000808 Sym.st_shndx, "Symbol '" + Name +
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000809 "' is defined has invalid section index " +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000810 Twine(Sym.st_shndx));
Petr Hosek79cee9e2017-08-29 02:12:03 +0000811 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000812
Petr Hosek79cee9e2017-08-29 02:12:03 +0000813 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000814 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000815 }
816}
817
818template <class ELFT>
Petr Hosekd7df9b22017-09-06 23:41:02 +0000819static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
820
821template <class ELFT>
822static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
823 ToSet = Rela.r_addend;
824}
825
Jake Ehrlich76e91102018-01-25 22:46:17 +0000826template <class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000827static void initRelocations(RelocationSection *Relocs,
828 SymbolTableSection *SymbolTable, T RelRange) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000829 for (const auto &Rel : RelRange) {
830 Relocation ToAdd;
831 ToAdd.Offset = Rel.r_offset;
832 getAddend(ToAdd.Addend, Rel);
833 ToAdd.Type = Rel.getType(false);
Paul Semel31a212d2018-05-22 01:04:36 +0000834 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000835 Relocs->addRelocation(ToAdd);
836 }
837}
838
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000839SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000840 if (Index == SHN_UNDEF || Index > Sections.size())
841 error(ErrMsg);
842 return Sections[Index - 1].get();
843}
844
845template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000846T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
Zachary Turner41a9ee92017-10-11 23:54:34 +0000847 Twine TypeErrMsg) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000848 if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000849 return Sec;
850 error(TypeErrMsg);
851}
852
Petr Hosekd7df9b22017-09-06 23:41:02 +0000853template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000854SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000855 ArrayRef<uint8_t> Data;
856 switch (Shdr.sh_type) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000857 case SHT_REL:
858 case SHT_RELA:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000859 if (Shdr.sh_flags & SHF_ALLOC) {
860 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000861 return Obj.addSection<DynamicRelocationSection>(Data);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000862 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000863 return Obj.addSection<RelocationSection>();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000864 case SHT_STRTAB:
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000865 // If a string table is allocated we don't want to mess with it. That would
866 // mean altering the memory image. There are no special link types or
867 // anything so we can just use a Section.
868 if (Shdr.sh_flags & SHF_ALLOC) {
869 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000870 return Obj.addSection<Section>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000871 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000872 return Obj.addSection<StringTableSection>();
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000873 case SHT_HASH:
874 case SHT_GNU_HASH:
875 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
876 // Because of this we don't need to mess with the hash tables either.
877 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000878 return Obj.addSection<Section>(Data);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000879 case SHT_GROUP:
880 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
881 return Obj.addSection<GroupSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000882 case SHT_DYNSYM:
883 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000884 return Obj.addSection<DynamicSymbolTableSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000885 case SHT_DYNAMIC:
886 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000887 return Obj.addSection<DynamicSection>(Data);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000888 case SHT_SYMTAB: {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000889 auto &SymTab = Obj.addSection<SymbolTableSection>();
890 Obj.SymbolTable = &SymTab;
891 return SymTab;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000892 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000893 case SHT_SYMTAB_SHNDX: {
894 auto &ShndxSection = Obj.addSection<SectionIndexSection>();
895 Obj.SectionIndexTable = &ShndxSection;
896 return ShndxSection;
897 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000898 case SHT_NOBITS:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000899 return Obj.addSection<Section>(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000900 default:
901 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000902 return Obj.addSection<Section>(Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000903 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000904}
905
Jake Ehrlich76e91102018-01-25 22:46:17 +0000906template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000907 uint32_t Index = 0;
908 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
909 if (Index == 0) {
910 ++Index;
911 continue;
912 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000913 auto &Sec = makeSection(Shdr);
914 Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
915 Sec.Type = Shdr.sh_type;
916 Sec.Flags = Shdr.sh_flags;
917 Sec.Addr = Shdr.sh_addr;
918 Sec.Offset = Shdr.sh_offset;
919 Sec.OriginalOffset = Shdr.sh_offset;
920 Sec.Size = Shdr.sh_size;
921 Sec.Link = Shdr.sh_link;
922 Sec.Info = Shdr.sh_info;
923 Sec.Align = Shdr.sh_addralign;
924 Sec.EntrySize = Shdr.sh_entsize;
925 Sec.Index = Index++;
Paul Semela42dec72018-08-09 17:05:21 +0000926 Sec.OriginalData =
927 ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
928 (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000929 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000930
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000931 // If a section index table exists we'll need to initialize it before we
932 // initialize the symbol table because the symbol table might need to
933 // reference it.
934 if (Obj.SectionIndexTable)
935 Obj.SectionIndexTable->initialize(Obj.sections());
936
Petr Hosek79cee9e2017-08-29 02:12:03 +0000937 // Now that all of the sections have been added we can fill out some extra
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000938 // details about symbol tables. We need the symbol table filled out before
939 // any relocations.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000940 if (Obj.SymbolTable) {
941 Obj.SymbolTable->initialize(Obj.sections());
942 initSymbolTable(Obj.SymbolTable);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000943 }
Petr Hosekd7df9b22017-09-06 23:41:02 +0000944
945 // Now that all sections and symbols have been added we can add
946 // relocations that reference symbols and set the link and info fields for
947 // relocation sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000948 for (auto &Section : Obj.sections()) {
949 if (&Section == Obj.SymbolTable)
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000950 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000951 Section.initialize(Obj.sections());
952 if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000953 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
954 if (RelSec->Type == SHT_REL)
Jake Ehrlich76e91102018-01-25 22:46:17 +0000955 initRelocations(RelSec, Obj.SymbolTable,
956 unwrapOrError(ElfFile.rels(Shdr)));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000957 else
Jake Ehrlich76e91102018-01-25 22:46:17 +0000958 initRelocations(RelSec, Obj.SymbolTable,
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000959 unwrapOrError(ElfFile.relas(Shdr)));
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000960 } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
961 initGroupSection(GroupSec);
Petr Hosekd7df9b22017-09-06 23:41:02 +0000962 }
963 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000964}
965
Jake Ehrlich76e91102018-01-25 22:46:17 +0000966template <class ELFT> void ELFBuilder<ELFT>::build() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000967 const auto &Ehdr = *ElfFile.getHeader();
968
Jake Ehrlich76e91102018-01-25 22:46:17 +0000969 Obj.Type = Ehdr.e_type;
970 Obj.Machine = Ehdr.e_machine;
971 Obj.Version = Ehdr.e_version;
972 Obj.Entry = Ehdr.e_entry;
973 Obj.Flags = Ehdr.e_flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000974
Jake Ehrlich76e91102018-01-25 22:46:17 +0000975 readSectionHeaders();
976 readProgramHeaders();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000977
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000978 uint32_t ShstrIndex = Ehdr.e_shstrndx;
979 if (ShstrIndex == SHN_XINDEX)
980 ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
981
Jake Ehrlich76e91102018-01-25 22:46:17 +0000982 Obj.SectionNames =
983 Obj.sections().template getSectionOfType<StringTableSection>(
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000984 ShstrIndex,
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000985 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +0000986 " in elf header " + " is invalid",
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000987 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +0000988 " in elf header " + " is not a string table");
Petr Hosek05a04cb2017-08-01 00:33:58 +0000989}
990
Jake Ehrlich76e91102018-01-25 22:46:17 +0000991// A generic size function which computes sizes of any random access range.
992template <class R> size_t size(R &&Range) {
993 return static_cast<size_t>(std::end(Range) - std::begin(Range));
994}
995
996Writer::~Writer() {}
997
998Reader::~Reader() {}
999
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001000std::unique_ptr<Object> BinaryReader::create() const {
1001 if (MInfo.Is64Bit)
1002 return MInfo.IsLittleEndian
1003 ? BinaryELFBuilder<ELF64LE>(MInfo.EMachine, MemBuf).build()
1004 : BinaryELFBuilder<ELF64BE>(MInfo.EMachine, MemBuf).build();
1005 else
1006 return MInfo.IsLittleEndian
1007 ? BinaryELFBuilder<ELF32LE>(MInfo.EMachine, MemBuf).build()
1008 : BinaryELFBuilder<ELF32BE>(MInfo.EMachine, MemBuf).build();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001009}
1010
1011std::unique_ptr<Object> ELFReader::create() const {
Alexander Shaposhnikov58cb1972018-06-07 19:41:42 +00001012 auto Obj = llvm::make_unique<Object>();
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001013 if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001014 ELFBuilder<ELF32LE> Builder(*o, *Obj);
1015 Builder.build();
1016 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001017 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001018 ELFBuilder<ELF64LE> Builder(*o, *Obj);
1019 Builder.build();
1020 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001021 } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001022 ELFBuilder<ELF32BE> Builder(*o, *Obj);
1023 Builder.build();
1024 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001025 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001026 ELFBuilder<ELF64BE> Builder(*o, *Obj);
1027 Builder.build();
1028 return Obj;
1029 }
1030 error("Invalid file type");
1031}
1032
1033template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001034 uint8_t *B = Buf.getBufferStart();
1035 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001036 std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
1037 Ehdr.e_ident[EI_MAG0] = 0x7f;
1038 Ehdr.e_ident[EI_MAG1] = 'E';
1039 Ehdr.e_ident[EI_MAG2] = 'L';
1040 Ehdr.e_ident[EI_MAG3] = 'F';
1041 Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1042 Ehdr.e_ident[EI_DATA] =
1043 ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
1044 Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1045 Ehdr.e_ident[EI_OSABI] = ELFOSABI_NONE;
1046 Ehdr.e_ident[EI_ABIVERSION] = 0;
1047
Jake Ehrlich76e91102018-01-25 22:46:17 +00001048 Ehdr.e_type = Obj.Type;
1049 Ehdr.e_machine = Obj.Machine;
1050 Ehdr.e_version = Obj.Version;
1051 Ehdr.e_entry = Obj.Entry;
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001052 // TODO: Only set phoff when a program header exists, to avoid tools
1053 // thinking this is corrupt data.
Jake Ehrlich6452b112018-02-14 23:31:33 +00001054 Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001055 Ehdr.e_flags = Obj.Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001056 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
1057 Ehdr.e_phentsize = sizeof(Elf_Phdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001058 Ehdr.e_phnum = size(Obj.segments());
Petr Hosek05a04cb2017-08-01 00:33:58 +00001059 Ehdr.e_shentsize = sizeof(Elf_Shdr);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001060 if (WriteSectionHeaders) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001061 Ehdr.e_shoff = Obj.SHOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001062 // """
1063 // If the number of sections is greater than or equal to
1064 // SHN_LORESERVE (0xff00), this member has the value zero and the actual
1065 // number of section header table entries is contained in the sh_size field
1066 // of the section header at index 0.
1067 // """
1068 auto Shnum = size(Obj.sections()) + 1;
1069 if (Shnum >= SHN_LORESERVE)
1070 Ehdr.e_shnum = 0;
1071 else
1072 Ehdr.e_shnum = Shnum;
1073 // """
1074 // If the section name string table section index is greater than or equal
1075 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
1076 // and the actual index of the section name string table section is
1077 // contained in the sh_link field of the section header at index 0.
1078 // """
1079 if (Obj.SectionNames->Index >= SHN_LORESERVE)
1080 Ehdr.e_shstrndx = SHN_XINDEX;
1081 else
1082 Ehdr.e_shstrndx = Obj.SectionNames->Index;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001083 } else {
1084 Ehdr.e_shoff = 0;
1085 Ehdr.e_shnum = 0;
1086 Ehdr.e_shstrndx = 0;
1087 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001088}
1089
Jake Ehrlich76e91102018-01-25 22:46:17 +00001090template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1091 for (auto &Seg : Obj.segments())
1092 writePhdr(Seg);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001093}
1094
Jake Ehrlich76e91102018-01-25 22:46:17 +00001095template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001096 uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001097 // This reference serves to write the dummy section header at the begining
Jake Ehrlich425ec9f2017-09-15 22:04:09 +00001098 // of the file. It is not used for anything else
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001099 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001100 Shdr.sh_name = 0;
1101 Shdr.sh_type = SHT_NULL;
1102 Shdr.sh_flags = 0;
1103 Shdr.sh_addr = 0;
1104 Shdr.sh_offset = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001105 // See writeEhdr for why we do this.
1106 uint64_t Shnum = size(Obj.sections()) + 1;
1107 if (Shnum >= SHN_LORESERVE)
1108 Shdr.sh_size = Shnum;
1109 else
1110 Shdr.sh_size = 0;
1111 // See writeEhdr for why we do this.
1112 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1113 Shdr.sh_link = Obj.SectionNames->Index;
1114 else
1115 Shdr.sh_link = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001116 Shdr.sh_info = 0;
1117 Shdr.sh_addralign = 0;
1118 Shdr.sh_entsize = 0;
1119
Jake Ehrlich76e91102018-01-25 22:46:17 +00001120 for (auto &Sec : Obj.sections())
1121 writeShdr(Sec);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001122}
1123
Jake Ehrlich76e91102018-01-25 22:46:17 +00001124template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1125 for (auto &Sec : Obj.sections())
1126 Sec.accept(*SecWriter);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001127}
1128
Jake Ehrlich76e91102018-01-25 22:46:17 +00001129void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001130
1131 auto Iter = std::stable_partition(
1132 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1133 if (ToRemove(*Sec))
1134 return false;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001135 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1136 if (auto ToRelSec = RelSec->getSection())
1137 return !ToRemove(*ToRelSec);
1138 }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001139 return true;
1140 });
1141 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1142 SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001143 if (SectionNames != nullptr && ToRemove(*SectionNames))
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001144 SectionNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001145 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1146 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001147 // Now make sure there are no remaining references to the sections that will
1148 // be removed. Sometimes it is impossible to remove a reference so we emit
1149 // an error here instead.
1150 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1151 for (auto &Segment : Segments)
1152 Segment->removeSection(RemoveSec.get());
1153 for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1154 KeepSec->removeSectionReferences(RemoveSec.get());
1155 }
1156 // Now finally get rid of them all togethor.
1157 Sections.erase(Iter, std::end(Sections));
1158}
1159
Paul Semel4246a462018-05-09 21:36:54 +00001160void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1161 if (!SymbolTable)
1162 return;
1163
1164 for (const SecPtr &Sec : Sections)
1165 Sec->removeSymbols(ToRemove);
1166}
1167
Jake Ehrlich76e91102018-01-25 22:46:17 +00001168void Object::sortSections() {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001169 // Put all sections in offset order. Maintain the ordering as closely as
1170 // possible while meeting that demand however.
1171 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1172 return A->OriginalOffset < B->OriginalOffset;
1173 };
1174 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1175 CompareSections);
1176}
1177
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001178static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1179 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1180 if (Align == 0)
1181 Align = 1;
1182 auto Diff =
1183 static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1184 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1185 // (Offset + Diff) & -Align == Addr & -Align will still hold.
1186 if (Diff < 0)
1187 Diff += Align;
1188 return Offset + Diff;
1189}
1190
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001191// Orders segments such that if x = y->ParentSegment then y comes before x.
1192static void OrderSegments(std::vector<Segment *> &Segments) {
Jake Ehrlich46814be2018-01-22 19:27:30 +00001193 std::stable_sort(std::begin(Segments), std::end(Segments),
1194 compareSegmentsByOffset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001195}
1196
1197// This function finds a consistent layout for a list of segments starting from
1198// an Offset. It assumes that Segments have been sorted by OrderSegments and
1199// returns an Offset one past the end of the last segment.
1200static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1201 uint64_t Offset) {
1202 assert(std::is_sorted(std::begin(Segments), std::end(Segments),
Jake Ehrlich46814be2018-01-22 19:27:30 +00001203 compareSegmentsByOffset));
Petr Hosek3f383832017-08-26 01:32:20 +00001204 // The only way a segment should move is if a section was between two
1205 // segments and that section was removed. If that section isn't in a segment
1206 // then it's acceptable, but not ideal, to simply move it to after the
1207 // segments. So we can simply layout segments one after the other accounting
1208 // for alignment.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001209 for (auto &Segment : Segments) {
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001210 // We assume that segments have been ordered by OriginalOffset and Index
1211 // such that a parent segment will always come before a child segment in
1212 // OrderedSegments. This means that the Offset of the ParentSegment should
1213 // already be set and we can set our offset relative to it.
1214 if (Segment->ParentSegment != nullptr) {
1215 auto Parent = Segment->ParentSegment;
1216 Segment->Offset =
1217 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1218 } else {
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001219 Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001220 Segment->Offset = Offset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001221 }
Jake Ehrlich084400b2017-10-04 17:44:42 +00001222 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
Petr Hosek3f383832017-08-26 01:32:20 +00001223 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001224 return Offset;
1225}
1226
1227// This function finds a consistent layout for a list of sections. It assumes
1228// that the ->ParentSegment of each section has already been laid out. The
1229// supplied starting Offset is used for the starting offset of any section that
1230// does not have a ParentSegment. It returns either the offset given if all
1231// sections had a ParentSegment or an offset one past the last section if there
1232// was a section that didn't have a ParentSegment.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001233template <class Range>
1234static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
Petr Hosek3f383832017-08-26 01:32:20 +00001235 // Now the offset of every segment has been set we can assign the offsets
1236 // of each section. For sections that are covered by a segment we should use
1237 // the segment's original offset and the section's original offset to compute
1238 // the offset from the start of the segment. Using the offset from the start
1239 // of the segment we can assign a new offset to the section. For sections not
1240 // covered by segments we can just bump Offset to the next valid location.
1241 uint32_t Index = 1;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001242 for (auto &Section : Sections) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001243 Section.Index = Index++;
1244 if (Section.ParentSegment != nullptr) {
1245 auto Segment = *Section.ParentSegment;
1246 Section.Offset =
1247 Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
Petr Hosek3f383832017-08-26 01:32:20 +00001248 } else {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001249 Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1250 Section.Offset = Offset;
1251 if (Section.Type != SHT_NOBITS)
1252 Offset += Section.Size;
Petr Hosek3f383832017-08-26 01:32:20 +00001253 }
1254 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001255 return Offset;
1256}
Petr Hosek3f383832017-08-26 01:32:20 +00001257
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001258template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
1259 auto &ElfHdr = Obj.ElfHdrSegment;
1260 ElfHdr.Type = PT_PHDR;
1261 ElfHdr.Flags = 0;
1262 ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
1263 ElfHdr.VAddr = 0;
1264 ElfHdr.PAddr = 0;
1265 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
1266 ElfHdr.Align = 0;
1267}
1268
Jake Ehrlich76e91102018-01-25 22:46:17 +00001269template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001270 // We need a temporary list of segments that has a special order to it
1271 // so that we know that anytime ->ParentSegment is set that segment has
1272 // already had its offset properly set.
1273 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001274 for (auto &Segment : Obj.segments())
1275 OrderedSegments.push_back(&Segment);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001276 OrderedSegments.push_back(&Obj.ElfHdrSegment);
1277 OrderedSegments.push_back(&Obj.ProgramHdrSegment);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001278 OrderSegments(OrderedSegments);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001279 // Offset is used as the start offset of the first segment to be laid out.
1280 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1281 // we start at offset 0.
1282 uint64_t Offset = 0;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001283 Offset = LayoutSegments(OrderedSegments, Offset);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001284 Offset = LayoutSections(Obj.sections(), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001285 // If we need to write the section header table out then we need to align the
1286 // Offset so that SHOffset is valid.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001287 if (WriteSectionHeaders)
Jordan Rupprechtde965ea2018-08-10 16:25:58 +00001288 Offset = alignTo(Offset, sizeof(Elf_Addr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001289 Obj.SHOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001290}
1291
Jake Ehrlich76e91102018-01-25 22:46:17 +00001292template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001293 // We already have the section header offset so we can calculate the total
1294 // size by just adding up the size of each section header.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001295 auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1296 return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001297 NullSectionSize;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001298}
1299
Jake Ehrlich76e91102018-01-25 22:46:17 +00001300template <class ELFT> void ELFWriter<ELFT>::write() {
1301 writeEhdr();
1302 writePhdrs();
1303 writeSectionData();
1304 if (WriteSectionHeaders)
1305 writeShdrs();
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001306 if (auto E = Buf.commit())
1307 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001308}
1309
1310template <class ELFT> void ELFWriter<ELFT>::finalize() {
1311 // It could happen that SectionNames has been removed and yet the user wants
1312 // a section header table output. We need to throw an error if a user tries
1313 // to do that.
1314 if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1315 error("Cannot write section header table because section header string "
1316 "table was removed.");
1317
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001318 Obj.sortSections();
1319
1320 // We need to assign indexes before we perform layout because we need to know
1321 // if we need large indexes or not. We can assign indexes first and check as
1322 // we go to see if we will actully need large indexes.
1323 bool NeedsLargeIndexes = false;
1324 if (size(Obj.sections()) >= SHN_LORESERVE) {
1325 auto Sections = Obj.sections();
1326 NeedsLargeIndexes =
1327 std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1328 [](const SectionBase &Sec) { return Sec.HasSymbol; });
1329 // TODO: handle case where only one section needs the large index table but
1330 // only needs it because the large index table hasn't been removed yet.
1331 }
1332
1333 if (NeedsLargeIndexes) {
1334 // This means we definitely need to have a section index table but if we
1335 // already have one then we should use it instead of making a new one.
1336 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1337 // Addition of a section to the end does not invalidate the indexes of
1338 // other sections and assigns the correct index to the new section.
1339 auto &Shndx = Obj.addSection<SectionIndexSection>();
1340 Obj.SymbolTable->setShndxTable(&Shndx);
1341 Shndx.setSymTab(Obj.SymbolTable);
1342 }
1343 } else {
1344 // Since we don't need SectionIndexTable we should remove it and all
1345 // references to it.
1346 if (Obj.SectionIndexTable != nullptr) {
1347 Obj.removeSections([this](const SectionBase &Sec) {
1348 return &Sec == Obj.SectionIndexTable;
1349 });
1350 }
1351 }
1352
1353 // Make sure we add the names of all the sections. Importantly this must be
1354 // done after we decide to add or remove SectionIndexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001355 if (Obj.SectionNames != nullptr)
1356 for (const auto &Section : Obj.sections()) {
1357 Obj.SectionNames->addString(Section.Name);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001358 }
Jake Ehrlich0a151bd2018-03-07 19:59:15 +00001359
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001360 initEhdrSegment();
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001361 // Before we can prepare for layout the indexes need to be finalized.
1362 uint64_t Index = 0;
1363 for (auto &Sec : Obj.sections())
1364 Sec.Index = Index++;
1365
1366 // The symbol table does not update all other sections on update. For
1367 // instance, symbol names are not added as new symbols are added. This means
1368 // that some sections, like .strtab, don't yet have their final size.
1369 if (Obj.SymbolTable != nullptr)
1370 Obj.SymbolTable->prepareForLayout();
1371
Petr Hosekc4df10e2017-08-04 21:09:26 +00001372 assignOffsets();
1373
1374 // Finalize SectionNames first so that we can assign name indexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001375 if (Obj.SectionNames != nullptr)
1376 Obj.SectionNames->finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001377 // Finally now that all offsets and indexes have been set we can finalize any
1378 // remaining issues.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001379 uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1380 for (auto &Section : Obj.sections()) {
1381 Section.HeaderOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001382 Offset += sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001383 if (WriteSectionHeaders)
1384 Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1385 Section.finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001386 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001387
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001388 Buf.allocate(totalSize());
1389 SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001390}
1391
Jake Ehrlich76e91102018-01-25 22:46:17 +00001392void BinaryWriter::write() {
1393 for (auto &Section : Obj.sections()) {
1394 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001395 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001396 Section.accept(*SecWriter);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001397 }
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001398 if (auto E = Buf.commit())
1399 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
Petr Hosekc4df10e2017-08-04 21:09:26 +00001400}
1401
Jake Ehrlich76e91102018-01-25 22:46:17 +00001402void BinaryWriter::finalize() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001403 // TODO: Create a filter range to construct OrderedSegments from so that this
1404 // code can be deduped with assignOffsets above. This should also solve the
1405 // todo below for LayoutSections.
1406 // We need a temporary list of segments that has a special order to it
1407 // so that we know that anytime ->ParentSegment is set that segment has
1408 // already had it's offset properly set. We only want to consider the segments
1409 // that will affect layout of allocated sections so we only add those.
1410 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001411 for (auto &Section : Obj.sections()) {
1412 if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1413 OrderedSegments.push_back(Section.ParentSegment);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001414 }
1415 }
Jake Ehrlich46814be2018-01-22 19:27:30 +00001416
1417 // For binary output, we're going to use physical addresses instead of
1418 // virtual addresses, since a binary output is used for cases like ROM
1419 // loading and physical addresses are intended for ROM loading.
1420 // However, if no segment has a physical address, we'll fallback to using
1421 // virtual addresses for all.
1422 if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments),
1423 [](const Segment *Segment) { return Segment->PAddr == 0; }))
1424 for (const auto &Segment : OrderedSegments)
1425 Segment->PAddr = Segment->VAddr;
1426
1427 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1428 compareSegmentsByPAddr);
1429
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001430 // Because we add a ParentSegment for each section we might have duplicate
1431 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1432 // would do very strange things.
1433 auto End =
1434 std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1435 OrderedSegments.erase(End, std::end(OrderedSegments));
1436
Jake Ehrlich46814be2018-01-22 19:27:30 +00001437 uint64_t Offset = 0;
1438
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001439 // Modify the first segment so that there is no gap at the start. This allows
1440 // our layout algorithm to proceed as expected while not out writing out the
1441 // gap at the start.
1442 if (!OrderedSegments.empty()) {
1443 auto Seg = OrderedSegments[0];
1444 auto Sec = Seg->firstSection();
1445 auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1446 Seg->OriginalOffset += Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001447 // The size needs to be shrunk as well.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001448 Seg->FileSize -= Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001449 // The PAddr needs to be increased to remove the gap before the first
1450 // section.
1451 Seg->PAddr += Diff;
1452 uint64_t LowestPAddr = Seg->PAddr;
1453 for (auto &Segment : OrderedSegments) {
1454 Segment->Offset = Segment->PAddr - LowestPAddr;
1455 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1456 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001457 }
1458
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001459 // TODO: generalize LayoutSections to take a range. Pass a special range
1460 // constructed from an iterator that skips values for which a predicate does
1461 // not hold. Then pass such a range to LayoutSections instead of constructing
1462 // AllocatedSections here.
1463 std::vector<SectionBase *> AllocatedSections;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001464 for (auto &Section : Obj.sections()) {
1465 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001466 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001467 AllocatedSections.push_back(&Section);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001468 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001469 LayoutSections(make_pointee_range(AllocatedSections), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001470
1471 // Now that every section has been laid out we just need to compute the total
1472 // file size. This might not be the same as the offset returned by
1473 // LayoutSections, because we want to truncate the last segment to the end of
1474 // its last section, to match GNU objcopy's behaviour.
1475 TotalSize = 0;
1476 for (const auto &Section : AllocatedSections) {
1477 if (Section->Type != SHT_NOBITS)
1478 TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1479 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001480
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001481 Buf.allocate(TotalSize);
1482 SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001483}
1484
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001485namespace llvm {
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001486namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001487
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001488template class BinaryELFBuilder<ELF64LE>;
1489template class BinaryELFBuilder<ELF64BE>;
1490template class BinaryELFBuilder<ELF32LE>;
1491template class BinaryELFBuilder<ELF32BE>;
1492
Jake Ehrlich76e91102018-01-25 22:46:17 +00001493template class ELFBuilder<ELF64LE>;
1494template class ELFBuilder<ELF64BE>;
1495template class ELFBuilder<ELF32LE>;
1496template class ELFBuilder<ELF32BE>;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001497
Jake Ehrlich76e91102018-01-25 22:46:17 +00001498template class ELFWriter<ELF64LE>;
1499template class ELFWriter<ELF64BE>;
1500template class ELFWriter<ELF32LE>;
1501template class ELFWriter<ELF32BE>;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001502} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001503} // end namespace llvm