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