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