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