blob: 95626fd504850f79c8dc1a6205f95dced9502f27 [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),
32 Relocs(File->getCOFFObj()->getRelocations(Header)) {
Rui Ueyama411c63602015-05-28 19:09:30 +000033 // Initialize SectionName.
34 File->getCOFFObj()->getSectionName(Header, SectionName);
Rui Ueyama8b33f592015-06-10 04:21:47 +000035
Rui Ueyama2bf6a122015-06-14 21:50:50 +000036 // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
37 unsigned Shift = (Header->Characteristics >> 20) & 0xF;
38 if (Shift > 0)
39 Align = uint32_t(1) << (Shift - 1);
Rui Ueyama8b33f592015-06-10 04:21:47 +000040
41 // When a new chunk is created, we don't if if it's going to make it
42 // to the final output. Initially all sections are unmarked in terms
43 // of garbage collection. The writer will call markLive() to mark
44 // all reachable section chunks.
45 Live = false;
46
47 // COMDAT sections are not GC root. Non-text sections are not
48 // subject of garbage collection (thus they are root).
49 if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE))
50 Root = true;
Rui Ueyama411c63602015-05-28 19:09:30 +000051}
52
Rui Ueyama42aa00b2015-06-25 00:33:38 +000053static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
54static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
55static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
56
Rui Ueyamad6fefba42015-05-28 19:45:43 +000057void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000058 if (!hasData())
59 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000060 // Copy section contents from source object file to output file.
Rui Ueyama411c63602015-05-28 19:09:30 +000061 ArrayRef<uint8_t> Data;
62 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000063 memcpy(Buf + FileOff, Data.data(), Data.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 {
157 ArrayRef<uint8_t> A;
158 File->getCOFFObj()->getSectionContents(Header, A);
159 return hash_combine(getPermissions(), llvm::hash_value(SectionName),
160 uint32_t(Header->SizeOfRawData),
161 uint32_t(Header->NumberOfRelocations),
162 hash_combine_range(A.data(), A.data() + A.size()));
163}
164
165// Returns true if this and a given chunk are identical COMDAT sections.
166bool SectionChunk::equals(const SectionChunk *X) const {
167 // Compare headers
168 if (getPermissions() != X->getPermissions())
169 return false;
170 if (SectionName != X->SectionName)
171 return false;
172 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
173 return false;
174 if (Header->NumberOfRelocations != X->Header->NumberOfRelocations)
175 return false;
176
177 // Compare data
178 ArrayRef<uint8_t> A, B;
179 File->getCOFFObj()->getSectionContents(Header, A);
180 X->File->getCOFFObj()->getSectionContents(X->Header, B);
181 assert(A.size() == B.size());
182 if (memcmp(A.data(), B.data(), A.size()))
183 return false;
184
185 // Compare relocations
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000186 const coff_relocation *Rel1 = Relocs.begin();
187 const coff_relocation *End = Relocs.end();
188 const coff_relocation *Rel2 = X->Relocs.begin();
Rui Ueyamacde92422015-06-24 23:03:17 +0000189 for (; Rel1 != End; ++Rel1, ++Rel2) {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000190 if (Rel1->Type != Rel2->Type)
191 return false;
192 if (Rel1->VirtualAddress != Rel2->VirtualAddress)
193 return false;
194 SymbolBody *B1 = File->getSymbolBody(Rel1->SymbolTableIndex);
195 SymbolBody *B2 = X->File->getSymbolBody(Rel2->SymbolTableIndex);
196 if (auto *C1 = dyn_cast<DefinedCOMDAT>(B1))
197 if (auto *C2 = dyn_cast<DefinedCOMDAT>(B2))
198 if (C1->getChunk() == C2->getChunk())
199 continue;
200 if (B1 != B2)
201 return false;
202 }
203 return true;
204}
205
206// Returns a pointer to this chunk or its replacement.
207SectionChunk *SectionChunk::repl() {
208 while (Ptr != Ptr->Ptr)
209 Ptr = Ptr->Ptr;
210 return Ptr;
211}
212
213void SectionChunk::replaceWith(SectionChunk *Other) {
214 Ptr = Other;
215 Live = false;
216}
217
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000218CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000219 // Common symbols are aligned on natural boundaries up to 32 bytes.
220 // This is what MSVC link.exe does.
221 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000222}
223
Rui Ueyama411c63602015-05-28 19:09:30 +0000224uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000225 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
226 IMAGE_SCN_MEM_WRITE;
227}
228
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000229void StringChunk::writeTo(uint8_t *Buf) {
230 memcpy(Buf + FileOff, Str.data(), Str.size());
231}
232
233void ImportThunkChunk::writeTo(uint8_t *Buf) {
234 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000235 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000236 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000237 write32le(Buf + FileOff + 2, Operand);
238}
239
Rui Ueyama588e8322015-06-15 01:23:58 +0000240// Windows-specific.
241// This class represents a block in .reloc section.
242BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
243 // Block header consists of 4 byte page RVA and 4 byte block size.
244 // Each entry is 2 byte. Last entry may be padding.
245 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
246 uint8_t *P = Data.data();
247 write32le(P, Page);
248 write32le(P + 4, Data.size());
249 P += 8;
250 for (uint32_t *I = Begin; I != End; ++I) {
251 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
252 P += 2;
253 }
254}
255
Rui Ueyama88e0f922015-06-25 03:31:47 +0000256void LocalImportChunk::writeTo(uint8_t *Buf) {
257 write32le(Buf + FileOff, Sym->getRVA());
258}
259
Rui Ueyama588e8322015-06-15 01:23:58 +0000260void BaserelChunk::writeTo(uint8_t *Buf) {
261 memcpy(Buf + FileOff, Data.data(), Data.size());
262}
263
Rui Ueyama411c63602015-05-28 19:09:30 +0000264} // namespace coff
265} // namespace lld