blob: a321d4639a4b6994963282633e9e5d65739b17f8 [file] [log] [blame]
Sam Cleggf61910d2018-01-12 22:18:22 +00001//===- InputChunks.cpp ----------------------------------------------------===//
Sam Cleggc94d3932017-11-17 18:14:09 +00002//
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
Sam Clegg5fa274b2018-01-10 01:13:34 +000010#include "InputChunks.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000011#include "Config.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "OutputSegment.h"
Rui Ueyamabf450d92018-02-20 04:26:26 +000013#include "WriterUtils.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000014#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/LLVM.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000016#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000017
18#define DEBUG_TYPE "lld"
19
20using namespace llvm;
Sam Cleggd96d9352018-01-10 19:22:42 +000021using namespace llvm::wasm;
Rui Ueyamae351c3a2018-02-16 20:38:15 +000022using namespace llvm::support::endian;
Sam Cleggd96d9352018-01-10 19:22:42 +000023using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000024using namespace lld::wasm;
25
Benjamin Kramer54550382018-05-15 22:01:54 +000026static StringRef ReloctTypeToString(uint8_t RelocType) {
Sam Cleggc1be8232018-03-11 01:35:02 +000027 switch (RelocType) {
28#define WASM_RELOC(NAME, REL) case REL: return #NAME;
29#include "llvm/BinaryFormat/WasmRelocs.def"
30#undef WASM_RELOC
31 }
32 llvm_unreachable("unknown reloc type");
33}
34
Rui Ueyama81bee042018-02-19 22:29:48 +000035std::string lld::toString(const InputChunk *C) {
36 return (toString(C->File) + ":(" + C->getName() + ")").str();
37}
38
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000039StringRef InputChunk::getComdatName() const {
40 uint32_t Index = getComdat();
41 if (Index == UINT32_MAX)
42 return StringRef();
43 return File->getWasmObj()->linkingData().Comdats[Index];
44}
45
Sam Clegg5fa274b2018-01-10 01:13:34 +000046void InputChunk::copyRelocations(const WasmSection &Section) {
Sam Clegg50686852018-01-12 18:35:13 +000047 if (Section.Relocations.empty())
48 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000049 size_t Start = getInputSectionOffset();
Sam Cleggfb983cd2018-05-18 23:28:05 +000050 size_t Size = getInputSize();
Sam Clegg5fa274b2018-01-10 01:13:34 +000051 for (const WasmRelocation &R : Section.Relocations)
52 if (R.Offset >= Start && R.Offset < Start + Size)
53 Relocations.push_back(R);
54}
Sam Cleggd96d9352018-01-10 19:22:42 +000055
Sam Cleggc1953142018-05-05 00:18:43 +000056void InputChunk::verifyRelocTargets() const {
57 for (const WasmRelocation &Rel : Relocations) {
58 uint32_t ExistingValue;
59 unsigned BytesRead = 0;
60 uint32_t Offset = Rel.Offset - getInputSectionOffset();
61 const uint8_t *Loc = data().data() + Offset;
62 switch (Rel.Type) {
63 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
64 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
65 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
66 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
67 ExistingValue = decodeULEB128(Loc, &BytesRead);
68 break;
69 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
70 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
71 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
72 break;
73 case R_WEBASSEMBLY_TABLE_INDEX_I32:
74 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
75 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
76 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
77 ExistingValue = static_cast<uint32_t>(read32le(Loc));
78 break;
79 default:
80 llvm_unreachable("unknown relocation type");
81 }
82
83 if (BytesRead && BytesRead != 5)
84 warn("expected LEB at relocation site be 5-byte padded");
85 uint32_t ExpectedValue = File->calcExpectedValue(Rel);
86 if (ExpectedValue != ExistingValue)
87 warn("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
88 ": existing=" + Twine(ExistingValue) +
89 " expected=" + Twine(ExpectedValue));
90 }
91}
92
Rui Ueyamabf450d92018-02-20 04:26:26 +000093// Copy this input chunk to an mmap'ed output file and apply relocations.
94void InputChunk::writeTo(uint8_t *Buf) const {
95 // Copy contents
96 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000097
Rui Ueyamabf450d92018-02-20 04:26:26 +000098 // Apply relocations
99 if (Relocations.empty())
100 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +0000101
Sam Cleggc1953142018-05-05 00:18:43 +0000102#ifndef NDEBUG
103 verifyRelocTargets();
104#endif
105
Nicola Zaghene7245b42018-05-15 13:36:20 +0000106 LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
107 << " count=" << Relocations.size() << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000108 int32_t Off = OutputOffset - getInputSectionOffset();
109
110 for (const WasmRelocation &Rel : Relocations) {
111 uint8_t *Loc = Buf + Rel.Offset + Off;
Sam Cleggc1be8232018-03-11 01:35:02 +0000112 uint32_t Value = File->calcNewValue(Rel);
Nicola Zaghene7245b42018-05-15 13:36:20 +0000113 LLVM_DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
114 << " addend=" << Rel.Addend << " index=" << Rel.Index
115 << " value=" << Value << " offset=" << Rel.Offset
116 << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000117
118 switch (Rel.Type) {
119 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
120 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
121 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
122 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
123 encodeULEB128(Value, Loc, 5);
124 break;
125 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
126 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
127 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
128 break;
129 case R_WEBASSEMBLY_TABLE_INDEX_I32:
130 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggd177ab22018-05-04 23:14:42 +0000131 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
132 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000133 write32le(Loc, Value);
134 break;
135 default:
136 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +0000137 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000138 }
139}
140
Rui Ueyamabf450d92018-02-20 04:26:26 +0000141// Copy relocation entries to a given output stream.
142// This function is used only when a user passes "-r". For a regular link,
143// we consume relocations instead of copying them to an output file.
144void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000145 if (Relocations.empty())
146 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000147
148 int32_t Off = OutputOffset - getInputSectionOffset();
Nicola Zaghene7245b42018-05-15 13:36:20 +0000149 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
150 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000151
Rui Ueyamabf450d92018-02-20 04:26:26 +0000152 for (const WasmRelocation &Rel : Relocations) {
153 writeUleb128(OS, Rel.Type, "reloc type");
154 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
155 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +0000156
Rui Ueyamabf450d92018-02-20 04:26:26 +0000157 switch (Rel.Type) {
158 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
159 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
160 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggd177ab22018-05-04 23:14:42 +0000161 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
162 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
163 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000164 break;
165 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000166 }
167}
Sam Clegg50686852018-01-12 18:35:13 +0000168
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000169void InputFunction::setFunctionIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000170 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
171 << " -> " << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000172 assert(!hasFunctionIndex());
173 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000174}
Sam Clegg67abf532018-01-24 21:45:25 +0000175
176void InputFunction::setTableIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000177 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
178 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000179 assert(!hasTableIndex());
180 TableIndex = Index;
181}
Sam Cleggfb983cd2018-05-18 23:28:05 +0000182
183// Write a relocation value without padding and return the number of bytes
184// witten.
185static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
186 uint32_t Value) {
187 switch (Rel.Type) {
188 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
189 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
190 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
191 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
192 return encodeULEB128(Value, Buf);
193 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
194 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
195 return encodeSLEB128(static_cast<int32_t>(Value), Buf);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000196 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000197 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000198 }
199}
200
201static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
202 switch (Rel.Type) {
203 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
204 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
205 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
206 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
207 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
208 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
209 return 5;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000210 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000211 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000212 }
213}
214
215static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
216 uint8_t Buf[5];
217 return writeCompressedReloc(Buf, Rel, Value);
218}
219
220// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
221// so that a fast linker can blindly overwrite them without needing to worry
222// about the number of bytes needed to encode the values.
223// However, for optimal output the code section can be compressed to remove
224// the padding then outputting non-relocatable files.
225// In this case we need to perform a size calculation based on the value at each
226// relocation. At best we end up saving 4 bytes for each relocation entry.
227//
228// This function only computes the final output size. It must be called
229// before getSize() is used to calculate of layout of the code section.
230void InputFunction::calculateSize() {
231 if (!File || !Config->CompressRelocTargets)
232 return;
233
234 DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
235
236 const uint8_t *SecStart = File->CodeSection->Content.data();
237 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
238 uint32_t FunctionSizeLength;
239 decodeULEB128(FuncStart, &FunctionSizeLength);
240
241 uint32_t Start = getInputSectionOffset();
242 uint32_t End = Start + Function->Size;
243
244 uint32_t LastRelocEnd = Start + FunctionSizeLength;
245 for (WasmRelocation &Rel : Relocations) {
246 DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n");
247 CompressedFuncSize += Rel.Offset - LastRelocEnd;
248 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
249 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
250 }
251 DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n");
252 CompressedFuncSize += End - LastRelocEnd;
253
254 // Now we know how long the resulting function is we can add the encoding
255 // of its length
256 uint8_t Buf[5];
257 CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
258
259 DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n");
260 DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n");
261}
262
263// Override the default writeTo method so that we can (optionally) write the
264// compressed version of the function.
265void InputFunction::writeTo(uint8_t *Buf) const {
266 if (!File || !Config->CompressRelocTargets)
267 return InputChunk::writeTo(Buf);
268
269 Buf += OutputOffset;
Richard Trieu6ac21162018-05-19 00:37:23 +0000270 uint8_t *Orig = Buf; (void)Orig;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000271
272 const uint8_t *SecStart = File->CodeSection->Content.data();
273 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
274 const uint8_t *End = FuncStart + Function->Size;
275 uint32_t Count;
Sam Clegg65a91282018-05-22 17:06:55 +0000276 decodeULEB128(FuncStart, &Count);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000277 FuncStart += Count;
278
279 DEBUG(dbgs() << "write func: " << getName() << "\n");
280 Buf += encodeULEB128(CompressedFuncSize, Buf);
281 const uint8_t *LastRelocEnd = FuncStart;
282 for (const WasmRelocation &Rel : Relocations) {
283 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
284 DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n");
285 memcpy(Buf, LastRelocEnd, ChunkSize);
286 Buf += ChunkSize;
287 Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
288 LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
289 }
290
291 unsigned ChunkSize = End - LastRelocEnd;
292 DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n");
293 memcpy(Buf, LastRelocEnd, ChunkSize);
294 DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n");
295}