blob: bd7b1aebbf30caae7cc84a92c152495f786383de [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 Ueyama9b921e52015-06-25 22:00:42 +000031 : Chunk(SectionKind), Ptr(this), File(F), 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;
Chandler Carruthbe6e80b2015-06-29 18:50:11 +000061 SymbolBody *Body =
62 File->getSymbolBody(Rel.SymbolTableIndex)->getReplacement();
Rui Ueyama42aa00b2015-06-25 00:33:38 +000063 uint64_t S = cast<Defined>(Body)->getRVA();
64 uint64_t P = RVA + Rel.VirtualAddress;
65 switch (Rel.Type) {
66 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
67 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
68 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
69 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
70 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
71 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
72 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
73 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
74 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
75 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
76 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
77 default:
78 llvm::report_fatal_error("Unsupported relocation type");
79 }
80 }
Rui Ueyama411c63602015-05-28 19:09:30 +000081}
82
Rui Ueyama8b33f592015-06-10 04:21:47 +000083void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000084 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000085 Live = true;
86
87 // Mark all symbols listed in the relocation table for this section.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000088 for (const coff_relocation &Rel : Relocs) {
Chandler Carruthbe6e80b2015-06-29 18:50:11 +000089 SymbolBody *B = File->getSymbolBody(Rel.SymbolTableIndex)->getReplacement();
Rui Ueyama9b921e52015-06-25 22:00:42 +000090 if (auto *D = dyn_cast<DefinedRegular>(B))
Rui Ueyamafc510f42015-06-25 19:10:58 +000091 D->markLive();
Rui Ueyama411c63602015-05-28 19:09:30 +000092 }
93
94 // Mark associative sections if any.
95 for (Chunk *C : AssocChildren)
Rui Ueyamafc510f42015-06-25 19:10:58 +000096 if (auto *SC = dyn_cast<SectionChunk>(C))
97 SC->markLive();
Rui Ueyama411c63602015-05-28 19:09:30 +000098}
99
100void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000101 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000102 // Associative sections are live if their parent COMDATs are live,
103 // and vice versa, so they are not considered live by themselves.
104 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000105}
106
Rui Ueyama588e8322015-06-15 01:23:58 +0000107// Windows-specific.
108// Collect all locations that contain absolute 64-bit addresses,
109// which need to be fixed by the loader if load-time relocation is needed.
110// Only called when base relocation is enabled.
111void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000112 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama588e8322015-06-15 01:23:58 +0000113 // ADDR64 relocations contain absolute addresses.
114 // Symbol __ImageBase is special -- it's an absolute symbol, but its
115 // address never changes even if image is relocated.
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000116 if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
Rui Ueyama588e8322015-06-15 01:23:58 +0000117 continue;
Chandler Carruthbe6e80b2015-06-29 18:50:11 +0000118 SymbolBody *Body =
119 File->getSymbolBody(Rel.SymbolTableIndex)->getReplacement();
Rui Ueyama588e8322015-06-15 01:23:58 +0000120 if (Body == ImageBase)
121 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000122 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000123 }
124}
125
Rui Ueyama411c63602015-05-28 19:09:30 +0000126bool SectionChunk::hasData() const {
127 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
128}
129
130uint32_t SectionChunk::getPermissions() const {
131 return Header->Characteristics & PermMask;
132}
133
134bool SectionChunk::isCOMDAT() const {
135 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
136}
137
Rui Ueyamafc510f42015-06-25 19:10:58 +0000138void SectionChunk::printDiscardedMessage() const {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000139 if (this == Ptr) {
140 // Removed by dead-stripping.
141 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
142 } else {
143 // Removed by ICF.
144 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
145 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000146}
147
Rui Ueyama6a60be72015-06-24 00:00:52 +0000148StringRef SectionChunk::getDebugName() {
149 return Sym->getName();
150}
151
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000152uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000153 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000154 return hash_combine(getPermissions(),
155 llvm::hash_value(SectionName),
156 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000157 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000158 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000159 hash_combine_range(A.data(), A.data() + A.size()));
160}
161
162// Returns true if this and a given chunk are identical COMDAT sections.
163bool SectionChunk::equals(const SectionChunk *X) const {
164 // Compare headers
165 if (getPermissions() != X->getPermissions())
166 return false;
167 if (SectionName != X->SectionName)
168 return false;
169 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
170 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000171 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000172 return false;
173
174 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000175 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000176 return false;
177
Rui Ueyama871847e2015-06-28 01:30:54 +0000178 // Compare associative sections
179 if (AssocChildren.size() != X->AssocChildren.size())
180 return false;
181 for (size_t I = 0, E = AssocChildren.size(); I != E; ++I)
182 if (AssocChildren[I]->Ptr != X->AssocChildren[I]->Ptr)
183 return false;
184
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000185 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000186 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
187 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000188 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000189 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000190 return false;
Chandler Carruthbe6e80b2015-06-29 18:50:11 +0000191 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex)->getReplacement();
192 SymbolBody *B2 =
193 X->File->getSymbolBody(R2.SymbolTableIndex)->getReplacement();
Rui Ueyama871847e2015-06-28 01:30:54 +0000194 if (B1 == B2)
195 return true;
Rui Ueyama9b921e52015-06-25 22:00:42 +0000196 auto *D1 = dyn_cast<DefinedRegular>(B1);
197 auto *D2 = dyn_cast<DefinedRegular>(B2);
Rui Ueyama871847e2015-06-28 01:30:54 +0000198 return (D1 && D2 &&
199 D1->getValue() == D2->getValue() &&
200 D1->getChunk() == D2->getChunk());
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000201 };
202 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000203}
204
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000205ArrayRef<uint8_t> SectionChunk::getContents() const {
206 ArrayRef<uint8_t> A;
207 File->getCOFFObj()->getSectionContents(Header, A);
208 return A;
209}
210
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000211void SectionChunk::replaceWith(SectionChunk *Other) {
Rui Ueyama9b921e52015-06-25 22:00:42 +0000212 Ptr = Other->Ptr;
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000213 Live = false;
214}
215
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000216CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000217 // Common symbols are aligned on natural boundaries up to 32 bytes.
218 // This is what MSVC link.exe does.
219 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000220}
221
Rui Ueyama411c63602015-05-28 19:09:30 +0000222uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000223 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
224 IMAGE_SCN_MEM_WRITE;
225}
226
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000227void StringChunk::writeTo(uint8_t *Buf) {
228 memcpy(Buf + FileOff, Str.data(), Str.size());
229}
230
Rui Ueyama73835622015-06-26 18:28:56 +0000231ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) {
232 // Intel Optimization Manual says that all branch targets
233 // should be 16-byte aligned. MSVC linker does this too.
234 Align = 16;
235}
236
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000237void ImportThunkChunk::writeTo(uint8_t *Buf) {
238 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000239 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000240 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000241 write32le(Buf + FileOff + 2, Operand);
242}
243
Rui Ueyama588e8322015-06-15 01:23:58 +0000244// Windows-specific.
245// This class represents a block in .reloc section.
246BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
247 // Block header consists of 4 byte page RVA and 4 byte block size.
248 // Each entry is 2 byte. Last entry may be padding.
249 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
250 uint8_t *P = Data.data();
251 write32le(P, Page);
252 write32le(P + 4, Data.size());
253 P += 8;
254 for (uint32_t *I = Begin; I != End; ++I) {
255 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
256 P += 2;
257 }
258}
259
Rui Ueyama88e0f922015-06-25 03:31:47 +0000260void LocalImportChunk::writeTo(uint8_t *Buf) {
261 write32le(Buf + FileOff, Sym->getRVA());
262}
263
Rui Ueyama588e8322015-06-15 01:23:58 +0000264void BaserelChunk::writeTo(uint8_t *Buf) {
265 memcpy(Buf + FileOff, Data.data(), Data.size());
266}
267
Rui Ueyama411c63602015-05-28 19:09:30 +0000268} // namespace coff
269} // namespace lld