blob: cb62f96c35b084ca659c0683a7728bf75a297600 [file] [log] [blame]
Sam Cleggf61910d2018-01-12 22:18:22 +00001//===- InputChunks.cpp ----------------------------------------------------===//
Sam Cleggc94d3932017-11-17 18:14:09 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Sam Cleggc94d3932017-11-17 18:14:09 +00006//
7//===----------------------------------------------------------------------===//
8
Sam Clegg5fa274b2018-01-10 01:13:34 +00009#include "InputChunks.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000010#include "Config.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000011#include "OutputSegment.h"
Rui Ueyamabf450d92018-02-20 04:26:26 +000012#include "WriterUtils.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000013#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000014#include "lld/Common/LLVM.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000015#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000016
17#define DEBUG_TYPE "lld"
18
19using namespace llvm;
Sam Cleggd96d9352018-01-10 19:22:42 +000020using namespace llvm::wasm;
Rui Ueyamae351c3a2018-02-16 20:38:15 +000021using namespace llvm::support::endian;
Sam Cleggd96d9352018-01-10 19:22:42 +000022using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000023using namespace lld::wasm;
24
Sam Clegge3a845e2019-03-29 22:56:39 +000025StringRef lld::relocTypeToString(uint8_t RelocType) {
Sam Cleggc1be8232018-03-11 01:35:02 +000026 switch (RelocType) {
Heejin Ahn4821ebf2018-08-29 21:03:16 +000027#define WASM_RELOC(NAME, REL) \
28 case REL: \
29 return #NAME;
Sam Cleggc1be8232018-03-11 01:35:02 +000030#include "llvm/BinaryFormat/WasmRelocs.def"
31#undef WASM_RELOC
32 }
33 llvm_unreachable("unknown reloc type");
34}
35
Rui Ueyama81bee042018-02-19 22:29:48 +000036std::string lld::toString(const InputChunk *C) {
37 return (toString(C->File) + ":(" + C->getName() + ")").str();
38}
39
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000040StringRef InputChunk::getComdatName() const {
41 uint32_t Index = getComdat();
42 if (Index == UINT32_MAX)
43 return StringRef();
44 return File->getWasmObj()->linkingData().Comdats[Index];
45}
46
Sam Cleggc1953142018-05-05 00:18:43 +000047void InputChunk::verifyRelocTargets() const {
48 for (const WasmRelocation &Rel : Relocations) {
49 uint32_t ExistingValue;
50 unsigned BytesRead = 0;
51 uint32_t Offset = Rel.Offset - getInputSectionOffset();
52 const uint8_t *Loc = data().data() + Offset;
53 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +000054 case R_WASM_TYPE_INDEX_LEB:
55 case R_WASM_FUNCTION_INDEX_LEB:
56 case R_WASM_GLOBAL_INDEX_LEB:
57 case R_WASM_EVENT_INDEX_LEB:
58 case R_WASM_MEMORY_ADDR_LEB:
Sam Cleggc1953142018-05-05 00:18:43 +000059 ExistingValue = decodeULEB128(Loc, &BytesRead);
60 break;
Sam Clegg79e33172019-02-04 17:49:33 +000061 case R_WASM_TABLE_INDEX_SLEB:
Sam Clegg2a7cac92019-04-04 17:43:50 +000062 case R_WASM_TABLE_INDEX_REL_SLEB:
Sam Clegg79e33172019-02-04 17:49:33 +000063 case R_WASM_MEMORY_ADDR_SLEB:
Sam Clegg2a7cac92019-04-04 17:43:50 +000064 case R_WASM_MEMORY_ADDR_REL_SLEB:
Sam Cleggc1953142018-05-05 00:18:43 +000065 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
66 break;
Sam Clegg79e33172019-02-04 17:49:33 +000067 case R_WASM_TABLE_INDEX_I32:
68 case R_WASM_MEMORY_ADDR_I32:
69 case R_WASM_FUNCTION_OFFSET_I32:
70 case R_WASM_SECTION_OFFSET_I32:
Sam Cleggc1953142018-05-05 00:18:43 +000071 ExistingValue = static_cast<uint32_t>(read32le(Loc));
72 break;
73 default:
74 llvm_unreachable("unknown relocation type");
75 }
76
77 if (BytesRead && BytesRead != 5)
78 warn("expected LEB at relocation site be 5-byte padded");
Sam Clegg492f7522019-03-26 19:46:15 +000079
80 if (Rel.Type != R_WASM_GLOBAL_INDEX_LEB) {
81 uint32_t ExpectedValue = File->calcExpectedValue(Rel);
82 if (ExpectedValue != ExistingValue)
Sam Clegge3a845e2019-03-29 22:56:39 +000083 warn("unexpected existing value for " + relocTypeToString(Rel.Type) +
Sam Clegg492f7522019-03-26 19:46:15 +000084 ": existing=" + Twine(ExistingValue) +
85 " expected=" + Twine(ExpectedValue));
86 }
Sam Cleggc1953142018-05-05 00:18:43 +000087 }
88}
89
Rui Ueyamabf450d92018-02-20 04:26:26 +000090// Copy this input chunk to an mmap'ed output file and apply relocations.
91void InputChunk::writeTo(uint8_t *Buf) const {
92 // Copy contents
93 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000094
Rui Ueyamabf450d92018-02-20 04:26:26 +000095 // Apply relocations
96 if (Relocations.empty())
97 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +000098
Sam Cleggc1953142018-05-05 00:18:43 +000099#ifndef NDEBUG
100 verifyRelocTargets();
101#endif
102
Nicola Zaghene7245b42018-05-15 13:36:20 +0000103 LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
104 << " count=" << Relocations.size() << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000105 int32_t Off = OutputOffset - getInputSectionOffset();
106
107 for (const WasmRelocation &Rel : Relocations) {
108 uint8_t *Loc = Buf + Rel.Offset + Off;
Sam Cleggc1be8232018-03-11 01:35:02 +0000109 uint32_t Value = File->calcNewValue(Rel);
Sam Clegg15006462019-07-08 10:35:08 +0000110 LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type));
111 if (Rel.Type != R_WASM_TYPE_INDEX_LEB)
112 LLVM_DEBUG(dbgs() << " sym=" << File->getSymbols()[Rel.Index]->getName());
113 LLVM_DEBUG(dbgs() << " addend=" << Rel.Addend << " index=" << Rel.Index
Nicola Zaghene7245b42018-05-15 13:36:20 +0000114 << " value=" << Value << " offset=" << Rel.Offset
115 << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000116
117 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000118 case R_WASM_TYPE_INDEX_LEB:
119 case R_WASM_FUNCTION_INDEX_LEB:
120 case R_WASM_GLOBAL_INDEX_LEB:
121 case R_WASM_EVENT_INDEX_LEB:
122 case R_WASM_MEMORY_ADDR_LEB:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000123 encodeULEB128(Value, Loc, 5);
124 break;
Sam Clegg79e33172019-02-04 17:49:33 +0000125 case R_WASM_TABLE_INDEX_SLEB:
Sam Clegg2a7cac92019-04-04 17:43:50 +0000126 case R_WASM_TABLE_INDEX_REL_SLEB:
Sam Clegg79e33172019-02-04 17:49:33 +0000127 case R_WASM_MEMORY_ADDR_SLEB:
Sam Clegg2a7cac92019-04-04 17:43:50 +0000128 case R_WASM_MEMORY_ADDR_REL_SLEB:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000129 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
130 break;
Sam Clegg79e33172019-02-04 17:49:33 +0000131 case R_WASM_TABLE_INDEX_I32:
132 case R_WASM_MEMORY_ADDR_I32:
133 case R_WASM_FUNCTION_OFFSET_I32:
134 case R_WASM_SECTION_OFFSET_I32:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000135 write32le(Loc, Value);
136 break;
137 default:
138 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +0000139 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000140 }
141}
142
Rui Ueyamabf450d92018-02-20 04:26:26 +0000143// Copy relocation entries to a given output stream.
144// This function is used only when a user passes "-r". For a regular link,
145// we consume relocations instead of copying them to an output file.
146void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000147 if (Relocations.empty())
148 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000149
150 int32_t Off = OutputOffset - getInputSectionOffset();
Nicola Zaghene7245b42018-05-15 13:36:20 +0000151 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
152 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000153
Rui Ueyamabf450d92018-02-20 04:26:26 +0000154 for (const WasmRelocation &Rel : Relocations) {
155 writeUleb128(OS, Rel.Type, "reloc type");
156 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
157 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +0000158
Keno Fischercadcb9e2019-06-26 00:52:42 +0000159 if (relocTypeHasAddend(Rel.Type))
Sam Cleggd177ab22018-05-04 23:14:42 +0000160 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
Sam Cleggd96d9352018-01-10 19:22:42 +0000161 }
162}
Sam Clegg50686852018-01-12 18:35:13 +0000163
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000164void InputFunction::setFunctionIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000165 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
166 << " -> " << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000167 assert(!hasFunctionIndex());
168 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000169}
Sam Clegg67abf532018-01-24 21:45:25 +0000170
171void InputFunction::setTableIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000172 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
173 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000174 assert(!hasTableIndex());
175 TableIndex = Index;
176}
Sam Cleggfb983cd2018-05-18 23:28:05 +0000177
178// Write a relocation value without padding and return the number of bytes
179// witten.
180static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
181 uint32_t Value) {
182 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000183 case R_WASM_TYPE_INDEX_LEB:
184 case R_WASM_FUNCTION_INDEX_LEB:
185 case R_WASM_GLOBAL_INDEX_LEB:
186 case R_WASM_EVENT_INDEX_LEB:
187 case R_WASM_MEMORY_ADDR_LEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000188 return encodeULEB128(Value, Buf);
Sam Clegg79e33172019-02-04 17:49:33 +0000189 case R_WASM_TABLE_INDEX_SLEB:
190 case R_WASM_MEMORY_ADDR_SLEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000191 return encodeSLEB128(static_cast<int32_t>(Value), Buf);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000192 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000193 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000194 }
195}
196
197static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
198 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000199 case R_WASM_TYPE_INDEX_LEB:
200 case R_WASM_FUNCTION_INDEX_LEB:
201 case R_WASM_GLOBAL_INDEX_LEB:
202 case R_WASM_EVENT_INDEX_LEB:
203 case R_WASM_MEMORY_ADDR_LEB:
204 case R_WASM_TABLE_INDEX_SLEB:
205 case R_WASM_MEMORY_ADDR_SLEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000206 return 5;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000207 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000208 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000209 }
210}
211
212static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
213 uint8_t Buf[5];
214 return writeCompressedReloc(Buf, Rel, Value);
215}
216
217// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
218// so that a fast linker can blindly overwrite them without needing to worry
219// about the number of bytes needed to encode the values.
220// However, for optimal output the code section can be compressed to remove
221// the padding then outputting non-relocatable files.
222// In this case we need to perform a size calculation based on the value at each
223// relocation. At best we end up saving 4 bytes for each relocation entry.
224//
225// This function only computes the final output size. It must be called
226// before getSize() is used to calculate of layout of the code section.
227void InputFunction::calculateSize() {
Sam Clegg4aad12c2018-09-27 00:46:54 +0000228 if (!File || !Config->CompressRelocations)
Sam Cleggfb983cd2018-05-18 23:28:05 +0000229 return;
230
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000231 LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000232
233 const uint8_t *SecStart = File->CodeSection->Content.data();
234 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
235 uint32_t FunctionSizeLength;
236 decodeULEB128(FuncStart, &FunctionSizeLength);
237
238 uint32_t Start = getInputSectionOffset();
239 uint32_t End = Start + Function->Size;
240
241 uint32_t LastRelocEnd = Start + FunctionSizeLength;
Sam Clegg47078f52018-08-22 17:50:51 +0000242 for (const WasmRelocation &Rel : Relocations) {
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000243 LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000244 CompressedFuncSize += Rel.Offset - LastRelocEnd;
245 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
246 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
247 }
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000248 LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000249 CompressedFuncSize += End - LastRelocEnd;
250
251 // Now we know how long the resulting function is we can add the encoding
252 // of its length
253 uint8_t Buf[5];
254 CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
255
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000256 LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n");
257 LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000258}
259
260// Override the default writeTo method so that we can (optionally) write the
261// compressed version of the function.
262void InputFunction::writeTo(uint8_t *Buf) const {
Sam Clegg4aad12c2018-09-27 00:46:54 +0000263 if (!File || !Config->CompressRelocations)
Sam Cleggfb983cd2018-05-18 23:28:05 +0000264 return InputChunk::writeTo(Buf);
265
266 Buf += OutputOffset;
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000267 uint8_t *Orig = Buf;
268 (void)Orig;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000269
270 const uint8_t *SecStart = File->CodeSection->Content.data();
271 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
272 const uint8_t *End = FuncStart + Function->Size;
273 uint32_t Count;
Sam Clegg65a91282018-05-22 17:06:55 +0000274 decodeULEB128(FuncStart, &Count);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000275 FuncStart += Count;
276
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000277 LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000278 Buf += encodeULEB128(CompressedFuncSize, Buf);
279 const uint8_t *LastRelocEnd = FuncStart;
280 for (const WasmRelocation &Rel : Relocations) {
281 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000282 LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000283 memcpy(Buf, LastRelocEnd, ChunkSize);
284 Buf += ChunkSize;
285 Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
286 LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
287 }
288
289 unsigned ChunkSize = End - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000290 LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000291 memcpy(Buf, LastRelocEnd, ChunkSize);
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000292 LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000293}
Sam Clegg09137be2019-04-04 18:40:51 +0000294
295// Generate code to apply relocations to the data section at runtime.
296// This is only called when generating shared libaries (PIC) where address are
297// not known at static link time.
298void InputSegment::generateRelocationCode(raw_ostream &OS) const {
Sam Cleggb685ddf2019-04-25 17:11:54 +0000299 LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
300 << " count=" << Relocations.size() << "\n");
301
302 // TODO(sbc): Encode the relocations in the data section and write a loop
303 // here to apply them.
Sam Clegg09137be2019-04-04 18:40:51 +0000304 uint32_t SegmentVA = OutputSeg->StartVA + OutputSegmentOffset;
305 for (const WasmRelocation &Rel : Relocations) {
306 uint32_t Offset = Rel.Offset - getInputSectionOffset();
Sam Cleggb685ddf2019-04-25 17:11:54 +0000307 uint32_t OutputOffset = SegmentVA + Offset;
308
309 LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(Rel.Type)
310 << " addend=" << Rel.Addend << " index=" << Rel.Index
311 << " output offset=" << OutputOffset << "\n");
Sam Clegg09137be2019-04-04 18:40:51 +0000312
313 // Get __memory_base
314 writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
315 writeUleb128(OS, WasmSym::MemoryBase->getGlobalIndex(), "memory_base");
316
317 // Add the offset of the relocation
318 writeU8(OS, WASM_OPCODE_I32_CONST, "I32_CONST");
Sam Cleggb685ddf2019-04-25 17:11:54 +0000319 writeSleb128(OS, OutputOffset, "offset");
Sam Clegg09137be2019-04-04 18:40:51 +0000320 writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
321
Sam Cleggb685ddf2019-04-25 17:11:54 +0000322 Symbol *Sym = File->getSymbol(Rel);
Sam Clegg09137be2019-04-04 18:40:51 +0000323 // Now figure out what we want to store
Sam Cleggb685ddf2019-04-25 17:11:54 +0000324 if (Sym->hasGOTIndex()) {
Sam Clegg09137be2019-04-04 18:40:51 +0000325 writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
Sam Cleggb685ddf2019-04-25 17:11:54 +0000326 writeUleb128(OS, Sym->getGOTIndex(), "global index");
327 if (Rel.Addend) {
Sam Clegg09137be2019-04-04 18:40:51 +0000328 writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
Sam Cleggb685ddf2019-04-25 17:11:54 +0000329 writeSleb128(OS, Rel.Addend, "addend");
Sam Clegg09137be2019-04-04 18:40:51 +0000330 writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
331 }
Sam Cleggb685ddf2019-04-25 17:11:54 +0000332 } else {
333 const GlobalSymbol* BaseSymbol = WasmSym::MemoryBase;
334 if (Rel.Type == R_WASM_TABLE_INDEX_I32)
335 BaseSymbol = WasmSym::TableBase;
336 writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
337 writeUleb128(OS, BaseSymbol->getGlobalIndex(), "base");
338 writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
339 writeSleb128(OS, File->calcNewValue(Rel), "offset");
340 writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
Sam Clegg09137be2019-04-04 18:40:51 +0000341 }
342
343 // Store that value at the virtual address
344 writeU8(OS, WASM_OPCODE_I32_STORE, "I32_STORE");
345 writeUleb128(OS, 2, "align");
346 writeUleb128(OS, 0, "offset");
347 }
348}