2018-01-13 06:18:22 +08:00
|
|
|
//===- InputChunks.cpp ----------------------------------------------------===//
|
2017-11-18 02:14:09 +08:00
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
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-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;
|
|
|
|
using namespace lld;
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace lld::wasm;
|
|
|
|
|
|
|
|
uint32_t InputSegment::translateVA(uint32_t Address) const {
|
|
|
|
assert(Address >= startVA() && Address < endVA());
|
2018-01-11 03:22:42 +08:00
|
|
|
int32_t Delta = OutputSeg->StartVA + OutputSegmentOffset - startVA();
|
2017-11-18 02:14:09 +08:00
|
|
|
DEBUG(dbgs() << "translateVA: " << getName() << " Delta=" << Delta
|
|
|
|
<< " Address=" << Address << "\n");
|
|
|
|
return Address + Delta;
|
|
|
|
}
|
2018-01-10 09:13:34 +08:00
|
|
|
|
|
|
|
void InputChunk::copyRelocations(const WasmSection &Section) {
|
2018-01-13 02:35:13 +08:00
|
|
|
if (Section.Relocations.empty())
|
|
|
|
return;
|
2018-01-10 09:13:34 +08:00
|
|
|
size_t Start = getInputSectionOffset();
|
|
|
|
size_t Size = getSize();
|
|
|
|
for (const WasmRelocation &R : Section.Relocations)
|
|
|
|
if (R.Offset >= Start && R.Offset < Start + Size)
|
|
|
|
Relocations.push_back(R);
|
|
|
|
}
|
2018-01-11 03:22:42 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
int64_t ExistingValue;
|
|
|
|
switch (Reloc.Reloc.Type) {
|
|
|
|
case R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
|
|
|
case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
2018-01-23 09:25:56 +08:00
|
|
|
case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
2018-01-11 03:22:42 +08:00
|
|
|
ExistingValue = decodeULEB128(Buf);
|
2018-01-23 09:25:56 +08:00
|
|
|
// Additional check to verify that the existing value that the location
|
|
|
|
// matches our expectations.
|
2018-01-11 03:22:42 +08:00
|
|
|
if (ExistingValue != 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<int32_t>(Reloc.Value), Buf, 5);
|
|
|
|
break;
|
|
|
|
case R_WEBASSEMBLY_TABLE_INDEX_I32:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
|
|
|
support::endian::write32<support::little>(Buf, Reloc.Value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) {
|
|
|
|
if (!Relocs.size())
|
|
|
|
return;
|
2018-01-12 08:34:04 +08:00
|
|
|
DEBUG(dbgs() << "applyRelocations: count=" << Relocs.size() << "\n");
|
2018-01-11 03:22:42 +08:00
|
|
|
for (const OutputRelocation &Reloc : Relocs)
|
|
|
|
applyRelocation(Buf, Reloc);
|
|
|
|
}
|
|
|
|
|
|
|
|
void InputChunk::writeTo(uint8_t *SectionStart) const {
|
2018-01-13 08:22:00 +08:00
|
|
|
memcpy(SectionStart + getOutputOffset(), data().data(), data().size());
|
2018-01-11 03:22:42 +08:00
|
|
|
applyRelocations(SectionStart, OutRelocations);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Populate OutRelocations based on the input relocations and offset within the
|
|
|
|
// output section. Calculates the updated index and offset for each relocation
|
|
|
|
// as well as the value to write out in the final binary.
|
|
|
|
void InputChunk::calcRelocations() {
|
2018-01-13 02:35:13 +08:00
|
|
|
if (Relocations.empty())
|
|
|
|
return;
|
2018-01-11 03:22:42 +08:00
|
|
|
int32_t Off = getOutputOffset() - getInputSectionOffset();
|
2018-01-13 02:35:13 +08:00
|
|
|
DEBUG(dbgs() << "calcRelocations: " << File->getName()
|
2018-01-12 08:34:04 +08:00
|
|
|
<< " offset=" << Twine(Off) << "\n");
|
2018-01-11 03:22:42 +08:00
|
|
|
for (const WasmRelocation &Reloc : Relocations) {
|
|
|
|
OutputRelocation NewReloc;
|
|
|
|
NewReloc.Reloc = Reloc;
|
|
|
|
assert(Reloc.Offset + Off > 0);
|
|
|
|
NewReloc.Reloc.Offset += Off;
|
|
|
|
DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index
|
|
|
|
<< " offset=" << Reloc.Offset
|
|
|
|
<< " newOffset=" << NewReloc.Reloc.Offset << "\n");
|
|
|
|
|
2018-01-23 05:55:43 +08:00
|
|
|
if (Config->Relocatable)
|
2018-01-13 02:35:13 +08:00
|
|
|
NewReloc.NewIndex = File->calcNewIndex(Reloc);
|
2018-01-11 03:22:42 +08:00
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
NewReloc.Value = File->calcNewValue(Reloc);
|
2018-01-11 03:22:42 +08:00
|
|
|
OutRelocations.emplace_back(NewReloc);
|
|
|
|
}
|
|
|
|
}
|
2018-01-13 02:35:13 +08:00
|
|
|
|
|
|
|
void InputFunction::setOutputIndex(uint32_t Index) {
|
|
|
|
DEBUG(dbgs() << "InputFunction::setOutputIndex: " << Index << "\n");
|
|
|
|
assert(!hasOutputIndex());
|
|
|
|
OutputIndex = Index;
|
2018-01-13 08:44:45 +08:00
|
|
|
}
|