blob: 7d8fbafe342be97da809a9f275626247a027589d [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 Ueyama411c63602015-05-28 19:09:30 +000013#include "llvm/ADT/STLExtras.h"
14#include "llvm/Object/COFF.h"
15#include "llvm/Support/COFF.h"
16#include "llvm/Support/Debug.h"
17#include "llvm/Support/Endian.h"
18#include "llvm/Support/raw_ostream.h"
19
20using namespace llvm::object;
21using namespace llvm::support::endian;
22using namespace llvm::COFF;
23using llvm::RoundUpToAlignment;
24
25namespace lld {
26namespace coff {
27
28SectionChunk::SectionChunk(ObjectFile *F, const coff_section *H, uint32_t SI)
29 : File(F), Header(H), SectionIndex(SI) {
30 // Initialize SectionName.
31 File->getCOFFObj()->getSectionName(Header, SectionName);
32 // Bit [20:24] contains section alignment.
33 unsigned Shift = ((Header->Characteristics & 0xF00000) >> 20) - 1;
34 Align = uint32_t(1) << Shift;
35}
36
Rui Ueyamad6fefba42015-05-28 19:45:43 +000037void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000038 if (!hasData())
39 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000040 // Copy section contents from source object file to output file.
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 Ueyama743afa02015-06-06 04:07:39 +000044
45 // Apply relocations.
46 for (const auto &I : getSectionRef().relocations()) {
47 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
48 applyReloc(Buf, Rel);
49 }
Rui Ueyama411c63602015-05-28 19:09:30 +000050}
51
52// Returns true if this chunk should be considered as a GC root.
53bool SectionChunk::isRoot() {
54 // COMDAT sections are live only when they are referenced by something else.
55 if (isCOMDAT())
56 return false;
57
58 // Associative sections are live if their parent COMDATs are live,
59 // and vice versa, so they are not considered live by themselves.
60 if (IsAssocChild)
61 return false;
62
63 // Only code is subject of dead-stripping.
64 return !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
65}
66
67void SectionChunk::markLive() {
68 if (Live)
69 return;
70 Live = true;
71
72 // Mark all symbols listed in the relocation table for this section.
73 for (const auto &I : getSectionRef().relocations()) {
74 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
75 SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex);
76 if (auto *Def = dyn_cast<Defined>(B))
77 Def->markLive();
78 }
79
80 // Mark associative sections if any.
81 for (Chunk *C : AssocChildren)
82 C->markLive();
83}
84
85void SectionChunk::addAssociative(SectionChunk *Child) {
86 Child->IsAssocChild = true;
87 AssocChildren.push_back(Child);
88}
89
Rui Ueyama411c63602015-05-28 19:09:30 +000090static 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
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000160void StringChunk::writeTo(uint8_t *Buf) {
161 memcpy(Buf + FileOff, Str.data(), Str.size());
162}
163
164void ImportThunkChunk::writeTo(uint8_t *Buf) {
165 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000166 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000167 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000168 write32le(Buf + FileOff + 2, Operand);
169}
170
Rui Ueyama5b25edd2015-06-01 03:55:04 +0000171size_t HintNameChunk::getSize() const {
172 // Starts with 2 byte Hint field, followed by a null-terminated string,
173 // ends with 0 or 1 byte padding.
174 return RoundUpToAlignment(Name.size() + 3, 2);
175}
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000176
177void HintNameChunk::writeTo(uint8_t *Buf) {
Rui Ueyamac9bfe322015-05-29 15:45:35 +0000178 write16le(Buf + FileOff, Hint);
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000179 memcpy(Buf + FileOff + 2, Name.data(), Name.size());
Rui Ueyama411c63602015-05-28 19:09:30 +0000180}
181
Rui Ueyama743afa02015-06-06 04:07:39 +0000182void LookupChunk::writeTo(uint8_t *Buf) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000183 write32le(Buf + FileOff, HintName->getRVA());
184}
185
Rui Ueyamafd99e012015-06-01 21:05:27 +0000186void OrdinalOnlyChunk::writeTo(uint8_t *Buf) {
187 // An import-by-ordinal slot has MSB 1 to indicate that
188 // this is import-by-ordinal (and not import-by-name).
189 write64le(Buf + FileOff, (uint64_t(1) << 63) | Ordinal);
190}
191
Rui Ueyama743afa02015-06-06 04:07:39 +0000192void DirectoryChunk::writeTo(uint8_t *Buf) {
Rui Ueyama411c63602015-05-28 19:09:30 +0000193 auto *E = (coff_import_directory_table_entry *)(Buf + FileOff);
194 E->ImportLookupTableRVA = LookupTab->getRVA();
195 E->NameRVA = DLLName->getRVA();
196 E->ImportAddressTableRVA = AddressTab->getRVA();
197}
198
199ImportTable::ImportTable(StringRef N,
200 std::vector<DefinedImportData *> &Symbols) {
Rui Ueyamafd99e012015-06-01 21:05:27 +0000201 // Create the import table hader.
Rui Ueyama411c63602015-05-28 19:09:30 +0000202 DLLName = new StringChunk(N);
203 DirTab = new DirectoryChunk(DLLName);
Rui Ueyama411c63602015-05-28 19:09:30 +0000204
Rui Ueyamafd99e012015-06-01 21:05:27 +0000205 // Create lookup and address tables. If they have external names,
206 // we need to create HintName chunks to store the names.
207 // If they don't (if they are import-by-ordinals), we store only
208 // ordinal values to the table.
209 for (DefinedImportData *S : Symbols) {
210 if (S->getExternalName().empty()) {
211 LookupTables.push_back(new OrdinalOnlyChunk(S->getOrdinal()));
212 AddressTables.push_back(new OrdinalOnlyChunk(S->getOrdinal()));
213 continue;
214 }
215 Chunk *C = new HintNameChunk(S->getExternalName(), S->getOrdinal());
216 HintNameTables.push_back(C);
217 LookupTables.push_back(new LookupChunk(C));
218 AddressTables.push_back(new LookupChunk(C));
Rui Ueyama411c63602015-05-28 19:09:30 +0000219 }
Rui Ueyama411c63602015-05-28 19:09:30 +0000220 for (int I = 0, E = Symbols.size(); I < E; ++I)
221 Symbols[I]->setLocation(AddressTables[I]);
Rui Ueyama411c63602015-05-28 19:09:30 +0000222 DirTab->LookupTab = LookupTables[0];
223 DirTab->AddressTab = AddressTables[0];
224}
225
226} // namespace coff
227} // namespace lld