blob: ef5dc5d79513d6701ad7b2206e2920d302cb95e6 [file] [log] [blame]
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001//===- Object.cpp ---------------------------------------------------------===//
Petr Hosek05a04cb2017-08-01 00:33:58 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Petr Hosek05a04cb2017-08-01 00:33:58 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00008
Petr Hosek05a04cb2017-08-01 00:33:58 +00009#include "Object.h"
10#include "llvm-objcopy.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000011#include "llvm/ADT/ArrayRef.h"
12#include "llvm/ADT/STLExtras.h"
13#include "llvm/ADT/StringRef.h"
14#include "llvm/ADT/Twine.h"
15#include "llvm/ADT/iterator_range.h"
16#include "llvm/BinaryFormat/ELF.h"
Puyan Lotfi99124cc2018-09-07 08:10:22 +000017#include "llvm/MC/MCTargetOptions.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000018#include "llvm/Object/ELFObjectFile.h"
Puyan Lotfi99124cc2018-09-07 08:10:22 +000019#include "llvm/Support/Compression.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000020#include "llvm/Support/ErrorHandling.h"
21#include "llvm/Support/FileOutputBuffer.h"
Jake Ehrlichea07d3c2018-01-25 22:15:14 +000022#include "llvm/Support/Path.h"
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000023#include <algorithm>
24#include <cstddef>
25#include <cstdint>
26#include <iterator>
27#include <utility>
28#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000029
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000030namespace llvm {
31namespace objcopy {
32namespace elf {
33
Petr Hosek05a04cb2017-08-01 00:33:58 +000034using namespace object;
35using namespace ELF;
36
Jake Ehrlich76e91102018-01-25 22:46:17 +000037template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000038 uint8_t *B = Buf.getBufferStart();
39 B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
40 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000041 Phdr.p_type = Seg.Type;
42 Phdr.p_flags = Seg.Flags;
43 Phdr.p_offset = Seg.Offset;
44 Phdr.p_vaddr = Seg.VAddr;
45 Phdr.p_paddr = Seg.PAddr;
46 Phdr.p_filesz = Seg.FileSize;
47 Phdr.p_memsz = Seg.MemSize;
48 Phdr.p_align = Seg.Align;
Petr Hosekc4df10e2017-08-04 21:09:26 +000049}
50
Jake Ehrlich36a2eb32017-10-10 18:47:09 +000051void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
Paul Semel4246a462018-05-09 21:36:54 +000052void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
Jake Ehrlichf5a43772017-09-25 20:37:28 +000053void SectionBase::initialize(SectionTableRef SecTable) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000054void SectionBase::finalize() {}
Paul Semel99dda0b2018-05-25 11:01:25 +000055void SectionBase::markSymbols() {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000056
Jake Ehrlich76e91102018-01-25 22:46:17 +000057template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000058 uint8_t *B = Buf.getBufferStart();
59 B += Sec.HeaderOffset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +000060 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000061 Shdr.sh_name = Sec.NameIndex;
62 Shdr.sh_type = Sec.Type;
63 Shdr.sh_flags = Sec.Flags;
64 Shdr.sh_addr = Sec.Addr;
65 Shdr.sh_offset = Sec.Offset;
66 Shdr.sh_size = Sec.Size;
67 Shdr.sh_link = Sec.Link;
68 Shdr.sh_info = Sec.Info;
69 Shdr.sh_addralign = Sec.Align;
70 Shdr.sh_entsize = Sec.EntrySize;
Petr Hosek05a04cb2017-08-01 00:33:58 +000071}
72
Jordan Rupprecht1f821762019-01-03 17:45:30 +000073template <class ELFT> void ELFSectionSizer<ELFT>::visit(Section &Sec) {}
74
75template <class ELFT>
76void ELFSectionSizer<ELFT>::visit(OwnedDataSection &Sec) {}
77
78template <class ELFT>
79void ELFSectionSizer<ELFT>::visit(StringTableSection &Sec) {}
80
81template <class ELFT>
82void ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &Sec) {}
83
84template <class ELFT>
85void ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
86 Sec.EntrySize = sizeof(Elf_Sym);
87 Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
Jordan Rupprecht78213c7e2019-01-03 17:51:32 +000088 // Align to the largest field in Elf_Sym.
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +000089 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
Jordan Rupprecht1f821762019-01-03 17:45:30 +000090}
91
92template <class ELFT>
93void ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
94 Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
95 Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
Jordan Rupprecht78213c7e2019-01-03 17:51:32 +000096 // Align to the largest field in Elf_Rel(a).
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +000097 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
Jordan Rupprecht1f821762019-01-03 17:45:30 +000098}
99
100template <class ELFT>
101void ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &Sec) {}
102
103template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {}
104
105template <class ELFT>
106void ELFSectionSizer<ELFT>::visit(SectionIndexSection &Sec) {}
107
108template <class ELFT>
109void ELFSectionSizer<ELFT>::visit(CompressedSection &Sec) {}
110
111template <class ELFT>
112void ELFSectionSizer<ELFT>::visit(DecompressedSection &Sec) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000113
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000114void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
115 error("Cannot write symbol section index table '" + Sec.Name + "' ");
116}
117
Jake Ehrlich76e91102018-01-25 22:46:17 +0000118void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
119 error("Cannot write symbol table '" + Sec.Name + "' out to binary");
120}
121
122void BinarySectionWriter::visit(const RelocationSection &Sec) {
123 error("Cannot write relocation section '" + Sec.Name + "' out to binary");
124}
125
126void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000127 error("Cannot write '" + Sec.Name + "' out to binary");
128}
129
130void BinarySectionWriter::visit(const GroupSection &Sec) {
131 error("Cannot write '" + Sec.Name + "' out to binary");
Jake Ehrlich76e91102018-01-25 22:46:17 +0000132}
133
134void SectionWriter::visit(const Section &Sec) {
135 if (Sec.Type == SHT_NOBITS)
Petr Hosek05a04cb2017-08-01 00:33:58 +0000136 return;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000137 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Fangrui Song75709322018-11-17 01:44:25 +0000138 llvm::copy(Sec.Contents, Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000139}
140
Jake Ehrlich76e91102018-01-25 22:46:17 +0000141void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
142
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000143void Section::accept(MutableSectionVisitor &Visitor) { Visitor.visit(*this); }
144
Jake Ehrlich76e91102018-01-25 22:46:17 +0000145void SectionWriter::visit(const OwnedDataSection &Sec) {
146 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Fangrui Song75709322018-11-17 01:44:25 +0000147 llvm::copy(Sec.Data, Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000148}
149
Puyan Lotfiaf048642018-10-01 10:29:41 +0000150static const std::vector<uint8_t> ZlibGnuMagic = {'Z', 'L', 'I', 'B'};
151
152static bool isDataGnuCompressed(ArrayRef<uint8_t> Data) {
153 return Data.size() > ZlibGnuMagic.size() &&
154 std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data());
155}
156
157template <class ELFT>
158static std::tuple<uint64_t, uint64_t>
159getDecompressedSizeAndAlignment(ArrayRef<uint8_t> Data) {
160 const bool IsGnuDebug = isDataGnuCompressed(Data);
161 const uint64_t DecompressedSize =
162 IsGnuDebug
163 ? support::endian::read64be(reinterpret_cast<const uint64_t *>(
164 Data.data() + ZlibGnuMagic.size()))
165 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())->ch_size;
166 const uint64_t DecompressedAlign =
167 IsGnuDebug ? 1
168 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())
169 ->ch_addralign;
170
171 return std::make_tuple(DecompressedSize, DecompressedAlign);
172}
173
174template <class ELFT>
175void ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
176 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
177
178 if (!zlib::isAvailable()) {
179 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
180 return;
181 }
182
183 const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData)
184 ? (ZlibGnuMagic.size() + sizeof(Sec.Size))
185 : sizeof(Elf_Chdr_Impl<ELFT>);
186
187 StringRef CompressedContent(
188 reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset,
189 Sec.OriginalData.size() - DataOffset);
190
191 SmallVector<char, 128> DecompressedContent;
192 if (Error E = zlib::uncompress(CompressedContent, DecompressedContent,
193 static_cast<size_t>(Sec.Size)))
194 reportError(Sec.Name, std::move(E));
195
196 std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);
197}
198
199void BinarySectionWriter::visit(const DecompressedSection &Sec) {
200 error("Cannot write compressed section '" + Sec.Name + "' ");
201}
202
203void DecompressedSection::accept(SectionVisitor &Visitor) const {
204 Visitor.visit(*this);
205}
206
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000207void DecompressedSection::accept(MutableSectionVisitor &Visitor) {
208 Visitor.visit(*this);
209}
210
Jake Ehrlich76e91102018-01-25 22:46:17 +0000211void OwnedDataSection::accept(SectionVisitor &Visitor) const {
212 Visitor.visit(*this);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000213}
214
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000215void OwnedDataSection::accept(MutableSectionVisitor &Visitor) {
216 Visitor.visit(*this);
217}
218
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000219void BinarySectionWriter::visit(const CompressedSection &Sec) {
220 error("Cannot write compressed section '" + Sec.Name + "' ");
221}
222
223template <class ELFT>
224void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
225 uint8_t *Buf = Out.getBufferStart();
226 Buf += Sec.Offset;
227
228 if (Sec.CompressionType == DebugCompressionType::None) {
229 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
230 return;
231 }
232
233 if (Sec.CompressionType == DebugCompressionType::GNU) {
234 const char *Magic = "ZLIB";
235 memcpy(Buf, Magic, strlen(Magic));
236 Buf += strlen(Magic);
237 const uint64_t DecompressedSize =
238 support::endian::read64be(&Sec.DecompressedSize);
239 memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
240 Buf += sizeof(DecompressedSize);
241 } else {
242 Elf_Chdr_Impl<ELFT> Chdr;
243 Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
244 Chdr.ch_size = Sec.DecompressedSize;
245 Chdr.ch_addralign = Sec.DecompressedAlign;
246 memcpy(Buf, &Chdr, sizeof(Chdr));
247 Buf += sizeof(Chdr);
248 }
249
250 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
251}
252
253CompressedSection::CompressedSection(const SectionBase &Sec,
254 DebugCompressionType CompressionType)
255 : SectionBase(Sec), CompressionType(CompressionType),
256 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
257
258 if (!zlib::isAvailable()) {
259 CompressionType = DebugCompressionType::None;
260 return;
261 }
262
263 if (Error E = zlib::compress(
264 StringRef(reinterpret_cast<const char *>(OriginalData.data()),
265 OriginalData.size()),
266 CompressedData))
267 reportError(Name, std::move(E));
268
269 size_t ChdrSize;
270 if (CompressionType == DebugCompressionType::GNU) {
271 Name = ".z" + Sec.Name.substr(1);
272 ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
273 } else {
274 Flags |= ELF::SHF_COMPRESSED;
275 ChdrSize =
276 std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
277 sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
278 std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
279 sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
280 }
281 Size = ChdrSize + CompressedData.size();
282 Align = 8;
283}
284
Puyan Lotfiaf048642018-10-01 10:29:41 +0000285CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData,
286 uint64_t DecompressedSize,
287 uint64_t DecompressedAlign)
288 : CompressionType(DebugCompressionType::None),
289 DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) {
290 OriginalData = CompressedData;
291}
292
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000293void CompressedSection::accept(SectionVisitor &Visitor) const {
294 Visitor.visit(*this);
295}
296
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000297void CompressedSection::accept(MutableSectionVisitor &Visitor) {
298 Visitor.visit(*this);
299}
300
Petr Hosek05a04cb2017-08-01 00:33:58 +0000301void StringTableSection::addString(StringRef Name) {
302 StrTabBuilder.add(Name);
303 Size = StrTabBuilder.getSize();
304}
305
306uint32_t StringTableSection::findIndex(StringRef Name) const {
307 return StrTabBuilder.getOffset(Name);
308}
309
310void StringTableSection::finalize() { StrTabBuilder.finalize(); }
311
Jake Ehrlich76e91102018-01-25 22:46:17 +0000312void SectionWriter::visit(const StringTableSection &Sec) {
313 Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
314}
315
316void StringTableSection::accept(SectionVisitor &Visitor) const {
317 Visitor.visit(*this);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000318}
319
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000320void StringTableSection::accept(MutableSectionVisitor &Visitor) {
321 Visitor.visit(*this);
322}
323
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000324template <class ELFT>
325void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
326 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000327 auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
Fangrui Song75709322018-11-17 01:44:25 +0000328 llvm::copy(Sec.Indexes, IndexesBuffer);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000329}
330
331void SectionIndexSection::initialize(SectionTableRef SecTable) {
332 Size = 0;
333 setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
334 Link,
335 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
336 "Link field value " + Twine(Link) + " in section " + Name +
337 " is not a symbol table"));
338 Symbols->setShndxTable(this);
339}
340
341void SectionIndexSection::finalize() { Link = Symbols->Index; }
342
343void SectionIndexSection::accept(SectionVisitor &Visitor) const {
344 Visitor.visit(*this);
345}
346
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000347void SectionIndexSection::accept(MutableSectionVisitor &Visitor) {
348 Visitor.visit(*this);
349}
350
Petr Hosekc1135772017-09-13 03:04:50 +0000351static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000352 switch (Index) {
353 case SHN_ABS:
354 case SHN_COMMON:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000355 return true;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000356 }
Petr Hosekc1135772017-09-13 03:04:50 +0000357 if (Machine == EM_HEXAGON) {
358 switch (Index) {
359 case SHN_HEXAGON_SCOMMON:
360 case SHN_HEXAGON_SCOMMON_2:
361 case SHN_HEXAGON_SCOMMON_4:
362 case SHN_HEXAGON_SCOMMON_8:
363 return true;
364 }
365 }
366 return false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000367}
368
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000369// Large indexes force us to clarify exactly what this function should do. This
370// function should return the value that will appear in st_shndx when written
371// out.
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000372uint16_t Symbol::getShndx() const {
373 if (DefinedIn != nullptr) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000374 if (DefinedIn->Index >= SHN_LORESERVE)
375 return SHN_XINDEX;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000376 return DefinedIn->Index;
377 }
378 switch (ShndxType) {
379 // This means that we don't have a defined section but we do need to
380 // output a legitimate section index.
381 case SYMBOL_SIMPLE_INDEX:
382 return SHN_UNDEF;
383 case SYMBOL_ABS:
384 case SYMBOL_COMMON:
385 case SYMBOL_HEXAGON_SCOMMON:
386 case SYMBOL_HEXAGON_SCOMMON_2:
387 case SYMBOL_HEXAGON_SCOMMON_4:
388 case SYMBOL_HEXAGON_SCOMMON_8:
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000389 case SYMBOL_XINDEX:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000390 return static_cast<uint16_t>(ShndxType);
391 }
392 llvm_unreachable("Symbol with invalid ShndxType encountered");
393}
394
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000395bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; }
396
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000397void SymbolTableSection::assignIndices() {
398 uint32_t Index = 0;
399 for (auto &Sym : Symbols)
400 Sym->Index = Index++;
401}
402
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000403void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
Petr Hosek79cee9e2017-08-29 02:12:03 +0000404 SectionBase *DefinedIn, uint64_t Value,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000405 uint8_t Visibility, uint16_t Shndx,
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000406 uint64_t Size) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000407 Symbol Sym;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000408 Sym.Name = Name.str();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000409 Sym.Binding = Bind;
410 Sym.Type = Type;
411 Sym.DefinedIn = DefinedIn;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000412 if (DefinedIn != nullptr)
413 DefinedIn->HasSymbol = true;
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000414 if (DefinedIn == nullptr) {
415 if (Shndx >= SHN_LORESERVE)
416 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
417 else
418 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
419 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000420 Sym.Value = Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000421 Sym.Visibility = Visibility;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000422 Sym.Size = Size;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000423 Sym.Index = Symbols.size();
424 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
425 Size += this->EntrySize;
426}
427
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000428void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000429 if (SectionIndexTable == Sec)
430 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000431 if (SymbolNames == Sec) {
432 error("String table " + SymbolNames->Name +
433 " cannot be removed because it is referenced by the symbol table " +
434 this->Name);
435 }
Paul Semel41695f82018-05-02 20:19:22 +0000436 removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000437}
438
Alexander Shaposhnikov40e9bdf2018-04-26 18:28:17 +0000439void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
Paul Semel46201fb2018-06-01 16:19:46 +0000440 std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
441 [Callable](SymPtr &Sym) { Callable(*Sym); });
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000442 std::stable_partition(
443 std::begin(Symbols), std::end(Symbols),
444 [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000445 assignIndices();
Jake Ehrlich27a29b02018-01-05 19:19:09 +0000446}
447
Paul Semel4246a462018-05-09 21:36:54 +0000448void SymbolTableSection::removeSymbols(
449 function_ref<bool(const Symbol &)> ToRemove) {
Paul Semel41695f82018-05-02 20:19:22 +0000450 Symbols.erase(
Paul Semel46201fb2018-06-01 16:19:46 +0000451 std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
Paul Semel41695f82018-05-02 20:19:22 +0000452 [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
453 std::end(Symbols));
454 Size = Symbols.size() * EntrySize;
455 assignIndices();
456}
457
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000458void SymbolTableSection::initialize(SectionTableRef SecTable) {
459 Size = 0;
460 setStrTab(SecTable.getSectionOfType<StringTableSection>(
461 Link,
462 "Symbol table has link index of " + Twine(Link) +
463 " which is not a valid index",
464 "Symbol table has link index of " + Twine(Link) +
465 " which is not a string table"));
466}
467
Petr Hosek79cee9e2017-08-29 02:12:03 +0000468void SymbolTableSection::finalize() {
469 // Make sure SymbolNames is finalized before getting name indexes.
470 SymbolNames->finalize();
471
472 uint32_t MaxLocalIndex = 0;
473 for (auto &Sym : Symbols) {
474 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
475 if (Sym->Binding == STB_LOCAL)
476 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
477 }
478 // Now we need to set the Link and Info fields.
479 Link = SymbolNames->Index;
480 Info = MaxLocalIndex + 1;
481}
482
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000483void SymbolTableSection::prepareForLayout() {
484 // Add all potential section indexes before file layout so that the section
485 // index section has the approprite size.
486 if (SectionIndexTable != nullptr) {
487 for (const auto &Sym : Symbols) {
488 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
489 SectionIndexTable->addIndex(Sym->DefinedIn->Index);
490 else
491 SectionIndexTable->addIndex(SHN_UNDEF);
492 }
493 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000494 // Add all of our strings to SymbolNames so that SymbolNames has the right
495 // size before layout is decided.
496 for (auto &Sym : Symbols)
497 SymbolNames->addString(Sym->Name);
498}
499
500const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
501 if (Symbols.size() <= Index)
502 error("Invalid symbol index: " + Twine(Index));
503 return Symbols[Index].get();
504}
505
Paul Semel99dda0b2018-05-25 11:01:25 +0000506Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
507 return const_cast<Symbol *>(
508 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
509}
510
Petr Hosek79cee9e2017-08-29 02:12:03 +0000511template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000512void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000513 uint8_t *Buf = Out.getBufferStart();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000514 Buf += Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000515 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000516 // Loop though symbols setting each entry of the symbol table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000517 for (auto &Symbol : Sec.Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000518 Sym->st_name = Symbol->NameIndex;
519 Sym->st_value = Symbol->Value;
520 Sym->st_size = Symbol->Size;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000521 Sym->st_other = Symbol->Visibility;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000522 Sym->setBinding(Symbol->Binding);
523 Sym->setType(Symbol->Type);
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000524 Sym->st_shndx = Symbol->getShndx();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000525 ++Sym;
526 }
527}
528
Jake Ehrlich76e91102018-01-25 22:46:17 +0000529void SymbolTableSection::accept(SectionVisitor &Visitor) const {
530 Visitor.visit(*this);
531}
532
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000533void SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
534 Visitor.visit(*this);
535}
536
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000537template <class SymTabType>
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000538void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
539 const SectionBase *Sec) {
540 if (Symbols == Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000541 error("Symbol table " + Symbols->Name +
542 " cannot be removed because it is "
543 "referenced by the relocation "
544 "section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000545 this->Name);
546 }
547}
548
549template <class SymTabType>
550void RelocSectionWithSymtabBase<SymTabType>::initialize(
551 SectionTableRef SecTable) {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000552 if (Link != SHN_UNDEF)
553 setSymTab(SecTable.getSectionOfType<SymTabType>(
554 Link,
555 "Link field value " + Twine(Link) + " in section " + Name +
556 " is invalid",
557 "Link field value " + Twine(Link) + " in section " + Name +
558 " is not a symbol table"));
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000559
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000560 if (Info != SHN_UNDEF)
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000561 setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
562 " in section " + Name +
563 " is invalid"));
James Y Knight2ea995a2017-09-26 22:44:01 +0000564 else
565 setSection(nullptr);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000566}
567
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000568template <class SymTabType>
569void RelocSectionWithSymtabBase<SymTabType>::finalize() {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000570 this->Link = Symbols ? Symbols->Index : 0;
571
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000572 if (SecToApplyRel != nullptr)
573 this->Info = SecToApplyRel->Index;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000574}
575
576template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000577static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
Petr Hosekd7df9b22017-09-06 23:41:02 +0000578
579template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000580static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000581 Rela.r_addend = Addend;
582}
583
Jake Ehrlich76e91102018-01-25 22:46:17 +0000584template <class RelRange, class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000585static void writeRel(const RelRange &Relocations, T *Buf) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000586 for (const auto &Reloc : Relocations) {
587 Buf->r_offset = Reloc.Offset;
588 setAddend(*Buf, Reloc.Addend);
589 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
590 ++Buf;
591 }
592}
593
594template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000595void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
596 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
597 if (Sec.Type == SHT_REL)
598 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000599 else
Jake Ehrlich76e91102018-01-25 22:46:17 +0000600 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000601}
602
Jake Ehrlich76e91102018-01-25 22:46:17 +0000603void RelocationSection::accept(SectionVisitor &Visitor) const {
604 Visitor.visit(*this);
605}
606
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000607void RelocationSection::accept(MutableSectionVisitor &Visitor) {
608 Visitor.visit(*this);
609}
610
Paul Semel4246a462018-05-09 21:36:54 +0000611void RelocationSection::removeSymbols(
612 function_ref<bool(const Symbol &)> ToRemove) {
613 for (const Relocation &Reloc : Relocations)
614 if (ToRemove(*Reloc.RelocSymbol))
Jordan Rupprecht88ed5e52018-08-09 22:52:03 +0000615 error("not stripping symbol '" + Reloc.RelocSymbol->Name +
Paul Semel4246a462018-05-09 21:36:54 +0000616 "' because it is named in a relocation");
617}
618
Paul Semel99dda0b2018-05-25 11:01:25 +0000619void RelocationSection::markSymbols() {
620 for (const Relocation &Reloc : Relocations)
621 Reloc.RelocSymbol->Referenced = true;
622}
623
Jake Ehrlich76e91102018-01-25 22:46:17 +0000624void SectionWriter::visit(const DynamicRelocationSection &Sec) {
Fangrui Song75709322018-11-17 01:44:25 +0000625 llvm::copy(Sec.Contents,
Jake Ehrlich76e91102018-01-25 22:46:17 +0000626 Out.getBufferStart() + Sec.Offset);
627}
628
629void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
630 Visitor.visit(*this);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000631}
632
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000633void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
634 Visitor.visit(*this);
635}
636
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000637void Section::removeSectionReferences(const SectionBase *Sec) {
638 if (LinkSection == Sec) {
639 error("Section " + LinkSection->Name +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000640 " cannot be removed because it is "
641 "referenced by the section " +
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000642 this->Name);
643 }
644}
645
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000646void GroupSection::finalize() {
647 this->Info = Sym->Index;
648 this->Link = SymTab->Index;
649}
650
Paul Semel4246a462018-05-09 21:36:54 +0000651void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
652 if (ToRemove(*Sym)) {
653 error("Symbol " + Sym->Name +
654 " cannot be removed because it is "
655 "referenced by the section " +
656 this->Name + "[" + Twine(this->Index) + "]");
657 }
658}
659
Paul Semel99dda0b2018-05-25 11:01:25 +0000660void GroupSection::markSymbols() {
661 if (Sym)
662 Sym->Referenced = true;
663}
664
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000665void Section::initialize(SectionTableRef SecTable) {
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000666 if (Link != ELF::SHN_UNDEF) {
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000667 LinkSection =
668 SecTable.getSection(Link, "Link field value " + Twine(Link) +
669 " in section " + Name + " is invalid");
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000670 if (LinkSection->Type == ELF::SHT_SYMTAB)
671 LinkSection = nullptr;
672 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000673}
674
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000675void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000676
Jake Ehrlich76e91102018-01-25 22:46:17 +0000677void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
Alexander Richardson6c859922018-02-19 19:53:44 +0000678 FileName = sys::path::filename(File);
679 // The format for the .gnu_debuglink starts with the file name and is
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000680 // followed by a null terminator and then the CRC32 of the file. The CRC32
681 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
682 // byte, and then finally push the size to alignment and add 4.
683 Size = alignTo(FileName.size() + 1, 4) + 4;
684 // The CRC32 will only be aligned if we align the whole section.
685 Align = 4;
686 Type = ELF::SHT_PROGBITS;
687 Name = ".gnu_debuglink";
688 // For sections not found in segments, OriginalOffset is only used to
689 // establish the order that sections should go in. By using the maximum
690 // possible offset we cause this section to wind up at the end.
691 OriginalOffset = std::numeric_limits<uint64_t>::max();
Fangrui Song32a34e62018-11-01 16:02:12 +0000692 JamCRC CRC;
693 CRC.update(ArrayRef<char>(Data.data(), Data.size()));
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000694 // The CRC32 value needs to be complemented because the JamCRC dosn't
695 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
696 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
Fangrui Song32a34e62018-11-01 16:02:12 +0000697 CRC32 = ~CRC.getCRC();
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000698}
699
Jake Ehrlich76e91102018-01-25 22:46:17 +0000700GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000701 // Read in the file to compute the CRC of it.
702 auto DebugOrErr = MemoryBuffer::getFile(File);
703 if (!DebugOrErr)
704 error("'" + File + "': " + DebugOrErr.getError().message());
705 auto Debug = std::move(*DebugOrErr);
706 init(File, Debug->getBuffer());
707}
708
709template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000710void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
711 auto Buf = Out.getBufferStart() + Sec.Offset;
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000712 char *File = reinterpret_cast<char *>(Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000713 Elf_Word *CRC =
714 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
715 *CRC = Sec.CRC32;
Fangrui Song75709322018-11-17 01:44:25 +0000716 llvm::copy(Sec.FileName, File);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000717}
718
719void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
720 Visitor.visit(*this);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000721}
722
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000723void GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) {
724 Visitor.visit(*this);
725}
726
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000727template <class ELFT>
728void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
729 ELF::Elf32_Word *Buf =
730 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
731 *Buf++ = Sec.FlagWord;
732 for (const auto *S : Sec.GroupMembers)
733 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
734}
735
736void GroupSection::accept(SectionVisitor &Visitor) const {
737 Visitor.visit(*this);
738}
739
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000740void GroupSection::accept(MutableSectionVisitor &Visitor) {
741 Visitor.visit(*this);
742}
743
Petr Hosek05a04cb2017-08-01 00:33:58 +0000744// Returns true IFF a section is wholly inside the range of a segment
745static bool sectionWithinSegment(const SectionBase &Section,
746 const Segment &Segment) {
747 // If a section is empty it should be treated like it has a size of 1. This is
748 // to clarify the case when an empty section lies on a boundary between two
749 // segments and ensures that the section "belongs" to the second segment and
750 // not the first.
751 uint64_t SecSize = Section.Size ? Section.Size : 1;
752 return Segment.Offset <= Section.OriginalOffset &&
753 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
754}
755
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000756// Returns true IFF a segment's original offset is inside of another segment's
757// range.
758static bool segmentOverlapsSegment(const Segment &Child,
759 const Segment &Parent) {
760
761 return Parent.OriginalOffset <= Child.OriginalOffset &&
762 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
763}
764
Jake Ehrlich46814be2018-01-22 19:27:30 +0000765static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +0000766 // Any segment without a parent segment should come before a segment
767 // that has a parent segment.
768 if (A->OriginalOffset < B->OriginalOffset)
769 return true;
770 if (A->OriginalOffset > B->OriginalOffset)
771 return false;
772 return A->Index < B->Index;
773}
774
Jake Ehrlich46814be2018-01-22 19:27:30 +0000775static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
776 if (A->PAddr < B->PAddr)
777 return true;
778 if (A->PAddr > B->PAddr)
779 return false;
780 return A->Index < B->Index;
781}
782
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000783void BinaryELFBuilder::initFileHeader() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000784 Obj->Flags = 0x0;
785 Obj->Type = ET_REL;
George Rimar3ac20a92018-12-20 10:59:52 +0000786 Obj->OSABI = ELFOSABI_NONE;
George Rimar4ded7732018-12-20 10:51:42 +0000787 Obj->ABIVersion = 0;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000788 Obj->Entry = 0x0;
789 Obj->Machine = EMachine;
790 Obj->Version = 1;
791}
792
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000793void BinaryELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; }
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000794
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000795StringTableSection *BinaryELFBuilder::addStrTab() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000796 auto &StrTab = Obj->addSection<StringTableSection>();
797 StrTab.Name = ".strtab";
798
799 Obj->SectionNames = &StrTab;
800 return &StrTab;
801}
802
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000803SymbolTableSection *BinaryELFBuilder::addSymTab(StringTableSection *StrTab) {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000804 auto &SymTab = Obj->addSection<SymbolTableSection>();
805
806 SymTab.Name = ".symtab";
807 SymTab.Link = StrTab->Index;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000808
809 // The symbol table always needs a null symbol
810 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
811
812 Obj->SymbolTable = &SymTab;
813 return &SymTab;
814}
815
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000816void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000817 auto Data = ArrayRef<uint8_t>(
818 reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
819 MemBuf->getBufferSize());
820 auto &DataSection = Obj->addSection<Section>(Data);
821 DataSection.Name = ".data";
822 DataSection.Type = ELF::SHT_PROGBITS;
823 DataSection.Size = Data.size();
824 DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
825
826 std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
827 std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
Fangrui Song32a34e62018-11-01 16:02:12 +0000828 [](char C) { return !isalnum(C); }, '_');
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000829 Twine Prefix = Twine("_binary_") + SanitizedFilename;
830
831 SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
832 /*Value=*/0, STV_DEFAULT, 0, 0);
833 SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
834 /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
835 SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
836 /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
837}
838
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000839void BinaryELFBuilder::initSections() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000840 for (auto &Section : Obj->sections()) {
841 Section.initialize(Obj->sections());
842 }
843}
844
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000845std::unique_ptr<Object> BinaryELFBuilder::build() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000846 initFileHeader();
847 initHeaderSegment();
848 StringTableSection *StrTab = addStrTab();
849 SymbolTableSection *SymTab = addSymTab(StrTab);
850 initSections();
851 addData(SymTab);
852
853 return std::move(Obj);
854}
855
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000856template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
Jake Ehrlich6452b112018-02-14 23:31:33 +0000857 for (auto &Parent : Obj.segments()) {
858 // Every segment will overlap with itself but we don't want a segment to
859 // be it's own parent so we avoid that situation.
860 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
861 // We want a canonical "most parental" segment but this requires
862 // inspecting the ParentSegment.
863 if (compareSegmentsByOffset(&Parent, &Child))
864 if (Child.ParentSegment == nullptr ||
865 compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
866 Child.ParentSegment = &Parent;
867 }
868 }
869 }
870}
871
Jake Ehrlich76e91102018-01-25 22:46:17 +0000872template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000873 uint32_t Index = 0;
874 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
Petr Hosekc4df10e2017-08-04 21:09:26 +0000875 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
876 (size_t)Phdr.p_filesz};
Jake Ehrlich76e91102018-01-25 22:46:17 +0000877 Segment &Seg = Obj.addSegment(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000878 Seg.Type = Phdr.p_type;
879 Seg.Flags = Phdr.p_flags;
Petr Hosek3f383832017-08-26 01:32:20 +0000880 Seg.OriginalOffset = Phdr.p_offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000881 Seg.Offset = Phdr.p_offset;
882 Seg.VAddr = Phdr.p_vaddr;
883 Seg.PAddr = Phdr.p_paddr;
884 Seg.FileSize = Phdr.p_filesz;
885 Seg.MemSize = Phdr.p_memsz;
886 Seg.Align = Phdr.p_align;
887 Seg.Index = Index++;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000888 for (auto &Section : Obj.sections()) {
889 if (sectionWithinSegment(Section, Seg)) {
890 Seg.addSection(&Section);
891 if (!Section.ParentSegment ||
892 Section.ParentSegment->Offset > Seg.Offset) {
893 Section.ParentSegment = &Seg;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000894 }
895 }
896 }
897 }
Jake Ehrlich6452b112018-02-14 23:31:33 +0000898
899 auto &ElfHdr = Obj.ElfHdrSegment;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000900 ElfHdr.Index = Index++;
901
902 const auto &Ehdr = *ElfFile.getHeader();
903 auto &PrHdr = Obj.ProgramHdrSegment;
904 PrHdr.Type = PT_PHDR;
905 PrHdr.Flags = 0;
906 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
907 // Whereas this works automatically for ElfHdr, here OriginalOffset is
908 // always non-zero and to ensure the equation we assign the same value to
909 // VAddr as well.
910 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
911 PrHdr.PAddr = 0;
912 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000913 // The spec requires us to naturally align all the fields.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000914 PrHdr.Align = sizeof(Elf_Addr);
915 PrHdr.Index = Index++;
916
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000917 // Now we do an O(n^2) loop through the segments in order to match up
918 // segments.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000919 for (auto &Child : Obj.segments())
920 setParentSegment(Child);
921 setParentSegment(ElfHdr);
922 setParentSegment(PrHdr);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000923}
924
925template <class ELFT>
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000926void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
927 auto SecTable = Obj.sections();
928 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
929 GroupSec->Link,
930 "Link field value " + Twine(GroupSec->Link) + " in section " +
931 GroupSec->Name + " is invalid",
932 "Link field value " + Twine(GroupSec->Link) + " in section " +
933 GroupSec->Name + " is not a symbol table");
934 auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
935 if (!Sym)
936 error("Info field value " + Twine(GroupSec->Info) + " in section " +
937 GroupSec->Name + " is not a valid symbol index");
938 GroupSec->setSymTab(SymTab);
939 GroupSec->setSymbol(Sym);
940 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
941 GroupSec->Contents.empty())
942 error("The content of the section " + GroupSec->Name + " is malformed");
943 const ELF::Elf32_Word *Word =
944 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
945 const ELF::Elf32_Word *End =
946 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
947 GroupSec->setFlagWord(*Word++);
948 for (; Word != End; ++Word) {
949 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
950 GroupSec->addMember(SecTable.getSection(
951 Index, "Group member index " + Twine(Index) + " in section " +
952 GroupSec->Name + " is invalid"));
953 }
954}
955
956template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000957void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000958 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
959 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000960 ArrayRef<Elf_Word> ShndxData;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000961
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000962 auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
963 for (const auto &Sym : Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000964 SectionBase *DefSection = nullptr;
965 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000966
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000967 if (Sym.st_shndx == SHN_XINDEX) {
968 if (SymTab->getShndxTable() == nullptr)
969 error("Symbol '" + Name +
970 "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
971 if (ShndxData.data() == nullptr) {
972 const Elf_Shdr &ShndxSec =
973 *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
974 ShndxData = unwrapOrError(
975 ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
976 if (ShndxData.size() != Symbols.size())
977 error("Symbol section index table does not have the same number of "
978 "entries as the symbol table.");
979 }
980 Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
981 DefSection = Obj.sections().getSection(
982 Index,
Puyan Lotfi97604b42018-08-02 18:16:52 +0000983 "Symbol '" + Name + "' has invalid section index " + Twine(Index));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000984 } else if (Sym.st_shndx >= SHN_LORESERVE) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000985 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000986 error(
987 "Symbol '" + Name +
988 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
989 Twine(Sym.st_shndx));
990 }
991 } else if (Sym.st_shndx != SHN_UNDEF) {
Jake Ehrlich76e91102018-01-25 22:46:17 +0000992 DefSection = Obj.sections().getSection(
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000993 Sym.st_shndx, "Symbol '" + Name +
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000994 "' is defined has invalid section index " +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000995 Twine(Sym.st_shndx));
Petr Hosek79cee9e2017-08-29 02:12:03 +0000996 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000997
Petr Hosek79cee9e2017-08-29 02:12:03 +0000998 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000999 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
Petr Hosek79cee9e2017-08-29 02:12:03 +00001000 }
1001}
1002
1003template <class ELFT>
Petr Hosekd7df9b22017-09-06 23:41:02 +00001004static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
1005
1006template <class ELFT>
1007static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
1008 ToSet = Rela.r_addend;
1009}
1010
Jake Ehrlich76e91102018-01-25 22:46:17 +00001011template <class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +00001012static void initRelocations(RelocationSection *Relocs,
1013 SymbolTableSection *SymbolTable, T RelRange) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001014 for (const auto &Rel : RelRange) {
1015 Relocation ToAdd;
1016 ToAdd.Offset = Rel.r_offset;
1017 getAddend(ToAdd.Addend, Rel);
1018 ToAdd.Type = Rel.getType(false);
Paul Semel31a212d2018-05-22 01:04:36 +00001019 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
Petr Hosekd7df9b22017-09-06 23:41:02 +00001020 Relocs->addRelocation(ToAdd);
1021 }
1022}
1023
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001024SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001025 if (Index == SHN_UNDEF || Index > Sections.size())
1026 error(ErrMsg);
1027 return Sections[Index - 1].get();
1028}
1029
1030template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001031T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
Zachary Turner41a9ee92017-10-11 23:54:34 +00001032 Twine TypeErrMsg) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001033 if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001034 return Sec;
1035 error(TypeErrMsg);
1036}
1037
Petr Hosekd7df9b22017-09-06 23:41:02 +00001038template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +00001039SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001040 ArrayRef<uint8_t> Data;
1041 switch (Shdr.sh_type) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001042 case SHT_REL:
1043 case SHT_RELA:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +00001044 if (Shdr.sh_flags & SHF_ALLOC) {
1045 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001046 return Obj.addSection<DynamicRelocationSection>(Data);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +00001047 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001048 return Obj.addSection<RelocationSection>();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001049 case SHT_STRTAB:
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001050 // If a string table is allocated we don't want to mess with it. That would
1051 // mean altering the memory image. There are no special link types or
1052 // anything so we can just use a Section.
1053 if (Shdr.sh_flags & SHF_ALLOC) {
1054 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001055 return Obj.addSection<Section>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001056 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001057 return Obj.addSection<StringTableSection>();
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001058 case SHT_HASH:
1059 case SHT_GNU_HASH:
1060 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
1061 // Because of this we don't need to mess with the hash tables either.
1062 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001063 return Obj.addSection<Section>(Data);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001064 case SHT_GROUP:
1065 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1066 return Obj.addSection<GroupSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001067 case SHT_DYNSYM:
1068 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001069 return Obj.addSection<DynamicSymbolTableSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001070 case SHT_DYNAMIC:
1071 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001072 return Obj.addSection<DynamicSection>(Data);
Petr Hosek79cee9e2017-08-29 02:12:03 +00001073 case SHT_SYMTAB: {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001074 auto &SymTab = Obj.addSection<SymbolTableSection>();
1075 Obj.SymbolTable = &SymTab;
1076 return SymTab;
Petr Hosek79cee9e2017-08-29 02:12:03 +00001077 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001078 case SHT_SYMTAB_SHNDX: {
1079 auto &ShndxSection = Obj.addSection<SectionIndexSection>();
1080 Obj.SectionIndexTable = &ShndxSection;
1081 return ShndxSection;
1082 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001083 case SHT_NOBITS:
Jake Ehrlich76e91102018-01-25 22:46:17 +00001084 return Obj.addSection<Section>(Data);
Puyan Lotfiaf048642018-10-01 10:29:41 +00001085 default: {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001086 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Puyan Lotfiaf048642018-10-01 10:29:41 +00001087
1088 if (isDataGnuCompressed(Data) || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) {
1089 uint64_t DecompressedSize, DecompressedAlign;
1090 std::tie(DecompressedSize, DecompressedAlign) =
1091 getDecompressedSizeAndAlignment<ELFT>(Data);
1092 return Obj.addSection<CompressedSection>(Data, DecompressedSize,
1093 DecompressedAlign);
1094 }
1095
Jake Ehrlich76e91102018-01-25 22:46:17 +00001096 return Obj.addSection<Section>(Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001097 }
Puyan Lotfiaf048642018-10-01 10:29:41 +00001098 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001099}
1100
Jake Ehrlich76e91102018-01-25 22:46:17 +00001101template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001102 uint32_t Index = 0;
1103 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
1104 if (Index == 0) {
1105 ++Index;
1106 continue;
1107 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001108 auto &Sec = makeSection(Shdr);
1109 Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
1110 Sec.Type = Shdr.sh_type;
1111 Sec.Flags = Shdr.sh_flags;
1112 Sec.Addr = Shdr.sh_addr;
1113 Sec.Offset = Shdr.sh_offset;
1114 Sec.OriginalOffset = Shdr.sh_offset;
1115 Sec.Size = Shdr.sh_size;
1116 Sec.Link = Shdr.sh_link;
1117 Sec.Info = Shdr.sh_info;
1118 Sec.Align = Shdr.sh_addralign;
1119 Sec.EntrySize = Shdr.sh_entsize;
1120 Sec.Index = Index++;
Paul Semela42dec72018-08-09 17:05:21 +00001121 Sec.OriginalData =
1122 ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
1123 (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001124 }
Petr Hosek79cee9e2017-08-29 02:12:03 +00001125
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001126 // If a section index table exists we'll need to initialize it before we
1127 // initialize the symbol table because the symbol table might need to
1128 // reference it.
1129 if (Obj.SectionIndexTable)
1130 Obj.SectionIndexTable->initialize(Obj.sections());
1131
Petr Hosek79cee9e2017-08-29 02:12:03 +00001132 // Now that all of the sections have been added we can fill out some extra
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001133 // details about symbol tables. We need the symbol table filled out before
1134 // any relocations.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001135 if (Obj.SymbolTable) {
1136 Obj.SymbolTable->initialize(Obj.sections());
1137 initSymbolTable(Obj.SymbolTable);
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001138 }
Petr Hosekd7df9b22017-09-06 23:41:02 +00001139
1140 // Now that all sections and symbols have been added we can add
1141 // relocations that reference symbols and set the link and info fields for
1142 // relocation sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001143 for (auto &Section : Obj.sections()) {
1144 if (&Section == Obj.SymbolTable)
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001145 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001146 Section.initialize(Obj.sections());
1147 if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001148 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
1149 if (RelSec->Type == SHT_REL)
Jake Ehrlich76e91102018-01-25 22:46:17 +00001150 initRelocations(RelSec, Obj.SymbolTable,
1151 unwrapOrError(ElfFile.rels(Shdr)));
Petr Hosekd7df9b22017-09-06 23:41:02 +00001152 else
Jake Ehrlich76e91102018-01-25 22:46:17 +00001153 initRelocations(RelSec, Obj.SymbolTable,
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001154 unwrapOrError(ElfFile.relas(Shdr)));
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001155 } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
1156 initGroupSection(GroupSec);
Petr Hosekd7df9b22017-09-06 23:41:02 +00001157 }
1158 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001159}
1160
Jake Ehrlich76e91102018-01-25 22:46:17 +00001161template <class ELFT> void ELFBuilder<ELFT>::build() {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001162 const auto &Ehdr = *ElfFile.getHeader();
1163
George Rimar4ded7732018-12-20 10:51:42 +00001164 Obj.OSABI = Ehdr.e_ident[EI_OSABI];
1165 Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
Jake Ehrlich76e91102018-01-25 22:46:17 +00001166 Obj.Type = Ehdr.e_type;
1167 Obj.Machine = Ehdr.e_machine;
1168 Obj.Version = Ehdr.e_version;
1169 Obj.Entry = Ehdr.e_entry;
1170 Obj.Flags = Ehdr.e_flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001171
Jake Ehrlich76e91102018-01-25 22:46:17 +00001172 readSectionHeaders();
1173 readProgramHeaders();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001174
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001175 uint32_t ShstrIndex = Ehdr.e_shstrndx;
1176 if (ShstrIndex == SHN_XINDEX)
1177 ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
1178
Jake Ehrlich76e91102018-01-25 22:46:17 +00001179 Obj.SectionNames =
1180 Obj.sections().template getSectionOfType<StringTableSection>(
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001181 ShstrIndex,
Jake Ehrlich8b831c12018-03-07 20:33:02 +00001182 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +00001183 " in elf header " + " is invalid",
Jake Ehrlich8b831c12018-03-07 20:33:02 +00001184 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
Jake Ehrlich76e91102018-01-25 22:46:17 +00001185 " in elf header " + " is not a string table");
Petr Hosek05a04cb2017-08-01 00:33:58 +00001186}
1187
Jake Ehrlich76e91102018-01-25 22:46:17 +00001188// A generic size function which computes sizes of any random access range.
1189template <class R> size_t size(R &&Range) {
1190 return static_cast<size_t>(std::end(Range) - std::begin(Range));
1191}
1192
1193Writer::~Writer() {}
1194
1195Reader::~Reader() {}
1196
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001197std::unique_ptr<Object> BinaryReader::create() const {
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001198 return BinaryELFBuilder(MInfo.EMachine, MemBuf).build();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001199}
1200
1201std::unique_ptr<Object> ELFReader::create() const {
Alexander Shaposhnikov58cb1972018-06-07 19:41:42 +00001202 auto Obj = llvm::make_unique<Object>();
Fangrui Song32a34e62018-11-01 16:02:12 +00001203 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
1204 ELFBuilder<ELF32LE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001205 Builder.build();
1206 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001207 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
1208 ELFBuilder<ELF64LE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001209 Builder.build();
1210 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001211 } else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
1212 ELFBuilder<ELF32BE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001213 Builder.build();
1214 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001215 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
1216 ELFBuilder<ELF64BE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001217 Builder.build();
1218 return Obj;
1219 }
1220 error("Invalid file type");
1221}
1222
1223template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001224 uint8_t *B = Buf.getBufferStart();
1225 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001226 std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
1227 Ehdr.e_ident[EI_MAG0] = 0x7f;
1228 Ehdr.e_ident[EI_MAG1] = 'E';
1229 Ehdr.e_ident[EI_MAG2] = 'L';
1230 Ehdr.e_ident[EI_MAG3] = 'F';
1231 Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1232 Ehdr.e_ident[EI_DATA] =
1233 ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
1234 Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
George Rimar4ded7732018-12-20 10:51:42 +00001235 Ehdr.e_ident[EI_OSABI] = Obj.OSABI;
1236 Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion;
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001237
Jake Ehrlich76e91102018-01-25 22:46:17 +00001238 Ehdr.e_type = Obj.Type;
1239 Ehdr.e_machine = Obj.Machine;
1240 Ehdr.e_version = Obj.Version;
1241 Ehdr.e_entry = Obj.Entry;
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001242 // We have to use the fully-qualified name llvm::size
1243 // since some compilers complain on ambiguous resolution.
1244 Ehdr.e_phnum = llvm::size(Obj.segments());
Julie Hockett468722e2018-09-12 17:56:31 +00001245 Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0;
1246 Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001247 Ehdr.e_flags = Obj.Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001248 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
Julie Hockett468722e2018-09-12 17:56:31 +00001249 if (WriteSectionHeaders && size(Obj.sections()) != 0) {
1250 Ehdr.e_shentsize = sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001251 Ehdr.e_shoff = Obj.SHOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001252 // """
1253 // If the number of sections is greater than or equal to
1254 // SHN_LORESERVE (0xff00), this member has the value zero and the actual
1255 // number of section header table entries is contained in the sh_size field
1256 // of the section header at index 0.
1257 // """
1258 auto Shnum = size(Obj.sections()) + 1;
1259 if (Shnum >= SHN_LORESERVE)
1260 Ehdr.e_shnum = 0;
1261 else
1262 Ehdr.e_shnum = Shnum;
1263 // """
1264 // If the section name string table section index is greater than or equal
1265 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
1266 // and the actual index of the section name string table section is
1267 // contained in the sh_link field of the section header at index 0.
1268 // """
1269 if (Obj.SectionNames->Index >= SHN_LORESERVE)
1270 Ehdr.e_shstrndx = SHN_XINDEX;
1271 else
1272 Ehdr.e_shstrndx = Obj.SectionNames->Index;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001273 } else {
Julie Hockett468722e2018-09-12 17:56:31 +00001274 Ehdr.e_shentsize = 0;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001275 Ehdr.e_shoff = 0;
1276 Ehdr.e_shnum = 0;
1277 Ehdr.e_shstrndx = 0;
1278 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001279}
1280
Jake Ehrlich76e91102018-01-25 22:46:17 +00001281template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1282 for (auto &Seg : Obj.segments())
1283 writePhdr(Seg);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001284}
1285
Jake Ehrlich76e91102018-01-25 22:46:17 +00001286template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001287 uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001288 // This reference serves to write the dummy section header at the begining
Jake Ehrlich425ec9f2017-09-15 22:04:09 +00001289 // of the file. It is not used for anything else
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001290 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001291 Shdr.sh_name = 0;
1292 Shdr.sh_type = SHT_NULL;
1293 Shdr.sh_flags = 0;
1294 Shdr.sh_addr = 0;
1295 Shdr.sh_offset = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001296 // See writeEhdr for why we do this.
1297 uint64_t Shnum = size(Obj.sections()) + 1;
1298 if (Shnum >= SHN_LORESERVE)
1299 Shdr.sh_size = Shnum;
1300 else
1301 Shdr.sh_size = 0;
1302 // See writeEhdr for why we do this.
1303 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1304 Shdr.sh_link = Obj.SectionNames->Index;
1305 else
1306 Shdr.sh_link = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001307 Shdr.sh_info = 0;
1308 Shdr.sh_addralign = 0;
1309 Shdr.sh_entsize = 0;
1310
Jake Ehrlich76e91102018-01-25 22:46:17 +00001311 for (auto &Sec : Obj.sections())
1312 writeShdr(Sec);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001313}
1314
Jake Ehrlich76e91102018-01-25 22:46:17 +00001315template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1316 for (auto &Sec : Obj.sections())
1317 Sec.accept(*SecWriter);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001318}
1319
Jake Ehrlich76e91102018-01-25 22:46:17 +00001320void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001321
1322 auto Iter = std::stable_partition(
1323 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1324 if (ToRemove(*Sec))
1325 return false;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001326 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1327 if (auto ToRelSec = RelSec->getSection())
1328 return !ToRemove(*ToRelSec);
1329 }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001330 return true;
1331 });
1332 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1333 SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001334 if (SectionNames != nullptr && ToRemove(*SectionNames))
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001335 SectionNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001336 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1337 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001338 // Now make sure there are no remaining references to the sections that will
1339 // be removed. Sometimes it is impossible to remove a reference so we emit
1340 // an error here instead.
1341 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1342 for (auto &Segment : Segments)
1343 Segment->removeSection(RemoveSec.get());
1344 for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1345 KeepSec->removeSectionReferences(RemoveSec.get());
1346 }
1347 // Now finally get rid of them all togethor.
1348 Sections.erase(Iter, std::end(Sections));
1349}
1350
Paul Semel4246a462018-05-09 21:36:54 +00001351void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1352 if (!SymbolTable)
1353 return;
1354
1355 for (const SecPtr &Sec : Sections)
1356 Sec->removeSymbols(ToRemove);
1357}
1358
Jake Ehrlich76e91102018-01-25 22:46:17 +00001359void Object::sortSections() {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001360 // Put all sections in offset order. Maintain the ordering as closely as
1361 // possible while meeting that demand however.
1362 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1363 return A->OriginalOffset < B->OriginalOffset;
1364 };
1365 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1366 CompareSections);
1367}
1368
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001369static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1370 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1371 if (Align == 0)
1372 Align = 1;
1373 auto Diff =
1374 static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1375 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1376 // (Offset + Diff) & -Align == Addr & -Align will still hold.
1377 if (Diff < 0)
1378 Diff += Align;
1379 return Offset + Diff;
1380}
1381
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001382// Orders segments such that if x = y->ParentSegment then y comes before x.
Fangrui Song32a34e62018-11-01 16:02:12 +00001383static void orderSegments(std::vector<Segment *> &Segments) {
Jake Ehrlich46814be2018-01-22 19:27:30 +00001384 std::stable_sort(std::begin(Segments), std::end(Segments),
1385 compareSegmentsByOffset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001386}
1387
1388// This function finds a consistent layout for a list of segments starting from
1389// an Offset. It assumes that Segments have been sorted by OrderSegments and
1390// returns an Offset one past the end of the last segment.
1391static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1392 uint64_t Offset) {
1393 assert(std::is_sorted(std::begin(Segments), std::end(Segments),
Jake Ehrlich46814be2018-01-22 19:27:30 +00001394 compareSegmentsByOffset));
Petr Hosek3f383832017-08-26 01:32:20 +00001395 // The only way a segment should move is if a section was between two
1396 // segments and that section was removed. If that section isn't in a segment
1397 // then it's acceptable, but not ideal, to simply move it to after the
1398 // segments. So we can simply layout segments one after the other accounting
1399 // for alignment.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001400 for (auto &Segment : Segments) {
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001401 // We assume that segments have been ordered by OriginalOffset and Index
1402 // such that a parent segment will always come before a child segment in
1403 // OrderedSegments. This means that the Offset of the ParentSegment should
1404 // already be set and we can set our offset relative to it.
1405 if (Segment->ParentSegment != nullptr) {
1406 auto Parent = Segment->ParentSegment;
1407 Segment->Offset =
1408 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1409 } else {
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001410 Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001411 Segment->Offset = Offset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001412 }
Jake Ehrlich084400b2017-10-04 17:44:42 +00001413 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
Petr Hosek3f383832017-08-26 01:32:20 +00001414 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001415 return Offset;
1416}
1417
1418// This function finds a consistent layout for a list of sections. It assumes
1419// that the ->ParentSegment of each section has already been laid out. The
1420// supplied starting Offset is used for the starting offset of any section that
1421// does not have a ParentSegment. It returns either the offset given if all
1422// sections had a ParentSegment or an offset one past the last section if there
1423// was a section that didn't have a ParentSegment.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001424template <class Range>
Fangrui Song32a34e62018-11-01 16:02:12 +00001425static uint64_t layoutSections(Range Sections, uint64_t Offset) {
Petr Hosek3f383832017-08-26 01:32:20 +00001426 // Now the offset of every segment has been set we can assign the offsets
1427 // of each section. For sections that are covered by a segment we should use
1428 // the segment's original offset and the section's original offset to compute
1429 // the offset from the start of the segment. Using the offset from the start
1430 // of the segment we can assign a new offset to the section. For sections not
1431 // covered by segments we can just bump Offset to the next valid location.
1432 uint32_t Index = 1;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001433 for (auto &Section : Sections) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001434 Section.Index = Index++;
1435 if (Section.ParentSegment != nullptr) {
1436 auto Segment = *Section.ParentSegment;
1437 Section.Offset =
1438 Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
Petr Hosek3f383832017-08-26 01:32:20 +00001439 } else {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001440 Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1441 Section.Offset = Offset;
1442 if (Section.Type != SHT_NOBITS)
1443 Offset += Section.Size;
Petr Hosek3f383832017-08-26 01:32:20 +00001444 }
1445 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001446 return Offset;
1447}
Petr Hosek3f383832017-08-26 01:32:20 +00001448
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001449template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
1450 auto &ElfHdr = Obj.ElfHdrSegment;
1451 ElfHdr.Type = PT_PHDR;
1452 ElfHdr.Flags = 0;
1453 ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
1454 ElfHdr.VAddr = 0;
1455 ElfHdr.PAddr = 0;
1456 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
1457 ElfHdr.Align = 0;
1458}
1459
Jake Ehrlich76e91102018-01-25 22:46:17 +00001460template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001461 // We need a temporary list of segments that has a special order to it
1462 // so that we know that anytime ->ParentSegment is set that segment has
1463 // already had its offset properly set.
1464 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001465 for (auto &Segment : Obj.segments())
1466 OrderedSegments.push_back(&Segment);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001467 OrderedSegments.push_back(&Obj.ElfHdrSegment);
1468 OrderedSegments.push_back(&Obj.ProgramHdrSegment);
Fangrui Song32a34e62018-11-01 16:02:12 +00001469 orderSegments(OrderedSegments);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001470 // Offset is used as the start offset of the first segment to be laid out.
1471 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1472 // we start at offset 0.
1473 uint64_t Offset = 0;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001474 Offset = LayoutSegments(OrderedSegments, Offset);
Fangrui Song32a34e62018-11-01 16:02:12 +00001475 Offset = layoutSections(Obj.sections(), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001476 // If we need to write the section header table out then we need to align the
1477 // Offset so that SHOffset is valid.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001478 if (WriteSectionHeaders)
Jordan Rupprechtde965ea2018-08-10 16:25:58 +00001479 Offset = alignTo(Offset, sizeof(Elf_Addr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001480 Obj.SHOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001481}
1482
Jake Ehrlich76e91102018-01-25 22:46:17 +00001483template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001484 // We already have the section header offset so we can calculate the total
1485 // size by just adding up the size of each section header.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001486 auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1487 return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001488 NullSectionSize;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001489}
1490
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001491template <class ELFT> Error ELFWriter<ELFT>::write() {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001492 writeEhdr();
1493 writePhdrs();
1494 writeSectionData();
1495 if (WriteSectionHeaders)
1496 writeShdrs();
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001497 return Buf.commit();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001498}
1499
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001500template <class ELFT> Error ELFWriter<ELFT>::finalize() {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001501 // It could happen that SectionNames has been removed and yet the user wants
1502 // a section header table output. We need to throw an error if a user tries
1503 // to do that.
1504 if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1505 error("Cannot write section header table because section header string "
1506 "table was removed.");
1507
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001508 Obj.sortSections();
1509
1510 // We need to assign indexes before we perform layout because we need to know
1511 // if we need large indexes or not. We can assign indexes first and check as
1512 // we go to see if we will actully need large indexes.
1513 bool NeedsLargeIndexes = false;
1514 if (size(Obj.sections()) >= SHN_LORESERVE) {
1515 auto Sections = Obj.sections();
1516 NeedsLargeIndexes =
1517 std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1518 [](const SectionBase &Sec) { return Sec.HasSymbol; });
1519 // TODO: handle case where only one section needs the large index table but
1520 // only needs it because the large index table hasn't been removed yet.
1521 }
1522
1523 if (NeedsLargeIndexes) {
1524 // This means we definitely need to have a section index table but if we
1525 // already have one then we should use it instead of making a new one.
1526 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1527 // Addition of a section to the end does not invalidate the indexes of
1528 // other sections and assigns the correct index to the new section.
1529 auto &Shndx = Obj.addSection<SectionIndexSection>();
1530 Obj.SymbolTable->setShndxTable(&Shndx);
1531 Shndx.setSymTab(Obj.SymbolTable);
1532 }
1533 } else {
1534 // Since we don't need SectionIndexTable we should remove it and all
1535 // references to it.
1536 if (Obj.SectionIndexTable != nullptr) {
1537 Obj.removeSections([this](const SectionBase &Sec) {
1538 return &Sec == Obj.SectionIndexTable;
1539 });
1540 }
1541 }
1542
1543 // Make sure we add the names of all the sections. Importantly this must be
1544 // done after we decide to add or remove SectionIndexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001545 if (Obj.SectionNames != nullptr)
1546 for (const auto &Section : Obj.sections()) {
1547 Obj.SectionNames->addString(Section.Name);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001548 }
Jake Ehrlich0a151bd2018-03-07 19:59:15 +00001549
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001550 initEhdrSegment();
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001551
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001552 // Before we can prepare for layout the indexes need to be finalized.
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001553 // Also, the output arch may not be the same as the input arch, so fix up
1554 // size-related fields before doing layout calculations.
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001555 uint64_t Index = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001556 auto SecSizer = llvm::make_unique<ELFSectionSizer<ELFT>>();
1557 for (auto &Sec : Obj.sections()) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001558 Sec.Index = Index++;
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001559 Sec.accept(*SecSizer);
1560 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001561
1562 // The symbol table does not update all other sections on update. For
1563 // instance, symbol names are not added as new symbols are added. This means
1564 // that some sections, like .strtab, don't yet have their final size.
1565 if (Obj.SymbolTable != nullptr)
1566 Obj.SymbolTable->prepareForLayout();
1567
Petr Hosekc4df10e2017-08-04 21:09:26 +00001568 assignOffsets();
1569
1570 // Finalize SectionNames first so that we can assign name indexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001571 if (Obj.SectionNames != nullptr)
1572 Obj.SectionNames->finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001573 // Finally now that all offsets and indexes have been set we can finalize any
1574 // remaining issues.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001575 uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1576 for (auto &Section : Obj.sections()) {
1577 Section.HeaderOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001578 Offset += sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001579 if (WriteSectionHeaders)
1580 Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1581 Section.finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001582 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001583
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001584 if (Error E = Buf.allocate(totalSize()))
1585 return E;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001586 SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001587 return Error::success();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001588}
1589
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001590Error BinaryWriter::write() {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001591 for (auto &Section : Obj.sections()) {
1592 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001593 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001594 Section.accept(*SecWriter);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001595 }
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001596 return Buf.commit();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001597}
1598
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001599Error BinaryWriter::finalize() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001600 // TODO: Create a filter range to construct OrderedSegments from so that this
1601 // code can be deduped with assignOffsets above. This should also solve the
1602 // todo below for LayoutSections.
1603 // We need a temporary list of segments that has a special order to it
1604 // so that we know that anytime ->ParentSegment is set that segment has
1605 // already had it's offset properly set. We only want to consider the segments
1606 // that will affect layout of allocated sections so we only add those.
1607 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001608 for (auto &Section : Obj.sections()) {
1609 if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1610 OrderedSegments.push_back(Section.ParentSegment);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001611 }
1612 }
Jake Ehrlich46814be2018-01-22 19:27:30 +00001613
1614 // For binary output, we're going to use physical addresses instead of
1615 // virtual addresses, since a binary output is used for cases like ROM
1616 // loading and physical addresses are intended for ROM loading.
1617 // However, if no segment has a physical address, we'll fallback to using
1618 // virtual addresses for all.
Fangrui Song5ec95db2018-11-17 01:15:55 +00001619 if (all_of(OrderedSegments,
1620 [](const Segment *Seg) { return Seg->PAddr == 0; }))
1621 for (Segment *Seg : OrderedSegments)
1622 Seg->PAddr = Seg->VAddr;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001623
1624 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1625 compareSegmentsByPAddr);
1626
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001627 // Because we add a ParentSegment for each section we might have duplicate
1628 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1629 // would do very strange things.
1630 auto End =
1631 std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1632 OrderedSegments.erase(End, std::end(OrderedSegments));
1633
Jake Ehrlich46814be2018-01-22 19:27:30 +00001634 uint64_t Offset = 0;
1635
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001636 // Modify the first segment so that there is no gap at the start. This allows
Fangrui Song5ec95db2018-11-17 01:15:55 +00001637 // our layout algorithm to proceed as expected while not writing out the gap
1638 // at the start.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001639 if (!OrderedSegments.empty()) {
1640 auto Seg = OrderedSegments[0];
1641 auto Sec = Seg->firstSection();
1642 auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1643 Seg->OriginalOffset += Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001644 // The size needs to be shrunk as well.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001645 Seg->FileSize -= Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001646 // The PAddr needs to be increased to remove the gap before the first
1647 // section.
1648 Seg->PAddr += Diff;
1649 uint64_t LowestPAddr = Seg->PAddr;
1650 for (auto &Segment : OrderedSegments) {
1651 Segment->Offset = Segment->PAddr - LowestPAddr;
1652 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1653 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001654 }
1655
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001656 // TODO: generalize LayoutSections to take a range. Pass a special range
1657 // constructed from an iterator that skips values for which a predicate does
1658 // not hold. Then pass such a range to LayoutSections instead of constructing
1659 // AllocatedSections here.
1660 std::vector<SectionBase *> AllocatedSections;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001661 for (auto &Section : Obj.sections()) {
1662 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001663 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001664 AllocatedSections.push_back(&Section);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001665 }
Fangrui Song32a34e62018-11-01 16:02:12 +00001666 layoutSections(make_pointee_range(AllocatedSections), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001667
1668 // Now that every section has been laid out we just need to compute the total
1669 // file size. This might not be the same as the offset returned by
1670 // LayoutSections, because we want to truncate the last segment to the end of
1671 // its last section, to match GNU objcopy's behaviour.
1672 TotalSize = 0;
1673 for (const auto &Section : AllocatedSections) {
1674 if (Section->Type != SHT_NOBITS)
1675 TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1676 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001677
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001678 if (Error E = Buf.allocate(TotalSize))
1679 return E;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001680 SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001681 return Error::success();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001682}
1683
Jake Ehrlich76e91102018-01-25 22:46:17 +00001684template class ELFBuilder<ELF64LE>;
1685template class ELFBuilder<ELF64BE>;
1686template class ELFBuilder<ELF32LE>;
1687template class ELFBuilder<ELF32BE>;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001688
Jake Ehrlich76e91102018-01-25 22:46:17 +00001689template class ELFWriter<ELF64LE>;
1690template class ELFWriter<ELF64BE>;
1691template class ELFWriter<ELF32LE>;
1692template class ELFWriter<ELF32BE>;
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001693
1694} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001695} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001696} // end namespace llvm