blob: 18963f283259c6504ba987a83127e8b772632ac9 [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"
Rui Ueyama5e31d0b2015-06-20 07:25:45 +000019#include <algorithm>
Rui Ueyama411c63602015-05-28 19:09:30 +000020
Rui Ueyamac6ea0572015-06-06 22:46:15 +000021using namespace llvm;
Rui Ueyama411c63602015-05-28 19:09:30 +000022using namespace llvm::object;
23using namespace llvm::support::endian;
24using namespace llvm::COFF;
Rui Ueyama411c63602015-05-28 19:09:30 +000025
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);
Rui Ueyama8b33f592015-06-10 04:21:47 +000033
Rui Ueyama2bf6a122015-06-14 21:50:50 +000034 // Bit [20:24] contains section alignment. Both 0 and 1 mean alignment 1.
35 unsigned Shift = (Header->Characteristics >> 20) & 0xF;
36 if (Shift > 0)
37 Align = uint32_t(1) << (Shift - 1);
Rui Ueyama8b33f592015-06-10 04:21:47 +000038
39 // When a new chunk is created, we don't if if it's going to make it
40 // to the final output. Initially all sections are unmarked in terms
41 // of garbage collection. The writer will call markLive() to mark
42 // all reachable section chunks.
43 Live = false;
44
45 // COMDAT sections are not GC root. Non-text sections are not
46 // subject of garbage collection (thus they are root).
47 if (!isCOMDAT() && !(Header->Characteristics & IMAGE_SCN_CNT_CODE))
48 Root = true;
Rui Ueyama411c63602015-05-28 19:09:30 +000049}
50
Rui Ueyamad6fefba42015-05-28 19:45:43 +000051void SectionChunk::writeTo(uint8_t *Buf) {
Rui Ueyama9aefa0c2015-05-28 20:04:51 +000052 if (!hasData())
53 return;
Rui Ueyama743afa02015-06-06 04:07:39 +000054 // Copy section contents from source object file to output file.
Rui Ueyama411c63602015-05-28 19:09:30 +000055 ArrayRef<uint8_t> Data;
56 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000057 memcpy(Buf + FileOff, Data.data(), Data.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000058
59 // Apply relocations.
60 for (const auto &I : getSectionRef().relocations()) {
61 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
62 applyReloc(Buf, Rel);
63 }
Rui Ueyama411c63602015-05-28 19:09:30 +000064}
65
Rui Ueyama8b33f592015-06-10 04:21:47 +000066void SectionChunk::mark() {
Rui Ueyama4108f3f2015-06-14 22:01:39 +000067 assert(!Live);
Rui Ueyama411c63602015-05-28 19:09:30 +000068 Live = true;
69
70 // Mark all symbols listed in the relocation table for this section.
71 for (const auto &I : getSectionRef().relocations()) {
72 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
73 SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex);
74 if (auto *Def = dyn_cast<Defined>(B))
75 Def->markLive();
76 }
77
78 // Mark associative sections if any.
79 for (Chunk *C : AssocChildren)
80 C->markLive();
81}
82
83void SectionChunk::addAssociative(SectionChunk *Child) {
Rui Ueyama411c63602015-05-28 19:09:30 +000084 AssocChildren.push_back(Child);
Rui Ueyama8b33f592015-06-10 04:21:47 +000085 // Associative sections are live if their parent COMDATs are live,
86 // and vice versa, so they are not considered live by themselves.
87 Child->Root = false;
Rui Ueyama411c63602015-05-28 19:09:30 +000088}
89
Rui Ueyamaa6cd6c02015-06-07 23:10:50 +000090static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
Rui Ueyama411c63602015-05-28 19:09:30 +000091static 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) {
Rui Ueyama411c63602015-05-28 19:09:30 +000096 uint8_t *Off = Buf + FileOff + Rel->VirtualAddress;
97 SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex);
98 uint64_t S = cast<Defined>(Body)->getRVA();
99 uint64_t P = RVA + Rel->VirtualAddress;
100 switch (Rel->Type) {
101 case IMAGE_REL_AMD64_ADDR32: add32(Off, S + Config->ImageBase); break;
102 case IMAGE_REL_AMD64_ADDR64: add64(Off, S + Config->ImageBase); break;
103 case IMAGE_REL_AMD64_ADDR32NB: add32(Off, S); break;
104 case IMAGE_REL_AMD64_REL32: add32(Off, S - P - 4); break;
105 case IMAGE_REL_AMD64_REL32_1: add32(Off, S - P - 5); break;
106 case IMAGE_REL_AMD64_REL32_2: add32(Off, S - P - 6); break;
107 case IMAGE_REL_AMD64_REL32_3: add32(Off, S - P - 7); break;
108 case IMAGE_REL_AMD64_REL32_4: add32(Off, S - P - 8); break;
109 case IMAGE_REL_AMD64_REL32_5: add32(Off, S - P - 9); break;
110 case IMAGE_REL_AMD64_SECTION: add16(Off, Out->getSectionIndex()); break;
111 case IMAGE_REL_AMD64_SECREL: add32(Off, S - Out->getRVA()); break;
112 default:
113 llvm::report_fatal_error("Unsupported relocation type");
114 }
115}
116
Rui Ueyama588e8322015-06-15 01:23:58 +0000117// Windows-specific.
118// Collect all locations that contain absolute 64-bit addresses,
119// which need to be fixed by the loader if load-time relocation is needed.
120// Only called when base relocation is enabled.
121void SectionChunk::getBaserels(std::vector<uint32_t> *Res, Defined *ImageBase) {
122 for (const auto &I : getSectionRef().relocations()) {
123 // ADDR64 relocations contain absolute addresses.
124 // Symbol __ImageBase is special -- it's an absolute symbol, but its
125 // address never changes even if image is relocated.
126 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
127 if (Rel->Type != IMAGE_REL_AMD64_ADDR64)
128 continue;
129 SymbolBody *Body = File->getSymbolBody(Rel->SymbolTableIndex);
130 if (Body == ImageBase)
131 continue;
132 Res->push_back(RVA + Rel->VirtualAddress);
133 }
134}
135
Rui Ueyama411c63602015-05-28 19:09:30 +0000136bool SectionChunk::hasData() const {
137 return !(Header->Characteristics & IMAGE_SCN_CNT_UNINITIALIZED_DATA);
138}
139
140uint32_t SectionChunk::getPermissions() const {
141 return Header->Characteristics & PermMask;
142}
143
144bool SectionChunk::isCOMDAT() const {
145 return Header->Characteristics & IMAGE_SCN_LNK_COMDAT;
146}
147
148// Prints "Discarded <symbol>" for all external function symbols.
149void SectionChunk::printDiscardedMessage() {
150 uint32_t E = File->getCOFFObj()->getNumberOfSymbols();
151 for (uint32_t I = 0; I < E; ++I) {
152 auto SrefOrErr = File->getCOFFObj()->getSymbol(I);
153 COFFSymbolRef Sym = SrefOrErr.get();
154 if (uint32_t(Sym.getSectionNumber()) != SectionIndex)
155 continue;
156 if (!Sym.isFunctionDefinition())
157 continue;
158 StringRef SymbolName;
159 File->getCOFFObj()->getSymbolName(Sym, SymbolName);
Rui Ueyama5b2588a2015-06-08 05:43:50 +0000160 llvm::outs() << "Discarded " << SymbolName << " from "
Rui Ueyama411c63602015-05-28 19:09:30 +0000161 << File->getShortName() << "\n";
162 I += Sym.getNumberOfAuxSymbols();
163 }
164}
165
166SectionRef SectionChunk::getSectionRef() {
167 DataRefImpl Ref;
168 Ref.p = uintptr_t(Header);
169 return SectionRef(Ref, File->getCOFFObj());
170}
171
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000172CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
Rui Ueyama5e31d0b2015-06-20 07:25:45 +0000173 // Common symbols are aligned on natural boundaries up to 32 bytes.
174 // This is what MSVC link.exe does.
175 Align = std::min(uint64_t(32), NextPowerOf2(Sym.getValue()));
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000176}
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