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" |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame^] | 18 | #include "llvm/MC/MCTargetOptions.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 19 | #include "llvm/Object/ELFObjectFile.h" |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame^] | 20 | #include "llvm/Support/Compression.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 21 | #include "llvm/Support/ErrorHandling.h" |
| 22 | #include "llvm/Support/FileOutputBuffer.h" |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Path.h" |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 24 | #include <algorithm> |
| 25 | #include <cstddef> |
| 26 | #include <cstdint> |
| 27 | #include <iterator> |
| 28 | #include <utility> |
| 29 | #include <vector> |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 32 | using namespace llvm::objcopy; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 33 | using namespace object; |
| 34 | using namespace ELF; |
| 35 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 36 | Buffer::~Buffer() {} |
| 37 | |
| 38 | void FileBuffer::allocate(size_t Size) { |
| 39 | Expected<std::unique_ptr<FileOutputBuffer>> BufferOrErr = |
| 40 | FileOutputBuffer::create(getName(), Size, FileOutputBuffer::F_executable); |
| 41 | handleAllErrors(BufferOrErr.takeError(), [this](const ErrorInfoBase &E) { |
| 42 | error("failed to open " + getName() + ": " + E.message()); |
| 43 | }); |
| 44 | Buf = std::move(*BufferOrErr); |
| 45 | } |
| 46 | |
| 47 | Error FileBuffer::commit() { return Buf->commit(); } |
| 48 | |
| 49 | uint8_t *FileBuffer::getBufferStart() { |
| 50 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 51 | } |
| 52 | |
| 53 | void MemBuffer::allocate(size_t Size) { |
| 54 | Buf = WritableMemoryBuffer::getNewMemBuffer(Size, getName()); |
| 55 | } |
| 56 | |
| 57 | Error MemBuffer::commit() { return Error::success(); } |
| 58 | |
| 59 | uint8_t *MemBuffer::getBufferStart() { |
| 60 | return reinterpret_cast<uint8_t *>(Buf->getBufferStart()); |
| 61 | } |
| 62 | |
| 63 | std::unique_ptr<WritableMemoryBuffer> MemBuffer::releaseMemoryBuffer() { |
| 64 | return std::move(Buf); |
| 65 | } |
| 66 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 67 | template <class ELFT> void ELFWriter<ELFT>::writePhdr(const Segment &Seg) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 68 | uint8_t *B = Buf.getBufferStart(); |
| 69 | B += Obj.ProgramHdrSegment.Offset + Seg.Index * sizeof(Elf_Phdr); |
| 70 | Elf_Phdr &Phdr = *reinterpret_cast<Elf_Phdr *>(B); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 71 | Phdr.p_type = Seg.Type; |
| 72 | Phdr.p_flags = Seg.Flags; |
| 73 | Phdr.p_offset = Seg.Offset; |
| 74 | Phdr.p_vaddr = Seg.VAddr; |
| 75 | Phdr.p_paddr = Seg.PAddr; |
| 76 | Phdr.p_filesz = Seg.FileSize; |
| 77 | Phdr.p_memsz = Seg.MemSize; |
| 78 | Phdr.p_align = Seg.Align; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 81 | void SectionBase::removeSectionReferences(const SectionBase *Sec) {} |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 82 | void SectionBase::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) {} |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 83 | void SectionBase::initialize(SectionTableRef SecTable) {} |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 84 | void SectionBase::finalize() {} |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 85 | void SectionBase::markSymbols() {} |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 86 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 87 | template <class ELFT> void ELFWriter<ELFT>::writeShdr(const SectionBase &Sec) { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 88 | uint8_t *B = Buf.getBufferStart(); |
| 89 | B += Sec.HeaderOffset; |
Jordan Rupprecht | de965ea | 2018-08-10 16:25:58 +0000 | [diff] [blame] | 90 | Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 91 | Shdr.sh_name = Sec.NameIndex; |
| 92 | Shdr.sh_type = Sec.Type; |
| 93 | Shdr.sh_flags = Sec.Flags; |
| 94 | Shdr.sh_addr = Sec.Addr; |
| 95 | Shdr.sh_offset = Sec.Offset; |
| 96 | Shdr.sh_size = Sec.Size; |
| 97 | Shdr.sh_link = Sec.Link; |
| 98 | Shdr.sh_info = Sec.Info; |
| 99 | Shdr.sh_addralign = Sec.Align; |
| 100 | Shdr.sh_entsize = Sec.EntrySize; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 103 | SectionVisitor::~SectionVisitor() {} |
| 104 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 105 | void BinarySectionWriter::visit(const SectionIndexSection &Sec) { |
| 106 | error("Cannot write symbol section index table '" + Sec.Name + "' "); |
| 107 | } |
| 108 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 109 | void BinarySectionWriter::visit(const SymbolTableSection &Sec) { |
| 110 | error("Cannot write symbol table '" + Sec.Name + "' out to binary"); |
| 111 | } |
| 112 | |
| 113 | void BinarySectionWriter::visit(const RelocationSection &Sec) { |
| 114 | error("Cannot write relocation section '" + Sec.Name + "' out to binary"); |
| 115 | } |
| 116 | |
| 117 | void BinarySectionWriter::visit(const GnuDebugLinkSection &Sec) { |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 118 | error("Cannot write '" + Sec.Name + "' out to binary"); |
| 119 | } |
| 120 | |
| 121 | void BinarySectionWriter::visit(const GroupSection &Sec) { |
| 122 | error("Cannot write '" + Sec.Name + "' out to binary"); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void SectionWriter::visit(const Section &Sec) { |
| 126 | if (Sec.Type == SHT_NOBITS) |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 127 | return; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 128 | uint8_t *Buf = Out.getBufferStart() + Sec.Offset; |
| 129 | std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), Buf); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 132 | void Section::accept(SectionVisitor &Visitor) const { Visitor.visit(*this); } |
| 133 | |
| 134 | void SectionWriter::visit(const OwnedDataSection &Sec) { |
| 135 | uint8_t *Buf = Out.getBufferStart() + Sec.Offset; |
| 136 | std::copy(std::begin(Sec.Data), std::end(Sec.Data), Buf); |
| 137 | } |
| 138 | |
| 139 | void OwnedDataSection::accept(SectionVisitor &Visitor) const { |
| 140 | Visitor.visit(*this); |
Jake Ehrlich | e8437de | 2017-12-19 00:47:30 +0000 | [diff] [blame] | 141 | } |
| 142 | |
Puyan Lotfi | 99124cc | 2018-09-07 08:10:22 +0000 | [diff] [blame^] | 143 | void BinarySectionWriter::visit(const CompressedSection &Sec) { |
| 144 | error("Cannot write compressed section '" + Sec.Name + "' "); |
| 145 | } |
| 146 | |
| 147 | template <class ELFT> |
| 148 | void ELFSectionWriter<ELFT>::visit(const CompressedSection &Sec) { |
| 149 | uint8_t *Buf = Out.getBufferStart(); |
| 150 | Buf += Sec.Offset; |
| 151 | |
| 152 | if (Sec.CompressionType == DebugCompressionType::None) { |
| 153 | std::copy(Sec.OriginalData.begin(), Sec.OriginalData.end(), Buf); |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | if (Sec.CompressionType == DebugCompressionType::GNU) { |
| 158 | const char *Magic = "ZLIB"; |
| 159 | memcpy(Buf, Magic, strlen(Magic)); |
| 160 | Buf += strlen(Magic); |
| 161 | const uint64_t DecompressedSize = |
| 162 | support::endian::read64be(&Sec.DecompressedSize); |
| 163 | memcpy(Buf, &DecompressedSize, sizeof(DecompressedSize)); |
| 164 | Buf += sizeof(DecompressedSize); |
| 165 | } else { |
| 166 | Elf_Chdr_Impl<ELFT> Chdr; |
| 167 | Chdr.ch_type = ELF::ELFCOMPRESS_ZLIB; |
| 168 | Chdr.ch_size = Sec.DecompressedSize; |
| 169 | Chdr.ch_addralign = Sec.DecompressedAlign; |
| 170 | memcpy(Buf, &Chdr, sizeof(Chdr)); |
| 171 | Buf += sizeof(Chdr); |
| 172 | } |
| 173 | |
| 174 | std::copy(Sec.CompressedData.begin(), Sec.CompressedData.end(), Buf); |
| 175 | } |
| 176 | |
| 177 | CompressedSection::CompressedSection(const SectionBase &Sec, |
| 178 | DebugCompressionType CompressionType) |
| 179 | : SectionBase(Sec), CompressionType(CompressionType), |
| 180 | DecompressedSize(Sec.OriginalData.size()), DecompressedAlign(Sec.Align) { |
| 181 | |
| 182 | if (!zlib::isAvailable()) { |
| 183 | CompressionType = DebugCompressionType::None; |
| 184 | return; |
| 185 | } |
| 186 | |
| 187 | if (Error E = zlib::compress( |
| 188 | StringRef(reinterpret_cast<const char *>(OriginalData.data()), |
| 189 | OriginalData.size()), |
| 190 | CompressedData)) |
| 191 | reportError(Name, std::move(E)); |
| 192 | |
| 193 | size_t ChdrSize; |
| 194 | if (CompressionType == DebugCompressionType::GNU) { |
| 195 | Name = ".z" + Sec.Name.substr(1); |
| 196 | ChdrSize = sizeof("ZLIB") - 1 + sizeof(uint64_t); |
| 197 | } else { |
| 198 | Flags |= ELF::SHF_COMPRESSED; |
| 199 | ChdrSize = |
| 200 | std::max(std::max(sizeof(object::Elf_Chdr_Impl<object::ELF64LE>), |
| 201 | sizeof(object::Elf_Chdr_Impl<object::ELF64BE>)), |
| 202 | std::max(sizeof(object::Elf_Chdr_Impl<object::ELF32LE>), |
| 203 | sizeof(object::Elf_Chdr_Impl<object::ELF32BE>))); |
| 204 | } |
| 205 | Size = ChdrSize + CompressedData.size(); |
| 206 | Align = 8; |
| 207 | } |
| 208 | |
| 209 | void CompressedSection::accept(SectionVisitor &Visitor) const { |
| 210 | Visitor.visit(*this); |
| 211 | } |
| 212 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 213 | void StringTableSection::addString(StringRef Name) { |
| 214 | StrTabBuilder.add(Name); |
| 215 | Size = StrTabBuilder.getSize(); |
| 216 | } |
| 217 | |
| 218 | uint32_t StringTableSection::findIndex(StringRef Name) const { |
| 219 | return StrTabBuilder.getOffset(Name); |
| 220 | } |
| 221 | |
| 222 | void StringTableSection::finalize() { StrTabBuilder.finalize(); } |
| 223 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 224 | void SectionWriter::visit(const StringTableSection &Sec) { |
| 225 | Sec.StrTabBuilder.write(Out.getBufferStart() + Sec.Offset); |
| 226 | } |
| 227 | |
| 228 | void StringTableSection::accept(SectionVisitor &Visitor) const { |
| 229 | Visitor.visit(*this); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 232 | template <class ELFT> |
| 233 | void ELFSectionWriter<ELFT>::visit(const SectionIndexSection &Sec) { |
| 234 | uint8_t *Buf = Out.getBufferStart() + Sec.Offset; |
Jordan Rupprecht | de965ea | 2018-08-10 16:25:58 +0000 | [diff] [blame] | 235 | auto *IndexesBuffer = reinterpret_cast<Elf_Word *>(Buf); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 236 | std::copy(std::begin(Sec.Indexes), std::end(Sec.Indexes), IndexesBuffer); |
| 237 | } |
| 238 | |
| 239 | void SectionIndexSection::initialize(SectionTableRef SecTable) { |
| 240 | Size = 0; |
| 241 | setSymTab(SecTable.getSectionOfType<SymbolTableSection>( |
| 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 | Symbols->setShndxTable(this); |
| 247 | } |
| 248 | |
| 249 | void SectionIndexSection::finalize() { Link = Symbols->Index; } |
| 250 | |
| 251 | void SectionIndexSection::accept(SectionVisitor &Visitor) const { |
| 252 | Visitor.visit(*this); |
| 253 | } |
| 254 | |
Petr Hosek | c113577 | 2017-09-13 03:04:50 +0000 | [diff] [blame] | 255 | static bool isValidReservedSectionIndex(uint16_t Index, uint16_t Machine) { |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 256 | switch (Index) { |
| 257 | case SHN_ABS: |
| 258 | case SHN_COMMON: |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 259 | return true; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 260 | } |
Petr Hosek | c113577 | 2017-09-13 03:04:50 +0000 | [diff] [blame] | 261 | if (Machine == EM_HEXAGON) { |
| 262 | switch (Index) { |
| 263 | case SHN_HEXAGON_SCOMMON: |
| 264 | case SHN_HEXAGON_SCOMMON_2: |
| 265 | case SHN_HEXAGON_SCOMMON_4: |
| 266 | case SHN_HEXAGON_SCOMMON_8: |
| 267 | return true; |
| 268 | } |
| 269 | } |
| 270 | return false; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 273 | // Large indexes force us to clarify exactly what this function should do. This |
| 274 | // function should return the value that will appear in st_shndx when written |
| 275 | // out. |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 276 | uint16_t Symbol::getShndx() const { |
| 277 | if (DefinedIn != nullptr) { |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 278 | if (DefinedIn->Index >= SHN_LORESERVE) |
| 279 | return SHN_XINDEX; |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 280 | return DefinedIn->Index; |
| 281 | } |
| 282 | switch (ShndxType) { |
| 283 | // This means that we don't have a defined section but we do need to |
| 284 | // output a legitimate section index. |
| 285 | case SYMBOL_SIMPLE_INDEX: |
| 286 | return SHN_UNDEF; |
| 287 | case SYMBOL_ABS: |
| 288 | case SYMBOL_COMMON: |
| 289 | case SYMBOL_HEXAGON_SCOMMON: |
| 290 | case SYMBOL_HEXAGON_SCOMMON_2: |
| 291 | case SYMBOL_HEXAGON_SCOMMON_4: |
| 292 | case SYMBOL_HEXAGON_SCOMMON_8: |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 293 | case SYMBOL_XINDEX: |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 294 | return static_cast<uint16_t>(ShndxType); |
| 295 | } |
| 296 | llvm_unreachable("Symbol with invalid ShndxType encountered"); |
| 297 | } |
| 298 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 299 | void SymbolTableSection::assignIndices() { |
| 300 | uint32_t Index = 0; |
| 301 | for (auto &Sym : Symbols) |
| 302 | Sym->Index = Index++; |
| 303 | } |
| 304 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 305 | void SymbolTableSection::addSymbol(Twine Name, uint8_t Bind, uint8_t Type, |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 306 | SectionBase *DefinedIn, uint64_t Value, |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 307 | uint8_t Visibility, uint16_t Shndx, |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 308 | uint64_t Size) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 309 | Symbol Sym; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 310 | Sym.Name = Name.str(); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 311 | Sym.Binding = Bind; |
| 312 | Sym.Type = Type; |
| 313 | Sym.DefinedIn = DefinedIn; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 314 | if (DefinedIn != nullptr) |
| 315 | DefinedIn->HasSymbol = true; |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 316 | if (DefinedIn == nullptr) { |
| 317 | if (Shndx >= SHN_LORESERVE) |
| 318 | Sym.ShndxType = static_cast<SymbolShndxType>(Shndx); |
| 319 | else |
| 320 | Sym.ShndxType = SYMBOL_SIMPLE_INDEX; |
| 321 | } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 322 | Sym.Value = Value; |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 323 | Sym.Visibility = Visibility; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 324 | Sym.Size = Size; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 325 | Sym.Index = Symbols.size(); |
| 326 | Symbols.emplace_back(llvm::make_unique<Symbol>(Sym)); |
| 327 | Size += this->EntrySize; |
| 328 | } |
| 329 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 330 | void SymbolTableSection::removeSectionReferences(const SectionBase *Sec) { |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 331 | if (SectionIndexTable == Sec) |
| 332 | SectionIndexTable = nullptr; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 333 | if (SymbolNames == Sec) { |
| 334 | error("String table " + SymbolNames->Name + |
| 335 | " cannot be removed because it is referenced by the symbol table " + |
| 336 | this->Name); |
| 337 | } |
Paul Semel | 41695f8 | 2018-05-02 20:19:22 +0000 | [diff] [blame] | 338 | removeSymbols([Sec](const Symbol &Sym) { return Sym.DefinedIn == Sec; }); |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Alexander Shaposhnikov | 40e9bdf | 2018-04-26 18:28:17 +0000 | [diff] [blame] | 341 | void SymbolTableSection::updateSymbols(function_ref<void(Symbol &)> Callable) { |
Paul Semel | 46201fb | 2018-06-01 16:19:46 +0000 | [diff] [blame] | 342 | std::for_each(std::begin(Symbols) + 1, std::end(Symbols), |
| 343 | [Callable](SymPtr &Sym) { Callable(*Sym); }); |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 344 | std::stable_partition( |
| 345 | std::begin(Symbols), std::end(Symbols), |
| 346 | [](const SymPtr &Sym) { return Sym->Binding == STB_LOCAL; }); |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 347 | assignIndices(); |
Jake Ehrlich | 27a29b0 | 2018-01-05 19:19:09 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 350 | void SymbolTableSection::removeSymbols( |
| 351 | function_ref<bool(const Symbol &)> ToRemove) { |
Paul Semel | 41695f8 | 2018-05-02 20:19:22 +0000 | [diff] [blame] | 352 | Symbols.erase( |
Paul Semel | 46201fb | 2018-06-01 16:19:46 +0000 | [diff] [blame] | 353 | std::remove_if(std::begin(Symbols) + 1, std::end(Symbols), |
Paul Semel | 41695f8 | 2018-05-02 20:19:22 +0000 | [diff] [blame] | 354 | [ToRemove](const SymPtr &Sym) { return ToRemove(*Sym); }), |
| 355 | std::end(Symbols)); |
| 356 | Size = Symbols.size() * EntrySize; |
| 357 | assignIndices(); |
| 358 | } |
| 359 | |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 360 | void SymbolTableSection::initialize(SectionTableRef SecTable) { |
| 361 | Size = 0; |
| 362 | setStrTab(SecTable.getSectionOfType<StringTableSection>( |
| 363 | Link, |
| 364 | "Symbol table has link index of " + Twine(Link) + |
| 365 | " which is not a valid index", |
| 366 | "Symbol table has link index of " + Twine(Link) + |
| 367 | " which is not a string table")); |
| 368 | } |
| 369 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 370 | void SymbolTableSection::finalize() { |
| 371 | // Make sure SymbolNames is finalized before getting name indexes. |
| 372 | SymbolNames->finalize(); |
| 373 | |
| 374 | uint32_t MaxLocalIndex = 0; |
| 375 | for (auto &Sym : Symbols) { |
| 376 | Sym->NameIndex = SymbolNames->findIndex(Sym->Name); |
| 377 | if (Sym->Binding == STB_LOCAL) |
| 378 | MaxLocalIndex = std::max(MaxLocalIndex, Sym->Index); |
| 379 | } |
| 380 | // Now we need to set the Link and Info fields. |
| 381 | Link = SymbolNames->Index; |
| 382 | Info = MaxLocalIndex + 1; |
| 383 | } |
| 384 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 385 | void SymbolTableSection::prepareForLayout() { |
| 386 | // Add all potential section indexes before file layout so that the section |
| 387 | // index section has the approprite size. |
| 388 | if (SectionIndexTable != nullptr) { |
| 389 | for (const auto &Sym : Symbols) { |
| 390 | if (Sym->DefinedIn != nullptr && Sym->DefinedIn->Index >= SHN_LORESERVE) |
| 391 | SectionIndexTable->addIndex(Sym->DefinedIn->Index); |
| 392 | else |
| 393 | SectionIndexTable->addIndex(SHN_UNDEF); |
| 394 | } |
| 395 | } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 396 | // Add all of our strings to SymbolNames so that SymbolNames has the right |
| 397 | // size before layout is decided. |
| 398 | for (auto &Sym : Symbols) |
| 399 | SymbolNames->addString(Sym->Name); |
| 400 | } |
| 401 | |
| 402 | const Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) const { |
| 403 | if (Symbols.size() <= Index) |
| 404 | error("Invalid symbol index: " + Twine(Index)); |
| 405 | return Symbols[Index].get(); |
| 406 | } |
| 407 | |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 408 | Symbol *SymbolTableSection::getSymbolByIndex(uint32_t Index) { |
| 409 | return const_cast<Symbol *>( |
| 410 | static_cast<const SymbolTableSection *>(this)->getSymbolByIndex(Index)); |
| 411 | } |
| 412 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 413 | template <class ELFT> |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 414 | void ELFSectionWriter<ELFT>::visit(const SymbolTableSection &Sec) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 415 | uint8_t *Buf = Out.getBufferStart(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 416 | Buf += Sec.Offset; |
Jordan Rupprecht | de965ea | 2018-08-10 16:25:58 +0000 | [diff] [blame] | 417 | Elf_Sym *Sym = reinterpret_cast<Elf_Sym *>(Buf); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 418 | // Loop though symbols setting each entry of the symbol table. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 419 | for (auto &Symbol : Sec.Symbols) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 420 | Sym->st_name = Symbol->NameIndex; |
| 421 | Sym->st_value = Symbol->Value; |
| 422 | Sym->st_size = Symbol->Size; |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 423 | Sym->st_other = Symbol->Visibility; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 424 | Sym->setBinding(Symbol->Binding); |
| 425 | Sym->setType(Symbol->Type); |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 426 | Sym->st_shndx = Symbol->getShndx(); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 427 | ++Sym; |
| 428 | } |
| 429 | } |
| 430 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 431 | void SymbolTableSection::accept(SectionVisitor &Visitor) const { |
| 432 | Visitor.visit(*this); |
| 433 | } |
| 434 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 435 | template <class SymTabType> |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 436 | void RelocSectionWithSymtabBase<SymTabType>::removeSectionReferences( |
| 437 | const SectionBase *Sec) { |
| 438 | if (Symbols == Sec) { |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 439 | error("Symbol table " + Symbols->Name + |
| 440 | " cannot be removed because it is " |
| 441 | "referenced by the relocation " |
| 442 | "section " + |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 443 | this->Name); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | template <class SymTabType> |
| 448 | void RelocSectionWithSymtabBase<SymTabType>::initialize( |
| 449 | SectionTableRef SecTable) { |
Jordan Rupprecht | ec277a8 | 2018-09-04 22:28:49 +0000 | [diff] [blame] | 450 | if (Link != SHN_UNDEF) |
| 451 | setSymTab(SecTable.getSectionOfType<SymTabType>( |
| 452 | Link, |
| 453 | "Link field value " + Twine(Link) + " in section " + Name + |
| 454 | " is invalid", |
| 455 | "Link field value " + Twine(Link) + " in section " + Name + |
| 456 | " is not a symbol table")); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 457 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 458 | if (Info != SHN_UNDEF) |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 459 | setSection(SecTable.getSection(Info, "Info field value " + Twine(Info) + |
| 460 | " in section " + Name + |
| 461 | " is invalid")); |
James Y Knight | 2ea995a | 2017-09-26 22:44:01 +0000 | [diff] [blame] | 462 | else |
| 463 | setSection(nullptr); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 464 | } |
| 465 | |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 466 | template <class SymTabType> |
| 467 | void RelocSectionWithSymtabBase<SymTabType>::finalize() { |
Jordan Rupprecht | ec277a8 | 2018-09-04 22:28:49 +0000 | [diff] [blame] | 468 | this->Link = Symbols ? Symbols->Index : 0; |
| 469 | |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 470 | if (SecToApplyRel != nullptr) |
| 471 | this->Info = SecToApplyRel->Index; |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | template <class ELFT> |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 475 | static void setAddend(Elf_Rel_Impl<ELFT, false> &Rel, uint64_t Addend) {} |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 476 | |
| 477 | template <class ELFT> |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 478 | static void setAddend(Elf_Rel_Impl<ELFT, true> &Rela, uint64_t Addend) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 479 | Rela.r_addend = Addend; |
| 480 | } |
| 481 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 482 | template <class RelRange, class T> |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 483 | static void writeRel(const RelRange &Relocations, T *Buf) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 484 | for (const auto &Reloc : Relocations) { |
| 485 | Buf->r_offset = Reloc.Offset; |
| 486 | setAddend(*Buf, Reloc.Addend); |
| 487 | Buf->setSymbolAndType(Reloc.RelocSymbol->Index, Reloc.Type, false); |
| 488 | ++Buf; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | template <class ELFT> |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 493 | void ELFSectionWriter<ELFT>::visit(const RelocationSection &Sec) { |
| 494 | uint8_t *Buf = Out.getBufferStart() + Sec.Offset; |
| 495 | if (Sec.Type == SHT_REL) |
| 496 | writeRel(Sec.Relocations, reinterpret_cast<Elf_Rel *>(Buf)); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 497 | else |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 498 | writeRel(Sec.Relocations, reinterpret_cast<Elf_Rela *>(Buf)); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 499 | } |
| 500 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 501 | void RelocationSection::accept(SectionVisitor &Visitor) const { |
| 502 | Visitor.visit(*this); |
| 503 | } |
| 504 | |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 505 | void RelocationSection::removeSymbols( |
| 506 | function_ref<bool(const Symbol &)> ToRemove) { |
| 507 | for (const Relocation &Reloc : Relocations) |
| 508 | if (ToRemove(*Reloc.RelocSymbol)) |
Jordan Rupprecht | 88ed5e5 | 2018-08-09 22:52:03 +0000 | [diff] [blame] | 509 | error("not stripping symbol '" + Reloc.RelocSymbol->Name + |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 510 | "' because it is named in a relocation"); |
| 511 | } |
| 512 | |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 513 | void RelocationSection::markSymbols() { |
| 514 | for (const Relocation &Reloc : Relocations) |
| 515 | Reloc.RelocSymbol->Referenced = true; |
| 516 | } |
| 517 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 518 | void SectionWriter::visit(const DynamicRelocationSection &Sec) { |
| 519 | std::copy(std::begin(Sec.Contents), std::end(Sec.Contents), |
| 520 | Out.getBufferStart() + Sec.Offset); |
| 521 | } |
| 522 | |
| 523 | void DynamicRelocationSection::accept(SectionVisitor &Visitor) const { |
| 524 | Visitor.visit(*this); |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 527 | void Section::removeSectionReferences(const SectionBase *Sec) { |
| 528 | if (LinkSection == Sec) { |
| 529 | error("Section " + LinkSection->Name + |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 530 | " cannot be removed because it is " |
| 531 | "referenced by the section " + |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 532 | this->Name); |
| 533 | } |
| 534 | } |
| 535 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 536 | void GroupSection::finalize() { |
| 537 | this->Info = Sym->Index; |
| 538 | this->Link = SymTab->Index; |
| 539 | } |
| 540 | |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 541 | void GroupSection::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { |
| 542 | if (ToRemove(*Sym)) { |
| 543 | error("Symbol " + Sym->Name + |
| 544 | " cannot be removed because it is " |
| 545 | "referenced by the section " + |
| 546 | this->Name + "[" + Twine(this->Index) + "]"); |
| 547 | } |
| 548 | } |
| 549 | |
Paul Semel | 99dda0b | 2018-05-25 11:01:25 +0000 | [diff] [blame] | 550 | void GroupSection::markSymbols() { |
| 551 | if (Sym) |
| 552 | Sym->Referenced = true; |
| 553 | } |
| 554 | |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 555 | void Section::initialize(SectionTableRef SecTable) { |
Peter Collingbourne | 1651ac1 | 2018-05-30 19:30:39 +0000 | [diff] [blame] | 556 | if (Link != ELF::SHN_UNDEF) { |
Alexander Shaposhnikov | 52db433 | 2018-04-20 20:46:04 +0000 | [diff] [blame] | 557 | LinkSection = |
| 558 | SecTable.getSection(Link, "Link field value " + Twine(Link) + |
| 559 | " in section " + Name + " is invalid"); |
Peter Collingbourne | 1651ac1 | 2018-05-30 19:30:39 +0000 | [diff] [blame] | 560 | if (LinkSection->Type == ELF::SHT_SYMTAB) |
| 561 | LinkSection = nullptr; |
| 562 | } |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 565 | void Section::finalize() { this->Link = LinkSection ? LinkSection->Index : 0; } |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 566 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 567 | void GnuDebugLinkSection::init(StringRef File, StringRef Data) { |
Alexander Richardson | 6c85992 | 2018-02-19 19:53:44 +0000 | [diff] [blame] | 568 | FileName = sys::path::filename(File); |
| 569 | // The format for the .gnu_debuglink starts with the file name and is |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 570 | // followed by a null terminator and then the CRC32 of the file. The CRC32 |
| 571 | // should be 4 byte aligned. So we add the FileName size, a 1 for the null |
| 572 | // byte, and then finally push the size to alignment and add 4. |
| 573 | Size = alignTo(FileName.size() + 1, 4) + 4; |
| 574 | // The CRC32 will only be aligned if we align the whole section. |
| 575 | Align = 4; |
| 576 | Type = ELF::SHT_PROGBITS; |
| 577 | Name = ".gnu_debuglink"; |
| 578 | // For sections not found in segments, OriginalOffset is only used to |
| 579 | // establish the order that sections should go in. By using the maximum |
| 580 | // possible offset we cause this section to wind up at the end. |
| 581 | OriginalOffset = std::numeric_limits<uint64_t>::max(); |
| 582 | JamCRC crc; |
| 583 | crc.update(ArrayRef<char>(Data.data(), Data.size())); |
| 584 | // The CRC32 value needs to be complemented because the JamCRC dosn't |
| 585 | // finalize the CRC32 value. It also dosn't negate the initial CRC32 value |
| 586 | // but it starts by default at 0xFFFFFFFF which is the complement of zero. |
| 587 | CRC32 = ~crc.getCRC(); |
| 588 | } |
| 589 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 590 | GnuDebugLinkSection::GnuDebugLinkSection(StringRef File) : FileName(File) { |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 591 | // Read in the file to compute the CRC of it. |
| 592 | auto DebugOrErr = MemoryBuffer::getFile(File); |
| 593 | if (!DebugOrErr) |
| 594 | error("'" + File + "': " + DebugOrErr.getError().message()); |
| 595 | auto Debug = std::move(*DebugOrErr); |
| 596 | init(File, Debug->getBuffer()); |
| 597 | } |
| 598 | |
| 599 | template <class ELFT> |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 600 | void ELFSectionWriter<ELFT>::visit(const GnuDebugLinkSection &Sec) { |
| 601 | auto Buf = Out.getBufferStart() + Sec.Offset; |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 602 | char *File = reinterpret_cast<char *>(Buf); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 603 | Elf_Word *CRC = |
| 604 | reinterpret_cast<Elf_Word *>(Buf + Sec.Size - sizeof(Elf_Word)); |
| 605 | *CRC = Sec.CRC32; |
| 606 | std::copy(std::begin(Sec.FileName), std::end(Sec.FileName), File); |
| 607 | } |
| 608 | |
| 609 | void GnuDebugLinkSection::accept(SectionVisitor &Visitor) const { |
| 610 | Visitor.visit(*this); |
Jake Ehrlich | ea07d3c | 2018-01-25 22:15:14 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 613 | template <class ELFT> |
| 614 | void ELFSectionWriter<ELFT>::visit(const GroupSection &Sec) { |
| 615 | ELF::Elf32_Word *Buf = |
| 616 | reinterpret_cast<ELF::Elf32_Word *>(Out.getBufferStart() + Sec.Offset); |
| 617 | *Buf++ = Sec.FlagWord; |
| 618 | for (const auto *S : Sec.GroupMembers) |
| 619 | support::endian::write32<ELFT::TargetEndianness>(Buf++, S->Index); |
| 620 | } |
| 621 | |
| 622 | void GroupSection::accept(SectionVisitor &Visitor) const { |
| 623 | Visitor.visit(*this); |
| 624 | } |
| 625 | |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 626 | // Returns true IFF a section is wholly inside the range of a segment |
| 627 | static bool sectionWithinSegment(const SectionBase &Section, |
| 628 | const Segment &Segment) { |
| 629 | // If a section is empty it should be treated like it has a size of 1. This is |
| 630 | // to clarify the case when an empty section lies on a boundary between two |
| 631 | // segments and ensures that the section "belongs" to the second segment and |
| 632 | // not the first. |
| 633 | uint64_t SecSize = Section.Size ? Section.Size : 1; |
| 634 | return Segment.Offset <= Section.OriginalOffset && |
| 635 | Segment.Offset + Segment.FileSize >= Section.OriginalOffset + SecSize; |
| 636 | } |
| 637 | |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 638 | // Returns true IFF a segment's original offset is inside of another segment's |
| 639 | // range. |
| 640 | static bool segmentOverlapsSegment(const Segment &Child, |
| 641 | const Segment &Parent) { |
| 642 | |
| 643 | return Parent.OriginalOffset <= Child.OriginalOffset && |
| 644 | Parent.OriginalOffset + Parent.FileSize > Child.OriginalOffset; |
| 645 | } |
| 646 | |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 647 | static bool compareSegmentsByOffset(const Segment *A, const Segment *B) { |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 648 | // Any segment without a parent segment should come before a segment |
| 649 | // that has a parent segment. |
| 650 | if (A->OriginalOffset < B->OriginalOffset) |
| 651 | return true; |
| 652 | if (A->OriginalOffset > B->OriginalOffset) |
| 653 | return false; |
| 654 | return A->Index < B->Index; |
| 655 | } |
| 656 | |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 657 | static bool compareSegmentsByPAddr(const Segment *A, const Segment *B) { |
| 658 | if (A->PAddr < B->PAddr) |
| 659 | return true; |
| 660 | if (A->PAddr > B->PAddr) |
| 661 | return false; |
| 662 | return A->Index < B->Index; |
| 663 | } |
| 664 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 665 | template <class ELFT> void BinaryELFBuilder<ELFT>::initFileHeader() { |
| 666 | Obj->Flags = 0x0; |
| 667 | Obj->Type = ET_REL; |
| 668 | Obj->Entry = 0x0; |
| 669 | Obj->Machine = EMachine; |
| 670 | Obj->Version = 1; |
| 671 | } |
| 672 | |
| 673 | template <class ELFT> void BinaryELFBuilder<ELFT>::initHeaderSegment() { |
| 674 | Obj->ElfHdrSegment.Index = 0; |
| 675 | } |
| 676 | |
| 677 | template <class ELFT> StringTableSection *BinaryELFBuilder<ELFT>::addStrTab() { |
| 678 | auto &StrTab = Obj->addSection<StringTableSection>(); |
| 679 | StrTab.Name = ".strtab"; |
| 680 | |
| 681 | Obj->SectionNames = &StrTab; |
| 682 | return &StrTab; |
| 683 | } |
| 684 | |
| 685 | template <class ELFT> |
| 686 | SymbolTableSection * |
| 687 | BinaryELFBuilder<ELFT>::addSymTab(StringTableSection *StrTab) { |
| 688 | auto &SymTab = Obj->addSection<SymbolTableSection>(); |
| 689 | |
| 690 | SymTab.Name = ".symtab"; |
| 691 | SymTab.Link = StrTab->Index; |
| 692 | // TODO: Factor out dependence on ElfType here. |
| 693 | SymTab.EntrySize = sizeof(Elf_Sym); |
| 694 | |
| 695 | // The symbol table always needs a null symbol |
| 696 | SymTab.addSymbol("", 0, 0, nullptr, 0, 0, 0, 0); |
| 697 | |
| 698 | Obj->SymbolTable = &SymTab; |
| 699 | return &SymTab; |
| 700 | } |
| 701 | |
| 702 | template <class ELFT> |
| 703 | void BinaryELFBuilder<ELFT>::addData(SymbolTableSection *SymTab) { |
| 704 | auto Data = ArrayRef<uint8_t>( |
| 705 | reinterpret_cast<const uint8_t *>(MemBuf->getBufferStart()), |
| 706 | MemBuf->getBufferSize()); |
| 707 | auto &DataSection = Obj->addSection<Section>(Data); |
| 708 | DataSection.Name = ".data"; |
| 709 | DataSection.Type = ELF::SHT_PROGBITS; |
| 710 | DataSection.Size = Data.size(); |
| 711 | DataSection.Flags = ELF::SHF_ALLOC | ELF::SHF_WRITE; |
| 712 | |
| 713 | std::string SanitizedFilename = MemBuf->getBufferIdentifier().str(); |
| 714 | std::replace_if(std::begin(SanitizedFilename), std::end(SanitizedFilename), |
| 715 | [](char c) { return !isalnum(c); }, '_'); |
| 716 | Twine Prefix = Twine("_binary_") + SanitizedFilename; |
| 717 | |
| 718 | SymTab->addSymbol(Prefix + "_start", STB_GLOBAL, STT_NOTYPE, &DataSection, |
| 719 | /*Value=*/0, STV_DEFAULT, 0, 0); |
| 720 | SymTab->addSymbol(Prefix + "_end", STB_GLOBAL, STT_NOTYPE, &DataSection, |
| 721 | /*Value=*/DataSection.Size, STV_DEFAULT, 0, 0); |
| 722 | SymTab->addSymbol(Prefix + "_size", STB_GLOBAL, STT_NOTYPE, nullptr, |
| 723 | /*Value=*/DataSection.Size, STV_DEFAULT, SHN_ABS, 0); |
| 724 | } |
| 725 | |
| 726 | template <class ELFT> void BinaryELFBuilder<ELFT>::initSections() { |
| 727 | for (auto &Section : Obj->sections()) { |
| 728 | Section.initialize(Obj->sections()); |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | template <class ELFT> std::unique_ptr<Object> BinaryELFBuilder<ELFT>::build() { |
| 733 | initFileHeader(); |
| 734 | initHeaderSegment(); |
| 735 | StringTableSection *StrTab = addStrTab(); |
| 736 | SymbolTableSection *SymTab = addSymTab(StrTab); |
| 737 | initSections(); |
| 738 | addData(SymTab); |
| 739 | |
| 740 | return std::move(Obj); |
| 741 | } |
| 742 | |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 743 | template <class ELFT> void ELFBuilder<ELFT>::setParentSegment(Segment &Child) { |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 744 | for (auto &Parent : Obj.segments()) { |
| 745 | // Every segment will overlap with itself but we don't want a segment to |
| 746 | // be it's own parent so we avoid that situation. |
| 747 | if (&Child != &Parent && segmentOverlapsSegment(Child, Parent)) { |
| 748 | // We want a canonical "most parental" segment but this requires |
| 749 | // inspecting the ParentSegment. |
| 750 | if (compareSegmentsByOffset(&Parent, &Child)) |
| 751 | if (Child.ParentSegment == nullptr || |
| 752 | compareSegmentsByOffset(&Parent, Child.ParentSegment)) { |
| 753 | Child.ParentSegment = &Parent; |
| 754 | } |
| 755 | } |
| 756 | } |
| 757 | } |
| 758 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 759 | template <class ELFT> void ELFBuilder<ELFT>::readProgramHeaders() { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 760 | uint32_t Index = 0; |
| 761 | for (const auto &Phdr : unwrapOrError(ElfFile.program_headers())) { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 762 | ArrayRef<uint8_t> Data{ElfFile.base() + Phdr.p_offset, |
| 763 | (size_t)Phdr.p_filesz}; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 764 | Segment &Seg = Obj.addSegment(Data); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 765 | Seg.Type = Phdr.p_type; |
| 766 | Seg.Flags = Phdr.p_flags; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 767 | Seg.OriginalOffset = Phdr.p_offset; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 768 | Seg.Offset = Phdr.p_offset; |
| 769 | Seg.VAddr = Phdr.p_vaddr; |
| 770 | Seg.PAddr = Phdr.p_paddr; |
| 771 | Seg.FileSize = Phdr.p_filesz; |
| 772 | Seg.MemSize = Phdr.p_memsz; |
| 773 | Seg.Align = Phdr.p_align; |
| 774 | Seg.Index = Index++; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 775 | for (auto &Section : Obj.sections()) { |
| 776 | if (sectionWithinSegment(Section, Seg)) { |
| 777 | Seg.addSection(&Section); |
| 778 | if (!Section.ParentSegment || |
| 779 | Section.ParentSegment->Offset > Seg.Offset) { |
| 780 | Section.ParentSegment = &Seg; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | } |
| 784 | } |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 785 | |
| 786 | auto &ElfHdr = Obj.ElfHdrSegment; |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 787 | ElfHdr.Index = Index++; |
| 788 | |
| 789 | const auto &Ehdr = *ElfFile.getHeader(); |
| 790 | auto &PrHdr = Obj.ProgramHdrSegment; |
| 791 | PrHdr.Type = PT_PHDR; |
| 792 | PrHdr.Flags = 0; |
| 793 | // The spec requires us to have p_vaddr % p_align == p_offset % p_align. |
| 794 | // Whereas this works automatically for ElfHdr, here OriginalOffset is |
| 795 | // always non-zero and to ensure the equation we assign the same value to |
| 796 | // VAddr as well. |
| 797 | PrHdr.OriginalOffset = PrHdr.Offset = PrHdr.VAddr = Ehdr.e_phoff; |
| 798 | PrHdr.PAddr = 0; |
| 799 | PrHdr.FileSize = PrHdr.MemSize = Ehdr.e_phentsize * Ehdr.e_phnum; |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 800 | // The spec requires us to naturally align all the fields. |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 801 | PrHdr.Align = sizeof(Elf_Addr); |
| 802 | PrHdr.Index = Index++; |
| 803 | |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 804 | // Now we do an O(n^2) loop through the segments in order to match up |
| 805 | // segments. |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 806 | for (auto &Child : Obj.segments()) |
| 807 | setParentSegment(Child); |
| 808 | setParentSegment(ElfHdr); |
| 809 | setParentSegment(PrHdr); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | template <class ELFT> |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 813 | void ELFBuilder<ELFT>::initGroupSection(GroupSection *GroupSec) { |
| 814 | auto SecTable = Obj.sections(); |
| 815 | auto SymTab = SecTable.template getSectionOfType<SymbolTableSection>( |
| 816 | GroupSec->Link, |
| 817 | "Link field value " + Twine(GroupSec->Link) + " in section " + |
| 818 | GroupSec->Name + " is invalid", |
| 819 | "Link field value " + Twine(GroupSec->Link) + " in section " + |
| 820 | GroupSec->Name + " is not a symbol table"); |
| 821 | auto Sym = SymTab->getSymbolByIndex(GroupSec->Info); |
| 822 | if (!Sym) |
| 823 | error("Info field value " + Twine(GroupSec->Info) + " in section " + |
| 824 | GroupSec->Name + " is not a valid symbol index"); |
| 825 | GroupSec->setSymTab(SymTab); |
| 826 | GroupSec->setSymbol(Sym); |
| 827 | if (GroupSec->Contents.size() % sizeof(ELF::Elf32_Word) || |
| 828 | GroupSec->Contents.empty()) |
| 829 | error("The content of the section " + GroupSec->Name + " is malformed"); |
| 830 | const ELF::Elf32_Word *Word = |
| 831 | reinterpret_cast<const ELF::Elf32_Word *>(GroupSec->Contents.data()); |
| 832 | const ELF::Elf32_Word *End = |
| 833 | Word + GroupSec->Contents.size() / sizeof(ELF::Elf32_Word); |
| 834 | GroupSec->setFlagWord(*Word++); |
| 835 | for (; Word != End; ++Word) { |
| 836 | uint32_t Index = support::endian::read32<ELFT::TargetEndianness>(Word); |
| 837 | GroupSec->addMember(SecTable.getSection( |
| 838 | Index, "Group member index " + Twine(Index) + " in section " + |
| 839 | GroupSec->Name + " is invalid")); |
| 840 | } |
| 841 | } |
| 842 | |
| 843 | template <class ELFT> |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 844 | void ELFBuilder<ELFT>::initSymbolTable(SymbolTableSection *SymTab) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 845 | const Elf_Shdr &Shdr = *unwrapOrError(ElfFile.getSection(SymTab->Index)); |
| 846 | StringRef StrTabData = unwrapOrError(ElfFile.getStringTableForSymtab(Shdr)); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 847 | ArrayRef<Elf_Word> ShndxData; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 848 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 849 | auto Symbols = unwrapOrError(ElfFile.symbols(&Shdr)); |
| 850 | for (const auto &Sym : Symbols) { |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 851 | SectionBase *DefSection = nullptr; |
| 852 | StringRef Name = unwrapOrError(Sym.getName(StrTabData)); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 853 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 854 | if (Sym.st_shndx == SHN_XINDEX) { |
| 855 | if (SymTab->getShndxTable() == nullptr) |
| 856 | error("Symbol '" + Name + |
| 857 | "' has index SHN_XINDEX but no SHT_SYMTAB_SHNDX section exists."); |
| 858 | if (ShndxData.data() == nullptr) { |
| 859 | const Elf_Shdr &ShndxSec = |
| 860 | *unwrapOrError(ElfFile.getSection(SymTab->getShndxTable()->Index)); |
| 861 | ShndxData = unwrapOrError( |
| 862 | ElfFile.template getSectionContentsAsArray<Elf_Word>(&ShndxSec)); |
| 863 | if (ShndxData.size() != Symbols.size()) |
| 864 | error("Symbol section index table does not have the same number of " |
| 865 | "entries as the symbol table."); |
| 866 | } |
| 867 | Elf_Word Index = ShndxData[&Sym - Symbols.begin()]; |
| 868 | DefSection = Obj.sections().getSection( |
| 869 | Index, |
Puyan Lotfi | 97604b4 | 2018-08-02 18:16:52 +0000 | [diff] [blame] | 870 | "Symbol '" + Name + "' has invalid section index " + Twine(Index)); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 871 | } else if (Sym.st_shndx >= SHN_LORESERVE) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 872 | if (!isValidReservedSectionIndex(Sym.st_shndx, Obj.Machine)) { |
Petr Hosek | ec2b3fc | 2017-09-07 23:02:50 +0000 | [diff] [blame] | 873 | error( |
| 874 | "Symbol '" + Name + |
| 875 | "' has unsupported value greater than or equal to SHN_LORESERVE: " + |
| 876 | Twine(Sym.st_shndx)); |
| 877 | } |
| 878 | } else if (Sym.st_shndx != SHN_UNDEF) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 879 | DefSection = Obj.sections().getSection( |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 880 | Sym.st_shndx, "Symbol '" + Name + |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 881 | "' is defined has invalid section index " + |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 882 | Twine(Sym.st_shndx)); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 883 | } |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 884 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 885 | SymTab->addSymbol(Name, Sym.getBinding(), Sym.getType(), DefSection, |
Jake Ehrlich | 30d927a | 2018-01-02 23:01:24 +0000 | [diff] [blame] | 886 | Sym.getValue(), Sym.st_other, Sym.st_shndx, Sym.st_size); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 887 | } |
| 888 | } |
| 889 | |
| 890 | template <class ELFT> |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 891 | static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, false> &Rel) {} |
| 892 | |
| 893 | template <class ELFT> |
| 894 | static void getAddend(uint64_t &ToSet, const Elf_Rel_Impl<ELFT, true> &Rela) { |
| 895 | ToSet = Rela.r_addend; |
| 896 | } |
| 897 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 898 | template <class T> |
Puyan Lotfi | c4846a5 | 2018-07-16 22:17:05 +0000 | [diff] [blame] | 899 | static void initRelocations(RelocationSection *Relocs, |
| 900 | SymbolTableSection *SymbolTable, T RelRange) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 901 | for (const auto &Rel : RelRange) { |
| 902 | Relocation ToAdd; |
| 903 | ToAdd.Offset = Rel.r_offset; |
| 904 | getAddend(ToAdd.Addend, Rel); |
| 905 | ToAdd.Type = Rel.getType(false); |
Paul Semel | 31a212d | 2018-05-22 01:04:36 +0000 | [diff] [blame] | 906 | ToAdd.RelocSymbol = SymbolTable->getSymbolByIndex(Rel.getSymbol(false)); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 907 | Relocs->addRelocation(ToAdd); |
| 908 | } |
| 909 | } |
| 910 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 911 | SectionBase *SectionTableRef::getSection(uint32_t Index, Twine ErrMsg) { |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 912 | if (Index == SHN_UNDEF || Index > Sections.size()) |
| 913 | error(ErrMsg); |
| 914 | return Sections[Index - 1].get(); |
| 915 | } |
| 916 | |
| 917 | template <class T> |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 918 | T *SectionTableRef::getSectionOfType(uint32_t Index, Twine IndexErrMsg, |
Zachary Turner | 41a9ee9 | 2017-10-11 23:54:34 +0000 | [diff] [blame] | 919 | Twine TypeErrMsg) { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 920 | if (T *Sec = dyn_cast<T>(getSection(Index, IndexErrMsg))) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 921 | return Sec; |
| 922 | error(TypeErrMsg); |
| 923 | } |
| 924 | |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 925 | template <class ELFT> |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 926 | SectionBase &ELFBuilder<ELFT>::makeSection(const Elf_Shdr &Shdr) { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 927 | ArrayRef<uint8_t> Data; |
| 928 | switch (Shdr.sh_type) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 929 | case SHT_REL: |
| 930 | case SHT_RELA: |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 931 | if (Shdr.sh_flags & SHF_ALLOC) { |
| 932 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 933 | return Obj.addSection<DynamicRelocationSection>(Data); |
Jake Ehrlich | 9f1a390 | 2017-09-26 18:02:25 +0000 | [diff] [blame] | 934 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 935 | return Obj.addSection<RelocationSection>(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 936 | case SHT_STRTAB: |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 937 | // If a string table is allocated we don't want to mess with it. That would |
| 938 | // mean altering the memory image. There are no special link types or |
| 939 | // anything so we can just use a Section. |
| 940 | if (Shdr.sh_flags & SHF_ALLOC) { |
| 941 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 942 | return Obj.addSection<Section>(Data); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 943 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 944 | return Obj.addSection<StringTableSection>(); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 945 | case SHT_HASH: |
| 946 | case SHT_GNU_HASH: |
| 947 | // Hash tables should refer to SHT_DYNSYM which we're not going to change. |
| 948 | // Because of this we don't need to mess with the hash tables either. |
| 949 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 950 | return Obj.addSection<Section>(Data); |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 951 | case SHT_GROUP: |
| 952 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
| 953 | return Obj.addSection<GroupSection>(Data); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 954 | case SHT_DYNSYM: |
| 955 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 956 | return Obj.addSection<DynamicSymbolTableSection>(Data); |
Jake Ehrlich | e5d424b | 2017-09-20 17:11:58 +0000 | [diff] [blame] | 957 | case SHT_DYNAMIC: |
| 958 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 959 | return Obj.addSection<DynamicSection>(Data); |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 960 | case SHT_SYMTAB: { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 961 | auto &SymTab = Obj.addSection<SymbolTableSection>(); |
| 962 | Obj.SymbolTable = &SymTab; |
| 963 | return SymTab; |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 964 | } |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 965 | case SHT_SYMTAB_SHNDX: { |
| 966 | auto &ShndxSection = Obj.addSection<SectionIndexSection>(); |
| 967 | Obj.SectionIndexTable = &ShndxSection; |
| 968 | return ShndxSection; |
| 969 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 970 | case SHT_NOBITS: |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 971 | return Obj.addSection<Section>(Data); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 972 | default: |
| 973 | Data = unwrapOrError(ElfFile.getSectionContents(&Shdr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 974 | return Obj.addSection<Section>(Data); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 975 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 976 | } |
| 977 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 978 | template <class ELFT> void ELFBuilder<ELFT>::readSectionHeaders() { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 979 | uint32_t Index = 0; |
| 980 | for (const auto &Shdr : unwrapOrError(ElfFile.sections())) { |
| 981 | if (Index == 0) { |
| 982 | ++Index; |
| 983 | continue; |
| 984 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 985 | auto &Sec = makeSection(Shdr); |
| 986 | Sec.Name = unwrapOrError(ElfFile.getSectionName(&Shdr)); |
| 987 | Sec.Type = Shdr.sh_type; |
| 988 | Sec.Flags = Shdr.sh_flags; |
| 989 | Sec.Addr = Shdr.sh_addr; |
| 990 | Sec.Offset = Shdr.sh_offset; |
| 991 | Sec.OriginalOffset = Shdr.sh_offset; |
| 992 | Sec.Size = Shdr.sh_size; |
| 993 | Sec.Link = Shdr.sh_link; |
| 994 | Sec.Info = Shdr.sh_info; |
| 995 | Sec.Align = Shdr.sh_addralign; |
| 996 | Sec.EntrySize = Shdr.sh_entsize; |
| 997 | Sec.Index = Index++; |
Paul Semel | a42dec7 | 2018-08-09 17:05:21 +0000 | [diff] [blame] | 998 | Sec.OriginalData = |
| 999 | ArrayRef<uint8_t>(ElfFile.base() + Shdr.sh_offset, |
| 1000 | (Shdr.sh_type == SHT_NOBITS) ? 0 : Shdr.sh_size); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1001 | } |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 1002 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1003 | // If a section index table exists we'll need to initialize it before we |
| 1004 | // initialize the symbol table because the symbol table might need to |
| 1005 | // reference it. |
| 1006 | if (Obj.SectionIndexTable) |
| 1007 | Obj.SectionIndexTable->initialize(Obj.sections()); |
| 1008 | |
Petr Hosek | 79cee9e | 2017-08-29 02:12:03 +0000 | [diff] [blame] | 1009 | // 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] | 1010 | // details about symbol tables. We need the symbol table filled out before |
| 1011 | // any relocations. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1012 | if (Obj.SymbolTable) { |
| 1013 | Obj.SymbolTable->initialize(Obj.sections()); |
| 1014 | initSymbolTable(Obj.SymbolTable); |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 1015 | } |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 1016 | |
| 1017 | // Now that all sections and symbols have been added we can add |
| 1018 | // relocations that reference symbols and set the link and info fields for |
| 1019 | // relocation sections. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1020 | for (auto &Section : Obj.sections()) { |
| 1021 | if (&Section == Obj.SymbolTable) |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 1022 | continue; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1023 | Section.initialize(Obj.sections()); |
| 1024 | if (auto RelSec = dyn_cast<RelocationSection>(&Section)) { |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 1025 | auto Shdr = unwrapOrError(ElfFile.sections()).begin() + RelSec->Index; |
| 1026 | if (RelSec->Type == SHT_REL) |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1027 | initRelocations(RelSec, Obj.SymbolTable, |
| 1028 | unwrapOrError(ElfFile.rels(Shdr))); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 1029 | else |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1030 | initRelocations(RelSec, Obj.SymbolTable, |
Jake Ehrlich | f5a4377 | 2017-09-25 20:37:28 +0000 | [diff] [blame] | 1031 | unwrapOrError(ElfFile.relas(Shdr))); |
Alexander Shaposhnikov | 6ecc6e6 | 2018-03-21 19:53:44 +0000 | [diff] [blame] | 1032 | } else if (auto GroupSec = dyn_cast<GroupSection>(&Section)) { |
| 1033 | initGroupSection(GroupSec); |
Petr Hosek | d7df9b2 | 2017-09-06 23:41:02 +0000 | [diff] [blame] | 1034 | } |
| 1035 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1038 | template <class ELFT> void ELFBuilder<ELFT>::build() { |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1039 | const auto &Ehdr = *ElfFile.getHeader(); |
| 1040 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1041 | Obj.Type = Ehdr.e_type; |
| 1042 | Obj.Machine = Ehdr.e_machine; |
| 1043 | Obj.Version = Ehdr.e_version; |
| 1044 | Obj.Entry = Ehdr.e_entry; |
| 1045 | Obj.Flags = Ehdr.e_flags; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1046 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1047 | readSectionHeaders(); |
| 1048 | readProgramHeaders(); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1049 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1050 | uint32_t ShstrIndex = Ehdr.e_shstrndx; |
| 1051 | if (ShstrIndex == SHN_XINDEX) |
| 1052 | ShstrIndex = unwrapOrError(ElfFile.getSection(0))->sh_link; |
| 1053 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1054 | Obj.SectionNames = |
| 1055 | Obj.sections().template getSectionOfType<StringTableSection>( |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1056 | ShstrIndex, |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 1057 | "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1058 | " in elf header " + " is invalid", |
Jake Ehrlich | 8b831c1 | 2018-03-07 20:33:02 +0000 | [diff] [blame] | 1059 | "e_shstrndx field value " + Twine(Ehdr.e_shstrndx) + |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1060 | " in elf header " + " is not a string table"); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1061 | } |
| 1062 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1063 | // A generic size function which computes sizes of any random access range. |
| 1064 | template <class R> size_t size(R &&Range) { |
| 1065 | return static_cast<size_t>(std::end(Range) - std::begin(Range)); |
| 1066 | } |
| 1067 | |
| 1068 | Writer::~Writer() {} |
| 1069 | |
| 1070 | Reader::~Reader() {} |
| 1071 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1072 | std::unique_ptr<Object> BinaryReader::create() const { |
| 1073 | if (MInfo.Is64Bit) |
| 1074 | return MInfo.IsLittleEndian |
| 1075 | ? BinaryELFBuilder<ELF64LE>(MInfo.EMachine, MemBuf).build() |
| 1076 | : BinaryELFBuilder<ELF64BE>(MInfo.EMachine, MemBuf).build(); |
| 1077 | else |
| 1078 | return MInfo.IsLittleEndian |
| 1079 | ? BinaryELFBuilder<ELF32LE>(MInfo.EMachine, MemBuf).build() |
| 1080 | : BinaryELFBuilder<ELF32BE>(MInfo.EMachine, MemBuf).build(); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | std::unique_ptr<Object> ELFReader::create() const { |
Alexander Shaposhnikov | 58cb197 | 2018-06-07 19:41:42 +0000 | [diff] [blame] | 1084 | auto Obj = llvm::make_unique<Object>(); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1085 | if (auto *o = dyn_cast<ELFObjectFile<ELF32LE>>(Bin)) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1086 | ELFBuilder<ELF32LE> Builder(*o, *Obj); |
| 1087 | Builder.build(); |
| 1088 | return Obj; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1089 | } else if (auto *o = dyn_cast<ELFObjectFile<ELF64LE>>(Bin)) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1090 | ELFBuilder<ELF64LE> Builder(*o, *Obj); |
| 1091 | Builder.build(); |
| 1092 | return Obj; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1093 | } else if (auto *o = dyn_cast<ELFObjectFile<ELF32BE>>(Bin)) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1094 | ELFBuilder<ELF32BE> Builder(*o, *Obj); |
| 1095 | Builder.build(); |
| 1096 | return Obj; |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1097 | } else if (auto *o = dyn_cast<ELFObjectFile<ELF64BE>>(Bin)) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1098 | ELFBuilder<ELF64BE> Builder(*o, *Obj); |
| 1099 | Builder.build(); |
| 1100 | return Obj; |
| 1101 | } |
| 1102 | error("Invalid file type"); |
| 1103 | } |
| 1104 | |
| 1105 | template <class ELFT> void ELFWriter<ELFT>::writeEhdr() { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1106 | uint8_t *B = Buf.getBufferStart(); |
| 1107 | Elf_Ehdr &Ehdr = *reinterpret_cast<Elf_Ehdr *>(B); |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1108 | std::fill(Ehdr.e_ident, Ehdr.e_ident + 16, 0); |
| 1109 | Ehdr.e_ident[EI_MAG0] = 0x7f; |
| 1110 | Ehdr.e_ident[EI_MAG1] = 'E'; |
| 1111 | Ehdr.e_ident[EI_MAG2] = 'L'; |
| 1112 | Ehdr.e_ident[EI_MAG3] = 'F'; |
| 1113 | Ehdr.e_ident[EI_CLASS] = ELFT::Is64Bits ? ELFCLASS64 : ELFCLASS32; |
| 1114 | Ehdr.e_ident[EI_DATA] = |
| 1115 | ELFT::TargetEndianness == support::big ? ELFDATA2MSB : ELFDATA2LSB; |
| 1116 | Ehdr.e_ident[EI_VERSION] = EV_CURRENT; |
| 1117 | Ehdr.e_ident[EI_OSABI] = ELFOSABI_NONE; |
| 1118 | Ehdr.e_ident[EI_ABIVERSION] = 0; |
| 1119 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1120 | Ehdr.e_type = Obj.Type; |
| 1121 | Ehdr.e_machine = Obj.Machine; |
| 1122 | Ehdr.e_version = Obj.Version; |
| 1123 | Ehdr.e_entry = Obj.Entry; |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1124 | // TODO: Only set phoff when a program header exists, to avoid tools |
| 1125 | // thinking this is corrupt data. |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 1126 | Ehdr.e_phoff = Obj.ProgramHdrSegment.Offset; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1127 | Ehdr.e_flags = Obj.Flags; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1128 | Ehdr.e_ehsize = sizeof(Elf_Ehdr); |
| 1129 | Ehdr.e_phentsize = sizeof(Elf_Phdr); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1130 | Ehdr.e_phnum = size(Obj.segments()); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1131 | Ehdr.e_shentsize = sizeof(Elf_Shdr); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1132 | if (WriteSectionHeaders) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1133 | Ehdr.e_shoff = Obj.SHOffset; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1134 | // """ |
| 1135 | // If the number of sections is greater than or equal to |
| 1136 | // SHN_LORESERVE (0xff00), this member has the value zero and the actual |
| 1137 | // number of section header table entries is contained in the sh_size field |
| 1138 | // of the section header at index 0. |
| 1139 | // """ |
| 1140 | auto Shnum = size(Obj.sections()) + 1; |
| 1141 | if (Shnum >= SHN_LORESERVE) |
| 1142 | Ehdr.e_shnum = 0; |
| 1143 | else |
| 1144 | Ehdr.e_shnum = Shnum; |
| 1145 | // """ |
| 1146 | // If the section name string table section index is greater than or equal |
| 1147 | // to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) |
| 1148 | // and the actual index of the section name string table section is |
| 1149 | // contained in the sh_link field of the section header at index 0. |
| 1150 | // """ |
| 1151 | if (Obj.SectionNames->Index >= SHN_LORESERVE) |
| 1152 | Ehdr.e_shstrndx = SHN_XINDEX; |
| 1153 | else |
| 1154 | Ehdr.e_shstrndx = Obj.SectionNames->Index; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1155 | } else { |
| 1156 | Ehdr.e_shoff = 0; |
| 1157 | Ehdr.e_shnum = 0; |
| 1158 | Ehdr.e_shstrndx = 0; |
| 1159 | } |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1160 | } |
| 1161 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1162 | template <class ELFT> void ELFWriter<ELFT>::writePhdrs() { |
| 1163 | for (auto &Seg : Obj.segments()) |
| 1164 | writePhdr(Seg); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1167 | template <class ELFT> void ELFWriter<ELFT>::writeShdrs() { |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1168 | uint8_t *B = Buf.getBufferStart() + Obj.SHOffset; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1169 | // This reference serves to write the dummy section header at the begining |
Jake Ehrlich | 425ec9f | 2017-09-15 22:04:09 +0000 | [diff] [blame] | 1170 | // of the file. It is not used for anything else |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1171 | Elf_Shdr &Shdr = *reinterpret_cast<Elf_Shdr *>(B); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1172 | Shdr.sh_name = 0; |
| 1173 | Shdr.sh_type = SHT_NULL; |
| 1174 | Shdr.sh_flags = 0; |
| 1175 | Shdr.sh_addr = 0; |
| 1176 | Shdr.sh_offset = 0; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1177 | // See writeEhdr for why we do this. |
| 1178 | uint64_t Shnum = size(Obj.sections()) + 1; |
| 1179 | if (Shnum >= SHN_LORESERVE) |
| 1180 | Shdr.sh_size = Shnum; |
| 1181 | else |
| 1182 | Shdr.sh_size = 0; |
| 1183 | // See writeEhdr for why we do this. |
| 1184 | if (Obj.SectionNames != nullptr && Obj.SectionNames->Index >= SHN_LORESERVE) |
| 1185 | Shdr.sh_link = Obj.SectionNames->Index; |
| 1186 | else |
| 1187 | Shdr.sh_link = 0; |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1188 | Shdr.sh_info = 0; |
| 1189 | Shdr.sh_addralign = 0; |
| 1190 | Shdr.sh_entsize = 0; |
| 1191 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1192 | for (auto &Sec : Obj.sections()) |
| 1193 | writeShdr(Sec); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1196 | template <class ELFT> void ELFWriter<ELFT>::writeSectionData() { |
| 1197 | for (auto &Sec : Obj.sections()) |
| 1198 | Sec.accept(*SecWriter); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1201 | void Object::removeSections(std::function<bool(const SectionBase &)> ToRemove) { |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 1202 | |
| 1203 | auto Iter = std::stable_partition( |
| 1204 | std::begin(Sections), std::end(Sections), [=](const SecPtr &Sec) { |
| 1205 | if (ToRemove(*Sec)) |
| 1206 | return false; |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1207 | if (auto RelSec = dyn_cast<RelocationSectionBase>(Sec.get())) { |
| 1208 | if (auto ToRelSec = RelSec->getSection()) |
| 1209 | return !ToRemove(*ToRelSec); |
| 1210 | } |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 1211 | return true; |
| 1212 | }); |
| 1213 | if (SymbolTable != nullptr && ToRemove(*SymbolTable)) |
| 1214 | SymbolTable = nullptr; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1215 | if (SectionNames != nullptr && ToRemove(*SectionNames)) |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1216 | SectionNames = nullptr; |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1217 | if (SectionIndexTable != nullptr && ToRemove(*SectionIndexTable)) |
| 1218 | SectionIndexTable = nullptr; |
Jake Ehrlich | 36a2eb3 | 2017-10-10 18:47:09 +0000 | [diff] [blame] | 1219 | // Now make sure there are no remaining references to the sections that will |
| 1220 | // be removed. Sometimes it is impossible to remove a reference so we emit |
| 1221 | // an error here instead. |
| 1222 | for (auto &RemoveSec : make_range(Iter, std::end(Sections))) { |
| 1223 | for (auto &Segment : Segments) |
| 1224 | Segment->removeSection(RemoveSec.get()); |
| 1225 | for (auto &KeepSec : make_range(std::begin(Sections), Iter)) |
| 1226 | KeepSec->removeSectionReferences(RemoveSec.get()); |
| 1227 | } |
| 1228 | // Now finally get rid of them all togethor. |
| 1229 | Sections.erase(Iter, std::end(Sections)); |
| 1230 | } |
| 1231 | |
Paul Semel | 4246a46 | 2018-05-09 21:36:54 +0000 | [diff] [blame] | 1232 | void Object::removeSymbols(function_ref<bool(const Symbol &)> ToRemove) { |
| 1233 | if (!SymbolTable) |
| 1234 | return; |
| 1235 | |
| 1236 | for (const SecPtr &Sec : Sections) |
| 1237 | Sec->removeSymbols(ToRemove); |
| 1238 | } |
| 1239 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1240 | void Object::sortSections() { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1241 | // Put all sections in offset order. Maintain the ordering as closely as |
| 1242 | // possible while meeting that demand however. |
| 1243 | auto CompareSections = [](const SecPtr &A, const SecPtr &B) { |
| 1244 | return A->OriginalOffset < B->OriginalOffset; |
| 1245 | }; |
| 1246 | std::stable_sort(std::begin(this->Sections), std::end(this->Sections), |
| 1247 | CompareSections); |
| 1248 | } |
| 1249 | |
Jake Ehrlich | 13153ee | 2017-11-02 23:24:04 +0000 | [diff] [blame] | 1250 | static uint64_t alignToAddr(uint64_t Offset, uint64_t Addr, uint64_t Align) { |
| 1251 | // Calculate Diff such that (Offset + Diff) & -Align == Addr & -Align. |
| 1252 | if (Align == 0) |
| 1253 | Align = 1; |
| 1254 | auto Diff = |
| 1255 | static_cast<int64_t>(Addr % Align) - static_cast<int64_t>(Offset % Align); |
| 1256 | // We only want to add to Offset, however, so if Diff < 0 we can add Align and |
| 1257 | // (Offset + Diff) & -Align == Addr & -Align will still hold. |
| 1258 | if (Diff < 0) |
| 1259 | Diff += Align; |
| 1260 | return Offset + Diff; |
| 1261 | } |
| 1262 | |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1263 | // Orders segments such that if x = y->ParentSegment then y comes before x. |
| 1264 | static void OrderSegments(std::vector<Segment *> &Segments) { |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1265 | std::stable_sort(std::begin(Segments), std::end(Segments), |
| 1266 | compareSegmentsByOffset); |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | // This function finds a consistent layout for a list of segments starting from |
| 1270 | // an Offset. It assumes that Segments have been sorted by OrderSegments and |
| 1271 | // returns an Offset one past the end of the last segment. |
| 1272 | static uint64_t LayoutSegments(std::vector<Segment *> &Segments, |
| 1273 | uint64_t Offset) { |
| 1274 | assert(std::is_sorted(std::begin(Segments), std::end(Segments), |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1275 | compareSegmentsByOffset)); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1276 | // The only way a segment should move is if a section was between two |
| 1277 | // segments and that section was removed. If that section isn't in a segment |
| 1278 | // then it's acceptable, but not ideal, to simply move it to after the |
| 1279 | // segments. So we can simply layout segments one after the other accounting |
| 1280 | // for alignment. |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1281 | for (auto &Segment : Segments) { |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 1282 | // We assume that segments have been ordered by OriginalOffset and Index |
| 1283 | // such that a parent segment will always come before a child segment in |
| 1284 | // OrderedSegments. This means that the Offset of the ParentSegment should |
| 1285 | // already be set and we can set our offset relative to it. |
| 1286 | if (Segment->ParentSegment != nullptr) { |
| 1287 | auto Parent = Segment->ParentSegment; |
| 1288 | Segment->Offset = |
| 1289 | Parent->Offset + Segment->OriginalOffset - Parent->OriginalOffset; |
| 1290 | } else { |
Jake Ehrlich | 13153ee | 2017-11-02 23:24:04 +0000 | [diff] [blame] | 1291 | Offset = alignToAddr(Offset, Segment->VAddr, Segment->Align); |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 1292 | Segment->Offset = Offset; |
Jake Ehrlich | d246b0a | 2017-09-19 21:37:35 +0000 | [diff] [blame] | 1293 | } |
Jake Ehrlich | 084400b | 2017-10-04 17:44:42 +0000 | [diff] [blame] | 1294 | Offset = std::max(Offset, Segment->Offset + Segment->FileSize); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1295 | } |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1296 | return Offset; |
| 1297 | } |
| 1298 | |
| 1299 | // This function finds a consistent layout for a list of sections. It assumes |
| 1300 | // that the ->ParentSegment of each section has already been laid out. The |
| 1301 | // supplied starting Offset is used for the starting offset of any section that |
| 1302 | // does not have a ParentSegment. It returns either the offset given if all |
| 1303 | // sections had a ParentSegment or an offset one past the last section if there |
| 1304 | // was a section that didn't have a ParentSegment. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1305 | template <class Range> |
| 1306 | static uint64_t LayoutSections(Range Sections, uint64_t Offset) { |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1307 | // Now the offset of every segment has been set we can assign the offsets |
| 1308 | // of each section. For sections that are covered by a segment we should use |
| 1309 | // the segment's original offset and the section's original offset to compute |
| 1310 | // the offset from the start of the segment. Using the offset from the start |
| 1311 | // of the segment we can assign a new offset to the section. For sections not |
| 1312 | // covered by segments we can just bump Offset to the next valid location. |
| 1313 | uint32_t Index = 1; |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1314 | for (auto &Section : Sections) { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1315 | Section.Index = Index++; |
| 1316 | if (Section.ParentSegment != nullptr) { |
| 1317 | auto Segment = *Section.ParentSegment; |
| 1318 | Section.Offset = |
| 1319 | Segment.Offset + (Section.OriginalOffset - Segment.OriginalOffset); |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1320 | } else { |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1321 | Offset = alignTo(Offset, Section.Align == 0 ? 1 : Section.Align); |
| 1322 | Section.Offset = Offset; |
| 1323 | if (Section.Type != SHT_NOBITS) |
| 1324 | Offset += Section.Size; |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1325 | } |
| 1326 | } |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1327 | return Offset; |
| 1328 | } |
Petr Hosek | 3f38383 | 2017-08-26 01:32:20 +0000 | [diff] [blame] | 1329 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1330 | template <class ELFT> void ELFWriter<ELFT>::initEhdrSegment() { |
| 1331 | auto &ElfHdr = Obj.ElfHdrSegment; |
| 1332 | ElfHdr.Type = PT_PHDR; |
| 1333 | ElfHdr.Flags = 0; |
| 1334 | ElfHdr.OriginalOffset = ElfHdr.Offset = 0; |
| 1335 | ElfHdr.VAddr = 0; |
| 1336 | ElfHdr.PAddr = 0; |
| 1337 | ElfHdr.FileSize = ElfHdr.MemSize = sizeof(Elf_Ehdr); |
| 1338 | ElfHdr.Align = 0; |
| 1339 | } |
| 1340 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1341 | template <class ELFT> void ELFWriter<ELFT>::assignOffsets() { |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1342 | // We need a temporary list of segments that has a special order to it |
| 1343 | // so that we know that anytime ->ParentSegment is set that segment has |
| 1344 | // already had its offset properly set. |
| 1345 | std::vector<Segment *> OrderedSegments; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1346 | for (auto &Segment : Obj.segments()) |
| 1347 | OrderedSegments.push_back(&Segment); |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 1348 | OrderedSegments.push_back(&Obj.ElfHdrSegment); |
| 1349 | OrderedSegments.push_back(&Obj.ProgramHdrSegment); |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1350 | OrderSegments(OrderedSegments); |
Jake Ehrlich | 6452b11 | 2018-02-14 23:31:33 +0000 | [diff] [blame] | 1351 | // Offset is used as the start offset of the first segment to be laid out. |
| 1352 | // Since the ELF Header (ElfHdrSegment) must be at the start of the file, |
| 1353 | // we start at offset 0. |
| 1354 | uint64_t Offset = 0; |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1355 | Offset = LayoutSegments(OrderedSegments, Offset); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1356 | Offset = LayoutSections(Obj.sections(), Offset); |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1357 | // If we need to write the section header table out then we need to align the |
| 1358 | // Offset so that SHOffset is valid. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1359 | if (WriteSectionHeaders) |
Jordan Rupprecht | de965ea | 2018-08-10 16:25:58 +0000 | [diff] [blame] | 1360 | Offset = alignTo(Offset, sizeof(Elf_Addr)); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1361 | Obj.SHOffset = Offset; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1362 | } |
| 1363 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1364 | template <class ELFT> size_t ELFWriter<ELFT>::totalSize() const { |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1365 | // We already have the section header offset so we can calculate the total |
| 1366 | // size by just adding up the size of each section header. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1367 | auto NullSectionSize = WriteSectionHeaders ? sizeof(Elf_Shdr) : 0; |
| 1368 | return Obj.SHOffset + size(Obj.sections()) * sizeof(Elf_Shdr) + |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1369 | NullSectionSize; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1370 | } |
| 1371 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1372 | template <class ELFT> void ELFWriter<ELFT>::write() { |
| 1373 | writeEhdr(); |
| 1374 | writePhdrs(); |
| 1375 | writeSectionData(); |
| 1376 | if (WriteSectionHeaders) |
| 1377 | writeShdrs(); |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1378 | if (auto E = Buf.commit()) |
| 1379 | reportError(Buf.getName(), errorToErrorCode(std::move(E))); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | template <class ELFT> void ELFWriter<ELFT>::finalize() { |
| 1383 | // It could happen that SectionNames has been removed and yet the user wants |
| 1384 | // a section header table output. We need to throw an error if a user tries |
| 1385 | // to do that. |
| 1386 | if (Obj.SectionNames == nullptr && WriteSectionHeaders) |
| 1387 | error("Cannot write section header table because section header string " |
| 1388 | "table was removed."); |
| 1389 | |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1390 | Obj.sortSections(); |
| 1391 | |
| 1392 | // We need to assign indexes before we perform layout because we need to know |
| 1393 | // if we need large indexes or not. We can assign indexes first and check as |
| 1394 | // we go to see if we will actully need large indexes. |
| 1395 | bool NeedsLargeIndexes = false; |
| 1396 | if (size(Obj.sections()) >= SHN_LORESERVE) { |
| 1397 | auto Sections = Obj.sections(); |
| 1398 | NeedsLargeIndexes = |
| 1399 | std::any_of(Sections.begin() + SHN_LORESERVE, Sections.end(), |
| 1400 | [](const SectionBase &Sec) { return Sec.HasSymbol; }); |
| 1401 | // TODO: handle case where only one section needs the large index table but |
| 1402 | // only needs it because the large index table hasn't been removed yet. |
| 1403 | } |
| 1404 | |
| 1405 | if (NeedsLargeIndexes) { |
| 1406 | // This means we definitely need to have a section index table but if we |
| 1407 | // already have one then we should use it instead of making a new one. |
| 1408 | if (Obj.SymbolTable != nullptr && Obj.SectionIndexTable == nullptr) { |
| 1409 | // Addition of a section to the end does not invalidate the indexes of |
| 1410 | // other sections and assigns the correct index to the new section. |
| 1411 | auto &Shndx = Obj.addSection<SectionIndexSection>(); |
| 1412 | Obj.SymbolTable->setShndxTable(&Shndx); |
| 1413 | Shndx.setSymTab(Obj.SymbolTable); |
| 1414 | } |
| 1415 | } else { |
| 1416 | // Since we don't need SectionIndexTable we should remove it and all |
| 1417 | // references to it. |
| 1418 | if (Obj.SectionIndexTable != nullptr) { |
| 1419 | Obj.removeSections([this](const SectionBase &Sec) { |
| 1420 | return &Sec == Obj.SectionIndexTable; |
| 1421 | }); |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | // Make sure we add the names of all the sections. Importantly this must be |
| 1426 | // done after we decide to add or remove SectionIndexes. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1427 | if (Obj.SectionNames != nullptr) |
| 1428 | for (const auto &Section : Obj.sections()) { |
| 1429 | Obj.SectionNames->addString(Section.Name); |
Jake Ehrlich | f03384d | 2017-10-11 18:09:18 +0000 | [diff] [blame] | 1430 | } |
Jake Ehrlich | 0a151bd | 2018-03-07 19:59:15 +0000 | [diff] [blame] | 1431 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1432 | initEhdrSegment(); |
Jake Ehrlich | c7f8ac7 | 2018-07-16 19:48:52 +0000 | [diff] [blame] | 1433 | // Before we can prepare for layout the indexes need to be finalized. |
| 1434 | uint64_t Index = 0; |
| 1435 | for (auto &Sec : Obj.sections()) |
| 1436 | Sec.Index = Index++; |
| 1437 | |
| 1438 | // The symbol table does not update all other sections on update. For |
| 1439 | // instance, symbol names are not added as new symbols are added. This means |
| 1440 | // that some sections, like .strtab, don't yet have their final size. |
| 1441 | if (Obj.SymbolTable != nullptr) |
| 1442 | Obj.SymbolTable->prepareForLayout(); |
| 1443 | |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1444 | assignOffsets(); |
| 1445 | |
| 1446 | // Finalize SectionNames first so that we can assign name indexes. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1447 | if (Obj.SectionNames != nullptr) |
| 1448 | Obj.SectionNames->finalize(); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1449 | // Finally now that all offsets and indexes have been set we can finalize any |
| 1450 | // remaining issues. |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1451 | uint64_t Offset = Obj.SHOffset + sizeof(Elf_Shdr); |
| 1452 | for (auto &Section : Obj.sections()) { |
| 1453 | Section.HeaderOffset = Offset; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1454 | Offset += sizeof(Elf_Shdr); |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1455 | if (WriteSectionHeaders) |
| 1456 | Section.NameIndex = Obj.SectionNames->findIndex(Section.Name); |
| 1457 | Section.finalize(); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1458 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1459 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1460 | Buf.allocate(totalSize()); |
| 1461 | SecWriter = llvm::make_unique<ELFSectionWriter<ELFT>>(Buf); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1464 | void BinaryWriter::write() { |
| 1465 | for (auto &Section : Obj.sections()) { |
| 1466 | if ((Section.Flags & SHF_ALLOC) == 0) |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1467 | continue; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1468 | Section.accept(*SecWriter); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1469 | } |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1470 | if (auto E = Buf.commit()) |
| 1471 | reportError(Buf.getName(), errorToErrorCode(std::move(E))); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1474 | void BinaryWriter::finalize() { |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1475 | // TODO: Create a filter range to construct OrderedSegments from so that this |
| 1476 | // code can be deduped with assignOffsets above. This should also solve the |
| 1477 | // todo below for LayoutSections. |
| 1478 | // We need a temporary list of segments that has a special order to it |
| 1479 | // so that we know that anytime ->ParentSegment is set that segment has |
| 1480 | // already had it's offset properly set. We only want to consider the segments |
| 1481 | // that will affect layout of allocated sections so we only add those. |
| 1482 | std::vector<Segment *> OrderedSegments; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1483 | for (auto &Section : Obj.sections()) { |
| 1484 | if ((Section.Flags & SHF_ALLOC) != 0 && Section.ParentSegment != nullptr) { |
| 1485 | OrderedSegments.push_back(Section.ParentSegment); |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1486 | } |
| 1487 | } |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1488 | |
| 1489 | // For binary output, we're going to use physical addresses instead of |
| 1490 | // virtual addresses, since a binary output is used for cases like ROM |
| 1491 | // loading and physical addresses are intended for ROM loading. |
| 1492 | // However, if no segment has a physical address, we'll fallback to using |
| 1493 | // virtual addresses for all. |
| 1494 | if (std::all_of(std::begin(OrderedSegments), std::end(OrderedSegments), |
| 1495 | [](const Segment *Segment) { return Segment->PAddr == 0; })) |
| 1496 | for (const auto &Segment : OrderedSegments) |
| 1497 | Segment->PAddr = Segment->VAddr; |
| 1498 | |
| 1499 | std::stable_sort(std::begin(OrderedSegments), std::end(OrderedSegments), |
| 1500 | compareSegmentsByPAddr); |
| 1501 | |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1502 | // Because we add a ParentSegment for each section we might have duplicate |
| 1503 | // segments in OrderedSegments. If there were duplicates then LayoutSegments |
| 1504 | // would do very strange things. |
| 1505 | auto End = |
| 1506 | std::unique(std::begin(OrderedSegments), std::end(OrderedSegments)); |
| 1507 | OrderedSegments.erase(End, std::end(OrderedSegments)); |
| 1508 | |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1509 | uint64_t Offset = 0; |
| 1510 | |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1511 | // Modify the first segment so that there is no gap at the start. This allows |
| 1512 | // our layout algorithm to proceed as expected while not out writing out the |
| 1513 | // gap at the start. |
| 1514 | if (!OrderedSegments.empty()) { |
| 1515 | auto Seg = OrderedSegments[0]; |
| 1516 | auto Sec = Seg->firstSection(); |
| 1517 | auto Diff = Sec->OriginalOffset - Seg->OriginalOffset; |
| 1518 | Seg->OriginalOffset += Diff; |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1519 | // The size needs to be shrunk as well. |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1520 | Seg->FileSize -= Diff; |
Jake Ehrlich | 46814be | 2018-01-22 19:27:30 +0000 | [diff] [blame] | 1521 | // The PAddr needs to be increased to remove the gap before the first |
| 1522 | // section. |
| 1523 | Seg->PAddr += Diff; |
| 1524 | uint64_t LowestPAddr = Seg->PAddr; |
| 1525 | for (auto &Segment : OrderedSegments) { |
| 1526 | Segment->Offset = Segment->PAddr - LowestPAddr; |
| 1527 | Offset = std::max(Offset, Segment->Offset + Segment->FileSize); |
| 1528 | } |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1531 | // TODO: generalize LayoutSections to take a range. Pass a special range |
| 1532 | // constructed from an iterator that skips values for which a predicate does |
| 1533 | // not hold. Then pass such a range to LayoutSections instead of constructing |
| 1534 | // AllocatedSections here. |
| 1535 | std::vector<SectionBase *> AllocatedSections; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1536 | for (auto &Section : Obj.sections()) { |
| 1537 | if ((Section.Flags & SHF_ALLOC) == 0) |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1538 | continue; |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1539 | AllocatedSections.push_back(&Section); |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1540 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1541 | LayoutSections(make_pointee_range(AllocatedSections), Offset); |
Jake Ehrlich | d49c92b | 2017-11-15 19:13:31 +0000 | [diff] [blame] | 1542 | |
| 1543 | // Now that every section has been laid out we just need to compute the total |
| 1544 | // file size. This might not be the same as the offset returned by |
| 1545 | // LayoutSections, because we want to truncate the last segment to the end of |
| 1546 | // its last section, to match GNU objcopy's behaviour. |
| 1547 | TotalSize = 0; |
| 1548 | for (const auto &Section : AllocatedSections) { |
| 1549 | if (Section->Type != SHT_NOBITS) |
| 1550 | TotalSize = std::max(TotalSize, Section->Offset + Section->Size); |
| 1551 | } |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1552 | |
Alexander Shaposhnikov | 42b5ef0 | 2018-07-06 17:51:03 +0000 | [diff] [blame] | 1553 | Buf.allocate(TotalSize); |
| 1554 | SecWriter = llvm::make_unique<BinarySectionWriter>(Buf); |
Petr Hosek | 05a04cb | 2017-08-01 00:33:58 +0000 | [diff] [blame] | 1555 | } |
| 1556 | |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1557 | namespace llvm { |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 1558 | namespace objcopy { |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1559 | |
Jordan Rupprecht | cf67633 | 2018-08-17 18:51:11 +0000 | [diff] [blame] | 1560 | template class BinaryELFBuilder<ELF64LE>; |
| 1561 | template class BinaryELFBuilder<ELF64BE>; |
| 1562 | template class BinaryELFBuilder<ELF32LE>; |
| 1563 | template class BinaryELFBuilder<ELF32BE>; |
| 1564 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1565 | template class ELFBuilder<ELF64LE>; |
| 1566 | template class ELFBuilder<ELF64BE>; |
| 1567 | template class ELFBuilder<ELF32LE>; |
| 1568 | template class ELFBuilder<ELF32BE>; |
Petr Hosek | c4df10e | 2017-08-04 21:09:26 +0000 | [diff] [blame] | 1569 | |
Jake Ehrlich | 76e9110 | 2018-01-25 22:46:17 +0000 | [diff] [blame] | 1570 | template class ELFWriter<ELF64LE>; |
| 1571 | template class ELFWriter<ELF64BE>; |
| 1572 | template class ELFWriter<ELF32LE>; |
| 1573 | template class ELFWriter<ELF32BE>; |
Puyan Lotfi | 0f5d5fa | 2018-07-18 00:10:51 +0000 | [diff] [blame] | 1574 | } // end namespace objcopy |
Eugene Zelenko | 0ad18f8 | 2017-11-01 21:16:06 +0000 | [diff] [blame] | 1575 | } // end namespace llvm |