Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 1 | //===- Chunks.cpp ---------------------------------------------------------===// |
| 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 |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "Chunks.h" |
Amy Huang | 6f7483b | 2021-09-16 16:48:26 -0700 | [diff] [blame] | 10 | #include "COFFLinkerContext.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 11 | #include "InputFiles.h" |
Amy Huang | 6f7483b | 2021-09-16 16:48:26 -0700 | [diff] [blame] | 12 | #include "SymbolTable.h" |
Rui Ueyama | 67fcd1a0 | 2015-08-05 19:51:28 +0000 | [diff] [blame] | 13 | #include "Symbols.h" |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 +0000 | [diff] [blame] | 14 | #include "Writer.h" |
Dmitri Gribenko | aba4303 | 2022-07-23 15:14:14 +0200 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
Elliot Goodrich | b0abd48 | 2023-06-17 13:18:23 +0100 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Joe Loser | 1173ecf | 2022-09-09 12:43:07 -0600 | [diff] [blame] | 17 | #include "llvm/ADT/Twine.h" |
Zachary Turner | 264b5d9 | 2017-06-07 03:48:56 +0000 | [diff] [blame] | 18 | #include "llvm/BinaryFormat/COFF.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 19 | #include "llvm/Object/COFF.h" |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Debug.h" |
| 21 | #include "llvm/Support/Endian.h" |
| 22 | #include "llvm/Support/raw_ostream.h" |
Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 23 | #include <algorithm> |
Joe Loser | 1173ecf | 2022-09-09 12:43:07 -0600 | [diff] [blame] | 24 | #include <iterator> |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 25 | |
Rui Ueyama | c6ea057 | 2015-06-06 22:46:15 +0000 | [diff] [blame] | 26 | using namespace llvm; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 27 | using namespace llvm::object; |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 28 | using namespace llvm::support; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 29 | using namespace llvm::support::endian; |
| 30 | using namespace llvm::COFF; |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 31 | using llvm::support::ulittle32_t; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 32 | |
Nico Weber | 7c26641 | 2022-08-08 11:32:26 -0400 | [diff] [blame] | 33 | namespace lld::coff { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 34 | |
Jacek Caban | fed8e38 | 2024-06-18 11:14:01 +0200 | [diff] [blame] | 35 | SectionChunk::SectionChunk(ObjFile *f, const coff_section *h, Kind k) |
| 36 | : Chunk(k), file(f), header(h), repl(this) { |
Fangrui Song | 2e2038b | 2019-07-16 08:26:38 +0000 | [diff] [blame] | 37 | // Initialize relocs. |
Martin Storsjö | 33b71ec | 2021-04-29 14:06:24 +0300 | [diff] [blame] | 38 | if (file) |
| 39 | setRelocs(file->getCOFFObj()->getRelocations(header)); |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 +0000 | [diff] [blame] | 40 | |
Fangrui Song | 2e2038b | 2019-07-16 08:26:38 +0000 | [diff] [blame] | 41 | // Initialize sectionName. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 42 | StringRef sectionName; |
Martin Storsjö | 33b71ec | 2021-04-29 14:06:24 +0300 | [diff] [blame] | 43 | if (file) { |
| 44 | if (Expected<StringRef> e = file->getCOFFObj()->getSectionName(header)) |
| 45 | sectionName = *e; |
| 46 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 47 | sectionNameData = sectionName.data(); |
| 48 | sectionNameSize = sectionName.size(); |
Rui Ueyama | 8b33f59 | 2015-06-10 04:21:47 +0000 | [diff] [blame] | 49 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 50 | setAlignment(header->getAlignment()); |
Rui Ueyama | 4dbff20 | 2015-09-16 21:40:47 +0000 | [diff] [blame] | 51 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 52 | hasData = !(header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA); |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 +0000 | [diff] [blame] | 53 | |
Reid Kleckner | 79ac99b | 2017-06-16 20:47:19 +0000 | [diff] [blame] | 54 | // If linker GC is disabled, every chunk starts out alive. If linker GC is |
| 55 | // enabled, treat non-comdat sections as roots. Generally optimized object |
| 56 | // files will be built with -ffunction-sections or /Gy, so most things worth |
| 57 | // stripping will be in a comdat. |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 58 | if (file) |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 59 | live = !file->symtab.ctx.config.doGC || !isCOMDAT(); |
Martin Storsjö | 33b71ec | 2021-04-29 14:06:24 +0300 | [diff] [blame] | 60 | else |
| 61 | live = true; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Jacek Caban | f8f01b5 | 2025-04-11 16:13:46 +0200 | [diff] [blame] | 64 | MachineTypes SectionChunk::getMachine() const { |
| 65 | MachineTypes machine = file->getMachineType(); |
| 66 | // On ARM64EC, the IMAGE_SCN_GPREL flag is repurposed to indicate that section |
| 67 | // code is x86_64. This enables embedding x86_64 code within ARM64EC object |
| 68 | // files. MSVC uses this for export thunks in .exp files. |
| 69 | if (isArm64EC(machine) && (header->Characteristics & IMAGE_SCN_GPREL)) |
| 70 | machine = AMD64; |
| 71 | return machine; |
| 72 | } |
| 73 | |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 +0000 | [diff] [blame] | 74 | // SectionChunk is one of the most frequently allocated classes, so it is |
| 75 | // important to keep it as compact as possible. As of this writing, the number |
| 76 | // below is the size of this class on x64 platforms. |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 +0000 | [diff] [blame] | 77 | static_assert(sizeof(SectionChunk) <= 88, "SectionChunk grew unexpectedly"); |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 +0000 | [diff] [blame] | 78 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 79 | static void add16(uint8_t *p, int16_t v) { write16le(p, read16le(p) + v); } |
| 80 | static void add32(uint8_t *p, int32_t v) { write32le(p, read32le(p) + v); } |
| 81 | static void add64(uint8_t *p, int64_t v) { write64le(p, read64le(p) + v); } |
| 82 | static void or16(uint8_t *p, uint16_t v) { write16le(p, read16le(p) | v); } |
| 83 | static void or32(uint8_t *p, uint32_t v) { write32le(p, read32le(p) | v); } |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 84 | |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 85 | // Verify that given sections are appropriate targets for SECREL |
| 86 | // relocations. This check is relaxed because unfortunately debug |
| 87 | // sections have section-relative relocations against absolute symbols. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 88 | static bool checkSecRel(const SectionChunk *sec, OutputSection *os) { |
| 89 | if (os) |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 90 | return true; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 91 | if (sec->isCodeView()) |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 92 | return false; |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 93 | error("SECREL relocation cannot be applied to absolute symbols"); |
| 94 | return false; |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 97 | static void applySecRel(const SectionChunk *sec, uint8_t *off, |
| 98 | OutputSection *os, uint64_t s) { |
| 99 | if (!checkSecRel(sec, os)) |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 100 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 101 | uint64_t secRel = s - os->getRVA(); |
| 102 | if (secRel > UINT32_MAX) { |
| 103 | error("overflow in SECREL relocation in section: " + sec->getSectionName()); |
Shoaib Meenai | e61ca35 | 2017-09-20 00:21:58 +0000 | [diff] [blame] | 104 | return; |
| 105 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 106 | add32(off, secRel); |
Reid Kleckner | 8456411 | 2017-06-22 23:33:04 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 109 | static void applySecIdx(uint8_t *off, OutputSection *os, |
| 110 | unsigned numOutputSections) { |
| 111 | // numOutputSections is the largest valid section index. Make sure that |
| 112 | // it fits in 16 bits. |
| 113 | assert(numOutputSections <= 0xffff && "size of outputSections is too big"); |
| 114 | |
Rui Ueyama | b310747 | 2018-02-17 20:41:38 +0000 | [diff] [blame] | 115 | // Absolute symbol doesn't have section index, but section index relocation |
| 116 | // against absolute symbol should be resolved to one plus the last output |
| 117 | // section index. This is required for compatibility with MSVC. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 118 | if (os) |
| 119 | add16(off, os->sectionIndex); |
Rui Ueyama | b310747 | 2018-02-17 20:41:38 +0000 | [diff] [blame] | 120 | else |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 121 | add16(off, numOutputSections + 1); |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 124 | void SectionChunk::applyRelX64(uint8_t *off, uint16_t type, OutputSection *os, |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 125 | uint64_t s, uint64_t p, |
| 126 | uint64_t imageBase) const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 127 | switch (type) { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 128 | case IMAGE_REL_AMD64_ADDR32: |
| 129 | add32(off, s + imageBase); |
| 130 | break; |
| 131 | case IMAGE_REL_AMD64_ADDR64: |
| 132 | add64(off, s + imageBase); |
| 133 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 134 | case IMAGE_REL_AMD64_ADDR32NB: add32(off, s); break; |
| 135 | case IMAGE_REL_AMD64_REL32: add32(off, s - p - 4); break; |
| 136 | case IMAGE_REL_AMD64_REL32_1: add32(off, s - p - 5); break; |
| 137 | case IMAGE_REL_AMD64_REL32_2: add32(off, s - p - 6); break; |
| 138 | case IMAGE_REL_AMD64_REL32_3: add32(off, s - p - 7); break; |
| 139 | case IMAGE_REL_AMD64_REL32_4: add32(off, s - p - 8); break; |
| 140 | case IMAGE_REL_AMD64_REL32_5: add32(off, s - p - 9); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 141 | case IMAGE_REL_AMD64_SECTION: |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 142 | applySecIdx(off, os, file->symtab.ctx.outputSections.size()); |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 143 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 144 | case IMAGE_REL_AMD64_SECREL: applySecRel(this, off, os, s); break; |
Rui Ueyama | 661a4e7a | 2015-07-07 22:49:21 +0000 | [diff] [blame] | 145 | default: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 146 | error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " + |
| 147 | toString(file)); |
Rui Ueyama | 661a4e7a | 2015-07-07 22:49:21 +0000 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 151 | void SectionChunk::applyRelX86(uint8_t *off, uint16_t type, OutputSection *os, |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 152 | uint64_t s, uint64_t p, |
| 153 | uint64_t imageBase) const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 154 | switch (type) { |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 155 | case IMAGE_REL_I386_ABSOLUTE: break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 156 | case IMAGE_REL_I386_DIR32: |
| 157 | add32(off, s + imageBase); |
| 158 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 159 | case IMAGE_REL_I386_DIR32NB: add32(off, s); break; |
| 160 | case IMAGE_REL_I386_REL32: add32(off, s - p - 4); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 161 | case IMAGE_REL_I386_SECTION: |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 162 | applySecIdx(off, os, file->symtab.ctx.outputSections.size()); |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 163 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 164 | case IMAGE_REL_I386_SECREL: applySecRel(this, off, os, s); break; |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 165 | default: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 166 | error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " + |
| 167 | toString(file)); |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 171 | static void applyMOV(uint8_t *off, uint16_t v) { |
| 172 | write16le(off, (read16le(off) & 0xfbf0) | ((v & 0x800) >> 1) | ((v >> 12) & 0xf)); |
| 173 | write16le(off + 2, (read16le(off + 2) & 0x8f00) | ((v & 0x700) << 4) | (v & 0xff)); |
Saleem Abdulrasool | fd063e7 | 2016-08-05 18:20:31 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 176 | static uint16_t readMOV(uint8_t *off, bool movt) { |
| 177 | uint16_t op1 = read16le(off); |
| 178 | if ((op1 & 0xfbf0) != (movt ? 0xf2c0 : 0xf240)) |
| 179 | error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") + |
Martin Storsjo | c4b0061 | 2018-08-27 06:04:36 +0000 | [diff] [blame] | 180 | " instruction in MOV32T relocation"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 181 | uint16_t op2 = read16le(off + 2); |
| 182 | if ((op2 & 0x8000) != 0) |
| 183 | error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") + |
Martin Storsjo | c4b0061 | 2018-08-27 06:04:36 +0000 | [diff] [blame] | 184 | " instruction in MOV32T relocation"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 185 | return (op2 & 0x00ff) | ((op2 >> 4) & 0x0700) | ((op1 << 1) & 0x0800) | |
| 186 | ((op1 & 0x000f) << 12); |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 189 | void applyMOV32T(uint8_t *off, uint32_t v) { |
| 190 | uint16_t immW = readMOV(off, false); // read MOVW operand |
| 191 | uint16_t immT = readMOV(off + 4, true); // read MOVT operand |
| 192 | uint32_t imm = immW | (immT << 16); |
| 193 | v += imm; // add the immediate offset |
| 194 | applyMOV(off, v); // set MOVW operand |
| 195 | applyMOV(off + 4, v >> 16); // set MOVT operand |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 198 | static void applyBranch20T(uint8_t *off, int32_t v) { |
| 199 | if (!isInt<21>(v)) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 200 | error("relocation out of range"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 201 | uint32_t s = v < 0 ? 1 : 0; |
| 202 | uint32_t j1 = (v >> 19) & 1; |
| 203 | uint32_t j2 = (v >> 18) & 1; |
| 204 | or16(off, (s << 10) | ((v >> 12) & 0x3f)); |
| 205 | or16(off + 2, (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff)); |
Rui Ueyama | ba7c041 | 2015-08-05 19:40:07 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 208 | void applyBranch24T(uint8_t *off, int32_t v) { |
| 209 | if (!isInt<25>(v)) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 210 | error("relocation out of range"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 211 | uint32_t s = v < 0 ? 1 : 0; |
| 212 | uint32_t j1 = ((~v >> 23) & 1) ^ s; |
| 213 | uint32_t j2 = ((~v >> 22) & 1) ^ s; |
| 214 | or16(off, (s << 10) | ((v >> 12) & 0x3ff)); |
Saleem Abdulrasool | 8202c6d | 2016-08-05 17:28:21 +0000 | [diff] [blame] | 215 | // Clear out the J1 and J2 bits which may be set. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 216 | write16le(off + 2, (read16le(off + 2) & 0xd000) | (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff)); |
Rui Ueyama | 3d9c863 | 2015-07-25 03:19:34 +0000 | [diff] [blame] | 217 | } |
| 218 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 219 | void SectionChunk::applyRelARM(uint8_t *off, uint16_t type, OutputSection *os, |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 220 | uint64_t s, uint64_t p, |
| 221 | uint64_t imageBase) const { |
Rui Ueyama | 8bc43a1 | 2015-07-29 19:25:00 +0000 | [diff] [blame] | 222 | // Pointer to thumb code must have the LSB set. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 223 | uint64_t sx = s; |
| 224 | if (os && (os->header.Characteristics & IMAGE_SCN_MEM_EXECUTE)) |
| 225 | sx |= 1; |
| 226 | switch (type) { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 227 | case IMAGE_REL_ARM_ADDR32: |
| 228 | add32(off, sx + imageBase); |
| 229 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 230 | case IMAGE_REL_ARM_ADDR32NB: add32(off, sx); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 231 | case IMAGE_REL_ARM_MOV32T: |
| 232 | applyMOV32T(off, sx + imageBase); |
| 233 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 234 | case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(off, sx - p - 4); break; |
| 235 | case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(off, sx - p - 4); break; |
| 236 | case IMAGE_REL_ARM_BLX23T: applyBranch24T(off, sx - p - 4); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 237 | case IMAGE_REL_ARM_SECTION: |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 238 | applySecIdx(off, os, file->symtab.ctx.outputSections.size()); |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 239 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 240 | case IMAGE_REL_ARM_SECREL: applySecRel(this, off, os, s); break; |
| 241 | case IMAGE_REL_ARM_REL32: add32(off, sx - p - 4); break; |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 242 | default: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 243 | error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " + |
| 244 | toString(file)); |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
Martin Storsjo | 38608c0 | 2017-07-26 20:51:47 +0000 | [diff] [blame] | 248 | // Interpret the existing immediate value as a byte offset to the |
| 249 | // target symbol, then update the instruction with the immediate as |
| 250 | // the page offset from the current instruction to the target. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 251 | void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift) { |
| 252 | uint32_t orig = read32le(off); |
Martin Storsjö | 7c15da6 | 2021-11-20 19:55:18 +0200 | [diff] [blame] | 253 | int64_t imm = |
| 254 | SignExtend64<21>(((orig >> 29) & 0x3) | ((orig >> 3) & 0x1FFFFC)); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 255 | s += imm; |
| 256 | imm = (s >> shift) - (p >> shift); |
| 257 | uint32_t immLo = (imm & 0x3) << 29; |
| 258 | uint32_t immHi = (imm & 0x1FFFFC) << 3; |
| 259 | uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3); |
| 260 | write32le(off, (orig & ~mask) | immLo | immHi); |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | // Update the immediate field in a AARCH64 ldr, str, and add instruction. |
Martin Storsjo | 38608c0 | 2017-07-26 20:51:47 +0000 | [diff] [blame] | 264 | // Optionally limit the range of the written immediate by one or more bits |
Fangrui Song | 2e2038b | 2019-07-16 08:26:38 +0000 | [diff] [blame] | 265 | // (rangeLimit). |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 266 | void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit) { |
| 267 | uint32_t orig = read32le(off); |
| 268 | imm += (orig >> 10) & 0xFFF; |
| 269 | orig &= ~(0xFFF << 10); |
| 270 | write32le(off, orig | ((imm & (0xFFF >> rangeLimit)) << 10)); |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Martin Storsjo | 38608c0 | 2017-07-26 20:51:47 +0000 | [diff] [blame] | 273 | // Add the 12 bit page offset to the existing immediate. |
| 274 | // Ldr/str instructions store the opcode immediate scaled |
| 275 | // by the load/store size (giving a larger range for larger |
| 276 | // loads/stores). The immediate is always (both before and after |
| 277 | // fixing up the relocation) stored scaled similarly. |
| 278 | // Even if larger loads/stores have a larger range, limit the |
| 279 | // effective offset to 12 bit, since it is intended to be a |
| 280 | // page offset. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 281 | static void applyArm64Ldr(uint8_t *off, uint64_t imm) { |
| 282 | uint32_t orig = read32le(off); |
| 283 | uint32_t size = orig >> 30; |
Martin Storsjo | 5ae7649 | 2017-07-20 16:48:33 +0000 | [diff] [blame] | 284 | // 0x04000000 indicates SIMD/FP registers |
| 285 | // 0x00800000 indicates 128 bit |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 286 | if ((orig & 0x4800000) == 0x4800000) |
| 287 | size += 4; |
| 288 | if ((imm & ((1 << size) - 1)) != 0) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 289 | error("misaligned ldr/str offset"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 290 | applyArm64Imm(off, imm >> size, size); |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 293 | static void applySecRelLow12A(const SectionChunk *sec, uint8_t *off, |
| 294 | OutputSection *os, uint64_t s) { |
| 295 | if (checkSecRel(sec, os)) |
| 296 | applyArm64Imm(off, (s - os->getRVA()) & 0xfff, 0); |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 299 | static void applySecRelHigh12A(const SectionChunk *sec, uint8_t *off, |
| 300 | OutputSection *os, uint64_t s) { |
| 301 | if (!checkSecRel(sec, os)) |
Rui Ueyama | 03c7f3a | 2018-02-17 20:28:15 +0000 | [diff] [blame] | 302 | return; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 303 | uint64_t secRel = (s - os->getRVA()) >> 12; |
| 304 | if (0xfff < secRel) { |
Martin Storsjo | ef4f78b | 2018-02-16 22:02:38 +0000 | [diff] [blame] | 305 | error("overflow in SECREL_HIGH12A relocation in section: " + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 306 | sec->getSectionName()); |
Martin Storsjo | ef4f78b | 2018-02-16 22:02:38 +0000 | [diff] [blame] | 307 | return; |
| 308 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 309 | applyArm64Imm(off, secRel & 0xfff, 0); |
Martin Storsjo | ef4f78b | 2018-02-16 22:02:38 +0000 | [diff] [blame] | 310 | } |
| 311 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 312 | static void applySecRelLdr(const SectionChunk *sec, uint8_t *off, |
| 313 | OutputSection *os, uint64_t s) { |
| 314 | if (checkSecRel(sec, os)) |
| 315 | applyArm64Ldr(off, (s - os->getRVA()) & 0xfff); |
Martin Storsjo | ef4f78b | 2018-02-16 22:02:38 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 318 | void applyArm64Branch26(uint8_t *off, int64_t v) { |
| 319 | if (!isInt<28>(v)) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 320 | error("relocation out of range"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 321 | or32(off, (v & 0x0FFFFFFC) >> 2); |
Martin Storsjo | cc80776 | 2018-05-04 06:06:27 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 324 | static void applyArm64Branch19(uint8_t *off, int64_t v) { |
| 325 | if (!isInt<21>(v)) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 326 | error("relocation out of range"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 327 | or32(off, (v & 0x001FFFFC) << 3); |
Martin Storsjo | cc80776 | 2018-05-04 06:06:27 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 330 | static void applyArm64Branch14(uint8_t *off, int64_t v) { |
| 331 | if (!isInt<16>(v)) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 332 | error("relocation out of range"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 333 | or32(off, (v & 0x0000FFFC) << 3); |
Martin Storsjo | cc80776 | 2018-05-04 06:06:27 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 336 | void SectionChunk::applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os, |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 337 | uint64_t s, uint64_t p, |
| 338 | uint64_t imageBase) const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 339 | switch (type) { |
| 340 | case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(off, s, p, 12); break; |
| 341 | case IMAGE_REL_ARM64_REL21: applyArm64Addr(off, s, p, 0); break; |
| 342 | case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(off, s & 0xfff, 0); break; |
| 343 | case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(off, s & 0xfff); break; |
| 344 | case IMAGE_REL_ARM64_BRANCH26: applyArm64Branch26(off, s - p); break; |
| 345 | case IMAGE_REL_ARM64_BRANCH19: applyArm64Branch19(off, s - p); break; |
| 346 | case IMAGE_REL_ARM64_BRANCH14: applyArm64Branch14(off, s - p); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 347 | case IMAGE_REL_ARM64_ADDR32: |
| 348 | add32(off, s + imageBase); |
| 349 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 350 | case IMAGE_REL_ARM64_ADDR32NB: add32(off, s); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 351 | case IMAGE_REL_ARM64_ADDR64: |
| 352 | add64(off, s + imageBase); |
| 353 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 354 | case IMAGE_REL_ARM64_SECREL: applySecRel(this, off, os, s); break; |
| 355 | case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelLow12A(this, off, os, s); break; |
| 356 | case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, off, os, s); break; |
| 357 | case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, off, os, s); break; |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 358 | case IMAGE_REL_ARM64_SECTION: |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 359 | applySecIdx(off, os, file->symtab.ctx.outputSections.size()); |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 360 | break; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 361 | case IMAGE_REL_ARM64_REL32: add32(off, s - p - 4); break; |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 362 | default: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 363 | error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " + |
| 364 | toString(file)); |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 368 | static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk, |
| 369 | Defined *sym, |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 370 | const coff_relocation &rel, |
| 371 | bool isMinGW) { |
Reid Kleckner | 551acf0 | 2018-11-13 18:30:31 +0000 | [diff] [blame] | 372 | // Don't report these errors when the relocation comes from a debug info |
| 373 | // section or in mingw mode. MinGW mode object files (built by GCC) can |
| 374 | // have leftover sections with relocations against discarded comdat |
| 375 | // sections. Such sections are left as is, with relocations untouched. |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 376 | if (fromChunk->isCodeView() || fromChunk->isDWARF() || isMinGW) |
Reid Kleckner | 551acf0 | 2018-11-13 18:30:31 +0000 | [diff] [blame] | 377 | return; |
| 378 | |
| 379 | // Get the name of the symbol. If it's null, it was discarded early, so we |
| 380 | // have to go back to the object file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 381 | ObjFile *file = fromChunk->file; |
Nico Weber | d73ef97 | 2024-12-13 19:35:51 -0500 | [diff] [blame] | 382 | std::string name; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 383 | if (sym) { |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 384 | name = toString(file->symtab.ctx, *sym); |
Reid Kleckner | 551acf0 | 2018-11-13 18:30:31 +0000 | [diff] [blame] | 385 | } else { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 386 | COFFSymbolRef coffSym = |
| 387 | check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex)); |
Nico Weber | d73ef97 | 2024-12-13 19:35:51 -0500 | [diff] [blame] | 388 | name = maybeDemangleSymbol( |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 389 | file->symtab.ctx, check(file->getCOFFObj()->getSymbolName(coffSym))); |
Reid Kleckner | 551acf0 | 2018-11-13 18:30:31 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 392 | std::vector<std::string> symbolLocations = |
| 393 | getSymbolLocations(file, rel.SymbolTableIndex); |
Nico Weber | 0142b9c | 2019-06-25 09:55:55 +0000 | [diff] [blame] | 394 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 395 | std::string out; |
| 396 | llvm::raw_string_ostream os(out); |
| 397 | os << "relocation against symbol in discarded section: " + name; |
| 398 | for (const std::string &s : symbolLocations) |
| 399 | os << s; |
JOE1994 | 4b27b58 | 2024-09-15 04:21:07 -0400 | [diff] [blame] | 400 | error(out); |
Reid Kleckner | 551acf0 | 2018-11-13 18:30:31 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 403 | void SectionChunk::writeTo(uint8_t *buf) const { |
| 404 | if (!hasData) |
Rui Ueyama | 9aefa0c | 2015-05-28 20:04:51 +0000 | [diff] [blame] | 405 | return; |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 406 | // Copy section contents from source object file to output file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 407 | ArrayRef<uint8_t> a = getContents(); |
| 408 | if (!a.empty()) |
| 409 | memcpy(buf, a.data(), a.size()); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 410 | |
| 411 | // Apply relocations. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 412 | size_t inputSize = getSize(); |
Reid Kleckner | 69e0bc7 | 2021-01-20 11:00:58 -0800 | [diff] [blame] | 413 | for (const coff_relocation &rel : getRelocs()) { |
Reid Kleckner | 3b8acb2 | 2017-07-13 20:29:59 +0000 | [diff] [blame] | 414 | // Check for an invalid relocation offset. This check isn't perfect, because |
| 415 | // we don't have the relocation size, which is only known after checking the |
| 416 | // machine and relocation type. As a result, a relocation may overwrite the |
| 417 | // beginning of the following input section. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 418 | if (rel.VirtualAddress >= inputSize) { |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 419 | error("relocation points beyond the end of its parent section"); |
| 420 | continue; |
| 421 | } |
Reid Kleckner | 3b8acb2 | 2017-07-13 20:29:59 +0000 | [diff] [blame] | 422 | |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 423 | applyRelocation(buf + rel.VirtualAddress, rel); |
| 424 | } |
Jacek Caban | fed8e38 | 2024-06-18 11:14:01 +0200 | [diff] [blame] | 425 | |
| 426 | // Write the offset to EC entry thunk preceding section contents. The low bit |
| 427 | // is always set, so it's effectively an offset from the last byte of the |
| 428 | // offset. |
| 429 | if (Defined *entryThunk = getEntryThunk()) |
| 430 | write32le(buf - sizeof(uint32_t), entryThunk->getRVA() - rva + 1); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 431 | } |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 +0000 | [diff] [blame] | 432 | |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 433 | void SectionChunk::applyRelocation(uint8_t *off, |
| 434 | const coff_relocation &rel) const { |
| 435 | auto *sym = dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex)); |
Martin Storsjo | 0f8f0d6 | 2018-09-30 18:31:03 +0000 | [diff] [blame] | 436 | |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 437 | // Get the output section of the symbol for this relocation. The output |
| 438 | // section is needed to compute SECREL and SECTION relocations used in debug |
| 439 | // info. |
| 440 | Chunk *c = sym ? sym->getChunk() : nullptr; |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 441 | COFFLinkerContext &ctx = file->symtab.ctx; |
| 442 | OutputSection *os = c ? ctx.getOutputSection(c) : nullptr; |
Reid Kleckner | a1001b8 | 2017-06-28 17:06:35 +0000 | [diff] [blame] | 443 | |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 444 | // Skip the relocation if it refers to a discarded section, and diagnose it |
| 445 | // as an error if appropriate. If a symbol was discarded early, it may be |
| 446 | // null. If it was discarded late, the output section will be null, unless |
| 447 | // it was an absolute or synthetic symbol. |
| 448 | if (!sym || |
| 449 | (!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) { |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 450 | maybeReportRelocationToDiscarded(this, sym, rel, ctx.config.mingw); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 451 | return; |
| 452 | } |
| 453 | |
| 454 | uint64_t s = sym->getRVA(); |
| 455 | |
| 456 | // Compute the RVA of the relocation for relative relocations. |
| 457 | uint64_t p = rva + rel.VirtualAddress; |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 458 | uint64_t imageBase = ctx.config.imageBase; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 459 | switch (getArch()) { |
| 460 | case Triple::x86_64: |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 461 | applyRelX64(off, rel.Type, os, s, p, imageBase); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 462 | break; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 463 | case Triple::x86: |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 464 | applyRelX86(off, rel.Type, os, s, p, imageBase); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 465 | break; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 466 | case Triple::thumb: |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 467 | applyRelARM(off, rel.Type, os, s, p, imageBase); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 468 | break; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 469 | case Triple::aarch64: |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 470 | applyRelARM64(off, rel.Type, os, s, p, imageBase); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 471 | break; |
| 472 | default: |
| 473 | llvm_unreachable("unknown machine type"); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | // Defend against unsorted relocations. This may be overly conservative. |
| 478 | void SectionChunk::sortRelocations() { |
| 479 | auto cmpByVa = [](const coff_relocation &l, const coff_relocation &r) { |
| 480 | return l.VirtualAddress < r.VirtualAddress; |
| 481 | }; |
| 482 | if (llvm::is_sorted(getRelocs(), cmpByVa)) |
| 483 | return; |
| 484 | warn("some relocations in " + file->getName() + " are not sorted"); |
| 485 | MutableArrayRef<coff_relocation> newRelocs( |
Alexandre Ganea | 83d59e0 | 2022-01-20 14:53:18 -0500 | [diff] [blame] | 486 | bAlloc().Allocate<coff_relocation>(relocsSize), relocsSize); |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 487 | memcpy(newRelocs.data(), relocsData, relocsSize * sizeof(coff_relocation)); |
| 488 | llvm::sort(newRelocs, cmpByVa); |
| 489 | setRelocs(newRelocs); |
| 490 | } |
| 491 | |
| 492 | // Similar to writeTo, but suitable for relocating a subsection of the overall |
| 493 | // section. |
| 494 | void SectionChunk::writeAndRelocateSubsection(ArrayRef<uint8_t> sec, |
| 495 | ArrayRef<uint8_t> subsec, |
| 496 | uint32_t &nextRelocIndex, |
| 497 | uint8_t *buf) const { |
| 498 | assert(!subsec.empty() && !sec.empty()); |
| 499 | assert(sec.begin() <= subsec.begin() && subsec.end() <= sec.end() && |
| 500 | "subsection is not part of this section"); |
| 501 | size_t vaBegin = std::distance(sec.begin(), subsec.begin()); |
| 502 | size_t vaEnd = std::distance(sec.begin(), subsec.end()); |
| 503 | memcpy(buf, subsec.data(), subsec.size()); |
| 504 | for (; nextRelocIndex < relocsSize; ++nextRelocIndex) { |
| 505 | const coff_relocation &rel = relocsData[nextRelocIndex]; |
| 506 | // Only apply relocations that apply to this subsection. These checks |
| 507 | // assume that all subsections completely contain their relocations. |
| 508 | // Relocations must not straddle the beginning or end of a subsection. |
| 509 | if (rel.VirtualAddress < vaBegin) |
Martin Storsjo | 6c67e04 | 2018-08-22 11:34:58 +0000 | [diff] [blame] | 510 | continue; |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 511 | if (rel.VirtualAddress + 1 >= vaEnd) |
Rui Ueyama | 11863b4a | 2015-07-08 01:45:29 +0000 | [diff] [blame] | 512 | break; |
Reid Kleckner | b69db4a | 2021-03-10 14:51:52 -0800 | [diff] [blame] | 513 | applyRelocation(&buf[rel.VirtualAddress - vaBegin], rel); |
Rui Ueyama | 42aa00b | 2015-06-25 00:33:38 +0000 | [diff] [blame] | 514 | } |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 517 | void SectionChunk::addAssociative(SectionChunk *child) { |
Reid Kleckner | 18a9b18 | 2021-04-14 10:39:48 -0700 | [diff] [blame] | 518 | // Insert the child section into the list of associated children. Keep the |
| 519 | // list ordered by section name so that ICF does not depend on section order. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 520 | assert(child->assocChildren == nullptr && |
Reid Kleckner | cc525c9 | 2019-04-02 22:11:58 +0000 | [diff] [blame] | 521 | "associated sections cannot have their own associated children"); |
Reid Kleckner | 18a9b18 | 2021-04-14 10:39:48 -0700 | [diff] [blame] | 522 | SectionChunk *prev = this; |
| 523 | SectionChunk *next = assocChildren; |
| 524 | for (; next != nullptr; prev = next, next = next->assocChildren) { |
| 525 | if (next->getSectionName() <= child->getSectionName()) |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | // Insert child between prev and next. |
| 530 | assert(prev->assocChildren == next); |
| 531 | prev->assocChildren = child; |
| 532 | child->assocChildren = next; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 535 | static uint8_t getBaserelType(const coff_relocation &rel, |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 536 | Triple::ArchType arch) { |
| 537 | switch (arch) { |
| 538 | case Triple::x86_64: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 539 | if (rel.Type == IMAGE_REL_AMD64_ADDR64) |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 540 | return IMAGE_REL_BASED_DIR64; |
Axel Y. Rivera | 4fb131b | 2021-05-21 23:31:03 +0300 | [diff] [blame] | 541 | if (rel.Type == IMAGE_REL_AMD64_ADDR32) |
| 542 | return IMAGE_REL_BASED_HIGHLOW; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 543 | return IMAGE_REL_BASED_ABSOLUTE; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 544 | case Triple::x86: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 545 | if (rel.Type == IMAGE_REL_I386_DIR32) |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 546 | return IMAGE_REL_BASED_HIGHLOW; |
| 547 | return IMAGE_REL_BASED_ABSOLUTE; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 548 | case Triple::thumb: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 549 | if (rel.Type == IMAGE_REL_ARM_ADDR32) |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 550 | return IMAGE_REL_BASED_HIGHLOW; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 551 | if (rel.Type == IMAGE_REL_ARM_MOV32T) |
Rui Ueyama | 237fca1 | 2015-07-25 03:03:46 +0000 | [diff] [blame] | 552 | return IMAGE_REL_BASED_ARM_MOV32T; |
| 553 | return IMAGE_REL_BASED_ABSOLUTE; |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 554 | case Triple::aarch64: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 555 | if (rel.Type == IMAGE_REL_ARM64_ADDR64) |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 556 | return IMAGE_REL_BASED_DIR64; |
| 557 | return IMAGE_REL_BASED_ABSOLUTE; |
Rui Ueyama | 93b4571 | 2015-07-09 20:36:59 +0000 | [diff] [blame] | 558 | default: |
| 559 | llvm_unreachable("unknown machine type"); |
| 560 | } |
| 561 | } |
| 562 | |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 563 | // Windows-specific. |
Rui Ueyama | 93b4571 | 2015-07-09 20:36:59 +0000 | [diff] [blame] | 564 | // Collect all locations that contain absolute addresses, which need to be |
| 565 | // fixed by the loader if load-time relocation is needed. |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 566 | // Only called when base relocation is enabled. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 567 | void SectionChunk::getBaserels(std::vector<Baserel> *res) { |
Reid Kleckner | 69e0bc7 | 2021-01-20 11:00:58 -0800 | [diff] [blame] | 568 | for (const coff_relocation &rel : getRelocs()) { |
Jacek Caban | 8f9903d | 2024-04-04 14:41:50 +0200 | [diff] [blame] | 569 | uint8_t ty = getBaserelType(rel, getArch()); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 570 | if (ty == IMAGE_REL_BASED_ABSOLUTE) |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 571 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 572 | Symbol *target = file->getSymbol(rel.SymbolTableIndex); |
| 573 | if (!target || isa<DefinedAbsolute>(target)) |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 574 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 575 | res->emplace_back(rva + rel.VirtualAddress, ty); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 576 | } |
Jacek Caban | 8408722 | 2025-01-09 21:48:16 +0100 | [diff] [blame] | 577 | |
| 578 | // Insert a 64-bit relocation for CHPEMetadataPointer in the native load |
| 579 | // config of a hybrid ARM64X image. Its value will be set in prepareLoadConfig |
| 580 | // to match the value in the EC load config, which is expected to be |
| 581 | // a relocatable pointer to the __chpe_metadata symbol. |
| 582 | COFFLinkerContext &ctx = file->symtab.ctx; |
Jacek Caban | 5b05728 | 2025-05-15 02:38:24 -0700 | [diff] [blame] | 583 | if (ctx.config.machine == ARM64X && ctx.hybridSymtab->loadConfigSym && |
Jacek Caban | 1c05c61 | 2025-04-11 15:53:25 +0200 | [diff] [blame] | 584 | ctx.hybridSymtab->loadConfigSym->getChunk() == this && |
| 585 | ctx.symtab.loadConfigSym && |
| 586 | ctx.hybridSymtab->loadConfigSize >= |
Jacek Caban | 8408722 | 2025-01-09 21:48:16 +0100 | [diff] [blame] | 587 | offsetof(coff_load_configuration64, CHPEMetadataPointer) + |
| 588 | sizeof(coff_load_configuration64::CHPEMetadataPointer)) |
| 589 | res->emplace_back( |
Jacek Caban | 1c05c61 | 2025-04-11 15:53:25 +0200 | [diff] [blame] | 590 | ctx.hybridSymtab->loadConfigSym->getRVA() + |
Jacek Caban | 8408722 | 2025-01-09 21:48:16 +0100 | [diff] [blame] | 591 | offsetof(coff_load_configuration64, CHPEMetadataPointer), |
| 592 | IMAGE_REL_BASED_DIR64); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 595 | // MinGW specific. |
| 596 | // Check whether a static relocation of type Type can be deferred and |
| 597 | // handled at runtime as a pseudo relocation (for references to a module |
| 598 | // local variable, which turned out to actually need to be imported from |
| 599 | // another DLL) This returns the size the relocation is supposed to update, |
| 600 | // in bits, or 0 if the relocation cannot be handled as a runtime pseudo |
| 601 | // relocation. |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 602 | static int getRuntimePseudoRelocSize(uint16_t type, Triple::ArchType arch) { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 603 | // Relocations that either contain an absolute address, or a plain |
| 604 | // relative offset, since the runtime pseudo reloc implementation |
| 605 | // adds 8/16/32/64 bit values to a memory address. |
| 606 | // |
| 607 | // Given a pseudo relocation entry, |
| 608 | // |
| 609 | // typedef struct { |
| 610 | // DWORD sym; |
| 611 | // DWORD target; |
| 612 | // DWORD flags; |
| 613 | // } runtime_pseudo_reloc_item_v2; |
| 614 | // |
| 615 | // the runtime relocation performs this adjustment: |
| 616 | // *(base + .target) += *(base + .sym) - (base + .sym) |
| 617 | // |
| 618 | // This works for both absolute addresses (IMAGE_REL_*_ADDR32/64, |
| 619 | // IMAGE_REL_I386_DIR32, where the memory location initially contains |
| 620 | // the address of the IAT slot, and for relative addresses (IMAGE_REL*_REL32), |
| 621 | // where the memory location originally contains the relative offset to the |
| 622 | // IAT slot. |
| 623 | // |
| 624 | // This requires the target address to be writable, either directly out of |
| 625 | // the image, or temporarily changed at runtime with VirtualProtect. |
| 626 | // Since this only operates on direct address values, it doesn't work for |
| 627 | // ARM/ARM64 relocations, other than the plain ADDR32/ADDR64 relocations. |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 628 | switch (arch) { |
| 629 | case Triple::x86_64: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 630 | switch (type) { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 631 | case IMAGE_REL_AMD64_ADDR64: |
| 632 | return 64; |
| 633 | case IMAGE_REL_AMD64_ADDR32: |
| 634 | case IMAGE_REL_AMD64_REL32: |
| 635 | case IMAGE_REL_AMD64_REL32_1: |
| 636 | case IMAGE_REL_AMD64_REL32_2: |
| 637 | case IMAGE_REL_AMD64_REL32_3: |
| 638 | case IMAGE_REL_AMD64_REL32_4: |
| 639 | case IMAGE_REL_AMD64_REL32_5: |
| 640 | return 32; |
| 641 | default: |
| 642 | return 0; |
| 643 | } |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 644 | case Triple::x86: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 645 | switch (type) { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 646 | case IMAGE_REL_I386_DIR32: |
| 647 | case IMAGE_REL_I386_REL32: |
| 648 | return 32; |
| 649 | default: |
| 650 | return 0; |
| 651 | } |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 652 | case Triple::thumb: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 653 | switch (type) { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 654 | case IMAGE_REL_ARM_ADDR32: |
| 655 | return 32; |
| 656 | default: |
| 657 | return 0; |
| 658 | } |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 659 | case Triple::aarch64: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 660 | switch (type) { |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 661 | case IMAGE_REL_ARM64_ADDR64: |
| 662 | return 64; |
| 663 | case IMAGE_REL_ARM64_ADDR32: |
| 664 | return 32; |
| 665 | default: |
| 666 | return 0; |
| 667 | } |
| 668 | default: |
| 669 | llvm_unreachable("unknown machine type"); |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // MinGW specific. |
| 674 | // Append information to the provided vector about all relocations that |
| 675 | // need to be handled at runtime as runtime pseudo relocations (references |
| 676 | // to a module local variable, which turned out to actually need to be |
| 677 | // imported from another DLL). |
| 678 | void SectionChunk::getRuntimePseudoRelocs( |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 679 | std::vector<RuntimePseudoReloc> &res) { |
| 680 | for (const coff_relocation &rel : getRelocs()) { |
| 681 | auto *target = |
| 682 | dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex)); |
| 683 | if (!target || !target->isRuntimePseudoReloc) |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 684 | continue; |
Martin Storsjö | 9c970d5 | 2024-04-15 20:14:07 +0300 | [diff] [blame] | 685 | // If the target doesn't have a chunk allocated, it may be a |
| 686 | // DefinedImportData symbol which ended up unnecessary after GC. |
| 687 | // Normally we wouldn't eliminate section chunks that are referenced, but |
| 688 | // references within DWARF sections don't count for keeping section chunks |
| 689 | // alive. Thus such dangling references in DWARF sections are expected. |
| 690 | if (!target->getChunk()) |
| 691 | continue; |
Jacek Caban | 31a6dbe | 2024-10-28 18:44:23 +0100 | [diff] [blame] | 692 | int sizeInBits = getRuntimePseudoRelocSize(rel.Type, getArch()); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 693 | if (sizeInBits == 0) { |
| 694 | error("unable to automatically import from " + target->getName() + |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 695 | " with relocation type " + |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 696 | file->getCOFFObj()->getRelocationTypeName(rel.Type) + " in " + |
| 697 | toString(file)); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 698 | continue; |
| 699 | } |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 700 | int addressSizeInBits = file->symtab.ctx.config.is64() ? 64 : 32; |
Martin Storsjö | 6daa4b9 | 2023-07-06 01:04:12 +0300 | [diff] [blame] | 701 | if (sizeInBits < addressSizeInBits) { |
| 702 | warn("runtime pseudo relocation in " + toString(file) + " against " + |
| 703 | "symbol " + target->getName() + " is too narrow (only " + |
| 704 | Twine(sizeInBits) + " bits wide); this can fail at runtime " + |
| 705 | "depending on memory layout"); |
| 706 | } |
Fangrui Song | 2e2038b | 2019-07-16 08:26:38 +0000 | [diff] [blame] | 707 | // sizeInBits is used to initialize the Flags field; currently no |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 708 | // other flags are defined. |
Jez Ng | 3df4c5a | 2023-01-26 20:28:58 -0500 | [diff] [blame] | 709 | res.emplace_back(target, this, rel.VirtualAddress, sizeInBits); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
| 712 | |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 713 | bool SectionChunk::isCOMDAT() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 714 | return header->Characteristics & IMAGE_SCN_LNK_COMDAT; |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 715 | } |
| 716 | |
Rui Ueyama | fc510f4 | 2015-06-25 19:10:58 +0000 | [diff] [blame] | 717 | void SectionChunk::printDiscardedMessage() const { |
Rui Ueyama | 4bce7bc | 2015-09-16 21:30:40 +0000 | [diff] [blame] | 718 | // Removed by dead-stripping. If it's removed by ICF, ICF already |
| 719 | // printed out the name, so don't repeat that here. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 720 | if (sym && this == repl) |
Nico Weber | 5730d11 | 2022-01-05 11:24:44 -0500 | [diff] [blame] | 721 | log("Discarded " + sym->getName()); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 722 | } |
| 723 | |
Reid Kleckner | a431dd7 | 2019-05-24 20:25:40 +0000 | [diff] [blame] | 724 | StringRef SectionChunk::getDebugName() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 725 | if (sym) |
| 726 | return sym->getName(); |
Rui Ueyama | 6a883b0 | 2015-08-21 07:01:10 +0000 | [diff] [blame] | 727 | return ""; |
Rui Ueyama | 6a60be7 | 2015-06-24 00:00:52 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 730 | ArrayRef<uint8_t> SectionChunk::getContents() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 731 | ArrayRef<uint8_t> a; |
| 732 | cantFail(file->getCOFFObj()->getSectionContents(header, a)); |
| 733 | return a; |
Rui Ueyama | f34c088 | 2015-06-25 17:56:36 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 736 | ArrayRef<uint8_t> SectionChunk::consumeDebugMagic() { |
| 737 | assert(isCodeView()); |
Reid Kleckner | 0a1b1d6 | 2019-05-03 20:17:14 +0000 | [diff] [blame] | 738 | return consumeDebugMagic(getContents(), getSectionName()); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 739 | } |
| 740 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 741 | ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> data, |
| 742 | StringRef sectionName) { |
| 743 | if (data.empty()) |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 744 | return {}; |
| 745 | |
| 746 | // First 4 bytes are section magic. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 747 | if (data.size() < 4) |
| 748 | fatal("the section is too short: " + sectionName); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 749 | |
Fangrui Song | 8d85c96 | 2023-06-05 14:36:19 -0700 | [diff] [blame] | 750 | if (!sectionName.starts_with(".debug$")) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 751 | fatal("invalid section: " + sectionName); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 752 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 753 | uint32_t magic = support::endian::read32le(data.data()); |
| 754 | uint32_t expectedMagic = sectionName == ".debug$H" |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 755 | ? DEBUG_HASHES_SECTION_MAGIC |
| 756 | : DEBUG_SECTION_MAGIC; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 757 | if (magic != expectedMagic) { |
| 758 | warn("ignoring section " + sectionName + " with unrecognized magic 0x" + |
| 759 | utohexstr(magic)); |
Reid Kleckner | efc01ea | 2019-06-12 22:22:44 +0000 | [diff] [blame] | 760 | return {}; |
| 761 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 762 | return data.slice(4); |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 765 | SectionChunk *SectionChunk::findByName(ArrayRef<SectionChunk *> sections, |
| 766 | StringRef name) { |
| 767 | for (SectionChunk *c : sections) |
| 768 | if (c->getSectionName() == name) |
| 769 | return c; |
Alexandre Ganea | d307c4c | 2019-02-23 01:46:18 +0000 | [diff] [blame] | 770 | return nullptr; |
| 771 | } |
| 772 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 773 | void SectionChunk::replace(SectionChunk *other) { |
| 774 | p2Align = std::max(p2Align, other->p2Align); |
| 775 | other->repl = repl; |
| 776 | other->live = false; |
Rui Ueyama | ddf71fc | 2015-06-24 04:36:52 +0000 | [diff] [blame] | 777 | } |
| 778 | |
Alexandre Ganea | 149de8d | 2018-10-05 12:56:46 +0000 | [diff] [blame] | 779 | uint32_t SectionChunk::getSectionNumber() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 780 | DataRefImpl r; |
| 781 | r.p = reinterpret_cast<uintptr_t>(header); |
| 782 | SectionRef s(r, file->getCOFFObj()); |
| 783 | return s.getIndex() + 1; |
Alexandre Ganea | 149de8d | 2018-10-05 12:56:46 +0000 | [diff] [blame] | 784 | } |
| 785 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 786 | CommonChunk::CommonChunk(const COFFSymbolRef s) : sym(s) { |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 +0000 | [diff] [blame] | 787 | // The value of a common symbol is its size. Align all common symbols smaller |
| 788 | // than 32 bytes naturally, i.e. round the size up to the next power of two. |
Rui Ueyama | 5e31d0b | 2015-06-20 07:25:45 +0000 | [diff] [blame] | 789 | // This is what MSVC link.exe does. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 790 | setAlignment(std::min(32U, uint32_t(PowerOf2Ceil(sym.getValue())))); |
| 791 | hasData = false; |
Martin Storsjo | d2752aa | 2017-08-14 19:07:27 +0000 | [diff] [blame] | 792 | } |
| 793 | |
Peter Collingbourne | fa322ab | 2018-04-19 20:03:24 +0000 | [diff] [blame] | 794 | uint32_t CommonChunk::getOutputCharacteristics() const { |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 795 | return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ | |
| 796 | IMAGE_SCN_MEM_WRITE; |
| 797 | } |
| 798 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 799 | void StringChunk::writeTo(uint8_t *buf) const { |
| 800 | memcpy(buf, str.data(), str.size()); |
| 801 | buf[str.size()] = '\0'; |
Rui Ueyama | d6fefba4 | 2015-05-28 19:45:43 +0000 | [diff] [blame] | 802 | } |
| 803 | |
Jacek Caban | 6be9be5 | 2024-09-13 15:42:05 +0200 | [diff] [blame] | 804 | ImportThunkChunk::ImportThunkChunk(COFFLinkerContext &ctx, Defined *s) |
| 805 | : NonSectionCodeChunk(ImportThunkKind), live(!ctx.config.doGC), |
| 806 | impSymbol(s), ctx(ctx) {} |
| 807 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 808 | ImportThunkChunkX64::ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s) |
| 809 | : ImportThunkChunk(ctx, s) { |
Rui Ueyama | 7383562 | 2015-06-26 18:28:56 +0000 | [diff] [blame] | 810 | // Intel Optimization Manual says that all branch targets |
| 811 | // should be 16-byte aligned. MSVC linker does this too. |
Reid Kleckner | ee4e0a2 | 2019-05-22 20:21:52 +0000 | [diff] [blame] | 812 | setAlignment(16); |
Rui Ueyama | 7383562 | 2015-06-26 18:28:56 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 815 | void ImportThunkChunkX64::writeTo(uint8_t *buf) const { |
| 816 | memcpy(buf, importThunkX86, sizeof(importThunkX86)); |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 +0000 | [diff] [blame] | 817 | // The first two bytes is a JMP instruction. Fill its operand. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 818 | write32le(buf + 2, impSymbol->getRVA() - rva - getSize()); |
Rui Ueyama | 33fb2cb | 2015-07-15 00:25:38 +0000 | [diff] [blame] | 819 | } |
| 820 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 821 | void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *res) { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 822 | res->emplace_back(getRVA() + 2, ctx.config.machine); |
Rui Ueyama | 28df042 | 2015-07-25 01:16:06 +0000 | [diff] [blame] | 823 | } |
| 824 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 825 | void ImportThunkChunkX86::writeTo(uint8_t *buf) const { |
| 826 | memcpy(buf, importThunkX86, sizeof(importThunkX86)); |
Rui Ueyama | 743afa0 | 2015-06-06 04:07:39 +0000 | [diff] [blame] | 827 | // The first two bytes is a JMP instruction. Fill its operand. |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 828 | write32le(buf + 2, impSymbol->getRVA() + ctx.config.imageBase); |
Rui Ueyama | 411c6360 | 2015-05-28 19:09:30 +0000 | [diff] [blame] | 829 | } |
| 830 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 831 | void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *res) { |
| 832 | res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T); |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 833 | } |
| 834 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 835 | void ImportThunkChunkARM::writeTo(uint8_t *buf) const { |
| 836 | memcpy(buf, importThunkARM, sizeof(importThunkARM)); |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 837 | // Fix mov.w and mov.t operands. |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 838 | applyMOV32T(buf, impSymbol->getRVA() + ctx.config.imageBase); |
Rui Ueyama | 3dd9372 | 2015-07-25 03:39:29 +0000 | [diff] [blame] | 839 | } |
| 840 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 841 | void ImportThunkChunkARM64::writeTo(uint8_t *buf) const { |
| 842 | int64_t off = impSymbol->getRVA() & 0xfff; |
| 843 | memcpy(buf, importThunkARM64, sizeof(importThunkARM64)); |
| 844 | applyArm64Addr(buf, impSymbol->getRVA(), rva, 12); |
| 845 | applyArm64Ldr(buf + 4, off); |
Martin Storsjo | 2779165 | 2017-07-11 07:22:44 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 +0000 | [diff] [blame] | 848 | // A Thumb2, PIC, non-interworking range extension thunk. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 849 | const uint8_t armThunk[] = { |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 +0000 | [diff] [blame] | 850 | 0x40, 0xf2, 0x00, 0x0c, // P: movw ip,:lower16:S - (P + (L1-P) + 4) |
| 851 | 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4) |
| 852 | 0xe7, 0x44, // L1: add pc, ip |
| 853 | }; |
| 854 | |
Martin Storsjo | c9f4d25 | 2019-02-01 22:08:09 +0000 | [diff] [blame] | 855 | size_t RangeExtensionThunkARM::getSize() const { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 856 | assert(ctx.config.machine == ARMNT); |
| 857 | (void)&ctx; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 858 | return sizeof(armThunk); |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 861 | void RangeExtensionThunkARM::writeTo(uint8_t *buf) const { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 862 | assert(ctx.config.machine == ARMNT); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 863 | uint64_t offset = target->getRVA() - rva - 12; |
| 864 | memcpy(buf, armThunk, sizeof(armThunk)); |
| 865 | applyMOV32T(buf, uint32_t(offset)); |
Martin Storsjo | 57ddec0 | 2018-09-25 10:59:29 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Martin Storsjo | c9f4d25 | 2019-02-01 22:08:09 +0000 | [diff] [blame] | 868 | // A position independent ARM64 adrp+add thunk, with a maximum range of |
| 869 | // +/- 4 GB, which is enough for any PE-COFF. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 870 | const uint8_t arm64Thunk[] = { |
Martin Storsjo | c9f4d25 | 2019-02-01 22:08:09 +0000 | [diff] [blame] | 871 | 0x10, 0x00, 0x00, 0x90, // adrp x16, Dest |
| 872 | 0x10, 0x02, 0x00, 0x91, // add x16, x16, :lo12:Dest |
| 873 | 0x00, 0x02, 0x1f, 0xd6, // br x16 |
| 874 | }; |
| 875 | |
Jacek Caban | efad561 | 2024-08-29 10:19:32 +0200 | [diff] [blame] | 876 | size_t RangeExtensionThunkARM64::getSize() const { return sizeof(arm64Thunk); } |
Martin Storsjo | c9f4d25 | 2019-02-01 22:08:09 +0000 | [diff] [blame] | 877 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 878 | void RangeExtensionThunkARM64::writeTo(uint8_t *buf) const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 879 | memcpy(buf, arm64Thunk, sizeof(arm64Thunk)); |
| 880 | applyArm64Addr(buf + 0, target->getRVA(), rva, 12); |
| 881 | applyArm64Imm(buf + 4, target->getRVA() & 0xfff, 0); |
Martin Storsjo | c9f4d25 | 2019-02-01 22:08:09 +0000 | [diff] [blame] | 882 | } |
| 883 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 884 | LocalImportChunk::LocalImportChunk(COFFLinkerContext &c, Defined *s) |
| 885 | : sym(s), ctx(c) { |
| 886 | setAlignment(ctx.config.wordsize); |
Amy Huang | 7370ff6 | 2022-07-14 15:53:32 -0400 | [diff] [blame] | 887 | } |
| 888 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 889 | void LocalImportChunk::getBaserels(std::vector<Baserel> *res) { |
| 890 | res->emplace_back(getRVA(), ctx.config.machine); |
| 891 | } |
| 892 | |
| 893 | size_t LocalImportChunk::getSize() const { return ctx.config.wordsize; } |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 894 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 895 | void LocalImportChunk::writeTo(uint8_t *buf) const { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 896 | if (ctx.config.is64()) { |
| 897 | write64le(buf, sym->getRVA() + ctx.config.imageBase); |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 898 | } else { |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 899 | write32le(buf, sym->getRVA() + ctx.config.imageBase); |
Rui Ueyama | d4b351f | 2015-07-09 21:15:58 +0000 | [diff] [blame] | 900 | } |
Rui Ueyama | 7a333c6 | 2015-07-02 20:33:50 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 903 | void RVATableChunk::writeTo(uint8_t *buf) const { |
| 904 | ulittle32_t *begin = reinterpret_cast<ulittle32_t *>(buf); |
| 905 | size_t cnt = 0; |
| 906 | for (const ChunkAndOffset &co : syms) |
| 907 | begin[cnt++] = co.inputChunk->getRVA() + co.offset; |
Dmitri Gribenko | aba4303 | 2022-07-23 15:14:14 +0200 | [diff] [blame] | 908 | llvm::sort(begin, begin + cnt); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 909 | assert(std::unique(begin, begin + cnt) == begin + cnt && |
Reid Kleckner | af2f7da | 2018-02-06 01:58:26 +0000 | [diff] [blame] | 910 | "RVA tables should be de-duplicated"); |
Rui Ueyama | cd3f99b | 2015-07-24 23:51:14 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Pengfei Wang | 184377d | 2021-04-14 14:21:52 +0800 | [diff] [blame] | 913 | void RVAFlagTableChunk::writeTo(uint8_t *buf) const { |
| 914 | struct RVAFlag { |
| 915 | ulittle32_t rva; |
| 916 | uint8_t flag; |
| 917 | }; |
David Blaikie | e5b66a3 | 2021-05-24 16:51:31 -0700 | [diff] [blame] | 918 | auto flags = |
Joe Loser | a288d7f | 2023-01-15 21:39:16 -0700 | [diff] [blame] | 919 | MutableArrayRef(reinterpret_cast<RVAFlag *>(buf), syms.size()); |
David Blaikie | e5b66a3 | 2021-05-24 16:51:31 -0700 | [diff] [blame] | 920 | for (auto t : zip(syms, flags)) { |
| 921 | const auto &sym = std::get<0>(t); |
| 922 | auto &flag = std::get<1>(t); |
| 923 | flag.rva = sym.inputChunk->getRVA() + sym.offset; |
| 924 | flag.flag = 0; |
Pengfei Wang | 184377d | 2021-04-14 14:21:52 +0800 | [diff] [blame] | 925 | } |
David Blaikie | e5b66a3 | 2021-05-24 16:51:31 -0700 | [diff] [blame] | 926 | llvm::sort(flags, |
| 927 | [](const RVAFlag &a, const RVAFlag &b) { return a.rva < b.rva; }); |
| 928 | assert(llvm::unique(flags, [](const RVAFlag &a, |
| 929 | const RVAFlag &b) { return a.rva == b.rva; }) == |
| 930 | flags.end() && |
Pengfei Wang | 184377d | 2021-04-14 14:21:52 +0800 | [diff] [blame] | 931 | "RVA tables should be de-duplicated"); |
| 932 | } |
| 933 | |
Jacek Caban | fe2bd12 | 2023-11-15 12:35:45 +0100 | [diff] [blame] | 934 | size_t ECCodeMapChunk::getSize() const { |
| 935 | return map.size() * sizeof(chpe_range_entry); |
| 936 | } |
| 937 | |
| 938 | void ECCodeMapChunk::writeTo(uint8_t *buf) const { |
| 939 | auto table = reinterpret_cast<chpe_range_entry *>(buf); |
| 940 | for (uint32_t i = 0; i < map.size(); i++) { |
| 941 | const ECCodeMapEntry &entry = map[i]; |
| 942 | uint32_t start = entry.first->getRVA(); |
| 943 | table[i].StartOffset = start | entry.type; |
| 944 | table[i].Length = entry.last->getRVA() + entry.last->getSize() - start; |
| 945 | } |
| 946 | } |
| 947 | |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 948 | // MinGW specific, for the "automatic import of variables from DLLs" feature. |
| 949 | size_t PseudoRelocTableChunk::getSize() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 950 | if (relocs.empty()) |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 951 | return 0; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 952 | return 12 + 12 * relocs.size(); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | // MinGW specific. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 956 | void PseudoRelocTableChunk::writeTo(uint8_t *buf) const { |
| 957 | if (relocs.empty()) |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 958 | return; |
| 959 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 960 | ulittle32_t *table = reinterpret_cast<ulittle32_t *>(buf); |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 961 | // This is the list header, to signal the runtime pseudo relocation v2 |
| 962 | // format. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 963 | table[0] = 0; |
| 964 | table[1] = 0; |
| 965 | table[2] = 1; |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 966 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 967 | size_t idx = 3; |
| 968 | for (const RuntimePseudoReloc &rpr : relocs) { |
| 969 | table[idx + 0] = rpr.sym->getRVA(); |
| 970 | table[idx + 1] = rpr.target->getRVA() + rpr.targetOffset; |
| 971 | table[idx + 2] = rpr.flags; |
| 972 | idx += 3; |
Martin Storsjo | eac1b05 | 2018-08-27 08:43:31 +0000 | [diff] [blame] | 973 | } |
| 974 | } |
| 975 | |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 976 | // Windows-specific. This class represents a block in .reloc section. |
| 977 | // The format is described here. |
| 978 | // |
| 979 | // On Windows, each DLL is linked against a fixed base address and |
| 980 | // usually loaded to that address. However, if there's already another |
| 981 | // DLL that overlaps, the loader has to relocate it. To do that, DLLs |
| 982 | // contain .reloc sections which contain offsets that need to be fixed |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 983 | // up at runtime. If the loader finds that a DLL cannot be loaded to its |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 984 | // desired base address, it loads it to somewhere else, and add <actual |
| 985 | // base address> - <desired base address> to each offset that is |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 986 | // specified by the .reloc section. In ELF terms, .reloc sections |
| 987 | // contain relative relocations in REL format (as opposed to RELA.) |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 988 | // |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 989 | // This already significantly reduces the size of relocations compared |
| 990 | // to ELF .rel.dyn, but Windows does more to reduce it (probably because |
| 991 | // it was invented for PCs in the late '80s or early '90s.) Offsets in |
| 992 | // .reloc are grouped by page where the page size is 12 bits, and |
| 993 | // offsets sharing the same page address are stored consecutively to |
| 994 | // represent them with less space. This is very similar to the page |
| 995 | // table which is grouped by (multiple stages of) pages. |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 996 | // |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 997 | // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00, |
| 998 | // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4 |
| 999 | // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they |
| 1000 | // are represented like this: |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 1001 | // |
| 1002 | // 0x00000 -- page address (4 bytes) |
| 1003 | // 16 -- size of this block (4 bytes) |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 1004 | // 0xA030 -- entries (2 bytes each) |
| 1005 | // 0xA500 |
| 1006 | // 0xA700 |
| 1007 | // 0xAA00 |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 1008 | // 0x20000 -- page address (4 bytes) |
| 1009 | // 12 -- size of this block (4 bytes) |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 1010 | // 0xA004 -- entries (2 bytes each) |
| 1011 | // 0xA008 |
Rui Ueyama | 1a7f6af | 2017-04-25 03:31:10 +0000 | [diff] [blame] | 1012 | // |
Rui Ueyama | 0417172 | 2017-04-26 20:20:05 +0000 | [diff] [blame] | 1013 | // Usually we have a lot of relocations for each page, so the number of |
Rui Ueyama | 6cef17c | 2017-04-26 19:50:49 +0000 | [diff] [blame] | 1014 | // bytes for one .reloc entry is close to 2 bytes on average. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1015 | BaserelChunk::BaserelChunk(uint32_t page, Baserel *begin, Baserel *end) { |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 1016 | // Block header consists of 4 byte page RVA and 4 byte block size. |
| 1017 | // Each entry is 2 byte. Last entry may be padding. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1018 | data.resize(alignTo((end - begin) * 2 + 8, 4)); |
| 1019 | uint8_t *p = data.data(); |
| 1020 | write32le(p, page); |
| 1021 | write32le(p + 4, data.size()); |
| 1022 | p += 8; |
| 1023 | for (Baserel *i = begin; i != end; ++i) { |
| 1024 | write16le(p, (i->type << 12) | (i->rva - page)); |
| 1025 | p += 2; |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 1026 | } |
| 1027 | } |
| 1028 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1029 | void BaserelChunk::writeTo(uint8_t *buf) const { |
| 1030 | memcpy(buf, data.data(), data.size()); |
Rui Ueyama | 588e832 | 2015-06-15 01:23:58 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Amy Huang | 5a58b19 | 2023-01-09 23:37:28 -0500 | [diff] [blame] | 1033 | uint8_t Baserel::getDefaultType(llvm::COFF::MachineTypes machine) { |
Jacek Caban | 233ed51 | 2024-09-05 15:57:20 +0200 | [diff] [blame] | 1034 | return is64Bit(machine) ? IMAGE_REL_BASED_DIR64 : IMAGE_REL_BASED_HIGHLOW; |
Rui Ueyama | 3afd5bf | 2015-07-25 01:44:32 +0000 | [diff] [blame] | 1035 | } |
| 1036 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1037 | MergeChunk::MergeChunk(uint32_t alignment) |
Guillaume Chatelet | 173f62d | 2022-12-02 12:42:43 +0000 | [diff] [blame] | 1038 | : builder(StringTableBuilder::RAW, llvm::Align(alignment)) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1039 | setAlignment(alignment); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
Amy Huang | 6f7483b | 2021-09-16 16:48:26 -0700 | [diff] [blame] | 1042 | void MergeChunk::addSection(COFFLinkerContext &ctx, SectionChunk *c) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1043 | assert(isPowerOf2_32(c->getAlignment())); |
| 1044 | uint8_t p2Align = llvm::Log2_32(c->getAlignment()); |
Joe Loser | 1173ecf | 2022-09-09 12:43:07 -0600 | [diff] [blame] | 1045 | assert(p2Align < std::size(ctx.mergeChunkInstances)); |
Amy Huang | 6f7483b | 2021-09-16 16:48:26 -0700 | [diff] [blame] | 1046 | auto *&mc = ctx.mergeChunkInstances[p2Align]; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1047 | if (!mc) |
| 1048 | mc = make<MergeChunk>(c->getAlignment()); |
| 1049 | mc->sections.push_back(c); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | void MergeChunk::finalizeContents() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1053 | assert(!finalized && "should only finalize once"); |
| 1054 | for (SectionChunk *c : sections) |
| 1055 | if (c->live) |
| 1056 | builder.add(toStringRef(c->getContents())); |
| 1057 | builder.finalize(); |
| 1058 | finalized = true; |
Reid Kleckner | 11c141e | 2019-05-24 00:02:00 +0000 | [diff] [blame] | 1059 | } |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1060 | |
Reid Kleckner | 11c141e | 2019-05-24 00:02:00 +0000 | [diff] [blame] | 1061 | void MergeChunk::assignSubsectionRVAs() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1062 | for (SectionChunk *c : sections) { |
| 1063 | if (!c->live) |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1064 | continue; |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1065 | size_t off = builder.getOffset(toStringRef(c->getContents())); |
| 1066 | c->setRVA(rva + off); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1067 | } |
| 1068 | } |
| 1069 | |
Peter Collingbourne | fa322ab | 2018-04-19 20:03:24 +0000 | [diff] [blame] | 1070 | uint32_t MergeChunk::getOutputCharacteristics() const { |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1071 | return IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA; |
| 1072 | } |
| 1073 | |
| 1074 | size_t MergeChunk::getSize() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1075 | return builder.getSize(); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1078 | void MergeChunk::writeTo(uint8_t *buf) const { |
| 1079 | builder.write(buf); |
Peter Collingbourne | f1a11f8 | 2018-03-15 21:14:02 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 +0000 | [diff] [blame] | 1082 | // MinGW specific. |
Jacek Caban | a92bfaa | 2025-02-17 21:34:12 +0100 | [diff] [blame] | 1083 | size_t AbsolutePointerChunk::getSize() const { |
| 1084 | return symtab.ctx.config.wordsize; |
| 1085 | } |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 +0000 | [diff] [blame] | 1086 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1087 | void AbsolutePointerChunk::writeTo(uint8_t *buf) const { |
Jacek Caban | a92bfaa | 2025-02-17 21:34:12 +0100 | [diff] [blame] | 1088 | if (symtab.ctx.config.is64()) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1089 | write64le(buf, value); |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 +0000 | [diff] [blame] | 1090 | } else { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 1091 | write32le(buf, value); |
Martin Storsjo | 7a41693 | 2018-09-14 22:26:59 +0000 | [diff] [blame] | 1092 | } |
| 1093 | } |
| 1094 | |
Jacek Caban | a92bfaa | 2025-02-17 21:34:12 +0100 | [diff] [blame] | 1095 | MachineTypes AbsolutePointerChunk::getMachine() const { return symtab.machine; } |
| 1096 | |
Jacek Caban | a2d8743 | 2024-08-22 22:03:05 +0200 | [diff] [blame] | 1097 | void ECExportThunkChunk::writeTo(uint8_t *buf) const { |
| 1098 | memcpy(buf, ECExportThunkCode, sizeof(ECExportThunkCode)); |
| 1099 | write32le(buf + 10, target->getRVA() - rva - 14); |
| 1100 | } |
| 1101 | |
Jacek Caban | 52a7116 | 2024-08-23 21:17:38 +0200 | [diff] [blame] | 1102 | size_t CHPECodeRangesChunk::getSize() const { |
| 1103 | return exportThunks.size() * sizeof(chpe_code_range_entry); |
| 1104 | } |
| 1105 | |
| 1106 | void CHPECodeRangesChunk::writeTo(uint8_t *buf) const { |
| 1107 | auto ranges = reinterpret_cast<chpe_code_range_entry *>(buf); |
| 1108 | |
| 1109 | for (uint32_t i = 0; i < exportThunks.size(); i++) { |
| 1110 | Chunk *thunk = exportThunks[i].first; |
| 1111 | uint32_t start = thunk->getRVA(); |
| 1112 | ranges[i].StartRva = start; |
| 1113 | ranges[i].EndRva = start + thunk->getSize(); |
| 1114 | ranges[i].EntryPoint = start; |
| 1115 | } |
| 1116 | } |
| 1117 | |
Jacek Caban | caa844e | 2024-08-23 20:29:19 +0200 | [diff] [blame] | 1118 | size_t CHPERedirectionChunk::getSize() const { |
Jacek Caban | dafbc97 | 2024-11-07 12:44:45 +0100 | [diff] [blame] | 1119 | // Add an extra +1 for a terminator entry. |
| 1120 | return (exportThunks.size() + 1) * sizeof(chpe_redirection_entry); |
Jacek Caban | caa844e | 2024-08-23 20:29:19 +0200 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | void CHPERedirectionChunk::writeTo(uint8_t *buf) const { |
| 1124 | auto entries = reinterpret_cast<chpe_redirection_entry *>(buf); |
| 1125 | |
| 1126 | for (uint32_t i = 0; i < exportThunks.size(); i++) { |
| 1127 | entries[i].Source = exportThunks[i].first->getRVA(); |
| 1128 | entries[i].Destination = exportThunks[i].second->getRVA(); |
| 1129 | } |
| 1130 | } |
| 1131 | |
Jacek Caban | 99a2354 | 2024-09-11 14:46:40 +0200 | [diff] [blame] | 1132 | ImportThunkChunkARM64EC::ImportThunkChunkARM64EC(ImportFile *file) |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 1133 | : ImportThunkChunk(file->symtab.ctx, file->impSym), file(file) {} |
Jacek Caban | 99a2354 | 2024-09-11 14:46:40 +0200 | [diff] [blame] | 1134 | |
Jacek Caban | f661e695 | 2024-09-26 10:44:40 +0200 | [diff] [blame] | 1135 | size_t ImportThunkChunkARM64EC::getSize() const { |
| 1136 | if (!extended) |
| 1137 | return sizeof(importThunkARM64EC); |
| 1138 | // The last instruction is replaced with an inline range extension thunk. |
| 1139 | return sizeof(importThunkARM64EC) + sizeof(arm64Thunk) - sizeof(uint32_t); |
| 1140 | } |
| 1141 | |
Jacek Caban | 99a2354 | 2024-09-11 14:46:40 +0200 | [diff] [blame] | 1142 | void ImportThunkChunkARM64EC::writeTo(uint8_t *buf) const { |
| 1143 | memcpy(buf, importThunkARM64EC, sizeof(importThunkARM64EC)); |
| 1144 | applyArm64Addr(buf, file->impSym->getRVA(), rva, 12); |
| 1145 | applyArm64Ldr(buf + 4, file->impSym->getRVA() & 0xfff); |
| 1146 | |
| 1147 | // The exit thunk may be missing. This can happen if the application only |
| 1148 | // references a function by its address (in which case the thunk is never |
| 1149 | // actually used, but is still required to fill the auxiliary IAT), or in |
| 1150 | // cases of hand-written assembly calling an imported ARM64EC function (where |
| 1151 | // the exit thunk is ignored by __icall_helper_arm64ec). In such cases, MSVC |
| 1152 | // link.exe uses 0 as the RVA. |
| 1153 | uint32_t exitThunkRVA = exitThunk ? exitThunk->getRVA() : 0; |
| 1154 | applyArm64Addr(buf + 8, exitThunkRVA, rva + 8, 12); |
| 1155 | applyArm64Imm(buf + 12, exitThunkRVA & 0xfff, 0); |
| 1156 | |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 1157 | Defined *helper = cast<Defined>(file->symtab.ctx.config.arm64ECIcallHelper); |
Jacek Caban | f661e695 | 2024-09-26 10:44:40 +0200 | [diff] [blame] | 1158 | if (extended) { |
| 1159 | // Replace last instruction with an inline range extension thunk. |
| 1160 | memcpy(buf + 16, arm64Thunk, sizeof(arm64Thunk)); |
| 1161 | applyArm64Addr(buf + 16, helper->getRVA(), rva + 16, 12); |
| 1162 | applyArm64Imm(buf + 20, helper->getRVA() & 0xfff, 0); |
| 1163 | } else { |
| 1164 | applyArm64Branch26(buf + 16, helper->getRVA() - rva - 16); |
| 1165 | } |
| 1166 | } |
| 1167 | |
| 1168 | bool ImportThunkChunkARM64EC::verifyRanges() { |
| 1169 | if (extended) |
| 1170 | return true; |
Jacek Caban | 6b493ba | 2024-12-15 12:45:34 +0100 | [diff] [blame] | 1171 | auto helper = cast<Defined>(file->symtab.ctx.config.arm64ECIcallHelper); |
Jacek Caban | f661e695 | 2024-09-26 10:44:40 +0200 | [diff] [blame] | 1172 | return isInt<28>(helper->getRVA() - rva - 16); |
| 1173 | } |
| 1174 | |
| 1175 | uint32_t ImportThunkChunkARM64EC::extendRanges() { |
| 1176 | if (extended || verifyRanges()) |
| 1177 | return 0; |
| 1178 | extended = true; |
| 1179 | // The last instruction is replaced with an inline range extension thunk. |
| 1180 | return sizeof(arm64Thunk) - sizeof(uint32_t); |
Jacek Caban | 99a2354 | 2024-09-11 14:46:40 +0200 | [diff] [blame] | 1181 | } |
| 1182 | |
Jacek Caban | 3b0dafff | 2025-01-10 21:50:07 +0100 | [diff] [blame] | 1183 | uint64_t Arm64XRelocVal::get() const { |
Jacek Caban | 659e66e | 2025-01-21 22:24:00 +0100 | [diff] [blame] | 1184 | return (sym ? sym->getRVA() : 0) + (chunk ? chunk->getRVA() : 0) + value; |
Jacek Caban | 3b0dafff | 2025-01-10 21:50:07 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1187 | size_t Arm64XDynamicRelocEntry::getSize() const { |
| 1188 | switch (type) { |
Jacek Caban | fb01a28 | 2025-01-26 22:11:40 +0100 | [diff] [blame] | 1189 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL: |
| 1190 | return sizeof(uint16_t); // Just a header. |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1191 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE: |
| 1192 | return sizeof(uint16_t) + size; // A header and a payload. |
| 1193 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA: |
Jacek Caban | fb01a28 | 2025-01-26 22:11:40 +0100 | [diff] [blame] | 1194 | return 2 * sizeof(uint16_t); // A header and a delta. |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1195 | } |
Jacek Caban | 799e988 | 2025-01-10 14:38:22 +0100 | [diff] [blame] | 1196 | llvm_unreachable("invalid type"); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1197 | } |
| 1198 | |
| 1199 | void Arm64XDynamicRelocEntry::writeTo(uint8_t *buf) const { |
| 1200 | auto out = reinterpret_cast<ulittle16_t *>(buf); |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1201 | *out = (offset.get() & 0xfff) | (type << 12); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1202 | |
| 1203 | switch (type) { |
Jacek Caban | fb01a28 | 2025-01-26 22:11:40 +0100 | [diff] [blame] | 1204 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_ZEROFILL: |
| 1205 | *out |= ((bit_width(size) - 1) << 14); // Encode the size. |
| 1206 | break; |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1207 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_VALUE: |
| 1208 | *out |= ((bit_width(size) - 1) << 14); // Encode the size. |
| 1209 | switch (size) { |
| 1210 | case 2: |
Jacek Caban | 3b0dafff | 2025-01-10 21:50:07 +0100 | [diff] [blame] | 1211 | out[1] = value.get(); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | case 4: |
Jacek Caban | 3b0dafff | 2025-01-10 21:50:07 +0100 | [diff] [blame] | 1214 | *reinterpret_cast<ulittle32_t *>(out + 1) = value.get(); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1215 | break; |
| 1216 | case 8: |
Jacek Caban | 3b0dafff | 2025-01-10 21:50:07 +0100 | [diff] [blame] | 1217 | *reinterpret_cast<ulittle64_t *>(out + 1) = value.get(); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1218 | break; |
| 1219 | default: |
| 1220 | llvm_unreachable("invalid size"); |
| 1221 | } |
| 1222 | break; |
| 1223 | case IMAGE_DVRT_ARM64X_FIXUP_TYPE_DELTA: |
Jacek Caban | fb01a28 | 2025-01-26 22:11:40 +0100 | [diff] [blame] | 1224 | int delta = value.get(); |
| 1225 | // Negative offsets use a sign bit in the header. |
| 1226 | if (delta < 0) { |
| 1227 | *out |= 1 << 14; |
| 1228 | delta = -delta; |
| 1229 | } |
| 1230 | // Depending on the value, the delta is encoded with a shift of 2 or 3 bits. |
| 1231 | if (delta & 7) { |
| 1232 | assert(!(delta & 3)); |
| 1233 | delta >>= 2; |
| 1234 | } else { |
| 1235 | *out |= (1 << 15); |
| 1236 | delta >>= 3; |
| 1237 | } |
| 1238 | out[1] = delta; |
| 1239 | assert(!(delta & ~0xffff)); |
| 1240 | break; |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1241 | } |
| 1242 | } |
| 1243 | |
| 1244 | void DynamicRelocsChunk::finalize() { |
| 1245 | llvm::stable_sort(arm64xRelocs, [=](const Arm64XDynamicRelocEntry &a, |
| 1246 | const Arm64XDynamicRelocEntry &b) { |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1247 | return a.offset.get() < b.offset.get(); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1248 | }); |
| 1249 | |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1250 | size = sizeof(coff_dynamic_reloc_table) + sizeof(coff_dynamic_relocation64); |
| 1251 | uint32_t prevPage = 0xfff; |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1252 | |
| 1253 | for (const Arm64XDynamicRelocEntry &entry : arm64xRelocs) { |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1254 | uint32_t page = entry.offset.get() & ~0xfff; |
| 1255 | if (page != prevPage) { |
| 1256 | size = alignTo(size, sizeof(uint32_t)) + |
| 1257 | sizeof(coff_base_reloc_block_header); |
| 1258 | prevPage = page; |
| 1259 | } |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1260 | size += entry.getSize(); |
| 1261 | } |
| 1262 | |
| 1263 | size = alignTo(size, sizeof(uint32_t)); |
| 1264 | } |
| 1265 | |
Jacek Caban | 659e66e | 2025-01-21 22:24:00 +0100 | [diff] [blame] | 1266 | // Set the reloc value. The reloc entry must be allocated beforehand. |
| 1267 | void DynamicRelocsChunk::set(uint32_t rva, Arm64XRelocVal value) { |
| 1268 | auto entry = |
| 1269 | llvm::find_if(arm64xRelocs, [rva](const Arm64XDynamicRelocEntry &e) { |
| 1270 | return e.offset.get() == rva; |
| 1271 | }); |
| 1272 | assert(entry != arm64xRelocs.end()); |
| 1273 | assert(!entry->value.get()); |
| 1274 | entry->value = value; |
| 1275 | } |
| 1276 | |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1277 | void DynamicRelocsChunk::writeTo(uint8_t *buf) const { |
| 1278 | auto table = reinterpret_cast<coff_dynamic_reloc_table *>(buf); |
| 1279 | table->Version = 1; |
| 1280 | table->Size = sizeof(coff_dynamic_relocation64); |
| 1281 | buf += sizeof(*table); |
| 1282 | |
| 1283 | auto header = reinterpret_cast<coff_dynamic_relocation64 *>(buf); |
| 1284 | header->Symbol = IMAGE_DYNAMIC_RELOCATION_ARM64X; |
| 1285 | buf += sizeof(*header); |
| 1286 | |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1287 | coff_base_reloc_block_header *pageHeader = nullptr; |
| 1288 | size_t relocSize = 0; |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1289 | for (const Arm64XDynamicRelocEntry &entry : arm64xRelocs) { |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1290 | uint32_t page = entry.offset.get() & ~0xfff; |
| 1291 | if (!pageHeader || page != pageHeader->PageRVA) { |
| 1292 | relocSize = alignTo(relocSize, sizeof(uint32_t)); |
| 1293 | if (pageHeader) |
| 1294 | pageHeader->BlockSize = |
| 1295 | buf + relocSize - reinterpret_cast<uint8_t *>(pageHeader); |
| 1296 | pageHeader = |
| 1297 | reinterpret_cast<coff_base_reloc_block_header *>(buf + relocSize); |
| 1298 | pageHeader->PageRVA = page; |
| 1299 | relocSize += sizeof(*pageHeader); |
| 1300 | } |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1301 | |
Jacek Caban | a16adaf | 2025-01-20 11:38:54 +0100 | [diff] [blame] | 1302 | entry.writeTo(buf + relocSize); |
| 1303 | relocSize += entry.getSize(); |
| 1304 | } |
| 1305 | relocSize = alignTo(relocSize, sizeof(uint32_t)); |
| 1306 | pageHeader->BlockSize = |
| 1307 | buf + relocSize - reinterpret_cast<uint8_t *>(pageHeader); |
| 1308 | |
| 1309 | header->BaseRelocSize = relocSize; |
| 1310 | table->Size += relocSize; |
| 1311 | assert(size == sizeof(*table) + sizeof(*header) + relocSize); |
Jacek Caban | 71bbafb | 2024-12-05 13:07:41 +0100 | [diff] [blame] | 1312 | } |
| 1313 | |
Nico Weber | 7c26641 | 2022-08-08 11:32:26 -0400 | [diff] [blame] | 1314 | } // namespace lld::coff |