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 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 114 << " value=" << Value << " offset=" << Rel.Offset 115 << "\n"); 116 117 switch (Rel.Type) { 118 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: 123 encodeULEB128(Value, Loc, 5); 124 break; 125 case R_WASM_TABLE_INDEX_SLEB: 126 case R_WASM_TABLE_INDEX_REL_SLEB: 127 case R_WASM_MEMORY_ADDR_SLEB: 128 case R_WASM_MEMORY_ADDR_REL_SLEB: 129 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); 130 break; 131 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: 135 write32le(Loc, Value); 136 break; 137 default: 138 llvm_unreachable("unknown relocation type"); 139 } 140 } 141 } 142 143 // 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. 146 void InputChunk::writeRelocations(raw_ostream &OS) const { 147 if (Relocations.empty()) 148 return; 149 150 int32_t Off = OutputOffset - getInputSectionOffset(); 151 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName() 152 << " offset=" << Twine(Off) << "\n"); 153 154 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"); 158 159 if (relocTypeHasAddend(Rel.Type)) 160 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); 161 } 162 } 163 164 void InputFunction::setFunctionIndex(uint32_t Index) { 165 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() 166 << " -> " << Index << "\n"); 167 assert(!hasFunctionIndex()); 168 FunctionIndex = Index; 169 } 170 171 void InputFunction::setTableIndex(uint32_t Index) { 172 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " 173 << Index << "\n"); 174 assert(!hasTableIndex()); 175 TableIndex = Index; 176 } 177 178 // Write a relocation value without padding and return the number of bytes 179 // witten. 180 static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel, 181 uint32_t Value) { 182 switch (Rel.Type) { 183 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: 188 return encodeULEB128(Value, Buf); 189 case R_WASM_TABLE_INDEX_SLEB: 190 case R_WASM_MEMORY_ADDR_SLEB: 191 return encodeSLEB128(static_cast<int32_t>(Value), Buf); 192 default: 193 llvm_unreachable("unexpected relocation type"); 194 } 195 } 196 197 static unsigned getRelocWidthPadded(const WasmRelocation &Rel) { 198 switch (Rel.Type) { 199 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: 206 return 5; 207 default: 208 llvm_unreachable("unexpected relocation type"); 209 } 210 } 211 212 static 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. 227 void InputFunction::calculateSize() { 228 if (!File || !Config->CompressRelocations) 229 return; 230 231 LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); 232 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; 242 for (const WasmRelocation &Rel : Relocations) { 243 LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n"); 244 CompressedFuncSize += Rel.Offset - LastRelocEnd; 245 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel)); 246 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel); 247 } 248 LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n"); 249 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 256 LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n"); 257 LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n"); 258 } 259 260 // Override the default writeTo method so that we can (optionally) write the 261 // compressed version of the function. 262 void InputFunction::writeTo(uint8_t *Buf) const { 263 if (!File || !Config->CompressRelocations) 264 return InputChunk::writeTo(Buf); 265 266 Buf += OutputOffset; 267 uint8_t *Orig = Buf; 268 (void)Orig; 269 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; 274 decodeULEB128(FuncStart, &Count); 275 FuncStart += Count; 276 277 LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); 278 Buf += encodeULEB128(CompressedFuncSize, Buf); 279 const uint8_t *LastRelocEnd = FuncStart; 280 for (const WasmRelocation &Rel : Relocations) { 281 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd; 282 LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n"); 283 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; 290 LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n"); 291 memcpy(Buf, LastRelocEnd, ChunkSize); 292 LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n"); 293 } 294 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. 298 void InputSegment::generateRelocationCode(raw_ostream &OS) const { 299 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. 304 uint32_t SegmentVA = OutputSeg->StartVA + OutputSegmentOffset; 305 for (const WasmRelocation &Rel : Relocations) { 306 uint32_t Offset = Rel.Offset - getInputSectionOffset(); 307 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"); 312 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"); 319 writeSleb128(OS, OutputOffset, "offset"); 320 writeU8(OS, WASM_OPCODE_I32_ADD, "ADD"); 321 322 Symbol *Sym = File->getSymbol(Rel); 323 // Now figure out what we want to store 324 if (Sym->hasGOTIndex()) { 325 writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); 326 writeUleb128(OS, Sym->getGOTIndex(), "global index"); 327 if (Rel.Addend) { 328 writeU8(OS, WASM_OPCODE_I32_CONST, "CONST"); 329 writeSleb128(OS, Rel.Addend, "addend"); 330 writeU8(OS, WASM_OPCODE_I32_ADD, "ADD"); 331 } 332 } 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"); 341 } 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 } 349