xref: /llvm-project-15.0.7/lld/COFF/Chunks.cpp (revision 303c9861)
1 //===- Chunks.cpp ---------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "Chunks.h"
10 #include "InputFiles.h"
11 #include "Symbols.h"
12 #include "Writer.h"
13 #include "SymbolTable.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), File(F), Header(H), Repl(this) {
34   // Initialize Relocs.
35   setRelocs(File->getCOFFObj()->getRelocations(Header));
36 
37   // Initialize SectionName.
38   StringRef SectionName;
39   if (Expected<StringRef> E = File->getCOFFObj()->getSectionName(Header))
40     SectionName = *E;
41   SectionNameData = SectionName.data();
42   SectionNameSize = SectionName.size();
43 
44   setAlignment(Header->getAlignment());
45 
46   HasData = !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
47 
48   // If linker GC is disabled, every chunk starts out alive.  If linker GC is
49   // enabled, treat non-comdat sections as roots. Generally optimized object
50   // files will be built with -ffunction-sections or /Gy, so most things worth
51   // stripping will be in a comdat.
52   Live = !Config->DoGC || !isCOMDAT();
53 }
54 
55 // SectionChunk is one of the most frequently allocated classes, so it is
56 // important to keep it as compact as possible. As of this writing, the number
57 // below is the size of this class on x64 platforms.
58 static_assert(sizeof(SectionChunk) <= 88, "SectionChunk grew unexpectedly");
59 
60 static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
61 static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
62 static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
63 static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
64 static void or32(uint8_t *P, uint32_t V) { write32le(P, read32le(P) | V); }
65 
66 // Verify that given sections are appropriate targets for SECREL
67 // relocations. This check is relaxed because unfortunately debug
68 // sections have section-relative relocations against absolute symbols.
69 static bool checkSecRel(const SectionChunk *Sec, OutputSection *OS) {
70   if (OS)
71     return true;
72   if (Sec->isCodeView())
73     return false;
74   error("SECREL relocation cannot be applied to absolute symbols");
75   return false;
76 }
77 
78 static void applySecRel(const SectionChunk *Sec, uint8_t *Off,
79                         OutputSection *OS, uint64_t S) {
80   if (!checkSecRel(Sec, OS))
81     return;
82   uint64_t SecRel = S - OS->getRVA();
83   if (SecRel > UINT32_MAX) {
84     error("overflow in SECREL relocation in section: " + Sec->getSectionName());
85     return;
86   }
87   add32(Off, SecRel);
88 }
89 
90 static void applySecIdx(uint8_t *Off, OutputSection *OS) {
91   // Absolute symbol doesn't have section index, but section index relocation
92   // against absolute symbol should be resolved to one plus the last output
93   // section index. This is required for compatibility with MSVC.
94   if (OS)
95     add16(Off, OS->SectionIndex);
96   else
97     add16(Off, DefinedAbsolute::NumOutputSections + 1);
98 }
99 
100 void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS,
101                                uint64_t S, uint64_t P) const {
102   switch (Type) {
103   case IMAGE_REL_AMD64_ADDR32:   add32(Off, S + Config->ImageBase); break;
104   case IMAGE_REL_AMD64_ADDR64:   add64(Off, S + Config->ImageBase); break;
105   case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
106   case IMAGE_REL_AMD64_REL32:    add32(Off, S - P - 4); break;
107   case IMAGE_REL_AMD64_REL32_1:  add32(Off, S - P - 5); break;
108   case IMAGE_REL_AMD64_REL32_2:  add32(Off, S - P - 6); break;
109   case IMAGE_REL_AMD64_REL32_3:  add32(Off, S - P - 7); break;
110   case IMAGE_REL_AMD64_REL32_4:  add32(Off, S - P - 8); break;
111   case IMAGE_REL_AMD64_REL32_5:  add32(Off, S - P - 9); break;
112   case IMAGE_REL_AMD64_SECTION:  applySecIdx(Off, OS); break;
113   case IMAGE_REL_AMD64_SECREL:   applySecRel(this, Off, OS, S); break;
114   default:
115     error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
116           toString(File));
117   }
118 }
119 
120 void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS,
121                                uint64_t S, uint64_t P) const {
122   switch (Type) {
123   case IMAGE_REL_I386_ABSOLUTE: break;
124   case IMAGE_REL_I386_DIR32:    add32(Off, S + Config->ImageBase); break;
125   case IMAGE_REL_I386_DIR32NB:  add32(Off, S); break;
126   case IMAGE_REL_I386_REL32:    add32(Off, S - P - 4); break;
127   case IMAGE_REL_I386_SECTION:  applySecIdx(Off, OS); break;
128   case IMAGE_REL_I386_SECREL:   applySecRel(this, Off, OS, S); break;
129   default:
130     error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
131           toString(File));
132   }
133 }
134 
135 static void applyMOV(uint8_t *Off, uint16_t V) {
136   write16le(Off, (read16le(Off) & 0xfbf0) | ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
137   write16le(Off + 2, (read16le(Off + 2) & 0x8f00) | ((V & 0x700) << 4) | (V & 0xff));
138 }
139 
140 static uint16_t readMOV(uint8_t *Off, bool MOVT) {
141   uint16_t Op1 = read16le(Off);
142   if ((Op1 & 0xfbf0) != (MOVT ? 0xf2c0 : 0xf240))
143     error("unexpected instruction in " + Twine(MOVT ? "MOVT" : "MOVW") +
144           " instruction in MOV32T relocation");
145   uint16_t Op2 = read16le(Off + 2);
146   if ((Op2 & 0x8000) != 0)
147     error("unexpected instruction in " + Twine(MOVT ? "MOVT" : "MOVW") +
148           " instruction in MOV32T relocation");
149   return (Op2 & 0x00ff) | ((Op2 >> 4) & 0x0700) | ((Op1 << 1) & 0x0800) |
150          ((Op1 & 0x000f) << 12);
151 }
152 
153 void applyMOV32T(uint8_t *Off, uint32_t V) {
154   uint16_t ImmW = readMOV(Off, false);    // read MOVW operand
155   uint16_t ImmT = readMOV(Off + 4, true); // read MOVT operand
156   uint32_t Imm = ImmW | (ImmT << 16);
157   V += Imm;                         // add the immediate offset
158   applyMOV(Off, V);           // set MOVW operand
159   applyMOV(Off + 4, V >> 16); // set MOVT operand
160 }
161 
162 static void applyBranch20T(uint8_t *Off, int32_t V) {
163   if (!isInt<21>(V))
164     error("relocation out of range");
165   uint32_t S = V < 0 ? 1 : 0;
166   uint32_t J1 = (V >> 19) & 1;
167   uint32_t J2 = (V >> 18) & 1;
168   or16(Off, (S << 10) | ((V >> 12) & 0x3f));
169   or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
170 }
171 
172 void applyBranch24T(uint8_t *Off, int32_t V) {
173   if (!isInt<25>(V))
174     error("relocation out of range");
175   uint32_t S = V < 0 ? 1 : 0;
176   uint32_t J1 = ((~V >> 23) & 1) ^ S;
177   uint32_t J2 = ((~V >> 22) & 1) ^ S;
178   or16(Off, (S << 10) | ((V >> 12) & 0x3ff));
179   // Clear out the J1 and J2 bits which may be set.
180   write16le(Off + 2, (read16le(Off + 2) & 0xd000) | (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
181 }
182 
183 void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS,
184                                uint64_t S, uint64_t P) const {
185   // Pointer to thumb code must have the LSB set.
186   uint64_t SX = S;
187   if (OS && (OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE))
188     SX |= 1;
189   switch (Type) {
190   case IMAGE_REL_ARM_ADDR32:    add32(Off, SX + Config->ImageBase); break;
191   case IMAGE_REL_ARM_ADDR32NB:  add32(Off, SX); break;
192   case IMAGE_REL_ARM_MOV32T:    applyMOV32T(Off, SX + Config->ImageBase); break;
193   case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, SX - P - 4); break;
194   case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, SX - P - 4); break;
195   case IMAGE_REL_ARM_BLX23T:    applyBranch24T(Off, SX - P - 4); break;
196   case IMAGE_REL_ARM_SECTION:   applySecIdx(Off, OS); break;
197   case IMAGE_REL_ARM_SECREL:    applySecRel(this, Off, OS, S); break;
198   case IMAGE_REL_ARM_REL32:     add32(Off, SX - P - 4); break;
199   default:
200     error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
201           toString(File));
202   }
203 }
204 
205 // Interpret the existing immediate value as a byte offset to the
206 // target symbol, then update the instruction with the immediate as
207 // the page offset from the current instruction to the target.
208 void applyArm64Addr(uint8_t *Off, uint64_t S, uint64_t P, int Shift) {
209   uint32_t Orig = read32le(Off);
210   uint64_t Imm = ((Orig >> 29) & 0x3) | ((Orig >> 3) & 0x1FFFFC);
211   S += Imm;
212   Imm = (S >> Shift) - (P >> Shift);
213   uint32_t ImmLo = (Imm & 0x3) << 29;
214   uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
215   uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
216   write32le(Off, (Orig & ~Mask) | ImmLo | ImmHi);
217 }
218 
219 // Update the immediate field in a AARCH64 ldr, str, and add instruction.
220 // Optionally limit the range of the written immediate by one or more bits
221 // (RangeLimit).
222 void applyArm64Imm(uint8_t *Off, uint64_t Imm, uint32_t RangeLimit) {
223   uint32_t Orig = read32le(Off);
224   Imm += (Orig >> 10) & 0xFFF;
225   Orig &= ~(0xFFF << 10);
226   write32le(Off, Orig | ((Imm & (0xFFF >> RangeLimit)) << 10));
227 }
228 
229 // Add the 12 bit page offset to the existing immediate.
230 // Ldr/str instructions store the opcode immediate scaled
231 // by the load/store size (giving a larger range for larger
232 // loads/stores). The immediate is always (both before and after
233 // fixing up the relocation) stored scaled similarly.
234 // Even if larger loads/stores have a larger range, limit the
235 // effective offset to 12 bit, since it is intended to be a
236 // page offset.
237 static void applyArm64Ldr(uint8_t *Off, uint64_t Imm) {
238   uint32_t Orig = read32le(Off);
239   uint32_t Size = Orig >> 30;
240   // 0x04000000 indicates SIMD/FP registers
241   // 0x00800000 indicates 128 bit
242   if ((Orig & 0x4800000) == 0x4800000)
243     Size += 4;
244   if ((Imm & ((1 << Size) - 1)) != 0)
245     error("misaligned ldr/str offset");
246   applyArm64Imm(Off, Imm >> Size, Size);
247 }
248 
249 static void applySecRelLow12A(const SectionChunk *Sec, uint8_t *Off,
250                               OutputSection *OS, uint64_t S) {
251   if (checkSecRel(Sec, OS))
252     applyArm64Imm(Off, (S - OS->getRVA()) & 0xfff, 0);
253 }
254 
255 static void applySecRelHigh12A(const SectionChunk *Sec, uint8_t *Off,
256                                OutputSection *OS, uint64_t S) {
257   if (!checkSecRel(Sec, OS))
258     return;
259   uint64_t SecRel = (S - OS->getRVA()) >> 12;
260   if (0xfff < SecRel) {
261     error("overflow in SECREL_HIGH12A relocation in section: " +
262           Sec->getSectionName());
263     return;
264   }
265   applyArm64Imm(Off, SecRel & 0xfff, 0);
266 }
267 
268 static void applySecRelLdr(const SectionChunk *Sec, uint8_t *Off,
269                            OutputSection *OS, uint64_t S) {
270   if (checkSecRel(Sec, OS))
271     applyArm64Ldr(Off, (S - OS->getRVA()) & 0xfff);
272 }
273 
274 void applyArm64Branch26(uint8_t *Off, int64_t V) {
275   if (!isInt<28>(V))
276     error("relocation out of range");
277   or32(Off, (V & 0x0FFFFFFC) >> 2);
278 }
279 
280 static void applyArm64Branch19(uint8_t *Off, int64_t V) {
281   if (!isInt<21>(V))
282     error("relocation out of range");
283   or32(Off, (V & 0x001FFFFC) << 3);
284 }
285 
286 static void applyArm64Branch14(uint8_t *Off, int64_t V) {
287   if (!isInt<16>(V))
288     error("relocation out of range");
289   or32(Off, (V & 0x0000FFFC) << 3);
290 }
291 
292 void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS,
293                                  uint64_t S, uint64_t P) const {
294   switch (Type) {
295   case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(Off, S, P, 12); break;
296   case IMAGE_REL_ARM64_REL21:          applyArm64Addr(Off, S, P, 0); break;
297   case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(Off, S & 0xfff, 0); break;
298   case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(Off, S & 0xfff); break;
299   case IMAGE_REL_ARM64_BRANCH26:       applyArm64Branch26(Off, S - P); break;
300   case IMAGE_REL_ARM64_BRANCH19:       applyArm64Branch19(Off, S - P); break;
301   case IMAGE_REL_ARM64_BRANCH14:       applyArm64Branch14(Off, S - P); break;
302   case IMAGE_REL_ARM64_ADDR32:         add32(Off, S + Config->ImageBase); break;
303   case IMAGE_REL_ARM64_ADDR32NB:       add32(Off, S); break;
304   case IMAGE_REL_ARM64_ADDR64:         add64(Off, S + Config->ImageBase); break;
305   case IMAGE_REL_ARM64_SECREL:         applySecRel(this, Off, OS, S); break;
306   case IMAGE_REL_ARM64_SECREL_LOW12A:  applySecRelLow12A(this, Off, OS, S); break;
307   case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, Off, OS, S); break;
308   case IMAGE_REL_ARM64_SECREL_LOW12L:  applySecRelLdr(this, Off, OS, S); break;
309   case IMAGE_REL_ARM64_SECTION:        applySecIdx(Off, OS); break;
310   case IMAGE_REL_ARM64_REL32:          add32(Off, S - P - 4); break;
311   default:
312     error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
313           toString(File));
314   }
315 }
316 
317 static void maybeReportRelocationToDiscarded(const SectionChunk *FromChunk,
318                                              Defined *Sym,
319                                              const coff_relocation &Rel) {
320   // Don't report these errors when the relocation comes from a debug info
321   // section or in mingw mode. MinGW mode object files (built by GCC) can
322   // have leftover sections with relocations against discarded comdat
323   // sections. Such sections are left as is, with relocations untouched.
324   if (FromChunk->isCodeView() || FromChunk->isDWARF() || Config->MinGW)
325     return;
326 
327   // Get the name of the symbol. If it's null, it was discarded early, so we
328   // have to go back to the object file.
329   ObjFile *File = FromChunk->File;
330   StringRef Name;
331   if (Sym) {
332     Name = Sym->getName();
333   } else {
334     COFFSymbolRef COFFSym =
335         check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex));
336     File->getCOFFObj()->getSymbolName(COFFSym, Name);
337   }
338 
339   error("relocation against symbol in discarded section: " + Name +
340         getSymbolLocations(File, Rel.SymbolTableIndex));
341 }
342 
343 void SectionChunk::writeTo(uint8_t *Buf) const {
344   if (!hasData())
345     return;
346   // Copy section contents from source object file to output file.
347   ArrayRef<uint8_t> A = getContents();
348   if (!A.empty())
349     memcpy(Buf, A.data(), A.size());
350 
351   // Apply relocations.
352   size_t InputSize = getSize();
353   for (size_t I = 0, E = RelocsSize; I < E; I++) {
354     const coff_relocation &Rel = RelocsData[I];
355 
356     // Check for an invalid relocation offset. This check isn't perfect, because
357     // we don't have the relocation size, which is only known after checking the
358     // machine and relocation type. As a result, a relocation may overwrite the
359     // beginning of the following input section.
360     if (Rel.VirtualAddress >= InputSize) {
361       error("relocation points beyond the end of its parent section");
362       continue;
363     }
364 
365     uint8_t *Off = Buf + Rel.VirtualAddress;
366 
367     auto *Sym =
368         dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
369 
370     // Get the output section of the symbol for this relocation.  The output
371     // section is needed to compute SECREL and SECTION relocations used in debug
372     // info.
373     Chunk *C = Sym ? Sym->getChunk() : nullptr;
374     OutputSection *OS = C ? C->getOutputSection() : nullptr;
375 
376     // Skip the relocation if it refers to a discarded section, and diagnose it
377     // as an error if appropriate. If a symbol was discarded early, it may be
378     // null. If it was discarded late, the output section will be null, unless
379     // it was an absolute or synthetic symbol.
380     if (!Sym ||
381         (!OS && !isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym))) {
382       maybeReportRelocationToDiscarded(this, Sym, Rel);
383       continue;
384     }
385 
386     uint64_t S = Sym->getRVA();
387 
388     // Compute the RVA of the relocation for relative relocations.
389     uint64_t P = RVA + Rel.VirtualAddress;
390     switch (Config->Machine) {
391     case AMD64:
392       applyRelX64(Off, Rel.Type, OS, S, P);
393       break;
394     case I386:
395       applyRelX86(Off, Rel.Type, OS, S, P);
396       break;
397     case ARMNT:
398       applyRelARM(Off, Rel.Type, OS, S, P);
399       break;
400     case ARM64:
401       applyRelARM64(Off, Rel.Type, OS, S, P);
402       break;
403     default:
404       llvm_unreachable("unknown machine type");
405     }
406   }
407 }
408 
409 void SectionChunk::addAssociative(SectionChunk *Child) {
410   // Insert this child at the head of the list.
411   assert(Child->AssocChildren == nullptr &&
412          "associated sections cannot have their own associated children");
413   Child->AssocChildren = AssocChildren;
414   AssocChildren = Child;
415 }
416 
417 static uint8_t getBaserelType(const coff_relocation &Rel) {
418   switch (Config->Machine) {
419   case AMD64:
420     if (Rel.Type == IMAGE_REL_AMD64_ADDR64)
421       return IMAGE_REL_BASED_DIR64;
422     return IMAGE_REL_BASED_ABSOLUTE;
423   case I386:
424     if (Rel.Type == IMAGE_REL_I386_DIR32)
425       return IMAGE_REL_BASED_HIGHLOW;
426     return IMAGE_REL_BASED_ABSOLUTE;
427   case ARMNT:
428     if (Rel.Type == IMAGE_REL_ARM_ADDR32)
429       return IMAGE_REL_BASED_HIGHLOW;
430     if (Rel.Type == IMAGE_REL_ARM_MOV32T)
431       return IMAGE_REL_BASED_ARM_MOV32T;
432     return IMAGE_REL_BASED_ABSOLUTE;
433   case ARM64:
434     if (Rel.Type == IMAGE_REL_ARM64_ADDR64)
435       return IMAGE_REL_BASED_DIR64;
436     return IMAGE_REL_BASED_ABSOLUTE;
437   default:
438     llvm_unreachable("unknown machine type");
439   }
440 }
441 
442 // Windows-specific.
443 // Collect all locations that contain absolute addresses, which need to be
444 // fixed by the loader if load-time relocation is needed.
445 // Only called when base relocation is enabled.
446 void SectionChunk::getBaserels(std::vector<Baserel> *Res) {
447   for (size_t I = 0, E = RelocsSize; I < E; I++) {
448     const coff_relocation &Rel = RelocsData[I];
449     uint8_t Ty = getBaserelType(Rel);
450     if (Ty == IMAGE_REL_BASED_ABSOLUTE)
451       continue;
452     Symbol *Target = File->getSymbol(Rel.SymbolTableIndex);
453     if (!Target || isa<DefinedAbsolute>(Target))
454       continue;
455     Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
456   }
457 }
458 
459 // MinGW specific.
460 // Check whether a static relocation of type Type can be deferred and
461 // handled at runtime as a pseudo relocation (for references to a module
462 // local variable, which turned out to actually need to be imported from
463 // another DLL) This returns the size the relocation is supposed to update,
464 // in bits, or 0 if the relocation cannot be handled as a runtime pseudo
465 // relocation.
466 static int getRuntimePseudoRelocSize(uint16_t Type) {
467   // Relocations that either contain an absolute address, or a plain
468   // relative offset, since the runtime pseudo reloc implementation
469   // adds 8/16/32/64 bit values to a memory address.
470   //
471   // Given a pseudo relocation entry,
472   //
473   // typedef struct {
474   //   DWORD sym;
475   //   DWORD target;
476   //   DWORD flags;
477   // } runtime_pseudo_reloc_item_v2;
478   //
479   // the runtime relocation performs this adjustment:
480   //     *(base + .target) += *(base + .sym) - (base + .sym)
481   //
482   // This works for both absolute addresses (IMAGE_REL_*_ADDR32/64,
483   // IMAGE_REL_I386_DIR32, where the memory location initially contains
484   // the address of the IAT slot, and for relative addresses (IMAGE_REL*_REL32),
485   // where the memory location originally contains the relative offset to the
486   // IAT slot.
487   //
488   // This requires the target address to be writable, either directly out of
489   // the image, or temporarily changed at runtime with VirtualProtect.
490   // Since this only operates on direct address values, it doesn't work for
491   // ARM/ARM64 relocations, other than the plain ADDR32/ADDR64 relocations.
492   switch (Config->Machine) {
493   case AMD64:
494     switch (Type) {
495     case IMAGE_REL_AMD64_ADDR64:
496       return 64;
497     case IMAGE_REL_AMD64_ADDR32:
498     case IMAGE_REL_AMD64_REL32:
499     case IMAGE_REL_AMD64_REL32_1:
500     case IMAGE_REL_AMD64_REL32_2:
501     case IMAGE_REL_AMD64_REL32_3:
502     case IMAGE_REL_AMD64_REL32_4:
503     case IMAGE_REL_AMD64_REL32_5:
504       return 32;
505     default:
506       return 0;
507     }
508   case I386:
509     switch (Type) {
510     case IMAGE_REL_I386_DIR32:
511     case IMAGE_REL_I386_REL32:
512       return 32;
513     default:
514       return 0;
515     }
516   case ARMNT:
517     switch (Type) {
518     case IMAGE_REL_ARM_ADDR32:
519       return 32;
520     default:
521       return 0;
522     }
523   case ARM64:
524     switch (Type) {
525     case IMAGE_REL_ARM64_ADDR64:
526       return 64;
527     case IMAGE_REL_ARM64_ADDR32:
528       return 32;
529     default:
530       return 0;
531     }
532   default:
533     llvm_unreachable("unknown machine type");
534   }
535 }
536 
537 // MinGW specific.
538 // Append information to the provided vector about all relocations that
539 // need to be handled at runtime as runtime pseudo relocations (references
540 // to a module local variable, which turned out to actually need to be
541 // imported from another DLL).
542 void SectionChunk::getRuntimePseudoRelocs(
543     std::vector<RuntimePseudoReloc> &Res) {
544   for (const coff_relocation &Rel : getRelocs()) {
545     auto *Target =
546         dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
547     if (!Target || !Target->IsRuntimePseudoReloc)
548       continue;
549     int SizeInBits = getRuntimePseudoRelocSize(Rel.Type);
550     if (SizeInBits == 0) {
551       error("unable to automatically import from " + Target->getName() +
552             " with relocation type " +
553             File->getCOFFObj()->getRelocationTypeName(Rel.Type) + " in " +
554             toString(File));
555       continue;
556     }
557     // SizeInBits is used to initialize the Flags field; currently no
558     // other flags are defined.
559     Res.emplace_back(
560         RuntimePseudoReloc(Target, this, Rel.VirtualAddress, SizeInBits));
561   }
562 }
563 
564 bool SectionChunk::isCOMDAT() const {
565   return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
566 }
567 
568 void SectionChunk::printDiscardedMessage() const {
569   // Removed by dead-stripping. If it's removed by ICF, ICF already
570   // printed out the name, so don't repeat that here.
571   if (Sym && this == Repl)
572     message("Discarded " + Sym->getName());
573 }
574 
575 StringRef SectionChunk::getDebugName() const {
576   if (Sym)
577     return Sym->getName();
578   return "";
579 }
580 
581 ArrayRef<uint8_t> SectionChunk::getContents() const {
582   ArrayRef<uint8_t> A;
583   cantFail(File->getCOFFObj()->getSectionContents(Header, A));
584   return A;
585 }
586 
587 ArrayRef<uint8_t> SectionChunk::consumeDebugMagic() {
588   assert(isCodeView());
589   return consumeDebugMagic(getContents(), getSectionName());
590 }
591 
592 ArrayRef<uint8_t> SectionChunk::consumeDebugMagic(ArrayRef<uint8_t> Data,
593                                                   StringRef SectionName) {
594   if (Data.empty())
595     return {};
596 
597   // First 4 bytes are section magic.
598   if (Data.size() < 4)
599     fatal("the section is too short: " + SectionName);
600 
601   if (!SectionName.startswith(".debug$"))
602     fatal("invalid section: " + SectionName);
603 
604   uint32_t Magic = support::endian::read32le(Data.data());
605   uint32_t ExpectedMagic = SectionName == ".debug$H"
606                                ? DEBUG_HASHES_SECTION_MAGIC
607                                : DEBUG_SECTION_MAGIC;
608   if (Magic != ExpectedMagic) {
609     warn("ignoring section " + SectionName + " with unrecognized magic 0x" +
610          utohexstr(Magic));
611     return {};
612   }
613   return Data.slice(4);
614 }
615 
616 SectionChunk *SectionChunk::findByName(ArrayRef<SectionChunk *> Sections,
617                                        StringRef Name) {
618   for (SectionChunk *C : Sections)
619     if (C->getSectionName() == Name)
620       return C;
621   return nullptr;
622 }
623 
624 void SectionChunk::replace(SectionChunk *Other) {
625   P2Align = std::max(P2Align, Other->P2Align);
626   Other->Repl = Repl;
627   Other->Live = false;
628 }
629 
630 uint32_t SectionChunk::getSectionNumber() const {
631   DataRefImpl R;
632   R.p = reinterpret_cast<uintptr_t>(Header);
633   SectionRef S(R, File->getCOFFObj());
634   return S.getIndex() + 1;
635 }
636 
637 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
638   // The value of a common symbol is its size. Align all common symbols smaller
639   // than 32 bytes naturally, i.e. round the size up to the next power of two.
640   // This is what MSVC link.exe does.
641   setAlignment(std::min(32U, uint32_t(PowerOf2Ceil(Sym.getValue()))));
642   HasData = false;
643 }
644 
645 uint32_t CommonChunk::getOutputCharacteristics() const {
646   return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
647          IMAGE_SCN_MEM_WRITE;
648 }
649 
650 void StringChunk::writeTo(uint8_t *Buf) const {
651   memcpy(Buf, Str.data(), Str.size());
652   Buf[Str.size()] = '\0';
653 }
654 
655 ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImportThunkChunk(S) {
656   // Intel Optimization Manual says that all branch targets
657   // should be 16-byte aligned. MSVC linker does this too.
658   setAlignment(16);
659 }
660 
661 void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
662   memcpy(Buf, ImportThunkX86, sizeof(ImportThunkX86));
663   // The first two bytes is a JMP instruction. Fill its operand.
664   write32le(Buf + 2, ImpSymbol->getRVA() - RVA - getSize());
665 }
666 
667 void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
668   Res->emplace_back(getRVA() + 2);
669 }
670 
671 void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
672   memcpy(Buf, ImportThunkX86, sizeof(ImportThunkX86));
673   // The first two bytes is a JMP instruction. Fill its operand.
674   write32le(Buf + 2,
675             ImpSymbol->getRVA() + Config->ImageBase);
676 }
677 
678 void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
679   Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
680 }
681 
682 void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
683   memcpy(Buf, ImportThunkARM, sizeof(ImportThunkARM));
684   // Fix mov.w and mov.t operands.
685   applyMOV32T(Buf, ImpSymbol->getRVA() + Config->ImageBase);
686 }
687 
688 void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const {
689   int64_t Off = ImpSymbol->getRVA() & 0xfff;
690   memcpy(Buf, ImportThunkARM64, sizeof(ImportThunkARM64));
691   applyArm64Addr(Buf, ImpSymbol->getRVA(), RVA, 12);
692   applyArm64Ldr(Buf + 4, Off);
693 }
694 
695 // A Thumb2, PIC, non-interworking range extension thunk.
696 const uint8_t ArmThunk[] = {
697     0x40, 0xf2, 0x00, 0x0c, // P:  movw ip,:lower16:S - (P + (L1-P) + 4)
698     0xc0, 0xf2, 0x00, 0x0c, //     movt ip,:upper16:S - (P + (L1-P) + 4)
699     0xe7, 0x44,             // L1: add  pc, ip
700 };
701 
702 size_t RangeExtensionThunkARM::getSize() const {
703   assert(Config->Machine == ARMNT);
704   return sizeof(ArmThunk);
705 }
706 
707 void RangeExtensionThunkARM::writeTo(uint8_t *Buf) const {
708   assert(Config->Machine == ARMNT);
709   uint64_t Offset = Target->getRVA() - RVA - 12;
710   memcpy(Buf, ArmThunk, sizeof(ArmThunk));
711   applyMOV32T(Buf, uint32_t(Offset));
712 }
713 
714 // A position independent ARM64 adrp+add thunk, with a maximum range of
715 // +/- 4 GB, which is enough for any PE-COFF.
716 const uint8_t Arm64Thunk[] = {
717     0x10, 0x00, 0x00, 0x90, // adrp x16, Dest
718     0x10, 0x02, 0x00, 0x91, // add  x16, x16, :lo12:Dest
719     0x00, 0x02, 0x1f, 0xd6, // br   x16
720 };
721 
722 size_t RangeExtensionThunkARM64::getSize() const {
723   assert(Config->Machine == ARM64);
724   return sizeof(Arm64Thunk);
725 }
726 
727 void RangeExtensionThunkARM64::writeTo(uint8_t *Buf) const {
728   assert(Config->Machine == ARM64);
729   memcpy(Buf, Arm64Thunk, sizeof(Arm64Thunk));
730   applyArm64Addr(Buf + 0, Target->getRVA(), RVA, 12);
731   applyArm64Imm(Buf + 4, Target->getRVA() & 0xfff, 0);
732 }
733 
734 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
735   Res->emplace_back(getRVA());
736 }
737 
738 size_t LocalImportChunk::getSize() const { return Config->Wordsize; }
739 
740 void LocalImportChunk::writeTo(uint8_t *Buf) const {
741   if (Config->is64()) {
742     write64le(Buf, Sym->getRVA() + Config->ImageBase);
743   } else {
744     write32le(Buf, Sym->getRVA() + Config->ImageBase);
745   }
746 }
747 
748 void RVATableChunk::writeTo(uint8_t *Buf) const {
749   ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf);
750   size_t Cnt = 0;
751   for (const ChunkAndOffset &CO : Syms)
752     Begin[Cnt++] = CO.InputChunk->getRVA() + CO.Offset;
753   std::sort(Begin, Begin + Cnt);
754   assert(std::unique(Begin, Begin + Cnt) == Begin + Cnt &&
755          "RVA tables should be de-duplicated");
756 }
757 
758 // MinGW specific, for the "automatic import of variables from DLLs" feature.
759 size_t PseudoRelocTableChunk::getSize() const {
760   if (Relocs.empty())
761     return 0;
762   return 12 + 12 * Relocs.size();
763 }
764 
765 // MinGW specific.
766 void PseudoRelocTableChunk::writeTo(uint8_t *Buf) const {
767   if (Relocs.empty())
768     return;
769 
770   ulittle32_t *Table = reinterpret_cast<ulittle32_t *>(Buf);
771   // This is the list header, to signal the runtime pseudo relocation v2
772   // format.
773   Table[0] = 0;
774   Table[1] = 0;
775   Table[2] = 1;
776 
777   size_t Idx = 3;
778   for (const RuntimePseudoReloc &RPR : Relocs) {
779     Table[Idx + 0] = RPR.Sym->getRVA();
780     Table[Idx + 1] = RPR.Target->getRVA() + RPR.TargetOffset;
781     Table[Idx + 2] = RPR.Flags;
782     Idx += 3;
783   }
784 }
785 
786 // Windows-specific. This class represents a block in .reloc section.
787 // The format is described here.
788 //
789 // On Windows, each DLL is linked against a fixed base address and
790 // usually loaded to that address. However, if there's already another
791 // DLL that overlaps, the loader has to relocate it. To do that, DLLs
792 // contain .reloc sections which contain offsets that need to be fixed
793 // up at runtime. If the loader finds that a DLL cannot be loaded to its
794 // desired base address, it loads it to somewhere else, and add <actual
795 // base address> - <desired base address> to each offset that is
796 // specified by the .reloc section. In ELF terms, .reloc sections
797 // contain relative relocations in REL format (as opposed to RELA.)
798 //
799 // This already significantly reduces the size of relocations compared
800 // to ELF .rel.dyn, but Windows does more to reduce it (probably because
801 // it was invented for PCs in the late '80s or early '90s.)  Offsets in
802 // .reloc are grouped by page where the page size is 12 bits, and
803 // offsets sharing the same page address are stored consecutively to
804 // represent them with less space. This is very similar to the page
805 // table which is grouped by (multiple stages of) pages.
806 //
807 // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
808 // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
809 // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
810 // are represented like this:
811 //
812 //   0x00000  -- page address (4 bytes)
813 //   16       -- size of this block (4 bytes)
814 //     0xA030 -- entries (2 bytes each)
815 //     0xA500
816 //     0xA700
817 //     0xAA00
818 //   0x20000  -- page address (4 bytes)
819 //   12       -- size of this block (4 bytes)
820 //     0xA004 -- entries (2 bytes each)
821 //     0xA008
822 //
823 // Usually we have a lot of relocations for each page, so the number of
824 // bytes for one .reloc entry is close to 2 bytes on average.
825 BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
826   // Block header consists of 4 byte page RVA and 4 byte block size.
827   // Each entry is 2 byte. Last entry may be padding.
828   Data.resize(alignTo((End - Begin) * 2 + 8, 4));
829   uint8_t *P = Data.data();
830   write32le(P, Page);
831   write32le(P + 4, Data.size());
832   P += 8;
833   for (Baserel *I = Begin; I != End; ++I) {
834     write16le(P, (I->Type << 12) | (I->RVA - Page));
835     P += 2;
836   }
837 }
838 
839 void BaserelChunk::writeTo(uint8_t *Buf) const {
840   memcpy(Buf, Data.data(), Data.size());
841 }
842 
843 uint8_t Baserel::getDefaultType() {
844   switch (Config->Machine) {
845   case AMD64:
846   case ARM64:
847     return IMAGE_REL_BASED_DIR64;
848   case I386:
849   case ARMNT:
850     return IMAGE_REL_BASED_HIGHLOW;
851   default:
852     llvm_unreachable("unknown machine type");
853   }
854 }
855 
856 MergeChunk *MergeChunk::Instances[Log2MaxSectionAlignment + 1] = {};
857 
858 MergeChunk::MergeChunk(uint32_t Alignment)
859     : Builder(StringTableBuilder::RAW, Alignment) {
860   setAlignment(Alignment);
861 }
862 
863 void MergeChunk::addSection(SectionChunk *C) {
864   assert(isPowerOf2_32(C->getAlignment()));
865   uint8_t P2Align = llvm::Log2_32(C->getAlignment());
866   assert(P2Align < array_lengthof(Instances));
867   auto *&MC = Instances[P2Align];
868   if (!MC)
869     MC = make<MergeChunk>(C->getAlignment());
870   MC->Sections.push_back(C);
871 }
872 
873 void MergeChunk::finalizeContents() {
874   assert(!Finalized && "should only finalize once");
875   for (SectionChunk *C : Sections)
876     if (C->Live)
877       Builder.add(toStringRef(C->getContents()));
878   Builder.finalize();
879   Finalized = true;
880 }
881 
882 void MergeChunk::assignSubsectionRVAs() {
883   for (SectionChunk *C : Sections) {
884     if (!C->Live)
885       continue;
886     size_t Off = Builder.getOffset(toStringRef(C->getContents()));
887     C->setRVA(RVA + Off);
888   }
889 }
890 
891 uint32_t MergeChunk::getOutputCharacteristics() const {
892   return IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA;
893 }
894 
895 size_t MergeChunk::getSize() const {
896   return Builder.getSize();
897 }
898 
899 void MergeChunk::writeTo(uint8_t *Buf) const {
900   Builder.write(Buf);
901 }
902 
903 // MinGW specific.
904 size_t AbsolutePointerChunk::getSize() const { return Config->Wordsize; }
905 
906 void AbsolutePointerChunk::writeTo(uint8_t *Buf) const {
907   if (Config->is64()) {
908     write64le(Buf, Value);
909   } else {
910     write32le(Buf, Value);
911   }
912 }
913 
914 } // namespace coff
915 } // namespace lld
916