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