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