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