2018-01-13 06:18:22 +08:00
|
|
|
//===- InputChunks.cpp ----------------------------------------------------===//
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputChunks.h"
|
2018-01-11 03:22:42 +08:00
|
|
|
#include "Config.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "OutputSegment.h"
|
2018-02-20 12:26:26 +08:00
|
|
|
#include "WriterUtils.h"
|
2018-01-11 03:22:42 +08:00
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2018-01-11 03:22:42 +08:00
|
|
|
#include "llvm/Support/LEB128.h"
|
2017-11-18 02:14:09 +08:00
|
|
|
|
|
|
|
#define DEBUG_TYPE "lld"
|
|
|
|
|
|
|
|
using namespace llvm;
|
2018-01-11 03:22:42 +08:00
|
|
|
using namespace llvm::wasm;
|
2018-02-17 04:38:15 +08:00
|
|
|
using namespace llvm::support::endian;
|
2017-11-18 02:14:09 +08:00
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace lld {
|
|
|
|
StringRef relocTypeToString(uint8_t relocType) {
|
2018-03-11 09:35:02 +08:00
|
|
|
switch (relocType) {
|
2018-08-30 05:03:16 +08:00
|
|
|
#define WASM_RELOC(NAME, REL) \
|
|
|
|
case REL: \
|
|
|
|
return #NAME;
|
2018-03-11 09:35:02 +08:00
|
|
|
#include "llvm/BinaryFormat/WasmRelocs.def"
|
|
|
|
#undef WASM_RELOC
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown reloc type");
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
std::string toString(const wasm::InputChunk *c) {
|
2018-02-20 06:29:48 +08:00
|
|
|
return (toString(c->file) + ":(" + c->getName() + ")").str();
|
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
namespace wasm {
|
2018-03-14 23:45:11 +08:00
|
|
|
StringRef InputChunk::getComdatName() const {
|
|
|
|
uint32_t index = getComdat();
|
|
|
|
if (index == UINT32_MAX)
|
|
|
|
return StringRef();
|
|
|
|
return file->getWasmObj()->linkingData().Comdats[index];
|
|
|
|
}
|
|
|
|
|
2018-05-05 08:18:43 +08:00
|
|
|
void InputChunk::verifyRelocTargets() const {
|
|
|
|
for (const WasmRelocation &rel : relocations) {
|
|
|
|
uint32_t existingValue;
|
|
|
|
unsigned bytesRead = 0;
|
|
|
|
uint32_t offset = rel.Offset - getInputSectionOffset();
|
|
|
|
const uint8_t *loc = data().data() + offset;
|
|
|
|
switch (rel.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2018-05-05 08:18:43 +08:00
|
|
|
existingValue = decodeULEB128(loc, &bytesRead);
|
|
|
|
break;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2018-05-05 08:18:43 +08:00
|
|
|
existingValue = static_cast<uint32_t>(decodeSLEB128(loc, &bytesRead));
|
|
|
|
break;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 08:18:43 +08:00
|
|
|
existingValue = static_cast<uint32_t>(read32le(loc));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (bytesRead && bytesRead != 5)
|
|
|
|
warn("expected LEB at relocation site be 5-byte padded");
|
2019-03-27 03:46:15 +08:00
|
|
|
|
|
|
|
if (rel.Type != R_WASM_GLOBAL_INDEX_LEB) {
|
|
|
|
uint32_t expectedValue = file->calcExpectedValue(rel);
|
|
|
|
if (expectedValue != existingValue)
|
2019-03-30 06:56:39 +08:00
|
|
|
warn("unexpected existing value for " + relocTypeToString(rel.Type) +
|
2019-03-27 03:46:15 +08:00
|
|
|
": existing=" + Twine(existingValue) +
|
|
|
|
" expected=" + Twine(expectedValue));
|
|
|
|
}
|
2018-05-05 08:18:43 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 12:26:26 +08:00
|
|
|
// Copy this input chunk to an mmap'ed output file and apply relocations.
|
2018-02-20 06:44:18 +08:00
|
|
|
void InputChunk::writeTo(uint8_t *buf) const {
|
|
|
|
// Copy contents
|
2018-02-20 12:26:26 +08:00
|
|
|
memcpy(buf + outputOffset, data().data(), data().size());
|
2018-02-20 06:44:18 +08:00
|
|
|
|
|
|
|
// Apply relocations
|
2018-02-20 12:26:26 +08:00
|
|
|
if (relocations.empty())
|
2018-01-11 03:22:42 +08:00
|
|
|
return;
|
2018-02-20 12:26:26 +08:00
|
|
|
|
2018-05-05 08:18:43 +08:00
|
|
|
#ifndef NDEBUG
|
|
|
|
verifyRelocTargets();
|
|
|
|
#endif
|
|
|
|
|
2019-08-21 02:39:24 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "applying relocations: " << toString(this)
|
2018-05-15 21:36:20 +08:00
|
|
|
<< " count=" << relocations.size() << "\n");
|
2018-02-20 12:26:26 +08:00
|
|
|
int32_t off = outputOffset - getInputSectionOffset();
|
2019-07-11 13:40:30 +08:00
|
|
|
|
2018-02-20 12:26:26 +08:00
|
|
|
for (const WasmRelocation &rel : relocations) {
|
|
|
|
uint8_t *loc = buf + rel.Offset + off;
|
2018-03-11 09:35:02 +08:00
|
|
|
uint32_t value = file->calcNewValue(rel);
|
2019-07-08 18:35:08 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(rel.Type));
|
|
|
|
if (rel.Type != R_WASM_TYPE_INDEX_LEB)
|
|
|
|
LLVM_DEBUG(dbgs() << " sym=" << file->getSymbols()[rel.Index]->getName());
|
|
|
|
LLVM_DEBUG(dbgs() << " addend=" << rel.Addend << " index=" << rel.Index
|
2018-05-15 21:36:20 +08:00
|
|
|
<< " value=" << value << " offset=" << rel.Offset
|
|
|
|
<< "\n");
|
2018-02-20 12:26:26 +08:00
|
|
|
|
|
|
|
switch (rel.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2018-02-20 12:26:26 +08:00
|
|
|
encodeULEB128(value, loc, 5);
|
|
|
|
break;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB:
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2019-04-05 01:43:50 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_REL_SLEB:
|
2018-02-20 12:26:26 +08:00
|
|
|
encodeSLEB128(static_cast<int32_t>(value), loc, 5);
|
|
|
|
break;
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-02-20 12:26:26 +08:00
|
|
|
write32le(loc, value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
2018-01-11 03:22:42 +08:00
|
|
|
}
|
|
|
|
|
2018-02-20 12:26:26 +08:00
|
|
|
// Copy relocation entries to a given output stream.
|
|
|
|
// This function is used only when a user passes "-r". For a regular link,
|
|
|
|
// we consume relocations instead of copying them to an output file.
|
|
|
|
void InputChunk::writeRelocations(raw_ostream &os) const {
|
2018-01-13 02:35:13 +08:00
|
|
|
if (relocations.empty())
|
|
|
|
return;
|
2018-02-20 12:26:26 +08:00
|
|
|
|
|
|
|
int32_t off = outputOffset - getInputSectionOffset();
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "writeRelocations: " << file->getName()
|
|
|
|
<< " offset=" << Twine(off) << "\n");
|
2018-02-20 12:26:26 +08:00
|
|
|
|
|
|
|
for (const WasmRelocation &rel : relocations) {
|
|
|
|
writeUleb128(os, rel.Type, "reloc type");
|
|
|
|
writeUleb128(os, rel.Offset + off, "reloc offset");
|
|
|
|
writeUleb128(os, file->calcNewIndex(rel), "reloc index");
|
|
|
|
|
2019-06-26 08:52:42 +08:00
|
|
|
if (relocTypeHasAddend(rel.Type))
|
2018-05-05 07:14:42 +08:00
|
|
|
writeSleb128(os, file->calcNewAddend(rel), "reloc addend");
|
2018-01-11 03:22:42 +08:00
|
|
|
}
|
|
|
|
}
|
2018-01-13 02:35:13 +08:00
|
|
|
|
2018-03-13 03:56:23 +08:00
|
|
|
void InputFunction::setFunctionIndex(uint32_t index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName()
|
|
|
|
<< " -> " << index << "\n");
|
2018-03-13 03:56:23 +08:00
|
|
|
assert(!hasFunctionIndex());
|
|
|
|
functionIndex = index;
|
2018-01-13 08:44:45 +08:00
|
|
|
}
|
2018-01-25 05:45:25 +08:00
|
|
|
|
|
|
|
void InputFunction::setTableIndex(uint32_t index) {
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
|
|
|
|
<< index << "\n");
|
2018-01-25 05:45:25 +08:00
|
|
|
assert(!hasTableIndex());
|
|
|
|
tableIndex = index;
|
|
|
|
}
|
2018-05-19 07:28:05 +08:00
|
|
|
|
|
|
|
// Write a relocation value without padding and return the number of bytes
|
|
|
|
// witten.
|
|
|
|
static unsigned writeCompressedReloc(uint8_t *buf, const WasmRelocation &rel,
|
|
|
|
uint32_t value) {
|
|
|
|
switch (rel.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2018-05-19 07:28:05 +08:00
|
|
|
return encodeULEB128(value, buf);
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2018-05-19 07:28:05 +08:00
|
|
|
return encodeSLEB128(static_cast<int32_t>(value), buf);
|
|
|
|
default:
|
2018-05-23 04:52:20 +08:00
|
|
|
llvm_unreachable("unexpected relocation type");
|
2018-05-19 07:28:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getRelocWidthPadded(const WasmRelocation &rel) {
|
|
|
|
switch (rel.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_TYPE_INDEX_LEB:
|
|
|
|
case R_WASM_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WASM_EVENT_INDEX_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
2018-05-19 07:28:05 +08:00
|
|
|
return 5;
|
|
|
|
default:
|
2018-05-23 04:52:20 +08:00
|
|
|
llvm_unreachable("unexpected relocation type");
|
2018-05-19 07:28:05 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned getRelocWidth(const WasmRelocation &rel, uint32_t value) {
|
|
|
|
uint8_t buf[5];
|
|
|
|
return writeCompressedReloc(buf, rel, value);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relocations of type LEB and SLEB in the code section are padded to 5 bytes
|
|
|
|
// so that a fast linker can blindly overwrite them without needing to worry
|
|
|
|
// about the number of bytes needed to encode the values.
|
|
|
|
// However, for optimal output the code section can be compressed to remove
|
|
|
|
// the padding then outputting non-relocatable files.
|
|
|
|
// In this case we need to perform a size calculation based on the value at each
|
|
|
|
// relocation. At best we end up saving 4 bytes for each relocation entry.
|
|
|
|
//
|
|
|
|
// This function only computes the final output size. It must be called
|
|
|
|
// before getSize() is used to calculate of layout of the code section.
|
|
|
|
void InputFunction::calculateSize() {
|
2018-09-27 08:46:54 +08:00
|
|
|
if (!file || !config->compressRelocations)
|
2018-05-19 07:28:05 +08:00
|
|
|
return;
|
|
|
|
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
|
|
|
|
const uint8_t *secStart = file->codeSection->Content.data();
|
|
|
|
const uint8_t *funcStart = secStart + getInputSectionOffset();
|
|
|
|
uint32_t functionSizeLength;
|
|
|
|
decodeULEB128(funcStart, &functionSizeLength);
|
|
|
|
|
|
|
|
uint32_t start = getInputSectionOffset();
|
|
|
|
uint32_t end = start + function->Size;
|
|
|
|
|
|
|
|
uint32_t lastRelocEnd = start + functionSizeLength;
|
2018-08-23 01:50:51 +08:00
|
|
|
for (const WasmRelocation &rel : relocations) {
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " region: " << (rel.Offset - lastRelocEnd) << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
compressedFuncSize += rel.Offset - lastRelocEnd;
|
|
|
|
compressedFuncSize += getRelocWidth(rel, file->calcNewValue(rel));
|
|
|
|
lastRelocEnd = rel.Offset + getRelocWidthPadded(rel);
|
|
|
|
}
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " final region: " << (end - lastRelocEnd) << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
compressedFuncSize += end - lastRelocEnd;
|
|
|
|
|
|
|
|
// Now we know how long the resulting function is we can add the encoding
|
|
|
|
// of its length
|
|
|
|
uint8_t buf[5];
|
|
|
|
compressedSize = compressedFuncSize + encodeULEB128(compressedFuncSize, buf);
|
|
|
|
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " calculateSize orig: " << function->Size << "\n");
|
|
|
|
LLVM_DEBUG(dbgs() << " calculateSize new: " << compressedSize << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Override the default writeTo method so that we can (optionally) write the
|
|
|
|
// compressed version of the function.
|
|
|
|
void InputFunction::writeTo(uint8_t *buf) const {
|
2018-09-27 08:46:54 +08:00
|
|
|
if (!file || !config->compressRelocations)
|
2018-05-19 07:28:05 +08:00
|
|
|
return InputChunk::writeTo(buf);
|
|
|
|
|
|
|
|
buf += outputOffset;
|
2018-08-30 05:03:16 +08:00
|
|
|
uint8_t *orig = buf;
|
|
|
|
(void)orig;
|
2018-05-19 07:28:05 +08:00
|
|
|
|
|
|
|
const uint8_t *secStart = file->codeSection->Content.data();
|
|
|
|
const uint8_t *funcStart = secStart + getInputSectionOffset();
|
|
|
|
const uint8_t *end = funcStart + function->Size;
|
|
|
|
uint32_t count;
|
2018-05-23 01:06:55 +08:00
|
|
|
decodeULEB128(funcStart, &count);
|
2018-05-19 07:28:05 +08:00
|
|
|
funcStart += count;
|
|
|
|
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
buf += encodeULEB128(compressedFuncSize, buf);
|
|
|
|
const uint8_t *lastRelocEnd = funcStart;
|
|
|
|
for (const WasmRelocation &rel : relocations) {
|
|
|
|
unsigned chunkSize = (secStart + rel.Offset) - lastRelocEnd;
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " write chunk: " << chunkSize << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
memcpy(buf, lastRelocEnd, chunkSize);
|
|
|
|
buf += chunkSize;
|
|
|
|
buf += writeCompressedReloc(buf, rel, file->calcNewValue(rel));
|
|
|
|
lastRelocEnd = secStart + rel.Offset + getRelocWidthPadded(rel);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned chunkSize = end - lastRelocEnd;
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " write final chunk: " << chunkSize << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
memcpy(buf, lastRelocEnd, chunkSize);
|
2018-05-23 22:03:01 +08:00
|
|
|
LLVM_DEBUG(dbgs() << " total: " << (buf + chunkSize - orig) << "\n");
|
2018-05-19 07:28:05 +08:00
|
|
|
}
|
2019-04-05 02:40:51 +08:00
|
|
|
|
|
|
|
// Generate code to apply relocations to the data section at runtime.
|
|
|
|
// This is only called when generating shared libaries (PIC) where address are
|
|
|
|
// not known at static link time.
|
|
|
|
void InputSegment::generateRelocationCode(raw_ostream &os) const {
|
2019-04-26 01:11:54 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName()
|
|
|
|
<< " count=" << relocations.size() << "\n");
|
|
|
|
|
|
|
|
// TODO(sbc): Encode the relocations in the data section and write a loop
|
|
|
|
// here to apply them.
|
2019-04-05 02:40:51 +08:00
|
|
|
uint32_t segmentVA = outputSeg->startVA + outputSegmentOffset;
|
|
|
|
for (const WasmRelocation &rel : relocations) {
|
|
|
|
uint32_t offset = rel.Offset - getInputSectionOffset();
|
2019-04-26 01:11:54 +08:00
|
|
|
uint32_t outputOffset = segmentVA + offset;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type)
|
|
|
|
<< " addend=" << rel.Addend << " index=" << rel.Index
|
|
|
|
<< " output offset=" << outputOffset << "\n");
|
2019-04-05 02:40:51 +08:00
|
|
|
|
|
|
|
// Get __memory_base
|
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
|
|
|
|
writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base");
|
|
|
|
|
|
|
|
// Add the offset of the relocation
|
|
|
|
writeU8(os, WASM_OPCODE_I32_CONST, "I32_CONST");
|
2019-04-26 01:11:54 +08:00
|
|
|
writeSleb128(os, outputOffset, "offset");
|
2019-04-05 02:40:51 +08:00
|
|
|
writeU8(os, WASM_OPCODE_I32_ADD, "ADD");
|
|
|
|
|
2019-04-26 01:11:54 +08:00
|
|
|
Symbol *sym = file->getSymbol(rel);
|
2019-04-05 02:40:51 +08:00
|
|
|
// Now figure out what we want to store
|
2019-04-26 01:11:54 +08:00
|
|
|
if (sym->hasGOTIndex()) {
|
2019-04-05 02:40:51 +08:00
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
|
2019-04-26 01:11:54 +08:00
|
|
|
writeUleb128(os, sym->getGOTIndex(), "global index");
|
|
|
|
if (rel.Addend) {
|
2019-04-05 02:40:51 +08:00
|
|
|
writeU8(os, WASM_OPCODE_I32_CONST, "CONST");
|
2019-04-26 01:11:54 +08:00
|
|
|
writeSleb128(os, rel.Addend, "addend");
|
2019-04-05 02:40:51 +08:00
|
|
|
writeU8(os, WASM_OPCODE_I32_ADD, "ADD");
|
|
|
|
}
|
2019-04-26 01:11:54 +08:00
|
|
|
} else {
|
|
|
|
const GlobalSymbol* baseSymbol = WasmSym::memoryBase;
|
|
|
|
if (rel.Type == R_WASM_TABLE_INDEX_I32)
|
|
|
|
baseSymbol = WasmSym::tableBase;
|
|
|
|
writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET");
|
|
|
|
writeUleb128(os, baseSymbol->getGlobalIndex(), "base");
|
|
|
|
writeU8(os, WASM_OPCODE_I32_CONST, "CONST");
|
|
|
|
writeSleb128(os, file->calcNewValue(rel), "offset");
|
|
|
|
writeU8(os, WASM_OPCODE_I32_ADD, "ADD");
|
2019-04-05 02:40:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Store that value at the virtual address
|
|
|
|
writeU8(os, WASM_OPCODE_I32_STORE, "I32_STORE");
|
|
|
|
writeUleb128(os, 2, "align");
|
|
|
|
writeUleb128(os, 0, "offset");
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|