1 //===- InputChunks.cpp ----------------------------------------------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "InputChunks.h"
10 #include "Config.h"
11 #include "OutputSegment.h"
12 #include "WriterUtils.h"
13 #include "lld/Common/ErrorHandler.h"
14 #include "lld/Common/LLVM.h"
15 #include "llvm/Support/LEB128.h"
16 
17 #define DEBUG_TYPE "lld"
18 
19 using namespace llvm;
20 using namespace llvm::wasm;
21 using namespace llvm::support::endian;
22 using namespace lld;
23 using namespace lld::wasm;
24 
25 StringRef lld::relocTypeToString(uint8_t RelocType) {
26   switch (RelocType) {
27 #define WASM_RELOC(NAME, REL)                                                  \
28   case REL:                                                                    \
29     return #NAME;
30 #include "llvm/BinaryFormat/WasmRelocs.def"
31 #undef WASM_RELOC
32   }
33   llvm_unreachable("unknown reloc type");
34 }
35 
36 std::string lld::toString(const InputChunk *C) {
37   return (toString(C->File) + ":(" + C->getName() + ")").str();
38 }
39 
40 StringRef 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 
47 void 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) {
54     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:
59       ExistingValue = decodeULEB128(Loc, &BytesRead);
60       break;
61     case R_WASM_TABLE_INDEX_SLEB:
62     case R_WASM_TABLE_INDEX_REL_SLEB:
63     case R_WASM_MEMORY_ADDR_SLEB:
64     case R_WASM_MEMORY_ADDR_REL_SLEB:
65       ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead));
66       break;
67     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:
71       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");
79 
80     if (Rel.Type != R_WASM_GLOBAL_INDEX_LEB) {
81       uint32_t ExpectedValue = File->calcExpectedValue(Rel);
82       if (ExpectedValue != ExistingValue)
83         warn("unexpected existing value for " + relocTypeToString(Rel.Type) +
84              ": existing=" + Twine(ExistingValue) +
85              " expected=" + Twine(ExpectedValue));
86     }
87   }
88 }
89 
90 // Copy this input chunk to an mmap'ed output file and apply relocations.
91 void InputChunk::writeTo(uint8_t *Buf) const {
92   // Copy contents
93   memcpy(Buf + OutputOffset, data().data(), data().size());
94 
95   // Apply relocations
96   if (Relocations.empty())
97     return;
98 
99 #ifndef NDEBUG
100   verifyRelocTargets();
101 #endif
102 
103   LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
104                     << " count=" << Relocations.size() << "\n");
105   int32_t Off = OutputOffset - getInputSectionOffset();
106 
107   for (const WasmRelocation &Rel : Relocations) {
108     uint8_t *Loc = Buf + Rel.Offset + Off;
109     uint32_t Value = File->calcNewValue(Rel);
110     LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type)
111                       << " addend=" << Rel.Addend << " index=" << Rel.Index
112                       << " value=" << Value << " offset=" << Rel.Offset
113                       << "\n");
114 
115     switch (Rel.Type) {
116     case R_WASM_TYPE_INDEX_LEB:
117     case R_WASM_FUNCTION_INDEX_LEB:
118     case R_WASM_GLOBAL_INDEX_LEB:
119     case R_WASM_EVENT_INDEX_LEB:
120     case R_WASM_MEMORY_ADDR_LEB:
121       encodeULEB128(Value, Loc, 5);
122       break;
123     case R_WASM_TABLE_INDEX_SLEB:
124     case R_WASM_TABLE_INDEX_REL_SLEB:
125     case R_WASM_MEMORY_ADDR_SLEB:
126     case R_WASM_MEMORY_ADDR_REL_SLEB:
127       encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
128       break;
129     case R_WASM_TABLE_INDEX_I32:
130     case R_WASM_MEMORY_ADDR_I32:
131     case R_WASM_FUNCTION_OFFSET_I32:
132     case R_WASM_SECTION_OFFSET_I32:
133       write32le(Loc, Value);
134       break;
135     default:
136       llvm_unreachable("unknown relocation type");
137     }
138   }
139 }
140 
141 // 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.
144 void InputChunk::writeRelocations(raw_ostream &OS) const {
145   if (Relocations.empty())
146     return;
147 
148   int32_t Off = OutputOffset - getInputSectionOffset();
149   LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName()
150                     << " offset=" << Twine(Off) << "\n");
151 
152   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");
156 
157     if (relocTypeHasAddend(Rel.Type))
158       writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
159   }
160 }
161 
162 void InputFunction::setFunctionIndex(uint32_t Index) {
163   LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
164                     << " -> " << Index << "\n");
165   assert(!hasFunctionIndex());
166   FunctionIndex = Index;
167 }
168 
169 void InputFunction::setTableIndex(uint32_t Index) {
170   LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
171                     << Index << "\n");
172   assert(!hasTableIndex());
173   TableIndex = Index;
174 }
175 
176 // Write a relocation value without padding and return the number of bytes
177 // witten.
178 static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel,
179                                      uint32_t Value) {
180   switch (Rel.Type) {
181   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:
186     return encodeULEB128(Value, Buf);
187   case R_WASM_TABLE_INDEX_SLEB:
188   case R_WASM_MEMORY_ADDR_SLEB:
189     return encodeSLEB128(static_cast<int32_t>(Value), Buf);
190   default:
191     llvm_unreachable("unexpected relocation type");
192   }
193 }
194 
195 static unsigned getRelocWidthPadded(const WasmRelocation &Rel) {
196   switch (Rel.Type) {
197   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:
204     return 5;
205   default:
206     llvm_unreachable("unexpected relocation type");
207   }
208 }
209 
210 static 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.
225 void InputFunction::calculateSize() {
226   if (!File || !Config->CompressRelocations)
227     return;
228 
229   LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
230 
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;
240   for (const WasmRelocation &Rel : Relocations) {
241     LLVM_DEBUG(dbgs() << "  region: " << (Rel.Offset - LastRelocEnd) << "\n");
242     CompressedFuncSize += Rel.Offset - LastRelocEnd;
243     CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel));
244     LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel);
245   }
246   LLVM_DEBUG(dbgs() << "  final region: " << (End - LastRelocEnd) << "\n");
247   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 
254   LLVM_DEBUG(dbgs() << "  calculateSize orig: " << Function->Size << "\n");
255   LLVM_DEBUG(dbgs() << "  calculateSize  new: " << CompressedSize << "\n");
256 }
257 
258 // Override the default writeTo method so that we can (optionally) write the
259 // compressed version of the function.
260 void InputFunction::writeTo(uint8_t *Buf) const {
261   if (!File || !Config->CompressRelocations)
262     return InputChunk::writeTo(Buf);
263 
264   Buf += OutputOffset;
265   uint8_t *Orig = Buf;
266   (void)Orig;
267 
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;
272   decodeULEB128(FuncStart, &Count);
273   FuncStart += Count;
274 
275   LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
276   Buf += encodeULEB128(CompressedFuncSize, Buf);
277   const uint8_t *LastRelocEnd = FuncStart;
278   for (const WasmRelocation &Rel : Relocations) {
279     unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd;
280     LLVM_DEBUG(dbgs() << "  write chunk: " << ChunkSize << "\n");
281     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;
288   LLVM_DEBUG(dbgs() << "  write final chunk: " << ChunkSize << "\n");
289   memcpy(Buf, LastRelocEnd, ChunkSize);
290   LLVM_DEBUG(dbgs() << "  total: " << (Buf + ChunkSize - Orig) << "\n");
291 }
292 
293 // Generate code to apply relocations to the data section at runtime.
294 // This is only called when generating shared libaries (PIC) where address are
295 // not known at static link time.
296 void InputSegment::generateRelocationCode(raw_ostream &OS) const {
297   LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
298                     << " count=" << Relocations.size() << "\n");
299 
300   // TODO(sbc): Encode the relocations in the data section and write a loop
301   // here to apply them.
302   uint32_t SegmentVA = OutputSeg->StartVA + OutputSegmentOffset;
303   for (const WasmRelocation &Rel : Relocations) {
304     uint32_t Offset = Rel.Offset - getInputSectionOffset();
305     uint32_t OutputOffset = SegmentVA + Offset;
306 
307     LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(Rel.Type)
308                       << " addend=" << Rel.Addend << " index=" << Rel.Index
309                       << " output offset=" << OutputOffset << "\n");
310 
311     // Get __memory_base
312     writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
313     writeUleb128(OS, WasmSym::MemoryBase->getGlobalIndex(), "memory_base");
314 
315     // Add the offset of the relocation
316     writeU8(OS, WASM_OPCODE_I32_CONST, "I32_CONST");
317     writeSleb128(OS, OutputOffset, "offset");
318     writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
319 
320     Symbol *Sym = File->getSymbol(Rel);
321     // Now figure out what we want to store
322     if (Sym->hasGOTIndex()) {
323       writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
324       writeUleb128(OS, Sym->getGOTIndex(), "global index");
325       if (Rel.Addend) {
326         writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
327         writeSleb128(OS, Rel.Addend, "addend");
328         writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
329       }
330     } else {
331       const GlobalSymbol* BaseSymbol = WasmSym::MemoryBase;
332       if (Rel.Type == R_WASM_TABLE_INDEX_I32)
333         BaseSymbol = WasmSym::TableBase;
334       writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
335       writeUleb128(OS, BaseSymbol->getGlobalIndex(), "base");
336       writeU8(OS, WASM_OPCODE_I32_CONST, "CONST");
337       writeSleb128(OS, File->calcNewValue(Rel), "offset");
338       writeU8(OS, WASM_OPCODE_I32_ADD, "ADD");
339     }
340 
341     // Store that value at the virtual address
342     writeU8(OS, WASM_OPCODE_I32_STORE, "I32_STORE");
343     writeUleb128(OS, 2, "align");
344     writeUleb128(OS, 0, "offset");
345   }
346 }
347