blob: 238c111ea390a45db1458588c916c635d82bddc8 [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
Heejin Ahna1cc4ea2019-02-04 19:13:46 +000025static StringRef reloctTypeToString(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:
62 case R_WASM_MEMORY_ADDR_SLEB:
Sam Cleggc1953142018-05-05 00:18:43 +000063 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
64 break;
Sam Clegg79e33172019-02-04 17:49:33 +000065 case R_WASM_TABLE_INDEX_I32:
66 case R_WASM_MEMORY_ADDR_I32:
67 case R_WASM_FUNCTION_OFFSET_I32:
68 case R_WASM_SECTION_OFFSET_I32:
Sam Cleggc1953142018-05-05 00:18:43 +000069 ExistingValue = static_cast<uint32_t>(read32le(Loc));
70 break;
71 default:
72 llvm_unreachable("unknown relocation type");
73 }
74
75 if (BytesRead && BytesRead != 5)
76 warn("expected LEB at relocation site be 5-byte padded");
77 uint32_t ExpectedValue = File->calcExpectedValue(Rel);
78 if (ExpectedValue != ExistingValue)
Heejin Ahna1cc4ea2019-02-04 19:13:46 +000079 warn("unexpected existing value for " + reloctTypeToString(Rel.Type) +
Sam Cleggc1953142018-05-05 00:18:43 +000080 ": existing=" + Twine(ExistingValue) +
81 " expected=" + Twine(ExpectedValue));
82 }
83}
84
Rui Ueyamabf450d92018-02-20 04:26:26 +000085// Copy this input chunk to an mmap'ed output file and apply relocations.
86void InputChunk::writeTo(uint8_t *Buf) const {
87 // Copy contents
88 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000089
Rui Ueyamabf450d92018-02-20 04:26:26 +000090 // Apply relocations
91 if (Relocations.empty())
92 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +000093
Sam Cleggc1953142018-05-05 00:18:43 +000094#ifndef NDEBUG
95 verifyRelocTargets();
96#endif
97
Nicola Zaghene7245b42018-05-15 13:36:20 +000098 LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
99 << " count=" << Relocations.size() << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000100 int32_t Off = OutputOffset - getInputSectionOffset();
101
102 for (const WasmRelocation &Rel : Relocations) {
103 uint8_t *Loc = Buf + Rel.Offset + Off;
Sam Cleggc1be8232018-03-11 01:35:02 +0000104 uint32_t Value = File->calcNewValue(Rel);
Heejin Ahna1cc4ea2019-02-04 19:13:46 +0000105 LLVM_DEBUG(dbgs() << "apply reloc: type=" << reloctTypeToString(Rel.Type)
Nicola Zaghene7245b42018-05-15 13:36:20 +0000106 << " addend=" << Rel.Addend << " index=" << Rel.Index
107 << " value=" << Value << " offset=" << Rel.Offset
108 << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000109
110 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000111 case R_WASM_TYPE_INDEX_LEB:
112 case R_WASM_FUNCTION_INDEX_LEB:
113 case R_WASM_GLOBAL_INDEX_LEB:
114 case R_WASM_EVENT_INDEX_LEB:
115 case R_WASM_MEMORY_ADDR_LEB:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000116 encodeULEB128(Value, Loc, 5);
117 break;
Sam Clegg79e33172019-02-04 17:49:33 +0000118 case R_WASM_TABLE_INDEX_SLEB:
119 case R_WASM_MEMORY_ADDR_SLEB:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000120 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
121 break;
Sam Clegg79e33172019-02-04 17:49:33 +0000122 case R_WASM_TABLE_INDEX_I32:
123 case R_WASM_MEMORY_ADDR_I32:
124 case R_WASM_FUNCTION_OFFSET_I32:
125 case R_WASM_SECTION_OFFSET_I32:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000126 write32le(Loc, Value);
127 break;
128 default:
129 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +0000130 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000131 }
132}
133
Rui Ueyamabf450d92018-02-20 04:26:26 +0000134// Copy relocation entries to a given output stream.
135// This function is used only when a user passes "-r". For a regular link,
136// we consume relocations instead of copying them to an output file.
137void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000138 if (Relocations.empty())
139 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000140
141 int32_t Off = OutputOffset - getInputSectionOffset();
Nicola Zaghene7245b42018-05-15 13:36:20 +0000142 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
143 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000144
Rui Ueyamabf450d92018-02-20 04:26:26 +0000145 for (const WasmRelocation &Rel : Relocations) {
146 writeUleb128(OS, Rel.Type, "reloc type");
147 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
148 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +0000149
Rui Ueyamabf450d92018-02-20 04:26:26 +0000150 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000151 case R_WASM_MEMORY_ADDR_LEB:
152 case R_WASM_MEMORY_ADDR_SLEB:
153 case R_WASM_MEMORY_ADDR_I32:
154 case R_WASM_FUNCTION_OFFSET_I32:
155 case R_WASM_SECTION_OFFSET_I32:
Sam Cleggd177ab22018-05-04 23:14:42 +0000156 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000157 break;
158 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000159 }
160}
Sam Clegg50686852018-01-12 18:35:13 +0000161
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000162void InputFunction::setFunctionIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000163 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
164 << " -> " << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000165 assert(!hasFunctionIndex());
166 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000167}
Sam Clegg67abf532018-01-24 21:45:25 +0000168
169void InputFunction::setTableIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000170 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
171 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000172 assert(!hasTableIndex());
173 TableIndex = Index;
174}
Sam Cleggfb983cd2018-05-18 23:28:05 +0000175
176// Write a relocation value without padding and return the number of bytes
177// witten.
178static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
179 uint32_t Value) {
180 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000181 case R_WASM_TYPE_INDEX_LEB:
182 case R_WASM_FUNCTION_INDEX_LEB:
183 case R_WASM_GLOBAL_INDEX_LEB:
184 case R_WASM_EVENT_INDEX_LEB:
185 case R_WASM_MEMORY_ADDR_LEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000186 return encodeULEB128(Value, Buf);
Sam Clegg79e33172019-02-04 17:49:33 +0000187 case R_WASM_TABLE_INDEX_SLEB:
188 case R_WASM_MEMORY_ADDR_SLEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000189 return encodeSLEB128(static_cast<int32_t>(Value), Buf);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000190 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000191 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000192 }
193}
194
195static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
196 switch (Rel.Type) {
Sam Clegg79e33172019-02-04 17:49:33 +0000197 case R_WASM_TYPE_INDEX_LEB:
198 case R_WASM_FUNCTION_INDEX_LEB:
199 case R_WASM_GLOBAL_INDEX_LEB:
200 case R_WASM_EVENT_INDEX_LEB:
201 case R_WASM_MEMORY_ADDR_LEB:
202 case R_WASM_TABLE_INDEX_SLEB:
203 case R_WASM_MEMORY_ADDR_SLEB:
Sam Cleggfb983cd2018-05-18 23:28:05 +0000204 return 5;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000205 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000206 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000207 }
208}
209
210static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
211 uint8_t Buf[5];
212 return writeCompressedReloc(Buf, Rel, Value);
213}
214
215// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
216// so that a fast linker can blindly overwrite them without needing to worry
217// about the number of bytes needed to encode the values.
218// However, for optimal output the code section can be compressed to remove
219// the padding then outputting non-relocatable files.
220// In this case we need to perform a size calculation based on the value at each
221// relocation. At best we end up saving 4 bytes for each relocation entry.
222//
223// This function only computes the final output size. It must be called
224// before getSize() is used to calculate of layout of the code section.
225void InputFunction::calculateSize() {
Sam Clegg4aad12c2018-09-27 00:46:54 +0000226 if (!File || !Config->CompressRelocations)
Sam Cleggfb983cd2018-05-18 23:28:05 +0000227 return;
228
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000229 LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000230
231 const uint8_t *SecStart = File->CodeSection->Content.data();
232 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
233 uint32_t FunctionSizeLength;
234 decodeULEB128(FuncStart, &FunctionSizeLength);
235
236 uint32_t Start = getInputSectionOffset();
237 uint32_t End = Start + Function->Size;
238
239 uint32_t LastRelocEnd = Start + FunctionSizeLength;
Sam Clegg47078f52018-08-22 17:50:51 +0000240 for (const WasmRelocation &Rel : Relocations) {
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000241 LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000242 CompressedFuncSize += Rel.Offset - LastRelocEnd;
243 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
244 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
245 }
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000246 LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000247 CompressedFuncSize += End - LastRelocEnd;
248
249 // Now we know how long the resulting function is we can add the encoding
250 // of its length
251 uint8_t Buf[5];
252 CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
253
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000254 LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n");
255 LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000256}
257
258// Override the default writeTo method so that we can (optionally) write the
259// compressed version of the function.
260void InputFunction::writeTo(uint8_t *Buf) const {
Sam Clegg4aad12c2018-09-27 00:46:54 +0000261 if (!File || !Config->CompressRelocations)
Sam Cleggfb983cd2018-05-18 23:28:05 +0000262 return InputChunk::writeTo(Buf);
263
264 Buf += OutputOffset;
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000265 uint8_t *Orig = Buf;
266 (void)Orig;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000267
268 const uint8_t *SecStart = File->CodeSection->Content.data();
269 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
270 const uint8_t *End = FuncStart + Function->Size;
271 uint32_t Count;
Sam Clegg65a91282018-05-22 17:06:55 +0000272 decodeULEB128(FuncStart, &Count);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000273 FuncStart += Count;
274
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000275 LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000276 Buf += encodeULEB128(CompressedFuncSize, Buf);
277 const uint8_t *LastRelocEnd = FuncStart;
278 for (const WasmRelocation &Rel : Relocations) {
279 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000280 LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000281 memcpy(Buf, LastRelocEnd, ChunkSize);
282 Buf += ChunkSize;
283 Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
284 LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
285 }
286
287 unsigned ChunkSize = End - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000288 LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000289 memcpy(Buf, LastRelocEnd, ChunkSize);
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000290 LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000291}