blob: 1369bdf465b5fc1f62fabba673935d31df1e6351 [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 Ueyamafc510f42015-06-25 19:10:58 +000031 : Chunk(SectionKind), 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
Rui Ueyama8b33f592015-06-10 04:21:47 +000042 // COMDAT sections are not GC root. Non-text sections are not
43 // subject of garbage collection (thus they are root).
Rui Ueyamafc510f42015-06-25 19:10:58 +000044 Root = !isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
Rui Ueyama411c63602015-05-28 19:09:30 +000045}
46
Rui Ueyama42aa00b2015-06-25 00:33:38 +000047static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
48static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
49static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
50
Rui Ueyamad6fefba42015-05-28 19:45:43 +000051void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000052 if (!hasData())
53 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000054 // Copy section contents from source object file to output file.
Rui Ueyamaf34c0882015-06-25 17:56:36 +000055 ArrayRef<uint8_t> A = getContents();
56 memcpy(Buf + FileOff, A.data(), A.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000057
58 // Apply relocations.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000059 for (const coff_relocation &Rel : Relocs) {
60 uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
61 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
62 uint64_t S = cast<Defined>(Body)->getRVA();
63 uint64_t P = RVA + Rel.VirtualAddress;
64 switch (Rel.Type) {
65 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
66 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
67 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
68 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
69 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
70 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
71 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
72 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
73 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
74 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
75 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
76 default:
77 llvm::report_fatal_error("Unsupported relocation type");
78 }
79 }
Rui Ueyama411c63602015-05-28 19:09:30 +000080}
81
Rui Ueyama8b33f592015-06-10 04:21:47 +000082void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000083 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000084 Live = true;
85
86 // Mark all symbols listed in the relocation table for this section.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000087 for (const coff_relocation &Rel : Relocs) {
88 SymbolBody *B = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyamafc510f42015-06-25 19:10:58 +000089 if (auto *D = dyn_cast<DefinedRegular>(B)) {
90 D->markLive();
91 } else if (auto *D = dyn_cast<DefinedCOMDAT>(B)) {
92 D->markLive();
93 }
Rui Ueyama411c63602015-05-28 19:09:30 +000094 }
95
96 // Mark associative sections if any.
97 for (Chunk *C : AssocChildren)
Rui Ueyamafc510f42015-06-25 19:10:58 +000098 if (auto *SC = dyn_cast<SectionChunk>(C))
99 SC->markLive();
Rui Ueyama411c63602015-05-28 19:09:30 +0000100}
101
102void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000103 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000104 // Associative sections are live if their parent COMDATs are live,
105 // and vice versa, so they are not considered live by themselves.
106 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000107}
108
Rui Ueyama588e8322015-06-15 01:23:58 +0000109// Windows-specific.
110// Collect all locations that contain absolute 64-bit addresses,
111// which need to be fixed by the loader if load-time relocation is needed.
112// Only called when base relocation is enabled.
113void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000114 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama588e8322015-06-15 01:23:58 +0000115 // ADDR64 relocations contain absolute addresses.
116 // Symbol __ImageBase is special -- it's an absolute symbol, but its
117 // address never changes even if image is relocated.
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000118 if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
Rui Ueyama588e8322015-06-15 01:23:58 +0000119 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000120 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama588e8322015-06-15 01:23:58 +0000121 if (Body == ImageBase)
122 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000123 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000124 }
125}
126
Rui Ueyama411c63602015-05-28 19:09:30 +0000127bool SectionChunk::hasData() const {
128 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
129}
130
131uint32_t SectionChunk::getPermissions() const {
132 return Header->Characteristics & PermMask;
133}
134
135bool SectionChunk::isCOMDAT() const {
136 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
137}
138
Rui Ueyamafc510f42015-06-25 19:10:58 +0000139void SectionChunk::printDiscardedMessage() const {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000140 if (this == Ptr) {
141 // Removed by dead-stripping.
142 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
143 } else {
144 // Removed by ICF.
145 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
146 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000147}
148
Rui Ueyama6a60be72015-06-24 00:00:52 +0000149StringRef SectionChunk::getDebugName() {
150 return Sym->getName();
151}
152
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000153uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000154 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000155 return hash_combine(getPermissions(),
156 llvm::hash_value(SectionName),
157 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000158 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000159 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000160 hash_combine_range(A.data(), A.data() + A.size()));
161}
162
163// Returns true if this and a given chunk are identical COMDAT sections.
164bool SectionChunk::equals(const SectionChunk *X) const {
165 // Compare headers
166 if (getPermissions() != X->getPermissions())
167 return false;
168 if (SectionName != X->SectionName)
169 return false;
170 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
171 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000172 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000173 return false;
174
175 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000176 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000177 return false;
178
179 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000180 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
181 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000182 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000183 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000184 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000185 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex);
186 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000187 if (auto *C1 = dyn_cast<DefinedCOMDAT>(B1))
188 if (auto *C2 = dyn_cast<DefinedCOMDAT>(B2))
189 if (C1->getChunk() == C2->getChunk())
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000190 return true;
191 return B1 == B2;
192 };
193 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000194}
195
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000196ArrayRef<uint8_t> SectionChunk::getContents() const {
197 ArrayRef<uint8_t> A;
198 File->getCOFFObj()->getSectionContents(Header, A);
199 return A;
200}
201
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000202// Returns a pointer to this chunk or its replacement.
203SectionChunk *SectionChunk::repl() {
204 while (Ptr != Ptr->Ptr)
205 Ptr = Ptr->Ptr;
206 return Ptr;
207}
208
209void SectionChunk::replaceWith(SectionChunk *Other) {
210 Ptr = Other;
211 Live = false;
212}
213
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000214CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000215 // Common symbols are aligned on natural boundaries up to 32 bytes.
216 // This is what MSVC link.exe does.
217 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000218}
219
Rui Ueyama411c63602015-05-28 19:09:30 +0000220uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000221 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
222 IMAGE_SCN_MEM_WRITE;
223}
224
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000225void StringChunk::writeTo(uint8_t *Buf) {
226 memcpy(Buf + FileOff, Str.data(), Str.size());
227}
228
229void ImportThunkChunk::writeTo(uint8_t *Buf) {
230 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000231 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000232 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000233 write32le(Buf + FileOff + 2, Operand);
234}
235
Rui Ueyama588e8322015-06-15 01:23:58 +0000236// Windows-specific.
237// This class represents a block in .reloc section.
238BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
239 // Block header consists of 4 byte page RVA and 4 byte block size.
240 // Each entry is 2 byte. Last entry may be padding.
241 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
242 uint8_t *P = Data.data();
243 write32le(P, Page);
244 write32le(P + 4, Data.size());
245 P += 8;
246 for (uint32_t *I = Begin; I != End; ++I) {
247 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
248 P += 2;
249 }
250}
251
Rui Ueyama88e0f922015-06-25 03:31:47 +0000252void LocalImportChunk::writeTo(uint8_t *Buf) {
253 write32le(Buf + FileOff, Sym->getRVA());
254}
255
Rui Ueyama588e8322015-06-15 01:23:58 +0000256void BaserelChunk::writeTo(uint8_t *Buf) {
257 memcpy(Buf + FileOff, Data.data(), Data.size());
258}
259
Rui Ueyama411c63602015-05-28 19:09:30 +0000260} // namespace coff
261} // namespace lld