blob: aa2c0be3ef51db5d6b0e2056f3ec25a5495dbbb1 [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;
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 Ueyama9b921e52015-06-25 22:00:42 +000089 if (auto *D = dyn_cast<DefinedRegular>(B))
Rui Ueyamafc510f42015-06-25 19:10:58 +000090 D->markLive();
Rui Ueyama411c63602015-05-28 19:09:30 +000091 }
92
93 // Mark associative sections if any.
94 for (Chunk *C : AssocChildren)
Rui Ueyamafc510f42015-06-25 19:10:58 +000095 if (auto *SC = dyn_cast<SectionChunk>(C))
96 SC->markLive();
Rui Ueyama411c63602015-05-28 19:09:30 +000097}
98
99void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000100 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000101 // Associative sections are live if their parent COMDATs are live,
102 // and vice versa, so they are not considered live by themselves.
103 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000104}
105
Rui Ueyama588e8322015-06-15 01:23:58 +0000106// Windows-specific.
107// Collect all locations that contain absolute 64-bit addresses,
108// which need to be fixed by the loader if load-time relocation is needed.
109// Only called when base relocation is enabled.
110void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000111 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama588e8322015-06-15 01:23:58 +0000112 // ADDR64 relocations contain absolute addresses.
113 // Symbol __ImageBase is special -- it's an absolute symbol, but its
114 // address never changes even if image is relocated.
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000115 if (Rel.Type != IMAGE_REL_AMD64_ADDR64)
Rui Ueyama588e8322015-06-15 01:23:58 +0000116 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000117 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex);
Rui Ueyama588e8322015-06-15 01:23:58 +0000118 if (Body == ImageBase)
119 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000120 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000121 }
122}
123
Rui Ueyama411c63602015-05-28 19:09:30 +0000124bool SectionChunk::hasData() const {
125 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
126}
127
128uint32_t SectionChunk::getPermissions() const {
129 return Header->Characteristics & PermMask;
130}
131
132bool SectionChunk::isCOMDAT() const {
133 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
134}
135
Rui Ueyamafc510f42015-06-25 19:10:58 +0000136void SectionChunk::printDiscardedMessage() const {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000137 if (this == Ptr) {
138 // Removed by dead-stripping.
139 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
140 } else {
141 // Removed by ICF.
142 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
143 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000144}
145
Rui Ueyama6a60be72015-06-24 00:00:52 +0000146StringRef SectionChunk::getDebugName() {
147 return Sym->getName();
148}
149
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000150uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000151 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000152 return hash_combine(getPermissions(),
153 llvm::hash_value(SectionName),
154 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000155 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000156 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000157 hash_combine_range(A.data(), A.data() + A.size()));
158}
159
160// Returns true if this and a given chunk are identical COMDAT sections.
161bool SectionChunk::equals(const SectionChunk *X) const {
162 // Compare headers
163 if (getPermissions() != X->getPermissions())
164 return false;
165 if (SectionName != X->SectionName)
166 return false;
167 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
168 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000169 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000170 return false;
171
172 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000173 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000174 return false;
175
176 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000177 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
178 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000179 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000180 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000181 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000182 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex);
183 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex);
Rui Ueyama9b921e52015-06-25 22:00:42 +0000184 auto *D1 = dyn_cast<DefinedRegular>(B1);
185 auto *D2 = dyn_cast<DefinedRegular>(B2);
186 if (D1 && D2 && D1->getChunk() == D2->getChunk())
187 return true;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000188 return B1 == B2;
189 };
190 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000191}
192
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000193ArrayRef<uint8_t> SectionChunk::getContents() const {
194 ArrayRef<uint8_t> A;
195 File->getCOFFObj()->getSectionContents(Header, A);
196 return A;
197}
198
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000199void SectionChunk::replaceWith(SectionChunk *Other) {
Rui Ueyama9b921e52015-06-25 22:00:42 +0000200 Ptr = Other->Ptr;
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000201 Live = false;
202}
203
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000204CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000205 // Common symbols are aligned on natural boundaries up to 32 bytes.
206 // This is what MSVC link.exe does.
207 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000208}
209
Rui Ueyama411c63602015-05-28 19:09:30 +0000210uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000211 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
212 IMAGE_SCN_MEM_WRITE;
213}
214
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000215void StringChunk::writeTo(uint8_t *Buf) {
216 memcpy(Buf + FileOff, Str.data(), Str.size());
217}
218
Rui Ueyama73835622015-06-26 18:28:56 +0000219ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) {
220 // Intel Optimization Manual says that all branch targets
221 // should be 16-byte aligned. MSVC linker does this too.
222 Align = 16;
223}
224
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000225void ImportThunkChunk::writeTo(uint8_t *Buf) {
226 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000227 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000228 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000229 write32le(Buf + FileOff + 2, Operand);
230}
231
Rui Ueyama588e8322015-06-15 01:23:58 +0000232// Windows-specific.
233// This class represents a block in .reloc section.
234BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
235 // Block header consists of 4 byte page RVA and 4 byte block size.
236 // Each entry is 2 byte. Last entry may be padding.
237 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
238 uint8_t *P = Data.data();
239 write32le(P, Page);
240 write32le(P + 4, Data.size());
241 P += 8;
242 for (uint32_t *I = Begin; I != End; ++I) {
243 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
244 P += 2;
245 }
246}
247
Rui Ueyama88e0f922015-06-25 03:31:47 +0000248void LocalImportChunk::writeTo(uint8_t *Buf) {
249 write32le(Buf + FileOff, Sym->getRVA());
250}
251
Rui Ueyama588e8322015-06-15 01:23:58 +0000252void BaserelChunk::writeTo(uint8_t *Buf) {
253 memcpy(Buf + FileOff, Data.data(), Data.size());
254}
255
Rui Ueyama411c63602015-05-28 19:09:30 +0000256} // namespace coff
257} // namespace lld