• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Object.cpp ---------------------------------------------------------===//
2 //
3 //                      The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Object.h"
11 #include "llvm-objcopy.h"
12 #include "llvm/ADT/ArrayRef.h"
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/ADT/iterator_range.h"
17 #include "llvm/BinaryFormat/ELF.h"
18 #include "llvm/Object/ELFObjectFile.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/FileOutputBuffer.h"
21 #include "llvm/Support/Path.h"
22 #include <algorithm>
23 #include <cstddef>
24 #include <cstdint>
25 #include <iterator>
26 #include <utility>
27 #include <vector>
28 
29 using namespace llvm;
30 using namespace llvm::objcopy;
31 using namespace object;
32 using namespace ELF;
33 
~Buffer()34 Buffer::~Buffer() {}
35 
allocate(size_t Size)36 void FileBuffer::allocate(size_t Size) {
37   Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
38       FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable);
39   handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) {
40     error("failed to open " + getName() + ": " + E.message());
41   });
42   Buf = std::move(*BufferOrErr);
43 }
44 
commit()45 Error FileBuffer::commit() { return Buf->commit(); }
46 
getBufferStart()47 uint8_t *FileBuffer::getBufferStart() {
48   return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
49 }
50 
allocate(size_t Size)51 void MemBuffer::allocate(size_t Size) {
52   Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName());
53 }
54 
commit()55 Error MemBuffer::commit() { return Error::success(); }
56 
getBufferStart()57 uint8_t *MemBuffer::getBufferStart() {
58   return reinterpret_cast<uint8_t *>(Buf->getBufferStart());
59 }
60 
releaseMemoryBuffer()61 std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() {
62   return std::move(Buf);
63 }
64 
writePhdr(const Segment & Seg)65 template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) {
66   using Elf_Phdr = typename ELFT::Phdr;
67 
68   uint8_t *B = Buf.getBufferStart();
69   B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr);
70   Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B);
71   Phdr.p_type = Seg.Type;
72   Phdr.p_flags = Seg.Flags;
73   Phdr.p_offset = Seg.Offset;
74   Phdr.p_vaddr = Seg.VAddr;
75   Phdr.p_paddr = Seg.PAddr;
76   Phdr.p_filesz = Seg.FileSize;
77   Phdr.p_memsz = Seg.MemSize;
78   Phdr.p_align = Seg.Align;
79 }
80 
removeSectionReferences(const SectionBase * Sec)81 void SectionBase::removeSectionReferences(const SectionBase *Sec) {}
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)82 void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {}
initialize(SectionTableRef SecTable)83 void SectionBase::initialize(SectionTableRef SecTable) {}
finalize()84 void SectionBase::finalize() {}
markSymbols()85 void SectionBase::markSymbols() {}
86 
writeShdr(const SectionBase & Sec)87 template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) {
88   uint8_t *B = Buf.getBufferStart();
89   B += Sec.HeaderOffset;
90   typename ELFT::Shdr &Shdr = *reinterpret_cast<typename ELFT::Shdr *>(B);
91   Shdr.sh_name = Sec.NameIndex;
92   Shdr.sh_type = Sec.Type;
93   Shdr.sh_flags = Sec.Flags;
94   Shdr.sh_addr = Sec.Addr;
95   Shdr.sh_offset = Sec.Offset;
96   Shdr.sh_size = Sec.Size;
97   Shdr.sh_link = Sec.Link;
98   Shdr.sh_info = Sec.Info;
99   Shdr.sh_addralign = Sec.Align;
100   Shdr.sh_entsize = Sec.EntrySize;
101 }
102 
~SectionVisitor()103 SectionVisitor::~SectionVisitor() {}
104 
visit(const SectionIndexSection & Sec)105 void BinarySectionWriter::visit(const SectionIndexSection &Sec) {
106   error("Cannot write symbol section index table '" + Sec.Name + "' ");
107 }
108 
visit(const SymbolTableSection & Sec)109 void BinarySectionWriter::visit(const SymbolTableSection &Sec) {
110   error("Cannot write symbol table '" + Sec.Name + "' out to binary");
111 }
112 
visit(const RelocationSection & Sec)113 void BinarySectionWriter::visit(const RelocationSection &Sec) {
114   error("Cannot write relocation section '" + Sec.Name + "' out to binary");
115 }
116 
visit(const GnuDebugLinkSection & Sec)117 void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) {
118   error("Cannot write '" + Sec.Name + "' out to binary");
119 }
120 
visit(const GroupSection & Sec)121 void BinarySectionWriter::visit(const GroupSection &Sec) {
122   error("Cannot write '" + Sec.Name + "' out to binary");
123 }
124 
visit(const Section & Sec)125 void SectionWriter::visit(const Section &Sec) {
126   if (Sec.Type == SHT_NOBITS)
127     return;
128   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
129   std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf);
130 }
131 
accept(SectionVisitor & Visitor) const132 void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); }
133 
visit(const OwnedDataSection & Sec)134 void SectionWriter::visit(const OwnedDataSection &Sec) {
135   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
136   std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf);
137 }
138 
accept(SectionVisitor & Visitor) const139 void OwnedDataSection::accept(SectionVisitor &Visitor) const {
140   Visitor.visit(*this);
141 }
142 
addString(StringRef Name)143 void StringTableSection::addString(StringRef Name) {
144   StrTabBuilder.add(Name);
145   Size = StrTabBuilder.getSize();
146 }
147 
findIndex(StringRef Name) const148 uint32_t StringTableSection::findIndex(StringRef Name) const {
149   return StrTabBuilder.getOffset(Name);
150 }
151 
finalize()152 void StringTableSection::finalize() { StrTabBuilder.finalize(); }
153 
visit(const StringTableSection & Sec)154 void SectionWriter::visit(const StringTableSection &Sec) {
155   Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset);
156 }
157 
accept(SectionVisitor & Visitor) const158 void StringTableSection::accept(SectionVisitor &Visitor) const {
159   Visitor.visit(*this);
160 }
161 
162 template <class ELFT>
visit(const SectionIndexSection & Sec)163 void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) {
164   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
165   auto *IndexesBuffer = reinterpret_cast<typename ELFT::Word *>(Buf);
166   std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer);
167 }
168 
initialize(SectionTableRef SecTable)169 void SectionIndexSection::initialize(SectionTableRef SecTable) {
170   Size = 0;
171   setSymTab(SecTable.getSectionOfType<SymbolTableSection>(
172       Link,
173       "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
174       "Link field value " + Twine(Link) + " in section " + Name +
175           " is not a symbol table"));
176   Symbols->setShndxTable(this);
177 }
178 
finalize()179 void SectionIndexSection::finalize() { Link = Symbols->Index; }
180 
accept(SectionVisitor & Visitor) const181 void SectionIndexSection::accept(SectionVisitor &Visitor) const {
182   Visitor.visit(*this);
183 }
184 
isValidReservedSectionIndex(uint16_t Index,uint16_t Machine)185 static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) {
186   switch (Index) {
187   case SHN_ABS:
188   case SHN_COMMON:
189     return true;
190   }
191   if (Machine == EM_HEXAGON) {
192     switch (Index) {
193     case SHN_HEXAGON_SCOMMON:
194     case SHN_HEXAGON_SCOMMON_2:
195     case SHN_HEXAGON_SCOMMON_4:
196     case SHN_HEXAGON_SCOMMON_8:
197       return true;
198     }
199   }
200   return false;
201 }
202 
203 // Large indexes force us to clarify exactly what this function should do. This
204 // function should return the value that will appear in st_shndx when written
205 // out.
getShndx() const206 uint16_t Symbol::getShndx() const {
207   if (DefinedIn != nullptr) {
208     if (DefinedIn->Index >= SHN_LORESERVE)
209       return SHN_XINDEX;
210     return DefinedIn->Index;
211   }
212   switch (ShndxType) {
213   // This means that we don't have a defined section but we do need to
214   // output a legitimate section index.
215   case SYMBOL_SIMPLE_INDEX:
216     return SHN_UNDEF;
217   case SYMBOL_ABS:
218   case SYMBOL_COMMON:
219   case SYMBOL_HEXAGON_SCOMMON:
220   case SYMBOL_HEXAGON_SCOMMON_2:
221   case SYMBOL_HEXAGON_SCOMMON_4:
222   case SYMBOL_HEXAGON_SCOMMON_8:
223   case SYMBOL_XINDEX:
224     return static_cast<uint16_t>(ShndxType);
225   }
226   llvm_unreachable("Symbol with invalid ShndxType encountered");
227 }
228 
assignIndices()229 void SymbolTableSection::assignIndices() {
230   uint32_t Index = 0;
231   for (auto &Sym : Symbols)
232     Sym->Index = Index++;
233 }
234 
addSymbol(StringRef Name,uint8_t Bind,uint8_t Type,SectionBase * DefinedIn,uint64_t Value,uint8_t Visibility,uint16_t Shndx,uint64_t Sz)235 void SymbolTableSection::addSymbol(StringRef Name, uint8_t Bind, uint8_t Type,
236                                    SectionBase *DefinedIn, uint64_t Value,
237                                    uint8_t Visibility, uint16_t Shndx,
238                                    uint64_t Sz) {
239   Symbol Sym;
240   Sym.Name = Name;
241   Sym.Binding = Bind;
242   Sym.Type = Type;
243   Sym.DefinedIn = DefinedIn;
244   if (DefinedIn != nullptr)
245     DefinedIn->HasSymbol = true;
246   if (DefinedIn == nullptr) {
247     if (Shndx >= SHN_LORESERVE)
248       Sym.ShndxType = static_cast<SymbolShndxType>(Shndx);
249     else
250       Sym.ShndxType = SYMBOL_SIMPLE_INDEX;
251   }
252   Sym.Value = Value;
253   Sym.Visibility = Visibility;
254   Sym.Size = Sz;
255   Sym.Index = Symbols.size();
256   Symbols.emplace_back(llvm::make_unique<Symbol>(Sym));
257   Size += this->EntrySize;
258 }
259 
removeSectionReferences(const SectionBase * Sec)260 void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) {
261   if (SectionIndexTable == Sec)
262     SectionIndexTable = nullptr;
263   if (SymbolNames == Sec) {
264     error("String table " + SymbolNames->Name +
265           " cannot be removed because it is referenced by the symbol table " +
266           this->Name);
267   }
268   removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; });
269 }
270 
updateSymbols(function_ref<void (Symbol &)> Callable)271 void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) {
272   std::for_each(std::begin(Symbols) + 1, std::end(Symbols),
273                 [Callable](SymPtr &Sym) { Callable(*Sym); });
274   std::stable_partition(
275       std::begin(Symbols), std::end(Symbols),
276       [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; });
277   assignIndices();
278 }
279 
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)280 void SymbolTableSection::removeSymbols(
281     function_ref<bool(const Symbol &)> ToRemove) {
282   Symbols.erase(
283       std::remove_if(std::begin(Symbols) + 1, std::end(Symbols),
284                      [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }),
285       std::end(Symbols));
286   Size = Symbols.size() * EntrySize;
287   assignIndices();
288 }
289 
initialize(SectionTableRef SecTable)290 void SymbolTableSection::initialize(SectionTableRef SecTable) {
291   Size = 0;
292   setStrTab(SecTable.getSectionOfType<StringTableSection>(
293       Link,
294       "Symbol table has link index of " + Twine(Link) +
295           " which is not a valid index",
296       "Symbol table has link index of " + Twine(Link) +
297           " which is not a string table"));
298 }
299 
finalize()300 void SymbolTableSection::finalize() {
301   // Make sure SymbolNames is finalized before getting name indexes.
302   SymbolNames->finalize();
303 
304   uint32_t MaxLocalIndex = 0;
305   for (auto &Sym : Symbols) {
306     Sym->NameIndex = SymbolNames->findIndex(Sym->Name);
307     if (Sym->Binding == STB_LOCAL)
308       MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index);
309   }
310   // Now we need to set the Link and Info fields.
311   Link = SymbolNames->Index;
312   Info = MaxLocalIndex + 1;
313 }
314 
prepareForLayout()315 void SymbolTableSection::prepareForLayout() {
316   // Add all potential section indexes before file layout so that the section
317   // index section has the approprite size.
318   if (SectionIndexTable != nullptr) {
319     for (const auto &Sym : Symbols) {
320       if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE)
321         SectionIndexTable->addIndex(Sym->DefinedIn->Index);
322       else
323         SectionIndexTable->addIndex(SHN_UNDEF);
324     }
325   }
326   // Add all of our strings to SymbolNames so that SymbolNames has the right
327   // size before layout is decided.
328   for (auto &Sym : Symbols)
329     SymbolNames->addString(Sym->Name);
330 }
331 
getSymbolByIndex(uint32_t Index) const332 const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const {
333   if (Symbols.size() <= Index)
334     error("Invalid symbol index: " + Twine(Index));
335   return Symbols[Index].get();
336 }
337 
getSymbolByIndex(uint32_t Index)338 Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) {
339   return const_cast<Symbol *>(
340       static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index));
341 }
342 
343 template <class ELFT>
visit(const SymbolTableSection & Sec)344 void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) {
345   uint8_t *Buf = Out.getBufferStart();
346   Buf += Sec.Offset;
347   typename ELFT::Sym *Sym = reinterpret_cast<typename ELFT::Sym *>(Buf);
348   // Loop though symbols setting each entry of the symbol table.
349   for (auto &Symbol : Sec.Symbols) {
350     Sym->st_name = Symbol->NameIndex;
351     Sym->st_value = Symbol->Value;
352     Sym->st_size = Symbol->Size;
353     Sym->st_other = Symbol->Visibility;
354     Sym->setBinding(Symbol->Binding);
355     Sym->setType(Symbol->Type);
356     Sym->st_shndx = Symbol->getShndx();
357     ++Sym;
358   }
359 }
360 
accept(SectionVisitor & Visitor) const361 void SymbolTableSection::accept(SectionVisitor &Visitor) const {
362   Visitor.visit(*this);
363 }
364 
365 template <class SymTabType>
removeSectionReferences(const SectionBase * Sec)366 void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences(
367     const SectionBase *Sec) {
368   if (Symbols == Sec) {
369     error("Symbol table " + Symbols->Name +
370           " cannot be removed because it is "
371           "referenced by the relocation "
372           "section " +
373           this->Name);
374   }
375 }
376 
377 template <class SymTabType>
initialize(SectionTableRef SecTable)378 void RelocSectionWithSymtabBase<SymTabType>::initialize(
379     SectionTableRef SecTable) {
380   setSymTab(SecTable.getSectionOfType<SymTabType>(
381       Link,
382       "Link field value " + Twine(Link) + " in section " + Name + " is invalid",
383       "Link field value " + Twine(Link) + " in section " + Name +
384           " is not a symbol table"));
385 
386   if (Info != SHN_UNDEF)
387     setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) +
388                                              " in section " + Name +
389                                              " is invalid"));
390   else
391     setSection(nullptr);
392 }
393 
394 template <class SymTabType>
finalize()395 void RelocSectionWithSymtabBase<SymTabType>::finalize() {
396   this->Link = Symbols->Index;
397   if (SecToApplyRel != nullptr)
398     this->Info = SecToApplyRel->Index;
399 }
400 
401 template <class ELFT>
setAddend(Elf_Rel_Impl<ELFT,false> & Rel,uint64_t Addend)402 static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {}
403 
404 template <class ELFT>
setAddend(Elf_Rel_Impl<ELFT,true> & Rela,uint64_t Addend)405 static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) {
406   Rela.r_addend = Addend;
407 }
408 
409 template <class RelRange, class T>
writeRel(const RelRange & Relocations,T * Buf)410 static void writeRel(const RelRange &Relocations, T *Buf) {
411   for (const auto &Reloc : Relocations) {
412     Buf->r_offset = Reloc.Offset;
413     setAddend(*Buf, Reloc.Addend);
414     Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false);
415     ++Buf;
416   }
417 }
418 
419 template <class ELFT>
visit(const RelocationSection & Sec)420 void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) {
421   uint8_t *Buf = Out.getBufferStart() + Sec.Offset;
422   if (Sec.Type == SHT_REL)
423     writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf));
424   else
425     writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf));
426 }
427 
accept(SectionVisitor & Visitor) const428 void RelocationSection::accept(SectionVisitor &Visitor) const {
429   Visitor.visit(*this);
430 }
431 
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)432 void RelocationSection::removeSymbols(
433     function_ref<bool(const Symbol &)> ToRemove) {
434   for (const Relocation &Reloc : Relocations)
435     if (ToRemove(*Reloc.RelocSymbol))
436       error("not stripping symbol `" + Reloc.RelocSymbol->Name +
437             "' because it is named in a relocation");
438 }
439 
markSymbols()440 void RelocationSection::markSymbols() {
441   for (const Relocation &Reloc : Relocations)
442     Reloc.RelocSymbol->Referenced = true;
443 }
444 
visit(const DynamicRelocationSection & Sec)445 void SectionWriter::visit(const DynamicRelocationSection &Sec) {
446   std::copy(std::begin(Sec.Contents), std::end(Sec.Contents),
447             Out.getBufferStart() + Sec.Offset);
448 }
449 
accept(SectionVisitor & Visitor) const450 void DynamicRelocationSection::accept(SectionVisitor &Visitor) const {
451   Visitor.visit(*this);
452 }
453 
removeSectionReferences(const SectionBase * Sec)454 void Section::removeSectionReferences(const SectionBase *Sec) {
455   if (LinkSection == Sec) {
456     error("Section " + LinkSection->Name +
457           " cannot be removed because it is "
458           "referenced by the section " +
459           this->Name);
460   }
461 }
462 
finalize()463 void GroupSection::finalize() {
464   this->Info = Sym->Index;
465   this->Link = SymTab->Index;
466 }
467 
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)468 void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
469   if (ToRemove(*Sym)) {
470     error("Symbol " + Sym->Name +
471           " cannot be removed because it is "
472           "referenced by the section " +
473           this->Name + "[" + Twine(this->Index) + "]");
474   }
475 }
476 
markSymbols()477 void GroupSection::markSymbols() {
478   if (Sym)
479     Sym->Referenced = true;
480 }
481 
initialize(SectionTableRef SecTable)482 void Section::initialize(SectionTableRef SecTable) {
483   if (Link != ELF::SHN_UNDEF) {
484     LinkSection =
485         SecTable.getSection(Link, "Link field value " + Twine(Link) +
486                                       " in section " + Name + " is invalid");
487     if (LinkSection->Type == ELF::SHT_SYMTAB)
488       LinkSection = nullptr;
489   }
490 }
491 
finalize()492 void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; }
493 
init(StringRef File,StringRef Data)494 void GnuDebugLinkSection::init(StringRef File, StringRef Data) {
495   FileName = sys::path::filename(File);
496   // The format for the .gnu_debuglink starts with the file name and is
497   // followed by a null terminator and then the CRC32 of the file. The CRC32
498   // should be 4 byte aligned. So we add the FileName size, a 1 for the null
499   // byte, and then finally push the size to alignment and add 4.
500   Size = alignTo(FileName.size() + 1, 4) + 4;
501   // The CRC32 will only be aligned if we align the whole section.
502   Align = 4;
503   Type = ELF::SHT_PROGBITS;
504   Name = ".gnu_debuglink";
505   // For sections not found in segments, OriginalOffset is only used to
506   // establish the order that sections should go in. By using the maximum
507   // possible offset we cause this section to wind up at the end.
508   OriginalOffset = std::numeric_limits<uint64_t>::max();
509   JamCRC crc;
510   crc.update(ArrayRef<char>(Data.data(), Data.size()));
511   // The CRC32 value needs to be complemented because the JamCRC dosn't
512   // finalize the CRC32 value. It also dosn't negate the initial CRC32 value
513   // but it starts by default at 0xFFFFFFFF which is the complement of zero.
514   CRC32 = ~crc.getCRC();
515 }
516 
GnuDebugLinkSection(StringRef File)517 GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) {
518   // Read in the file to compute the CRC of it.
519   auto DebugOrErr = MemoryBuffer::getFile(File);
520   if (!DebugOrErr)
521     error("'" + File + "': " + DebugOrErr.getError().message());
522   auto Debug = std::move(*DebugOrErr);
523   init(File, Debug->getBuffer());
524 }
525 
526 template <class ELFT>
visit(const GnuDebugLinkSection & Sec)527 void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) {
528   auto Buf = Out.getBufferStart() + Sec.Offset;
529   char *File = reinterpret_cast<char *>(Buf);
530   Elf_Word *CRC =
531       reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word));
532   *CRC = Sec.CRC32;
533   std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File);
534 }
535 
accept(SectionVisitor & Visitor) const536 void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const {
537   Visitor.visit(*this);
538 }
539 
540 template <class ELFT>
visit(const GroupSection & Sec)541 void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) {
542   ELF::Elf32_Word *Buf =
543       reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset);
544   *Buf++ = Sec.FlagWord;
545   for (const auto *S : Sec.GroupMembers)
546     support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index);
547 }
548 
accept(SectionVisitor & Visitor) const549 void GroupSection::accept(SectionVisitor &Visitor) const {
550   Visitor.visit(*this);
551 }
552 
553 // Returns true IFF a section is wholly inside the range of a segment
sectionWithinSegment(const SectionBase & Section,const Segment & Segment)554 static bool sectionWithinSegment(const SectionBase &Section,
555                                  const Segment &Segment) {
556   // If a section is empty it should be treated like it has a size of 1. This is
557   // to clarify the case when an empty section lies on a boundary between two
558   // segments and ensures that the section "belongs" to the second segment and
559   // not the first.
560   uint64_t SecSize = Section.Size ? Section.Size : 1;
561   return Segment.Offset <= Section.OriginalOffset &&
562          Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize;
563 }
564 
565 // Returns true IFF a segment's original offset is inside of another segment's
566 // range.
segmentOverlapsSegment(const Segment & Child,const Segment & Parent)567 static bool segmentOverlapsSegment(const Segment &Child,
568                                    const Segment &Parent) {
569 
570   return Parent.OriginalOffset <= Child.OriginalOffset &&
571          Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset;
572 }
573 
compareSegmentsByOffset(const Segment * A,const Segment * B)574 static bool compareSegmentsByOffset(const Segment *A, const Segment *B) {
575   // Any segment without a parent segment should come before a segment
576   // that has a parent segment.
577   if (A->OriginalOffset < B->OriginalOffset)
578     return true;
579   if (A->OriginalOffset > B->OriginalOffset)
580     return false;
581   return A->Index < B->Index;
582 }
583 
compareSegmentsByPAddr(const Segment * A,const Segment * B)584 static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) {
585   if (A->PAddr < B->PAddr)
586     return true;
587   if (A->PAddr > B->PAddr)
588     return false;
589   return A->Index < B->Index;
590 }
591 
setParentSegment(Segment & Child)592 template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) {
593   for (auto &Parent : Obj.segments()) {
594     // Every segment will overlap with itself but we don't want a segment to
595     // be it's own parent so we avoid that situation.
596     if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) {
597       // We want a canonical "most parental" segment but this requires
598       // inspecting the ParentSegment.
599       if (compareSegmentsByOffset(&Parent, &Child))
600         if (Child.ParentSegment == nullptr ||
601             compareSegmentsByOffset(&Parent, Child.ParentSegment)) {
602           Child.ParentSegment = &Parent;
603         }
604     }
605   }
606 }
607 
readProgramHeaders()608 template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() {
609   uint32_t Index = 0;
610   for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) {
611     ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset,
612                            (size_t)Phdr.p_filesz};
613     Segment &Seg = Obj.addSegment(Data);
614     Seg.Type = Phdr.p_type;
615     Seg.Flags = Phdr.p_flags;
616     Seg.OriginalOffset = Phdr.p_offset;
617     Seg.Offset = Phdr.p_offset;
618     Seg.VAddr = Phdr.p_vaddr;
619     Seg.PAddr = Phdr.p_paddr;
620     Seg.FileSize = Phdr.p_filesz;
621     Seg.MemSize = Phdr.p_memsz;
622     Seg.Align = Phdr.p_align;
623     Seg.Index = Index++;
624     for (auto &Section : Obj.sections()) {
625       if (sectionWithinSegment(Section, Seg)) {
626         Seg.addSection(&Section);
627         if (!Section.ParentSegment ||
628             Section.ParentSegment->Offset > Seg.Offset) {
629           Section.ParentSegment = &Seg;
630         }
631       }
632     }
633   }
634 
635   auto &ElfHdr = Obj.ElfHdrSegment;
636   // Creating multiple PT_PHDR segments technically is not valid, but PT_LOAD
637   // segments must not overlap, and other types fit even less.
638   ElfHdr.Type = PT_PHDR;
639   ElfHdr.Flags = 0;
640   ElfHdr.OriginalOffset = ElfHdr.Offset = 0;
641   ElfHdr.VAddr = 0;
642   ElfHdr.PAddr = 0;
643   ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr);
644   ElfHdr.Align = 0;
645   ElfHdr.Index = Index++;
646 
647   const auto &Ehdr = *ElfFile.getHeader();
648   auto &PrHdr = Obj.ProgramHdrSegment;
649   PrHdr.Type = PT_PHDR;
650   PrHdr.Flags = 0;
651   // The spec requires us to have p_vaddr % p_align == p_offset % p_align.
652   // Whereas this works automatically for ElfHdr, here OriginalOffset is
653   // always non-zero and to ensure the equation we assign the same value to
654   // VAddr as well.
655   PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff;
656   PrHdr.PAddr = 0;
657   PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum;
658   // The spec requires us to naturally align all the fields.
659   PrHdr.Align = sizeof(Elf_Addr);
660   PrHdr.Index = Index++;
661 
662   // Now we do an O(n^2) loop through the segments in order to match up
663   // segments.
664   for (auto &Child : Obj.segments())
665     setParentSegment(Child);
666   setParentSegment(ElfHdr);
667   setParentSegment(PrHdr);
668 }
669 
670 template <class ELFT>
initGroupSection(GroupSection * GroupSec)671 void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) {
672   auto SecTable = Obj.sections();
673   auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>(
674       GroupSec->Link,
675       "Link field value " + Twine(GroupSec->Link) + " in section " +
676           GroupSec->Name + " is invalid",
677       "Link field value " + Twine(GroupSec->Link) + " in section " +
678           GroupSec->Name + " is not a symbol table");
679   auto Sym = SymTab->getSymbolByIndex(GroupSec->Info);
680   if (!Sym)
681     error("Info field value " + Twine(GroupSec->Info) + " in section " +
682           GroupSec->Name + " is not a valid symbol index");
683   GroupSec->setSymTab(SymTab);
684   GroupSec->setSymbol(Sym);
685   if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) ||
686       GroupSec->Contents.empty())
687     error("The content of the section " + GroupSec->Name + " is malformed");
688   const ELF::Elf32_Word *Word =
689       reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data());
690   const ELF::Elf32_Word *End =
691       Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word);
692   GroupSec->setFlagWord(*Word++);
693   for (; Word != End; ++Word) {
694     uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word);
695     GroupSec->addMember(SecTable.getSection(
696         Index, "Group member index " + Twine(Index) + " in section " +
697                    GroupSec->Name + " is invalid"));
698   }
699 }
700 
701 template <class ELFT>
initSymbolTable(SymbolTableSection * SymTab)702 void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) {
703   const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index));
704   StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr));
705   ArrayRef<Elf_Word> ShndxData;
706 
707   auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr));
708   for (const auto &Sym : Symbols) {
709     SectionBase *DefSection = nullptr;
710     StringRef Name = unwrapOrError(Sym.getName(StrTabData));
711 
712     if (Sym.st_shndx == SHN_XINDEX) {
713       if (SymTab->getShndxTable() == nullptr)
714         error("Symbol '" + Name +
715               "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists.");
716       if (ShndxData.data() == nullptr) {
717         const Elf_Shdr &ShndxSec =
718             *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index));
719         ShndxData = unwrapOrError(
720             ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec));
721         if (ShndxData.size() != Symbols.size())
722           error("Symbol section index table does not have the same number of "
723                 "entries as the symbol table.");
724       }
725       Elf_Word Index = ShndxData[&Sym - Symbols.begin()];
726       DefSection = Obj.sections().getSection(
727           Index,
728           "Symbol '" + Name + "' has invalid section index " +
729               Twine(Index));
730     } else if (Sym.st_shndx >= SHN_LORESERVE) {
731       if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) {
732         error(
733             "Symbol '" + Name +
734             "' has unsupported value greater than or equal to SHN_LORESERVE: " +
735             Twine(Sym.st_shndx));
736       }
737     } else if (Sym.st_shndx != SHN_UNDEF) {
738       DefSection = Obj.sections().getSection(
739           Sym.st_shndx, "Symbol '" + Name +
740                             "' is defined has invalid section index " +
741                             Twine(Sym.st_shndx));
742     }
743 
744     SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection,
745                       Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size);
746   }
747 }
748 
749 template <class ELFT>
getAddend(uint64_t & ToSet,const Elf_Rel_Impl<ELFT,false> & Rel)750 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {}
751 
752 template <class ELFT>
getAddend(uint64_t & ToSet,const Elf_Rel_Impl<ELFT,true> & Rela)753 static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) {
754   ToSet = Rela.r_addend;
755 }
756 
757 template <class T>
initRelocations(RelocationSection * Relocs,SymbolTableSection * SymbolTable,T RelRange)758 static void initRelocations(RelocationSection *Relocs,
759                             SymbolTableSection *SymbolTable, T RelRange) {
760   for (const auto &Rel : RelRange) {
761     Relocation ToAdd;
762     ToAdd.Offset = Rel.r_offset;
763     getAddend(ToAdd.Addend, Rel);
764     ToAdd.Type = Rel.getType(false);
765     ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false));
766     Relocs->addRelocation(ToAdd);
767   }
768 }
769 
getSection(uint32_t Index,Twine ErrMsg)770 SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) {
771   if (Index == SHN_UNDEF || Index > Sections.size())
772     error(ErrMsg);
773   return Sections[Index - 1].get();
774 }
775 
776 template <class T>
getSectionOfType(uint32_t Index,Twine IndexErrMsg,Twine TypeErrMsg)777 T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg,
778                                      Twine TypeErrMsg) {
779   if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg)))
780     return Sec;
781   error(TypeErrMsg);
782 }
783 
784 template <class ELFT>
makeSection(const Elf_Shdr & Shdr)785 SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) {
786   ArrayRef<uint8_t> Data;
787   switch (Shdr.sh_type) {
788   case SHT_REL:
789   case SHT_RELA:
790     if (Shdr.sh_flags & SHF_ALLOC) {
791       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
792       return Obj.addSection<DynamicRelocationSection>(Data);
793     }
794     return Obj.addSection<RelocationSection>();
795   case SHT_STRTAB:
796     // If a string table is allocated we don't want to mess with it. That would
797     // mean altering the memory image. There are no special link types or
798     // anything so we can just use a Section.
799     if (Shdr.sh_flags & SHF_ALLOC) {
800       Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
801       return Obj.addSection<Section>(Data);
802     }
803     return Obj.addSection<StringTableSection>();
804   case SHT_HASH:
805   case SHT_GNU_HASH:
806     // Hash tables should refer to SHT_DYNSYM which we're not going to change.
807     // Because of this we don't need to mess with the hash tables either.
808     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
809     return Obj.addSection<Section>(Data);
810   case SHT_GROUP:
811     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
812     return Obj.addSection<GroupSection>(Data);
813   case SHT_DYNSYM:
814     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
815     return Obj.addSection<DynamicSymbolTableSection>(Data);
816   case SHT_DYNAMIC:
817     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
818     return Obj.addSection<DynamicSection>(Data);
819   case SHT_SYMTAB: {
820     auto &SymTab = Obj.addSection<SymbolTableSection>();
821     Obj.SymbolTable = &SymTab;
822     return SymTab;
823   }
824   case SHT_SYMTAB_SHNDX: {
825     auto &ShndxSection = Obj.addSection<SectionIndexSection>();
826     Obj.SectionIndexTable = &ShndxSection;
827     return ShndxSection;
828   }
829   case SHT_NOBITS:
830     return Obj.addSection<Section>(Data);
831   default:
832     Data = unwrapOrError(ElfFile.getSectionContents(&Shdr));
833     return Obj.addSection<Section>(Data);
834   }
835 }
836 
readSectionHeaders()837 template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() {
838   uint32_t Index = 0;
839   for (const auto &Shdr : unwrapOrError(ElfFile.sections())) {
840     if (Index == 0) {
841       ++Index;
842       continue;
843     }
844     auto &Sec = makeSection(Shdr);
845     Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr));
846     Sec.Type = Shdr.sh_type;
847     Sec.Flags = Shdr.sh_flags;
848     Sec.Addr = Shdr.sh_addr;
849     Sec.Offset = Shdr.sh_offset;
850     Sec.OriginalOffset = Shdr.sh_offset;
851     Sec.Size = Shdr.sh_size;
852     Sec.Link = Shdr.sh_link;
853     Sec.Info = Shdr.sh_info;
854     Sec.Align = Shdr.sh_addralign;
855     Sec.EntrySize = Shdr.sh_entsize;
856     Sec.Index = Index++;
857   }
858 
859   // If a section index table exists we'll need to initialize it before we
860   // initialize the symbol table because the symbol table might need to
861   // reference it.
862   if (Obj.SectionIndexTable)
863     Obj.SectionIndexTable->initialize(Obj.sections());
864 
865   // Now that all of the sections have been added we can fill out some extra
866   // details about symbol tables. We need the symbol table filled out before
867   // any relocations.
868   if (Obj.SymbolTable) {
869     Obj.SymbolTable->initialize(Obj.sections());
870     initSymbolTable(Obj.SymbolTable);
871   }
872 
873   // Now that all sections and symbols have been added we can add
874   // relocations that reference symbols and set the link and info fields for
875   // relocation sections.
876   for (auto &Section : Obj.sections()) {
877     if (&Section == Obj.SymbolTable)
878       continue;
879     Section.initialize(Obj.sections());
880     if (auto RelSec = dyn_cast<RelocationSection>(&Section)) {
881       auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index;
882       if (RelSec->Type == SHT_REL)
883         initRelocations(RelSec, Obj.SymbolTable,
884                         unwrapOrError(ElfFile.rels(Shdr)));
885       else
886         initRelocations(RelSec, Obj.SymbolTable,
887                         unwrapOrError(ElfFile.relas(Shdr)));
888     } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) {
889       initGroupSection(GroupSec);
890     }
891   }
892 }
893 
build()894 template <class ELFT> void ELFBuilder<ELFT>::build() {
895   const auto &Ehdr = *ElfFile.getHeader();
896 
897   std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Obj.Ident);
898   Obj.Type = Ehdr.e_type;
899   Obj.Machine = Ehdr.e_machine;
900   Obj.Version = Ehdr.e_version;
901   Obj.Entry = Ehdr.e_entry;
902   Obj.Flags = Ehdr.e_flags;
903 
904   readSectionHeaders();
905   readProgramHeaders();
906 
907   uint32_t ShstrIndex = Ehdr.e_shstrndx;
908   if (ShstrIndex == SHN_XINDEX)
909     ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link;
910 
911   Obj.SectionNames =
912       Obj.sections().template getSectionOfType<StringTableSection>(
913           ShstrIndex,
914           "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
915               " in elf header " + " is invalid",
916           "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) +
917               " in elf header " + " is not a string table");
918 }
919 
920 // A generic size function which computes sizes of any random access range.
size(R && Range)921 template <class R> size_t size(R &&Range) {
922   return static_cast<size_t>(std::end(Range) - std::begin(Range));
923 }
924 
~Writer()925 Writer::~Writer() {}
926 
~Reader()927 Reader::~Reader() {}
928 
getElfType() const929 ElfType ELFReader::getElfType() const {
930   if (isa<ELFObjectFile<ELF32LE>>(Bin))
931     return ELFT_ELF32LE;
932   if (isa<ELFObjectFile<ELF64LE>>(Bin))
933     return ELFT_ELF64LE;
934   if (isa<ELFObjectFile<ELF32BE>>(Bin))
935     return ELFT_ELF32BE;
936   if (isa<ELFObjectFile<ELF64BE>>(Bin))
937     return ELFT_ELF64BE;
938   llvm_unreachable("Invalid ELFType");
939 }
940 
create() const941 std::unique_ptr<Object> ELFReader::create() const {
942   auto Obj = llvm::make_unique<Object>();
943   if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) {
944     ELFBuilder<ELF32LE> Builder(*o, *Obj);
945     Builder.build();
946     return Obj;
947   } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) {
948     ELFBuilder<ELF64LE> Builder(*o, *Obj);
949     Builder.build();
950     return Obj;
951   } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) {
952     ELFBuilder<ELF32BE> Builder(*o, *Obj);
953     Builder.build();
954     return Obj;
955   } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) {
956     ELFBuilder<ELF64BE> Builder(*o, *Obj);
957     Builder.build();
958     return Obj;
959   }
960   error("Invalid file type");
961 }
962 
writeEhdr()963 template <class ELFT> void ELFWriter<ELFT>::writeEhdr() {
964   uint8_t *B = Buf.getBufferStart();
965   Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B);
966   std::copy(Obj.Ident, Obj.Ident + 16, Ehdr.e_ident);
967   Ehdr.e_type = Obj.Type;
968   Ehdr.e_machine = Obj.Machine;
969   Ehdr.e_version = Obj.Version;
970   Ehdr.e_entry = Obj.Entry;
971   Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset;
972   Ehdr.e_flags = Obj.Flags;
973   Ehdr.e_ehsize = sizeof(Elf_Ehdr);
974   Ehdr.e_phentsize = sizeof(Elf_Phdr);
975   Ehdr.e_phnum = size(Obj.segments());
976   Ehdr.e_shentsize = sizeof(Elf_Shdr);
977   if (WriteSectionHeaders) {
978     Ehdr.e_shoff = Obj.SHOffset;
979     // """
980     // If the number of sections is greater than or equal to
981     // SHN_LORESERVE (0xff00), this member has the value zero and the actual
982     // number of section header table entries is contained in the sh_size field
983     // of the section header at index 0.
984     // """
985     auto Shnum = size(Obj.sections()) + 1;
986     if (Shnum >= SHN_LORESERVE)
987       Ehdr.e_shnum = 0;
988     else
989       Ehdr.e_shnum = Shnum;
990     // """
991     // If the section name string table section index is greater than or equal
992     // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff)
993     // and the actual index of the section name string table section is
994     // contained in the sh_link field of the section header at index 0.
995     // """
996     if (Obj.SectionNames->Index >= SHN_LORESERVE)
997       Ehdr.e_shstrndx = SHN_XINDEX;
998     else
999       Ehdr.e_shstrndx = Obj.SectionNames->Index;
1000   } else {
1001     Ehdr.e_shoff = 0;
1002     Ehdr.e_shnum = 0;
1003     Ehdr.e_shstrndx = 0;
1004   }
1005 }
1006 
writePhdrs()1007 template <class ELFT> void ELFWriter<ELFT>::writePhdrs() {
1008   for (auto &Seg : Obj.segments())
1009     writePhdr(Seg);
1010 }
1011 
writeShdrs()1012 template <class ELFT> void ELFWriter<ELFT>::writeShdrs() {
1013   uint8_t *B = Buf.getBufferStart() + Obj.SHOffset;
1014   // This reference serves to write the dummy section header at the begining
1015   // of the file. It is not used for anything else
1016   Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B);
1017   Shdr.sh_name = 0;
1018   Shdr.sh_type = SHT_NULL;
1019   Shdr.sh_flags = 0;
1020   Shdr.sh_addr = 0;
1021   Shdr.sh_offset = 0;
1022   // See writeEhdr for why we do this.
1023   uint64_t Shnum = size(Obj.sections()) + 1;
1024   if (Shnum >= SHN_LORESERVE)
1025     Shdr.sh_size = Shnum;
1026   else
1027     Shdr.sh_size = 0;
1028   // See writeEhdr for why we do this.
1029   if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE)
1030     Shdr.sh_link = Obj.SectionNames->Index;
1031   else
1032     Shdr.sh_link = 0;
1033   Shdr.sh_info = 0;
1034   Shdr.sh_addralign = 0;
1035   Shdr.sh_entsize = 0;
1036 
1037   for (auto &Sec : Obj.sections())
1038     writeShdr(Sec);
1039 }
1040 
writeSectionData()1041 template <class ELFT> void ELFWriter<ELFT>::writeSectionData() {
1042   for (auto &Sec : Obj.sections())
1043     Sec.accept(*SecWriter);
1044 }
1045 
removeSections(std::function<bool (const SectionBase &)> ToRemove)1046 void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) {
1047 
1048   auto Iter = std::stable_partition(
1049       std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) {
1050         if (ToRemove(*Sec))
1051           return false;
1052         if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) {
1053           if (auto ToRelSec = RelSec->getSection())
1054             return !ToRemove(*ToRelSec);
1055         }
1056         return true;
1057       });
1058   if (SymbolTable != nullptr && ToRemove(*SymbolTable))
1059     SymbolTable = nullptr;
1060   if (SectionNames != nullptr && ToRemove(*SectionNames))
1061     SectionNames = nullptr;
1062   if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable))
1063     SectionIndexTable = nullptr;
1064   // Now make sure there are no remaining references to the sections that will
1065   // be removed. Sometimes it is impossible to remove a reference so we emit
1066   // an error here instead.
1067   for (auto &RemoveSec : make_range(Iter, std::end(Sections))) {
1068     for (auto &Segment : Segments)
1069       Segment->removeSection(RemoveSec.get());
1070     for (auto &KeepSec : make_range(std::begin(Sections), Iter))
1071       KeepSec->removeSectionReferences(RemoveSec.get());
1072   }
1073   // Now finally get rid of them all togethor.
1074   Sections.erase(Iter, std::end(Sections));
1075 }
1076 
removeSymbols(function_ref<bool (const Symbol &)> ToRemove)1077 void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {
1078   if (!SymbolTable)
1079     return;
1080 
1081   for (const SecPtr &Sec : Sections)
1082     Sec->removeSymbols(ToRemove);
1083 }
1084 
sortSections()1085 void Object::sortSections() {
1086   // Put all sections in offset order. Maintain the ordering as closely as
1087   // possible while meeting that demand however.
1088   auto CompareSections = [](const SecPtr &A, const SecPtr &B) {
1089     return A->OriginalOffset < B->OriginalOffset;
1090   };
1091   std::stable_sort(std::begin(this->Sections), std::end(this->Sections),
1092                    CompareSections);
1093 }
1094 
alignToAddr(uint64_t Offset,uint64_t Addr,uint64_t Align)1095 static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) {
1096   // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align.
1097   if (Align == 0)
1098     Align = 1;
1099   auto Diff =
1100       static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align);
1101   // We only want to add to Offset, however, so if Diff < 0 we can add Align and
1102   // (Offset + Diff) & -Align == Addr & -Align will still hold.
1103   if (Diff < 0)
1104     Diff += Align;
1105   return Offset + Diff;
1106 }
1107 
1108 // Orders segments such that if x = y->ParentSegment then y comes before x.
OrderSegments(std::vector<Segment * > & Segments)1109 static void OrderSegments(std::vector<Segment *> &Segments) {
1110   std::stable_sort(std::begin(Segments), std::end(Segments),
1111                    compareSegmentsByOffset);
1112 }
1113 
1114 // This function finds a consistent layout for a list of segments starting from
1115 // an Offset. It assumes that Segments have been sorted by OrderSegments and
1116 // returns an Offset one past the end of the last segment.
LayoutSegments(std::vector<Segment * > & Segments,uint64_t Offset)1117 static uint64_t LayoutSegments(std::vector<Segment *> &Segments,
1118                                uint64_t Offset) {
1119   assert(std::is_sorted(std::begin(Segments), std::end(Segments),
1120                         compareSegmentsByOffset));
1121   // The only way a segment should move is if a section was between two
1122   // segments and that section was removed. If that section isn't in a segment
1123   // then it's acceptable, but not ideal, to simply move it to after the
1124   // segments. So we can simply layout segments one after the other accounting
1125   // for alignment.
1126   for (auto &Segment : Segments) {
1127     // We assume that segments have been ordered by OriginalOffset and Index
1128     // such that a parent segment will always come before a child segment in
1129     // OrderedSegments. This means that the Offset of the ParentSegment should
1130     // already be set and we can set our offset relative to it.
1131     if (Segment->ParentSegment != nullptr) {
1132       auto Parent = Segment->ParentSegment;
1133       Segment->Offset =
1134           Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset;
1135     } else {
1136       Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align);
1137       Segment->Offset = Offset;
1138     }
1139     Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1140   }
1141   return Offset;
1142 }
1143 
1144 // This function finds a consistent layout for a list of sections. It assumes
1145 // that the ->ParentSegment of each section has already been laid out. The
1146 // supplied starting Offset is used for the starting offset of any section that
1147 // does not have a ParentSegment. It returns either the offset given if all
1148 // sections had a ParentSegment or an offset one past the last section if there
1149 // was a section that didn't have a ParentSegment.
1150 template <class Range>
LayoutSections(Range Sections,uint64_t Offset)1151 static uint64_t LayoutSections(Range Sections, uint64_t Offset) {
1152   // Now the offset of every segment has been set we can assign the offsets
1153   // of each section. For sections that are covered by a segment we should use
1154   // the segment's original offset and the section's original offset to compute
1155   // the offset from the start of the segment. Using the offset from the start
1156   // of the segment we can assign a new offset to the section. For sections not
1157   // covered by segments we can just bump Offset to the next valid location.
1158   uint32_t Index = 1;
1159   for (auto &Section : Sections) {
1160     Section.Index = Index++;
1161     if (Section.ParentSegment != nullptr) {
1162       auto Segment = *Section.ParentSegment;
1163       Section.Offset =
1164           Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset);
1165     } else {
1166       Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align);
1167       Section.Offset = Offset;
1168       if (Section.Type != SHT_NOBITS)
1169         Offset += Section.Size;
1170     }
1171   }
1172   return Offset;
1173 }
1174 
assignOffsets()1175 template <class ELFT> void ELFWriter<ELFT>::assignOffsets() {
1176   // We need a temporary list of segments that has a special order to it
1177   // so that we know that anytime ->ParentSegment is set that segment has
1178   // already had its offset properly set.
1179   std::vector<Segment *> OrderedSegments;
1180   for (auto &Segment : Obj.segments())
1181     OrderedSegments.push_back(&Segment);
1182   OrderedSegments.push_back(&Obj.ElfHdrSegment);
1183   OrderedSegments.push_back(&Obj.ProgramHdrSegment);
1184   OrderSegments(OrderedSegments);
1185   // Offset is used as the start offset of the first segment to be laid out.
1186   // Since the ELF Header (ElfHdrSegment) must be at the start of the file,
1187   // we start at offset 0.
1188   uint64_t Offset = 0;
1189   Offset = LayoutSegments(OrderedSegments, Offset);
1190   Offset = LayoutSections(Obj.sections(), Offset);
1191   // If we need to write the section header table out then we need to align the
1192   // Offset so that SHOffset is valid.
1193   if (WriteSectionHeaders)
1194     Offset = alignTo(Offset, sizeof(typename ELFT::Addr));
1195   Obj.SHOffset = Offset;
1196 }
1197 
totalSize() const1198 template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const {
1199   // We already have the section header offset so we can calculate the total
1200   // size by just adding up the size of each section header.
1201   auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0;
1202   return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) +
1203          NullSectionSize;
1204 }
1205 
write()1206 template <class ELFT> void ELFWriter<ELFT>::write() {
1207   writeEhdr();
1208   writePhdrs();
1209   writeSectionData();
1210   if (WriteSectionHeaders)
1211     writeShdrs();
1212   if (auto E = Buf.commit())
1213     reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1214 }
1215 
finalize()1216 template <class ELFT> void ELFWriter<ELFT>::finalize() {
1217   // It could happen that SectionNames has been removed and yet the user wants
1218   // a section header table output. We need to throw an error if a user tries
1219   // to do that.
1220   if (Obj.SectionNames == nullptr && WriteSectionHeaders)
1221     error("Cannot write section header table because section header string "
1222           "table was removed.");
1223 
1224   Obj.sortSections();
1225 
1226   // We need to assign indexes before we perform layout because we need to know
1227   // if we need large indexes or not. We can assign indexes first and check as
1228   // we go to see if we will actully need large indexes.
1229   bool NeedsLargeIndexes = false;
1230   if (size(Obj.sections()) >= SHN_LORESERVE) {
1231     auto Sections = Obj.sections();
1232     NeedsLargeIndexes =
1233         std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(),
1234                     [](const SectionBase &Sec) { return Sec.HasSymbol; });
1235     // TODO: handle case where only one section needs the large index table but
1236     // only needs it because the large index table hasn't been removed yet.
1237   }
1238 
1239   if (NeedsLargeIndexes) {
1240     // This means we definitely need to have a section index table but if we
1241     // already have one then we should use it instead of making a new one.
1242     if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) {
1243       // Addition of a section to the end does not invalidate the indexes of
1244       // other sections and assigns the correct index to the new section.
1245       auto &Shndx = Obj.addSection<SectionIndexSection>();
1246       Obj.SymbolTable->setShndxTable(&Shndx);
1247       Shndx.setSymTab(Obj.SymbolTable);
1248     }
1249   } else {
1250     // Since we don't need SectionIndexTable we should remove it and all
1251     // references to it.
1252     if (Obj.SectionIndexTable != nullptr) {
1253       Obj.removeSections([this](const SectionBase &Sec) {
1254         return &Sec == Obj.SectionIndexTable;
1255       });
1256     }
1257   }
1258 
1259   // Make sure we add the names of all the sections. Importantly this must be
1260   // done after we decide to add or remove SectionIndexes.
1261   if (Obj.SectionNames != nullptr)
1262     for (const auto &Section : Obj.sections()) {
1263       Obj.SectionNames->addString(Section.Name);
1264     }
1265 
1266   // Before we can prepare for layout the indexes need to be finalized.
1267   uint64_t Index = 0;
1268   for (auto &Sec : Obj.sections())
1269     Sec.Index = Index++;
1270 
1271   // The symbol table does not update all other sections on update. For
1272   // instance, symbol names are not added as new symbols are added. This means
1273   // that some sections, like .strtab, don't yet have their final size.
1274   if (Obj.SymbolTable != nullptr)
1275     Obj.SymbolTable->prepareForLayout();
1276 
1277   assignOffsets();
1278 
1279   // Finalize SectionNames first so that we can assign name indexes.
1280   if (Obj.SectionNames != nullptr)
1281     Obj.SectionNames->finalize();
1282   // Finally now that all offsets and indexes have been set we can finalize any
1283   // remaining issues.
1284   uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr);
1285   for (auto &Section : Obj.sections()) {
1286     Section.HeaderOffset = Offset;
1287     Offset += sizeof(Elf_Shdr);
1288     if (WriteSectionHeaders)
1289       Section.NameIndex = Obj.SectionNames->findIndex(Section.Name);
1290     Section.finalize();
1291   }
1292 
1293   Buf.allocate(totalSize());
1294   SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf);
1295 }
1296 
write()1297 void BinaryWriter::write() {
1298   for (auto &Section : Obj.sections()) {
1299     if ((Section.Flags & SHF_ALLOC) == 0)
1300       continue;
1301     Section.accept(*SecWriter);
1302   }
1303   if (auto E = Buf.commit())
1304     reportError(Buf.getName(), errorToErrorCode(std::move(E)));
1305 }
1306 
finalize()1307 void BinaryWriter::finalize() {
1308   // TODO: Create a filter range to construct OrderedSegments from so that this
1309   // code can be deduped with assignOffsets above. This should also solve the
1310   // todo below for LayoutSections.
1311   // We need a temporary list of segments that has a special order to it
1312   // so that we know that anytime ->ParentSegment is set that segment has
1313   // already had it's offset properly set. We only want to consider the segments
1314   // that will affect layout of allocated sections so we only add those.
1315   std::vector<Segment *> OrderedSegments;
1316   for (auto &Section : Obj.sections()) {
1317     if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) {
1318       OrderedSegments.push_back(Section.ParentSegment);
1319     }
1320   }
1321 
1322   // For binary output, we're going to use physical addresses instead of
1323   // virtual addresses, since a binary output is used for cases like ROM
1324   // loading and physical addresses are intended for ROM loading.
1325   // However, if no segment has a physical address, we'll fallback to using
1326   // virtual addresses for all.
1327   if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments),
1328                   [](const Segment *Segment) { return Segment->PAddr == 0; }))
1329     for (const auto &Segment : OrderedSegments)
1330       Segment->PAddr = Segment->VAddr;
1331 
1332   std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments),
1333                    compareSegmentsByPAddr);
1334 
1335   // Because we add a ParentSegment for each section we might have duplicate
1336   // segments in OrderedSegments. If there were duplicates then LayoutSegments
1337   // would do very strange things.
1338   auto End =
1339       std::unique(std::begin(OrderedSegments), std::end(OrderedSegments));
1340   OrderedSegments.erase(End, std::end(OrderedSegments));
1341 
1342   uint64_t Offset = 0;
1343 
1344   // Modify the first segment so that there is no gap at the start. This allows
1345   // our layout algorithm to proceed as expected while not out writing out the
1346   // gap at the start.
1347   if (!OrderedSegments.empty()) {
1348     auto Seg = OrderedSegments[0];
1349     auto Sec = Seg->firstSection();
1350     auto Diff = Sec->OriginalOffset - Seg->OriginalOffset;
1351     Seg->OriginalOffset += Diff;
1352     // The size needs to be shrunk as well.
1353     Seg->FileSize -= Diff;
1354     // The PAddr needs to be increased to remove the gap before the first
1355     // section.
1356     Seg->PAddr += Diff;
1357     uint64_t LowestPAddr = Seg->PAddr;
1358     for (auto &Segment : OrderedSegments) {
1359       Segment->Offset = Segment->PAddr - LowestPAddr;
1360       Offset = std::max(Offset, Segment->Offset + Segment->FileSize);
1361     }
1362   }
1363 
1364   // TODO: generalize LayoutSections to take a range. Pass a special range
1365   // constructed from an iterator that skips values for which a predicate does
1366   // not hold. Then pass such a range to LayoutSections instead of constructing
1367   // AllocatedSections here.
1368   std::vector<SectionBase *> AllocatedSections;
1369   for (auto &Section : Obj.sections()) {
1370     if ((Section.Flags & SHF_ALLOC) == 0)
1371       continue;
1372     AllocatedSections.push_back(&Section);
1373   }
1374   LayoutSections(make_pointee_range(AllocatedSections), Offset);
1375 
1376   // Now that every section has been laid out we just need to compute the total
1377   // file size. This might not be the same as the offset returned by
1378   // LayoutSections, because we want to truncate the last segment to the end of
1379   // its last section, to match GNU objcopy's behaviour.
1380   TotalSize = 0;
1381   for (const auto &Section : AllocatedSections) {
1382     if (Section->Type != SHT_NOBITS)
1383       TotalSize = std::max(TotalSize, Section->Offset + Section->Size);
1384   }
1385 
1386   Buf.allocate(TotalSize);
1387   SecWriter = llvm::make_unique<BinarySectionWriter>(Buf);
1388 }
1389 
1390 namespace llvm {
1391 namespace objcopy {
1392 
1393 template class ELFBuilder<ELF64LE>;
1394 template class ELFBuilder<ELF64BE>;
1395 template class ELFBuilder<ELF32LE>;
1396 template class ELFBuilder<ELF32BE>;
1397 
1398 template class ELFWriter<ELF64LE>;
1399 template class ELFWriter<ELF64BE>;
1400 template class ELFWriter<ELF32LE>;
1401 template class ELFWriter<ELF32BE>;
1402 } // end namespace objcopy
1403 } // end namespace llvm
1404