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