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