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) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 230 | error("Symbol table " + Symbols->Name + |
| 231 | " cannot be removed because it is " |
| 232 | "referenced by the relocation " |
| 233 | "section " + |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 234 | this->Name); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | template <class SymTabType> |
| 239 | void RelocSectionWithSymtabBase<SymTabType>::initialize( |
| 240 | SectionTableRef SecTable) { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 241 | setSymTab(SecTable.getSectionOfType<SymTabType>( |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 242 | Link, |
| 243 | "Link field value " + Twine(Link) + " in section " + Name + " is invalid", |
| 244 | "Link field value " + Twine(Link) + " in section " + Name + |
| 245 | " is not a symbol table")); |
| 246 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 247 | if (Info != SHN_UNDEF) |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 248 | setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) + |
| 249 | " in section " + Name + |
| 250 | " is invalid")); |
James Y Knight | 2ea995a | 2017-09-26 22:44:01 +0000 | [diff] [blame] | 251 | else |
| 252 | setSection(nullptr); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 255 | template <class SymTabType> |
| 256 | void RelocSectionWithSymtabBase<SymTabType>::finalize() { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 257 | this->Link = Symbols->Index; |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 258 | if (SecToApplyRel != nullptr) |
| 259 | this->Info = SecToApplyRel->Index; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | template <class ELFT> |
| 263 | void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {} |
| 264 | |
| 265 | template <class ELFT> |
| 266 | void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) { |
| 267 | Rela.r_addend = Addend; |
| 268 | } |
| 269 | |
| 270 | template <class ELFT> |
| 271 | template <class T> |
| 272 | void RelocationSection<ELFT>::writeRel(T *Buf) const { |
| 273 | for (const auto &Reloc : Relocations) { |
| 274 | Buf->r_offset = Reloc.Offset; |
| 275 | setAddend(*Buf, Reloc.Addend); |
| 276 | Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false); |
| 277 | ++Buf; |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | template <class ELFT> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 282 | void RelocationSection<ELFT>::writeSection(FileOutputBuffer &Out) const { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 283 | uint8_t *Buf = Out.getBufferStart() + Offset; |
| 284 | if (Type == SHT_REL) |
| 285 | writeRel(reinterpret_cast<Elf_Rel *>(Buf)); |
| 286 | else |
| 287 | writeRel(reinterpret_cast<Elf_Rela *>(Buf)); |
| 288 | } |
| 289 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 290 | void DynamicRelocationSection::writeSection(FileOutputBuffer &Out) const { |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 291 | std::copy(std::begin(Contents), std::end(Contents), |
| 292 | Out.getBufferStart() + Offset); |
| 293 | } |
| 294 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 295 | void SectionWithStrTab::removeSectionReferences(const SectionBase *Sec) { |
| 296 | if (StrTab == Sec) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 297 | error("String table " + StrTab->Name + |
| 298 | " cannot be removed because it is " |
| 299 | "referenced by the section " + |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 300 | this->Name); |
| 301 | } |
| 302 | } |
| 303 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 304 | bool SectionWithStrTab::classof(const SectionBase *S) { |
| 305 | return isa<DynamicSymbolTableSection>(S) || isa<DynamicSection>(S); |
| 306 | } |
| 307 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 308 | void SectionWithStrTab::initialize(SectionTableRef SecTable) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 309 | auto StrTab = |
| 310 | SecTable.getSection(Link, "Link field value " + Twine(Link) + |
| 311 | " in section " + Name + " is invalid"); |
Jake Ehrlich | 70bd75f | 2017-10-10 21:28:22 +0000 | [diff] [blame] | 312 | if (StrTab->Type != SHT_STRTAB) { |
| 313 | error("Link field value " + Twine(Link) + " in section " + Name + |
| 314 | " is not a string table"); |
| 315 | } |
| 316 | setStrTab(StrTab); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 319 | void SectionWithStrTab::finalize() { this->Link = StrTab->Index; } |
| 320 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 321 | // Returns true IFF a section is wholly inside the range of a segment |
| 322 | static bool sectionWithinSegment(const SectionBase &Section, |
| 323 | const Segment &Segment) { |
| 324 | // If a section is empty it should be treated like it has a size of 1. This is |
| 325 | // to clarify the case when an empty section lies on a boundary between two |
| 326 | // segments and ensures that the section "belongs" to the second segment and |
| 327 | // not the first. |
| 328 | uint64_t SecSize = Section.Size ? Section.Size : 1; |
| 329 | return Segment.Offset <= Section.OriginalOffset && |
| 330 | Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize; |
| 331 | } |
| 332 | |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 333 | // Returns true IFF a segment's original offset is inside of another segment's |
| 334 | // range. |
| 335 | static bool segmentOverlapsSegment(const Segment &Child, |
| 336 | const Segment &Parent) { |
| 337 | |
| 338 | return Parent.OriginalOffset <= Child.OriginalOffset && |
| 339 | Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset; |
| 340 | } |
| 341 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 342 | template <class ELFT> |
| 343 | void Object<ELFT>::readProgramHeaders(const ELFFile<ELFT> &ElfFile) { |
| 344 | uint32_t Index = 0; |
| 345 | for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 346 | ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset, |
| 347 | (size_t)Phdr.p_filesz}; |
| 348 | Segments.emplace_back(llvm::make_unique<Segment>(Data)); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 349 | Segment &Seg = *Segments.back(); |
| 350 | Seg.Type = Phdr.p_type; |
| 351 | Seg.Flags = Phdr.p_flags; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 352 | Seg.OriginalOffset = Phdr.p_offset; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 353 | Seg.Offset = Phdr.p_offset; |
| 354 | Seg.VAddr = Phdr.p_vaddr; |
| 355 | Seg.PAddr = Phdr.p_paddr; |
| 356 | Seg.FileSize = Phdr.p_filesz; |
| 357 | Seg.MemSize = Phdr.p_memsz; |
| 358 | Seg.Align = Phdr.p_align; |
| 359 | Seg.Index = Index++; |
| 360 | for (auto &Section : Sections) { |
| 361 | if (sectionWithinSegment(*Section, Seg)) { |
| 362 | Seg.addSection(&*Section); |
| 363 | if (!Section->ParentSegment || |
| 364 | Section->ParentSegment->Offset > Seg.Offset) { |
| 365 | Section->ParentSegment = &Seg; |
| 366 | } |
| 367 | } |
| 368 | } |
| 369 | } |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 370 | // Now we do an O(n^2) loop through the segments in order to match up |
| 371 | // segments. |
| 372 | for (auto &Child : Segments) { |
| 373 | for (auto &Parent : Segments) { |
| 374 | // Every segment will overlap with itself but we don't want a segment to |
| 375 | // be it's own parent so we avoid that situation. |
| 376 | if (&Child != &Parent && segmentOverlapsSegment(*Child, *Parent)) { |
| 377 | // We want a canonical "most parental" segment but this requires |
| 378 | // inspecting the ParentSegment. |
| 379 | if (Child->ParentSegment != nullptr) { |
| 380 | if (Child->ParentSegment->OriginalOffset > Parent->OriginalOffset) { |
| 381 | Child->ParentSegment = Parent.get(); |
| 382 | } else if (Child->ParentSegment->Index > Parent->Index) { |
| 383 | // They must have equal OriginalOffsets so we need to disambiguate. |
| 384 | // To decide which is the parent we'll choose the one with the |
| 385 | // higher index. |
| 386 | Child->ParentSegment = Parent.get(); |
| 387 | } |
| 388 | } else { |
| 389 | Child->ParentSegment = Parent.get(); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | template <class ELFT> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 397 | void Object<ELFT>::initSymbolTable(const object::ELFFile<ELFT> &ElfFile, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 398 | SymbolTableSection *SymTab, |
| 399 | SectionTableRef SecTable) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 400 | const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); |
| 401 | StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); |
| 402 | |
| 403 | for (const auto &Sym : unwrapOrError(ElfFile.symbols(&Shdr))) { |
| 404 | SectionBase *DefSection = nullptr; |
| 405 | StringRef Name = unwrapOrError(Sym.getName(StrTabData)); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 406 | |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 407 | if (Sym.st_shndx >= SHN_LORESERVE) { |
Petr Hosek | c113577 | 2017-09-13 03:04:50 +0000 | [diff] [blame] | 408 | if (!isValidReservedSectionIndex(Sym.st_shndx, Machine)) { |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 409 | error( |
| 410 | "Symbol '" + Name + |
| 411 | "' has unsupported value greater than or equal to SHN_LORESERVE: " + |
| 412 | Twine(Sym.st_shndx)); |
| 413 | } |
| 414 | } else if (Sym.st_shndx != SHN_UNDEF) { |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 415 | DefSection = SecTable.getSection( |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 416 | Sym.st_shndx, "Symbol '" + Name + |
| 417 | "' is defined in invalid section with index " + |
| 418 | Twine(Sym.st_shndx)); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 419 | } |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 420 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 421 | SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection, |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 422 | Sym.getValue(), Sym.st_shndx, Sym.st_size); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
| 426 | template <class ELFT> |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 427 | static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {} |
| 428 | |
| 429 | template <class ELFT> |
| 430 | static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) { |
| 431 | ToSet = Rela.r_addend; |
| 432 | } |
| 433 | |
| 434 | template <class ELFT, class T> |
| 435 | void initRelocations(RelocationSection<ELFT> *Relocs, |
| 436 | SymbolTableSection *SymbolTable, T RelRange) { |
| 437 | for (const auto &Rel : RelRange) { |
| 438 | Relocation ToAdd; |
| 439 | ToAdd.Offset = Rel.r_offset; |
| 440 | getAddend(ToAdd.Addend, Rel); |
| 441 | ToAdd.Type = Rel.getType(false); |
| 442 | ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false)); |
| 443 | Relocs->addRelocation(ToAdd); |
| 444 | } |
| 445 | } |
| 446 | |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 447 | SectionBase *SectionTableRef::getSection(uint16_t Index, Twine ErrMsg) { |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 448 | if (Index == SHN_UNDEF || Index > Sections.size()) |
| 449 | error(ErrMsg); |
| 450 | return Sections[Index - 1].get(); |
| 451 | } |
| 452 | |
| 453 | template <class T> |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 454 | T *SectionTableRef::getSectionOfType(uint16_t Index, Twine IndexErrMsg, |
| 455 | Twine TypeErrMsg) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 456 | if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg))) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 457 | return Sec; |
| 458 | error(TypeErrMsg); |
| 459 | } |
| 460 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 461 | template <class ELFT> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 462 | std::unique_ptr<SectionBase> |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 463 | Object<ELFT>::makeSection(const object::ELFFile<ELFT> &ElfFile, |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 464 | const Elf_Shdr &Shdr) { |
| 465 | ArrayRef<uint8_t> Data; |
| 466 | switch (Shdr.sh_type) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 467 | case SHT_REL: |
| 468 | case SHT_RELA: |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 469 | if (Shdr.sh_flags & SHF_ALLOC) { |
| 470 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 471 | return llvm::make_unique<DynamicRelocationSection>(Data); |
| 472 | } |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 473 | return llvm::make_unique<RelocationSection<ELFT>>(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 474 | case SHT_STRTAB: |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 475 | // If a string table is allocated we don't want to mess with it. That would |
| 476 | // mean altering the memory image. There are no special link types or |
| 477 | // anything so we can just use a Section. |
| 478 | if (Shdr.sh_flags & SHF_ALLOC) { |
| 479 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 480 | return llvm::make_unique<Section>(Data); |
| 481 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 482 | return llvm::make_unique<StringTableSection>(); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 483 | case SHT_HASH: |
| 484 | case SHT_GNU_HASH: |
| 485 | // Hash tables should refer to SHT_DYNSYM which we're not going to change. |
| 486 | // Because of this we don't need to mess with the hash tables either. |
| 487 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 488 | return llvm::make_unique<Section>(Data); |
| 489 | case SHT_DYNSYM: |
| 490 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 491 | return llvm::make_unique<DynamicSymbolTableSection>(Data); |
| 492 | case SHT_DYNAMIC: |
| 493 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 494 | return llvm::make_unique<DynamicSection>(Data); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 495 | case SHT_SYMTAB: { |
| 496 | auto SymTab = llvm::make_unique<SymbolTableSectionImpl<ELFT>>(); |
| 497 | SymbolTable = SymTab.get(); |
| 498 | return std::move(SymTab); |
| 499 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 500 | case SHT_NOBITS: |
| 501 | return llvm::make_unique<Section>(Data); |
| 502 | default: |
| 503 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 504 | return llvm::make_unique<Section>(Data); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 505 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | template <class ELFT> |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 509 | SectionTableRef Object<ELFT>::readSectionHeaders(const ELFFile<ELFT> &ElfFile) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 510 | uint32_t Index = 0; |
| 511 | for (const auto &Shdr : unwrapOrError(ElfFile.sections())) { |
| 512 | if (Index == 0) { |
| 513 | ++Index; |
| 514 | continue; |
| 515 | } |
| 516 | SecPtr Sec = makeSection(ElfFile, Shdr); |
| 517 | Sec->Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); |
| 518 | Sec->Type = Shdr.sh_type; |
| 519 | Sec->Flags = Shdr.sh_flags; |
| 520 | Sec->Addr = Shdr.sh_addr; |
| 521 | Sec->Offset = Shdr.sh_offset; |
| 522 | Sec->OriginalOffset = Shdr.sh_offset; |
| 523 | Sec->Size = Shdr.sh_size; |
| 524 | Sec->Link = Shdr.sh_link; |
| 525 | Sec->Info = Shdr.sh_info; |
| 526 | Sec->Align = Shdr.sh_addralign; |
| 527 | Sec->EntrySize = Shdr.sh_entsize; |
| 528 | Sec->Index = Index++; |
| 529 | Sections.push_back(std::move(Sec)); |
| 530 | } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 531 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 532 | SectionTableRef SecTable(Sections); |
| 533 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 534 | // 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] | 535 | // details about symbol tables. We need the symbol table filled out before |
| 536 | // any relocations. |
| 537 | if (SymbolTable) { |
| 538 | SymbolTable->initialize(SecTable); |
| 539 | initSymbolTable(ElfFile, SymbolTable, SecTable); |
| 540 | } |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 541 | |
| 542 | // Now that all sections and symbols have been added we can add |
| 543 | // relocations that reference symbols and set the link and info fields for |
| 544 | // relocation sections. |
| 545 | for (auto &Section : Sections) { |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 546 | if (Section.get() == SymbolTable) |
| 547 | continue; |
| 548 | Section->initialize(SecTable); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 549 | if (auto RelSec = dyn_cast<RelocationSection<ELFT>>(Section.get())) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 550 | auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; |
| 551 | if (RelSec->Type == SHT_REL) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 552 | initRelocations(RelSec, SymbolTable, unwrapOrError(ElfFile.rels(Shdr))); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 553 | else |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 554 | initRelocations(RelSec, SymbolTable, |
| 555 | unwrapOrError(ElfFile.relas(Shdr))); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 556 | } |
| 557 | } |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 558 | |
| 559 | return SecTable; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 562 | template <class ELFT> Object<ELFT>::Object(const ELFObjectFile<ELFT> &Obj) { |
| 563 | const auto &ElfFile = *Obj.getELFFile(); |
| 564 | const auto &Ehdr = *ElfFile.getHeader(); |
| 565 | |
| 566 | std::copy(Ehdr.e_ident, Ehdr.e_ident + 16, Ident); |
| 567 | Type = Ehdr.e_type; |
| 568 | Machine = Ehdr.e_machine; |
| 569 | Version = Ehdr.e_version; |
| 570 | Entry = Ehdr.e_entry; |
| 571 | Flags = Ehdr.e_flags; |
| 572 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 573 | SectionTableRef SecTable = readSectionHeaders(ElfFile); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 574 | readProgramHeaders(ElfFile); |
| 575 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 576 | SectionNames = SecTable.getSectionOfType<StringTableSection>( |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 577 | Ehdr.e_shstrndx, |
| 578 | "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " + |
| 579 | " is invalid", |
| 580 | "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + " in elf header " + |
| 581 | " is not a string table"); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 584 | template <class ELFT> |
| 585 | void Object<ELFT>::writeHeader(FileOutputBuffer &Out) const { |
| 586 | uint8_t *Buf = Out.getBufferStart(); |
| 587 | Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(Buf); |
| 588 | std::copy(Ident, Ident + 16, Ehdr.e_ident); |
| 589 | Ehdr.e_type = Type; |
| 590 | Ehdr.e_machine = Machine; |
| 591 | Ehdr.e_version = Version; |
| 592 | Ehdr.e_entry = Entry; |
| 593 | Ehdr.e_phoff = sizeof(Elf_Ehdr); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 594 | Ehdr.e_flags = Flags; |
| 595 | Ehdr.e_ehsize = sizeof(Elf_Ehdr); |
| 596 | Ehdr.e_phentsize = sizeof(Elf_Phdr); |
| 597 | Ehdr.e_phnum = Segments.size(); |
| 598 | Ehdr.e_shentsize = sizeof(Elf_Shdr); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 599 | if (WriteSectionHeaders) { |
| 600 | Ehdr.e_shoff = SHOffset; |
| 601 | Ehdr.e_shnum = Sections.size() + 1; |
| 602 | Ehdr.e_shstrndx = SectionNames->Index; |
| 603 | } else { |
| 604 | Ehdr.e_shoff = 0; |
| 605 | Ehdr.e_shnum = 0; |
| 606 | Ehdr.e_shstrndx = 0; |
| 607 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | template <class ELFT> |
| 611 | void Object<ELFT>::writeProgramHeaders(FileOutputBuffer &Out) const { |
| 612 | for (auto &Phdr : Segments) |
| 613 | Phdr->template writeHeader<ELFT>(Out); |
| 614 | } |
| 615 | |
| 616 | template <class ELFT> |
| 617 | void Object<ELFT>::writeSectionHeaders(FileOutputBuffer &Out) const { |
| 618 | uint8_t *Buf = Out.getBufferStart() + SHOffset; |
| 619 | // This reference serves to write the dummy section header at the begining |
Jake Ehrlich | 425ec9f | 2017-09-15 22:04:09 +0000 | [diff] [blame] | 620 | // of the file. It is not used for anything else |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 621 | Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(Buf); |
| 622 | Shdr.sh_name = 0; |
| 623 | Shdr.sh_type = SHT_NULL; |
| 624 | Shdr.sh_flags = 0; |
| 625 | Shdr.sh_addr = 0; |
| 626 | Shdr.sh_offset = 0; |
| 627 | Shdr.sh_size = 0; |
| 628 | Shdr.sh_link = 0; |
| 629 | Shdr.sh_info = 0; |
| 630 | Shdr.sh_addralign = 0; |
| 631 | Shdr.sh_entsize = 0; |
| 632 | |
| 633 | for (auto &Section : Sections) |
| 634 | Section->template writeHeader<ELFT>(Out); |
| 635 | } |
| 636 | |
| 637 | template <class ELFT> |
| 638 | void Object<ELFT>::writeSectionData(FileOutputBuffer &Out) const { |
| 639 | for (auto &Section : Sections) |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 640 | Section->writeSection(Out); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 641 | } |
| 642 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 643 | template <class ELFT> |
| 644 | void Object<ELFT>::removeSections( |
| 645 | std::function<bool(const SectionBase &)> ToRemove) { |
| 646 | |
| 647 | auto Iter = std::stable_partition( |
| 648 | std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { |
| 649 | if (ToRemove(*Sec)) |
| 650 | return false; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 651 | if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) { |
| 652 | if (auto ToRelSec = RelSec->getSection()) |
| 653 | return !ToRemove(*ToRelSec); |
| 654 | } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 655 | return true; |
| 656 | }); |
| 657 | if (SymbolTable != nullptr && ToRemove(*SymbolTable)) |
| 658 | SymbolTable = nullptr; |
| 659 | if (ToRemove(*SectionNames)) { |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 660 | if (WriteSectionHeaders) |
| 661 | error("Cannot remove " + SectionNames->Name + |
| 662 | " because it is the section header string table."); |
| 663 | SectionNames = nullptr; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 664 | } |
| 665 | // Now make sure there are no remaining references to the sections that will |
| 666 | // be removed. Sometimes it is impossible to remove a reference so we emit |
| 667 | // an error here instead. |
| 668 | for (auto &RemoveSec : make_range(Iter, std::end(Sections))) { |
| 669 | for (auto &Segment : Segments) |
| 670 | Segment->removeSection(RemoveSec.get()); |
| 671 | for (auto &KeepSec : make_range(std::begin(Sections), Iter)) |
| 672 | KeepSec->removeSectionReferences(RemoveSec.get()); |
| 673 | } |
| 674 | // Now finally get rid of them all togethor. |
| 675 | Sections.erase(Iter, std::end(Sections)); |
| 676 | } |
| 677 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 678 | template <class ELFT> void ELFObject<ELFT>::sortSections() { |
| 679 | // Put all sections in offset order. Maintain the ordering as closely as |
| 680 | // possible while meeting that demand however. |
| 681 | auto CompareSections = [](const SecPtr &A, const SecPtr &B) { |
| 682 | return A->OriginalOffset < B->OriginalOffset; |
| 683 | }; |
| 684 | std::stable_sort(std::begin(this->Sections), std::end(this->Sections), |
| 685 | CompareSections); |
| 686 | } |
| 687 | |
| 688 | template <class ELFT> void ELFObject<ELFT>::assignOffsets() { |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 689 | // We need a temporary list of segments that has a special order to it |
| 690 | // so that we know that anytime ->ParentSegment is set that segment has |
| 691 | // already had it's offset properly set. |
| 692 | std::vector<Segment *> OrderedSegments; |
| 693 | for (auto &Segment : this->Segments) |
| 694 | OrderedSegments.push_back(Segment.get()); |
| 695 | auto CompareSegments = [](const Segment *A, const Segment *B) { |
| 696 | // Any segment without a parent segment should come before a segment |
| 697 | // that has a parent segment. |
| 698 | if (A->OriginalOffset < B->OriginalOffset) |
| 699 | return true; |
| 700 | if (A->OriginalOffset > B->OriginalOffset) |
| 701 | return false; |
| 702 | return A->Index < B->Index; |
| 703 | }; |
| 704 | std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments), |
| 705 | CompareSegments); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 706 | // The size of ELF + program headers will not change so it is ok to assume |
| 707 | // that the first offset of the first segment is a good place to start |
| 708 | // outputting sections. This covers both the standard case and the PT_PHDR |
| 709 | // case. |
| 710 | uint64_t Offset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 711 | if (!OrderedSegments.empty()) { |
| 712 | Offset = OrderedSegments[0]->Offset; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 713 | } else { |
| 714 | Offset = sizeof(Elf_Ehdr); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 715 | } |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 716 | // The only way a segment should move is if a section was between two |
| 717 | // segments and that section was removed. If that section isn't in a segment |
| 718 | // then it's acceptable, but not ideal, to simply move it to after the |
| 719 | // segments. So we can simply layout segments one after the other accounting |
| 720 | // for alignment. |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 721 | for (auto &Segment : OrderedSegments) { |
| 722 | // We assume that segments have been ordered by OriginalOffset and Index |
| 723 | // such that a parent segment will always come before a child segment in |
| 724 | // OrderedSegments. This means that the Offset of the ParentSegment should |
| 725 | // already be set and we can set our offset relative to it. |
| 726 | if (Segment->ParentSegment != nullptr) { |
| 727 | auto Parent = Segment->ParentSegment; |
| 728 | Segment->Offset = |
| 729 | Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset; |
| 730 | } else { |
| 731 | Offset = alignTo(Offset, Segment->Align == 0 ? 1 : Segment->Align); |
| 732 | Segment->Offset = Offset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 733 | } |
Jake Ehrlich | 084400b | 2017-10-04 17:44:42 +0000 | [diff] [blame] | 734 | Offset = std::max(Offset, Segment->Offset + Segment->FileSize); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 735 | } |
| 736 | // Now the offset of every segment has been set we can assign the offsets |
| 737 | // of each section. For sections that are covered by a segment we should use |
| 738 | // the segment's original offset and the section's original offset to compute |
| 739 | // the offset from the start of the segment. Using the offset from the start |
| 740 | // of the segment we can assign a new offset to the section. For sections not |
| 741 | // covered by segments we can just bump Offset to the next valid location. |
| 742 | uint32_t Index = 1; |
| 743 | for (auto &Section : this->Sections) { |
| 744 | Section->Index = Index++; |
| 745 | if (Section->ParentSegment != nullptr) { |
| 746 | auto Segment = Section->ParentSegment; |
| 747 | Section->Offset = |
| 748 | Segment->Offset + (Section->OriginalOffset - Segment->OriginalOffset); |
| 749 | } else { |
Jake Ehrlich | 084400b | 2017-10-04 17:44:42 +0000 | [diff] [blame] | 750 | Offset = alignTo(Offset, Section->Align == 0 ? 1 : Section->Align); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 751 | Section->Offset = Offset; |
| 752 | if (Section->Type != SHT_NOBITS) |
| 753 | Offset += Section->Size; |
| 754 | } |
| 755 | } |
| 756 | |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 757 | if (this->WriteSectionHeaders) { |
| 758 | Offset = alignTo(Offset, sizeof(typename ELFT::Addr)); |
| 759 | } |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 760 | this->SHOffset = Offset; |
| 761 | } |
| 762 | |
| 763 | template <class ELFT> size_t ELFObject<ELFT>::totalSize() const { |
| 764 | // We already have the section header offset so we can calculate the total |
| 765 | // size by just adding up the size of each section header. |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 766 | auto NullSectionSize = this->WriteSectionHeaders ? sizeof(Elf_Shdr) : 0; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 767 | return this->SHOffset + this->Sections.size() * sizeof(Elf_Shdr) + |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 768 | NullSectionSize; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 769 | } |
| 770 | |
| 771 | template <class ELFT> void ELFObject<ELFT>::write(FileOutputBuffer &Out) const { |
| 772 | this->writeHeader(Out); |
| 773 | this->writeProgramHeaders(Out); |
| 774 | this->writeSectionData(Out); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 775 | if (this->WriteSectionHeaders) |
| 776 | this->writeSectionHeaders(Out); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | template <class ELFT> void ELFObject<ELFT>::finalize() { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 780 | // Make sure we add the names of all the sections. |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 781 | if (this->SectionNames != nullptr) |
| 782 | for (const auto &Section : this->Sections) { |
| 783 | this->SectionNames->addString(Section->Name); |
| 784 | } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 785 | // Make sure we add the names of all the symbols. |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 786 | if (this->SymbolTable != nullptr) |
| 787 | this->SymbolTable->addSymbolNames(); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 788 | |
| 789 | sortSections(); |
| 790 | assignOffsets(); |
| 791 | |
| 792 | // Finalize SectionNames first so that we can assign name indexes. |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 793 | if (this->SectionNames != nullptr) |
| 794 | this->SectionNames->finalize(); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 795 | // Finally now that all offsets and indexes have been set we can finalize any |
| 796 | // remaining issues. |
| 797 | uint64_t Offset = this->SHOffset + sizeof(Elf_Shdr); |
| 798 | for (auto &Section : this->Sections) { |
| 799 | Section->HeaderOffset = Offset; |
| 800 | Offset += sizeof(Elf_Shdr); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 801 | if (this->WriteSectionHeaders) |
| 802 | Section->NameIndex = this->SectionNames->findIndex(Section->Name); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 803 | Section->finalize(); |
| 804 | } |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| 807 | template <class ELFT> size_t BinaryObject<ELFT>::totalSize() const { |
| 808 | return TotalSize; |
| 809 | } |
| 810 | |
| 811 | template <class ELFT> |
| 812 | void BinaryObject<ELFT>::write(FileOutputBuffer &Out) const { |
| 813 | for (auto &Segment : this->Segments) { |
Petr Hosek | d53951d | 2017-08-04 23:18:18 +0000 | [diff] [blame] | 814 | // GNU objcopy does not output segments that do not cover a section. Such |
| 815 | // segments can sometimes be produced by LLD due to how LLD handles PT_PHDR. |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 816 | if (Segment->Type == PT_LOAD && Segment->firstSection() != nullptr) { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 817 | Segment->writeSegment(Out); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | template <class ELFT> void BinaryObject<ELFT>::finalize() { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 823 | // Put all segments in offset order. |
| 824 | auto CompareSegments = [](const SegPtr &A, const SegPtr &B) { |
| 825 | return A->Offset < B->Offset; |
| 826 | }; |
| 827 | std::sort(std::begin(this->Segments), std::end(this->Segments), |
| 828 | CompareSegments); |
| 829 | |
| 830 | uint64_t Offset = 0; |
| 831 | for (auto &Segment : this->Segments) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 832 | if (Segment->Type == PT_LOAD && Segment->firstSection() != nullptr) { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 833 | Offset = alignTo(Offset, Segment->Align); |
| 834 | Segment->Offset = Offset; |
| 835 | Offset += Segment->FileSize; |
| 836 | } |
| 837 | } |
| 838 | TotalSize = Offset; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 841 | namespace llvm { |
| 842 | |
Petr Hosek | 35fdbd5 | 2017-08-01 05:31:50 +0000 | [diff] [blame] | 843 | template class Object<ELF64LE>; |
| 844 | template class Object<ELF64BE>; |
| 845 | template class Object<ELF32LE>; |
| 846 | template class Object<ELF32BE>; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 847 | |
| 848 | template class ELFObject<ELF64LE>; |
| 849 | template class ELFObject<ELF64BE>; |
| 850 | template class ELFObject<ELF32LE>; |
| 851 | template class ELFObject<ELF32BE>; |
| 852 | |
| 853 | template class BinaryObject<ELF64LE>; |
| 854 | template class BinaryObject<ELF64BE>; |
| 855 | template class BinaryObject<ELF32LE>; |
| 856 | template class BinaryObject<ELF32BE>; |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame^] | 857 | |
| 858 | } // end namespace llvm |