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