blob: 30e52963da0f935fee102ee00b387b1e563fdbfc [file] [log] [blame]
Rui Ueyama411c63602015-05-28 19:09:30 +00001//===- 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 "Writer.h"
Rui Ueyamaddf71fc2015-06-24 04:36:52 +000013#include "llvm/ADT/Hashing.h"
Rui Ueyama411c63602015-05-28 19:09:30 +000014#include "llvm/ADT/STLExtras.h"
15#include "llvm/Object/COFF.h"
16#include "llvm/Support/COFF.h"
17#include "llvm/Support/Debug.h"
18#include "llvm/Support/Endian.h"
19#include "llvm/Support/raw_ostream.h"
Rui Ueyama5e31d0b2015-06-20 07:25:45 +000020#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000021
Rui Ueyamac6ea0572015-06-06 22:46:15 +000022using namespace llvm;
Rui Ueyama411c63602015-05-28 19:09:30 +000023using namespace llvm::object;
24using namespace llvm::support::endian;
25using namespace llvm::COFF;
Rui Ueyamacd3f99b2015-07-24 23:51:14 +000026using llvm::support::ulittle32_t;
Rui Ueyama411c63602015-05-28 19:09:30 +000027
28namespace lld {
29namespace coff {
30
Peter Collingbournebd3a29d2015-06-24 00:12:36 +000031SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H)
Rui Ueyama9b921e52015-06-25 22:00:42 +000032 : Chunk(SectionKind), Ptr(this), File(F), Header(H),
Rui Ueyama02c30272015-06-25 17:43:37 +000033 Relocs(File->getCOFFObj()->getRelocations(Header)),
34 NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
Rui Ueyama411c63602015-05-28 19:09:30 +000035 // Initialize SectionName.
36 File->getCOFFObj()->getSectionName(Header, SectionName);
Rui Ueyama8b33f592015-06-10 04:21:47 +000037
Rui Ueyama2bf6a122015-06-14 21:50:50 +000038 // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
39 unsigned Shift = (Header->Characteristics >> 20) & 0xF;
40 if (Shift > 0)
41 Align = uint32_t(1) << (Shift - 1);
Rui Ueyama8b33f592015-06-10 04:21:47 +000042
Rui Ueyama8b33f592015-06-10 04:21:47 +000043 // COMDAT sections are not GC root. Non-text sections are not
44 // subject of garbage collection (thus they are root).
Rui Ueyamafc510f42015-06-25 19:10:58 +000045 Root = !isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
Rui Ueyama411c63602015-05-28 19:09:30 +000046}
47
Rui Ueyama42aa00b2015-06-25 00:33:38 +000048static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
49static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
50static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
51
Rui Ueyama661a4e7a2015-07-07 22:49:21 +000052void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, uint64_t S,
53 uint64_t P) {
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;
David Majnemer2c345a32015-07-08 16:37:50 +000064 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->SectionIndex); break;
Rui Ueyama661a4e7a2015-07-07 22:49:21 +000065 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
66 default:
67 llvm::report_fatal_error("Unsupported relocation type");
68 }
69}
70
Rui Ueyama11863b4a2015-07-08 01:45:29 +000071void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, uint64_t S,
72 uint64_t P) {
73 switch (Type) {
74 case IMAGE_REL_I386_ABSOLUTE: break;
75 case IMAGE_REL_I386_DIR32: add32(Off, S + Config->ImageBase); break;
76 case IMAGE_REL_I386_DIR32NB: add32(Off, S); break;
77 case IMAGE_REL_I386_REL32: add32(Off, S - P - 4); break;
David Majnemer2c345a32015-07-08 16:37:50 +000078 case IMAGE_REL_I386_SECTION: add16(Off, Out->SectionIndex); break;
Rui Ueyama11863b4a2015-07-08 01:45:29 +000079 case IMAGE_REL_I386_SECREL: add32(Off, S - Out->getRVA()); break;
80 default:
81 llvm::report_fatal_error("Unsupported relocation type");
82 }
83}
84
Rui Ueyamad6fefba42015-05-28 19:45:43 +000085void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000086 if (!hasData())
87 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000088 // Copy section contents from source object file to output file.
Rui Ueyamaf34c0882015-06-25 17:56:36 +000089 ArrayRef<uint8_t> A = getContents();
90 memcpy(Buf + FileOff, A.data(), A.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000091
92 // Apply relocations.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000093 for (const coff_relocation &Rel : Relocs) {
94 uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
Rui Ueyama0744e872015-07-02 00:21:11 +000095 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
Rui Ueyama42aa00b2015-06-25 00:33:38 +000096 uint64_t S = cast<Defined>(Body)->getRVA();
97 uint64_t P = RVA + Rel.VirtualAddress;
Rui Ueyama11863b4a2015-07-08 01:45:29 +000098 switch (Config->MachineType) {
99 case IMAGE_FILE_MACHINE_AMD64:
100 applyRelX64(Off, Rel.Type, S, P);
101 break;
102 case IMAGE_FILE_MACHINE_I386:
103 applyRelX86(Off, Rel.Type, S, P);
104 break;
105 default:
106 llvm_unreachable("unknown machine type");
107 }
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000108 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000109}
110
Rui Ueyama411c63602015-05-28 19:09:30 +0000111void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000112 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000113 // Associative sections are live if their parent COMDATs are live,
114 // and vice versa, so they are not considered live by themselves.
115 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000116}
117
Rui Ueyama93b45712015-07-09 20:36:59 +0000118static bool isAbs(const coff_relocation &Rel) {
119 switch (Config->MachineType) {
120 case IMAGE_FILE_MACHINE_AMD64:
121 return Rel.Type == IMAGE_REL_AMD64_ADDR64;
122 case IMAGE_FILE_MACHINE_I386:
123 return Rel.Type == IMAGE_REL_I386_DIR32;
124 default:
125 llvm_unreachable("unknown machine type");
126 }
127}
128
Rui Ueyama588e8322015-06-15 01:23:58 +0000129// Windows-specific.
Rui Ueyama93b45712015-07-09 20:36:59 +0000130// Collect all locations that contain absolute addresses, which need to be
131// fixed by the loader if load-time relocation is needed.
Rui Ueyama588e8322015-06-15 01:23:58 +0000132// Only called when base relocation is enabled.
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000133void SectionChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000134 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama93b45712015-07-09 20:36:59 +0000135 if (!isAbs(Rel))
Rui Ueyama588e8322015-06-15 01:23:58 +0000136 continue;
Rui Ueyama0744e872015-07-02 00:21:11 +0000137 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000138 if (isa<DefinedAbsolute>(Body))
Rui Ueyama588e8322015-06-15 01:23:58 +0000139 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000140 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000141 }
142}
143
Rui Ueyama411c63602015-05-28 19:09:30 +0000144bool SectionChunk::hasData() const {
145 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
146}
147
148uint32_t SectionChunk::getPermissions() const {
149 return Header->Characteristics & PermMask;
150}
151
152bool SectionChunk::isCOMDAT() const {
153 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
154}
155
Rui Ueyamafc510f42015-06-25 19:10:58 +0000156void SectionChunk::printDiscardedMessage() const {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000157 if (this == Ptr) {
158 // Removed by dead-stripping.
159 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
160 } else {
161 // Removed by ICF.
162 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
163 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000164}
165
Rui Ueyama6a60be72015-06-24 00:00:52 +0000166StringRef SectionChunk::getDebugName() {
167 return Sym->getName();
168}
169
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000170uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000171 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000172 return hash_combine(getPermissions(),
173 llvm::hash_value(SectionName),
174 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000175 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000176 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000177 hash_combine_range(A.data(), A.data() + A.size()));
178}
179
180// Returns true if this and a given chunk are identical COMDAT sections.
181bool SectionChunk::equals(const SectionChunk *X) const {
182 // Compare headers
183 if (getPermissions() != X->getPermissions())
184 return false;
185 if (SectionName != X->SectionName)
186 return false;
187 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
188 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000189 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000190 return false;
191
192 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000193 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000194 return false;
195
Rui Ueyama871847e2015-06-28 01:30:54 +0000196 // Compare associative sections
197 if (AssocChildren.size() != X->AssocChildren.size())
198 return false;
199 for (size_t I = 0, E = AssocChildren.size(); I != E; ++I)
200 if (AssocChildren[I]->Ptr != X->AssocChildren[I]->Ptr)
201 return false;
202
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000203 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000204 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
205 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000206 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000207 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000208 return false;
Rui Ueyama0744e872015-07-02 00:21:11 +0000209 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex)->repl();
210 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex)->repl();
Rui Ueyama871847e2015-06-28 01:30:54 +0000211 if (B1 == B2)
212 return true;
Rui Ueyama9b921e52015-06-25 22:00:42 +0000213 auto *D1 = dyn_cast<DefinedRegular>(B1);
214 auto *D2 = dyn_cast<DefinedRegular>(B2);
Rui Ueyama871847e2015-06-28 01:30:54 +0000215 return (D1 && D2 &&
216 D1->getValue() == D2->getValue() &&
217 D1->getChunk() == D2->getChunk());
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000218 };
219 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000220}
221
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000222ArrayRef<uint8_t> SectionChunk::getContents() const {
223 ArrayRef<uint8_t> A;
224 File->getCOFFObj()->getSectionContents(Header, A);
225 return A;
226}
227
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000228void SectionChunk::replaceWith(SectionChunk *Other) {
Rui Ueyama9b921e52015-06-25 22:00:42 +0000229 Ptr = Other->Ptr;
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000230 Live = false;
231}
232
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000233CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000234 // Common symbols are aligned on natural boundaries up to 32 bytes.
235 // This is what MSVC link.exe does.
236 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000237}
238
Rui Ueyama411c63602015-05-28 19:09:30 +0000239uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000240 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
241 IMAGE_SCN_MEM_WRITE;
242}
243
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000244void StringChunk::writeTo(uint8_t *Buf) {
245 memcpy(Buf + FileOff, Str.data(), Str.size());
246}
247
Rui Ueyama73835622015-06-26 18:28:56 +0000248ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) {
249 // Intel Optimization Manual says that all branch targets
250 // should be 16-byte aligned. MSVC linker does this too.
251 Align = 16;
252}
253
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000254void ImportThunkChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyama33fb2cb2015-07-15 00:25:38 +0000255 if (!Config->is64())
256 Res->push_back(getRVA() + 2);
257}
258
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000259void ImportThunkChunk::writeTo(uint8_t *Buf) {
260 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000261 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000262 uint32_t Operand = Config->is64()
263 ? ImpSymbol->getRVA() - RVA - getSize()
264 : ImpSymbol->getRVA() + Config->ImageBase;
Rui Ueyama411c63602015-05-28 19:09:30 +0000265 write32le(Buf + FileOff + 2, Operand);
266}
267
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000268void LocalImportChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyamac851ccc2015-07-10 04:30:54 +0000269 Res->push_back(getRVA());
Rui Ueyama7a333c62015-07-02 20:33:50 +0000270}
271
Rui Ueyamad4b351f2015-07-09 21:15:58 +0000272size_t LocalImportChunk::getSize() const {
273 return Config->is64() ? 8 : 4;
274}
275
Rui Ueyama7a333c62015-07-02 20:33:50 +0000276void LocalImportChunk::writeTo(uint8_t *Buf) {
Rui Ueyamad4b351f2015-07-09 21:15:58 +0000277 if (Config->is64()) {
278 write64le(Buf + FileOff, Sym->getRVA() + Config->ImageBase);
279 } else {
280 write32le(Buf + FileOff, Sym->getRVA() + Config->ImageBase);
281 }
Rui Ueyama7a333c62015-07-02 20:33:50 +0000282}
283
Rui Ueyamacd3f99b2015-07-24 23:51:14 +0000284void SEHTableChunk::writeTo(uint8_t *Buf) {
285 ulittle32_t *Begin = reinterpret_cast<ulittle32_t *>(Buf + FileOff);
286 size_t Cnt = 0;
287 for (Defined *D : Syms)
288 Begin[Cnt++] = D->getRVA();
289 std::sort(Begin, Begin + Cnt);
290}
291
Rui Ueyama588e8322015-06-15 01:23:58 +0000292// Windows-specific.
293// This class represents a block in .reloc section.
294BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
295 // Block header consists of 4 byte page RVA and 4 byte block size.
296 // Each entry is 2 byte. Last entry may be padding.
297 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
298 uint8_t *P = Data.data();
299 write32le(P, Page);
300 write32le(P + 4, Data.size());
301 P += 8;
Rui Ueyama2296dc12015-07-24 23:24:45 +0000302 uint16_t RelTy =
303 Config->is64() ? IMAGE_REL_BASED_DIR64 : IMAGE_REL_BASED_HIGHLOW;
Rui Ueyama588e8322015-06-15 01:23:58 +0000304 for (uint32_t *I = Begin; I != End; ++I) {
Rui Ueyama2296dc12015-07-24 23:24:45 +0000305 write16le(P, (RelTy << 12) | (*I - Page));
Rui Ueyama588e8322015-06-15 01:23:58 +0000306 P += 2;
307 }
308}
309
310void BaserelChunk::writeTo(uint8_t *Buf) {
311 memcpy(Buf + FileOff, Data.data(), Data.size());
312}
313
Rui Ueyama411c63602015-05-28 19:09:30 +0000314} // namespace coff
315} // namespace lld