xref: /llvm-project-15.0.7/lld/COFF/Chunks.cpp (revision ad38fbff)
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 "Error.h"
12 #include "InputFiles.h"
13 #include "Symbols.h"
14 #include "Writer.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   default:
228     fatal("unsupported relocation type 0x" + Twine::utohexstr(Type));
229   }
230 }
231 
232 void SectionChunk::writeTo(uint8_t *Buf) const {
233   if (!hasData())
234     return;
235   // Copy section contents from source object file to output file.
236   ArrayRef<uint8_t> A = getContents();
237   memcpy(Buf + OutputSectionOff, A.data(), A.size());
238 
239   // Apply relocations.
240   size_t InputSize = getSize();
241   for (const coff_relocation &Rel : Relocs) {
242     // Check for an invalid relocation offset. This check isn't perfect, because
243     // we don't have the relocation size, which is only known after checking the
244     // machine and relocation type. As a result, a relocation may overwrite the
245     // beginning of the following input section.
246     if (Rel.VirtualAddress >= InputSize)
247       fatal("relocation points beyond the end of its parent section");
248 
249     uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress;
250 
251     // Get the output section of the symbol for this relocation.  The output
252     // section is needed to compute SECREL and SECTION relocations used in debug
253     // info.
254     SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
255     Defined *Sym = cast<Defined>(Body);
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     SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
332     if (isa<DefinedAbsolute>(Body))
333       continue;
334     Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
335   }
336 }
337 
338 bool SectionChunk::hasData() const {
339   return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
340 }
341 
342 uint32_t SectionChunk::getPermissions() const {
343   return Header->Characteristics & PermMask;
344 }
345 
346 bool SectionChunk::isCOMDAT() const {
347   return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
348 }
349 
350 void SectionChunk::printDiscardedMessage() const {
351   // Removed by dead-stripping. If it's removed by ICF, ICF already
352   // printed out the name, so don't repeat that here.
353   if (Sym && this == Repl) {
354     if (Discarded)
355       message("Discarded comdat symbol " + Sym->getName());
356     else if (!Live)
357       message("Discarded " + Sym->getName());
358   }
359 }
360 
361 StringRef SectionChunk::getDebugName() {
362   if (Sym)
363     return Sym->getName();
364   return "";
365 }
366 
367 ArrayRef<uint8_t> SectionChunk::getContents() const {
368   ArrayRef<uint8_t> A;
369   File->getCOFFObj()->getSectionContents(Header, A);
370   return A;
371 }
372 
373 void SectionChunk::replace(SectionChunk *Other) {
374   Other->Repl = Repl;
375   Other->Live = false;
376 }
377 
378 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
379   // Common symbols are aligned on natural boundaries up to 32 bytes.
380   // This is what MSVC link.exe does.
381   Alignment = std::min(uint64_t(32), PowerOf2Ceil(Sym.getValue()));
382 }
383 
384 uint32_t CommonChunk::getPermissions() const {
385   return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
386          IMAGE_SCN_MEM_WRITE;
387 }
388 
389 void StringChunk::writeTo(uint8_t *Buf) const {
390   memcpy(Buf + OutputSectionOff, Str.data(), Str.size());
391 }
392 
393 ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
394   // Intel Optimization Manual says that all branch targets
395   // should be 16-byte aligned. MSVC linker does this too.
396   Alignment = 16;
397 }
398 
399 void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
400   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
401   // The first two bytes is a JMP instruction. Fill its operand.
402   write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize());
403 }
404 
405 void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
406   Res->emplace_back(getRVA() + 2);
407 }
408 
409 void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
410   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
411   // The first two bytes is a JMP instruction. Fill its operand.
412   write32le(Buf + OutputSectionOff + 2,
413             ImpSymbol->getRVA() + Config->ImageBase);
414 }
415 
416 void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
417   Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
418 }
419 
420 void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
421   memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM));
422   // Fix mov.w and mov.t operands.
423   applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase);
424 }
425 
426 void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const {
427   int64_t Off = ImpSymbol->getRVA() & 0xfff;
428   memcpy(Buf + OutputSectionOff, ImportThunkARM64, sizeof(ImportThunkARM64));
429   applyArm64Addr(Buf + OutputSectionOff, ImpSymbol->getRVA(), RVA);
430   applyArm64Ldr(Buf + OutputSectionOff + 4, Off);
431 }
432 
433 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
434   Res->emplace_back(getRVA());
435 }
436 
437 size_t LocalImportChunk::getSize() const {
438   return Config->is64() ? 8 : 4;
439 }
440 
441 void LocalImportChunk::writeTo(uint8_t *Buf) const {
442   if (Config->is64()) {
443     write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
444   } else {
445     write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
446   }
447 }
448 
449 void SEHTableChunk::writeTo(uint8_t *Buf) const {
450   ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
451   size_t Cnt = 0;
452   for (Defined *D : Syms)
453     Begin[Cnt++] = D->getRVA();
454   std::sort(Begin, Begin + Cnt);
455 }
456 
457 // Windows-specific. This class represents a block in .reloc section.
458 // The format is described here.
459 //
460 // On Windows, each DLL is linked against a fixed base address and
461 // usually loaded to that address. However, if there's already another
462 // DLL that overlaps, the loader has to relocate it. To do that, DLLs
463 // contain .reloc sections which contain offsets that need to be fixed
464 // up at runtime. If the loader finds that a DLL cannot be loaded to its
465 // desired base address, it loads it to somewhere else, and add <actual
466 // base address> - <desired base address> to each offset that is
467 // specified by the .reloc section. In ELF terms, .reloc sections
468 // contain relative relocations in REL format (as opposed to RELA.)
469 //
470 // This already significantly reduces the size of relocations compared
471 // to ELF .rel.dyn, but Windows does more to reduce it (probably because
472 // it was invented for PCs in the late '80s or early '90s.)  Offsets in
473 // .reloc are grouped by page where the page size is 12 bits, and
474 // offsets sharing the same page address are stored consecutively to
475 // represent them with less space. This is very similar to the page
476 // table which is grouped by (multiple stages of) pages.
477 //
478 // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
479 // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
480 // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
481 // are represented like this:
482 //
483 //   0x00000  -- page address (4 bytes)
484 //   16       -- size of this block (4 bytes)
485 //     0xA030 -- entries (2 bytes each)
486 //     0xA500
487 //     0xA700
488 //     0xAA00
489 //   0x20000  -- page address (4 bytes)
490 //   12       -- size of this block (4 bytes)
491 //     0xA004 -- entries (2 bytes each)
492 //     0xA008
493 //
494 // Usually we have a lot of relocations for each page, so the number of
495 // bytes for one .reloc entry is close to 2 bytes on average.
496 BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
497   // Block header consists of 4 byte page RVA and 4 byte block size.
498   // Each entry is 2 byte. Last entry may be padding.
499   Data.resize(alignTo((End - Begin) * 2 + 8, 4));
500   uint8_t *P = Data.data();
501   write32le(P, Page);
502   write32le(P + 4, Data.size());
503   P += 8;
504   for (Baserel *I = Begin; I != End; ++I) {
505     write16le(P, (I->Type << 12) | (I->RVA - Page));
506     P += 2;
507   }
508 }
509 
510 void BaserelChunk::writeTo(uint8_t *Buf) const {
511   memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
512 }
513 
514 uint8_t Baserel::getDefaultType() {
515   switch (Config->Machine) {
516   case AMD64:
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