blob: 2acc72210673ea1f73a02c4bd653763f5556ea32 [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 Ueyamaf34c0882015-06-25 17:56:36 +000062 ArrayRef<uint8_t> A = getContents();
63 memcpy(Buf + FileOff, A.data(), A.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000064
65 // Apply relocations.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000066 for (const coff_relocation &Rel : Relocs) {
67 uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
68 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
69 uint64_t S = cast<Defined>(Body)->getRVA();
70 uint64_t P = RVA + Rel.VirtualAddress;
71 switch (Rel.Type) {
72 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
73 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
74 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
75 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
76 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
77 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
78 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
79 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
80 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
81 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
82 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
83 default:
84 llvm::report_fatal_error("Unsupported relocation type");
85 }
86 }
Rui Ueyama411c63602015-05-28 19:09:30 +000087}
88
Rui Ueyama8b33f592015-06-10 04:21:47 +000089void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000090 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000091 Live = true;
92
93 // Mark all symbols listed in the relocation table for this section.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000094 for (const coff_relocation &Rel : Relocs) {
95 SymbolBody *B = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama411c63602015-05-28 19:09:30 +000096 if (auto *Def = dyn_cast<Defined>(B))
97 Def->markLive();
98 }
99
100 // Mark associative sections if any.
101 for (Chunk *C : AssocChildren)
102 C->markLive();
103}
104
105void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000106 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000107 // Associative sections are live if their parent COMDATs are live,
108 // and vice versa, so they are not considered live by themselves.
109 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000110}
111
Rui Ueyama588e8322015-06-15 01:23:58 +0000112// Windows-specific.
113// Collect all locations that contain absolute 64-bit addresses,
114// which need to be fixed by the loader if load-time relocation is needed.
115// Only called when base relocation is enabled.
116void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000117 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama588e8322015-06-15 01:23:58 +0000118 // ADDR64 relocations contain absolute addresses.
119 // Symbol __ImageBase is special -- it's an absolute symbol, but its
120 // address never changes even if image is relocated.
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000121 if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
Rui Ueyama588e8322015-06-15 01:23:58 +0000122 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000123 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama588e8322015-06-15 01:23:58 +0000124 if (Body == ImageBase)
125 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000126 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000127 }
128}
129
Rui Ueyama411c63602015-05-28 19:09:30 +0000130bool SectionChunk::hasData() const {
131 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
132}
133
134uint32_t SectionChunk::getPermissions() const {
135 return Header->Characteristics & PermMask;
136}
137
138bool SectionChunk::isCOMDAT() const {
139 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
140}
141
Rui Ueyama411c63602015-05-28 19:09:30 +0000142void SectionChunk::printDiscardedMessage() {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000143 if (this == Ptr) {
144 // Removed by dead-stripping.
145 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
146 } else {
147 // Removed by ICF.
148 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
149 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000150}
151
Rui Ueyama6a60be72015-06-24 00:00:52 +0000152StringRef SectionChunk::getDebugName() {
153 return Sym->getName();
154}
155
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000156uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000157 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000158 return hash_combine(getPermissions(),
159 llvm::hash_value(SectionName),
160 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000161 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000162 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000163 hash_combine_range(A.data(), A.data() + A.size()));
164}
165
166// Returns true if this and a given chunk are identical COMDAT sections.
167bool SectionChunk::equals(const SectionChunk *X) const {
168 // Compare headers
169 if (getPermissions() != X->getPermissions())
170 return false;
171 if (SectionName != X->SectionName)
172 return false;
173 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
174 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000175 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000176 return false;
177
178 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000179 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000180 return false;
181
182 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000183 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
184 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000185 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000186 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000187 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000188 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex);
189 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000190 if (auto *C1 = dyn_cast<DefinedCOMDAT>(B1))
191 if (auto *C2 = dyn_cast<DefinedCOMDAT>(B2))
192 if (C1->getChunk() == C2->getChunk())
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000193 return true;
194 return B1 == B2;
195 };
196 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000197}
198
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000199ArrayRef<uint8_t> SectionChunk::getContents() const {
200 ArrayRef<uint8_t> A;
201 File->getCOFFObj()->getSectionContents(Header, A);
202 return A;
203}
204
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000205// 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