blob: fde24070bb92122901776867b5d4b6cbf76ae93e [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 Ueyama411c63602015-05-28 19:09:30 +000026
27namespace lld {
28namespace coff {
29
Peter Collingbournebd3a29d2015-06-24 00:12:36 +000030SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H)
Rui Ueyama42aa00b2015-06-25 00:33:38 +000031 : File(F), Ptr(this), Header(H),
Rui Ueyama02c30272015-06-25 17:43:37 +000032 Relocs(File->getCOFFObj()->getRelocations(Header)),
33 NumRelocs(std::distance(Relocs.begin(), Relocs.end())) {
Rui Ueyama411c63602015-05-28 19:09:30 +000034 // Initialize SectionName.
35 File->getCOFFObj()->getSectionName(Header, SectionName);
Rui Ueyama8b33f592015-06-10 04:21:47 +000036
Rui Ueyama2bf6a122015-06-14 21:50:50 +000037 // 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);
Rui Ueyama8b33f592015-06-10 04:21:47 +000041
42 // When a new chunk is created, we don't if if it's going to make it
43 // to the final output. Initially all sections are unmarked in terms
44 // of garbage collection. The writer will call markLive() to mark
45 // all reachable section chunks.
46 Live = false;
47
48 // COMDAT sections are not GC root. Non-text sections are not
49 // subject of garbage collection (thus they are root).
50 if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE))
51 Root = true;
Rui Ueyama411c63602015-05-28 19:09:30 +000052}
53
Rui Ueyama42aa00b2015-06-25 00:33:38 +000054static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
55static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
56static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
57
Rui Ueyamad6fefba42015-05-28 19:45:43 +000058void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000059 if (!hasData())
60 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000061 // Copy section contents from source object file to output file.
Rui Ueyama411c63602015-05-28 19:09:30 +000062 ArrayRef<uint8_t> Data;
63 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000064 memcpy(Buf + FileOff, Data.data(), Data.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000065
66 // Apply relocations.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000067 for (const coff_relocation &Rel : Relocs) {
68 uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
69 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
70 uint64_t S = cast<Defined>(Body)->getRVA();
71 uint64_t P = RVA + Rel.VirtualAddress;
72 switch (Rel.Type) {
73 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
74 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
75 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
76 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
77 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
78 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
79 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
80 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
81 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
82 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
83 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
84 default:
85 llvm::report_fatal_error("Unsupported relocation type");
86 }
87 }
Rui Ueyama411c63602015-05-28 19:09:30 +000088}
89
Rui Ueyama8b33f592015-06-10 04:21:47 +000090void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000091 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000092 Live = true;
93
94 // Mark all symbols listed in the relocation table for this section.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000095 for (const coff_relocation &Rel : Relocs) {
96 SymbolBody *B = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama411c63602015-05-28 19:09:30 +000097 if (auto *Def = dyn_cast<Defined>(B))
98 Def->markLive();
99 }
100
101 // Mark associative sections if any.
102 for (Chunk *C : AssocChildren)
103 C->markLive();
104}
105
106void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000107 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000108 // Associative sections are live if their parent COMDATs are live,
109 // and vice versa, so they are not considered live by themselves.
110 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000111}
112
Rui Ueyama588e8322015-06-15 01:23:58 +0000113// Windows-specific.
114// Collect all locations that contain absolute 64-bit addresses,
115// which need to be fixed by the loader if load-time relocation is needed.
116// Only called when base relocation is enabled.
117void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000118 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama588e8322015-06-15 01:23:58 +0000119 // ADDR64 relocations contain absolute addresses.
120 // Symbol __ImageBase is special -- it's an absolute symbol, but its
121 // address never changes even if image is relocated.
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000122 if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
Rui Ueyama588e8322015-06-15 01:23:58 +0000123 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000124 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama588e8322015-06-15 01:23:58 +0000125 if (Body == ImageBase)
126 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000127 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000128 }
129}
130
Rui Ueyama411c63602015-05-28 19:09:30 +0000131bool SectionChunk::hasData() const {
132 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
133}
134
135uint32_t SectionChunk::getPermissions() const {
136 return Header->Characteristics & PermMask;
137}
138
139bool SectionChunk::isCOMDAT() const {
140 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
141}
142
Rui Ueyama411c63602015-05-28 19:09:30 +0000143void SectionChunk::printDiscardedMessage() {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000144 if (this == Ptr) {
145 // Removed by dead-stripping.
146 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
147 } else {
148 // Removed by ICF.
149 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
150 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000151}
152
Rui Ueyama6a60be72015-06-24 00:00:52 +0000153StringRef SectionChunk::getDebugName() {
154 return Sym->getName();
155}
156
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000157uint64_t SectionChunk::getHash() const {
158 ArrayRef<uint8_t> A;
159 File->getCOFFObj()->getSectionContents(Header, A);
Rui Ueyama02c30272015-06-25 17:43:37 +0000160 return hash_combine(getPermissions(),
161 llvm::hash_value(SectionName),
162 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000163 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000164 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000165 hash_combine_range(A.data(), A.data() + A.size()));
166}
167
168// Returns true if this and a given chunk are identical COMDAT sections.
169bool SectionChunk::equals(const SectionChunk *X) const {
170 // Compare headers
171 if (getPermissions() != X->getPermissions())
172 return false;
173 if (SectionName != X->SectionName)
174 return false;
175 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
176 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000177 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000178 return false;
179
180 // Compare data
181 ArrayRef<uint8_t> A, B;
182 File->getCOFFObj()->getSectionContents(Header, A);
183 X->File->getCOFFObj()->getSectionContents(X->Header, B);
184 assert(A.size() == B.size());
185 if (memcmp(A.data(), B.data(), A.size()))
186 return false;
187
188 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000189 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
190 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000191 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000192 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000193 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000194 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex);
195 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000196 if (auto *C1 = dyn_cast<DefinedCOMDAT>(B1))
197 if (auto *C2 = dyn_cast<DefinedCOMDAT>(B2))
198 if (C1->getChunk() == C2->getChunk())
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000199 return true;
200 return B1 == B2;
201 };
202 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000203}
204
205// Returns a pointer to this chunk or its replacement.
206SectionChunk *SectionChunk::repl() {
207 while (Ptr != Ptr->Ptr)
208 Ptr = Ptr->Ptr;
209 return Ptr;
210}
211
212void SectionChunk::replaceWith(SectionChunk *Other) {
213 Ptr = Other;
214 Live = false;
215}
216
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000217CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000218 // Common symbols are aligned on natural boundaries up to 32 bytes.
219 // This is what MSVC link.exe does.
220 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000221}
222
Rui Ueyama411c63602015-05-28 19:09:30 +0000223uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000224 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
225 IMAGE_SCN_MEM_WRITE;
226}
227
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000228void StringChunk::writeTo(uint8_t *Buf) {
229 memcpy(Buf + FileOff, Str.data(), Str.size());
230}
231
232void ImportThunkChunk::writeTo(uint8_t *Buf) {
233 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000234 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000235 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000236 write32le(Buf + FileOff + 2, Operand);
237}
238
Rui Ueyama588e8322015-06-15 01:23:58 +0000239// Windows-specific.
240// This class represents a block in .reloc section.
241BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
242 // Block header consists of 4 byte page RVA and 4 byte block size.
243 // Each entry is 2 byte. Last entry may be padding.
244 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
245 uint8_t *P = Data.data();
246 write32le(P, Page);
247 write32le(P + 4, Data.size());
248 P += 8;
249 for (uint32_t *I = Begin; I != End; ++I) {
250 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
251 P += 2;
252 }
253}
254
Rui Ueyama88e0f922015-06-25 03:31:47 +0000255void LocalImportChunk::writeTo(uint8_t *Buf) {
256 write32le(Buf + FileOff, Sym->getRVA());
257}
258
Rui Ueyama588e8322015-06-15 01:23:58 +0000259void BaserelChunk::writeTo(uint8_t *Buf) {
260 memcpy(Buf + FileOff, Data.data(), Data.size());
261}
262
Rui Ueyama411c63602015-05-28 19:09:30 +0000263} // namespace coff
264} // namespace lld