blob: 979ab0a2163302d6d0a9cd0b4fc4fcbbe07247f7 [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"
Jordan Rupprecht971d47622019-02-01 15:20:36 +000020#include "llvm/Support/Errc.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>
Jordan Rupprecht52d57812019-02-21 16:45:42 +000028#include <unordered_set>
Eugene Zelenko0ad18f82017-11-01 21:16:06 +000029#include <utility>
30#include <vector>
Petr Hosek05a04cb2017-08-01 00:33:58 +000031
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +000032namespace llvm {
33namespace objcopy {
34namespace elf {
35
Petr Hosek05a04cb2017-08-01 00:33:58 +000036using namespace object;
37using namespace ELF;
38
Jake Ehrlich76e91102018-01-25 22:46:17 +000039template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000040 uint8_t *B = Buf.getBufferStart();
41 B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
42 Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000043 Phdr.p_type = Seg.Type;
44 Phdr.p_flags = Seg.Flags;
45 Phdr.p_offset = Seg.Offset;
46 Phdr.p_vaddr = Seg.VAddr;
47 Phdr.p_paddr = Seg.PAddr;
48 Phdr.p_filesz = Seg.FileSize;
49 Phdr.p_memsz = Seg.MemSize;
50 Phdr.p_align = Seg.Align;
Petr Hosekc4df10e2017-08-04 21:09:26 +000051}
52
Jordan Rupprecht52d57812019-02-21 16:45:42 +000053Error SectionBase::removeSectionReferences(
54 function_ref<bool(const SectionBase *)> ToRemove) {
Jordan Rupprecht971d47622019-02-01 15:20:36 +000055 return Error::success();
56}
57
58Error SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
59 return Error::success();
60}
61
Jake Ehrlichf5a43772017-09-25 20:37:28 +000062void SectionBase::initialize(SectionTableRef SecTable) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000063void SectionBase::finalize() {}
Paul Semel99dda0b2018-05-25 11:01:25 +000064void SectionBase::markSymbols() {}
George Rimard8a5c6c2019-03-11 11:01:24 +000065void SectionBase::replaceSectionReferences(
66 const DenseMap<SectionBase *, SectionBase *> &) {}
Petr Hosek05a04cb2017-08-01 00:33:58 +000067
Jake Ehrlich76e91102018-01-25 22:46:17 +000068template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +000069 uint8_t *B = Buf.getBufferStart();
70 B += Sec.HeaderOffset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +000071 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Jake Ehrlich76e91102018-01-25 22:46:17 +000072 Shdr.sh_name = Sec.NameIndex;
73 Shdr.sh_type = Sec.Type;
74 Shdr.sh_flags = Sec.Flags;
75 Shdr.sh_addr = Sec.Addr;
76 Shdr.sh_offset = Sec.Offset;
77 Shdr.sh_size = Sec.Size;
78 Shdr.sh_link = Sec.Link;
79 Shdr.sh_info = Sec.Info;
80 Shdr.sh_addralign = Sec.Align;
81 Shdr.sh_entsize = Sec.EntrySize;
Petr Hosek05a04cb2017-08-01 00:33:58 +000082}
83
Jordan Rupprecht1f821762019-01-03 17:45:30 +000084template <class ELFT> void ELFSectionSizer<ELFT>::visit(Section &Sec) {}
85
86template <class ELFT>
87void ELFSectionSizer<ELFT>::visit(OwnedDataSection &Sec) {}
88
89template <class ELFT>
90void ELFSectionSizer<ELFT>::visit(StringTableSection &Sec) {}
91
92template <class ELFT>
93void ELFSectionSizer<ELFT>::visit(DynamicRelocationSection &Sec) {}
94
95template <class ELFT>
96void ELFSectionSizer<ELFT>::visit(SymbolTableSection &Sec) {
97 Sec.EntrySize = sizeof(Elf_Sym);
98 Sec.Size = Sec.Symbols.size() * Sec.EntrySize;
Jordan Rupprecht78213c7e2019-01-03 17:51:32 +000099 // Align to the largest field in Elf_Sym.
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000100 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000101}
102
103template <class ELFT>
104void ELFSectionSizer<ELFT>::visit(RelocationSection &Sec) {
105 Sec.EntrySize = Sec.Type == SHT_REL ? sizeof(Elf_Rel) : sizeof(Elf_Rela);
106 Sec.Size = Sec.Relocations.size() * Sec.EntrySize;
Jordan Rupprecht78213c7e2019-01-03 17:51:32 +0000107 // Align to the largest field in Elf_Rel(a).
Jordan Rupprecht415dc5d2019-01-03 19:09:00 +0000108 Sec.Align = ELFT::Is64Bits ? sizeof(Elf_Xword) : sizeof(Elf_Word);
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000109}
110
111template <class ELFT>
112void ELFSectionSizer<ELFT>::visit(GnuDebugLinkSection &Sec) {}
113
114template <class ELFT> void ELFSectionSizer<ELFT>::visit(GroupSection &Sec) {}
115
116template <class ELFT>
117void ELFSectionSizer<ELFT>::visit(SectionIndexSection &Sec) {}
118
119template <class ELFT>
120void ELFSectionSizer<ELFT>::visit(CompressedSection &Sec) {}
121
122template <class ELFT>
123void ELFSectionSizer<ELFT>::visit(DecompressedSection &Sec) {}
Jake Ehrlich76e91102018-01-25 22:46:17 +0000124
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000125void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
126 error("Cannot write symbol section index table '" + Sec.Name + "' ");
127}
128
Jake Ehrlich76e91102018-01-25 22:46:17 +0000129void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
130 error("Cannot write symbol table '" + Sec.Name + "' out to binary");
131}
132
133void BinarySectionWriter::visit(const RelocationSection &Sec) {
134 error("Cannot write relocation section '" + Sec.Name + "' out to binary");
135}
136
137void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000138 error("Cannot write '" + Sec.Name + "' out to binary");
139}
140
141void BinarySectionWriter::visit(const GroupSection &Sec) {
142 error("Cannot write '" + Sec.Name + "' out to binary");
Jake Ehrlich76e91102018-01-25 22:46:17 +0000143}
144
145void SectionWriter::visit(const Section &Sec) {
146 if (Sec.Type == SHT_NOBITS)
Petr Hosek05a04cb2017-08-01 00:33:58 +0000147 return;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000148 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Fangrui Song75709322018-11-17 01:44:25 +0000149 llvm::copy(Sec.Contents, Buf);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000150}
151
Jake Ehrlich76e91102018-01-25 22:46:17 +0000152void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
153
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000154void Section::accept(MutableSectionVisitor &Visitor) { Visitor.visit(*this); }
155
Jake Ehrlich76e91102018-01-25 22:46:17 +0000156void SectionWriter::visit(const OwnedDataSection &Sec) {
157 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Fangrui Song75709322018-11-17 01:44:25 +0000158 llvm::copy(Sec.Data, Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000159}
160
Puyan Lotfiaf048642018-10-01 10:29:41 +0000161static const std::vector<uint8_t> ZlibGnuMagic = {'Z', 'L', 'I', 'B'};
162
163static bool isDataGnuCompressed(ArrayRef<uint8_t> Data) {
164 return Data.size() > ZlibGnuMagic.size() &&
165 std::equal(ZlibGnuMagic.begin(), ZlibGnuMagic.end(), Data.data());
166}
167
168template <class ELFT>
169static std::tuple<uint64_t, uint64_t>
170getDecompressedSizeAndAlignment(ArrayRef<uint8_t> Data) {
171 const bool IsGnuDebug = isDataGnuCompressed(Data);
172 const uint64_t DecompressedSize =
173 IsGnuDebug
Fangrui Song5ed0a8b2019-03-29 08:08:20 +0000174 ? support::endian::read64be(Data.data() + ZlibGnuMagic.size())
Puyan Lotfiaf048642018-10-01 10:29:41 +0000175 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())->ch_size;
176 const uint64_t DecompressedAlign =
177 IsGnuDebug ? 1
178 : reinterpret_cast<const Elf_Chdr_Impl<ELFT> *>(Data.data())
179 ->ch_addralign;
180
181 return std::make_tuple(DecompressedSize, DecompressedAlign);
182}
183
184template <class ELFT>
185void ELFSectionWriter<ELFT>::visit(const DecompressedSection &Sec) {
Puyan Lotfiaf048642018-10-01 10:29:41 +0000186 const size_t DataOffset = isDataGnuCompressed(Sec.OriginalData)
187 ? (ZlibGnuMagic.size() + sizeof(Sec.Size))
188 : sizeof(Elf_Chdr_Impl<ELFT>);
189
190 StringRef CompressedContent(
191 reinterpret_cast<const char *>(Sec.OriginalData.data()) + DataOffset,
192 Sec.OriginalData.size() - DataOffset);
193
194 SmallVector<char, 128> DecompressedContent;
195 if (Error E = zlib::uncompress(CompressedContent, DecompressedContent,
196 static_cast<size_t>(Sec.Size)))
197 reportError(Sec.Name, std::move(E));
198
George Rimar281a5be2019-03-06 14:12:18 +0000199 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Puyan Lotfiaf048642018-10-01 10:29:41 +0000200 std::copy(DecompressedContent.begin(), DecompressedContent.end(), Buf);
201}
202
203void BinarySectionWriter::visit(const DecompressedSection &Sec) {
204 error("Cannot write compressed section '" + Sec.Name + "' ");
205}
206
207void DecompressedSection::accept(SectionVisitor &Visitor) const {
208 Visitor.visit(*this);
209}
210
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000211void DecompressedSection::accept(MutableSectionVisitor &Visitor) {
212 Visitor.visit(*this);
213}
214
Jake Ehrlich76e91102018-01-25 22:46:17 +0000215void OwnedDataSection::accept(SectionVisitor &Visitor) const {
216 Visitor.visit(*this);
Jake Ehrliche8437de2017-12-19 00:47:30 +0000217}
218
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000219void OwnedDataSection::accept(MutableSectionVisitor &Visitor) {
220 Visitor.visit(*this);
221}
222
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000223void BinarySectionWriter::visit(const CompressedSection &Sec) {
224 error("Cannot write compressed section '" + Sec.Name + "' ");
225}
226
227template <class ELFT>
228void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) {
229 uint8_t *Buf = Out.getBufferStart();
230 Buf += Sec.Offset;
231
232 if (Sec.CompressionType == DebugCompressionType::None) {
233 std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf);
234 return;
235 }
236
237 if (Sec.CompressionType == DebugCompressionType::GNU) {
238 const char *Magic = "ZLIB";
239 memcpy(Buf, Magic, strlen(Magic));
240 Buf += strlen(Magic);
241 const uint64_t DecompressedSize =
242 support::endian::read64be(&Sec.DecompressedSize);
243 memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize));
244 Buf += sizeof(DecompressedSize);
245 } else {
246 Elf_Chdr_Impl<ELFT> Chdr;
247 Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB;
248 Chdr.ch_size = Sec.DecompressedSize;
249 Chdr.ch_addralign = Sec.DecompressedAlign;
250 memcpy(Buf, &Chdr, sizeof(Chdr));
251 Buf += sizeof(Chdr);
252 }
253
254 std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf);
255}
256
257CompressedSection::CompressedSection(const SectionBase &Sec,
258 DebugCompressionType CompressionType)
259 : SectionBase(Sec), CompressionType(CompressionType),
260 DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) {
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000261 if (Error E = zlib::compress(
262 StringRef(reinterpret_cast<const char *>(OriginalData.data()),
263 OriginalData.size()),
264 CompressedData))
265 reportError(Name, std::move(E));
266
267 size_t ChdrSize;
268 if (CompressionType == DebugCompressionType::GNU) {
269 Name = ".z" + Sec.Name.substr(1);
270 ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t);
271 } else {
272 Flags |= ELF::SHF_COMPRESSED;
273 ChdrSize =
274 std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>),
275 sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)),
276 std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>),
277 sizeof(object::Elf_Chdr_Impl<object::ELF32BE>)));
278 }
279 Size = ChdrSize + CompressedData.size();
280 Align = 8;
281}
282
Puyan Lotfiaf048642018-10-01 10:29:41 +0000283CompressedSection::CompressedSection(ArrayRef<uint8_t> CompressedData,
284 uint64_t DecompressedSize,
285 uint64_t DecompressedAlign)
286 : CompressionType(DebugCompressionType::None),
287 DecompressedSize(DecompressedSize), DecompressedAlign(DecompressedAlign) {
288 OriginalData = CompressedData;
289}
290
Puyan Lotfi99124cc2018-09-07 08:10:22 +0000291void CompressedSection::accept(SectionVisitor &Visitor) const {
292 Visitor.visit(*this);
293}
294
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000295void CompressedSection::accept(MutableSectionVisitor &Visitor) {
296 Visitor.visit(*this);
297}
298
George Rimarfaf308b2019-03-18 14:27:41 +0000299void StringTableSection::addString(StringRef Name) { StrTabBuilder.add(Name); }
Petr Hosek05a04cb2017-08-01 00:33:58 +0000300
301uint32_t StringTableSection::findIndex(StringRef Name) const {
302 return StrTabBuilder.getOffset(Name);
303}
304
George Rimarfaf308b2019-03-18 14:27:41 +0000305void StringTableSection::prepareForLayout() {
306 StrTabBuilder.finalize();
307 Size = StrTabBuilder.getSize();
308}
Petr Hosek05a04cb2017-08-01 00:33:58 +0000309
Jake Ehrlich76e91102018-01-25 22:46:17 +0000310void SectionWriter::visit(const StringTableSection &Sec) {
311 Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
312}
313
314void StringTableSection::accept(SectionVisitor &Visitor) const {
315 Visitor.visit(*this);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000316}
317
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000318void StringTableSection::accept(MutableSectionVisitor &Visitor) {
319 Visitor.visit(*this);
320}
321
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000322template <class ELFT>
323void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
324 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000325 auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf);
Fangrui Song75709322018-11-17 01:44:25 +0000326 llvm::copy(Sec.Indexes, IndexesBuffer);
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000327}
328
329void SectionIndexSection::initialize(SectionTableRef SecTable) {
330 Size = 0;
331 setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
332 Link,
333 "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
334 "Link field value " + Twine(Link) + " in section " + Name +
335 " is not a symbol table"));
336 Symbols->setShndxTable(this);
337}
338
339void SectionIndexSection::finalize() { Link = Symbols->Index; }
340
341void SectionIndexSection::accept(SectionVisitor &Visitor) const {
342 Visitor.visit(*this);
343}
344
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000345void SectionIndexSection::accept(MutableSectionVisitor &Visitor) {
346 Visitor.visit(*this);
347}
348
Petr Hosekc1135772017-09-13 03:04:50 +0000349static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000350 switch (Index) {
351 case SHN_ABS:
352 case SHN_COMMON:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000353 return true;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000354 }
Petr Hosekc1135772017-09-13 03:04:50 +0000355 if (Machine == EM_HEXAGON) {
356 switch (Index) {
357 case SHN_HEXAGON_SCOMMON:
358 case SHN_HEXAGON_SCOMMON_2:
359 case SHN_HEXAGON_SCOMMON_4:
360 case SHN_HEXAGON_SCOMMON_8:
361 return true;
362 }
363 }
364 return false;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000365}
366
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000367// Large indexes force us to clarify exactly what this function should do. This
368// function should return the value that will appear in st_shndx when written
369// out.
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000370uint16_t Symbol::getShndx() const {
371 if (DefinedIn != nullptr) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000372 if (DefinedIn->Index >= SHN_LORESERVE)
373 return SHN_XINDEX;
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000374 return DefinedIn->Index;
375 }
376 switch (ShndxType) {
377 // This means that we don't have a defined section but we do need to
378 // output a legitimate section index.
379 case SYMBOL_SIMPLE_INDEX:
380 return SHN_UNDEF;
381 case SYMBOL_ABS:
382 case SYMBOL_COMMON:
383 case SYMBOL_HEXAGON_SCOMMON:
384 case SYMBOL_HEXAGON_SCOMMON_2:
385 case SYMBOL_HEXAGON_SCOMMON_4:
386 case SYMBOL_HEXAGON_SCOMMON_8:
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000387 case SYMBOL_XINDEX:
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000388 return static_cast<uint16_t>(ShndxType);
389 }
390 llvm_unreachable("Symbol with invalid ShndxType encountered");
391}
392
Jordan Rupprechtb47475c2018-11-01 17:26:36 +0000393bool Symbol::isCommon() const { return getShndx() == SHN_COMMON; }
394
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000395void SymbolTableSection::assignIndices() {
396 uint32_t Index = 0;
397 for (auto &Sym : Symbols)
398 Sym->Index = Index++;
399}
400
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000401void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type,
Petr Hosek79cee9e2017-08-29 02:12:03 +0000402 SectionBase *DefinedIn, uint64_t Value,
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000403 uint8_t Visibility, uint16_t Shndx,
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000404 uint64_t Size) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000405 Symbol Sym;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000406 Sym.Name = Name.str();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000407 Sym.Binding = Bind;
408 Sym.Type = Type;
409 Sym.DefinedIn = DefinedIn;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000410 if (DefinedIn != nullptr)
411 DefinedIn->HasSymbol = true;
Jake Ehrlich8b831c12018-03-07 20:33:02 +0000412 if (DefinedIn == nullptr) {
413 if (Shndx >= SHN_LORESERVE)
414 Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
415 else
416 Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
417 }
Petr Hosek79cee9e2017-08-29 02:12:03 +0000418 Sym.Value = Value;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000419 Sym.Visibility = Visibility;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000420 Sym.Size = Size;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000421 Sym.Index = Symbols.size();
422 Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
423 Size += this->EntrySize;
424}
425
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000426Error SymbolTableSection::removeSectionReferences(
427 function_ref<bool(const SectionBase *)> ToRemove) {
428 if (ToRemove(SectionIndexTable))
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000429 SectionIndexTable = nullptr;
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000430 if (ToRemove(SymbolNames))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000431 return createStringError(llvm::errc::invalid_argument,
432 "String table %s cannot be removed because it is "
433 "referenced by the symbol table %s",
434 SymbolNames->Name.data(), this->Name.data());
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000435 return removeSymbols(
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000436 [ToRemove](const Symbol &Sym) { return ToRemove(Sym.DefinedIn); });
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
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000448Error SymbolTableSection::removeSymbols(
Paul Semel4246a462018-05-09 21:36:54 +0000449 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();
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000456 return Error::success();
Paul Semel41695f82018-05-02 20:19:22 +0000457}
458
George Rimar0373bed2019-03-20 13:57:47 +0000459void SymbolTableSection::replaceSectionReferences(
460 const DenseMap<SectionBase *, SectionBase *> &FromTo) {
461 for (std::unique_ptr<Symbol> &Sym : Symbols)
462 if (SectionBase *To = FromTo.lookup(Sym->DefinedIn))
463 Sym->DefinedIn = To;
464}
465
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000466void SymbolTableSection::initialize(SectionTableRef SecTable) {
467 Size = 0;
468 setStrTab(SecTable.getSectionOfType<StringTableSection>(
469 Link,
470 "Symbol table has link index of " + Twine(Link) +
471 " which is not a valid index",
472 "Symbol table has link index of " + Twine(Link) +
473 " which is not a string table"));
474}
475
Petr Hosek79cee9e2017-08-29 02:12:03 +0000476void SymbolTableSection::finalize() {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000477 uint32_t MaxLocalIndex = 0;
478 for (auto &Sym : Symbols) {
479 Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
480 if (Sym->Binding == STB_LOCAL)
481 MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
482 }
483 // Now we need to set the Link and Info fields.
484 Link = SymbolNames->Index;
485 Info = MaxLocalIndex + 1;
486}
487
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +0000488void SymbolTableSection::prepareForLayout() {
Eugene Leviant88089fe2019-04-12 11:59:30 +0000489 // Reserve proper amount of space in section index table, so we can
490 // layout sections correctly. We will fill the table with correct
491 // indexes later in fillShdnxTable.
492 if (SectionIndexTable)
493 SectionIndexTable->reserve(Symbols.size());
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
Eugene Leviant88089fe2019-04-12 11:59:30 +0000500void SymbolTableSection::fillShndxTable() {
501 if (SectionIndexTable == nullptr)
502 return;
503 // Fill section index table with real section indexes. This function must
504 // be called after assignOffsets.
505 for (const auto &Sym : Symbols) {
506 if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
507 SectionIndexTable->addIndex(Sym->DefinedIn->Index);
508 else
509 SectionIndexTable->addIndex(SHN_UNDEF);
510 }
511}
512
Petr Hosek79cee9e2017-08-29 02:12:03 +0000513const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
514 if (Symbols.size() <= Index)
515 error("Invalid symbol index: " + Twine(Index));
516 return Symbols[Index].get();
517}
518
Paul Semel99dda0b2018-05-25 11:01:25 +0000519Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
520 return const_cast<Symbol *>(
521 static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
522}
523
Petr Hosek79cee9e2017-08-29 02:12:03 +0000524template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000525void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000526 uint8_t *Buf = Out.getBufferStart();
Jake Ehrlich76e91102018-01-25 22:46:17 +0000527 Buf += Sec.Offset;
Jordan Rupprechtde965ea2018-08-10 16:25:58 +0000528 Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf);
Petr Hosek79cee9e2017-08-29 02:12:03 +0000529 // Loop though symbols setting each entry of the symbol table.
Jake Ehrlich76e91102018-01-25 22:46:17 +0000530 for (auto &Symbol : Sec.Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +0000531 Sym->st_name = Symbol->NameIndex;
532 Sym->st_value = Symbol->Value;
533 Sym->st_size = Symbol->Size;
Jake Ehrlich30d927a2018-01-02 23:01:24 +0000534 Sym->st_other = Symbol->Visibility;
Petr Hosek79cee9e2017-08-29 02:12:03 +0000535 Sym->setBinding(Symbol->Binding);
536 Sym->setType(Symbol->Type);
Petr Hosekec2b3fc2017-09-07 23:02:50 +0000537 Sym->st_shndx = Symbol->getShndx();
Petr Hosek79cee9e2017-08-29 02:12:03 +0000538 ++Sym;
539 }
540}
541
Jake Ehrlich76e91102018-01-25 22:46:17 +0000542void SymbolTableSection::accept(SectionVisitor &Visitor) const {
543 Visitor.visit(*this);
544}
545
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000546void SymbolTableSection::accept(MutableSectionVisitor &Visitor) {
547 Visitor.visit(*this);
548}
549
George Rimar79fb8582019-02-27 11:18:27 +0000550Error RelocationSection::removeSectionReferences(
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000551 function_ref<bool(const SectionBase *)> ToRemove) {
552 if (ToRemove(Symbols))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000553 return createStringError(llvm::errc::invalid_argument,
554 "Symbol table %s cannot be removed because it is "
555 "referenced by the relocation section %s.",
556 Symbols->Name.data(), this->Name.data());
George Rimar79fb8582019-02-27 11:18:27 +0000557
558 for (const Relocation &R : Relocations) {
559 if (!R.RelocSymbol->DefinedIn || !ToRemove(R.RelocSymbol->DefinedIn))
560 continue;
George Rimarbf447a52019-02-28 08:21:50 +0000561 return createStringError(llvm::errc::invalid_argument,
562 "Section %s can't be removed: (%s+0x%" PRIx64
563 ") has relocation against symbol '%s'",
564 R.RelocSymbol->DefinedIn->Name.data(),
565 SecToApplyRel->Name.data(), R.Offset,
566 R.RelocSymbol->Name.c_str());
George Rimar79fb8582019-02-27 11:18:27 +0000567 }
568
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000569 return Error::success();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000570}
571
572template <class SymTabType>
573void RelocSectionWithSymtabBase<SymTabType>::initialize(
574 SectionTableRef SecTable) {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000575 if (Link != SHN_UNDEF)
576 setSymTab(SecTable.getSectionOfType<SymTabType>(
577 Link,
578 "Link field value " + Twine(Link) + " in section " + Name +
579 " is invalid",
580 "Link field value " + Twine(Link) + " in section " + Name +
581 " is not a symbol table"));
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000582
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000583 if (Info != SHN_UNDEF)
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000584 setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
585 " in section " + Name +
586 " is invalid"));
James Y Knight2ea995a2017-09-26 22:44:01 +0000587 else
588 setSection(nullptr);
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000589}
590
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000591template <class SymTabType>
592void RelocSectionWithSymtabBase<SymTabType>::finalize() {
Jordan Rupprechtec277a82018-09-04 22:28:49 +0000593 this->Link = Symbols ? Symbols->Index : 0;
594
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000595 if (SecToApplyRel != nullptr)
596 this->Info = SecToApplyRel->Index;
Petr Hosekd7df9b22017-09-06 23:41:02 +0000597}
598
599template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000600static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
Petr Hosekd7df9b22017-09-06 23:41:02 +0000601
602template <class ELFT>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000603static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000604 Rela.r_addend = Addend;
605}
606
Jake Ehrlich76e91102018-01-25 22:46:17 +0000607template <class RelRange, class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +0000608static void writeRel(const RelRange &Relocations, T *Buf) {
Petr Hosekd7df9b22017-09-06 23:41:02 +0000609 for (const auto &Reloc : Relocations) {
610 Buf->r_offset = Reloc.Offset;
611 setAddend(*Buf, Reloc.Addend);
612 Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
613 ++Buf;
614 }
615}
616
617template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000618void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
619 uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
620 if (Sec.Type == SHT_REL)
621 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000622 else
Jake Ehrlich76e91102018-01-25 22:46:17 +0000623 writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
Petr Hosekd7df9b22017-09-06 23:41:02 +0000624}
625
Jake Ehrlich76e91102018-01-25 22:46:17 +0000626void RelocationSection::accept(SectionVisitor &Visitor) const {
627 Visitor.visit(*this);
628}
629
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000630void RelocationSection::accept(MutableSectionVisitor &Visitor) {
631 Visitor.visit(*this);
632}
633
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000634Error RelocationSection::removeSymbols(
Paul Semel4246a462018-05-09 21:36:54 +0000635 function_ref<bool(const Symbol &)> ToRemove) {
636 for (const Relocation &Reloc : Relocations)
637 if (ToRemove(*Reloc.RelocSymbol))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000638 return createStringError(
639 llvm::errc::invalid_argument,
640 "not stripping symbol '%s' because it is named in a relocation.",
641 Reloc.RelocSymbol->Name.data());
642 return Error::success();
Paul Semel4246a462018-05-09 21:36:54 +0000643}
644
Paul Semel99dda0b2018-05-25 11:01:25 +0000645void RelocationSection::markSymbols() {
646 for (const Relocation &Reloc : Relocations)
647 Reloc.RelocSymbol->Referenced = true;
648}
649
George Rimard8a5c6c2019-03-11 11:01:24 +0000650void RelocationSection::replaceSectionReferences(
651 const DenseMap<SectionBase *, SectionBase *> &FromTo) {
652 // Update the target section if it was replaced.
653 if (SectionBase *To = FromTo.lookup(SecToApplyRel))
654 SecToApplyRel = To;
George Rimard8a5c6c2019-03-11 11:01:24 +0000655}
656
Jake Ehrlich76e91102018-01-25 22:46:17 +0000657void SectionWriter::visit(const DynamicRelocationSection &Sec) {
Fangrui Song75709322018-11-17 01:44:25 +0000658 llvm::copy(Sec.Contents,
Jake Ehrlich76e91102018-01-25 22:46:17 +0000659 Out.getBufferStart() + Sec.Offset);
660}
661
662void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
663 Visitor.visit(*this);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +0000664}
665
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000666void DynamicRelocationSection::accept(MutableSectionVisitor &Visitor) {
667 Visitor.visit(*this);
668}
669
Jordan Rupprecht52d57812019-02-21 16:45:42 +0000670Error Section::removeSectionReferences(
671 function_ref<bool(const SectionBase *)> ToRemove) {
672 if (ToRemove(LinkSection))
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000673 return createStringError(llvm::errc::invalid_argument,
674 "Section %s cannot be removed because it is "
675 "referenced by the section %s",
676 LinkSection->Name.data(), this->Name.data());
677 return Error::success();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +0000678}
679
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000680void GroupSection::finalize() {
681 this->Info = Sym->Index;
682 this->Link = SymTab->Index;
683}
684
Jordan Rupprecht971d47622019-02-01 15:20:36 +0000685Error GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
686 if (ToRemove(*Sym))
687 return createStringError(llvm::errc::invalid_argument,
688 "Symbol %s cannot be removed because it is "
689 "referenced by the section %s[%d].",
690 Sym->Name.data(), this->Name.data(), this->Index);
691 return Error::success();
Paul Semel4246a462018-05-09 21:36:54 +0000692}
693
Paul Semel99dda0b2018-05-25 11:01:25 +0000694void GroupSection::markSymbols() {
695 if (Sym)
696 Sym->Referenced = true;
697}
698
George Rimar27257172019-03-24 14:41:45 +0000699void GroupSection::replaceSectionReferences(
700 const DenseMap<SectionBase *, SectionBase *> &FromTo) {
701 for (SectionBase *&Sec : GroupMembers)
702 if (SectionBase *To = FromTo.lookup(Sec))
703 Sec = To;
704}
705
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000706void Section::initialize(SectionTableRef SecTable) {
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000707 if (Link != ELF::SHN_UNDEF) {
Alexander Shaposhnikov52db4332018-04-20 20:46:04 +0000708 LinkSection =
709 SecTable.getSection(Link, "Link field value " + Twine(Link) +
710 " in section " + Name + " is invalid");
Peter Collingbourne1651ac12018-05-30 19:30:39 +0000711 if (LinkSection->Type == ELF::SHT_SYMTAB)
712 LinkSection = nullptr;
713 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +0000714}
715
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +0000716void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
Jake Ehrlichf5a43772017-09-25 20:37:28 +0000717
Jake Ehrlich76e91102018-01-25 22:46:17 +0000718void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
Alexander Richardson6c859922018-02-19 19:53:44 +0000719 FileName = sys::path::filename(File);
720 // The format for the .gnu_debuglink starts with the file name and is
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000721 // followed by a null terminator and then the CRC32 of the file. The CRC32
722 // should be 4 byte aligned. So we add the FileName size, a 1 for the null
723 // byte, and then finally push the size to alignment and add 4.
724 Size = alignTo(FileName.size() + 1, 4) + 4;
725 // The CRC32 will only be aligned if we align the whole section.
726 Align = 4;
727 Type = ELF::SHT_PROGBITS;
728 Name = ".gnu_debuglink";
729 // For sections not found in segments, OriginalOffset is only used to
730 // establish the order that sections should go in. By using the maximum
731 // possible offset we cause this section to wind up at the end.
732 OriginalOffset = std::numeric_limits<uint64_t>::max();
Fangrui Song32a34e62018-11-01 16:02:12 +0000733 JamCRC CRC;
734 CRC.update(ArrayRef<char>(Data.data(), Data.size()));
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000735 // The CRC32 value needs to be complemented because the JamCRC dosn't
736 // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
737 // but it starts by default at 0xFFFFFFFF which is the complement of zero.
Fangrui Song32a34e62018-11-01 16:02:12 +0000738 CRC32 = ~CRC.getCRC();
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000739}
740
Jake Ehrlich76e91102018-01-25 22:46:17 +0000741GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000742 // Read in the file to compute the CRC of it.
743 auto DebugOrErr = MemoryBuffer::getFile(File);
744 if (!DebugOrErr)
745 error("'" + File + "': " + DebugOrErr.getError().message());
746 auto Debug = std::move(*DebugOrErr);
747 init(File, Debug->getBuffer());
748}
749
750template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +0000751void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
Fangrui Song5ed0a8b2019-03-29 08:08:20 +0000752 unsigned char *Buf = Out.getBufferStart() + Sec.Offset;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000753 Elf_Word *CRC =
754 reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
755 *CRC = Sec.CRC32;
Fangrui Song5ed0a8b2019-03-29 08:08:20 +0000756 llvm::copy(Sec.FileName, Buf);
Jake Ehrlich76e91102018-01-25 22:46:17 +0000757}
758
759void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
760 Visitor.visit(*this);
Jake Ehrlichea07d3c2018-01-25 22:15:14 +0000761}
762
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000763void GnuDebugLinkSection::accept(MutableSectionVisitor &Visitor) {
764 Visitor.visit(*this);
765}
766
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000767template <class ELFT>
768void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
769 ELF::Elf32_Word *Buf =
770 reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
771 *Buf++ = Sec.FlagWord;
772 for (const auto *S : Sec.GroupMembers)
773 support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
774}
775
776void GroupSection::accept(SectionVisitor &Visitor) const {
777 Visitor.visit(*this);
778}
779
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000780void GroupSection::accept(MutableSectionVisitor &Visitor) {
781 Visitor.visit(*this);
782}
783
Petr Hosek05a04cb2017-08-01 00:33:58 +0000784// Returns true IFF a section is wholly inside the range of a segment
785static bool sectionWithinSegment(const SectionBase &Section,
786 const Segment &Segment) {
787 // If a section is empty it should be treated like it has a size of 1. This is
788 // to clarify the case when an empty section lies on a boundary between two
789 // segments and ensures that the section "belongs" to the second segment and
790 // not the first.
791 uint64_t SecSize = Section.Size ? Section.Size : 1;
792 return Segment.Offset <= Section.OriginalOffset &&
793 Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
794}
795
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000796// Returns true IFF a segment's original offset is inside of another segment's
797// range.
798static bool segmentOverlapsSegment(const Segment &Child,
799 const Segment &Parent) {
800
801 return Parent.OriginalOffset <= Child.OriginalOffset &&
802 Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
803}
804
Jake Ehrlich46814be2018-01-22 19:27:30 +0000805static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +0000806 // Any segment without a parent segment should come before a segment
807 // that has a parent segment.
808 if (A->OriginalOffset < B->OriginalOffset)
809 return true;
810 if (A->OriginalOffset > B->OriginalOffset)
811 return false;
812 return A->Index < B->Index;
813}
814
Jake Ehrlich46814be2018-01-22 19:27:30 +0000815static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
816 if (A->PAddr < B->PAddr)
817 return true;
818 if (A->PAddr > B->PAddr)
819 return false;
820 return A->Index < B->Index;
821}
822
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000823void BinaryELFBuilder::initFileHeader() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000824 Obj->Flags = 0x0;
825 Obj->Type = ET_REL;
George Rimar3ac20a92018-12-20 10:59:52 +0000826 Obj->OSABI = ELFOSABI_NONE;
George Rimar4ded7732018-12-20 10:51:42 +0000827 Obj->ABIVersion = 0;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000828 Obj->Entry = 0x0;
829 Obj->Machine = EMachine;
830 Obj->Version = 1;
831}
832
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000833void BinaryELFBuilder::initHeaderSegment() { Obj->ElfHdrSegment.Index = 0; }
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000834
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000835StringTableSection *BinaryELFBuilder::addStrTab() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000836 auto &StrTab = Obj->addSection<StringTableSection>();
837 StrTab.Name = ".strtab";
838
839 Obj->SectionNames = &StrTab;
840 return &StrTab;
841}
842
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000843SymbolTableSection *BinaryELFBuilder::addSymTab(StringTableSection *StrTab) {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000844 auto &SymTab = Obj->addSection<SymbolTableSection>();
845
846 SymTab.Name = ".symtab";
847 SymTab.Link = StrTab->Index;
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000848
849 // The symbol table always needs a null symbol
850 SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0);
851
852 Obj->SymbolTable = &SymTab;
853 return &SymTab;
854}
855
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000856void BinaryELFBuilder::addData(SymbolTableSection *SymTab) {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000857 auto Data = ArrayRef<uint8_t>(
858 reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()),
859 MemBuf->getBufferSize());
860 auto &DataSection = Obj->addSection<Section>(Data);
861 DataSection.Name = ".data";
862 DataSection.Type = ELF::SHT_PROGBITS;
863 DataSection.Size = Data.size();
864 DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE;
865
866 std::string SanitizedFilename = MemBuf->getBufferIdentifier().str();
867 std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename),
Fangrui Song32a34e62018-11-01 16:02:12 +0000868 [](char C) { return !isalnum(C); }, '_');
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000869 Twine Prefix = Twine("_binary_") + SanitizedFilename;
870
871 SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection,
872 /*Value=*/0, STV_DEFAULT, 0, 0);
873 SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection,
874 /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0);
875 SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr,
876 /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0);
877}
878
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000879void BinaryELFBuilder::initSections() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000880 for (auto &Section : Obj->sections()) {
881 Section.initialize(Obj->sections());
882 }
883}
884
Jordan Rupprecht1f821762019-01-03 17:45:30 +0000885std::unique_ptr<Object> BinaryELFBuilder::build() {
Jordan Rupprechtcf676332018-08-17 18:51:11 +0000886 initFileHeader();
887 initHeaderSegment();
888 StringTableSection *StrTab = addStrTab();
889 SymbolTableSection *SymTab = addSymTab(StrTab);
890 initSections();
891 addData(SymTab);
892
893 return std::move(Obj);
894}
895
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000896template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
Jake Ehrlich6452b112018-02-14 23:31:33 +0000897 for (auto &Parent : Obj.segments()) {
898 // Every segment will overlap with itself but we don't want a segment to
899 // be it's own parent so we avoid that situation.
900 if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
901 // We want a canonical "most parental" segment but this requires
902 // inspecting the ParentSegment.
903 if (compareSegmentsByOffset(&Parent, &Child))
904 if (Child.ParentSegment == nullptr ||
905 compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
906 Child.ParentSegment = &Parent;
907 }
908 }
909 }
910}
911
Jake Ehrlich76e91102018-01-25 22:46:17 +0000912template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +0000913 uint32_t Index = 0;
914 for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
James Henderson1f448142019-03-25 16:36:26 +0000915 ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
916 (size_t)Phdr.p_filesz};
917 Segment &Seg = Obj.addSegment(Data);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000918 Seg.Type = Phdr.p_type;
919 Seg.Flags = Phdr.p_flags;
Petr Hosek3f383832017-08-26 01:32:20 +0000920 Seg.OriginalOffset = Phdr.p_offset;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000921 Seg.Offset = Phdr.p_offset;
922 Seg.VAddr = Phdr.p_vaddr;
923 Seg.PAddr = Phdr.p_paddr;
924 Seg.FileSize = Phdr.p_filesz;
925 Seg.MemSize = Phdr.p_memsz;
926 Seg.Align = Phdr.p_align;
927 Seg.Index = Index++;
Jake Ehrlich76e91102018-01-25 22:46:17 +0000928 for (auto &Section : Obj.sections()) {
929 if (sectionWithinSegment(Section, Seg)) {
930 Seg.addSection(&Section);
931 if (!Section.ParentSegment ||
932 Section.ParentSegment->Offset > Seg.Offset) {
933 Section.ParentSegment = &Seg;
Petr Hosek05a04cb2017-08-01 00:33:58 +0000934 }
935 }
936 }
937 }
Jake Ehrlich6452b112018-02-14 23:31:33 +0000938
939 auto &ElfHdr = Obj.ElfHdrSegment;
Jake Ehrlich6452b112018-02-14 23:31:33 +0000940 ElfHdr.Index = Index++;
941
942 const auto &Ehdr = *ElfFile.getHeader();
943 auto &PrHdr = Obj.ProgramHdrSegment;
944 PrHdr.Type = PT_PHDR;
945 PrHdr.Flags = 0;
946 // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
947 // Whereas this works automatically for ElfHdr, here OriginalOffset is
948 // always non-zero and to ensure the equation we assign the same value to
949 // VAddr as well.
950 PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
951 PrHdr.PAddr = 0;
952 PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000953 // The spec requires us to naturally align all the fields.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000954 PrHdr.Align = sizeof(Elf_Addr);
955 PrHdr.Index = Index++;
956
Jake Ehrlichd246b0a2017-09-19 21:37:35 +0000957 // Now we do an O(n^2) loop through the segments in order to match up
958 // segments.
Jake Ehrlich6452b112018-02-14 23:31:33 +0000959 for (auto &Child : Obj.segments())
960 setParentSegment(Child);
961 setParentSegment(ElfHdr);
962 setParentSegment(PrHdr);
Petr Hosek05a04cb2017-08-01 00:33:58 +0000963}
964
965template <class ELFT>
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000966void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
George Rimar0a5d4b82019-03-24 13:31:08 +0000967 if (GroupSec->Align % sizeof(ELF::Elf32_Word) != 0)
968 error("Invalid alignment " + Twine(GroupSec->Align) + " of group section " +
969 GroupSec->Name);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +0000970 auto SecTable = Obj.sections();
971 auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
972 GroupSec->Link,
973 "Link field value " + Twine(GroupSec->Link) + " in section " +
974 GroupSec->Name + " is invalid",
975 "Link field value " + Twine(GroupSec->Link) + " in section " +
976 GroupSec->Name + " is not a symbol table");
977 auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
978 if (!Sym)
979 error("Info field value " + Twine(GroupSec->Info) + " in section " +
980 GroupSec->Name + " is not a valid symbol index");
981 GroupSec->setSymTab(SymTab);
982 GroupSec->setSymbol(Sym);
983 if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
984 GroupSec->Contents.empty())
985 error("The content of the section " + GroupSec->Name + " is malformed");
986 const ELF::Elf32_Word *Word =
987 reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
988 const ELF::Elf32_Word *End =
989 Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
990 GroupSec->setFlagWord(*Word++);
991 for (; Word != End; ++Word) {
992 uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
993 GroupSec->addMember(SecTable.getSection(
994 Index, "Group member index " + Twine(Index) + " in section " +
995 GroupSec->Name + " is invalid"));
996 }
997}
998
999template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +00001000void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
Petr Hosek79cee9e2017-08-29 02:12:03 +00001001 const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
1002 StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001003 ArrayRef<Elf_Word> ShndxData;
Petr Hosek79cee9e2017-08-29 02:12:03 +00001004
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001005 auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
1006 for (const auto &Sym : Symbols) {
Petr Hosek79cee9e2017-08-29 02:12:03 +00001007 SectionBase *DefSection = nullptr;
1008 StringRef Name = unwrapOrError(Sym.getName(StrTabData));
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001009
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001010 if (Sym.st_shndx == SHN_XINDEX) {
1011 if (SymTab->getShndxTable() == nullptr)
1012 error("Symbol '" + Name +
1013 "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
1014 if (ShndxData.data() == nullptr) {
1015 const Elf_Shdr &ShndxSec =
1016 *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
1017 ShndxData = unwrapOrError(
1018 ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
1019 if (ShndxData.size() != Symbols.size())
1020 error("Symbol section index table does not have the same number of "
1021 "entries as the symbol table.");
1022 }
1023 Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
1024 DefSection = Obj.sections().getSection(
1025 Index,
Puyan Lotfi97604b42018-08-02 18:16:52 +00001026 "Symbol '" + Name + "' has invalid section index " + Twine(Index));
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001027 } else if (Sym.st_shndx >= SHN_LORESERVE) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001028 if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
Petr Hosekec2b3fc2017-09-07 23:02:50 +00001029 error(
1030 "Symbol '" + Name +
1031 "' has unsupported value greater than or equal to SHN_LORESERVE: " +
1032 Twine(Sym.st_shndx));
1033 }
1034 } else if (Sym.st_shndx != SHN_UNDEF) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001035 DefSection = Obj.sections().getSection(
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001036 Sym.st_shndx, "Symbol '" + Name +
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001037 "' is defined has invalid section index " +
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001038 Twine(Sym.st_shndx));
Petr Hosek79cee9e2017-08-29 02:12:03 +00001039 }
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001040
Petr Hosek79cee9e2017-08-29 02:12:03 +00001041 SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
Jake Ehrlich30d927a2018-01-02 23:01:24 +00001042 Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
Petr Hosek79cee9e2017-08-29 02:12:03 +00001043 }
1044}
1045
1046template <class ELFT>
Petr Hosekd7df9b22017-09-06 23:41:02 +00001047static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
1048
1049template <class ELFT>
1050static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
1051 ToSet = Rela.r_addend;
1052}
1053
Jake Ehrlich76e91102018-01-25 22:46:17 +00001054template <class T>
Puyan Lotfic4846a52018-07-16 22:17:05 +00001055static void initRelocations(RelocationSection *Relocs,
1056 SymbolTableSection *SymbolTable, T RelRange) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001057 for (const auto &Rel : RelRange) {
1058 Relocation ToAdd;
1059 ToAdd.Offset = Rel.r_offset;
1060 getAddend(ToAdd.Addend, Rel);
1061 ToAdd.Type = Rel.getType(false);
Paul Semel31a212d2018-05-22 01:04:36 +00001062 ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
Petr Hosekd7df9b22017-09-06 23:41:02 +00001063 Relocs->addRelocation(ToAdd);
1064 }
1065}
1066
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001067SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001068 if (Index == SHN_UNDEF || Index > Sections.size())
1069 error(ErrMsg);
1070 return Sections[Index - 1].get();
1071}
1072
1073template <class T>
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001074T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
Zachary Turner41a9ee92017-10-11 23:54:34 +00001075 Twine TypeErrMsg) {
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001076 if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001077 return Sec;
1078 error(TypeErrMsg);
1079}
1080
Petr Hosekd7df9b22017-09-06 23:41:02 +00001081template <class ELFT>
Jake Ehrlich76e91102018-01-25 22:46:17 +00001082SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001083 ArrayRef<uint8_t> Data;
1084 switch (Shdr.sh_type) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001085 case SHT_REL:
1086 case SHT_RELA:
Jake Ehrlich9f1a3902017-09-26 18:02:25 +00001087 if (Shdr.sh_flags & SHF_ALLOC) {
1088 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001089 return Obj.addSection<DynamicRelocationSection>(Data);
Jake Ehrlich9f1a3902017-09-26 18:02:25 +00001090 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001091 return Obj.addSection<RelocationSection>();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001092 case SHT_STRTAB:
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001093 // If a string table is allocated we don't want to mess with it. That would
1094 // mean altering the memory image. There are no special link types or
1095 // anything so we can just use a Section.
1096 if (Shdr.sh_flags & SHF_ALLOC) {
1097 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001098 return Obj.addSection<Section>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001099 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001100 return Obj.addSection<StringTableSection>();
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001101 case SHT_HASH:
1102 case SHT_GNU_HASH:
1103 // Hash tables should refer to SHT_DYNSYM which we're not going to change.
1104 // Because of this we don't need to mess with the hash tables either.
1105 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001106 return Obj.addSection<Section>(Data);
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001107 case SHT_GROUP:
1108 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
1109 return Obj.addSection<GroupSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001110 case SHT_DYNSYM:
1111 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001112 return Obj.addSection<DynamicSymbolTableSection>(Data);
Jake Ehrliche5d424b2017-09-20 17:11:58 +00001113 case SHT_DYNAMIC:
1114 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001115 return Obj.addSection<DynamicSection>(Data);
Petr Hosek79cee9e2017-08-29 02:12:03 +00001116 case SHT_SYMTAB: {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001117 auto &SymTab = Obj.addSection<SymbolTableSection>();
1118 Obj.SymbolTable = &SymTab;
1119 return SymTab;
Petr Hosek79cee9e2017-08-29 02:12:03 +00001120 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001121 case SHT_SYMTAB_SHNDX: {
1122 auto &ShndxSection = Obj.addSection<SectionIndexSection>();
1123 Obj.SectionIndexTable = &ShndxSection;
1124 return ShndxSection;
1125 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001126 case SHT_NOBITS:
Jake Ehrlich76e91102018-01-25 22:46:17 +00001127 return Obj.addSection<Section>(Data);
Puyan Lotfiaf048642018-10-01 10:29:41 +00001128 default: {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001129 Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
Puyan Lotfiaf048642018-10-01 10:29:41 +00001130
George Rimarf2eb8ca2019-03-06 14:01:54 +00001131 StringRef Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
1132 if (Name.startswith(".zdebug") || (Shdr.sh_flags & ELF::SHF_COMPRESSED)) {
Puyan Lotfiaf048642018-10-01 10:29:41 +00001133 uint64_t DecompressedSize, DecompressedAlign;
1134 std::tie(DecompressedSize, DecompressedAlign) =
1135 getDecompressedSizeAndAlignment<ELFT>(Data);
1136 return Obj.addSection<CompressedSection>(Data, DecompressedSize,
1137 DecompressedAlign);
1138 }
1139
Jake Ehrlich76e91102018-01-25 22:46:17 +00001140 return Obj.addSection<Section>(Data);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001141 }
Puyan Lotfiaf048642018-10-01 10:29:41 +00001142 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001143}
1144
Jake Ehrlich76e91102018-01-25 22:46:17 +00001145template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001146 uint32_t Index = 0;
1147 for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
1148 if (Index == 0) {
1149 ++Index;
1150 continue;
1151 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001152 auto &Sec = makeSection(Shdr);
1153 Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
1154 Sec.Type = Shdr.sh_type;
1155 Sec.Flags = Shdr.sh_flags;
1156 Sec.Addr = Shdr.sh_addr;
1157 Sec.Offset = Shdr.sh_offset;
1158 Sec.OriginalOffset = Shdr.sh_offset;
1159 Sec.Size = Shdr.sh_size;
1160 Sec.Link = Shdr.sh_link;
1161 Sec.Info = Shdr.sh_info;
1162 Sec.Align = Shdr.sh_addralign;
1163 Sec.EntrySize = Shdr.sh_entsize;
1164 Sec.Index = Index++;
Paul Semela42dec72018-08-09 17:05:21 +00001165 Sec.OriginalData =
1166 ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset,
1167 (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001168 }
Petr Hosek79cee9e2017-08-29 02:12:03 +00001169
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001170 // If a section index table exists we'll need to initialize it before we
1171 // initialize the symbol table because the symbol table might need to
1172 // reference it.
1173 if (Obj.SectionIndexTable)
1174 Obj.SectionIndexTable->initialize(Obj.sections());
1175
Petr Hosek79cee9e2017-08-29 02:12:03 +00001176 // Now that all of the sections have been added we can fill out some extra
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001177 // details about symbol tables. We need the symbol table filled out before
1178 // any relocations.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001179 if (Obj.SymbolTable) {
1180 Obj.SymbolTable->initialize(Obj.sections());
1181 initSymbolTable(Obj.SymbolTable);
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001182 }
Petr Hosekd7df9b22017-09-06 23:41:02 +00001183
1184 // Now that all sections and symbols have been added we can add
1185 // relocations that reference symbols and set the link and info fields for
1186 // relocation sections.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001187 for (auto &Section : Obj.sections()) {
1188 if (&Section == Obj.SymbolTable)
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001189 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001190 Section.initialize(Obj.sections());
1191 if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
Petr Hosekd7df9b22017-09-06 23:41:02 +00001192 auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
1193 if (RelSec->Type == SHT_REL)
Jake Ehrlich76e91102018-01-25 22:46:17 +00001194 initRelocations(RelSec, Obj.SymbolTable,
1195 unwrapOrError(ElfFile.rels(Shdr)));
Petr Hosekd7df9b22017-09-06 23:41:02 +00001196 else
Jake Ehrlich76e91102018-01-25 22:46:17 +00001197 initRelocations(RelSec, Obj.SymbolTable,
Jake Ehrlichf5a43772017-09-25 20:37:28 +00001198 unwrapOrError(ElfFile.relas(Shdr)));
Alexander Shaposhnikov6ecc6e62018-03-21 19:53:44 +00001199 } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
1200 initGroupSection(GroupSec);
Petr Hosekd7df9b22017-09-06 23:41:02 +00001201 }
1202 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001203}
1204
Jake Ehrlich76e91102018-01-25 22:46:17 +00001205template <class ELFT> void ELFBuilder<ELFT>::build() {
Petr Hosek05a04cb2017-08-01 00:33:58 +00001206 const auto &Ehdr = *ElfFile.getHeader();
1207
George Rimar4ded7732018-12-20 10:51:42 +00001208 Obj.OSABI = Ehdr.e_ident[EI_OSABI];
1209 Obj.ABIVersion = Ehdr.e_ident[EI_ABIVERSION];
Jake Ehrlich76e91102018-01-25 22:46:17 +00001210 Obj.Type = Ehdr.e_type;
1211 Obj.Machine = Ehdr.e_machine;
1212 Obj.Version = Ehdr.e_version;
1213 Obj.Entry = Ehdr.e_entry;
1214 Obj.Flags = Ehdr.e_flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001215
Jake Ehrlich76e91102018-01-25 22:46:17 +00001216 readSectionHeaders();
1217 readProgramHeaders();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001218
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001219 uint32_t ShstrIndex = Ehdr.e_shstrndx;
1220 if (ShstrIndex == SHN_XINDEX)
1221 ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
1222
James Henderson38cb2382019-04-02 14:11:13 +00001223 if (ShstrIndex == SHN_UNDEF)
1224 Obj.HadShdrs = false;
1225 else
1226 Obj.SectionNames =
1227 Obj.sections().template getSectionOfType<StringTableSection>(
1228 ShstrIndex,
1229 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
1230 " in elf header is invalid",
1231 "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
1232 " in elf header is not a string table");
Petr Hosek05a04cb2017-08-01 00:33:58 +00001233}
1234
Jake Ehrlich76e91102018-01-25 22:46:17 +00001235Writer::~Writer() {}
1236
1237Reader::~Reader() {}
1238
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001239std::unique_ptr<Object> BinaryReader::create() const {
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001240 return BinaryELFBuilder(MInfo.EMachine, MemBuf).build();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001241}
1242
1243std::unique_ptr<Object> ELFReader::create() const {
Alexander Shaposhnikov58cb1972018-06-07 19:41:42 +00001244 auto Obj = llvm::make_unique<Object>();
Fangrui Song32a34e62018-11-01 16:02:12 +00001245 if (auto *O = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
1246 ELFBuilder<ELF32LE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001247 Builder.build();
1248 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001249 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
1250 ELFBuilder<ELF64LE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001251 Builder.build();
1252 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001253 } else if (auto *O = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
1254 ELFBuilder<ELF32BE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001255 Builder.build();
1256 return Obj;
Fangrui Song32a34e62018-11-01 16:02:12 +00001257 } else if (auto *O = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
1258 ELFBuilder<ELF64BE> Builder(*O, *Obj);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001259 Builder.build();
1260 return Obj;
1261 }
1262 error("Invalid file type");
1263}
1264
1265template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001266 uint8_t *B = Buf.getBufferStart();
1267 Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001268 std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0);
1269 Ehdr.e_ident[EI_MAG0] = 0x7f;
1270 Ehdr.e_ident[EI_MAG1] = 'E';
1271 Ehdr.e_ident[EI_MAG2] = 'L';
1272 Ehdr.e_ident[EI_MAG3] = 'F';
1273 Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32;
1274 Ehdr.e_ident[EI_DATA] =
1275 ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB;
1276 Ehdr.e_ident[EI_VERSION] = EV_CURRENT;
George Rimar4ded7732018-12-20 10:51:42 +00001277 Ehdr.e_ident[EI_OSABI] = Obj.OSABI;
1278 Ehdr.e_ident[EI_ABIVERSION] = Obj.ABIVersion;
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001279
Jake Ehrlich76e91102018-01-25 22:46:17 +00001280 Ehdr.e_type = Obj.Type;
1281 Ehdr.e_machine = Obj.Machine;
1282 Ehdr.e_version = Obj.Version;
1283 Ehdr.e_entry = Obj.Entry;
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001284 // We have to use the fully-qualified name llvm::size
1285 // since some compilers complain on ambiguous resolution.
1286 Ehdr.e_phnum = llvm::size(Obj.segments());
Julie Hockett468722e2018-09-12 17:56:31 +00001287 Ehdr.e_phoff = (Ehdr.e_phnum != 0) ? Obj.ProgramHdrSegment.Offset : 0;
1288 Ehdr.e_phentsize = (Ehdr.e_phnum != 0) ? sizeof(Elf_Phdr) : 0;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001289 Ehdr.e_flags = Obj.Flags;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001290 Ehdr.e_ehsize = sizeof(Elf_Ehdr);
Fangrui Song82b01e02019-03-30 14:08:59 +00001291 if (WriteSectionHeaders && Obj.sections().size() != 0) {
Julie Hockett468722e2018-09-12 17:56:31 +00001292 Ehdr.e_shentsize = sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001293 Ehdr.e_shoff = Obj.SHOffset;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001294 // """
1295 // If the number of sections is greater than or equal to
1296 // SHN_LORESERVE (0xff00), this member has the value zero and the actual
1297 // number of section header table entries is contained in the sh_size field
1298 // of the section header at index 0.
1299 // """
Fangrui Song82b01e02019-03-30 14:08:59 +00001300 auto Shnum = Obj.sections().size() + 1;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001301 if (Shnum >= SHN_LORESERVE)
1302 Ehdr.e_shnum = 0;
1303 else
1304 Ehdr.e_shnum = Shnum;
1305 // """
1306 // If the section name string table section index is greater than or equal
1307 // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
1308 // and the actual index of the section name string table section is
1309 // contained in the sh_link field of the section header at index 0.
1310 // """
1311 if (Obj.SectionNames->Index >= SHN_LORESERVE)
1312 Ehdr.e_shstrndx = SHN_XINDEX;
1313 else
1314 Ehdr.e_shstrndx = Obj.SectionNames->Index;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001315 } else {
Julie Hockett468722e2018-09-12 17:56:31 +00001316 Ehdr.e_shentsize = 0;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001317 Ehdr.e_shoff = 0;
1318 Ehdr.e_shnum = 0;
1319 Ehdr.e_shstrndx = 0;
1320 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001321}
1322
Jake Ehrlich76e91102018-01-25 22:46:17 +00001323template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1324 for (auto &Seg : Obj.segments())
1325 writePhdr(Seg);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001326}
1327
Jake Ehrlich76e91102018-01-25 22:46:17 +00001328template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001329 uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001330 // This reference serves to write the dummy section header at the begining
Jake Ehrlich425ec9f2017-09-15 22:04:09 +00001331 // of the file. It is not used for anything else
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001332 Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001333 Shdr.sh_name = 0;
1334 Shdr.sh_type = SHT_NULL;
1335 Shdr.sh_flags = 0;
1336 Shdr.sh_addr = 0;
1337 Shdr.sh_offset = 0;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001338 // See writeEhdr for why we do this.
Fangrui Song82b01e02019-03-30 14:08:59 +00001339 uint64_t Shnum = Obj.sections().size() + 1;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001340 if (Shnum >= SHN_LORESERVE)
1341 Shdr.sh_size = Shnum;
1342 else
1343 Shdr.sh_size = 0;
1344 // See writeEhdr for why we do this.
1345 if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1346 Shdr.sh_link = Obj.SectionNames->Index;
1347 else
1348 Shdr.sh_link = 0;
Petr Hosek05a04cb2017-08-01 00:33:58 +00001349 Shdr.sh_info = 0;
1350 Shdr.sh_addralign = 0;
1351 Shdr.sh_entsize = 0;
1352
Jake Ehrlich76e91102018-01-25 22:46:17 +00001353 for (auto &Sec : Obj.sections())
1354 writeShdr(Sec);
Petr Hosek05a04cb2017-08-01 00:33:58 +00001355}
1356
Jake Ehrlich76e91102018-01-25 22:46:17 +00001357template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1358 for (auto &Sec : Obj.sections())
James Henderson1f448142019-03-25 16:36:26 +00001359 // Segments are responsible for writing their contents, so only write the
1360 // section data if the section is not in a segment. Note that this renders
1361 // sections in segments effectively immutable.
1362 if (Sec.ParentSegment == nullptr)
1363 Sec.accept(*SecWriter);
1364}
1365
1366template <class ELFT> void ELFWriter<ELFT>::writeSegmentData() {
1367 for (Segment &Seg : Obj.segments()) {
1368 uint8_t *B = Buf.getBufferStart() + Seg.Offset;
1369 assert(Seg.FileSize == Seg.getContents().size() &&
1370 "Segment size must match contents size");
1371 std::memcpy(B, Seg.getContents().data(), Seg.FileSize);
1372 }
1373
1374 // Iterate over removed sections and overwrite their old data with zeroes.
1375 for (auto &Sec : Obj.removedSections()) {
1376 Segment *Parent = Sec.ParentSegment;
1377 if (Parent == nullptr || Sec.Type == SHT_NOBITS || Sec.Size == 0)
1378 continue;
1379 uint64_t Offset =
1380 Sec.OriginalOffset - Parent->OriginalOffset + Parent->Offset;
1381 uint8_t *B = Buf.getBufferStart();
1382 std::memset(B + Offset, 0, Sec.Size);
1383 }
Petr Hosek05a04cb2017-08-01 00:33:58 +00001384}
1385
James Henderson38cb2382019-04-02 14:11:13 +00001386template <class ELFT>
1387ELFWriter<ELFT>::ELFWriter(Object &Obj, Buffer &Buf, bool WSH)
1388 : Writer(Obj, Buf), WriteSectionHeaders(WSH && Obj.HadShdrs) {}
1389
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001390Error Object::removeSections(
1391 std::function<bool(const SectionBase &)> ToRemove) {
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001392
1393 auto Iter = std::stable_partition(
1394 std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1395 if (ToRemove(*Sec))
1396 return false;
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001397 if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1398 if (auto ToRelSec = RelSec->getSection())
1399 return !ToRemove(*ToRelSec);
1400 }
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001401 return true;
1402 });
1403 if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1404 SymbolTable = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001405 if (SectionNames != nullptr && ToRemove(*SectionNames))
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001406 SectionNames = nullptr;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001407 if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1408 SectionIndexTable = nullptr;
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001409 // Now make sure there are no remaining references to the sections that will
1410 // be removed. Sometimes it is impossible to remove a reference so we emit
1411 // an error here instead.
Jordan Rupprecht52d57812019-02-21 16:45:42 +00001412 std::unordered_set<const SectionBase *> RemoveSections;
1413 RemoveSections.reserve(std::distance(Iter, std::end(Sections)));
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001414 for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1415 for (auto &Segment : Segments)
1416 Segment->removeSection(RemoveSec.get());
Jordan Rupprecht52d57812019-02-21 16:45:42 +00001417 RemoveSections.insert(RemoveSec.get());
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001418 }
George Rimar79fb8582019-02-27 11:18:27 +00001419
1420 // For each section that remains alive, we want to remove the dead references.
1421 // This either might update the content of the section (e.g. remove symbols
1422 // from symbol table that belongs to removed section) or trigger an error if
1423 // a live section critically depends on a section being removed somehow
1424 // (e.g. the removed section is referenced by a relocation).
1425 for (auto &KeepSec : make_range(std::begin(Sections), Iter)) {
Jordan Rupprecht52d57812019-02-21 16:45:42 +00001426 if (Error E = KeepSec->removeSectionReferences(
1427 [&RemoveSections](const SectionBase *Sec) {
1428 return RemoveSections.find(Sec) != RemoveSections.end();
1429 }))
1430 return E;
George Rimar79fb8582019-02-27 11:18:27 +00001431 }
1432
James Henderson1f448142019-03-25 16:36:26 +00001433 // Transfer removed sections into the Object RemovedSections container for use
1434 // later.
1435 std::move(Iter, Sections.end(), std::back_inserter(RemovedSections));
1436 // Now finally get rid of them all together.
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001437 Sections.erase(Iter, std::end(Sections));
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001438 return Error::success();
Jake Ehrlich36a2eb32017-10-10 18:47:09 +00001439}
1440
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001441Error Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1442 if (SymbolTable)
1443 for (const SecPtr &Sec : Sections)
1444 if (Error E = Sec->removeSymbols(ToRemove))
1445 return E;
1446 return Error::success();
Paul Semel4246a462018-05-09 21:36:54 +00001447}
1448
Jake Ehrlich76e91102018-01-25 22:46:17 +00001449void Object::sortSections() {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001450 // Put all sections in offset order. Maintain the ordering as closely as
1451 // possible while meeting that demand however.
1452 auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1453 return A->OriginalOffset < B->OriginalOffset;
1454 };
1455 std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1456 CompareSections);
1457}
1458
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001459static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1460 // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1461 if (Align == 0)
1462 Align = 1;
1463 auto Diff =
1464 static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1465 // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1466 // (Offset + Diff) & -Align == Addr & -Align will still hold.
1467 if (Diff < 0)
1468 Diff += Align;
1469 return Offset + Diff;
1470}
1471
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001472// Orders segments such that if x = y->ParentSegment then y comes before x.
Fangrui Song32a34e62018-11-01 16:02:12 +00001473static void orderSegments(std::vector<Segment *> &Segments) {
Jake Ehrlich46814be2018-01-22 19:27:30 +00001474 std::stable_sort(std::begin(Segments), std::end(Segments),
1475 compareSegmentsByOffset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001476}
1477
1478// This function finds a consistent layout for a list of segments starting from
1479// an Offset. It assumes that Segments have been sorted by OrderSegments and
1480// returns an Offset one past the end of the last segment.
Fangrui Song8da6a6c2019-03-29 15:27:58 +00001481static uint64_t layoutSegments(std::vector<Segment *> &Segments,
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001482 uint64_t Offset) {
1483 assert(std::is_sorted(std::begin(Segments), std::end(Segments),
Jake Ehrlich46814be2018-01-22 19:27:30 +00001484 compareSegmentsByOffset));
Petr Hosek3f383832017-08-26 01:32:20 +00001485 // The only way a segment should move is if a section was between two
1486 // segments and that section was removed. If that section isn't in a segment
1487 // then it's acceptable, but not ideal, to simply move it to after the
1488 // segments. So we can simply layout segments one after the other accounting
1489 // for alignment.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001490 for (auto &Segment : Segments) {
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001491 // We assume that segments have been ordered by OriginalOffset and Index
1492 // such that a parent segment will always come before a child segment in
1493 // OrderedSegments. This means that the Offset of the ParentSegment should
1494 // already be set and we can set our offset relative to it.
1495 if (Segment->ParentSegment != nullptr) {
1496 auto Parent = Segment->ParentSegment;
1497 Segment->Offset =
1498 Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1499 } else {
Jake Ehrlich13153ee2017-11-02 23:24:04 +00001500 Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001501 Segment->Offset = Offset;
Jake Ehrlichd246b0a2017-09-19 21:37:35 +00001502 }
Jake Ehrlich084400b2017-10-04 17:44:42 +00001503 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
Petr Hosek3f383832017-08-26 01:32:20 +00001504 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001505 return Offset;
1506}
1507
1508// This function finds a consistent layout for a list of sections. It assumes
1509// that the ->ParentSegment of each section has already been laid out. The
1510// supplied starting Offset is used for the starting offset of any section that
1511// does not have a ParentSegment. It returns either the offset given if all
1512// sections had a ParentSegment or an offset one past the last section if there
1513// was a section that didn't have a ParentSegment.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001514template <class Range>
Fangrui Song32a34e62018-11-01 16:02:12 +00001515static uint64_t layoutSections(Range Sections, uint64_t Offset) {
Petr Hosek3f383832017-08-26 01:32:20 +00001516 // Now the offset of every segment has been set we can assign the offsets
1517 // of each section. For sections that are covered by a segment we should use
1518 // the segment's original offset and the section's original offset to compute
1519 // the offset from the start of the segment. Using the offset from the start
1520 // of the segment we can assign a new offset to the section. For sections not
1521 // covered by segments we can just bump Offset to the next valid location.
1522 uint32_t Index = 1;
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001523 for (auto &Section : Sections) {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001524 Section.Index = Index++;
1525 if (Section.ParentSegment != nullptr) {
1526 auto Segment = *Section.ParentSegment;
1527 Section.Offset =
1528 Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
Petr Hosek3f383832017-08-26 01:32:20 +00001529 } else {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001530 Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1531 Section.Offset = Offset;
1532 if (Section.Type != SHT_NOBITS)
1533 Offset += Section.Size;
Petr Hosek3f383832017-08-26 01:32:20 +00001534 }
1535 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001536 return Offset;
1537}
Petr Hosek3f383832017-08-26 01:32:20 +00001538
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001539template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() {
1540 auto &ElfHdr = Obj.ElfHdrSegment;
1541 ElfHdr.Type = PT_PHDR;
1542 ElfHdr.Flags = 0;
1543 ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
1544 ElfHdr.VAddr = 0;
1545 ElfHdr.PAddr = 0;
1546 ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
1547 ElfHdr.Align = 0;
1548}
1549
Jake Ehrlich76e91102018-01-25 22:46:17 +00001550template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001551 // We need a temporary list of segments that has a special order to it
1552 // so that we know that anytime ->ParentSegment is set that segment has
1553 // already had its offset properly set.
1554 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001555 for (auto &Segment : Obj.segments())
1556 OrderedSegments.push_back(&Segment);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001557 OrderedSegments.push_back(&Obj.ElfHdrSegment);
1558 OrderedSegments.push_back(&Obj.ProgramHdrSegment);
Fangrui Song32a34e62018-11-01 16:02:12 +00001559 orderSegments(OrderedSegments);
Jake Ehrlich6452b112018-02-14 23:31:33 +00001560 // Offset is used as the start offset of the first segment to be laid out.
1561 // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1562 // we start at offset 0.
1563 uint64_t Offset = 0;
Fangrui Song8da6a6c2019-03-29 15:27:58 +00001564 Offset = layoutSegments(OrderedSegments, Offset);
Fangrui Song32a34e62018-11-01 16:02:12 +00001565 Offset = layoutSections(Obj.sections(), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001566 // If we need to write the section header table out then we need to align the
1567 // Offset so that SHOffset is valid.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001568 if (WriteSectionHeaders)
Jordan Rupprechtde965ea2018-08-10 16:25:58 +00001569 Offset = alignTo(Offset, sizeof(Elf_Addr));
Jake Ehrlich76e91102018-01-25 22:46:17 +00001570 Obj.SHOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001571}
1572
Jake Ehrlich76e91102018-01-25 22:46:17 +00001573template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
Petr Hosekc4df10e2017-08-04 21:09:26 +00001574 // We already have the section header offset so we can calculate the total
1575 // size by just adding up the size of each section header.
James Henderson38cb2382019-04-02 14:11:13 +00001576 if (!WriteSectionHeaders)
1577 return Obj.SHOffset;
1578 size_t ShdrCount = Obj.sections().size() + 1; // Includes null shdr.
1579 return Obj.SHOffset + ShdrCount * sizeof(Elf_Shdr);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001580}
1581
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001582template <class ELFT> Error ELFWriter<ELFT>::write() {
James Henderson1f448142019-03-25 16:36:26 +00001583 // Segment data must be written first, so that the ELF header and program
1584 // header tables can overwrite it, if covered by a segment.
1585 writeSegmentData();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001586 writeEhdr();
1587 writePhdrs();
1588 writeSectionData();
1589 if (WriteSectionHeaders)
1590 writeShdrs();
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001591 return Buf.commit();
Jake Ehrlich76e91102018-01-25 22:46:17 +00001592}
1593
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001594template <class ELFT> Error ELFWriter<ELFT>::finalize() {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001595 // It could happen that SectionNames has been removed and yet the user wants
1596 // a section header table output. We need to throw an error if a user tries
1597 // to do that.
1598 if (Obj.SectionNames == nullptr && WriteSectionHeaders)
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001599 return createStringError(llvm::errc::invalid_argument,
1600 "Cannot write section header table because "
1601 "section header string table was removed.");
Jake Ehrlich76e91102018-01-25 22:46:17 +00001602
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001603 Obj.sortSections();
1604
1605 // We need to assign indexes before we perform layout because we need to know
1606 // if we need large indexes or not. We can assign indexes first and check as
1607 // we go to see if we will actully need large indexes.
1608 bool NeedsLargeIndexes = false;
Fangrui Song82b01e02019-03-30 14:08:59 +00001609 if (Obj.sections().size() >= SHN_LORESERVE) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001610 auto Sections = Obj.sections();
1611 NeedsLargeIndexes =
1612 std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1613 [](const SectionBase &Sec) { return Sec.HasSymbol; });
1614 // TODO: handle case where only one section needs the large index table but
1615 // only needs it because the large index table hasn't been removed yet.
1616 }
1617
1618 if (NeedsLargeIndexes) {
1619 // This means we definitely need to have a section index table but if we
1620 // already have one then we should use it instead of making a new one.
1621 if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1622 // Addition of a section to the end does not invalidate the indexes of
1623 // other sections and assigns the correct index to the new section.
1624 auto &Shndx = Obj.addSection<SectionIndexSection>();
1625 Obj.SymbolTable->setShndxTable(&Shndx);
1626 Shndx.setSymTab(Obj.SymbolTable);
1627 }
1628 } else {
1629 // Since we don't need SectionIndexTable we should remove it and all
1630 // references to it.
1631 if (Obj.SectionIndexTable != nullptr) {
Jordan Rupprecht971d47622019-02-01 15:20:36 +00001632 if (Error E = Obj.removeSections([this](const SectionBase &Sec) {
1633 return &Sec == Obj.SectionIndexTable;
1634 }))
1635 return E;
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001636 }
1637 }
1638
1639 // Make sure we add the names of all the sections. Importantly this must be
1640 // done after we decide to add or remove SectionIndexes.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001641 if (Obj.SectionNames != nullptr)
1642 for (const auto &Section : Obj.sections()) {
1643 Obj.SectionNames->addString(Section.Name);
Jake Ehrlichf03384d2017-10-11 18:09:18 +00001644 }
Jake Ehrlich0a151bd2018-03-07 19:59:15 +00001645
Jordan Rupprechtcf676332018-08-17 18:51:11 +00001646 initEhdrSegment();
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001647
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001648 // Before we can prepare for layout the indexes need to be finalized.
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001649 // Also, the output arch may not be the same as the input arch, so fix up
1650 // size-related fields before doing layout calculations.
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001651 uint64_t Index = 0;
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001652 auto SecSizer = llvm::make_unique<ELFSectionSizer<ELFT>>();
1653 for (auto &Sec : Obj.sections()) {
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001654 Sec.Index = Index++;
Jordan Rupprecht1f821762019-01-03 17:45:30 +00001655 Sec.accept(*SecSizer);
1656 }
Jake Ehrlichc7f8ac72018-07-16 19:48:52 +00001657
1658 // The symbol table does not update all other sections on update. For
1659 // instance, symbol names are not added as new symbols are added. This means
1660 // that some sections, like .strtab, don't yet have their final size.
1661 if (Obj.SymbolTable != nullptr)
1662 Obj.SymbolTable->prepareForLayout();
1663
George Rimarfaf308b2019-03-18 14:27:41 +00001664 // Now that all strings are added we want to finalize string table builders,
1665 // because that affects section sizes which in turn affects section offsets.
1666 for (auto &Sec : Obj.sections())
1667 if (auto StrTab = dyn_cast<StringTableSection>(&Sec))
1668 StrTab->prepareForLayout();
1669
Petr Hosekc4df10e2017-08-04 21:09:26 +00001670 assignOffsets();
1671
Eugene Leviant88089fe2019-04-12 11:59:30 +00001672 // layoutSections could have modified section indexes, so we need
1673 // to fill the index table after assignOffsets.
1674 if (Obj.SymbolTable != nullptr)
1675 Obj.SymbolTable->fillShndxTable();
1676
Petr Hosekc4df10e2017-08-04 21:09:26 +00001677 // Finally now that all offsets and indexes have been set we can finalize any
1678 // remaining issues.
Jake Ehrlich76e91102018-01-25 22:46:17 +00001679 uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1680 for (auto &Section : Obj.sections()) {
1681 Section.HeaderOffset = Offset;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001682 Offset += sizeof(Elf_Shdr);
Jake Ehrlich76e91102018-01-25 22:46:17 +00001683 if (WriteSectionHeaders)
1684 Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1685 Section.finalize();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001686 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001687
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001688 if (Error E = Buf.allocate(totalSize()))
1689 return E;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001690 SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001691 return Error::success();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001692}
1693
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001694Error BinaryWriter::write() {
Jake Ehrlich76e91102018-01-25 22:46:17 +00001695 for (auto &Section : Obj.sections()) {
1696 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001697 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001698 Section.accept(*SecWriter);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001699 }
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001700 return Buf.commit();
Petr Hosekc4df10e2017-08-04 21:09:26 +00001701}
1702
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001703Error BinaryWriter::finalize() {
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001704 // TODO: Create a filter range to construct OrderedSegments from so that this
1705 // code can be deduped with assignOffsets above. This should also solve the
1706 // todo below for LayoutSections.
1707 // We need a temporary list of segments that has a special order to it
1708 // so that we know that anytime ->ParentSegment is set that segment has
1709 // already had it's offset properly set. We only want to consider the segments
1710 // that will affect layout of allocated sections so we only add those.
1711 std::vector<Segment *> OrderedSegments;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001712 for (auto &Section : Obj.sections()) {
1713 if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1714 OrderedSegments.push_back(Section.ParentSegment);
Petr Hosekc4df10e2017-08-04 21:09:26 +00001715 }
1716 }
Jake Ehrlich46814be2018-01-22 19:27:30 +00001717
1718 // For binary output, we're going to use physical addresses instead of
1719 // virtual addresses, since a binary output is used for cases like ROM
1720 // loading and physical addresses are intended for ROM loading.
1721 // However, if no segment has a physical address, we'll fallback to using
1722 // virtual addresses for all.
Fangrui Song5ec95db2018-11-17 01:15:55 +00001723 if (all_of(OrderedSegments,
1724 [](const Segment *Seg) { return Seg->PAddr == 0; }))
1725 for (Segment *Seg : OrderedSegments)
1726 Seg->PAddr = Seg->VAddr;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001727
1728 std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1729 compareSegmentsByPAddr);
1730
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001731 // Because we add a ParentSegment for each section we might have duplicate
1732 // segments in OrderedSegments. If there were duplicates then LayoutSegments
1733 // would do very strange things.
1734 auto End =
1735 std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1736 OrderedSegments.erase(End, std::end(OrderedSegments));
1737
Jake Ehrlich46814be2018-01-22 19:27:30 +00001738 uint64_t Offset = 0;
1739
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001740 // Modify the first segment so that there is no gap at the start. This allows
Fangrui Song5ec95db2018-11-17 01:15:55 +00001741 // our layout algorithm to proceed as expected while not writing out the gap
1742 // at the start.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001743 if (!OrderedSegments.empty()) {
1744 auto Seg = OrderedSegments[0];
1745 auto Sec = Seg->firstSection();
1746 auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1747 Seg->OriginalOffset += Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001748 // The size needs to be shrunk as well.
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001749 Seg->FileSize -= Diff;
Jake Ehrlich46814be2018-01-22 19:27:30 +00001750 // The PAddr needs to be increased to remove the gap before the first
1751 // section.
1752 Seg->PAddr += Diff;
1753 uint64_t LowestPAddr = Seg->PAddr;
1754 for (auto &Segment : OrderedSegments) {
1755 Segment->Offset = Segment->PAddr - LowestPAddr;
1756 Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1757 }
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001758 }
1759
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001760 // TODO: generalize LayoutSections to take a range. Pass a special range
1761 // constructed from an iterator that skips values for which a predicate does
1762 // not hold. Then pass such a range to LayoutSections instead of constructing
1763 // AllocatedSections here.
1764 std::vector<SectionBase *> AllocatedSections;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001765 for (auto &Section : Obj.sections()) {
1766 if ((Section.Flags & SHF_ALLOC) == 0)
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001767 continue;
Jake Ehrlich76e91102018-01-25 22:46:17 +00001768 AllocatedSections.push_back(&Section);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001769 }
Fangrui Song32a34e62018-11-01 16:02:12 +00001770 layoutSections(make_pointee_range(AllocatedSections), Offset);
Jake Ehrlichd49c92b2017-11-15 19:13:31 +00001771
1772 // Now that every section has been laid out we just need to compute the total
1773 // file size. This might not be the same as the offset returned by
1774 // LayoutSections, because we want to truncate the last segment to the end of
1775 // its last section, to match GNU objcopy's behaviour.
1776 TotalSize = 0;
1777 for (const auto &Section : AllocatedSections) {
1778 if (Section->Type != SHT_NOBITS)
1779 TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1780 }
Jake Ehrlich76e91102018-01-25 22:46:17 +00001781
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001782 if (Error E = Buf.allocate(TotalSize))
1783 return E;
Alexander Shaposhnikov42b5ef02018-07-06 17:51:03 +00001784 SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
Jordan Rupprecht881cae72019-01-22 23:49:16 +00001785 return Error::success();
Petr Hosek05a04cb2017-08-01 00:33:58 +00001786}
1787
Jake Ehrlich76e91102018-01-25 22:46:17 +00001788template class ELFBuilder<ELF64LE>;
1789template class ELFBuilder<ELF64BE>;
1790template class ELFBuilder<ELF32LE>;
1791template class ELFBuilder<ELF32BE>;
Petr Hosekc4df10e2017-08-04 21:09:26 +00001792
Jake Ehrlich76e91102018-01-25 22:46:17 +00001793template class ELFWriter<ELF64LE>;
1794template class ELFWriter<ELF64BE>;
1795template class ELFWriter<ELF32LE>;
1796template class ELFWriter<ELF32BE>;
Alexander Shaposhnikov654d3a92018-10-24 22:49:06 +00001797
1798} // end namespace elf
Puyan Lotfi0f5d5fa2018-07-18 00:10:51 +00001799} // end namespace objcopy
Eugene Zelenko0ad18f82017-11-01 21:16:06 +00001800} // end namespace llvm