xref: /llvm-project-15.0.7/lld/COFF/Chunks.cpp (revision d2793a03)
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 "Error.h"
12 #include "InputFiles.h"
13 #include "Symbols.h"
14 #include "llvm/Object/COFF.h"
15 #include "llvm/Support/COFF.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/Endian.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <algorithm>
20 
21 using namespace llvm;
22 using namespace llvm::object;
23 using namespace llvm::support::endian;
24 using namespace llvm::COFF;
25 using llvm::support::ulittle32_t;
26 
27 namespace lld {
28 namespace coff {
29 
30 SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H)
31     : Chunk(SectionKind), Repl(this), File(F), Header(H),
32       Relocs(File->getCOFFObj()->getRelocations(Header)),
33       NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
34   // Initialize SectionName.
35   File->getCOFFObj()->getSectionName(Header, SectionName);
36 
37   // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
38   unsigned Shift = (Header->Characteristics >> 20) & 0xF;
39   if (Shift > 0)
40     Align = uint32_t(1) << (Shift - 1);
41 
42   // Only COMDAT sections are subject of dead-stripping.
43   Live = !isCOMDAT();
44 }
45 
46 static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
47 static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
48 static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
49 static void or16(uint8_t *P, uint16_t V) { write16le(P, read16le(P) | V); }
50 
51 void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, Defined *Sym,
52                                uint64_t P) const {
53   uint64_t S = Sym->getRVA();
54   switch (Type) {
55   case IMAGE_REL_AMD64_ADDR32:   add32(Off, S + Config->ImageBase); break;
56   case IMAGE_REL_AMD64_ADDR64:   add64(Off, S + Config->ImageBase); break;
57   case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
58   case IMAGE_REL_AMD64_REL32:    add32(Off, S - P - 4); break;
59   case IMAGE_REL_AMD64_REL32_1:  add32(Off, S - P - 5); break;
60   case IMAGE_REL_AMD64_REL32_2:  add32(Off, S - P - 6); break;
61   case IMAGE_REL_AMD64_REL32_3:  add32(Off, S - P - 7); break;
62   case IMAGE_REL_AMD64_REL32_4:  add32(Off, S - P - 8); break;
63   case IMAGE_REL_AMD64_REL32_5:  add32(Off, S - P - 9); break;
64   case IMAGE_REL_AMD64_SECTION:  add16(Off, Sym->getSectionIndex()); break;
65   case IMAGE_REL_AMD64_SECREL:   add32(Off, Sym->getSecrel()); break;
66   default:
67     error("Unsupported relocation type");
68   }
69 }
70 
71 void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, Defined *Sym,
72                                uint64_t P) const {
73   uint64_t S = Sym->getRVA();
74   switch (Type) {
75   case IMAGE_REL_I386_ABSOLUTE: break;
76   case IMAGE_REL_I386_DIR32:    add32(Off, S + Config->ImageBase); break;
77   case IMAGE_REL_I386_DIR32NB:  add32(Off, S); break;
78   case IMAGE_REL_I386_REL32:    add32(Off, S - P - 4); break;
79   case IMAGE_REL_I386_SECTION:  add16(Off, Sym->getSectionIndex()); break;
80   case IMAGE_REL_I386_SECREL:   add32(Off, Sym->getSecrel()); break;
81   default:
82     error("Unsupported relocation type");
83   }
84 }
85 
86 static void applyMOV(uint8_t *Off, uint16_t V) {
87   or16(Off, ((V & 0x800) >> 1) | ((V >> 12) & 0xf));
88   or16(Off + 2, ((V & 0x700) << 4) | (V & 0xff));
89 }
90 
91 static void applyMOV32T(uint8_t *Off, uint32_t V) {
92   applyMOV(Off, V);           // set MOVW operand
93   applyMOV(Off + 4, V >> 16); // set MOVT operand
94 }
95 
96 static void applyBranch20T(uint8_t *Off, int32_t V) {
97   uint32_t S = V < 0 ? 1 : 0;
98   uint32_t J1 = (V >> 19) & 1;
99   uint32_t J2 = (V >> 18) & 1;
100   or16(Off, (S << 10) | ((V >> 12) & 0x3f));
101   or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
102 }
103 
104 static void applyBranch24T(uint8_t *Off, int32_t V) {
105   uint32_t S = V < 0 ? 1 : 0;
106   uint32_t J1 = ((~V >> 23) & 1) ^ S;
107   uint32_t J2 = ((~V >> 22) & 1) ^ S;
108   or16(Off, (S << 10) | ((V >> 12) & 0x3ff));
109   or16(Off + 2, (J1 << 13) | (J2 << 11) | ((V >> 1) & 0x7ff));
110 }
111 
112 void SectionChunk::applyRelARM(uint8_t *Off, uint16_t Type, Defined *Sym,
113                                uint64_t P) const {
114   uint64_t S = Sym->getRVA();
115   // Pointer to thumb code must have the LSB set.
116   if (Sym->isExecutable())
117     S |= 1;
118   switch (Type) {
119   case IMAGE_REL_ARM_ADDR32:    add32(Off, S + Config->ImageBase); break;
120   case IMAGE_REL_ARM_ADDR32NB:  add32(Off, S); break;
121   case IMAGE_REL_ARM_MOV32T:    applyMOV32T(Off, S + Config->ImageBase); break;
122   case IMAGE_REL_ARM_BRANCH20T: applyBranch20T(Off, S - P - 4); break;
123   case IMAGE_REL_ARM_BRANCH24T: applyBranch24T(Off, S - P - 4); break;
124   case IMAGE_REL_ARM_BLX23T:    applyBranch24T(Off, S - P - 4); break;
125   default:
126     error("Unsupported relocation type");
127   }
128 }
129 
130 void SectionChunk::writeTo(uint8_t *Buf) const {
131   if (!hasData())
132     return;
133   // Copy section contents from source object file to output file.
134   ArrayRef<uint8_t> A = getContents();
135   memcpy(Buf + OutputSectionOff, A.data(), A.size());
136 
137   // Apply relocations.
138   for (const coff_relocation &Rel : Relocs) {
139     uint8_t *Off = Buf + OutputSectionOff + Rel.VirtualAddress;
140     SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
141     Defined *Sym = cast<Defined>(Body);
142     uint64_t P = RVA + Rel.VirtualAddress;
143     switch (Config->Machine) {
144     case AMD64:
145       applyRelX64(Off, Rel.Type, Sym, P);
146       break;
147     case I386:
148       applyRelX86(Off, Rel.Type, Sym, P);
149       break;
150     case ARMNT:
151       applyRelARM(Off, Rel.Type, Sym, P);
152       break;
153     default:
154       llvm_unreachable("unknown machine type");
155     }
156   }
157 }
158 
159 void SectionChunk::addAssociative(SectionChunk *Child) {
160   AssocChildren.push_back(Child);
161 }
162 
163 static uint8_t getBaserelType(const coff_relocation &Rel) {
164   switch (Config->Machine) {
165   case AMD64:
166     if (Rel.Type == IMAGE_REL_AMD64_ADDR64)
167       return IMAGE_REL_BASED_DIR64;
168     return IMAGE_REL_BASED_ABSOLUTE;
169   case I386:
170     if (Rel.Type == IMAGE_REL_I386_DIR32)
171       return IMAGE_REL_BASED_HIGHLOW;
172     return IMAGE_REL_BASED_ABSOLUTE;
173   case ARMNT:
174     if (Rel.Type == IMAGE_REL_ARM_ADDR32)
175       return IMAGE_REL_BASED_HIGHLOW;
176     if (Rel.Type == IMAGE_REL_ARM_MOV32T)
177       return IMAGE_REL_BASED_ARM_MOV32T;
178     return IMAGE_REL_BASED_ABSOLUTE;
179   default:
180     llvm_unreachable("unknown machine type");
181   }
182 }
183 
184 // Windows-specific.
185 // Collect all locations that contain absolute addresses, which need to be
186 // fixed by the loader if load-time relocation is needed.
187 // Only called when base relocation is enabled.
188 void SectionChunk::getBaserels(std::vector<Baserel> *Res) {
189   for (const coff_relocation &Rel : Relocs) {
190     uint8_t Ty = getBaserelType(Rel);
191     if (Ty == IMAGE_REL_BASED_ABSOLUTE)
192       continue;
193     SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
194     if (isa<DefinedAbsolute>(Body))
195       continue;
196     Res->emplace_back(RVA + Rel.VirtualAddress, Ty);
197   }
198 }
199 
200 bool SectionChunk::hasData() const {
201   return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
202 }
203 
204 uint32_t SectionChunk::getPermissions() const {
205   return Header->Characteristics & PermMask;
206 }
207 
208 bool SectionChunk::isCOMDAT() const {
209   return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
210 }
211 
212 void SectionChunk::printDiscardedMessage() const {
213   // Removed by dead-stripping. If it's removed by ICF, ICF already
214   // printed out the name, so don't repeat that here.
215   if (Sym && this == Repl)
216     llvm::outs() << "Discarded " << Sym->getName() << "\n";
217 }
218 
219 StringRef SectionChunk::getDebugName() {
220   if (Sym)
221     return Sym->getName();
222   return "";
223 }
224 
225 ArrayRef<uint8_t> SectionChunk::getContents() const {
226   ArrayRef<uint8_t> A;
227   File->getCOFFObj()->getSectionContents(Header, A);
228   return A;
229 }
230 
231 void SectionChunk::replace(SectionChunk *Other) {
232   Other->Repl = Repl;
233   Other->Live = false;
234 }
235 
236 CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
237   // Common symbols are aligned on natural boundaries up to 32 bytes.
238   // This is what MSVC link.exe does.
239   Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
240 }
241 
242 uint32_t CommonChunk::getPermissions() const {
243   return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
244          IMAGE_SCN_MEM_WRITE;
245 }
246 
247 void StringChunk::writeTo(uint8_t *Buf) const {
248   memcpy(Buf + OutputSectionOff, Str.data(), Str.size());
249 }
250 
251 ImportThunkChunkX64::ImportThunkChunkX64(Defined *S) : ImpSymbol(S) {
252   // Intel Optimization Manual says that all branch targets
253   // should be 16-byte aligned. MSVC linker does this too.
254   Align = 16;
255 }
256 
257 void ImportThunkChunkX64::writeTo(uint8_t *Buf) const {
258   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
259   // The first two bytes is a JMP instruction. Fill its operand.
260   write32le(Buf + OutputSectionOff + 2, ImpSymbol->getRVA() - RVA - getSize());
261 }
262 
263 void ImportThunkChunkX86::getBaserels(std::vector<Baserel> *Res) {
264   Res->emplace_back(getRVA() + 2);
265 }
266 
267 void ImportThunkChunkX86::writeTo(uint8_t *Buf) const {
268   memcpy(Buf + OutputSectionOff, ImportThunkX86, sizeof(ImportThunkX86));
269   // The first two bytes is a JMP instruction. Fill its operand.
270   write32le(Buf + OutputSectionOff + 2,
271             ImpSymbol->getRVA() + Config->ImageBase);
272 }
273 
274 void ImportThunkChunkARM::getBaserels(std::vector<Baserel> *Res) {
275   Res->emplace_back(getRVA(), IMAGE_REL_BASED_ARM_MOV32T);
276 }
277 
278 void ImportThunkChunkARM::writeTo(uint8_t *Buf) const {
279   memcpy(Buf + OutputSectionOff, ImportThunkARM, sizeof(ImportThunkARM));
280   // Fix mov.w and mov.t operands.
281   applyMOV32T(Buf + OutputSectionOff, ImpSymbol->getRVA() + Config->ImageBase);
282 }
283 
284 void LocalImportChunk::getBaserels(std::vector<Baserel> *Res) {
285   Res->emplace_back(getRVA());
286 }
287 
288 size_t LocalImportChunk::getSize() const {
289   return Config->is64() ? 8 : 4;
290 }
291 
292 void LocalImportChunk::writeTo(uint8_t *Buf) const {
293   if (Config->is64()) {
294     write64le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
295   } else {
296     write32le(Buf + OutputSectionOff, Sym->getRVA() + Config->ImageBase);
297   }
298 }
299 
300 void SEHTableChunk::writeTo(uint8_t *Buf) const {
301   ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + OutputSectionOff);
302   size_t Cnt = 0;
303   for (Defined *D : Syms)
304     Begin[Cnt++] = D->getRVA();
305   std::sort(Begin, Begin + Cnt);
306 }
307 
308 // Windows-specific.
309 // This class represents a block in .reloc section.
310 BaserelChunk::BaserelChunk(uint32_t Page, Baserel *Begin, Baserel *End) {
311   // Block header consists of 4 byte page RVA and 4 byte block size.
312   // Each entry is 2 byte. Last entry may be padding.
313   Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
314   uint8_t *P = Data.data();
315   write32le(P, Page);
316   write32le(P + 4, Data.size());
317   P += 8;
318   for (Baserel *I = Begin; I != End; ++I) {
319     write16le(P, (I->Type << 12) | (I->RVA - Page));
320     P += 2;
321   }
322 }
323 
324 void BaserelChunk::writeTo(uint8_t *Buf) const {
325   memcpy(Buf + OutputSectionOff, Data.data(), Data.size());
326 }
327 
328 uint8_t Baserel::getDefaultType() {
329   switch (Config->Machine) {
330   case AMD64:
331     return IMAGE_REL_BASED_DIR64;
332   case I386:
333     return IMAGE_REL_BASED_HIGHLOW;
334   default:
335     llvm_unreachable("unknown machine type");
336   }
337 }
338 
339 } // namespace coff
340 } // namespace lld
341