blob: 4f1ac166847363b8a6061a0aeaee6da8e151bb42 [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 Ueyama661a4e7a2015-07-07 22:49:21 +000051void SectionChunk::applyRelX64(uint8_t *Off, uint16_t Type, uint64_t S,
52 uint64_t P) {
53 switch (Type) {
54 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
55 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
56 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
57 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
58 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
59 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
60 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
61 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
62 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
David Majnemer2c345a32015-07-08 16:37:50 +000063 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->SectionIndex); break;
Rui Ueyama661a4e7a2015-07-07 22:49:21 +000064 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
65 default:
66 llvm::report_fatal_error("Unsupported relocation type");
67 }
68}
69
Rui Ueyama11863b4a2015-07-08 01:45:29 +000070void SectionChunk::applyRelX86(uint8_t *Off, uint16_t Type, uint64_t S,
71 uint64_t P) {
72 switch (Type) {
73 case IMAGE_REL_I386_ABSOLUTE: break;
74 case IMAGE_REL_I386_DIR32: add32(Off, S + Config->ImageBase); break;
75 case IMAGE_REL_I386_DIR32NB: add32(Off, S); break;
76 case IMAGE_REL_I386_REL32: add32(Off, S - P - 4); break;
David Majnemer2c345a32015-07-08 16:37:50 +000077 case IMAGE_REL_I386_SECTION: add16(Off, Out->SectionIndex); break;
Rui Ueyama11863b4a2015-07-08 01:45:29 +000078 case IMAGE_REL_I386_SECREL: add32(Off, S - Out->getRVA()); break;
79 default:
80 llvm::report_fatal_error("Unsupported relocation type");
81 }
82}
83
Rui Ueyamad6fefba42015-05-28 19:45:43 +000084void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000085 if (!hasData())
86 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000087 // Copy section contents from source object file to output file.
Rui Ueyamaf34c0882015-06-25 17:56:36 +000088 ArrayRef<uint8_t> A = getContents();
89 memcpy(Buf + FileOff, A.data(), A.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000090
91 // Apply relocations.
Rui Ueyama42aa00b2015-06-25 00:33:38 +000092 for (const coff_relocation &Rel : Relocs) {
93 uint8_t *Off = Buf + FileOff + Rel.VirtualAddress;
Rui Ueyama0744e872015-07-02 00:21:11 +000094 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
Rui Ueyama42aa00b2015-06-25 00:33:38 +000095 uint64_t S = cast<Defined>(Body)->getRVA();
96 uint64_t P = RVA + Rel.VirtualAddress;
Rui Ueyama11863b4a2015-07-08 01:45:29 +000097 switch (Config->MachineType) {
98 case IMAGE_FILE_MACHINE_AMD64:
99 applyRelX64(Off, Rel.Type, S, P);
100 break;
101 case IMAGE_FILE_MACHINE_I386:
102 applyRelX86(Off, Rel.Type, S, P);
103 break;
104 default:
105 llvm_unreachable("unknown machine type");
106 }
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000107 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000108}
109
Rui Ueyama411c63602015-05-28 19:09:30 +0000110void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000111 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +0000112 // Associative sections are live if their parent COMDATs are live,
113 // and vice versa, so they are not considered live by themselves.
114 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +0000115}
116
Rui Ueyama93b45712015-07-09 20:36:59 +0000117static bool isAbs(const coff_relocation &Rel) {
118 switch (Config->MachineType) {
119 case IMAGE_FILE_MACHINE_AMD64:
120 return Rel.Type == IMAGE_REL_AMD64_ADDR64;
121 case IMAGE_FILE_MACHINE_I386:
122 return Rel.Type == IMAGE_REL_I386_DIR32;
123 default:
124 llvm_unreachable("unknown machine type");
125 }
126}
127
Rui Ueyama588e8322015-06-15 01:23:58 +0000128// Windows-specific.
Rui Ueyama93b45712015-07-09 20:36:59 +0000129// Collect all locations that contain absolute addresses, which need to be
130// fixed by the loader if load-time relocation is needed.
Rui Ueyama588e8322015-06-15 01:23:58 +0000131// Only called when base relocation is enabled.
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000132void SectionChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000133 for (const coff_relocation &Rel : Relocs) {
Rui Ueyama93b45712015-07-09 20:36:59 +0000134 if (!isAbs(Rel))
Rui Ueyama588e8322015-06-15 01:23:58 +0000135 continue;
Rui Ueyama0744e872015-07-02 00:21:11 +0000136 SymbolBody *Body = File->getSymbolBody(Rel.SymbolTableIndex)->repl();
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000137 if (isa<DefinedAbsolute>(Body))
Rui Ueyama588e8322015-06-15 01:23:58 +0000138 continue;
Rui Ueyama42aa00b2015-06-25 00:33:38 +0000139 Res->push_back(RVA + Rel.VirtualAddress);
Rui Ueyama588e8322015-06-15 01:23:58 +0000140 }
141}
142
Rui Ueyama411c63602015-05-28 19:09:30 +0000143bool SectionChunk::hasData() const {
144 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
145}
146
147uint32_t SectionChunk::getPermissions() const {
148 return Header->Characteristics & PermMask;
149}
150
151bool SectionChunk::isCOMDAT() const {
152 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
153}
154
Rui Ueyamafc510f42015-06-25 19:10:58 +0000155void SectionChunk::printDiscardedMessage() const {
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000156 if (this == Ptr) {
157 // Removed by dead-stripping.
158 llvm::dbgs() << "Discarded " << Sym->getName() << "\n";
159 } else {
160 // Removed by ICF.
161 llvm::dbgs() << "Replaced " << Sym->getName() << "\n";
162 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000163}
164
Rui Ueyama6a60be72015-06-24 00:00:52 +0000165StringRef SectionChunk::getDebugName() {
166 return Sym->getName();
167}
168
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000169uint64_t SectionChunk::getHash() const {
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000170 ArrayRef<uint8_t> A = getContents();
Rui Ueyama02c30272015-06-25 17:43:37 +0000171 return hash_combine(getPermissions(),
172 llvm::hash_value(SectionName),
173 NumRelocs,
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000174 uint32_t(Header->SizeOfRawData),
Rui Ueyama02c30272015-06-25 17:43:37 +0000175 std::distance(Relocs.end(), Relocs.begin()),
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000176 hash_combine_range(A.data(), A.data() + A.size()));
177}
178
179// Returns true if this and a given chunk are identical COMDAT sections.
180bool SectionChunk::equals(const SectionChunk *X) const {
181 // Compare headers
182 if (getPermissions() != X->getPermissions())
183 return false;
184 if (SectionName != X->SectionName)
185 return false;
186 if (Header->SizeOfRawData != X->Header->SizeOfRawData)
187 return false;
Rui Ueyama02c30272015-06-25 17:43:37 +0000188 if (NumRelocs != X->NumRelocs)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000189 return false;
190
191 // Compare data
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000192 if (getContents() != X->getContents())
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000193 return false;
194
Rui Ueyama871847e2015-06-28 01:30:54 +0000195 // Compare associative sections
196 if (AssocChildren.size() != X->AssocChildren.size())
197 return false;
198 for (size_t I = 0, E = AssocChildren.size(); I != E; ++I)
199 if (AssocChildren[I]->Ptr != X->AssocChildren[I]->Ptr)
200 return false;
201
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000202 // Compare relocations
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000203 auto Eq = [&](const coff_relocation &R1, const coff_relocation &R2) {
204 if (R1.Type != R2.Type)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000205 return false;
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000206 if (R1.VirtualAddress != R2.VirtualAddress)
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000207 return false;
Rui Ueyama0744e872015-07-02 00:21:11 +0000208 SymbolBody *B1 = File->getSymbolBody(R1.SymbolTableIndex)->repl();
209 SymbolBody *B2 = X->File->getSymbolBody(R2.SymbolTableIndex)->repl();
Rui Ueyama871847e2015-06-28 01:30:54 +0000210 if (B1 == B2)
211 return true;
Rui Ueyama9b921e52015-06-25 22:00:42 +0000212 auto *D1 = dyn_cast<DefinedRegular>(B1);
213 auto *D2 = dyn_cast<DefinedRegular>(B2);
Rui Ueyama871847e2015-06-28 01:30:54 +0000214 return (D1 && D2 &&
215 D1->getValue() == D2->getValue() &&
216 D1->getChunk() == D2->getChunk());
Rui Ueyamac6fcfbc2015-06-25 17:51:07 +0000217 };
218 return std::equal(Relocs.begin(), Relocs.end(), X->Relocs.begin(), Eq);
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000219}
220
Rui Ueyamaf34c0882015-06-25 17:56:36 +0000221ArrayRef<uint8_t> SectionChunk::getContents() const {
222 ArrayRef<uint8_t> A;
223 File->getCOFFObj()->getSectionContents(Header, A);
224 return A;
225}
226
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000227void SectionChunk::replaceWith(SectionChunk *Other) {
Rui Ueyama9b921e52015-06-25 22:00:42 +0000228 Ptr = Other->Ptr;
Rui Ueyamaddf71fc2015-06-24 04:36:52 +0000229 Live = false;
230}
231
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000232CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000233 // Common symbols are aligned on natural boundaries up to 32 bytes.
234 // This is what MSVC link.exe does.
235 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000236}
237
Rui Ueyama411c63602015-05-28 19:09:30 +0000238uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000239 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
240 IMAGE_SCN_MEM_WRITE;
241}
242
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000243void StringChunk::writeTo(uint8_t *Buf) {
244 memcpy(Buf + FileOff, Str.data(), Str.size());
245}
246
Rui Ueyama73835622015-06-26 18:28:56 +0000247ImportThunkChunk::ImportThunkChunk(Defined *S) : ImpSymbol(S) {
248 // Intel Optimization Manual says that all branch targets
249 // should be 16-byte aligned. MSVC linker does this too.
250 Align = 16;
251}
252
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000253void ImportThunkChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyama33fb2cb2015-07-15 00:25:38 +0000254 if (!Config->is64())
255 Res->push_back(getRVA() + 2);
256}
257
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000258void ImportThunkChunk::writeTo(uint8_t *Buf) {
259 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000260 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama7c3e23f2015-07-09 01:25:49 +0000261 uint32_t Operand = Config->is64()
262 ? ImpSymbol->getRVA() - RVA - getSize()
263 : ImpSymbol->getRVA() + Config->ImageBase;
Rui Ueyama411c63602015-05-28 19:09:30 +0000264 write32le(Buf + FileOff + 2, Operand);
265}
266
Rui Ueyama3cb895c2015-07-24 22:58:44 +0000267void LocalImportChunk::getBaserels(std::vector<uint32_t> *Res) {
Rui Ueyamac851ccc2015-07-10 04:30:54 +0000268 Res->push_back(getRVA());
Rui Ueyama7a333c62015-07-02 20:33:50 +0000269}
270
Rui Ueyamad4b351f2015-07-09 21:15:58 +0000271size_t LocalImportChunk::getSize() const {
272 return Config->is64() ? 8 : 4;
273}
274
Rui Ueyama7a333c62015-07-02 20:33:50 +0000275void LocalImportChunk::writeTo(uint8_t *Buf) {
Rui Ueyamad4b351f2015-07-09 21:15:58 +0000276 if (Config->is64()) {
277 write64le(Buf + FileOff, Sym->getRVA() + Config->ImageBase);
278 } else {
279 write32le(Buf + FileOff, Sym->getRVA() + Config->ImageBase);
280 }
Rui Ueyama7a333c62015-07-02 20:33:50 +0000281}
282
Rui Ueyama588e8322015-06-15 01:23:58 +0000283// Windows-specific.
284// This class represents a block in .reloc section.
285BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
286 // Block header consists of 4 byte page RVA and 4 byte block size.
287 // Each entry is 2 byte. Last entry may be padding.
288 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
289 uint8_t *P = Data.data();
290 write32le(P, Page);
291 write32le(P + 4, Data.size());
292 P += 8;
Rui Ueyama2296dc12015-07-24 23:24:45 +0000293 uint16_t RelTy =
294 Config->is64() ? IMAGE_REL_BASED_DIR64 : IMAGE_REL_BASED_HIGHLOW;
Rui Ueyama588e8322015-06-15 01:23:58 +0000295 for (uint32_t *I = Begin; I != End; ++I) {
Rui Ueyama2296dc12015-07-24 23:24:45 +0000296 write16le(P, (RelTy << 12) | (*I - Page));
Rui Ueyama588e8322015-06-15 01:23:58 +0000297 P += 2;
298 }
299}
300
301void BaserelChunk::writeTo(uint8_t *Buf) {
302 memcpy(Buf + FileOff, Data.data(), Data.size());
303}
304
Rui Ueyama411c63602015-05-28 19:09:30 +0000305} // namespace coff
306} // namespace lld