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