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