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