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