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