1 //===- Chunks.cpp ---------------------------------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 #include "InputFiles.h"
14 #include "SymbolTable.h"
15 #include "lld/Common/ErrorHandler.h"
16 #include "llvm/ADT/Twine.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"
25 using namespace llvm::object;
26 using namespace llvm::support::endian;
27 using namespace llvm::COFF;
28 using llvm::support::ulittle32_t;
33 SectionChunk::SectionChunk(ObjFile *F, const coff_section *H)
34 : Chunk(SectionKind), Repl(this), Header(H), File(F),
35 Relocs(File->getCOFFObj()->getRelocations(Header)) {
36 // Initialize SectionName.
37 File->getCOFFObj()->getSectionName(Header, SectionName);
39 Alignment = Header->getAlignment();
41 // If linker GC is disabled, every chunk starts out alive. If linker GC is
42 // enabled, treat non-comdat sections as roots. Generally optimized object
43 // files will be built with -ffunction-sections or /Gy, so most things worth
44 // stripping will be in a comdat.
45 Live = !Config->DoGC || !isCOMDAT();
48 // Initialize the RelocTargets vector, to allow redirecting certain relocations
49 // to a thunk instead of the actual symbol the relocation's symbol table index
51 void SectionChunk::readRelocTargets() {
52 assert(RelocTargets.empty());
53 RelocTargets.reserve(Relocs.size());
54 for (const coff_relocation &Rel : Relocs)
55 RelocTargets.push_back(File->getSymbol(Rel.SymbolTableIndex));
58 // Reset RelocTargets to their original targets before thunks were added.
59 void SectionChunk::resetRelocTargets() {
60 for (size_t I = 0, E = Relocs.size(); I < E; ++I)
61 RelocTargets[I] = File->getSymbol(Relocs[I].SymbolTableIndex);
64 static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
65 static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
66 static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
67 static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
68 static void or32(uint8_t *P, uint32_t V) { write32le(P, read32le(P) | V); }
70 // Verify that given sections are appropriate targets for SECREL
71 // relocations. This check is relaxed because unfortunately debug
72 // sections have section-relative relocations against absolute symbols.
73 static bool checkSecRel(const SectionChunk *Sec, OutputSection *OS) {
76 if (Sec->isCodeView())
78 error("SECREL relocation cannot be applied to absolute symbols");
82 static void applySecRel(const SectionChunk *Sec, uint8_t *Off,
83 OutputSection *OS, uint64_t S) {
84 if (!checkSecRel(Sec, OS))
86 uint64_t SecRel = S - OS->getRVA();
87 if (SecRel > UINT32_MAX) {
88 error("overflow in SECREL relocation in section: " + Sec->getSectionName());
94 static void applySecIdx(uint8_t *Off, OutputSection *OS) {
95 // Absolute symbol doesn't have section index, but section index relocation
96 // against absolute symbol should be resolved to one plus the last output
97 // section index. This is required for compatibility with MSVC.
99 add16(Off, OS->SectionIndex);
101 add16(Off, DefinedAbsolute::NumOutputSections + 1);
104 void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, OutputSection *OS,
105 uint64_t S, uint64_t P) const {
107 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
108 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
109 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
110 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
111 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
112 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
113 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
114 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
115 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
116 case IMAGE_REL_AMD64_SECTION: applySecIdx(Off, OS); break;
117 case IMAGE_REL_AMD64_SECREL: applySecRel(this, Off, OS, S); break;
119 error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
124 void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, OutputSection *OS,
125 uint64_t S, uint64_t P) const {
127 case IMAGE_REL_I386_ABSOLUTE: break;
128 case IMAGE_REL_I386_DIR32: add32(Off, S + Config->ImageBase); break;
129 case IMAGE_REL_I386_DIR32NB: add32(Off, S); break;
130 case IMAGE_REL_I386_REL32: add32(Off, S - P - 4); break;
131 case IMAGE_REL_I386_SECTION: applySecIdx(Off, OS); break;
132 case IMAGE_REL_I386_SECREL: applySecRel(this, Off, OS, S); break;
134 error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
139 static void applyMOV(uint8_t *Off, uint16_t V) {
140 write16le(Off, (read16le(Off) & 0xfbf0) | ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
141 write16le(Off + 2, (read16le(Off + 2) & 0x8f00) | ((V & 0x700) << 4) | (V & 0xff));
144 static uint16_t readMOV(uint8_t *Off, bool MOVT) {
145 uint16_t Op1 = read16le(Off);
146 if ((Op1 & 0xfbf0) != (MOVT ? 0xf2c0 : 0xf240))
147 error("unexpected instruction in " + Twine(MOVT ? "MOVT" : "MOVW") +
148 " instruction in MOV32T relocation");
149 uint16_t Op2 = read16le(Off + 2);
150 if ((Op2 & 0x8000) != 0)
151 error("unexpected instruction in " + Twine(MOVT ? "MOVT" : "MOVW") +
152 " instruction in MOV32T relocation");
153 return (Op2 & 0x00ff) | ((Op2 >> 4) & 0x0700) | ((Op1 << 1) & 0x0800) |
154 ((Op1 & 0x000f) << 12);
157 void applyMOV32T(uint8_t *Off, uint32_t V) {
158 uint16_t ImmW = readMOV(Off, false); // read MOVW operand
159 uint16_t ImmT = readMOV(Off + 4, true); // read MOVT operand
160 uint32_t Imm = ImmW | (ImmT << 16);
161 V += Imm; // add the immediate offset
162 applyMOV(Off, V); // set MOVW operand
163 applyMOV(Off + 4, V >> 16); // set MOVT operand
166 static void applyBranch20T(uint8_t *Off, int32_t V) {
168 error("relocation out of range");
169 uint32_t S = V < 0 ? 1 : 0;
170 uint32_t J1 = (V >> 19) & 1;
171 uint32_t J2 = (V >> 18) & 1;
172 or16(Off, (S << 10) | ((V >> 12) & 0x3f));
173 or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
176 void applyBranch24T(uint8_t *Off, int32_t V) {
178 error("relocation out of range");
179 uint32_t S = V < 0 ? 1 : 0;
180 uint32_t J1 = ((~V >> 23) & 1) ^ S;
181 uint32_t J2 = ((~V >> 22) & 1) ^ S;
182 or16(Off, (S << 10) | ((V >> 12) & 0x3ff));
183 // Clear out the J1 and J2 bits which may be set.
184 write16le(Off + 2, (read16le(Off + 2) & 0xd000) | (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
187 void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, OutputSection *OS,
188 uint64_t S, uint64_t P) const {
189 // Pointer to thumb code must have the LSB set.
191 if (OS && (OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE))
194 case IMAGE_REL_ARM_ADDR32: add32(Off, SX + Config->ImageBase); break;
195 case IMAGE_REL_ARM_ADDR32NB: add32(Off, SX); break;
196 case IMAGE_REL_ARM_MOV32T: applyMOV32T(Off, SX + Config->ImageBase); break;
197 case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, SX - P - 4); break;
198 case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, SX - P - 4); break;
199 case IMAGE_REL_ARM_BLX23T: applyBranch24T(Off, SX - P - 4); break;
200 case IMAGE_REL_ARM_SECTION: applySecIdx(Off, OS); break;
201 case IMAGE_REL_ARM_SECREL: applySecRel(this, Off, OS, S); break;
203 error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
208 // Interpret the existing immediate value as a byte offset to the
209 // target symbol, then update the instruction with the immediate as
210 // the page offset from the current instruction to the target.
211 void applyArm64Addr(uint8_t *Off, uint64_t S, uint64_t P, int Shift) {
212 uint32_t Orig = read32le(Off);
213 uint64_t Imm = ((Orig >> 29) & 0x3) | ((Orig >> 3) & 0x1FFFFC);
215 Imm = (S >> Shift) - (P >> Shift);
216 uint32_t ImmLo = (Imm & 0x3) << 29;
217 uint32_t ImmHi = (Imm & 0x1FFFFC) << 3;
218 uint64_t Mask = (0x3 << 29) | (0x1FFFFC << 3);
219 write32le(Off, (Orig & ~Mask) | ImmLo | ImmHi);
222 // Update the immediate field in a AARCH64 ldr, str, and add instruction.
223 // Optionally limit the range of the written immediate by one or more bits
225 void applyArm64Imm(uint8_t *Off, uint64_t Imm, uint32_t RangeLimit) {
226 uint32_t Orig = read32le(Off);
227 Imm += (Orig >> 10) & 0xFFF;
228 Orig &= ~(0xFFF << 10);
229 write32le(Off, Orig | ((Imm & (0xFFF >> RangeLimit)) << 10));
232 // Add the 12 bit page offset to the existing immediate.
233 // Ldr/str instructions store the opcode immediate scaled
234 // by the load/store size (giving a larger range for larger
235 // loads/stores). The immediate is always (both before and after
236 // fixing up the relocation) stored scaled similarly.
237 // Even if larger loads/stores have a larger range, limit the
238 // effective offset to 12 bit, since it is intended to be a
240 static void applyArm64Ldr(uint8_t *Off, uint64_t Imm) {
241 uint32_t Orig = read32le(Off);
242 uint32_t Size = Orig >> 30;
243 // 0x04000000 indicates SIMD/FP registers
244 // 0x00800000 indicates 128 bit
245 if ((Orig & 0x4800000) == 0x4800000)
247 if ((Imm & ((1 << Size) - 1)) != 0)
248 error("misaligned ldr/str offset");
249 applyArm64Imm(Off, Imm >> Size, Size);
252 static void applySecRelLow12A(const SectionChunk *Sec, uint8_t *Off,
253 OutputSection *OS, uint64_t S) {
254 if (checkSecRel(Sec, OS))
255 applyArm64Imm(Off, (S - OS->getRVA()) & 0xfff, 0);
258 static void applySecRelHigh12A(const SectionChunk *Sec, uint8_t *Off,
259 OutputSection *OS, uint64_t S) {
260 if (!checkSecRel(Sec, OS))
262 uint64_t SecRel = (S - OS->getRVA()) >> 12;
263 if (0xfff < SecRel) {
264 error("overflow in SECREL_HIGH12A relocation in section: " +
265 Sec->getSectionName());
268 applyArm64Imm(Off, SecRel & 0xfff, 0);
271 static void applySecRelLdr(const SectionChunk *Sec, uint8_t *Off,
272 OutputSection *OS, uint64_t S) {
273 if (checkSecRel(Sec, OS))
274 applyArm64Ldr(Off, (S - OS->getRVA()) & 0xfff);
277 void applyArm64Branch26(uint8_t *Off, int64_t V) {
279 error("relocation out of range");
280 or32(Off, (V & 0x0FFFFFFC) >> 2);
283 static void applyArm64Branch19(uint8_t *Off, int64_t V) {
285 error("relocation out of range");
286 or32(Off, (V & 0x001FFFFC) << 3);
289 static void applyArm64Branch14(uint8_t *Off, int64_t V) {
291 error("relocation out of range");
292 or32(Off, (V & 0x0000FFFC) << 3);
295 void SectionChunk::applyRelARM64(uint8_t *Off, uint16_t Type, OutputSection *OS,
296 uint64_t S, uint64_t P) const {
298 case IMAGE_REL_ARM64_PAGEBASE_REL21: applyArm64Addr(Off, S, P, 12); break;
299 case IMAGE_REL_ARM64_REL21: applyArm64Addr(Off, S, P, 0); break;
300 case IMAGE_REL_ARM64_PAGEOFFSET_12A: applyArm64Imm(Off, S & 0xfff, 0); break;
301 case IMAGE_REL_ARM64_PAGEOFFSET_12L: applyArm64Ldr(Off, S & 0xfff); break;
302 case IMAGE_REL_ARM64_BRANCH26: applyArm64Branch26(Off, S - P); break;
303 case IMAGE_REL_ARM64_BRANCH19: applyArm64Branch19(Off, S - P); break;
304 case IMAGE_REL_ARM64_BRANCH14: applyArm64Branch14(Off, S - P); break;
305 case IMAGE_REL_ARM64_ADDR32: add32(Off, S + Config->ImageBase); break;
306 case IMAGE_REL_ARM64_ADDR32NB: add32(Off, S); break;
307 case IMAGE_REL_ARM64_ADDR64: add64(Off, S + Config->ImageBase); break;
308 case IMAGE_REL_ARM64_SECREL: applySecRel(this, Off, OS, S); break;
309 case IMAGE_REL_ARM64_SECREL_LOW12A: applySecRelLow12A(this, Off, OS, S); break;
310 case IMAGE_REL_ARM64_SECREL_HIGH12A: applySecRelHigh12A(this, Off, OS, S); break;
311 case IMAGE_REL_ARM64_SECREL_LOW12L: applySecRelLdr(this, Off, OS, S); break;
312 case IMAGE_REL_ARM64_SECTION: applySecIdx(Off, OS); break;
314 error("unsupported relocation type 0x" + Twine::utohexstr(Type) + " in " +
319 static void maybeReportRelocationToDiscarded(const SectionChunk *FromChunk,
321 const coff_relocation &Rel) {
322 // Don't report these errors when the relocation comes from a debug info
323 // section or in mingw mode. MinGW mode object files (built by GCC) can
324 // have leftover sections with relocations against discarded comdat
325 // sections. Such sections are left as is, with relocations untouched.
326 if (FromChunk->isCodeView() || FromChunk->isDWARF() || Config->MinGW)
329 // Get the name of the symbol. If it's null, it was discarded early, so we
330 // have to go back to the object file.
331 ObjFile *File = FromChunk->File;
334 Name = Sym->getName();
336 COFFSymbolRef COFFSym =
337 check(File->getCOFFObj()->getSymbol(Rel.SymbolTableIndex));
338 File->getCOFFObj()->getSymbolName(COFFSym, Name);
341 error("relocation against symbol in discarded section: " + Name +
342 getSymbolLocations(File, Rel.SymbolTableIndex));
345 void SectionChunk::writeTo(uint8_t *Buf) const {
348 // Copy section contents from source object file to output file.
349 ArrayRef<uint8_t> A = getContents();
351 memcpy(Buf + OutputSectionOff, A.data(), A.size());
353 // Apply relocations.
354 size_t InputSize = getSize();
355 for (size_t I = 0, E = Relocs.size(); I < E; I++) {
356 const coff_relocation &Rel = Relocs[I];
358 // Check for an invalid relocation offset. This check isn't perfect, because
359 // we don't have the relocation size, which is only known after checking the
360 // machine and relocation type. As a result, a relocation may overwrite the
361 // beginning of the following input section.
362 if (Rel.VirtualAddress >= InputSize) {
363 error("relocation points beyond the end of its parent section");
367 uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress;
369 // Use the potentially remapped Symbol instead of the one that the
370 // relocation points to.
371 auto *Sym = dyn_cast_or_null<Defined>(RelocTargets[I]);
373 // Get the output section of the symbol for this relocation. The output
374 // section is needed to compute SECREL and SECTION relocations used in debug
376 Chunk *C = Sym ? Sym->getChunk() : nullptr;
377 OutputSection *OS = C ? C->getOutputSection() : nullptr;
379 // Skip the relocation if it refers to a discarded section, and diagnose it
380 // as an error if appropriate. If a symbol was discarded early, it may be
381 // null. If it was discarded late, the output section will be null, unless
382 // it was an absolute or synthetic symbol.
384 (!OS && !isa<DefinedAbsolute>(Sym) && !isa<DefinedSynthetic>(Sym))) {
385 maybeReportRelocationToDiscarded(this, Sym, Rel);
389 uint64_t S = Sym->getRVA();
391 // Compute the RVA of the relocation for relative relocations.
392 uint64_t P = RVA + Rel.VirtualAddress;
393 switch (Config->Machine) {
395 applyRelX64(Off, Rel.Type, OS, S, P);
398 applyRelX86(Off, Rel.Type, OS, S, P);
401 applyRelARM(Off, Rel.Type, OS, S, P);
404 applyRelARM64(Off, Rel.Type, OS, S, P);
407 llvm_unreachable("unknown machine type");
412 void SectionChunk::addAssociative(SectionChunk *Child) {
413 AssocChildren.push_back(Child);
416 static uint8_t getBaserelType(const coff_relocation &Rel) {
417 switch (Config->Machine) {
419 if (Rel.Type == IMAGE_REL_AMD64_ADDR64)
420 return IMAGE_REL_BASED_DIR64;
421 return IMAGE_REL_BASED_ABSOLUTE;
423 if (Rel.Type == IMAGE_REL_I386_DIR32)
424 return IMAGE_REL_BASED_HIGHLOW;
425 return IMAGE_REL_BASED_ABSOLUTE;
427 if (Rel.Type == IMAGE_REL_ARM_ADDR32)
428 return IMAGE_REL_BASED_HIGHLOW;
429 if (Rel.Type == IMAGE_REL_ARM_MOV32T)
430 return IMAGE_REL_BASED_ARM_MOV32T;
431 return IMAGE_REL_BASED_ABSOLUTE;
433 if (Rel.Type == IMAGE_REL_ARM64_ADDR64)
434 return IMAGE_REL_BASED_DIR64;
435 return IMAGE_REL_BASED_ABSOLUTE;
437 llvm_unreachable("unknown machine type");
442 // Collect all locations that contain absolute addresses, which need to be
443 // fixed by the loader if load-time relocation is needed.
444 // Only called when base relocation is enabled.
445 void SectionChunk::getBaserels(std::vector<Baserel> *Res) {
446 for (size_t I = 0, E = Relocs.size(); I < E; I++) {
447 const coff_relocation &Rel = Relocs[I];
448 uint8_t Ty = getBaserelType(Rel);
449 if (Ty == IMAGE_REL_BASED_ABSOLUTE)
451 // Use the potentially remapped Symbol instead of the one that the
452 // relocation points to.
453 Symbol *Target = RelocTargets[I];
454 if (!Target || isa<DefinedAbsolute>(Target))
456 Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
461 // Check whether a static relocation of type Type can be deferred and
462 // handled at runtime as a pseudo relocation (for references to a module
463 // local variable, which turned out to actually need to be imported from
464 // another DLL) This returns the size the relocation is supposed to update,
465 // in bits, or 0 if the relocation cannot be handled as a runtime pseudo
467 static int getRuntimePseudoRelocSize(uint16_t Type) {
468 // Relocations that either contain an absolute address, or a plain
469 // relative offset, since the runtime pseudo reloc implementation
470 // adds 8/16/32/64 bit values to a memory address.
472 // Given a pseudo relocation entry,
478 // } runtime_pseudo_reloc_item_v2;
480 // the runtime relocation performs this adjustment:
481 // *(base + .target) += *(base + .sym) - (base + .sym)
483 // This works for both absolute addresses (IMAGE_REL_*_ADDR32/64,
484 // IMAGE_REL_I386_DIR32, where the memory location initially contains
485 // the address of the IAT slot, and for relative addresses (IMAGE_REL*_REL32),
486 // where the memory location originally contains the relative offset to the
489 // This requires the target address to be writable, either directly out of
490 // the image, or temporarily changed at runtime with VirtualProtect.
491 // Since this only operates on direct address values, it doesn't work for
492 // ARM/ARM64 relocations, other than the plain ADDR32/ADDR64 relocations.
493 switch (Config->Machine) {
496 case IMAGE_REL_AMD64_ADDR64:
498 case IMAGE_REL_AMD64_ADDR32:
499 case IMAGE_REL_AMD64_REL32:
500 case IMAGE_REL_AMD64_REL32_1:
501 case IMAGE_REL_AMD64_REL32_2:
502 case IMAGE_REL_AMD64_REL32_3:
503 case IMAGE_REL_AMD64_REL32_4:
504 case IMAGE_REL_AMD64_REL32_5:
511 case IMAGE_REL_I386_DIR32:
512 case IMAGE_REL_I386_REL32:
519 case IMAGE_REL_ARM_ADDR32:
526 case IMAGE_REL_ARM64_ADDR64:
528 case IMAGE_REL_ARM64_ADDR32:
534 llvm_unreachable("unknown machine type");
539 // Append information to the provided vector about all relocations that
540 // need to be handled at runtime as runtime pseudo relocations (references
541 // to a module local variable, which turned out to actually need to be
542 // imported from another DLL).
543 void SectionChunk::getRuntimePseudoRelocs(
544 std::vector<RuntimePseudoReloc> &Res) {
545 for (const coff_relocation &Rel : Relocs) {
547 dyn_cast_or_null<Defined>(File->getSymbol(Rel.SymbolTableIndex));
548 if (!Target || !Target->IsRuntimePseudoReloc)
550 int SizeInBits = getRuntimePseudoRelocSize(Rel.Type);
551 if (SizeInBits == 0) {
552 error("unable to automatically import from " + Target->getName() +
553 " with relocation type " +
554 File->getCOFFObj()->getRelocationTypeName(Rel.Type) + " in " +
558 // SizeInBits is used to initialize the Flags field; currently no
559 // other flags are defined.
561 RuntimePseudoReloc(Target, this, Rel.VirtualAddress, SizeInBits));
565 bool SectionChunk::hasData() const {
566 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
569 uint32_t SectionChunk::getOutputCharacteristics() const {
570 return Header->Characteristics & (PermMask | TypeMask);
573 bool SectionChunk::isCOMDAT() const {
574 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
577 void SectionChunk::printDiscardedMessage() const {
578 // Removed by dead-stripping. If it's removed by ICF, ICF already
579 // printed out the name, so don't repeat that here.
580 if (Sym && this == Repl)
581 message("Discarded " + Sym->getName());
584 StringRef SectionChunk::getDebugName() {
586 return Sym->getName();
590 ArrayRef<uint8_t> SectionChunk::getContents() const {
592 File->getCOFFObj()->getSectionContents(Header, A);
596 void SectionChunk::replace(SectionChunk *Other) {
597 Alignment = std::max(Alignment, Other->Alignment);
602 uint32_t SectionChunk::getSectionNumber() const {
604 R.p = reinterpret_cast<uintptr_t>(Header);
605 SectionRef S(R, File->getCOFFObj());
606 return S.getIndex() + 1;
609 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
610 // Common symbols are aligned on natural boundaries up to 32 bytes.
611 // This is what MSVC link.exe does.
612 Alignment = std::min(uint64_t(32), PowerOf2Ceil(Sym.getValue()));
615 uint32_t CommonChunk::getOutputCharacteristics() const {
616 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
620 void StringChunk::writeTo(uint8_t *Buf) const {
621 memcpy(Buf + OutputSectionOff, Str.data(), Str.size());
622 Buf[OutputSectionOff + Str.size()] = '\0';
625 ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
626 // Intel Optimization Manual says that all branch targets
627 // should be 16-byte aligned. MSVC linker does this too.
631 void ImportThunkChunkX64::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, ImpSymbol->getRVA() - RVA - getSize());
637 void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
638 Res->emplace_back(getRVA() + 2);
641 void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
642 memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
643 // The first two bytes is a JMP instruction. Fill its operand.
644 write32le(Buf + OutputSectionOff + 2,
645 ImpSymbol->getRVA() + Config->ImageBase);
648 void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
649 Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
652 void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
653 memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM));
654 // Fix mov.w and mov.t operands.
655 applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase);
658 void ImportThunkChunkARM64::writeTo(uint8_t *Buf) const {
659 int64_t Off = ImpSymbol->getRVA() & 0xfff;
660 memcpy(Buf + OutputSectionOff, ImportThunkARM64, sizeof(ImportThunkARM64));
661 applyArm64Addr(Buf + OutputSectionOff, ImpSymbol->getRVA(), RVA, 12);
662 applyArm64Ldr(Buf + OutputSectionOff + 4, Off);
665 // A Thumb2, PIC, non-interworking range extension thunk.
666 const uint8_t ArmThunk[] = {
667 0x40, 0xf2, 0x00, 0x0c, // P: movw ip,:lower16:S - (P + (L1-P) + 4)
668 0xc0, 0xf2, 0x00, 0x0c, // movt ip,:upper16:S - (P + (L1-P) + 4)
669 0xe7, 0x44, // L1: add pc, ip
672 size_t RangeExtensionThunk::getSize() const {
673 assert(Config->Machine == ARMNT);
674 return sizeof(ArmThunk);
677 void RangeExtensionThunk::writeTo(uint8_t *Buf) const {
678 assert(Config->Machine == ARMNT);
679 uint64_t Offset = Target->getRVA() - RVA - 12;
680 memcpy(Buf + OutputSectionOff, ArmThunk, sizeof(ArmThunk));
681 applyMOV32T(Buf + OutputSectionOff, uint32_t(Offset));
684 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
685 Res->emplace_back(getRVA());
688 size_t LocalImportChunk::getSize() const { return Config->Wordsize; }
690 void LocalImportChunk::writeTo(uint8_t *Buf) const {
691 if (Config->is64()) {
692 write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
694 write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
698 void RVATableChunk::writeTo(uint8_t *Buf) const {
699 ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
701 for (const ChunkAndOffset &CO : Syms)
702 Begin[Cnt++] = CO.InputChunk->getRVA() + CO.Offset;
703 std::sort(Begin, Begin + Cnt);
704 assert(std::unique(Begin, Begin + Cnt) == Begin + Cnt &&
705 "RVA tables should be de-duplicated");
708 // MinGW specific, for the "automatic import of variables from DLLs" feature.
709 size_t PseudoRelocTableChunk::getSize() const {
712 return 12 + 12 * Relocs.size();
716 void PseudoRelocTableChunk::writeTo(uint8_t *Buf) const {
720 ulittle32_t *Table = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
721 // This is the list header, to signal the runtime pseudo relocation v2
728 for (const RuntimePseudoReloc &RPR : Relocs) {
729 Table[Idx + 0] = RPR.Sym->getRVA();
730 Table[Idx + 1] = RPR.Target->getRVA() + RPR.TargetOffset;
731 Table[Idx + 2] = RPR.Flags;
736 // Windows-specific. This class represents a block in .reloc section.
737 // The format is described here.
739 // On Windows, each DLL is linked against a fixed base address and
740 // usually loaded to that address. However, if there's already another
741 // DLL that overlaps, the loader has to relocate it. To do that, DLLs
742 // contain .reloc sections which contain offsets that need to be fixed
743 // up at runtime. If the loader finds that a DLL cannot be loaded to its
744 // desired base address, it loads it to somewhere else, and add <actual
745 // base address> - <desired base address> to each offset that is
746 // specified by the .reloc section. In ELF terms, .reloc sections
747 // contain relative relocations in REL format (as opposed to RELA.)
749 // This already significantly reduces the size of relocations compared
750 // to ELF .rel.dyn, but Windows does more to reduce it (probably because
751 // it was invented for PCs in the late '80s or early '90s.) Offsets in
752 // .reloc are grouped by page where the page size is 12 bits, and
753 // offsets sharing the same page address are stored consecutively to
754 // represent them with less space. This is very similar to the page
755 // table which is grouped by (multiple stages of) pages.
757 // For example, let's say we have 0x00030, 0x00500, 0x00700, 0x00A00,
758 // 0x20004, and 0x20008 in a .reloc section for x64. The uppermost 4
759 // bits have a type IMAGE_REL_BASED_DIR64 or 0xA. In the section, they
760 // are represented like this:
762 // 0x00000 -- page address (4 bytes)
763 // 16 -- size of this block (4 bytes)
764 // 0xA030 -- entries (2 bytes each)
768 // 0x20000 -- page address (4 bytes)
769 // 12 -- size of this block (4 bytes)
770 // 0xA004 -- entries (2 bytes each)
773 // Usually we have a lot of relocations for each page, so the number of
774 // bytes for one .reloc entry is close to 2 bytes on average.
775 BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
776 // Block header consists of 4 byte page RVA and 4 byte block size.
777 // Each entry is 2 byte. Last entry may be padding.
778 Data.resize(alignTo((End - Begin) * 2 + 8, 4));
779 uint8_t *P = Data.data();
781 write32le(P + 4, Data.size());
783 for (Baserel *I = Begin; I != End; ++I) {
784 write16le(P, (I->Type << 12) | (I->RVA - Page));
789 void BaserelChunk::writeTo(uint8_t *Buf) const {
790 memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
793 uint8_t Baserel::getDefaultType() {
794 switch (Config->Machine) {
797 return IMAGE_REL_BASED_DIR64;
800 return IMAGE_REL_BASED_HIGHLOW;
802 llvm_unreachable("unknown machine type");
806 std::map<uint32_t, MergeChunk *> MergeChunk::Instances;
808 MergeChunk::MergeChunk(uint32_t Alignment)
809 : Builder(StringTableBuilder::RAW, Alignment) {
810 this->Alignment = Alignment;
813 void MergeChunk::addSection(SectionChunk *C) {
814 auto *&MC = Instances[C->Alignment];
816 MC = make<MergeChunk>(C->Alignment);
817 MC->Sections.push_back(C);
820 void MergeChunk::finalizeContents() {
822 for (SectionChunk *C : Sections)
824 Builder.add(toStringRef(C->getContents()));
829 for (SectionChunk *C : Sections) {
832 size_t Off = Builder.getOffset(toStringRef(C->getContents()));
833 C->setOutputSection(Out);
834 C->setRVA(RVA + Off);
835 C->OutputSectionOff = OutputSectionOff + Off;
839 uint32_t MergeChunk::getOutputCharacteristics() const {
840 return IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA;
843 size_t MergeChunk::getSize() const {
844 return Builder.getSize();
847 void MergeChunk::writeTo(uint8_t *Buf) const {
848 Builder.write(Buf + OutputSectionOff);
852 size_t AbsolutePointerChunk::getSize() const { return Config->Wordsize; }
854 void AbsolutePointerChunk::writeTo(uint8_t *Buf) const {
855 if (Config->is64()) {
856 write64le(Buf + OutputSectionOff, Value);
858 write32le(Buf + OutputSectionOff, Value);