xref: /llvm-project-15.0.7/lld/COFF/Chunks.cpp (revision 200458f3)
1 //===- Chunks.cpp ---------------------------------------------------------===//
2 //
3 //                             The LLVM Linker
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "Chunks.h"
11 #include "InputFiles.h"
12 #include "Symbols.h"
13 #include "Writer.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "llvm/ADT/Twine.h"
16 #include "llvm/BinaryFormat/COFF.h"
17 #include "llvm/Object/COFF.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/Endian.h"
20 #include "llvm/Support/raw_ostream.h"
21 #include <algorithm>
22 
23 using namespace llvm;
24 using namespace llvm::object;
25 using namespace llvm::support::endian;
26 using namespace llvm::COFF;
27 using llvm::support::ulittle32_t;
28 
29 namespace lld {
30 namespace coff {
31 
32 SectionChunk::SectionChunk(ObjFile *F, const coff_section *H)
33     : Chunk(SectionKind), Repl(this), Header(H), File(F),
34       Relocs(File->getCOFFObj()->getRelocations(Header)),
35       NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
36   // Initialize SectionName.
37   File->getCOFFObj()->getSectionName(Header, SectionName);
38 
39   Alignment = Header->getAlignment();
40 
41   // Chunks may be discarded during comdat merging.
42   Discarded = false;
43 
44   // If linker GC is disabled, every chunk starts out alive.  If linker GC is
45   // enabled, treat non-comdat sections as roots. Generally optimized object
46   // files will be built with -ffunction-sections or /Gy, so most things worth
47   // stripping will be in a comdat.
48   Live = !Config->DoGC || !isCOMDAT();
49 }
50 
51 static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
52 static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
53 static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
54 static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
55 static void or32(uint8_t *P, uint32_t V) { write32le(P, read32le(P) | V); }
56 
57 static void applySecRel(const SectionChunk *Sec, uint8_t *Off,
58                         OutputSection *OS, uint64_t S) {
59   if (!OS) {
60     if (Sec->isCodeView())
61       return;
62     fatal("SECREL relocation cannot be applied to absolute symbols");
63   }
64   uint64_t SecRel = S - OS->getRVA();
65   if (SecRel > UINT32_MAX) {
66     error("overflow in SECREL relocation in section: " + Sec->getSectionName());
67     return;
68   }
69   add32(Off, SecRel);
70 }
71 
72 static void applySecIdx(uint8_t *Off, OutputSection *OS) {
73   // If we have no output section, this must be an absolute symbol. Use the
74   // sentinel absolute symbol section index.
75   uint16_t SecIdx = OS ? OS->SectionIndex : DefinedAbsolute::OutputSectionIndex;
76   add16(Off, SecIdx);
77 }
78 
79 void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS,
80                                uint64_t S, uint64_t P) const {
81   switch (Type) {
82   case IMAGE_REL_AMD64_ADDR32:   add32(Off, S + Config->ImageBase); break;
83   case IMAGE_REL_AMD64_ADDR64:   add64(Off, S + Config->ImageBase); break;
84   case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
85   case IMAGE_REL_AMD64_REL32:    add32(Off, S - P - 4); break;
86   case IMAGE_REL_AMD64_REL32_1:  add32(Off, S - P - 5); break;
87   case IMAGE_REL_AMD64_REL32_2:  add32(Off, S - P - 6); break;
88   case IMAGE_REL_AMD64_REL32_3:  add32(Off, S - P - 7); break;
89   case IMAGE_REL_AMD64_REL32_4:  add32(Off, S - P - 8); break;
90   case IMAGE_REL_AMD64_REL32_5:  add32(Off, S - P - 9); break;
91   case IMAGE_REL_AMD64_SECTION:  applySecIdx(Off, OS); break;
92   case IMAGE_REL_AMD64_SECREL:   applySecRel(this, Off, OS, S); break;
93   default:
94     fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
95   }
96 }
97 
98 void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS,
99                                uint64_t S, uint64_t P) const {
100   switch (Type) {
101   case IMAGE_REL_I386_ABSOLUTE: break;
102   case IMAGE_REL_I386_DIR32:    add32(Off, S + Config->ImageBase); break;
103   case IMAGE_REL_I386_DIR32NB:  add32(Off, S); break;
104   case IMAGE_REL_I386_REL32:    add32(Off, S - P - 4); break;
105   case IMAGE_REL_I386_SECTION:  applySecIdx(Off, OS); break;
106   case IMAGE_REL_I386_SECREL:   applySecRel(this, Off, OS, S); break;
107   default:
108     fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
109   }
110 }
111 
112 static void applyMOV(uint8_t *Off, uint16_t V) {
113   write16le(Off, (read16le(Off) & 0xfbf0) | ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
114   write16le(Off + 2, (read16le(Off + 2) & 0x8f00) | ((V & 0x700) << 4) | (V & 0xff));
115 }
116 
117 static uint16_t readMOV(uint8_t *Off) {
118   uint16_t Opcode1 = read16le(Off);
119   uint16_t Opcode2 = read16le(Off + 2);
120   uint16_t Imm = (Opcode2 & 0x00ff) | ((Opcode2 >> 4) & 0x0700);
121   Imm |= ((Opcode1 << 1) & 0x0800) | ((Opcode1 & 0x000f) << 12);
122   return Imm;
123 }
124 
125 void applyMOV32T(uint8_t *Off, uint32_t V) {
126   uint16_t ImmW = readMOV(Off);     // read MOVW operand
127   uint16_t ImmT = readMOV(Off + 4); // read MOVT operand
128   uint32_t Imm = ImmW | (ImmT << 16);
129   V += Imm;                         // add the immediate offset
130   applyMOV(Off, V);           // set MOVW operand
131   applyMOV(Off + 4, V >> 16); // set MOVT operand
132 }
133 
134 static void applyBranch20T(uint8_t *Off, int32_t V) {
135   uint32_t S = V < 0 ? 1 : 0;
136   uint32_t J1 = (V >> 19) & 1;
137   uint32_t J2 = (V >> 18) & 1;
138   or16(Off, (S << 10) | ((V >> 12) & 0x3f));
139   or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
140 }
141 
142 void applyBranch24T(uint8_t *Off, int32_t V) {
143   if (!isInt<25>(V))
144     fatal("relocation out of range");
145   uint32_t S = V < 0 ? 1 : 0;
146   uint32_t J1 = ((~V >> 23) & 1) ^ S;
147   uint32_t J2 = ((~V >> 22) & 1) ^ S;
148   or16(Off, (S << 10) | ((V >> 12) & 0x3ff));
149   // Clear out the J1 and J2 bits which may be set.
150   write16le(Off + 2, (read16le(Off + 2) & 0xd000) | (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
151 }
152 
153 void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS,
154                                uint64_t S, uint64_t P) const {
155   // Pointer to thumb code must have the LSB set.
156   uint64_t SX = S;
157   if (OS && (OS->getPermissions() & IMAGE_SCN_MEM_EXECUTE))
158     SX |= 1;
159   switch (Type) {
160   case IMAGE_REL_ARM_ADDR32:    add32(Off, SX + Config->ImageBase); break;
161   case IMAGE_REL_ARM_ADDR32NB:  add32(Off, SX); break;
162   case IMAGE_REL_ARM_MOV32T:    applyMOV32T(Off, SX + Config->ImageBase); break;
163   case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, SX - P - 4); break;
164   case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, SX - P - 4); break;
165   case IMAGE_REL_ARM_BLX23T:    applyBranch24T(Off, SX - P - 4); break;
166   case IMAGE_REL_ARM_SECTION:   applySecIdx(Off, OS); break;
167   case IMAGE_REL_ARM_SECREL:    applySecRel(this, Off, OS, S); break;
168   default:
169     fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
170   }
171 }
172 
173 // Interpret the existing immediate value as a byte offset to the
174 // target symbol, then update the instruction with the immediate as
175 // the page offset from the current instruction to the target.
176 static void applyArm64Addr(uint8_t *Off, uint64_t S, uint64_t P) {
177   uint32_t Orig = read32le(Off);
178   uint64_t Imm = ((Orig >> 29) & 0x3) | ((Orig >> 3) & 0x1FFFFC);
179   S += Imm;
180   Imm = (S >> 12) - (P >> 12);
181   uint32_t ImmLo = (Imm & 0x3) << 29;
182   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
183   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
184   write32le(Off, (Orig & ~Mask) | ImmLo | ImmHi);
185 }
186 
187 // Update the immediate field in a AARCH64 ldr, str, and add instruction.
188 // Optionally limit the range of the written immediate by one or more bits
189 // (RangeLimit).
190 static void applyArm64Imm(uint8_t *Off, uint64_t Imm, uint32_t RangeLimit) {
191   uint32_t Orig = read32le(Off);
192   Imm += (Orig >> 10) & 0xFFF;
193   Orig &= ~(0xFFF << 10);
194   write32le(Off, Orig | ((Imm & (0xFFF >> RangeLimit)) << 10));
195 }
196 
197 // Add the 12 bit page offset to the existing immediate.
198 // Ldr/str instructions store the opcode immediate scaled
199 // by the load/store size (giving a larger range for larger
200 // loads/stores). The immediate is always (both before and after
201 // fixing up the relocation) stored scaled similarly.
202 // Even if larger loads/stores have a larger range, limit the
203 // effective offset to 12 bit, since it is intended to be a
204 // page offset.
205 static void applyArm64Ldr(uint8_t *Off, uint64_t Imm) {
206   uint32_t Orig = read32le(Off);
207   uint32_t Size = Orig >> 30;
208   // 0x04000000 indicates SIMD/FP registers
209   // 0x00800000 indicates 128 bit
210   if ((Orig & 0x4800000) == 0x4800000)
211     Size += 4;
212   if ((Imm & ((1 << Size) - 1)) != 0)
213     fatal("misaligned ldr/str offset");
214   applyArm64Imm(Off, Imm >> Size, Size);
215 }
216 
217 void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS,
218                                  uint64_t S, uint64_t P) const {
219   switch (Type) {
220   case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(Off, S, P); break;
221   case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(Off, S & 0xfff, 0); break;
222   case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(Off, S & 0xfff); break;
223   case IMAGE_REL_ARM64_BRANCH26:       or32(Off, ((S - P) & 0x0FFFFFFC) >> 2); break;
224   case IMAGE_REL_ARM64_ADDR32:         add32(Off, S + Config->ImageBase); break;
225   case IMAGE_REL_ARM64_ADDR32NB:       add32(Off, S); break;
226   case IMAGE_REL_ARM64_ADDR64:         add64(Off, S + Config->ImageBase); break;
227   case IMAGE_REL_ARM64_SECREL:         applySecRel(this, Off, OS, S); break;
228   default:
229     fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
230   }
231 }
232 
233 void SectionChunk::writeTo(uint8_t *Buf) const {
234   if (!hasData())
235     return;
236   // Copy section contents from source object file to output file.
237   ArrayRef<uint8_t> A = getContents();
238   memcpy(Buf + OutputSectionOff, A.data(), A.size());
239 
240   // Apply relocations.
241   size_t InputSize = getSize();
242   for (const coff_relocation &Rel : Relocs) {
243     // Check for an invalid relocation offset. This check isn't perfect, because
244     // we don't have the relocation size, which is only known after checking the
245     // machine and relocation type. As a result, a relocation may overwrite the
246     // beginning of the following input section.
247     if (Rel.VirtualAddress >= InputSize)
248       fatal("relocation points beyond the end of its parent section");
249 
250     uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress;
251 
252     // Get the output section of the symbol for this relocation.  The output
253     // section is needed to compute SECREL and SECTION relocations used in debug
254     // info.
255     Defined *Sym = cast<Defined>(File->getSymbol(Rel.SymbolTableIndex));
256     Chunk *C = Sym->getChunk();
257     OutputSection *OS = C ? C->getOutputSection() : nullptr;
258 
259     // Only absolute and __ImageBase symbols lack an output section. For any
260     // other symbol, this indicates that the chunk was discarded.  Normally
261     // relocations against discarded sections are an error.  However, debug info
262     // sections are not GC roots and can end up with these kinds of relocations.
263     // Skip these relocations.
264     if (!OS && !isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym)) {
265       if (isCodeView() || isDWARF())
266         continue;
267       fatal("relocation against symbol in discarded section: " +
268             Sym->getName());
269     }
270     uint64_t S = Sym->getRVA();
271 
272     // Compute the RVA of the relocation for relative relocations.
273     uint64_t P = RVA + Rel.VirtualAddress;
274     switch (Config->Machine) {
275     case AMD64:
276       applyRelX64(Off, Rel.Type, OS, S, P);
277       break;
278     case I386:
279       applyRelX86(Off, Rel.Type, OS, S, P);
280       break;
281     case ARMNT:
282       applyRelARM(Off, Rel.Type, OS, S, P);
283       break;
284     case ARM64:
285       applyRelARM64(Off, Rel.Type, OS, S, P);
286       break;
287     default:
288       llvm_unreachable("unknown machine type");
289     }
290   }
291 }
292 
293 void SectionChunk::addAssociative(SectionChunk *Child) {
294   AssocChildren.push_back(Child);
295 }
296 
297 static uint8_t getBaserelType(const coff_relocation &Rel) {
298   switch (Config->Machine) {
299   case AMD64:
300     if (Rel.Type == IMAGE_REL_AMD64_ADDR64)
301       return IMAGE_REL_BASED_DIR64;
302     return IMAGE_REL_BASED_ABSOLUTE;
303   case I386:
304     if (Rel.Type == IMAGE_REL_I386_DIR32)
305       return IMAGE_REL_BASED_HIGHLOW;
306     return IMAGE_REL_BASED_ABSOLUTE;
307   case ARMNT:
308     if (Rel.Type == IMAGE_REL_ARM_ADDR32)
309       return IMAGE_REL_BASED_HIGHLOW;
310     if (Rel.Type == IMAGE_REL_ARM_MOV32T)
311       return IMAGE_REL_BASED_ARM_MOV32T;
312     return IMAGE_REL_BASED_ABSOLUTE;
313   case ARM64:
314     if (Rel.Type == IMAGE_REL_ARM64_ADDR64)
315       return IMAGE_REL_BASED_DIR64;
316     return IMAGE_REL_BASED_ABSOLUTE;
317   default:
318     llvm_unreachable("unknown machine type");
319   }
320 }
321 
322 // Windows-specific.
323 // Collect all locations that contain absolute addresses, which need to be
324 // fixed by the loader if load-time relocation is needed.
325 // Only called when base relocation is enabled.
326 void SectionChunk::getBaserels(std::vector<Baserel> *Res) {
327   for (const coff_relocation &Rel : Relocs) {
328     uint8_t Ty = getBaserelType(Rel);
329     if (Ty == IMAGE_REL_BASED_ABSOLUTE)
330       continue;
331     if (isa<DefinedAbsolute>(File->getSymbol(Rel.SymbolTableIndex)))
332       continue;
333     Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
334   }
335 }
336 
337 bool SectionChunk::hasData() const {
338   return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
339 }
340 
341 uint32_t SectionChunk::getPermissions() const {
342   return Header->Characteristics & PermMask;
343 }
344 
345 bool SectionChunk::isCOMDAT() const {
346   return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
347 }
348 
349 void SectionChunk::printDiscardedMessage() const {
350   // Removed by dead-stripping. If it's removed by ICF, ICF already
351   // printed out the name, so don't repeat that here.
352   if (Sym && this == Repl) {
353     if (Discarded)
354       message("Discarded comdat symbol " + Sym->getName());
355     else if (!Live)
356       message("Discarded " + Sym->getName());
357   }
358 }
359 
360 StringRef SectionChunk::getDebugName() {
361   if (Sym)
362     return Sym->getName();
363   return "";
364 }
365 
366 ArrayRef<uint8_t> SectionChunk::getContents() const {
367   ArrayRef<uint8_t> A;
368   File->getCOFFObj()->getSectionContents(Header, A);
369   return A;
370 }
371 
372 void SectionChunk::replace(SectionChunk *Other) {
373   Other->Repl = Repl;
374   Other->Live = false;
375 }
376 
377 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
378   // Common symbols are aligned on natural boundaries up to 32 bytes.
379   // This is what MSVC link.exe does.
380   Alignment = std::min(uint64_t(32), PowerOf2Ceil(Sym.getValue()));
381 }
382 
383 uint32_t CommonChunk::getPermissions() const {
384   return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
385          IMAGE_SCN_MEM_WRITE;
386 }
387 
388 void StringChunk::writeTo(uint8_t *Buf) const {
389   memcpy(Buf + OutputSectionOff, Str.data(), Str.size());
390 }
391 
392 ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
393   // Intel Optimization Manual says that all branch targets
394   // should be 16-byte aligned. MSVC linker does this too.
395   Alignment = 16;
396 }
397 
398 void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
399   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
400   // The first two bytes is a JMP instruction. Fill its operand.
401   write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize());
402 }
403 
404 void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
405   Res->emplace_back(getRVA() + 2);
406 }
407 
408 void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
409   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
410   // The first two bytes is a JMP instruction. Fill its operand.
411   write32le(Buf + OutputSectionOff + 2,
412             ImpSymbol->getRVA() + Config->ImageBase);
413 }
414 
415 void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
416   Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
417 }
418 
419 void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
420   memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM));
421   // Fix mov.w and mov.t operands.
422   applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase);
423 }
424 
425 void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const {
426   int64_t Off = ImpSymbol->getRVA() & 0xfff;
427   memcpy(Buf + OutputSectionOff, ImportThunkARM64, sizeof(ImportThunkARM64));
428   applyArm64Addr(Buf + OutputSectionOff, ImpSymbol->getRVA(), RVA);
429   applyArm64Ldr(Buf + OutputSectionOff + 4, Off);
430 }
431 
432 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
433   Res->emplace_back(getRVA());
434 }
435 
436 size_t LocalImportChunk::getSize() const {
437   return Config->is64() ? 8 : 4;
438 }
439 
440 void LocalImportChunk::writeTo(uint8_t *Buf) const {
441   if (Config->is64()) {
442     write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
443   } else {
444     write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
445   }
446 }
447 
448 void SEHTableChunk::writeTo(uint8_t *Buf) const {
449   ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
450   size_t Cnt = 0;
451   for (Defined *D : Syms)
452     Begin[Cnt++] = D->getRVA();
453   std::sort(Begin, Begin + Cnt);
454 }
455 
456 // Windows-specific. This class represents a block in .reloc section.
457 // The format is described here.
458 //
459 // On Windows, each DLL is linked against a fixed base address and
460 // usually loaded to that address. However, if there's already another
461 // DLL that overlaps, the loader has to relocate it. To do that, DLLs
462 // contain .reloc sections which contain offsets that need to be fixed
463 // up at runtime. If the loader finds that a DLL cannot be loaded to its
464 // desired base address, it loads it to somewhere else, and add <actual
465 // base address> - <desired base address> to each offset that is
466 // specified by the .reloc section. In ELF terms, .reloc sections
467 // contain relative relocations in REL format (as opposed to RELA.)
468 //
469 // This already significantly reduces the size of relocations compared
470 // to ELF .rel.dyn, but Windows does more to reduce it (probably because
471 // it was invented for PCs in the late '80s or early '90s.)  Offsets in
472 // .reloc are grouped by page where the page size is 12 bits, and
473 // offsets sharing the same page address are stored consecutively to
474 // represent them with less space. This is very similar to the page
475 // table which is grouped by (multiple stages of) pages.
476 //
477 // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
478 // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
479 // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
480 // are represented like this:
481 //
482 //   0x00000  -- page address (4 bytes)
483 //   16       -- size of this block (4 bytes)
484 //     0xA030 -- entries (2 bytes each)
485 //     0xA500
486 //     0xA700
487 //     0xAA00
488 //   0x20000  -- page address (4 bytes)
489 //   12       -- size of this block (4 bytes)
490 //     0xA004 -- entries (2 bytes each)
491 //     0xA008
492 //
493 // Usually we have a lot of relocations for each page, so the number of
494 // bytes for one .reloc entry is close to 2 bytes on average.
495 BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
496   // Block header consists of 4 byte page RVA and 4 byte block size.
497   // Each entry is 2 byte. Last entry may be padding.
498   Data.resize(alignTo((End - Begin) * 2 + 8, 4));
499   uint8_t *P = Data.data();
500   write32le(P, Page);
501   write32le(P + 4, Data.size());
502   P += 8;
503   for (Baserel *I = Begin; I != End; ++I) {
504     write16le(P, (I->Type << 12) | (I->RVA - Page));
505     P += 2;
506   }
507 }
508 
509 void BaserelChunk::writeTo(uint8_t *Buf) const {
510   memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
511 }
512 
513 uint8_t Baserel::getDefaultType() {
514   switch (Config->Machine) {
515   case AMD64:
516   case ARM64:
517     return IMAGE_REL_BASED_DIR64;
518   case I386:
519   case ARMNT:
520     return IMAGE_REL_BASED_HIGHLOW;
521   default:
522     llvm_unreachable("unknown machine type");
523   }
524 }
525 
526 } // namespace coff
527 } // namespace lld
528