blob: e6403c2fc50a35dcd5d341cc7b92f61040b470f7 [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
Rui Ueyamac6ea0572015-06-06 22:46:15 +000020using namespace llvm;
Rui Ueyama411c63602015-05-28 19:09:30 +000021using namespace llvm::object;
22using namespace llvm::support::endian;
23using namespace llvm::COFF;
Rui Ueyama411c63602015-05-28 19:09:30 +000024
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);
Rui Ueyama8b33f592015-06-10 04:21:47 +000032
Rui Ueyama2bf6a122015-06-14 21:50:50 +000033 // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
34 unsigned Shift = (Header->Characteristics >> 20) & 0xF;
35 if (Shift > 0)
36 Align = uint32_t(1) << (Shift - 1);
Rui Ueyama8b33f592015-06-10 04:21:47 +000037
38 // When a new chunk is created, we don't if if it's going to make it
39 // to the final output. Initially all sections are unmarked in terms
40 // of garbage collection. The writer will call markLive() to mark
41 // all reachable section chunks.
42 Live = false;
43
44 // COMDAT sections are not GC root. Non-text sections are not
45 // subject of garbage collection (thus they are root).
46 if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE))
47 Root = true;
Rui Ueyama411c63602015-05-28 19:09:30 +000048}
49
Rui Ueyamad6fefba42015-05-28 19:45:43 +000050void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000051 if (!hasData())
52 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000053 // Copy section contents from source object file to output file.
Rui Ueyama411c63602015-05-28 19:09:30 +000054 ArrayRef<uint8_t> Data;
55 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000056 memcpy(Buf + FileOff, Data.data(), Data.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000057
58 // Apply relocations.
59 for (const auto &I : getSectionRef().relocations()) {
60 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
61 applyReloc(Buf, Rel);
62 }
Rui Ueyama411c63602015-05-28 19:09:30 +000063}
64
Rui Ueyama8b33f592015-06-10 04:21:47 +000065void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000066 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000067 Live = true;
68
69 // Mark all symbols listed in the relocation table for this section.
70 for (const auto &I : getSectionRef().relocations()) {
71 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
72 SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex);
73 if (auto *Def = dyn_cast<Defined>(B))
74 Def->markLive();
75 }
76
77 // Mark associative sections if any.
78 for (Chunk *C : AssocChildren)
79 C->markLive();
80}
81
82void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +000083 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +000084 // Associative sections are live if their parent COMDATs are live,
85 // and vice versa, so they are not considered live by themselves.
86 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +000087}
88
Rui Ueyamaa6cd6c02015-06-07 23:10:50 +000089static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
Rui Ueyama411c63602015-05-28 19:09:30 +000090static void add32(uint8_t *P, int32_t V) { write32le(P, read32le(P) + V); }
91static void add64(uint8_t *P, int64_t V) { write64le(P, read64le(P) + V); }
92
93// Implements x64 PE/COFF relocations.
94void SectionChunk::applyReloc(uint8_t *Buf, const coff_relocation *Rel) {
Rui Ueyama411c63602015-05-28 19:09:30 +000095 uint8_t *Off = Buf + FileOff + Rel->VirtualAddress;
96 SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex);
97 uint64_t S = cast<Defined>(Body)->getRVA();
98 uint64_t P = RVA + Rel->VirtualAddress;
99 switch (Rel->Type) {
100 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
101 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
102 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
103 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
104 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
105 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
106 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
107 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
108 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
109 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
110 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
111 default:
112 llvm::report_fatal_error("Unsupported relocation type");
113 }
114}
115
Rui Ueyama588e8322015-06-15 01:23:58 +0000116// Windows-specific.
117// Collect all locations that contain absolute 64-bit addresses,
118// which need to be fixed by the loader if load-time relocation is needed.
119// Only called when base relocation is enabled.
120void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
121 for (const auto &I : getSectionRef().relocations()) {
122 // ADDR64 relocations contain absolute addresses.
123 // Symbol __ImageBase is special -- it's an absolute symbol, but its
124 // address never changes even if image is relocated.
125 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
126 if (Rel->Type != IMAGE_REL_AMD64_ADDR64)
127 continue;
128 SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex);
129 if (Body == ImageBase)
130 continue;
131 Res->push_back(RVA + Rel->VirtualAddress);
132 }
133}
134
Rui Ueyama411c63602015-05-28 19:09:30 +0000135bool SectionChunk::hasData() const {
136 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
137}
138
139uint32_t SectionChunk::getPermissions() const {
140 return Header->Characteristics & PermMask;
141}
142
143bool SectionChunk::isCOMDAT() const {
144 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
145}
146
147// Prints "Discarded <symbol>" for all external function symbols.
148void SectionChunk::printDiscardedMessage() {
149 uint32_t E = File->getCOFFObj()->getNumberOfSymbols();
150 for (uint32_t I = 0; I < E; ++I) {
151 auto SrefOrErr = File->getCOFFObj()->getSymbol(I);
152 COFFSymbolRef Sym = SrefOrErr.get();
153 if (uint32_t(Sym.getSectionNumber()) != SectionIndex)
154 continue;
155 if (!Sym.isFunctionDefinition())
156 continue;
157 StringRef SymbolName;
158 File->getCOFFObj()->getSymbolName(Sym, SymbolName);
Rui Ueyama5b2588a2015-06-08 05:43:50 +0000159 llvm::outs() << "Discarded " << SymbolName << " from "
Rui Ueyama411c63602015-05-28 19:09:30 +0000160 << File->getShortName() << "\n";
161 I += Sym.getNumberOfAuxSymbols();
162 }
163}
164
165SectionRef SectionChunk::getSectionRef() {
166 DataRefImpl Ref;
167 Ref.p = uintptr_t(Header);
168 return SectionRef(Ref, File->getCOFFObj());
169}
170
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000171CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
172 // Alignment is a section attribute, but common symbols don't
173 // belong to any section. How do we know common data alignments?
174 // Needs investigating. For now, we set a large number as an alignment.
175 Align = 16;
176}
177
Rui Ueyama411c63602015-05-28 19:09:30 +0000178uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000179 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
180 IMAGE_SCN_MEM_WRITE;
181}
182
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000183void StringChunk::writeTo(uint8_t *Buf) {
184 memcpy(Buf + FileOff, Str.data(), Str.size());
185}
186
187void ImportThunkChunk::writeTo(uint8_t *Buf) {
188 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000189 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000190 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000191 write32le(Buf + FileOff + 2, Operand);
192}
193
Rui Ueyama588e8322015-06-15 01:23:58 +0000194// Windows-specific.
195// This class represents a block in .reloc section.
196BaserelChunk::BaserelChunk(uint32_t Page, uint32_t *Begin, uint32_t *End) {
197 // Block header consists of 4 byte page RVA and 4 byte block size.
198 // Each entry is 2 byte. Last entry may be padding.
199 Data.resize(RoundUpToAlignment((End - Begin) * 2 + 8, 4));
200 uint8_t *P = Data.data();
201 write32le(P, Page);
202 write32le(P + 4, Data.size());
203 P += 8;
204 for (uint32_t *I = Begin; I != End; ++I) {
205 write16le(P, (IMAGE_REL_BASED_DIR64 << 12) | (*I - Page));
206 P += 2;
207 }
208}
209
210void BaserelChunk::writeTo(uint8_t *Buf) {
211 memcpy(Buf + FileOff, Data.data(), Data.size());
212}
213
Rui Ueyama411c63602015-05-28 19:09:30 +0000214} // namespace coff
215} // namespace lld