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