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