blob: ecd5b4102b0cc6ea7df415eedb21d7ab859d3ce3 [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;
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 Ueyama743afa02015-06-06 04:07:39 +000041 // Copy section contents from source object file to output file.
Rui Ueyama411c63602015-05-28 19:09:30 +000042 ArrayRef<uint8_t> Data;
43 File->getCOFFObj()->getSectionContents(Header, Data);
Rui Ueyamad6fefba42015-05-28 19:45:43 +000044 memcpy(Buf + FileOff, Data.data(), Data.size());
Rui Ueyama743afa02015-06-06 04:07:39 +000045
46 // Apply relocations.
47 for (const auto &I : getSectionRef().relocations()) {
48 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
49 applyReloc(Buf, Rel);
50 }
Rui Ueyama411c63602015-05-28 19:09:30 +000051}
52
53// Returns true if this chunk should be considered as a GC root.
54bool SectionChunk::isRoot() {
55 // COMDAT sections are live only when they are referenced by something else.
56 if (isCOMDAT())
57 return false;
58
59 // Associative sections are live if their parent COMDATs are live,
60 // and vice versa, so they are not considered live by themselves.
61 if (IsAssocChild)
62 return false;
63
64 // Only code is subject of dead-stripping.
65 return !(Header->Characteristics & IMAGE_SCN_CNT_CODE);
66}
67
68void SectionChunk::markLive() {
69 if (Live)
70 return;
71 Live = true;
72
73 // Mark all symbols listed in the relocation table for this section.
74 for (const auto &I : getSectionRef().relocations()) {
75 const coff_relocation *Rel = File->getCOFFObj()->getCOFFRelocation(I);
76 SymbolBody *B = File->getSymbolBody(Rel->SymbolTableIndex);
77 if (auto *Def = dyn_cast<Defined>(B))
78 Def->markLive();
79 }
80
81 // Mark associative sections if any.
82 for (Chunk *C : AssocChildren)
83 C->markLive();
84}
85
86void SectionChunk::addAssociative(SectionChunk *Child) {
87 Child->IsAssocChild = true;
88 AssocChildren.push_back(Child);
89}
90
Rui Ueyamaa6cd6c02015-06-07 23:10:50 +000091static void add16(uint8_t *P, int16_t V) { write16le(P, read16le(P) + V); }
Rui Ueyama411c63602015-05-28 19:09:30 +000092static 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) {
Rui Ueyama411c63602015-05-28 19:09:30 +000097 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
Rui Ueyama9cf1abb2015-06-08 03:17:07 +0000154CommonChunk::CommonChunk(const COFFSymbolRef S) : Sym(S) {
155 // Alignment is a section attribute, but common symbols don't
156 // belong to any section. How do we know common data alignments?
157 // Needs investigating. For now, we set a large number as an alignment.
158 Align = 16;
159}
160
Rui Ueyama411c63602015-05-28 19:09:30 +0000161uint32_t CommonChunk::getPermissions() const {
Rui Ueyama411c63602015-05-28 19:09:30 +0000162 return IMAGE_SCN_CNT_UNINITIALIZED_DATA | IMAGE_SCN_MEM_READ |
163 IMAGE_SCN_MEM_WRITE;
164}
165
Rui Ueyamad6fefba42015-05-28 19:45:43 +0000166void StringChunk::writeTo(uint8_t *Buf) {
167 memcpy(Buf + FileOff, Str.data(), Str.size());
168}
169
170void ImportThunkChunk::writeTo(uint8_t *Buf) {
171 memcpy(Buf + FileOff, ImportThunkData, sizeof(ImportThunkData));
Rui Ueyama743afa02015-06-06 04:07:39 +0000172 // The first two bytes is a JMP instruction. Fill its operand.
Rui Ueyama411c63602015-05-28 19:09:30 +0000173 uint32_t Operand = ImpSymbol->getRVA() - RVA - getSize();
Rui Ueyama411c63602015-05-28 19:09:30 +0000174 write32le(Buf + FileOff + 2, Operand);
175}
176
Rui Ueyama411c63602015-05-28 19:09:30 +0000177} // namespace coff
178} // namespace lld