blob: aaefcf99f387818121d23a37eebf635437b86678 [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"
Puyan Lotfi99124cc2018-09-07 08:10:22 +000018#include "llvm/MC/MCTargetOptions.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000019#include "llvm/Object/ELFObjectFile.h"
Puyan Lotfi99124cc2018-09-07 08:10:22 +000020#include "llvm/Support/Compression.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000021#include "llvm/Support/ErrorHandling.h"
22#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000023#include "llvm/Support/Path.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000024#include <algorithm>
25#include <cstddef>
26#include <cstdint>
27#include <iterator>
28#include <utility>
29#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000030
31using namespace llvm;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +000032using namespace llvm::objcopy;
Petr Hosek05a04cb2017-08-01 00:33:58 +000033using namespace object;
34using namespace ELF;
35
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000036Buffer::~Buffer() {}
37
38void FileBuffer::allocate(size_t Size) {
39 Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
40 FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
41 handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) {
42 error("failed to open " + getName() + ": " + E.message());
43 });
44 Buf = std::move(*BufferOrErr);
45}
46
47Error FileBuffer::commit() { return Buf->commit(); }
48
49uint8_t *FileBuffer::getBufferStart() {
50 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
51}
52
53void MemBuffer::allocate(size_t Size) {
54 Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
55}
56
57Error MemBuffer::commit() { return Error::success(); }
58
59uint8_t *MemBuffer::getBufferStart() {
60 return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
61}
62
63std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
64 return std::move(Buf);
65}
66
Jake Ehrlich76e91102018-01-25 22:46:17 +000067template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000068 uint8_t *B = Buf.getBufferStart();
69 B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
70 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000071 Phdr.p_type = Seg.Type;
72 Phdr.p_flags = Seg.Flags;
73 Phdr.p_offset = Seg.Offset;
74 Phdr.p_vaddr = Seg.VAddr;
75 Phdr.p_paddr = Seg.PAddr;
76 Phdr.p_filesz = Seg.FileSize;
77 Phdr.p_memsz = Seg.MemSize;
78 Phdr.p_align = Seg.Align;
Petr Hosekc4df10e2017-08-04 21:09:26 +000079}
80
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000081void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
Paul Semel4246a462018-05-09 21:36:54 +000082void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
Jake Ehrlichf5a43772017-09-25 20:37:28 +000083void SectionBase::initialize(SectionTableRef SecTable) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000084void SectionBase::finalize() {}
Paul Semel99dda0b2018-05-25 11:01:25 +000085void SectionBase::markSymbols() {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000086
Jake Ehrlich76e91102018-01-25 22:46:17 +000087template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000088 uint8_t *B = Buf.getBufferStart();
89 B += Sec.HeaderOffset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +000090 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000091 Shdr.sh_name = Sec.NameIndex;
92 Shdr.sh_type = Sec.Type;
93 Shdr.sh_flags = Sec.Flags;
94 Shdr.sh_addr = Sec.Addr;
95 Shdr.sh_offset = Sec.Offset;
96 Shdr.sh_size = Sec.Size;
97 Shdr.sh_link = Sec.Link;
98 Shdr.sh_info = Sec.Info;
99 Shdr.sh_addralign = Sec.Align;
100 Shdr.sh_entsize = Sec.EntrySize;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000101}
102
Jake Ehrlich76e91102018-01-25 22:46:17 +0000103SectionVisitor::~SectionVisitor() {}
104
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000105void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
106 error("Cannot write symbol section index table '" + Sec.Name + "' ");
107}
108
Jake Ehrlich76e91102018-01-25 22:46:17 +0000109void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
110 error("Cannot write symbol table '" + Sec.Name + "' out to binary");
111}
112
113void BinarySectionWriter::visit(const RelocationSection &Sec) {
114 error("Cannot write relocation section '" + Sec.Name + "' out to binary");
115}
116
117void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000118 error("Cannot write '" + Sec.Name + "' out to binary");
119}
120
121void BinarySectionWriter::visit(const GroupSection &Sec) {
122 error("Cannot write '" + Sec.Name + "' out to binary");
Jake Ehrlich76e91102018-01-25 22:46:17 +0000123}
124
125void SectionWriter::visit(const Section &Sec) {
126 if (Sec.Type == SHT_NOBITS)
Petr Hosek05a04cb2017-08-01 00:33:58 +0000127 return;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000128 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
129 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000130}
131
Jake Ehrlich76e91102018-01-25 22:46:17 +0000132void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
133
134void SectionWriter::visit(const OwnedDataSection &Sec) {
135 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
136 std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
137}
138
139void OwnedDataSection::accept(SectionVisitor &Visitor) const {
140 Visitor.visit(*this);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000141}
142
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000143void BinarySectionWriter::visit(const CompressedSection &Sec) {
144 error("Cannot write compressed section '" + Sec.Name + "' ");
145}
146
147template <class ELFT>
148void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
149 uint8_t *Buf = Out.getBufferStart();
150 Buf += Sec.Offset;
151
152 if (Sec.CompressionType == DebugCompressionType::None) {
153 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
154 return;
155 }
156
157 if (Sec.CompressionType == DebugCompressionType::GNU) {
158 const char *Magic = "ZLIB";
159 memcpy(Buf, Magic, strlen(Magic));
160 Buf += strlen(Magic);
161 const uint64_t DecompressedSize =
162 support::endian::read64be(&Sec.DecompressedSize);
163 memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
164 Buf += sizeof(DecompressedSize);
165 } else {
166 Elf_Chdr_Impl<ELFT> Chdr;
167 Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
168 Chdr.ch_size = Sec.DecompressedSize;
169 Chdr.ch_addralign = Sec.DecompressedAlign;
170 memcpy(Buf, &Chdr, sizeof(Chdr));
171 Buf += sizeof(Chdr);
172 }
173
174 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
175}
176
177CompressedSection::CompressedSection(const SectionBase &Sec,
178 DebugCompressionType CompressionType)
179 : SectionBase(Sec), CompressionType(CompressionType),
180 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
181
182 if (!zlib::isAvailable()) {
183 CompressionType = DebugCompressionType::None;
184 return;
185 }
186
187 if (Error E = zlib::compress(
188 StringRef(reinterpret_cast<const char *>(OriginalData.data()),
189 OriginalData.size()),
190 CompressedData))
191 reportError(Name, std::move(E));
192
193 size_t ChdrSize;
194 if (CompressionType == DebugCompressionType::GNU) {
195 Name = ".z" + Sec.Name.substr(1);
196 ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
197 } else {
198 Flags |= ELF::SHF_COMPRESSED;
199 ChdrSize =
200 std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
201 sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
202 std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
203 sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
204 }
205 Size = ChdrSize + CompressedData.size();
206 Align = 8;
207}
208
209void CompressedSection::accept(SectionVisitor &Visitor) const {
210 Visitor.visit(*this);
211}
212
Petr Hosek05a04cb2017-08-01 00:33:58 +0000213void StringTableSection::addString(StringRef Name) {
214 StrTabBuilder.add(Name);
215 Size = StrTabBuilder.getSize();
216}
217
218uint32_t StringTableSection::findIndex(StringRef Name) const {
219 return StrTabBuilder.getOffset(Name);
220}
221
222void StringTableSection::finalize() { StrTabBuilder.finalize(); }
223
Jake Ehrlich76e91102018-01-25 22:46:17 +0000224void SectionWriter::visit(const StringTableSection &Sec) {
225 Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
226}
227
228void StringTableSection::accept(SectionVisitor &Visitor) const {
229 Visitor.visit(*this);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000230}
231
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000232template <class ELFT>
233void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
234 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000235 auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000236 std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer);
237}
238
239void SectionIndexSection::initialize(SectionTableRef SecTable) {
240 Size = 0;
241 setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
242 Link,
243 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
244 "Link field value " + Twine(Link) + " in section " + Name +
245 " is not a symbol table"));
246 Symbols->setShndxTable(this);
247}
248
249void SectionIndexSection::finalize() { Link = Symbols->Index; }
250
251void SectionIndexSection::accept(SectionVisitor &Visitor) const {
252 Visitor.visit(*this);
253}
254
Petr Hosekc1135772017-09-13 03:04:50 +0000255static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000256 switch (Index) {
257 case SHN_ABS:
258 case SHN_COMMON:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000259 return true;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000260 }
Petr Hosekc1135772017-09-13 03:04:50 +0000261 if (Machine == EM_HEXAGON) {
262 switch (Index) {
263 case SHN_HEXAGON_SCOMMON:
264 case SHN_HEXAGON_SCOMMON_2:
265 case SHN_HEXAGON_SCOMMON_4:
266 case SHN_HEXAGON_SCOMMON_8:
267 return true;
268 }
269 }
270 return false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000271}
272
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000273// Large indexes force us to clarify exactly what this function should do. This
274// function should return the value that will appear in st_shndx when written
275// out.
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000276uint16_t Symbol::getShndx() const {
277 if (DefinedIn != nullptr) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000278 if (DefinedIn->Index >= SHN_LORESERVE)
279 return SHN_XINDEX;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000280 return DefinedIn->Index;
281 }
282 switch (ShndxType) {
283 // This means that we don't have a defined section but we do need to
284 // output a legitimate section index.
285 case SYMBOL_SIMPLE_INDEX:
286 return SHN_UNDEF;
287 case SYMBOL_ABS:
288 case SYMBOL_COMMON:
289 case SYMBOL_HEXAGON_SCOMMON:
290 case SYMBOL_HEXAGON_SCOMMON_2:
291 case SYMBOL_HEXAGON_SCOMMON_4:
292 case SYMBOL_HEXAGON_SCOMMON_8:
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000293 case SYMBOL_XINDEX:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000294 return static_cast<uint16_t>(ShndxType);
295 }
296 llvm_unreachable("Symbol with invalid ShndxType encountered");
297}
298
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000299void SymbolTableSection::assignIndices() {
300 uint32_t Index = 0;
301 for (auto &Sym : Symbols)
302 Sym->Index = Index++;
303}
304
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000305void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
Petr Hosek79cee9e2017-08-29 02:12:03 +0000306 SectionBase *DefinedIn, uint64_t Value,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000307 uint8_t Visibility, uint16_t Shndx,
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000308 uint64_t Size) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000309 Symbol Sym;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000310 Sym.Name = Name.str();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000311 Sym.Binding = Bind;
312 Sym.Type = Type;
313 Sym.DefinedIn = DefinedIn;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000314 if (DefinedIn != nullptr)
315 DefinedIn->HasSymbol = true;
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000316 if (DefinedIn == nullptr) {
317 if (Shndx >= SHN_LORESERVE)
318 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
319 else
320 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
321 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000322 Sym.Value = Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000323 Sym.Visibility = Visibility;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000324 Sym.Size = Size;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000325 Sym.Index = Symbols.size();
326 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
327 Size += this->EntrySize;
328}
329
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000330void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000331 if (SectionIndexTable == Sec)
332 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000333 if (SymbolNames == Sec) {
334 error("String table " + SymbolNames->Name +
335 " cannot be removed because it is referenced by the symbol table " +
336 this->Name);
337 }
Paul Semel41695f82018-05-02 20:19:22 +0000338 removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000339}
340
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000341void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
Paul Semel46201fb2018-06-01 16:19:46 +0000342 std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
343 [Callable](SymPtr &Sym) { Callable(*Sym); });
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000344 std::stable_partition(
345 std::begin(Symbols), std::end(Symbols),
346 [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000347 assignIndices();
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000348}
349
Paul Semel4246a462018-05-09 21:36:54 +0000350void SymbolTableSection::removeSymbols(
351 function_ref<bool(const Symbol &)> ToRemove) {
Paul Semel41695f82018-05-02 20:19:22 +0000352 Symbols.erase(
Paul Semel46201fb2018-06-01 16:19:46 +0000353 std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
Paul Semel41695f82018-05-02 20:19:22 +0000354 [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
355 std::end(Symbols));
356 Size = Symbols.size() * EntrySize;
357 assignIndices();
358}
359
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000360void SymbolTableSection::initialize(SectionTableRef SecTable) {
361 Size = 0;
362 setStrTab(SecTable.getSectionOfType<StringTableSection>(
363 Link,
364 "Symbol table has link index of " + Twine(Link) +
365 " which is not a valid index",
366 "Symbol table has link index of " + Twine(Link) +
367 " which is not a string table"));
368}
369
Petr Hosek79cee9e2017-08-29 02:12:03 +0000370void SymbolTableSection::finalize() {
371 // Make sure SymbolNames is finalized before getting name indexes.
372 SymbolNames->finalize();
373
374 uint32_t MaxLocalIndex = 0;
375 for (auto &Sym : Symbols) {
376 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
377 if (Sym->Binding == STB_LOCAL)
378 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
379 }
380 // Now we need to set the Link and Info fields.
381 Link = SymbolNames->Index;
382 Info = MaxLocalIndex + 1;
383}
384
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000385void SymbolTableSection::prepareForLayout() {
386 // Add all potential section indexes before file layout so that the section
387 // index section has the approprite size.
388 if (SectionIndexTable != nullptr) {
389 for (const auto &Sym : Symbols) {
390 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
391 SectionIndexTable->addIndex(Sym->DefinedIn->Index);
392 else
393 SectionIndexTable->addIndex(SHN_UNDEF);
394 }
395 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000396 // Add all of our strings to SymbolNames so that SymbolNames has the right
397 // size before layout is decided.
398 for (auto &Sym : Symbols)
399 SymbolNames->addString(Sym->Name);
400}
401
402const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
403 if (Symbols.size() <= Index)
404 error("Invalid symbol index: " + Twine(Index));
405 return Symbols[Index].get();
406}
407
Paul Semel99dda0b2018-05-25 11:01:25 +0000408Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
409 return const_cast<Symbol *>(
410 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
411}
412
Petr Hosek79cee9e2017-08-29 02:12:03 +0000413template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000414void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000415 uint8_t *Buf = Out.getBufferStart();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000416 Buf += Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000417 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000418 // Loop though symbols setting each entry of the symbol table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000419 for (auto &Symbol : Sec.Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000420 Sym->st_name = Symbol->NameIndex;
421 Sym->st_value = Symbol->Value;
422 Sym->st_size = Symbol->Size;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000423 Sym->st_other = Symbol->Visibility;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000424 Sym->setBinding(Symbol->Binding);
425 Sym->setType(Symbol->Type);
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000426 Sym->st_shndx = Symbol->getShndx();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000427 ++Sym;
428 }
429}
430
Jake Ehrlich76e91102018-01-25 22:46:17 +0000431void SymbolTableSection::accept(SectionVisitor &Visitor) const {
432 Visitor.visit(*this);
433}
434
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000435template <class SymTabType>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000436void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
437 const SectionBase *Sec) {
438 if (Symbols == Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000439 error("Symbol table " + Symbols->Name +
440 " cannot be removed because it is "
441 "referenced by the relocation "
442 "section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000443 this->Name);
444 }
445}
446
447template <class SymTabType>
448void RelocSectionWithSymtabBase<SymTabType>::initialize(
449 SectionTableRef SecTable) {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000450 if (Link != SHN_UNDEF)
451 setSymTab(SecTable.getSectionOfType<SymTabType>(
452 Link,
453 "Link field value " + Twine(Link) + " in section " + Name +
454 " is invalid",
455 "Link field value " + Twine(Link) + " in section " + Name +
456 " is not a symbol table"));
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000457
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000458 if (Info != SHN_UNDEF)
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000459 setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
460 " in section " + Name +
461 " is invalid"));
James Y Knight2ea995a2017-09-26 22:44:01 +0000462 else
463 setSection(nullptr);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000464}
465
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000466template <class SymTabType>
467void RelocSectionWithSymtabBase<SymTabType>::finalize() {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000468 this->Link = Symbols ? Symbols->Index : 0;
469
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000470 if (SecToApplyRel != nullptr)
471 this->Info = SecToApplyRel->Index;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000472}
473
474template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000475static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
Petr Hosekd7df9b22017-09-06 23:41:02 +0000476
477template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000478static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000479 Rela.r_addend = Addend;
480}
481
Jake Ehrlich76e91102018-01-25 22:46:17 +0000482template <class RelRange, class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000483static void writeRel(const RelRange &Relocations, T *Buf) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000484 for (const auto &Reloc : Relocations) {
485 Buf->r_offset = Reloc.Offset;
486 setAddend(*Buf, Reloc.Addend);
487 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
488 ++Buf;
489 }
490}
491
492template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000493void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
494 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
495 if (Sec.Type == SHT_REL)
496 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000497 else
Jake Ehrlich76e91102018-01-25 22:46:17 +0000498 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000499}
500
Jake Ehrlich76e91102018-01-25 22:46:17 +0000501void RelocationSection::accept(SectionVisitor &Visitor) const {
502 Visitor.visit(*this);
503}
504
Paul Semel4246a462018-05-09 21:36:54 +0000505void RelocationSection::removeSymbols(
506 function_ref<bool(const Symbol &)> ToRemove) {
507 for (const Relocation &Reloc : Relocations)
508 if (ToRemove(*Reloc.RelocSymbol))
Jordan Rupprecht88ed5e52018-08-09 22:52:03 +0000509 error("not stripping symbol '" + Reloc.RelocSymbol->Name +
Paul Semel4246a462018-05-09 21:36:54 +0000510 "' because it is named in a relocation");
511}
512
Paul Semel99dda0b2018-05-25 11:01:25 +0000513void RelocationSection::markSymbols() {
514 for (const Relocation &Reloc : Relocations)
515 Reloc.RelocSymbol->Referenced = true;
516}
517
Jake Ehrlich76e91102018-01-25 22:46:17 +0000518void SectionWriter::visit(const DynamicRelocationSection &Sec) {
519 std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
520 Out.getBufferStart() + Sec.Offset);
521}
522
523void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
524 Visitor.visit(*this);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000525}
526
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000527void Section::removeSectionReferences(const SectionBase *Sec) {
528 if (LinkSection == Sec) {
529 error("Section " + LinkSection->Name +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000530 " cannot be removed because it is "
531 "referenced by the section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000532 this->Name);
533 }
534}
535
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000536void GroupSection::finalize() {
537 this->Info = Sym->Index;
538 this->Link = SymTab->Index;
539}
540
Paul Semel4246a462018-05-09 21:36:54 +0000541void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
542 if (ToRemove(*Sym)) {
543 error("Symbol " + Sym->Name +
544 " cannot be removed because it is "
545 "referenced by the section " +
546 this->Name + "[" + Twine(this->Index) + "]");
547 }
548}
549
Paul Semel99dda0b2018-05-25 11:01:25 +0000550void GroupSection::markSymbols() {
551 if (Sym)
552 Sym->Referenced = true;
553}
554
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000555void Section::initialize(SectionTableRef SecTable) {
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000556 if (Link != ELF::SHN_UNDEF) {
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000557 LinkSection =
558 SecTable.getSection(Link, "Link field value " + Twine(Link) +
559 " in section " + Name + " is invalid");
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000560 if (LinkSection->Type == ELF::SHT_SYMTAB)
561 LinkSection = nullptr;
562 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000563}
564
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000565void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000566
Jake Ehrlich76e91102018-01-25 22:46:17 +0000567void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
Alexander Richardson6c859922018-02-19 19:53:44 +0000568 FileName = sys::path::filename(File);
569 // The format for the .gnu_debuglink starts with the file name and is
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000570 // followed by a null terminator and then the CRC32 of the file. The CRC32
571 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
572 // byte, and then finally push the size to alignment and add 4.
573 Size = alignTo(FileName.size() + 1, 4) + 4;
574 // The CRC32 will only be aligned if we align the whole section.
575 Align = 4;
576 Type = ELF::SHT_PROGBITS;
577 Name = ".gnu_debuglink";
578 // For sections not found in segments, OriginalOffset is only used to
579 // establish the order that sections should go in. By using the maximum
580 // possible offset we cause this section to wind up at the end.
581 OriginalOffset = std::numeric_limits<uint64_t>::max();
582 JamCRC crc;
583 crc.update(ArrayRef<char>(Data.data(), Data.size()));
584 // The CRC32 value needs to be complemented because the JamCRC dosn't
585 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
586 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
587 CRC32 = ~crc.getCRC();
588}
589
Jake Ehrlich76e91102018-01-25 22:46:17 +0000590GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000591 // Read in the file to compute the CRC of it.
592 auto DebugOrErr = MemoryBuffer::getFile(File);
593 if (!DebugOrErr)
594 error("'" + File + "': " + DebugOrErr.getError().message());
595 auto Debug = std::move(*DebugOrErr);
596 init(File, Debug->getBuffer());
597}
598
599template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000600void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
601 auto Buf = Out.getBufferStart() + Sec.Offset;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000602 char *File = reinterpret_cast<char *>(Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000603 Elf_Word *CRC =
604 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
605 *CRC = Sec.CRC32;
606 std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
607}
608
609void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
610 Visitor.visit(*this);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000611}
612
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000613template <class ELFT>
614void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
615 ELF::Elf32_Word *Buf =
616 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
617 *Buf++ = Sec.FlagWord;
618 for (const auto *S : Sec.GroupMembers)
619 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
620}
621
622void GroupSection::accept(SectionVisitor &Visitor) const {
623 Visitor.visit(*this);
624}
625
Petr Hosek05a04cb2017-08-01 00:33:58 +0000626// Returns true IFF a section is wholly inside the range of a segment
627static bool sectionWithinSegment(const SectionBase &Section,
628 const Segment &Segment) {
629 // If a section is empty it should be treated like it has a size of 1. This is
630 // to clarify the case when an empty section lies on a boundary between two
631 // segments and ensures that the section "belongs" to the second segment and
632 // not the first.
633 uint64_t SecSize = Section.Size ? Section.Size : 1;
634 return Segment.Offset <= Section.OriginalOffset &&
635 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
636}
637
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000638// Returns true IFF a segment's original offset is inside of another segment's
639// range.
640static bool segmentOverlapsSegment(const Segment &Child,
641 const Segment &Parent) {
642
643 return Parent.OriginalOffset <= Child.OriginalOffset &&
644 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
645}
646
Jake Ehrlich46814be2018-01-22 19:27:30 +0000647static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +0000648 // Any segment without a parent segment should come before a segment
649 // that has a parent segment.
650 if (A->OriginalOffset < B->OriginalOffset)
651 return true;
652 if (A->OriginalOffset > B->OriginalOffset)
653 return false;
654 return A->Index < B->Index;
655}
656
Jake Ehrlich46814be2018-01-22 19:27:30 +0000657static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
658 if (A->PAddr < B->PAddr)
659 return true;
660 if (A->PAddr > B->PAddr)
661 return false;
662 return A->Index < B->Index;
663}
664
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000665template <class ELFT> void BinaryELFBuilder<ELFT>::initFileHeader() {
666 Obj->Flags = 0x0;
667 Obj->Type = ET_REL;
668 Obj->Entry = 0x0;
669 Obj->Machine = EMachine;
670 Obj->Version = 1;
671}
672
673template <class ELFT> void BinaryELFBuilder<ELFT>::initHeaderSegment() {
674 Obj->ElfHdrSegment.Index = 0;
675}
676
677template <class ELFT> StringTableSection *BinaryELFBuilder<ELFT>::addStrTab() {
678 auto &StrTab = Obj->addSection<StringTableSection>();
679 StrTab.Name = ".strtab";
680
681 Obj->SectionNames = &StrTab;
682 return &StrTab;
683}
684
685template <class ELFT>
686SymbolTableSection *
687BinaryELFBuilder<ELFT>::addSymTab(StringTableSection *StrTab) {
688 auto &SymTab = Obj->addSection<SymbolTableSection>();
689
690 SymTab.Name = ".symtab";
691 SymTab.Link = StrTab->Index;
692 // TODO: Factor out dependence on ElfType here.
693 SymTab.EntrySize = sizeof(Elf_Sym);
694
695 // The symbol table always needs a null symbol
696 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
697
698 Obj->SymbolTable = &SymTab;
699 return &SymTab;
700}
701
702template <class ELFT>
703void BinaryELFBuilder<ELFT>::addData(SymbolTableSection *SymTab) {
704 auto Data = ArrayRef<uint8_t>(
705 reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
706 MemBuf->getBufferSize());
707 auto &DataSection = Obj->addSection<Section>(Data);
708 DataSection.Name = ".data";
709 DataSection.Type = ELF::SHT_PROGBITS;
710 DataSection.Size = Data.size();
711 DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
712
713 std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
714 std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
715 [](char c) { return !isalnum(c); }, '_');
716 Twine Prefix = Twine("_binary_") + SanitizedFilename;
717
718 SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
719 /*Value=*/0, STV_DEFAULT, 0, 0);
720 SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
721 /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
722 SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
723 /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
724}
725
726template <class ELFT> void BinaryELFBuilder<ELFT>::initSections() {
727 for (auto &Section : Obj->sections()) {
728 Section.initialize(Obj->sections());
729 }
730}
731
732template <class ELFT> std::unique_ptr<Object> BinaryELFBuilder<ELFT>::build() {
733 initFileHeader();
734 initHeaderSegment();
735 StringTableSection *StrTab = addStrTab();
736 SymbolTableSection *SymTab = addSymTab(StrTab);
737 initSections();
738 addData(SymTab);
739
740 return std::move(Obj);
741}
742
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000743template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
Jake Ehrlich6452b112018-02-14 23:31:33 +0000744 for (auto &Parent : Obj.segments()) {
745 // Every segment will overlap with itself but we don't want a segment to
746 // be it's own parent so we avoid that situation.
747 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
748 // We want a canonical "most parental" segment but this requires
749 // inspecting the ParentSegment.
750 if (compareSegmentsByOffset(&Parent, &Child))
751 if (Child.ParentSegment == nullptr ||
752 compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
753 Child.ParentSegment = &Parent;
754 }
755 }
756 }
757}
758
Jake Ehrlich76e91102018-01-25 22:46:17 +0000759template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000760 uint32_t Index = 0;
761 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000762 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
763 (size_t)Phdr.p_filesz};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000764 Segment &Seg = Obj.addSegment(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000765 Seg.Type = Phdr.p_type;
766 Seg.Flags = Phdr.p_flags;
Petr Hosek3f383832017-08-26 01:32:20 +0000767 Seg.OriginalOffset = Phdr.p_offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000768 Seg.Offset = Phdr.p_offset;
769 Seg.VAddr = Phdr.p_vaddr;
770 Seg.PAddr = Phdr.p_paddr;
771 Seg.FileSize = Phdr.p_filesz;
772 Seg.MemSize = Phdr.p_memsz;
773 Seg.Align = Phdr.p_align;
774 Seg.Index = Index++;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000775 for (auto &Section : Obj.sections()) {
776 if (sectionWithinSegment(Section, Seg)) {
777 Seg.addSection(&Section);
778 if (!Section.ParentSegment ||
779 Section.ParentSegment->Offset > Seg.Offset) {
780 Section.ParentSegment = &Seg;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000781 }
782 }
783 }
784 }
Jake Ehrlich6452b112018-02-14 23:31:33 +0000785
786 auto &ElfHdr = Obj.ElfHdrSegment;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000787 ElfHdr.Index = Index++;
788
789 const auto &Ehdr = *ElfFile.getHeader();
790 auto &PrHdr = Obj.ProgramHdrSegment;
791 PrHdr.Type = PT_PHDR;
792 PrHdr.Flags = 0;
793 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
794 // Whereas this works automatically for ElfHdr, here OriginalOffset is
795 // always non-zero and to ensure the equation we assign the same value to
796 // VAddr as well.
797 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
798 PrHdr.PAddr = 0;
799 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000800 // The spec requires us to naturally align all the fields.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000801 PrHdr.Align = sizeof(Elf_Addr);
802 PrHdr.Index = Index++;
803
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000804 // Now we do an O(n^2) loop through the segments in order to match up
805 // segments.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000806 for (auto &Child : Obj.segments())
807 setParentSegment(Child);
808 setParentSegment(ElfHdr);
809 setParentSegment(PrHdr);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000810}
811
812template <class ELFT>
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000813void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
814 auto SecTable = Obj.sections();
815 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
816 GroupSec->Link,
817 "Link field value " + Twine(GroupSec->Link) + " in section " +
818 GroupSec->Name + " is invalid",
819 "Link field value " + Twine(GroupSec->Link) + " in section " +
820 GroupSec->Name + " is not a symbol table");
821 auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
822 if (!Sym)
823 error("Info field value " + Twine(GroupSec->Info) + " in section " +
824 GroupSec->Name + " is not a valid symbol index");
825 GroupSec->setSymTab(SymTab);
826 GroupSec->setSymbol(Sym);
827 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
828 GroupSec->Contents.empty())
829 error("The content of the section " + GroupSec->Name + " is malformed");
830 const ELF::Elf32_Word *Word =
831 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
832 const ELF::Elf32_Word *End =
833 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
834 GroupSec->setFlagWord(*Word++);
835 for (; Word != End; ++Word) {
836 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
837 GroupSec->addMember(SecTable.getSection(
838 Index, "Group member index " + Twine(Index) + " in section " +
839 GroupSec->Name + " is invalid"));
840 }
841}
842
843template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000844void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000845 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
846 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000847 ArrayRef<Elf_Word> ShndxData;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000848
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000849 auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
850 for (const auto &Sym : Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000851 SectionBase *DefSection = nullptr;
852 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000853
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000854 if (Sym.st_shndx == SHN_XINDEX) {
855 if (SymTab->getShndxTable() == nullptr)
856 error("Symbol '" + Name +
857 "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
858 if (ShndxData.data() == nullptr) {
859 const Elf_Shdr &ShndxSec =
860 *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
861 ShndxData = unwrapOrError(
862 ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
863 if (ShndxData.size() != Symbols.size())
864 error("Symbol section index table does not have the same number of "
865 "entries as the symbol table.");
866 }
867 Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
868 DefSection = Obj.sections().getSection(
869 Index,
Puyan Lotfi97604b42018-08-02 18:16:52 +0000870 "Symbol '" + Name + "' has invalid section index " + Twine(Index));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000871 } else if (Sym.st_shndx >= SHN_LORESERVE) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000872 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000873 error(
874 "Symbol '" + Name +
875 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
876 Twine(Sym.st_shndx));
877 }
878 } else if (Sym.st_shndx != SHN_UNDEF) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000879 DefSection = Obj.sections().getSection(
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000880 Sym.st_shndx, "Symbol '" + Name +
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000881 "' is defined has invalid section index " +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000882 Twine(Sym.st_shndx));
Petr Hosek79cee9e2017-08-29 02:12:03 +0000883 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000884
Petr Hosek79cee9e2017-08-29 02:12:03 +0000885 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000886 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000887 }
888}
889
890template <class ELFT>
Petr Hosekd7df9b22017-09-06 23:41:02 +0000891static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
892
893template <class ELFT>
894static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
895 ToSet = Rela.r_addend;
896}
897
Jake Ehrlich76e91102018-01-25 22:46:17 +0000898template <class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000899static void initRelocations(RelocationSection *Relocs,
900 SymbolTableSection *SymbolTable, T RelRange) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000901 for (const auto &Rel : RelRange) {
902 Relocation ToAdd;
903 ToAdd.Offset = Rel.r_offset;
904 getAddend(ToAdd.Addend, Rel);
905 ToAdd.Type = Rel.getType(false);
Paul Semel31a212d2018-05-22 01:04:36 +0000906 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000907 Relocs->addRelocation(ToAdd);
908 }
909}
910
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000911SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000912 if (Index == SHN_UNDEF || Index > Sections.size())
913 error(ErrMsg);
914 return Sections[Index - 1].get();
915}
916
917template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000918T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
Zachary Turner41a9ee92017-10-11 23:54:34 +0000919 Twine TypeErrMsg) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +0000920 if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000921 return Sec;
922 error(TypeErrMsg);
923}
924
Petr Hosekd7df9b22017-09-06 23:41:02 +0000925template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000926SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000927 ArrayRef<uint8_t> Data;
928 switch (Shdr.sh_type) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000929 case SHT_REL:
930 case SHT_RELA:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000931 if (Shdr.sh_flags & SHF_ALLOC) {
932 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000933 return Obj.addSection<DynamicRelocationSection>(Data);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000934 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000935 return Obj.addSection<RelocationSection>();
Petr Hosek05a04cb2017-08-01 00:33:58 +0000936 case SHT_STRTAB:
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000937 // If a string table is allocated we don't want to mess with it. That would
938 // mean altering the memory image. There are no special link types or
939 // anything so we can just use a Section.
940 if (Shdr.sh_flags & SHF_ALLOC) {
941 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000942 return Obj.addSection<Section>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000943 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000944 return Obj.addSection<StringTableSection>();
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000945 case SHT_HASH:
946 case SHT_GNU_HASH:
947 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
948 // Because of this we don't need to mess with the hash tables either.
949 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000950 return Obj.addSection<Section>(Data);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000951 case SHT_GROUP:
952 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
953 return Obj.addSection<GroupSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000954 case SHT_DYNSYM:
955 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000956 return Obj.addSection<DynamicSymbolTableSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000957 case SHT_DYNAMIC:
958 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000959 return Obj.addSection<DynamicSection>(Data);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000960 case SHT_SYMTAB: {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000961 auto &SymTab = Obj.addSection<SymbolTableSection>();
962 Obj.SymbolTable = &SymTab;
963 return SymTab;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000964 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000965 case SHT_SYMTAB_SHNDX: {
966 auto &ShndxSection = Obj.addSection<SectionIndexSection>();
967 Obj.SectionIndexTable = &ShndxSection;
968 return ShndxSection;
969 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000970 case SHT_NOBITS:
Jake Ehrlich76e91102018-01-25 22:46:17 +0000971 return Obj.addSection<Section>(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000972 default:
973 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +0000974 return Obj.addSection<Section>(Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +0000975 }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000976}
977
Jake Ehrlich76e91102018-01-25 22:46:17 +0000978template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000979 uint32_t Index = 0;
980 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
981 if (Index == 0) {
982 ++Index;
983 continue;
984 }
Jake Ehrlich76e91102018-01-25 22:46:17 +0000985 auto &Sec = makeSection(Shdr);
986 Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
987 Sec.Type = Shdr.sh_type;
988 Sec.Flags = Shdr.sh_flags;
989 Sec.Addr = Shdr.sh_addr;
990 Sec.Offset = Shdr.sh_offset;
991 Sec.OriginalOffset = Shdr.sh_offset;
992 Sec.Size = Shdr.sh_size;
993 Sec.Link = Shdr.sh_link;
994 Sec.Info = Shdr.sh_info;
995 Sec.Align = Shdr.sh_addralign;
996 Sec.EntrySize = Shdr.sh_entsize;
997 Sec.Index = Index++;
Paul Semela42dec72018-08-09 17:05:21 +0000998 Sec.OriginalData =
999 ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
1000 (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001001 }
Petr Hosek79cee9e2017-08-29 02:12:03 +00001002
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001003 // If a section index table exists we'll need to initialize it before we
1004 // initialize the symbol table because the symbol table might need to
1005 // reference it.
1006 if (Obj.SectionIndexTable)
1007 Obj.SectionIndexTable->initialize(Obj.sections());
1008
Petr Hosek79cee9e2017-08-29 02:12:03 +00001009 // Now that all of the sections have been added we can fill out some extra
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001010 // details about symbol tables. We need the symbol table filled out before
1011 // any relocations.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001012 if (Obj.SymbolTable) {
1013 Obj.SymbolTable->initialize(Obj.sections());
1014 initSymbolTable(Obj.SymbolTable);
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001015 }
Petr Hosekd7df9b22017-09-06 23:41:02 +00001016
1017 // Now that all sections and symbols have been added we can add
1018 // relocations that reference symbols and set the link and info fields for
1019 // relocation sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001020 for (auto &Section : Obj.sections()) {
1021 if (&Section == Obj.SymbolTable)
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001022 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001023 Section.initialize(Obj.sections());
1024 if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001025 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
1026 if (RelSec->Type == SHT_REL)
Jake Ehrlich76e91102018-01-25 22:46:17 +00001027 initRelocations(RelSec, Obj.SymbolTable,
1028 unwrapOrError(ElfFile.rels(Shdr)));
Petr Hosekd7df9b22017-09-06 23:41:02 +00001029 else
Jake Ehrlich76e91102018-01-25 22:46:17 +00001030 initRelocations(RelSec, Obj.SymbolTable,
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001031 unwrapOrError(ElfFile.relas(Shdr)));
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001032 } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
1033 initGroupSection(GroupSec);
Petr Hosekd7df9b22017-09-06 23:41:02 +00001034 }
1035 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001036}
1037
Jake Ehrlich76e91102018-01-25 22:46:17 +00001038template <class ELFT> void ELFBuilder<ELFT>::build() {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001039 const auto &Ehdr = *ElfFile.getHeader();
1040
Jake Ehrlich76e91102018-01-25 22:46:17 +00001041 Obj.Type = Ehdr.e_type;
1042 Obj.Machine = Ehdr.e_machine;
1043 Obj.Version = Ehdr.e_version;
1044 Obj.Entry = Ehdr.e_entry;
1045 Obj.Flags = Ehdr.e_flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001046
Jake Ehrlich76e91102018-01-25 22:46:17 +00001047 readSectionHeaders();
1048 readProgramHeaders();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001049
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001050 uint32_t ShstrIndex = Ehdr.e_shstrndx;
1051 if (ShstrIndex == SHN_XINDEX)
1052 ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
1053
Jake Ehrlich76e91102018-01-25 22:46:17 +00001054 Obj.SectionNames =
1055 Obj.sections().template getSectionOfType<StringTableSection>(
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001056 ShstrIndex,
Jake Ehrlich8b831c12018-03-07 20:33:02 +00001057 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +00001058 " in elf header " + " is invalid",
Jake Ehrlich8b831c12018-03-07 20:33:02 +00001059 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +00001060 " in elf header " + " is not a string table");
Petr Hosek05a04cb2017-08-01 00:33:58 +00001061}
1062
Jake Ehrlich76e91102018-01-25 22:46:17 +00001063// A generic size function which computes sizes of any random access range.
1064template <class R> size_t size(R &&Range) {
1065 return static_cast<size_t>(std::end(Range) - std::begin(Range));
1066}
1067
1068Writer::~Writer() {}
1069
1070Reader::~Reader() {}
1071
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001072std::unique_ptr<Object> BinaryReader::create() const {
1073 if (MInfo.Is64Bit)
1074 return MInfo.IsLittleEndian
1075 ? BinaryELFBuilder<ELF64LE>(MInfo.EMachine, MemBuf).build()
1076 : BinaryELFBuilder<ELF64BE>(MInfo.EMachine, MemBuf).build();
1077 else
1078 return MInfo.IsLittleEndian
1079 ? BinaryELFBuilder<ELF32LE>(MInfo.EMachine, MemBuf).build()
1080 : BinaryELFBuilder<ELF32BE>(MInfo.EMachine, MemBuf).build();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001081}
1082
1083std::unique_ptr<Object> ELFReader::create() const {
Alexander Shaposhnikov58cb1972018-06-07 19:41:42 +00001084 auto Obj = llvm::make_unique<Object>();
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001085 if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001086 ELFBuilder<ELF32LE> Builder(*o, *Obj);
1087 Builder.build();
1088 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001089 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001090 ELFBuilder<ELF64LE> Builder(*o, *Obj);
1091 Builder.build();
1092 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001093 } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001094 ELFBuilder<ELF32BE> Builder(*o, *Obj);
1095 Builder.build();
1096 return Obj;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001097 } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001098 ELFBuilder<ELF64BE> Builder(*o, *Obj);
1099 Builder.build();
1100 return Obj;
1101 }
1102 error("Invalid file type");
1103}
1104
1105template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001106 uint8_t *B = Buf.getBufferStart();
1107 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001108 std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
1109 Ehdr.e_ident[EI_MAG0] = 0x7f;
1110 Ehdr.e_ident[EI_MAG1] = 'E';
1111 Ehdr.e_ident[EI_MAG2] = 'L';
1112 Ehdr.e_ident[EI_MAG3] = 'F';
1113 Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1114 Ehdr.e_ident[EI_DATA] =
1115 ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
1116 Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
1117 Ehdr.e_ident[EI_OSABI] = ELFOSABI_NONE;
1118 Ehdr.e_ident[EI_ABIVERSION] = 0;
1119
Jake Ehrlich76e91102018-01-25 22:46:17 +00001120 Ehdr.e_type = Obj.Type;
1121 Ehdr.e_machine = Obj.Machine;
1122 Ehdr.e_version = Obj.Version;
1123 Ehdr.e_entry = Obj.Entry;
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001124 // TODO: Only set phoff when a program header exists, to avoid tools
1125 // thinking this is corrupt data.
Jake Ehrlich6452b112018-02-14 23:31:33 +00001126 Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001127 Ehdr.e_flags = Obj.Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001128 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
1129 Ehdr.e_phentsize = sizeof(Elf_Phdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001130 Ehdr.e_phnum = size(Obj.segments());
Petr Hosek05a04cb2017-08-01 00:33:58 +00001131 Ehdr.e_shentsize = sizeof(Elf_Shdr);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001132 if (WriteSectionHeaders) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001133 Ehdr.e_shoff = Obj.SHOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001134 // """
1135 // If the number of sections is greater than or equal to
1136 // SHN_LORESERVE (0xff00), this member has the value zero and the actual
1137 // number of section header table entries is contained in the sh_size field
1138 // of the section header at index 0.
1139 // """
1140 auto Shnum = size(Obj.sections()) + 1;
1141 if (Shnum >= SHN_LORESERVE)
1142 Ehdr.e_shnum = 0;
1143 else
1144 Ehdr.e_shnum = Shnum;
1145 // """
1146 // If the section name string table section index is greater than or equal
1147 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
1148 // and the actual index of the section name string table section is
1149 // contained in the sh_link field of the section header at index 0.
1150 // """
1151 if (Obj.SectionNames->Index >= SHN_LORESERVE)
1152 Ehdr.e_shstrndx = SHN_XINDEX;
1153 else
1154 Ehdr.e_shstrndx = Obj.SectionNames->Index;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001155 } else {
1156 Ehdr.e_shoff = 0;
1157 Ehdr.e_shnum = 0;
1158 Ehdr.e_shstrndx = 0;
1159 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001160}
1161
Jake Ehrlich76e91102018-01-25 22:46:17 +00001162template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1163 for (auto &Seg : Obj.segments())
1164 writePhdr(Seg);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001165}
1166
Jake Ehrlich76e91102018-01-25 22:46:17 +00001167template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001168 uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001169 // This reference serves to write the dummy section header at the begining
Jake Ehrlich425ec9f2017-09-15 22:04:09 +00001170 // of the file. It is not used for anything else
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001171 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001172 Shdr.sh_name = 0;
1173 Shdr.sh_type = SHT_NULL;
1174 Shdr.sh_flags = 0;
1175 Shdr.sh_addr = 0;
1176 Shdr.sh_offset = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001177 // See writeEhdr for why we do this.
1178 uint64_t Shnum = size(Obj.sections()) + 1;
1179 if (Shnum >= SHN_LORESERVE)
1180 Shdr.sh_size = Shnum;
1181 else
1182 Shdr.sh_size = 0;
1183 // See writeEhdr for why we do this.
1184 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1185 Shdr.sh_link = Obj.SectionNames->Index;
1186 else
1187 Shdr.sh_link = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001188 Shdr.sh_info = 0;
1189 Shdr.sh_addralign = 0;
1190 Shdr.sh_entsize = 0;
1191
Jake Ehrlich76e91102018-01-25 22:46:17 +00001192 for (auto &Sec : Obj.sections())
1193 writeShdr(Sec);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001194}
1195
Jake Ehrlich76e91102018-01-25 22:46:17 +00001196template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1197 for (auto &Sec : Obj.sections())
1198 Sec.accept(*SecWriter);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001199}
1200
Jake Ehrlich76e91102018-01-25 22:46:17 +00001201void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001202
1203 auto Iter = std::stable_partition(
1204 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1205 if (ToRemove(*Sec))
1206 return false;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001207 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1208 if (auto ToRelSec = RelSec->getSection())
1209 return !ToRemove(*ToRelSec);
1210 }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001211 return true;
1212 });
1213 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1214 SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001215 if (SectionNames != nullptr && ToRemove(*SectionNames))
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001216 SectionNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001217 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1218 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001219 // Now make sure there are no remaining references to the sections that will
1220 // be removed. Sometimes it is impossible to remove a reference so we emit
1221 // an error here instead.
1222 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1223 for (auto &Segment : Segments)
1224 Segment->removeSection(RemoveSec.get());
1225 for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1226 KeepSec->removeSectionReferences(RemoveSec.get());
1227 }
1228 // Now finally get rid of them all togethor.
1229 Sections.erase(Iter, std::end(Sections));
1230}
1231
Paul Semel4246a462018-05-09 21:36:54 +00001232void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1233 if (!SymbolTable)
1234 return;
1235
1236 for (const SecPtr &Sec : Sections)
1237 Sec->removeSymbols(ToRemove);
1238}
1239
Jake Ehrlich76e91102018-01-25 22:46:17 +00001240void Object::sortSections() {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001241 // Put all sections in offset order. Maintain the ordering as closely as
1242 // possible while meeting that demand however.
1243 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1244 return A->OriginalOffset < B->OriginalOffset;
1245 };
1246 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1247 CompareSections);
1248}
1249
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001250static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1251 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1252 if (Align == 0)
1253 Align = 1;
1254 auto Diff =
1255 static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1256 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1257 // (Offset + Diff) & -Align == Addr & -Align will still hold.
1258 if (Diff < 0)
1259 Diff += Align;
1260 return Offset + Diff;
1261}
1262
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001263// Orders segments such that if x = y->ParentSegment then y comes before x.
1264static void OrderSegments(std::vector<Segment *> &Segments) {
Jake Ehrlich46814be2018-01-22 19:27:30 +00001265 std::stable_sort(std::begin(Segments), std::end(Segments),
1266 compareSegmentsByOffset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001267}
1268
1269// This function finds a consistent layout for a list of segments starting from
1270// an Offset. It assumes that Segments have been sorted by OrderSegments and
1271// returns an Offset one past the end of the last segment.
1272static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1273 uint64_t Offset) {
1274 assert(std::is_sorted(std::begin(Segments), std::end(Segments),
Jake Ehrlich46814be2018-01-22 19:27:30 +00001275 compareSegmentsByOffset));
Petr Hosek3f383832017-08-26 01:32:20 +00001276 // The only way a segment should move is if a section was between two
1277 // segments and that section was removed. If that section isn't in a segment
1278 // then it's acceptable, but not ideal, to simply move it to after the
1279 // segments. So we can simply layout segments one after the other accounting
1280 // for alignment.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001281 for (auto &Segment : Segments) {
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001282 // We assume that segments have been ordered by OriginalOffset and Index
1283 // such that a parent segment will always come before a child segment in
1284 // OrderedSegments. This means that the Offset of the ParentSegment should
1285 // already be set and we can set our offset relative to it.
1286 if (Segment->ParentSegment != nullptr) {
1287 auto Parent = Segment->ParentSegment;
1288 Segment->Offset =
1289 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1290 } else {
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001291 Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001292 Segment->Offset = Offset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001293 }
Jake Ehrlich084400b2017-10-04 17:44:42 +00001294 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
Petr Hosek3f383832017-08-26 01:32:20 +00001295 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001296 return Offset;
1297}
1298
1299// This function finds a consistent layout for a list of sections. It assumes
1300// that the ->ParentSegment of each section has already been laid out. The
1301// supplied starting Offset is used for the starting offset of any section that
1302// does not have a ParentSegment. It returns either the offset given if all
1303// sections had a ParentSegment or an offset one past the last section if there
1304// was a section that didn't have a ParentSegment.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001305template <class Range>
1306static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
Petr Hosek3f383832017-08-26 01:32:20 +00001307 // Now the offset of every segment has been set we can assign the offsets
1308 // of each section. For sections that are covered by a segment we should use
1309 // the segment's original offset and the section's original offset to compute
1310 // the offset from the start of the segment. Using the offset from the start
1311 // of the segment we can assign a new offset to the section. For sections not
1312 // covered by segments we can just bump Offset to the next valid location.
1313 uint32_t Index = 1;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001314 for (auto &Section : Sections) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001315 Section.Index = Index++;
1316 if (Section.ParentSegment != nullptr) {
1317 auto Segment = *Section.ParentSegment;
1318 Section.Offset =
1319 Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
Petr Hosek3f383832017-08-26 01:32:20 +00001320 } else {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001321 Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1322 Section.Offset = Offset;
1323 if (Section.Type != SHT_NOBITS)
1324 Offset += Section.Size;
Petr Hosek3f383832017-08-26 01:32:20 +00001325 }
1326 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001327 return Offset;
1328}
Petr Hosek3f383832017-08-26 01:32:20 +00001329
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001330template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
1331 auto &ElfHdr = Obj.ElfHdrSegment;
1332 ElfHdr.Type = PT_PHDR;
1333 ElfHdr.Flags = 0;
1334 ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
1335 ElfHdr.VAddr = 0;
1336 ElfHdr.PAddr = 0;
1337 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
1338 ElfHdr.Align = 0;
1339}
1340
Jake Ehrlich76e91102018-01-25 22:46:17 +00001341template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001342 // We need a temporary list of segments that has a special order to it
1343 // so that we know that anytime ->ParentSegment is set that segment has
1344 // already had its offset properly set.
1345 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001346 for (auto &Segment : Obj.segments())
1347 OrderedSegments.push_back(&Segment);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001348 OrderedSegments.push_back(&Obj.ElfHdrSegment);
1349 OrderedSegments.push_back(&Obj.ProgramHdrSegment);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001350 OrderSegments(OrderedSegments);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001351 // Offset is used as the start offset of the first segment to be laid out.
1352 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1353 // we start at offset 0.
1354 uint64_t Offset = 0;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001355 Offset = LayoutSegments(OrderedSegments, Offset);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001356 Offset = LayoutSections(Obj.sections(), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001357 // If we need to write the section header table out then we need to align the
1358 // Offset so that SHOffset is valid.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001359 if (WriteSectionHeaders)
Jordan Rupprechtde965ea2018-08-10 16:25:58 +00001360 Offset = alignTo(Offset, sizeof(Elf_Addr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001361 Obj.SHOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001362}
1363
Jake Ehrlich76e91102018-01-25 22:46:17 +00001364template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001365 // We already have the section header offset so we can calculate the total
1366 // size by just adding up the size of each section header.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001367 auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1368 return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001369 NullSectionSize;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001370}
1371
Jake Ehrlich76e91102018-01-25 22:46:17 +00001372template <class ELFT> void ELFWriter<ELFT>::write() {
1373 writeEhdr();
1374 writePhdrs();
1375 writeSectionData();
1376 if (WriteSectionHeaders)
1377 writeShdrs();
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001378 if (auto E = Buf.commit())
1379 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001380}
1381
1382template <class ELFT> void ELFWriter<ELFT>::finalize() {
1383 // It could happen that SectionNames has been removed and yet the user wants
1384 // a section header table output. We need to throw an error if a user tries
1385 // to do that.
1386 if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1387 error("Cannot write section header table because section header string "
1388 "table was removed.");
1389
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001390 Obj.sortSections();
1391
1392 // We need to assign indexes before we perform layout because we need to know
1393 // if we need large indexes or not. We can assign indexes first and check as
1394 // we go to see if we will actully need large indexes.
1395 bool NeedsLargeIndexes = false;
1396 if (size(Obj.sections()) >= SHN_LORESERVE) {
1397 auto Sections = Obj.sections();
1398 NeedsLargeIndexes =
1399 std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1400 [](const SectionBase &Sec) { return Sec.HasSymbol; });
1401 // TODO: handle case where only one section needs the large index table but
1402 // only needs it because the large index table hasn't been removed yet.
1403 }
1404
1405 if (NeedsLargeIndexes) {
1406 // This means we definitely need to have a section index table but if we
1407 // already have one then we should use it instead of making a new one.
1408 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1409 // Addition of a section to the end does not invalidate the indexes of
1410 // other sections and assigns the correct index to the new section.
1411 auto &Shndx = Obj.addSection<SectionIndexSection>();
1412 Obj.SymbolTable->setShndxTable(&Shndx);
1413 Shndx.setSymTab(Obj.SymbolTable);
1414 }
1415 } else {
1416 // Since we don't need SectionIndexTable we should remove it and all
1417 // references to it.
1418 if (Obj.SectionIndexTable != nullptr) {
1419 Obj.removeSections([this](const SectionBase &Sec) {
1420 return &Sec == Obj.SectionIndexTable;
1421 });
1422 }
1423 }
1424
1425 // Make sure we add the names of all the sections. Importantly this must be
1426 // done after we decide to add or remove SectionIndexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001427 if (Obj.SectionNames != nullptr)
1428 for (const auto &Section : Obj.sections()) {
1429 Obj.SectionNames->addString(Section.Name);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001430 }
Jake Ehrlich0a151bd2018-03-07 19:59:15 +00001431
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001432 initEhdrSegment();
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001433 // Before we can prepare for layout the indexes need to be finalized.
1434 uint64_t Index = 0;
1435 for (auto &Sec : Obj.sections())
1436 Sec.Index = Index++;
1437
1438 // The symbol table does not update all other sections on update. For
1439 // instance, symbol names are not added as new symbols are added. This means
1440 // that some sections, like .strtab, don't yet have their final size.
1441 if (Obj.SymbolTable != nullptr)
1442 Obj.SymbolTable->prepareForLayout();
1443
Petr Hosekc4df10e2017-08-04 21:09:26 +00001444 assignOffsets();
1445
1446 // Finalize SectionNames first so that we can assign name indexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001447 if (Obj.SectionNames != nullptr)
1448 Obj.SectionNames->finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001449 // Finally now that all offsets and indexes have been set we can finalize any
1450 // remaining issues.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001451 uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1452 for (auto &Section : Obj.sections()) {
1453 Section.HeaderOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001454 Offset += sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001455 if (WriteSectionHeaders)
1456 Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1457 Section.finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001458 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001459
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001460 Buf.allocate(totalSize());
1461 SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001462}
1463
Jake Ehrlich76e91102018-01-25 22:46:17 +00001464void BinaryWriter::write() {
1465 for (auto &Section : Obj.sections()) {
1466 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001467 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001468 Section.accept(*SecWriter);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001469 }
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001470 if (auto E = Buf.commit())
1471 reportError(Buf.getName(), errorToErrorCode(std::move(E)));
Petr Hosekc4df10e2017-08-04 21:09:26 +00001472}
1473
Jake Ehrlich76e91102018-01-25 22:46:17 +00001474void BinaryWriter::finalize() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001475 // TODO: Create a filter range to construct OrderedSegments from so that this
1476 // code can be deduped with assignOffsets above. This should also solve the
1477 // todo below for LayoutSections.
1478 // We need a temporary list of segments that has a special order to it
1479 // so that we know that anytime ->ParentSegment is set that segment has
1480 // already had it's offset properly set. We only want to consider the segments
1481 // that will affect layout of allocated sections so we only add those.
1482 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001483 for (auto &Section : Obj.sections()) {
1484 if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1485 OrderedSegments.push_back(Section.ParentSegment);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001486 }
1487 }
Jake Ehrlich46814be2018-01-22 19:27:30 +00001488
1489 // For binary output, we're going to use physical addresses instead of
1490 // virtual addresses, since a binary output is used for cases like ROM
1491 // loading and physical addresses are intended for ROM loading.
1492 // However, if no segment has a physical address, we'll fallback to using
1493 // virtual addresses for all.
1494 if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments),
1495 [](const Segment *Segment) { return Segment->PAddr == 0; }))
1496 for (const auto &Segment : OrderedSegments)
1497 Segment->PAddr = Segment->VAddr;
1498
1499 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1500 compareSegmentsByPAddr);
1501
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001502 // Because we add a ParentSegment for each section we might have duplicate
1503 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1504 // would do very strange things.
1505 auto End =
1506 std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1507 OrderedSegments.erase(End, std::end(OrderedSegments));
1508
Jake Ehrlich46814be2018-01-22 19:27:30 +00001509 uint64_t Offset = 0;
1510
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001511 // Modify the first segment so that there is no gap at the start. This allows
1512 // our layout algorithm to proceed as expected while not out writing out the
1513 // gap at the start.
1514 if (!OrderedSegments.empty()) {
1515 auto Seg = OrderedSegments[0];
1516 auto Sec = Seg->firstSection();
1517 auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1518 Seg->OriginalOffset += Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001519 // The size needs to be shrunk as well.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001520 Seg->FileSize -= Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001521 // The PAddr needs to be increased to remove the gap before the first
1522 // section.
1523 Seg->PAddr += Diff;
1524 uint64_t LowestPAddr = Seg->PAddr;
1525 for (auto &Segment : OrderedSegments) {
1526 Segment->Offset = Segment->PAddr - LowestPAddr;
1527 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1528 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001529 }
1530
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001531 // TODO: generalize LayoutSections to take a range. Pass a special range
1532 // constructed from an iterator that skips values for which a predicate does
1533 // not hold. Then pass such a range to LayoutSections instead of constructing
1534 // AllocatedSections here.
1535 std::vector<SectionBase *> AllocatedSections;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001536 for (auto &Section : Obj.sections()) {
1537 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001538 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001539 AllocatedSections.push_back(&Section);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001540 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001541 LayoutSections(make_pointee_range(AllocatedSections), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001542
1543 // Now that every section has been laid out we just need to compute the total
1544 // file size. This might not be the same as the offset returned by
1545 // LayoutSections, because we want to truncate the last segment to the end of
1546 // its last section, to match GNU objcopy's behaviour.
1547 TotalSize = 0;
1548 for (const auto &Section : AllocatedSections) {
1549 if (Section->Type != SHT_NOBITS)
1550 TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1551 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001552
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001553 Buf.allocate(TotalSize);
1554 SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001555}
1556
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001557namespace llvm {
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001558namespace objcopy {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001559
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001560template class BinaryELFBuilder<ELF64LE>;
1561template class BinaryELFBuilder<ELF64BE>;
1562template class BinaryELFBuilder<ELF32LE>;
1563template class BinaryELFBuilder<ELF32BE>;
1564
Jake Ehrlich76e91102018-01-25 22:46:17 +00001565template class ELFBuilder<ELF64LE>;
1566template class ELFBuilder<ELF64BE>;
1567template class ELFBuilder<ELF32LE>;
1568template class ELFBuilder<ELF32BE>;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001569
Jake Ehrlich76e91102018-01-25 22:46:17 +00001570template class ELFWriter<ELF64LE>;
1571template class ELFWriter<ELF64BE>;
1572template class ELFWriter<ELF32LE>;
1573template class ELFWriter<ELF32BE>;
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001574} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001575} // end namespace llvm