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