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