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