Index: lld/wasm/InputChunks.cpp =================================================================== --- lld/wasm/InputChunks.cpp +++ lld/wasm/InputChunks.cpp @@ -44,51 +44,47 @@ Relocations.push_back(R); } -static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) { - DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type - << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value - << " offset=" << Reloc.Reloc.Offset << "\n"); - - Buf += Reloc.Reloc.Offset; - - switch (Reloc.Reloc.Type) { - case R_WEBASSEMBLY_TYPE_INDEX_LEB: - case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: - case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: - // Additional check to verify that the existing value that the location - // matches our expectations. - if (decodeULEB128(Buf) != Reloc.Reloc.Index) { - DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n"); - assert(decodeULEB128(Buf) == Reloc.Reloc.Index); - } - LLVM_FALLTHROUGH; - case R_WEBASSEMBLY_MEMORY_ADDR_LEB: - encodeULEB128(Reloc.Value, Buf, 5); - break; - case R_WEBASSEMBLY_TABLE_INDEX_SLEB: - case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: - encodeSLEB128(static_cast(Reloc.Value), Buf, 5); - break; - case R_WEBASSEMBLY_TABLE_INDEX_I32: - case R_WEBASSEMBLY_MEMORY_ADDR_I32: - write32le(Buf, Reloc.Value); - break; - default: - llvm_unreachable("unknown relocation type"); - } -} - // Copy this input chunk to an mmap'ed output file. void InputChunk::writeTo(uint8_t *Buf) const { // Copy contents memcpy(Buf + getOutputOffset(), data().data(), data().size()); // Apply relocations - if (OutRelocations.empty()) - return; DEBUG(dbgs() << "applyRelocations: count=" << OutRelocations.size() << "\n"); - for (const OutputRelocation &Reloc : OutRelocations) - applyRelocation(Buf, Reloc); + + for (const OutputRelocation &Reloc : OutRelocations) { + DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type + << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value + << " offset=" << Reloc.Reloc.Offset << "\n"); + + uint8_t *Loc = Buf + Reloc.Reloc.Offset; + + switch (Reloc.Reloc.Type) { + case R_WEBASSEMBLY_TYPE_INDEX_LEB: + case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: + case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: + // Additional check to verify that the existing value that the location + // matches our expectations. + if (decodeULEB128(Loc) != Reloc.Reloc.Index) { + DEBUG(dbgs() << "existing value: " << decodeULEB128(Loc) << "\n"); + assert(decodeULEB128(Loc) == Reloc.Reloc.Index); + } + LLVM_FALLTHROUGH; + case R_WEBASSEMBLY_MEMORY_ADDR_LEB: + encodeULEB128(Reloc.Value, Loc, 5); + break; + case R_WEBASSEMBLY_TABLE_INDEX_SLEB: + case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: + encodeSLEB128(static_cast(Reloc.Value), Loc, 5); + break; + case R_WEBASSEMBLY_TABLE_INDEX_I32: + case R_WEBASSEMBLY_MEMORY_ADDR_I32: + write32le(Loc, Reloc.Value); + break; + default: + llvm_unreachable("unknown relocation type"); + } + } } // Populate OutRelocations based on the input relocations and offset within the