blob: ed658366b5dc08ef29d342831420fa9b1e0da593 [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) {
Heejin Ahn4821ebf2018-08-29 21:03:16 +000028#define WASM_RELOC(NAME, REL) \
29 case REL: \
30 return #NAME;
Sam Cleggc1be8232018-03-11 01:35:02 +000031#include "llvm/BinaryFormat/WasmRelocs.def"
32#undef WASM_RELOC
33 }
34 llvm_unreachable("unknown reloc type");
35}
36
Rui Ueyama81bee042018-02-19 22:29:48 +000037std::string lld::toString(const InputChunk *C) {
38 return (toString(C->File) + ":(" + C->getName() + ")").str();
39}
40
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000041StringRef InputChunk::getComdatName() const {
42 uint32_t Index = getComdat();
43 if (Index == UINT32_MAX)
44 return StringRef();
45 return File->getWasmObj()->linkingData().Comdats[Index];
46}
47
Sam Cleggc1953142018-05-05 00:18:43 +000048void InputChunk::verifyRelocTargets() const {
49 for (const WasmRelocation &Rel : Relocations) {
50 uint32_t ExistingValue;
51 unsigned BytesRead = 0;
52 uint32_t Offset = Rel.Offset - getInputSectionOffset();
53 const uint8_t *Loc = data().data() + Offset;
54 switch (Rel.Type) {
55 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
56 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
57 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
58 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
59 ExistingValue = decodeULEB128(Loc, &BytesRead);
60 break;
61 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
62 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
63 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
64 break;
65 case R_WEBASSEMBLY_TABLE_INDEX_I32:
66 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
67 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
68 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
69 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)
79 warn("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
80 ": 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);
Nicola Zaghene7245b42018-05-15 13:36:20 +0000105 LLVM_DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
106 << " 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) {
111 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
112 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
113 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
114 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
115 encodeULEB128(Value, Loc, 5);
116 break;
117 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
118 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
119 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
120 break;
121 case R_WEBASSEMBLY_TABLE_INDEX_I32:
122 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggd177ab22018-05-04 23:14:42 +0000123 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
124 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000125 write32le(Loc, Value);
126 break;
127 default:
128 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +0000129 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000130 }
131}
132
Rui Ueyamabf450d92018-02-20 04:26:26 +0000133// Copy relocation entries to a given output stream.
134// This function is used only when a user passes "-r". For a regular link,
135// we consume relocations instead of copying them to an output file.
136void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000137 if (Relocations.empty())
138 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000139
140 int32_t Off = OutputOffset - getInputSectionOffset();
Nicola Zaghene7245b42018-05-15 13:36:20 +0000141 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
142 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000143
Rui Ueyamabf450d92018-02-20 04:26:26 +0000144 for (const WasmRelocation &Rel : Relocations) {
145 writeUleb128(OS, Rel.Type, "reloc type");
146 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
147 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +0000148
Rui Ueyamabf450d92018-02-20 04:26:26 +0000149 switch (Rel.Type) {
150 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
151 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
152 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggd177ab22018-05-04 23:14:42 +0000153 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
154 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
155 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000156 break;
157 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000158 }
159}
Sam Clegg50686852018-01-12 18:35:13 +0000160
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000161void InputFunction::setFunctionIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000162 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
163 << " -> " << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000164 assert(!hasFunctionIndex());
165 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000166}
Sam Clegg67abf532018-01-24 21:45:25 +0000167
168void InputFunction::setTableIndex(uint32_t Index) {
Nicola Zaghene7245b42018-05-15 13:36:20 +0000169 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
170 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000171 assert(!hasTableIndex());
172 TableIndex = Index;
173}
Sam Cleggfb983cd2018-05-18 23:28:05 +0000174
175// Write a relocation value without padding and return the number of bytes
176// witten.
177static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
178 uint32_t Value) {
179 switch (Rel.Type) {
180 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
181 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
182 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
183 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
184 return encodeULEB128(Value, Buf);
185 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
186 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
187 return encodeSLEB128(static_cast<int32_t>(Value), Buf);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000188 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000189 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000190 }
191}
192
193static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
194 switch (Rel.Type) {
195 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
196 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
197 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
198 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
199 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
200 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
201 return 5;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000202 default:
Sam Cleggf3770302018-05-22 20:52:20 +0000203 llvm_unreachable("unexpected relocation type");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000204 }
205}
206
207static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) {
208 uint8_t Buf[5];
209 return writeCompressedReloc(Buf, Rel, Value);
210}
211
212// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
213// so that a fast linker can blindly overwrite them without needing to worry
214// about the number of bytes needed to encode the values.
215// However, for optimal output the code section can be compressed to remove
216// the padding then outputting non-relocatable files.
217// In this case we need to perform a size calculation based on the value at each
218// relocation. At best we end up saving 4 bytes for each relocation entry.
219//
220// This function only computes the final output size. It must be called
221// before getSize() is used to calculate of layout of the code section.
222void InputFunction::calculateSize() {
223 if (!File || !Config->CompressRelocTargets)
224 return;
225
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000226 LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000227
228 const uint8_t *SecStart = File->CodeSection->Content.data();
229 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
230 uint32_t FunctionSizeLength;
231 decodeULEB128(FuncStart, &FunctionSizeLength);
232
233 uint32_t Start = getInputSectionOffset();
234 uint32_t End = Start + Function->Size;
235
236 uint32_t LastRelocEnd = Start + FunctionSizeLength;
Sam Clegg47078f52018-08-22 17:50:51 +0000237 for (const WasmRelocation &Rel : Relocations) {
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000238 LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000239 CompressedFuncSize += Rel.Offset - LastRelocEnd;
240 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
241 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
242 }
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000243 LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000244 CompressedFuncSize += End - LastRelocEnd;
245
246 // Now we know how long the resulting function is we can add the encoding
247 // of its length
248 uint8_t Buf[5];
249 CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf);
250
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000251 LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n");
252 LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000253}
254
255// Override the default writeTo method so that we can (optionally) write the
256// compressed version of the function.
257void InputFunction::writeTo(uint8_t *Buf) const {
258 if (!File || !Config->CompressRelocTargets)
259 return InputChunk::writeTo(Buf);
260
261 Buf += OutputOffset;
Heejin Ahn4821ebf2018-08-29 21:03:16 +0000262 uint8_t *Orig = Buf;
263 (void)Orig;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000264
265 const uint8_t *SecStart = File->CodeSection->Content.data();
266 const uint8_t *FuncStart = SecStart + getInputSectionOffset();
267 const uint8_t *End = FuncStart + Function->Size;
268 uint32_t Count;
Sam Clegg65a91282018-05-22 17:06:55 +0000269 decodeULEB128(FuncStart, &Count);
Sam Cleggfb983cd2018-05-18 23:28:05 +0000270 FuncStart += Count;
271
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000272 LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000273 Buf += encodeULEB128(CompressedFuncSize, Buf);
274 const uint8_t *LastRelocEnd = FuncStart;
275 for (const WasmRelocation &Rel : Relocations) {
276 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000277 LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000278 memcpy(Buf, LastRelocEnd, ChunkSize);
279 Buf += ChunkSize;
280 Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel));
281 LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel);
282 }
283
284 unsigned ChunkSize = End - LastRelocEnd;
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000285 LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000286 memcpy(Buf, LastRelocEnd, ChunkSize);
Nicola Zaghen5c4fb452018-05-23 14:03:01 +0000287 LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n");
Sam Cleggfb983cd2018-05-18 23:28:05 +0000288}