blob: 8f248856fe1b2e7ef9218405a3ea7a33e157b405 [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"
13#include "lld/Core/Error.h"
14#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"
20
21using namespace llvm::object;
22using namespace llvm::support::endian;
23using namespace llvm::COFF;
24using llvm::RoundUpToAlignment;
25
26namespace lld {
27namespace coff {
28
29SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H, uint32_t SI)
30 : File(F), Header(H), SectionIndex(SI) {
31 // Initialize SectionName.
32 File->getCOFFObj()->getSectionName(Header, SectionName);
33 // Bit [20:24] contains section alignment.
34 unsigned Shift = ((Header->Characteristics & 0xF00000) >> 20) - 1;
35 Align = uint32_t(1) << Shift;
36}
37
Rui Ueyamad6fefba42015-05-28 19:45:43 +000038void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000039 if (!hasData())
40 return;
Rui Ueyama411c63602015-05-28 19:09:30 +000041 ArrayRef<uint8_t> Data;
42 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000043 memcpy(Buf + FileOff, Data.data(), Data.size());
Rui Ueyama411c63602015-05-28 19:09:30 +000044}
45
46// Returns true if this chunk should be considered as a GC root.
47bool SectionChunk::isRoot() {
48 // COMDAT sections are live only when they are referenced by something else.
49 if (isCOMDAT())
50 return false;
51
52 // Associative sections are live if their parent COMDATs are live,
53 // and vice versa, so they are not considered live by themselves.
54 if (IsAssocChild)
55 return false;
56
57 // Only code is subject of dead-stripping.
58 return !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
59}
60
61void SectionChunk::markLive() {
62 if (Live)
63 return;
64 Live = true;
65
66 // Mark all symbols listed in the relocation table for this section.
67 for (const auto &I : getSectionRef().relocations()) {
68 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
69 SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex);
70 if (auto *Def = dyn_cast<Defined>(B))
71 Def->markLive();
72 }
73
74 // Mark associative sections if any.
75 for (Chunk *C : AssocChildren)
76 C->markLive();
77}
78
79void SectionChunk::addAssociative(SectionChunk *Child) {
80 Child->IsAssocChild = true;
81 AssocChildren.push_back(Child);
82}
83
84void SectionChunk::applyRelocations(uint8_t *Buf) {
85 for (const auto &I : getSectionRef().relocations()) {
86 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
87 applyReloc(Buf, Rel);
88 }
89}
90
91static void add16(uint8_t *P, int32_t V) { write16le(P, read16le(P) + V); }
92static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
93static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
94
95// Implements x64 PE/COFF relocations.
96void SectionChunk::applyReloc(uint8_t *Buf, const coff_relocation *Rel) {
97 using namespace llvm::COFF;
98 uint8_t *Off = Buf + FileOff + Rel->VirtualAddress;
99 SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex);
100 uint64_t S = cast<Defined>(Body)->getRVA();
101 uint64_t P = RVA + Rel->VirtualAddress;
102 switch (Rel->Type) {
103 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
104 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
105 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
106 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
107 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
108 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
109 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
110 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
111 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
112 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
113 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
114 default:
115 llvm::report_fatal_error("Unsupported relocation type");
116 }
117}
118
119bool SectionChunk::hasData() const {
120 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
121}
122
123uint32_t SectionChunk::getPermissions() const {
124 return Header->Characteristics & PermMask;
125}
126
127bool SectionChunk::isCOMDAT() const {
128 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
129}
130
131// Prints "Discarded <symbol>" for all external function symbols.
132void SectionChunk::printDiscardedMessage() {
133 uint32_t E = File->getCOFFObj()->getNumberOfSymbols();
134 for (uint32_t I = 0; I < E; ++I) {
135 auto SrefOrErr = File->getCOFFObj()->getSymbol(I);
136 COFFSymbolRef Sym = SrefOrErr.get();
137 if (uint32_t(Sym.getSectionNumber()) != SectionIndex)
138 continue;
139 if (!Sym.isFunctionDefinition())
140 continue;
141 StringRef SymbolName;
142 File->getCOFFObj()->getSymbolName(Sym, SymbolName);
143 llvm::dbgs() << "Discarded " << SymbolName << " from "
144 << File->getShortName() << "\n";
145 I += Sym.getNumberOfAuxSymbols();
146 }
147}
148
149SectionRef SectionChunk::getSectionRef() {
150 DataRefImpl Ref;
151 Ref.p = uintptr_t(Header);
152 return SectionRef(Ref, File->getCOFFObj());
153}
154
155uint32_t CommonChunk::getPermissions() const {
156 using namespace llvm::COFF;
157 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
158 IMAGE_SCN_MEM_WRITE;
159}
160
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000161void StringChunk::writeTo(uint8_t *Buf) {
162 memcpy(Buf + FileOff, Str.data(), Str.size());
163}
164
165void ImportThunkChunk::writeTo(uint8_t *Buf) {
166 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama411c63602015-05-28 19:09:30 +0000167}
168
169void ImportThunkChunk::applyRelocations(uint8_t *Buf) {
170 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
171 // The first two bytes are a JMP instruction. Fill its operand.
172 write32le(Buf + FileOff + 2, Operand);
173}
174
Rui Ueyamac9bfe322015-05-29 15:45:35 +0000175HintNameChunk::HintNameChunk(StringRef N, uint16_t H)
176 : Name(N), Hint(H), Size(RoundUpToAlignment(Name.size() + 4, 2)) {}
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000177
178void HintNameChunk::writeTo(uint8_t *Buf) {
Rui Ueyamac9bfe322015-05-29 15:45:35 +0000179 write16le(Buf + FileOff, Hint);
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000180 memcpy(Buf + FileOff + 2, Name.data(), Name.size());
Rui Ueyama411c63602015-05-28 19:09:30 +0000181}
182
183void LookupChunk::applyRelocations(uint8_t *Buf) {
184 write32le(Buf + FileOff, HintName->getRVA());
185}
186
187void DirectoryChunk::applyRelocations(uint8_t *Buf) {
188 auto *E = (coff_import_directory_table_entry *)(Buf + FileOff);
189 E->ImportLookupTableRVA = LookupTab->getRVA();
190 E->NameRVA = DLLName->getRVA();
191 E->ImportAddressTableRVA = AddressTab->getRVA();
192}
193
194ImportTable::ImportTable(StringRef N,
195 std::vector<DefinedImportData *> &Symbols) {
196 DLLName = new StringChunk(N);
197 DirTab = new DirectoryChunk(DLLName);
198 for (DefinedImportData *S : Symbols)
Rui Ueyamac9bfe322015-05-29 15:45:35 +0000199 HintNameTables.push_back(
200 new HintNameChunk(S->getExportName(), S->getOrdinal()));
Rui Ueyama411c63602015-05-28 19:09:30 +0000201
202 for (HintNameChunk *H : HintNameTables) {
203 LookupTables.push_back(new LookupChunk(H));
204 AddressTables.push_back(new LookupChunk(H));
205 }
206
207 for (int I = 0, E = Symbols.size(); I < E; ++I)
208 Symbols[I]->setLocation(AddressTables[I]);
209
210 DirTab->LookupTab = LookupTables[0];
211 DirTab->AddressTab = AddressTables[0];
212}
213
214} // namespace coff
215} // namespace lld