xref: /openbsd-src/gnu/llvm/lld/COFF/Chunks.cpp (revision dfe94b169149f14cc1aee2cf6dad58a8d9a1860c)
1ece8a530Spatrick //===- Chunks.cpp ---------------------------------------------------------===//
2ece8a530Spatrick //
3ece8a530Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ece8a530Spatrick // See https://llvm.org/LICENSE.txt for license information.
5ece8a530Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ece8a530Spatrick //
7ece8a530Spatrick //===----------------------------------------------------------------------===//
8ece8a530Spatrick 
9ece8a530Spatrick #include "Chunks.h"
10*dfe94b16Srobert #include "COFFLinkerContext.h"
11ece8a530Spatrick #include "InputFiles.h"
12*dfe94b16Srobert #include "SymbolTable.h"
13ece8a530Spatrick #include "Symbols.h"
14ece8a530Spatrick #include "Writer.h"
15*dfe94b16Srobert #include "llvm/ADT/STLExtras.h"
16ece8a530Spatrick #include "llvm/ADT/Twine.h"
17ece8a530Spatrick #include "llvm/BinaryFormat/COFF.h"
18ece8a530Spatrick #include "llvm/Object/COFF.h"
19ece8a530Spatrick #include "llvm/Support/Debug.h"
20ece8a530Spatrick #include "llvm/Support/Endian.h"
21ece8a530Spatrick #include "llvm/Support/raw_ostream.h"
22ece8a530Spatrick #include <algorithm>
23*dfe94b16Srobert #include <iterator>
24ece8a530Spatrick 
25ece8a530Spatrick using namespace llvm;
26ece8a530Spatrick using namespace llvm::object;
27ece8a530Spatrick using namespace llvm::support::endian;
28ece8a530Spatrick using namespace llvm::COFF;
29ece8a530Spatrick using llvm::support::ulittle32_t;
30ece8a530Spatrick 
31*dfe94b16Srobert namespace lld::coff {
32ece8a530Spatrick 
SectionChunk(ObjFile * f,const coff_section * h)33ece8a530Spatrick SectionChunk::SectionChunk(ObjFile *f, const coff_section *h)
34ece8a530Spatrick     : Chunk(SectionKind), file(f), header(h), repl(this) {
35ece8a530Spatrick   // Initialize relocs.
361cf9926bSpatrick   if (file)
37ece8a530Spatrick     setRelocs(file->getCOFFObj()->getRelocations(header));
38ece8a530Spatrick 
39ece8a530Spatrick   // Initialize sectionName.
40ece8a530Spatrick   StringRef sectionName;
411cf9926bSpatrick   if (file) {
42ece8a530Spatrick     if (Expected<StringRef> e = file->getCOFFObj()->getSectionName(header))
43ece8a530Spatrick       sectionName = *e;
441cf9926bSpatrick   }
45ece8a530Spatrick   sectionNameData = sectionName.data();
46ece8a530Spatrick   sectionNameSize = sectionName.size();
47ece8a530Spatrick 
48ece8a530Spatrick   setAlignment(header->getAlignment());
49ece8a530Spatrick 
50ece8a530Spatrick   hasData = !(header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
51ece8a530Spatrick 
52ece8a530Spatrick   // If linker GC is disabled, every chunk starts out alive.  If linker GC is
53ece8a530Spatrick   // enabled, treat non-comdat sections as roots. Generally optimized object
54ece8a530Spatrick   // files will be built with -ffunction-sections or /Gy, so most things worth
55ece8a530Spatrick   // stripping will be in a comdat.
56*dfe94b16Srobert   if (file)
57*dfe94b16Srobert     live = !file->ctx.config.doGC || !isCOMDAT();
581cf9926bSpatrick   else
591cf9926bSpatrick     live = true;
60ece8a530Spatrick }
61ece8a530Spatrick 
62ece8a530Spatrick // SectionChunk is one of the most frequently allocated classes, so it is
63ece8a530Spatrick // important to keep it as compact as possible. As of this writing, the number
64ece8a530Spatrick // below is the size of this class on x64 platforms.
65ece8a530Spatrick static_assert(sizeof(SectionChunk) <= 88, "SectionChunk grew unexpectedly");
66ece8a530Spatrick 
add16(uint8_t * p,int16_t v)67ece8a530Spatrick static void add16(uint8_t *p, int16_t v) { write16le(p, read16le(p) + v); }
add32(uint8_t * p,int32_t v)68ece8a530Spatrick static void add32(uint8_t *p, int32_t v) { write32le(p, read32le(p) + v); }
add64(uint8_t * p,int64_t v)69ece8a530Spatrick static void add64(uint8_t *p, int64_t v) { write64le(p, read64le(p) + v); }
or16(uint8_t * p,uint16_t v)70ece8a530Spatrick static void or16(uint8_t *p, uint16_t v) { write16le(p, read16le(p) | v); }
or32(uint8_t * p,uint32_t v)71ece8a530Spatrick static void or32(uint8_t *p, uint32_t v) { write32le(p, read32le(p) | v); }
72ece8a530Spatrick 
73ece8a530Spatrick // Verify that given sections are appropriate targets for SECREL
74ece8a530Spatrick // relocations. This check is relaxed because unfortunately debug
75ece8a530Spatrick // sections have section-relative relocations against absolute symbols.
checkSecRel(const SectionChunk * sec,OutputSection * os)76ece8a530Spatrick static bool checkSecRel(const SectionChunk *sec, OutputSection *os) {
77ece8a530Spatrick   if (os)
78ece8a530Spatrick     return true;
79ece8a530Spatrick   if (sec->isCodeView())
80ece8a530Spatrick     return false;
81ece8a530Spatrick   error("SECREL relocation cannot be applied to absolute symbols");
82ece8a530Spatrick   return false;
83ece8a530Spatrick }
84ece8a530Spatrick 
applySecRel(const SectionChunk * sec,uint8_t * off,OutputSection * os,uint64_t s)85ece8a530Spatrick static void applySecRel(const SectionChunk *sec, uint8_t *off,
86ece8a530Spatrick                         OutputSection *os, uint64_t s) {
87ece8a530Spatrick   if (!checkSecRel(sec, os))
88ece8a530Spatrick     return;
89ece8a530Spatrick   uint64_t secRel = s - os->getRVA();
90ece8a530Spatrick   if (secRel > UINT32_MAX) {
91ece8a530Spatrick     error("overflow in SECREL relocation in section: " + sec->getSectionName());
92ece8a530Spatrick     return;
93ece8a530Spatrick   }
94ece8a530Spatrick   add32(off, secRel);
95ece8a530Spatrick }
96ece8a530Spatrick 
applySecIdx(uint8_t * off,OutputSection * os,unsigned numOutputSections)97*dfe94b16Srobert static void applySecIdx(uint8_t *off, OutputSection *os,
98*dfe94b16Srobert                         unsigned numOutputSections) {
99*dfe94b16Srobert   // numOutputSections is the largest valid section index. Make sure that
100*dfe94b16Srobert   // it fits in 16 bits.
101*dfe94b16Srobert   assert(numOutputSections <= 0xffff && "size of outputSections is too big");
102*dfe94b16Srobert 
103ece8a530Spatrick   // Absolute symbol doesn't have section index, but section index relocation
104ece8a530Spatrick   // against absolute symbol should be resolved to one plus the last output
105ece8a530Spatrick   // section index. This is required for compatibility with MSVC.
106ece8a530Spatrick   if (os)
107ece8a530Spatrick     add16(off, os->sectionIndex);
108ece8a530Spatrick   else
109*dfe94b16Srobert     add16(off, numOutputSections + 1);
110ece8a530Spatrick }
111ece8a530Spatrick 
applyRelX64(uint8_t * off,uint16_t type,OutputSection * os,uint64_t s,uint64_t p,uint64_t imageBase) const112ece8a530Spatrick void SectionChunk::applyRelX64(uint8_t *off, uint16_t type, OutputSection *os,
113*dfe94b16Srobert                                uint64_t s, uint64_t p,
114*dfe94b16Srobert                                uint64_t imageBase) const {
115ece8a530Spatrick   switch (type) {
116*dfe94b16Srobert   case IMAGE_REL_AMD64_ADDR32:
117*dfe94b16Srobert     add32(off, s + imageBase);
118*dfe94b16Srobert     break;
119*dfe94b16Srobert   case IMAGE_REL_AMD64_ADDR64:
120*dfe94b16Srobert     add64(off, s + imageBase);
121*dfe94b16Srobert     break;
122ece8a530Spatrick   case IMAGE_REL_AMD64_ADDR32NB: add32(off, s); break;
123ece8a530Spatrick   case IMAGE_REL_AMD64_REL32:    add32(off, s - p - 4); break;
124ece8a530Spatrick   case IMAGE_REL_AMD64_REL32_1:  add32(off, s - p - 5); break;
125ece8a530Spatrick   case IMAGE_REL_AMD64_REL32_2:  add32(off, s - p - 6); break;
126ece8a530Spatrick   case IMAGE_REL_AMD64_REL32_3:  add32(off, s - p - 7); break;
127ece8a530Spatrick   case IMAGE_REL_AMD64_REL32_4:  add32(off, s - p - 8); break;
128ece8a530Spatrick   case IMAGE_REL_AMD64_REL32_5:  add32(off, s - p - 9); break;
129*dfe94b16Srobert   case IMAGE_REL_AMD64_SECTION:
130*dfe94b16Srobert     applySecIdx(off, os, file->ctx.outputSections.size());
131*dfe94b16Srobert     break;
132ece8a530Spatrick   case IMAGE_REL_AMD64_SECREL:   applySecRel(this, off, os, s); break;
133ece8a530Spatrick   default:
134ece8a530Spatrick     error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
135ece8a530Spatrick           toString(file));
136ece8a530Spatrick   }
137ece8a530Spatrick }
138ece8a530Spatrick 
applyRelX86(uint8_t * off,uint16_t type,OutputSection * os,uint64_t s,uint64_t p,uint64_t imageBase) const139ece8a530Spatrick void SectionChunk::applyRelX86(uint8_t *off, uint16_t type, OutputSection *os,
140*dfe94b16Srobert                                uint64_t s, uint64_t p,
141*dfe94b16Srobert                                uint64_t imageBase) const {
142ece8a530Spatrick   switch (type) {
143ece8a530Spatrick   case IMAGE_REL_I386_ABSOLUTE: break;
144*dfe94b16Srobert   case IMAGE_REL_I386_DIR32:
145*dfe94b16Srobert     add32(off, s + imageBase);
146*dfe94b16Srobert     break;
147ece8a530Spatrick   case IMAGE_REL_I386_DIR32NB:  add32(off, s); break;
148ece8a530Spatrick   case IMAGE_REL_I386_REL32:    add32(off, s - p - 4); break;
149*dfe94b16Srobert   case IMAGE_REL_I386_SECTION:
150*dfe94b16Srobert     applySecIdx(off, os, file->ctx.outputSections.size());
151*dfe94b16Srobert     break;
152ece8a530Spatrick   case IMAGE_REL_I386_SECREL:   applySecRel(this, off, os, s); break;
153ece8a530Spatrick   default:
154ece8a530Spatrick     error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
155ece8a530Spatrick           toString(file));
156ece8a530Spatrick   }
157ece8a530Spatrick }
158ece8a530Spatrick 
applyMOV(uint8_t * off,uint16_t v)159ece8a530Spatrick static void applyMOV(uint8_t *off, uint16_t v) {
160ece8a530Spatrick   write16le(off, (read16le(off) & 0xfbf0) | ((v & 0x800) >> 1) | ((v >> 12) & 0xf));
161ece8a530Spatrick   write16le(off + 2, (read16le(off + 2) & 0x8f00) | ((v & 0x700) << 4) | (v & 0xff));
162ece8a530Spatrick }
163ece8a530Spatrick 
readMOV(uint8_t * off,bool movt)164ece8a530Spatrick static uint16_t readMOV(uint8_t *off, bool movt) {
165ece8a530Spatrick   uint16_t op1 = read16le(off);
166ece8a530Spatrick   if ((op1 & 0xfbf0) != (movt ? 0xf2c0 : 0xf240))
167ece8a530Spatrick     error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") +
168ece8a530Spatrick           " instruction in MOV32T relocation");
169ece8a530Spatrick   uint16_t op2 = read16le(off + 2);
170ece8a530Spatrick   if ((op2 & 0x8000) != 0)
171ece8a530Spatrick     error("unexpected instruction in " + Twine(movt ? "MOVT" : "MOVW") +
172ece8a530Spatrick           " instruction in MOV32T relocation");
173ece8a530Spatrick   return (op2 & 0x00ff) | ((op2 >> 4) & 0x0700) | ((op1 << 1) & 0x0800) |
174ece8a530Spatrick          ((op1 & 0x000f) << 12);
175ece8a530Spatrick }
176ece8a530Spatrick 
applyMOV32T(uint8_t * off,uint32_t v)177ece8a530Spatrick void applyMOV32T(uint8_t *off, uint32_t v) {
178ece8a530Spatrick   uint16_t immW = readMOV(off, false);    // read MOVW operand
179ece8a530Spatrick   uint16_t immT = readMOV(off + 4, true); // read MOVT operand
180ece8a530Spatrick   uint32_t imm = immW | (immT << 16);
181ece8a530Spatrick   v += imm;                         // add the immediate offset
182ece8a530Spatrick   applyMOV(off, v);           // set MOVW operand
183ece8a530Spatrick   applyMOV(off + 4, v >> 16); // set MOVT operand
184ece8a530Spatrick }
185ece8a530Spatrick 
applyBranch20T(uint8_t * off,int32_t v)186ece8a530Spatrick static void applyBranch20T(uint8_t *off, int32_t v) {
187ece8a530Spatrick   if (!isInt<21>(v))
188ece8a530Spatrick     error("relocation out of range");
189ece8a530Spatrick   uint32_t s = v < 0 ? 1 : 0;
190ece8a530Spatrick   uint32_t j1 = (v >> 19) & 1;
191ece8a530Spatrick   uint32_t j2 = (v >> 18) & 1;
192ece8a530Spatrick   or16(off, (s << 10) | ((v >> 12) & 0x3f));
193ece8a530Spatrick   or16(off + 2, (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff));
194ece8a530Spatrick }
195ece8a530Spatrick 
applyBranch24T(uint8_t * off,int32_t v)196ece8a530Spatrick void applyBranch24T(uint8_t *off, int32_t v) {
197ece8a530Spatrick   if (!isInt<25>(v))
198ece8a530Spatrick     error("relocation out of range");
199ece8a530Spatrick   uint32_t s = v < 0 ? 1 : 0;
200ece8a530Spatrick   uint32_t j1 = ((~v >> 23) & 1) ^ s;
201ece8a530Spatrick   uint32_t j2 = ((~v >> 22) & 1) ^ s;
202ece8a530Spatrick   or16(off, (s << 10) | ((v >> 12) & 0x3ff));
203ece8a530Spatrick   // Clear out the J1 and J2 bits which may be set.
204ece8a530Spatrick   write16le(off + 2, (read16le(off + 2) & 0xd000) | (j1 << 13) | (j2 << 11) | ((v >> 1) & 0x7ff));
205ece8a530Spatrick }
206ece8a530Spatrick 
applyRelARM(uint8_t * off,uint16_t type,OutputSection * os,uint64_t s,uint64_t p,uint64_t imageBase) const207ece8a530Spatrick void SectionChunk::applyRelARM(uint8_t *off, uint16_t type, OutputSection *os,
208*dfe94b16Srobert                                uint64_t s, uint64_t p,
209*dfe94b16Srobert                                uint64_t imageBase) const {
210ece8a530Spatrick   // Pointer to thumb code must have the LSB set.
211ece8a530Spatrick   uint64_t sx = s;
212ece8a530Spatrick   if (os && (os->header.Characteristics & IMAGE_SCN_MEM_EXECUTE))
213ece8a530Spatrick     sx |= 1;
214ece8a530Spatrick   switch (type) {
215*dfe94b16Srobert   case IMAGE_REL_ARM_ADDR32:
216*dfe94b16Srobert     add32(off, sx + imageBase);
217*dfe94b16Srobert     break;
218ece8a530Spatrick   case IMAGE_REL_ARM_ADDR32NB:  add32(off, sx); break;
219*dfe94b16Srobert   case IMAGE_REL_ARM_MOV32T:
220*dfe94b16Srobert     applyMOV32T(off, sx + imageBase);
221*dfe94b16Srobert     break;
222ece8a530Spatrick   case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(off, sx - p - 4); break;
223ece8a530Spatrick   case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(off, sx - p - 4); break;
224ece8a530Spatrick   case IMAGE_REL_ARM_BLX23T:    applyBranch24T(off, sx - p - 4); break;
225*dfe94b16Srobert   case IMAGE_REL_ARM_SECTION:
226*dfe94b16Srobert     applySecIdx(off, os, file->ctx.outputSections.size());
227*dfe94b16Srobert     break;
228ece8a530Spatrick   case IMAGE_REL_ARM_SECREL:    applySecRel(this, off, os, s); break;
229ece8a530Spatrick   case IMAGE_REL_ARM_REL32:     add32(off, sx - p - 4); break;
230ece8a530Spatrick   default:
231ece8a530Spatrick     error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
232ece8a530Spatrick           toString(file));
233ece8a530Spatrick   }
234ece8a530Spatrick }
235ece8a530Spatrick 
236ece8a530Spatrick // Interpret the existing immediate value as a byte offset to the
237ece8a530Spatrick // target symbol, then update the instruction with the immediate as
238ece8a530Spatrick // the page offset from the current instruction to the target.
applyArm64Addr(uint8_t * off,uint64_t s,uint64_t p,int shift)239ece8a530Spatrick void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift) {
240ece8a530Spatrick   uint32_t orig = read32le(off);
241*dfe94b16Srobert   int64_t imm =
242*dfe94b16Srobert       SignExtend64<21>(((orig >> 29) & 0x3) | ((orig >> 3) & 0x1FFFFC));
243ece8a530Spatrick   s += imm;
244ece8a530Spatrick   imm = (s >> shift) - (p >> shift);
245ece8a530Spatrick   uint32_t immLo = (imm & 0x3) << 29;
246ece8a530Spatrick   uint32_t immHi = (imm & 0x1FFFFC) << 3;
247ece8a530Spatrick   uint64_t mask = (0x3 << 29) | (0x1FFFFC << 3);
248ece8a530Spatrick   write32le(off, (orig & ~mask) | immLo | immHi);
249ece8a530Spatrick }
250ece8a530Spatrick 
251ece8a530Spatrick // Update the immediate field in a AARCH64 ldr, str, and add instruction.
252ece8a530Spatrick // Optionally limit the range of the written immediate by one or more bits
253ece8a530Spatrick // (rangeLimit).
applyArm64Imm(uint8_t * off,uint64_t imm,uint32_t rangeLimit)254ece8a530Spatrick void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit) {
255ece8a530Spatrick   uint32_t orig = read32le(off);
256ece8a530Spatrick   imm += (orig >> 10) & 0xFFF;
257ece8a530Spatrick   orig &= ~(0xFFF << 10);
258ece8a530Spatrick   write32le(off, orig | ((imm & (0xFFF >> rangeLimit)) << 10));
259ece8a530Spatrick }
260ece8a530Spatrick 
261ece8a530Spatrick // Add the 12 bit page offset to the existing immediate.
262ece8a530Spatrick // Ldr/str instructions store the opcode immediate scaled
263ece8a530Spatrick // by the load/store size (giving a larger range for larger
264ece8a530Spatrick // loads/stores). The immediate is always (both before and after
265ece8a530Spatrick // fixing up the relocation) stored scaled similarly.
266ece8a530Spatrick // Even if larger loads/stores have a larger range, limit the
267ece8a530Spatrick // effective offset to 12 bit, since it is intended to be a
268ece8a530Spatrick // page offset.
applyArm64Ldr(uint8_t * off,uint64_t imm)269ece8a530Spatrick static void applyArm64Ldr(uint8_t *off, uint64_t imm) {
270ece8a530Spatrick   uint32_t orig = read32le(off);
271ece8a530Spatrick   uint32_t size = orig >> 30;
272ece8a530Spatrick   // 0x04000000 indicates SIMD/FP registers
273ece8a530Spatrick   // 0x00800000 indicates 128 bit
274ece8a530Spatrick   if ((orig & 0x4800000) == 0x4800000)
275ece8a530Spatrick     size += 4;
276ece8a530Spatrick   if ((imm & ((1 << size) - 1)) != 0)
277ece8a530Spatrick     error("misaligned ldr/str offset");
278ece8a530Spatrick   applyArm64Imm(off, imm >> size, size);
279ece8a530Spatrick }
280ece8a530Spatrick 
applySecRelLow12A(const SectionChunk * sec,uint8_t * off,OutputSection * os,uint64_t s)281ece8a530Spatrick static void applySecRelLow12A(const SectionChunk *sec, uint8_t *off,
282ece8a530Spatrick                               OutputSection *os, uint64_t s) {
283ece8a530Spatrick   if (checkSecRel(sec, os))
284ece8a530Spatrick     applyArm64Imm(off, (s - os->getRVA()) & 0xfff, 0);
285ece8a530Spatrick }
286ece8a530Spatrick 
applySecRelHigh12A(const SectionChunk * sec,uint8_t * off,OutputSection * os,uint64_t s)287ece8a530Spatrick static void applySecRelHigh12A(const SectionChunk *sec, uint8_t *off,
288ece8a530Spatrick                                OutputSection *os, uint64_t s) {
289ece8a530Spatrick   if (!checkSecRel(sec, os))
290ece8a530Spatrick     return;
291ece8a530Spatrick   uint64_t secRel = (s - os->getRVA()) >> 12;
292ece8a530Spatrick   if (0xfff < secRel) {
293ece8a530Spatrick     error("overflow in SECREL_HIGH12A relocation in section: " +
294ece8a530Spatrick           sec->getSectionName());
295ece8a530Spatrick     return;
296ece8a530Spatrick   }
297ece8a530Spatrick   applyArm64Imm(off, secRel & 0xfff, 0);
298ece8a530Spatrick }
299ece8a530Spatrick 
applySecRelLdr(const SectionChunk * sec,uint8_t * off,OutputSection * os,uint64_t s)300ece8a530Spatrick static void applySecRelLdr(const SectionChunk *sec, uint8_t *off,
301ece8a530Spatrick                            OutputSection *os, uint64_t s) {
302ece8a530Spatrick   if (checkSecRel(sec, os))
303ece8a530Spatrick     applyArm64Ldr(off, (s - os->getRVA()) & 0xfff);
304ece8a530Spatrick }
305ece8a530Spatrick 
applyArm64Branch26(uint8_t * off,int64_t v)306ece8a530Spatrick void applyArm64Branch26(uint8_t *off, int64_t v) {
307ece8a530Spatrick   if (!isInt<28>(v))
308ece8a530Spatrick     error("relocation out of range");
309ece8a530Spatrick   or32(off, (v & 0x0FFFFFFC) >> 2);
310ece8a530Spatrick }
311ece8a530Spatrick 
applyArm64Branch19(uint8_t * off,int64_t v)312ece8a530Spatrick static void applyArm64Branch19(uint8_t *off, int64_t v) {
313ece8a530Spatrick   if (!isInt<21>(v))
314ece8a530Spatrick     error("relocation out of range");
315ece8a530Spatrick   or32(off, (v & 0x001FFFFC) << 3);
316ece8a530Spatrick }
317ece8a530Spatrick 
applyArm64Branch14(uint8_t * off,int64_t v)318ece8a530Spatrick static void applyArm64Branch14(uint8_t *off, int64_t v) {
319ece8a530Spatrick   if (!isInt<16>(v))
320ece8a530Spatrick     error("relocation out of range");
321ece8a530Spatrick   or32(off, (v & 0x0000FFFC) << 3);
322ece8a530Spatrick }
323ece8a530Spatrick 
applyRelARM64(uint8_t * off,uint16_t type,OutputSection * os,uint64_t s,uint64_t p,uint64_t imageBase) const324ece8a530Spatrick void SectionChunk::applyRelARM64(uint8_t *off, uint16_t type, OutputSection *os,
325*dfe94b16Srobert                                  uint64_t s, uint64_t p,
326*dfe94b16Srobert                                  uint64_t imageBase) const {
327ece8a530Spatrick   switch (type) {
328ece8a530Spatrick   case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(off, s, p, 12); break;
329ece8a530Spatrick   case IMAGE_REL_ARM64_REL21:          applyArm64Addr(off, s, p, 0); break;
330ece8a530Spatrick   case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(off, s & 0xfff, 0); break;
331ece8a530Spatrick   case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(off, s & 0xfff); break;
332ece8a530Spatrick   case IMAGE_REL_ARM64_BRANCH26:       applyArm64Branch26(off, s - p); break;
333ece8a530Spatrick   case IMAGE_REL_ARM64_BRANCH19:       applyArm64Branch19(off, s - p); break;
334ece8a530Spatrick   case IMAGE_REL_ARM64_BRANCH14:       applyArm64Branch14(off, s - p); break;
335*dfe94b16Srobert   case IMAGE_REL_ARM64_ADDR32:
336*dfe94b16Srobert     add32(off, s + imageBase);
337*dfe94b16Srobert     break;
338ece8a530Spatrick   case IMAGE_REL_ARM64_ADDR32NB:       add32(off, s); break;
339*dfe94b16Srobert   case IMAGE_REL_ARM64_ADDR64:
340*dfe94b16Srobert     add64(off, s + imageBase);
341*dfe94b16Srobert     break;
342ece8a530Spatrick   case IMAGE_REL_ARM64_SECREL:         applySecRel(this, off, os, s); break;
343ece8a530Spatrick   case IMAGE_REL_ARM64_SECREL_LOW12A:  applySecRelLow12A(this, off, os, s); break;
344ece8a530Spatrick   case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, off, os, s); break;
345ece8a530Spatrick   case IMAGE_REL_ARM64_SECREL_LOW12L:  applySecRelLdr(this, off, os, s); break;
346*dfe94b16Srobert   case IMAGE_REL_ARM64_SECTION:
347*dfe94b16Srobert     applySecIdx(off, os, file->ctx.outputSections.size());
348*dfe94b16Srobert     break;
349ece8a530Spatrick   case IMAGE_REL_ARM64_REL32:          add32(off, s - p - 4); break;
350ece8a530Spatrick   default:
351ece8a530Spatrick     error("unsupported relocation type 0x" + Twine::utohexstr(type) + " in " +
352ece8a530Spatrick           toString(file));
353ece8a530Spatrick   }
354ece8a530Spatrick }
355ece8a530Spatrick 
maybeReportRelocationToDiscarded(const SectionChunk * fromChunk,Defined * sym,const coff_relocation & rel,bool isMinGW)356ece8a530Spatrick static void maybeReportRelocationToDiscarded(const SectionChunk *fromChunk,
357ece8a530Spatrick                                              Defined *sym,
358*dfe94b16Srobert                                              const coff_relocation &rel,
359*dfe94b16Srobert                                              bool isMinGW) {
360ece8a530Spatrick   // Don't report these errors when the relocation comes from a debug info
361ece8a530Spatrick   // section or in mingw mode. MinGW mode object files (built by GCC) can
362ece8a530Spatrick   // have leftover sections with relocations against discarded comdat
363ece8a530Spatrick   // sections. Such sections are left as is, with relocations untouched.
364*dfe94b16Srobert   if (fromChunk->isCodeView() || fromChunk->isDWARF() || isMinGW)
365ece8a530Spatrick     return;
366ece8a530Spatrick 
367ece8a530Spatrick   // Get the name of the symbol. If it's null, it was discarded early, so we
368ece8a530Spatrick   // have to go back to the object file.
369ece8a530Spatrick   ObjFile *file = fromChunk->file;
370ece8a530Spatrick   StringRef name;
371ece8a530Spatrick   if (sym) {
372ece8a530Spatrick     name = sym->getName();
373ece8a530Spatrick   } else {
374ece8a530Spatrick     COFFSymbolRef coffSym =
375ece8a530Spatrick         check(file->getCOFFObj()->getSymbol(rel.SymbolTableIndex));
376bb684c34Spatrick     name = check(file->getCOFFObj()->getSymbolName(coffSym));
377ece8a530Spatrick   }
378ece8a530Spatrick 
379ece8a530Spatrick   std::vector<std::string> symbolLocations =
380ece8a530Spatrick       getSymbolLocations(file, rel.SymbolTableIndex);
381ece8a530Spatrick 
382ece8a530Spatrick   std::string out;
383ece8a530Spatrick   llvm::raw_string_ostream os(out);
384ece8a530Spatrick   os << "relocation against symbol in discarded section: " + name;
385ece8a530Spatrick   for (const std::string &s : symbolLocations)
386ece8a530Spatrick     os << s;
387ece8a530Spatrick   error(os.str());
388ece8a530Spatrick }
389ece8a530Spatrick 
writeTo(uint8_t * buf) const390ece8a530Spatrick void SectionChunk::writeTo(uint8_t *buf) const {
391ece8a530Spatrick   if (!hasData)
392ece8a530Spatrick     return;
393ece8a530Spatrick   // Copy section contents from source object file to output file.
394ece8a530Spatrick   ArrayRef<uint8_t> a = getContents();
395ece8a530Spatrick   if (!a.empty())
396ece8a530Spatrick     memcpy(buf, a.data(), a.size());
397ece8a530Spatrick 
398ece8a530Spatrick   // Apply relocations.
399ece8a530Spatrick   size_t inputSize = getSize();
4001cf9926bSpatrick   for (const coff_relocation &rel : getRelocs()) {
401ece8a530Spatrick     // Check for an invalid relocation offset. This check isn't perfect, because
402ece8a530Spatrick     // we don't have the relocation size, which is only known after checking the
403ece8a530Spatrick     // machine and relocation type. As a result, a relocation may overwrite the
404ece8a530Spatrick     // beginning of the following input section.
405ece8a530Spatrick     if (rel.VirtualAddress >= inputSize) {
406ece8a530Spatrick       error("relocation points beyond the end of its parent section");
407ece8a530Spatrick       continue;
408ece8a530Spatrick     }
409ece8a530Spatrick 
4101cf9926bSpatrick     applyRelocation(buf + rel.VirtualAddress, rel);
4111cf9926bSpatrick   }
4121cf9926bSpatrick }
413ece8a530Spatrick 
applyRelocation(uint8_t * off,const coff_relocation & rel) const4141cf9926bSpatrick void SectionChunk::applyRelocation(uint8_t *off,
4151cf9926bSpatrick                                    const coff_relocation &rel) const {
4161cf9926bSpatrick   auto *sym = dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex));
417ece8a530Spatrick 
418ece8a530Spatrick   // Get the output section of the symbol for this relocation.  The output
419ece8a530Spatrick   // section is needed to compute SECREL and SECTION relocations used in debug
420ece8a530Spatrick   // info.
421ece8a530Spatrick   Chunk *c = sym ? sym->getChunk() : nullptr;
422*dfe94b16Srobert   OutputSection *os = c ? file->ctx.getOutputSection(c) : nullptr;
423ece8a530Spatrick 
424ece8a530Spatrick   // Skip the relocation if it refers to a discarded section, and diagnose it
425ece8a530Spatrick   // as an error if appropriate. If a symbol was discarded early, it may be
426ece8a530Spatrick   // null. If it was discarded late, the output section will be null, unless
427ece8a530Spatrick   // it was an absolute or synthetic symbol.
428ece8a530Spatrick   if (!sym ||
429ece8a530Spatrick       (!os && !isa<DefinedAbsolute>(sym) && !isa<DefinedSynthetic>(sym))) {
430*dfe94b16Srobert     maybeReportRelocationToDiscarded(this, sym, rel, file->ctx.config.mingw);
4311cf9926bSpatrick     return;
432ece8a530Spatrick   }
433ece8a530Spatrick 
434ece8a530Spatrick   uint64_t s = sym->getRVA();
435ece8a530Spatrick 
436ece8a530Spatrick   // Compute the RVA of the relocation for relative relocations.
437ece8a530Spatrick   uint64_t p = rva + rel.VirtualAddress;
438*dfe94b16Srobert   uint64_t imageBase = file->ctx.config.imageBase;
439*dfe94b16Srobert   switch (file->ctx.config.machine) {
440ece8a530Spatrick   case AMD64:
441*dfe94b16Srobert     applyRelX64(off, rel.Type, os, s, p, imageBase);
442ece8a530Spatrick     break;
443ece8a530Spatrick   case I386:
444*dfe94b16Srobert     applyRelX86(off, rel.Type, os, s, p, imageBase);
445ece8a530Spatrick     break;
446ece8a530Spatrick   case ARMNT:
447*dfe94b16Srobert     applyRelARM(off, rel.Type, os, s, p, imageBase);
448ece8a530Spatrick     break;
449ece8a530Spatrick   case ARM64:
450*dfe94b16Srobert     applyRelARM64(off, rel.Type, os, s, p, imageBase);
451ece8a530Spatrick     break;
452ece8a530Spatrick   default:
453ece8a530Spatrick     llvm_unreachable("unknown machine type");
454ece8a530Spatrick   }
455ece8a530Spatrick }
4561cf9926bSpatrick 
4571cf9926bSpatrick // Defend against unsorted relocations. This may be overly conservative.
sortRelocations()4581cf9926bSpatrick void SectionChunk::sortRelocations() {
4591cf9926bSpatrick   auto cmpByVa = [](const coff_relocation &l, const coff_relocation &r) {
4601cf9926bSpatrick     return l.VirtualAddress < r.VirtualAddress;
4611cf9926bSpatrick   };
4621cf9926bSpatrick   if (llvm::is_sorted(getRelocs(), cmpByVa))
4631cf9926bSpatrick     return;
4641cf9926bSpatrick   warn("some relocations in " + file->getName() + " are not sorted");
4651cf9926bSpatrick   MutableArrayRef<coff_relocation> newRelocs(
466*dfe94b16Srobert       bAlloc().Allocate<coff_relocation>(relocsSize), relocsSize);
4671cf9926bSpatrick   memcpy(newRelocs.data(), relocsData, relocsSize * sizeof(coff_relocation));
4681cf9926bSpatrick   llvm::sort(newRelocs, cmpByVa);
4691cf9926bSpatrick   setRelocs(newRelocs);
4701cf9926bSpatrick }
4711cf9926bSpatrick 
4721cf9926bSpatrick // Similar to writeTo, but suitable for relocating a subsection of the overall
4731cf9926bSpatrick // section.
writeAndRelocateSubsection(ArrayRef<uint8_t> sec,ArrayRef<uint8_t> subsec,uint32_t & nextRelocIndex,uint8_t * buf) const4741cf9926bSpatrick void SectionChunk::writeAndRelocateSubsection(ArrayRef<uint8_t> sec,
4751cf9926bSpatrick                                               ArrayRef<uint8_t> subsec,
4761cf9926bSpatrick                                               uint32_t &nextRelocIndex,
4771cf9926bSpatrick                                               uint8_t *buf) const {
4781cf9926bSpatrick   assert(!subsec.empty() && !sec.empty());
4791cf9926bSpatrick   assert(sec.begin() <= subsec.begin() && subsec.end() <= sec.end() &&
4801cf9926bSpatrick          "subsection is not part of this section");
4811cf9926bSpatrick   size_t vaBegin = std::distance(sec.begin(), subsec.begin());
4821cf9926bSpatrick   size_t vaEnd = std::distance(sec.begin(), subsec.end());
4831cf9926bSpatrick   memcpy(buf, subsec.data(), subsec.size());
4841cf9926bSpatrick   for (; nextRelocIndex < relocsSize; ++nextRelocIndex) {
4851cf9926bSpatrick     const coff_relocation &rel = relocsData[nextRelocIndex];
4861cf9926bSpatrick     // Only apply relocations that apply to this subsection. These checks
4871cf9926bSpatrick     // assume that all subsections completely contain their relocations.
4881cf9926bSpatrick     // Relocations must not straddle the beginning or end of a subsection.
4891cf9926bSpatrick     if (rel.VirtualAddress < vaBegin)
4901cf9926bSpatrick       continue;
4911cf9926bSpatrick     if (rel.VirtualAddress + 1 >= vaEnd)
4921cf9926bSpatrick       break;
4931cf9926bSpatrick     applyRelocation(&buf[rel.VirtualAddress - vaBegin], rel);
4941cf9926bSpatrick   }
495ece8a530Spatrick }
496ece8a530Spatrick 
addAssociative(SectionChunk * child)497ece8a530Spatrick void SectionChunk::addAssociative(SectionChunk *child) {
4981cf9926bSpatrick   // Insert the child section into the list of associated children. Keep the
4991cf9926bSpatrick   // list ordered by section name so that ICF does not depend on section order.
500ece8a530Spatrick   assert(child->assocChildren == nullptr &&
501ece8a530Spatrick          "associated sections cannot have their own associated children");
5021cf9926bSpatrick   SectionChunk *prev = this;
5031cf9926bSpatrick   SectionChunk *next = assocChildren;
5041cf9926bSpatrick   for (; next != nullptr; prev = next, next = next->assocChildren) {
5051cf9926bSpatrick     if (next->getSectionName() <= child->getSectionName())
5061cf9926bSpatrick       break;
5071cf9926bSpatrick   }
5081cf9926bSpatrick 
5091cf9926bSpatrick   // Insert child between prev and next.
5101cf9926bSpatrick   assert(prev->assocChildren == next);
5111cf9926bSpatrick   prev->assocChildren = child;
5121cf9926bSpatrick   child->assocChildren = next;
513ece8a530Spatrick }
514ece8a530Spatrick 
getBaserelType(const coff_relocation & rel,llvm::COFF::MachineTypes machine)515*dfe94b16Srobert static uint8_t getBaserelType(const coff_relocation &rel,
516*dfe94b16Srobert                               llvm::COFF::MachineTypes machine) {
517*dfe94b16Srobert   switch (machine) {
518ece8a530Spatrick   case AMD64:
519ece8a530Spatrick     if (rel.Type == IMAGE_REL_AMD64_ADDR64)
520ece8a530Spatrick       return IMAGE_REL_BASED_DIR64;
5211cf9926bSpatrick     if (rel.Type == IMAGE_REL_AMD64_ADDR32)
5221cf9926bSpatrick       return IMAGE_REL_BASED_HIGHLOW;
523ece8a530Spatrick     return IMAGE_REL_BASED_ABSOLUTE;
524ece8a530Spatrick   case I386:
525ece8a530Spatrick     if (rel.Type == IMAGE_REL_I386_DIR32)
526ece8a530Spatrick       return IMAGE_REL_BASED_HIGHLOW;
527ece8a530Spatrick     return IMAGE_REL_BASED_ABSOLUTE;
528ece8a530Spatrick   case ARMNT:
529ece8a530Spatrick     if (rel.Type == IMAGE_REL_ARM_ADDR32)
530ece8a530Spatrick       return IMAGE_REL_BASED_HIGHLOW;
531ece8a530Spatrick     if (rel.Type == IMAGE_REL_ARM_MOV32T)
532ece8a530Spatrick       return IMAGE_REL_BASED_ARM_MOV32T;
533ece8a530Spatrick     return IMAGE_REL_BASED_ABSOLUTE;
534ece8a530Spatrick   case ARM64:
535ece8a530Spatrick     if (rel.Type == IMAGE_REL_ARM64_ADDR64)
536ece8a530Spatrick       return IMAGE_REL_BASED_DIR64;
537ece8a530Spatrick     return IMAGE_REL_BASED_ABSOLUTE;
538ece8a530Spatrick   default:
539ece8a530Spatrick     llvm_unreachable("unknown machine type");
540ece8a530Spatrick   }
541ece8a530Spatrick }
542ece8a530Spatrick 
543ece8a530Spatrick // Windows-specific.
544ece8a530Spatrick // Collect all locations that contain absolute addresses, which need to be
545ece8a530Spatrick // fixed by the loader if load-time relocation is needed.
546ece8a530Spatrick // Only called when base relocation is enabled.
getBaserels(std::vector<Baserel> * res)547ece8a530Spatrick void SectionChunk::getBaserels(std::vector<Baserel> *res) {
5481cf9926bSpatrick   for (const coff_relocation &rel : getRelocs()) {
549*dfe94b16Srobert     uint8_t ty = getBaserelType(rel, file->ctx.config.machine);
550ece8a530Spatrick     if (ty == IMAGE_REL_BASED_ABSOLUTE)
551ece8a530Spatrick       continue;
552ece8a530Spatrick     Symbol *target = file->getSymbol(rel.SymbolTableIndex);
553ece8a530Spatrick     if (!target || isa<DefinedAbsolute>(target))
554ece8a530Spatrick       continue;
555ece8a530Spatrick     res->emplace_back(rva + rel.VirtualAddress, ty);
556ece8a530Spatrick   }
557ece8a530Spatrick }
558ece8a530Spatrick 
559ece8a530Spatrick // MinGW specific.
560ece8a530Spatrick // Check whether a static relocation of type Type can be deferred and
561ece8a530Spatrick // handled at runtime as a pseudo relocation (for references to a module
562ece8a530Spatrick // local variable, which turned out to actually need to be imported from
563ece8a530Spatrick // another DLL) This returns the size the relocation is supposed to update,
564ece8a530Spatrick // in bits, or 0 if the relocation cannot be handled as a runtime pseudo
565ece8a530Spatrick // relocation.
getRuntimePseudoRelocSize(uint16_t type,llvm::COFF::MachineTypes machine)566*dfe94b16Srobert static int getRuntimePseudoRelocSize(uint16_t type,
567*dfe94b16Srobert                                      llvm::COFF::MachineTypes machine) {
568ece8a530Spatrick   // Relocations that either contain an absolute address, or a plain
569ece8a530Spatrick   // relative offset, since the runtime pseudo reloc implementation
570ece8a530Spatrick   // adds 8/16/32/64 bit values to a memory address.
571ece8a530Spatrick   //
572ece8a530Spatrick   // Given a pseudo relocation entry,
573ece8a530Spatrick   //
574ece8a530Spatrick   // typedef struct {
575ece8a530Spatrick   //   DWORD sym;
576ece8a530Spatrick   //   DWORD target;
577ece8a530Spatrick   //   DWORD flags;
578ece8a530Spatrick   // } runtime_pseudo_reloc_item_v2;
579ece8a530Spatrick   //
580ece8a530Spatrick   // the runtime relocation performs this adjustment:
581ece8a530Spatrick   //     *(base + .target) += *(base + .sym) - (base + .sym)
582ece8a530Spatrick   //
583ece8a530Spatrick   // This works for both absolute addresses (IMAGE_REL_*_ADDR32/64,
584ece8a530Spatrick   // IMAGE_REL_I386_DIR32, where the memory location initially contains
585ece8a530Spatrick   // the address of the IAT slot, and for relative addresses (IMAGE_REL*_REL32),
586ece8a530Spatrick   // where the memory location originally contains the relative offset to the
587ece8a530Spatrick   // IAT slot.
588ece8a530Spatrick   //
589ece8a530Spatrick   // This requires the target address to be writable, either directly out of
590ece8a530Spatrick   // the image, or temporarily changed at runtime with VirtualProtect.
591ece8a530Spatrick   // Since this only operates on direct address values, it doesn't work for
592ece8a530Spatrick   // ARM/ARM64 relocations, other than the plain ADDR32/ADDR64 relocations.
593*dfe94b16Srobert   switch (machine) {
594ece8a530Spatrick   case AMD64:
595ece8a530Spatrick     switch (type) {
596ece8a530Spatrick     case IMAGE_REL_AMD64_ADDR64:
597ece8a530Spatrick       return 64;
598ece8a530Spatrick     case IMAGE_REL_AMD64_ADDR32:
599ece8a530Spatrick     case IMAGE_REL_AMD64_REL32:
600ece8a530Spatrick     case IMAGE_REL_AMD64_REL32_1:
601ece8a530Spatrick     case IMAGE_REL_AMD64_REL32_2:
602ece8a530Spatrick     case IMAGE_REL_AMD64_REL32_3:
603ece8a530Spatrick     case IMAGE_REL_AMD64_REL32_4:
604ece8a530Spatrick     case IMAGE_REL_AMD64_REL32_5:
605ece8a530Spatrick       return 32;
606ece8a530Spatrick     default:
607ece8a530Spatrick       return 0;
608ece8a530Spatrick     }
609ece8a530Spatrick   case I386:
610ece8a530Spatrick     switch (type) {
611ece8a530Spatrick     case IMAGE_REL_I386_DIR32:
612ece8a530Spatrick     case IMAGE_REL_I386_REL32:
613ece8a530Spatrick       return 32;
614ece8a530Spatrick     default:
615ece8a530Spatrick       return 0;
616ece8a530Spatrick     }
617ece8a530Spatrick   case ARMNT:
618ece8a530Spatrick     switch (type) {
619ece8a530Spatrick     case IMAGE_REL_ARM_ADDR32:
620ece8a530Spatrick       return 32;
621ece8a530Spatrick     default:
622ece8a530Spatrick       return 0;
623ece8a530Spatrick     }
624ece8a530Spatrick   case ARM64:
625ece8a530Spatrick     switch (type) {
626ece8a530Spatrick     case IMAGE_REL_ARM64_ADDR64:
627ece8a530Spatrick       return 64;
628ece8a530Spatrick     case IMAGE_REL_ARM64_ADDR32:
629ece8a530Spatrick       return 32;
630ece8a530Spatrick     default:
631ece8a530Spatrick       return 0;
632ece8a530Spatrick     }
633ece8a530Spatrick   default:
634ece8a530Spatrick     llvm_unreachable("unknown machine type");
635ece8a530Spatrick   }
636ece8a530Spatrick }
637ece8a530Spatrick 
638ece8a530Spatrick // MinGW specific.
639ece8a530Spatrick // Append information to the provided vector about all relocations that
640ece8a530Spatrick // need to be handled at runtime as runtime pseudo relocations (references
641ece8a530Spatrick // to a module local variable, which turned out to actually need to be
642ece8a530Spatrick // imported from another DLL).
getRuntimePseudoRelocs(std::vector<RuntimePseudoReloc> & res)643ece8a530Spatrick void SectionChunk::getRuntimePseudoRelocs(
644ece8a530Spatrick     std::vector<RuntimePseudoReloc> &res) {
645ece8a530Spatrick   for (const coff_relocation &rel : getRelocs()) {
646ece8a530Spatrick     auto *target =
647ece8a530Spatrick         dyn_cast_or_null<Defined>(file->getSymbol(rel.SymbolTableIndex));
648ece8a530Spatrick     if (!target || !target->isRuntimePseudoReloc)
649ece8a530Spatrick       continue;
650*dfe94b16Srobert     int sizeInBits =
651*dfe94b16Srobert         getRuntimePseudoRelocSize(rel.Type, file->ctx.config.machine);
652ece8a530Spatrick     if (sizeInBits == 0) {
653ece8a530Spatrick       error("unable to automatically import from " + target->getName() +
654ece8a530Spatrick             " with relocation type " +
655ece8a530Spatrick             file->getCOFFObj()->getRelocationTypeName(rel.Type) + " in " +
656ece8a530Spatrick             toString(file));
657ece8a530Spatrick       continue;
658ece8a530Spatrick     }
659ece8a530Spatrick     // sizeInBits is used to initialize the Flags field; currently no
660ece8a530Spatrick     // other flags are defined.
661ece8a530Spatrick     res.emplace_back(
662ece8a530Spatrick         RuntimePseudoReloc(target, this, rel.VirtualAddress, sizeInBits));
663ece8a530Spatrick   }
664ece8a530Spatrick }
665ece8a530Spatrick 
isCOMDAT() const666ece8a530Spatrick bool SectionChunk::isCOMDAT() const {
667ece8a530Spatrick   return header->Characteristics & IMAGE_SCN_LNK_COMDAT;
668ece8a530Spatrick }
669ece8a530Spatrick 
printDiscardedMessage() const670ece8a530Spatrick void SectionChunk::printDiscardedMessage() const {
671ece8a530Spatrick   // Removed by dead-stripping. If it's removed by ICF, ICF already
672ece8a530Spatrick   // printed out the name, so don't repeat that here.
673ece8a530Spatrick   if (sym && this == repl)
674*dfe94b16Srobert     log("Discarded " + sym->getName());
675ece8a530Spatrick }
676ece8a530Spatrick 
getDebugName() const677ece8a530Spatrick StringRef SectionChunk::getDebugName() const {
678ece8a530Spatrick   if (sym)
679ece8a530Spatrick     return sym->getName();
680ece8a530Spatrick   return "";
681ece8a530Spatrick }
682ece8a530Spatrick 
getContents() const683ece8a530Spatrick ArrayRef<uint8_t> SectionChunk::getContents() const {
684ece8a530Spatrick   ArrayRef<uint8_t> a;
685ece8a530Spatrick   cantFail(file->getCOFFObj()->getSectionContents(header, a));
686ece8a530Spatrick   return a;
687ece8a530Spatrick }
688ece8a530Spatrick 
consumeDebugMagic()689ece8a530Spatrick ArrayRef<uint8_t> SectionChunk::consumeDebugMagic() {
690ece8a530Spatrick   assert(isCodeView());
691ece8a530Spatrick   return consumeDebugMagic(getContents(), getSectionName());
692ece8a530Spatrick }
693ece8a530Spatrick 
consumeDebugMagic(ArrayRef<uint8_t> data,StringRef sectionName)694ece8a530Spatrick ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> data,
695ece8a530Spatrick                                                   StringRef sectionName) {
696ece8a530Spatrick   if (data.empty())
697ece8a530Spatrick     return {};
698ece8a530Spatrick 
699ece8a530Spatrick   // First 4 bytes are section magic.
700ece8a530Spatrick   if (data.size() < 4)
701ece8a530Spatrick     fatal("the section is too short: " + sectionName);
702ece8a530Spatrick 
703ece8a530Spatrick   if (!sectionName.startswith(".debug$"))
704ece8a530Spatrick     fatal("invalid section: " + sectionName);
705ece8a530Spatrick 
706ece8a530Spatrick   uint32_t magic = support::endian::read32le(data.data());
707ece8a530Spatrick   uint32_t expectedMagic = sectionName == ".debug$H"
708ece8a530Spatrick                                ? DEBUG_HASHES_SECTION_MAGIC
709ece8a530Spatrick                                : DEBUG_SECTION_MAGIC;
710ece8a530Spatrick   if (magic != expectedMagic) {
711ece8a530Spatrick     warn("ignoring section " + sectionName + " with unrecognized magic 0x" +
712ece8a530Spatrick          utohexstr(magic));
713ece8a530Spatrick     return {};
714ece8a530Spatrick   }
715ece8a530Spatrick   return data.slice(4);
716ece8a530Spatrick }
717ece8a530Spatrick 
findByName(ArrayRef<SectionChunk * > sections,StringRef name)718ece8a530Spatrick SectionChunk *SectionChunk::findByName(ArrayRef<SectionChunk *> sections,
719ece8a530Spatrick                                        StringRef name) {
720ece8a530Spatrick   for (SectionChunk *c : sections)
721ece8a530Spatrick     if (c->getSectionName() == name)
722ece8a530Spatrick       return c;
723ece8a530Spatrick   return nullptr;
724ece8a530Spatrick }
725ece8a530Spatrick 
replace(SectionChunk * other)726ece8a530Spatrick void SectionChunk::replace(SectionChunk *other) {
727ece8a530Spatrick   p2Align = std::max(p2Align, other->p2Align);
728ece8a530Spatrick   other->repl = repl;
729ece8a530Spatrick   other->live = false;
730ece8a530Spatrick }
731ece8a530Spatrick 
getSectionNumber() const732ece8a530Spatrick uint32_t SectionChunk::getSectionNumber() const {
733ece8a530Spatrick   DataRefImpl r;
734ece8a530Spatrick   r.p = reinterpret_cast<uintptr_t>(header);
735ece8a530Spatrick   SectionRef s(r, file->getCOFFObj());
736ece8a530Spatrick   return s.getIndex() + 1;
737ece8a530Spatrick }
738ece8a530Spatrick 
CommonChunk(const COFFSymbolRef s)739ece8a530Spatrick CommonChunk::CommonChunk(const COFFSymbolRef s) : sym(s) {
740ece8a530Spatrick   // The value of a common symbol is its size. Align all common symbols smaller
741ece8a530Spatrick   // than 32 bytes naturally, i.e. round the size up to the next power of two.
742ece8a530Spatrick   // This is what MSVC link.exe does.
743ece8a530Spatrick   setAlignment(std::min(32U, uint32_t(PowerOf2Ceil(sym.getValue()))));
744ece8a530Spatrick   hasData = false;
745ece8a530Spatrick }
746ece8a530Spatrick 
getOutputCharacteristics() const747ece8a530Spatrick uint32_t CommonChunk::getOutputCharacteristics() const {
748ece8a530Spatrick   return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
749ece8a530Spatrick          IMAGE_SCN_MEM_WRITE;
750ece8a530Spatrick }
751ece8a530Spatrick 
writeTo(uint8_t * buf) const752ece8a530Spatrick void StringChunk::writeTo(uint8_t *buf) const {
753ece8a530Spatrick   memcpy(buf, str.data(), str.size());
754ece8a530Spatrick   buf[str.size()] = '\0';
755ece8a530Spatrick }
756ece8a530Spatrick 
ImportThunkChunkX64(COFFLinkerContext & ctx,Defined * s)757*dfe94b16Srobert ImportThunkChunkX64::ImportThunkChunkX64(COFFLinkerContext &ctx, Defined *s)
758*dfe94b16Srobert     : ImportThunkChunk(ctx, s) {
759ece8a530Spatrick   // Intel Optimization Manual says that all branch targets
760ece8a530Spatrick   // should be 16-byte aligned. MSVC linker does this too.
761ece8a530Spatrick   setAlignment(16);
762ece8a530Spatrick }
763ece8a530Spatrick 
writeTo(uint8_t * buf) const764ece8a530Spatrick void ImportThunkChunkX64::writeTo(uint8_t *buf) const {
765ece8a530Spatrick   memcpy(buf, importThunkX86, sizeof(importThunkX86));
766ece8a530Spatrick   // The first two bytes is a JMP instruction. Fill its operand.
767ece8a530Spatrick   write32le(buf + 2, impSymbol->getRVA() - rva - getSize());
768ece8a530Spatrick }
769ece8a530Spatrick 
getBaserels(std::vector<Baserel> * res)770ece8a530Spatrick void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *res) {
771*dfe94b16Srobert   res->emplace_back(getRVA() + 2, ctx.config.machine);
772ece8a530Spatrick }
773ece8a530Spatrick 
writeTo(uint8_t * buf) const774ece8a530Spatrick void ImportThunkChunkX86::writeTo(uint8_t *buf) const {
775ece8a530Spatrick   memcpy(buf, importThunkX86, sizeof(importThunkX86));
776ece8a530Spatrick   // The first two bytes is a JMP instruction. Fill its operand.
777*dfe94b16Srobert   write32le(buf + 2, impSymbol->getRVA() + ctx.config.imageBase);
778ece8a530Spatrick }
779ece8a530Spatrick 
getBaserels(std::vector<Baserel> * res)780ece8a530Spatrick void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *res) {
781ece8a530Spatrick   res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
782ece8a530Spatrick }
783ece8a530Spatrick 
writeTo(uint8_t * buf) const784ece8a530Spatrick void ImportThunkChunkARM::writeTo(uint8_t *buf) const {
785ece8a530Spatrick   memcpy(buf, importThunkARM, sizeof(importThunkARM));
786ece8a530Spatrick   // Fix mov.w and mov.t operands.
787*dfe94b16Srobert   applyMOV32T(buf, impSymbol->getRVA() + ctx.config.imageBase);
788ece8a530Spatrick }
789ece8a530Spatrick 
writeTo(uint8_t * buf) const790ece8a530Spatrick void ImportThunkChunkARM64::writeTo(uint8_t *buf) const {
791ece8a530Spatrick   int64_t off = impSymbol->getRVA() & 0xfff;
792ece8a530Spatrick   memcpy(buf, importThunkARM64, sizeof(importThunkARM64));
793ece8a530Spatrick   applyArm64Addr(buf, impSymbol->getRVA(), rva, 12);
794ece8a530Spatrick   applyArm64Ldr(buf + 4, off);
795ece8a530Spatrick }
796ece8a530Spatrick 
797ece8a530Spatrick // A Thumb2, PIC, non-interworking range extension thunk.
798ece8a530Spatrick const uint8_t armThunk[] = {
799ece8a530Spatrick     0x40, 0xf2, 0x00, 0x0c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
800ece8a530Spatrick     0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
801ece8a530Spatrick     0xe7, 0x44,             // L1: add  pc, ip
802ece8a530Spatrick };
803ece8a530Spatrick 
getSize() const804ece8a530Spatrick size_t RangeExtensionThunkARM::getSize() const {
805*dfe94b16Srobert   assert(ctx.config.machine == ARMNT);
806*dfe94b16Srobert   (void)&ctx;
807ece8a530Spatrick   return sizeof(armThunk);
808ece8a530Spatrick }
809ece8a530Spatrick 
writeTo(uint8_t * buf) const810ece8a530Spatrick void RangeExtensionThunkARM::writeTo(uint8_t *buf) const {
811*dfe94b16Srobert   assert(ctx.config.machine == ARMNT);
812ece8a530Spatrick   uint64_t offset = target->getRVA() - rva - 12;
813ece8a530Spatrick   memcpy(buf, armThunk, sizeof(armThunk));
814ece8a530Spatrick   applyMOV32T(buf, uint32_t(offset));
815ece8a530Spatrick }
816ece8a530Spatrick 
817ece8a530Spatrick // A position independent ARM64 adrp+add thunk, with a maximum range of
818ece8a530Spatrick // +/- 4 GB, which is enough for any PE-COFF.
819ece8a530Spatrick const uint8_t arm64Thunk[] = {
820ece8a530Spatrick     0x10, 0x00, 0x00, 0x90, // adrp x16, Dest
821ece8a530Spatrick     0x10, 0x02, 0x00, 0x91, // add  x16, x16, :lo12:Dest
822ece8a530Spatrick     0x00, 0x02, 0x1f, 0xd6, // br   x16
823ece8a530Spatrick };
824ece8a530Spatrick 
getSize() const825ece8a530Spatrick size_t RangeExtensionThunkARM64::getSize() const {
826*dfe94b16Srobert   assert(ctx.config.machine == ARM64);
827*dfe94b16Srobert   (void)&ctx;
828ece8a530Spatrick   return sizeof(arm64Thunk);
829ece8a530Spatrick }
830ece8a530Spatrick 
writeTo(uint8_t * buf) const831ece8a530Spatrick void RangeExtensionThunkARM64::writeTo(uint8_t *buf) const {
832*dfe94b16Srobert   assert(ctx.config.machine == ARM64);
833ece8a530Spatrick   memcpy(buf, arm64Thunk, sizeof(arm64Thunk));
834ece8a530Spatrick   applyArm64Addr(buf + 0, target->getRVA(), rva, 12);
835ece8a530Spatrick   applyArm64Imm(buf + 4, target->getRVA() & 0xfff, 0);
836ece8a530Spatrick }
837ece8a530Spatrick 
LocalImportChunk(COFFLinkerContext & c,Defined * s)838*dfe94b16Srobert LocalImportChunk::LocalImportChunk(COFFLinkerContext &c, Defined *s)
839*dfe94b16Srobert     : sym(s), ctx(c) {
840*dfe94b16Srobert   setAlignment(ctx.config.wordsize);
841ece8a530Spatrick }
842ece8a530Spatrick 
getBaserels(std::vector<Baserel> * res)843*dfe94b16Srobert void LocalImportChunk::getBaserels(std::vector<Baserel> *res) {
844*dfe94b16Srobert   res->emplace_back(getRVA(), ctx.config.machine);
845*dfe94b16Srobert }
846*dfe94b16Srobert 
getSize() const847*dfe94b16Srobert size_t LocalImportChunk::getSize() const { return ctx.config.wordsize; }
848ece8a530Spatrick 
writeTo(uint8_t * buf) const849ece8a530Spatrick void LocalImportChunk::writeTo(uint8_t *buf) const {
850*dfe94b16Srobert   if (ctx.config.is64()) {
851*dfe94b16Srobert     write64le(buf, sym->getRVA() + ctx.config.imageBase);
852ece8a530Spatrick   } else {
853*dfe94b16Srobert     write32le(buf, sym->getRVA() + ctx.config.imageBase);
854ece8a530Spatrick   }
855ece8a530Spatrick }
856ece8a530Spatrick 
writeTo(uint8_t * buf) const857ece8a530Spatrick void RVATableChunk::writeTo(uint8_t *buf) const {
858ece8a530Spatrick   ulittle32_t *begin = reinterpret_cast<ulittle32_t *>(buf);
859ece8a530Spatrick   size_t cnt = 0;
860ece8a530Spatrick   for (const ChunkAndOffset &co : syms)
861ece8a530Spatrick     begin[cnt++] = co.inputChunk->getRVA() + co.offset;
862*dfe94b16Srobert   llvm::sort(begin, begin + cnt);
863ece8a530Spatrick   assert(std::unique(begin, begin + cnt) == begin + cnt &&
864ece8a530Spatrick          "RVA tables should be de-duplicated");
865ece8a530Spatrick }
866ece8a530Spatrick 
writeTo(uint8_t * buf) const8671cf9926bSpatrick void RVAFlagTableChunk::writeTo(uint8_t *buf) const {
8681cf9926bSpatrick   struct RVAFlag {
8691cf9926bSpatrick     ulittle32_t rva;
8701cf9926bSpatrick     uint8_t flag;
8711cf9926bSpatrick   };
8721cf9926bSpatrick   auto flags =
873*dfe94b16Srobert       MutableArrayRef(reinterpret_cast<RVAFlag *>(buf), syms.size());
8741cf9926bSpatrick   for (auto t : zip(syms, flags)) {
8751cf9926bSpatrick     const auto &sym = std::get<0>(t);
8761cf9926bSpatrick     auto &flag = std::get<1>(t);
8771cf9926bSpatrick     flag.rva = sym.inputChunk->getRVA() + sym.offset;
8781cf9926bSpatrick     flag.flag = 0;
8791cf9926bSpatrick   }
8801cf9926bSpatrick   llvm::sort(flags,
8811cf9926bSpatrick              [](const RVAFlag &a, const RVAFlag &b) { return a.rva < b.rva; });
8821cf9926bSpatrick   assert(llvm::unique(flags, [](const RVAFlag &a,
8831cf9926bSpatrick                                 const RVAFlag &b) { return a.rva == b.rva; }) ==
8841cf9926bSpatrick              flags.end() &&
8851cf9926bSpatrick          "RVA tables should be de-duplicated");
8861cf9926bSpatrick }
8871cf9926bSpatrick 
888ece8a530Spatrick // MinGW specific, for the "automatic import of variables from DLLs" feature.
getSize() const889ece8a530Spatrick size_t PseudoRelocTableChunk::getSize() const {
890ece8a530Spatrick   if (relocs.empty())
891ece8a530Spatrick     return 0;
892ece8a530Spatrick   return 12 + 12 * relocs.size();
893ece8a530Spatrick }
894ece8a530Spatrick 
895ece8a530Spatrick // MinGW specific.
writeTo(uint8_t * buf) const896ece8a530Spatrick void PseudoRelocTableChunk::writeTo(uint8_t *buf) const {
897ece8a530Spatrick   if (relocs.empty())
898ece8a530Spatrick     return;
899ece8a530Spatrick 
900ece8a530Spatrick   ulittle32_t *table = reinterpret_cast<ulittle32_t *>(buf);
901ece8a530Spatrick   // This is the list header, to signal the runtime pseudo relocation v2
902ece8a530Spatrick   // format.
903ece8a530Spatrick   table[0] = 0;
904ece8a530Spatrick   table[1] = 0;
905ece8a530Spatrick   table[2] = 1;
906ece8a530Spatrick 
907ece8a530Spatrick   size_t idx = 3;
908ece8a530Spatrick   for (const RuntimePseudoReloc &rpr : relocs) {
909ece8a530Spatrick     table[idx + 0] = rpr.sym->getRVA();
910ece8a530Spatrick     table[idx + 1] = rpr.target->getRVA() + rpr.targetOffset;
911ece8a530Spatrick     table[idx + 2] = rpr.flags;
912ece8a530Spatrick     idx += 3;
913ece8a530Spatrick   }
914ece8a530Spatrick }
915ece8a530Spatrick 
916ece8a530Spatrick // Windows-specific. This class represents a block in .reloc section.
917ece8a530Spatrick // The format is described here.
918ece8a530Spatrick //
919ece8a530Spatrick // On Windows, each DLL is linked against a fixed base address and
920ece8a530Spatrick // usually loaded to that address. However, if there's already another
921ece8a530Spatrick // DLL that overlaps, the loader has to relocate it. To do that, DLLs
922ece8a530Spatrick // contain .reloc sections which contain offsets that need to be fixed
923ece8a530Spatrick // up at runtime. If the loader finds that a DLL cannot be loaded to its
924ece8a530Spatrick // desired base address, it loads it to somewhere else, and add <actual
925ece8a530Spatrick // base address> - <desired base address> to each offset that is
926ece8a530Spatrick // specified by the .reloc section. In ELF terms, .reloc sections
927ece8a530Spatrick // contain relative relocations in REL format (as opposed to RELA.)
928ece8a530Spatrick //
929ece8a530Spatrick // This already significantly reduces the size of relocations compared
930ece8a530Spatrick // to ELF .rel.dyn, but Windows does more to reduce it (probably because
931ece8a530Spatrick // it was invented for PCs in the late '80s or early '90s.)  Offsets in
932ece8a530Spatrick // .reloc are grouped by page where the page size is 12 bits, and
933ece8a530Spatrick // offsets sharing the same page address are stored consecutively to
934ece8a530Spatrick // represent them with less space. This is very similar to the page
935ece8a530Spatrick // table which is grouped by (multiple stages of) pages.
936ece8a530Spatrick //
937ece8a530Spatrick // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
938ece8a530Spatrick // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
939ece8a530Spatrick // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
940ece8a530Spatrick // are represented like this:
941ece8a530Spatrick //
942ece8a530Spatrick //   0x00000  -- page address (4 bytes)
943ece8a530Spatrick //   16       -- size of this block (4 bytes)
944ece8a530Spatrick //     0xA030 -- entries (2 bytes each)
945ece8a530Spatrick //     0xA500
946ece8a530Spatrick //     0xA700
947ece8a530Spatrick //     0xAA00
948ece8a530Spatrick //   0x20000  -- page address (4 bytes)
949ece8a530Spatrick //   12       -- size of this block (4 bytes)
950ece8a530Spatrick //     0xA004 -- entries (2 bytes each)
951ece8a530Spatrick //     0xA008
952ece8a530Spatrick //
953ece8a530Spatrick // Usually we have a lot of relocations for each page, so the number of
954ece8a530Spatrick // bytes for one .reloc entry is close to 2 bytes on average.
BaserelChunk(uint32_t page,Baserel * begin,Baserel * end)955ece8a530Spatrick BaserelChunk::BaserelChunk(uint32_t page, Baserel *begin, Baserel *end) {
956ece8a530Spatrick   // Block header consists of 4 byte page RVA and 4 byte block size.
957ece8a530Spatrick   // Each entry is 2 byte. Last entry may be padding.
958ece8a530Spatrick   data.resize(alignTo((end - begin) * 2 + 8, 4));
959ece8a530Spatrick   uint8_t *p = data.data();
960ece8a530Spatrick   write32le(p, page);
961ece8a530Spatrick   write32le(p + 4, data.size());
962ece8a530Spatrick   p += 8;
963ece8a530Spatrick   for (Baserel *i = begin; i != end; ++i) {
964ece8a530Spatrick     write16le(p, (i->type << 12) | (i->rva - page));
965ece8a530Spatrick     p += 2;
966ece8a530Spatrick   }
967ece8a530Spatrick }
968ece8a530Spatrick 
writeTo(uint8_t * buf) const969ece8a530Spatrick void BaserelChunk::writeTo(uint8_t *buf) const {
970ece8a530Spatrick   memcpy(buf, data.data(), data.size());
971ece8a530Spatrick }
972ece8a530Spatrick 
getDefaultType(llvm::COFF::MachineTypes machine)973*dfe94b16Srobert uint8_t Baserel::getDefaultType(llvm::COFF::MachineTypes machine) {
974*dfe94b16Srobert   switch (machine) {
975ece8a530Spatrick   case AMD64:
976ece8a530Spatrick   case ARM64:
977ece8a530Spatrick     return IMAGE_REL_BASED_DIR64;
978ece8a530Spatrick   case I386:
979ece8a530Spatrick   case ARMNT:
980ece8a530Spatrick     return IMAGE_REL_BASED_HIGHLOW;
981ece8a530Spatrick   default:
982ece8a530Spatrick     llvm_unreachable("unknown machine type");
983ece8a530Spatrick   }
984ece8a530Spatrick }
985ece8a530Spatrick 
MergeChunk(uint32_t alignment)986ece8a530Spatrick MergeChunk::MergeChunk(uint32_t alignment)
987*dfe94b16Srobert     : builder(StringTableBuilder::RAW, llvm::Align(alignment)) {
988ece8a530Spatrick   setAlignment(alignment);
989ece8a530Spatrick }
990ece8a530Spatrick 
addSection(COFFLinkerContext & ctx,SectionChunk * c)991*dfe94b16Srobert void MergeChunk::addSection(COFFLinkerContext &ctx, SectionChunk *c) {
992ece8a530Spatrick   assert(isPowerOf2_32(c->getAlignment()));
993ece8a530Spatrick   uint8_t p2Align = llvm::Log2_32(c->getAlignment());
994*dfe94b16Srobert   assert(p2Align < std::size(ctx.mergeChunkInstances));
995*dfe94b16Srobert   auto *&mc = ctx.mergeChunkInstances[p2Align];
996ece8a530Spatrick   if (!mc)
997ece8a530Spatrick     mc = make<MergeChunk>(c->getAlignment());
998ece8a530Spatrick   mc->sections.push_back(c);
999ece8a530Spatrick }
1000ece8a530Spatrick 
finalizeContents()1001ece8a530Spatrick void MergeChunk::finalizeContents() {
1002ece8a530Spatrick   assert(!finalized && "should only finalize once");
1003ece8a530Spatrick   for (SectionChunk *c : sections)
1004ece8a530Spatrick     if (c->live)
1005ece8a530Spatrick       builder.add(toStringRef(c->getContents()));
1006ece8a530Spatrick   builder.finalize();
1007ece8a530Spatrick   finalized = true;
1008ece8a530Spatrick }
1009ece8a530Spatrick 
assignSubsectionRVAs()1010ece8a530Spatrick void MergeChunk::assignSubsectionRVAs() {
1011ece8a530Spatrick   for (SectionChunk *c : sections) {
1012ece8a530Spatrick     if (!c->live)
1013ece8a530Spatrick       continue;
1014ece8a530Spatrick     size_t off = builder.getOffset(toStringRef(c->getContents()));
1015ece8a530Spatrick     c->setRVA(rva + off);
1016ece8a530Spatrick   }
1017ece8a530Spatrick }
1018ece8a530Spatrick 
getOutputCharacteristics() const1019ece8a530Spatrick uint32_t MergeChunk::getOutputCharacteristics() const {
1020ece8a530Spatrick   return IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA;
1021ece8a530Spatrick }
1022ece8a530Spatrick 
getSize() const1023ece8a530Spatrick size_t MergeChunk::getSize() const {
1024ece8a530Spatrick   return builder.getSize();
1025ece8a530Spatrick }
1026ece8a530Spatrick 
writeTo(uint8_t * buf) const1027ece8a530Spatrick void MergeChunk::writeTo(uint8_t *buf) const {
1028ece8a530Spatrick   builder.write(buf);
1029ece8a530Spatrick }
1030ece8a530Spatrick 
1031ece8a530Spatrick // MinGW specific.
getSize() const1032*dfe94b16Srobert size_t AbsolutePointerChunk::getSize() const { return ctx.config.wordsize; }
1033ece8a530Spatrick 
writeTo(uint8_t * buf) const1034ece8a530Spatrick void AbsolutePointerChunk::writeTo(uint8_t *buf) const {
1035*dfe94b16Srobert   if (ctx.config.is64()) {
1036ece8a530Spatrick     write64le(buf, value);
1037ece8a530Spatrick   } else {
1038ece8a530Spatrick     write32le(buf, value);
1039ece8a530Spatrick   }
1040ece8a530Spatrick }
1041ece8a530Spatrick 
1042*dfe94b16Srobert } // namespace lld::coff
1043