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-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;
|
2018-01-11 03:22:42 +08:00
|
|
|
using namespace lld;
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace lld::wasm;
|
|
|
|
|
2018-03-11 09:35:02 +08:00
|
|
|
StringRef ReloctTypeToString(uint8_t RelocType) {
|
|
|
|
switch (RelocType) {
|
|
|
|
#define WASM_RELOC(NAME, REL) case REL: return #NAME;
|
|
|
|
#include "llvm/BinaryFormat/WasmRelocs.def"
|
|
|
|
#undef WASM_RELOC
|
|
|
|
}
|
|
|
|
llvm_unreachable("unknown reloc type");
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:29:48 +08:00
|
|
|
std::string lld::toString(const InputChunk *C) {
|
|
|
|
return (toString(C->File) + ":(" + C->getName() + ")").str();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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-03-11 09:35:02 +08:00
|
|
|
DEBUG(dbgs() << "applying relocations: count=" << Relocations.size() << "\n");
|
2018-02-20 12:26:26 +08:00
|
|
|
int32_t Off = OutputOffset - getInputSectionOffset();
|
|
|
|
|
|
|
|
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);
|
2018-03-13 03:54:26 +08:00
|
|
|
uint32_t ExistingValue;
|
2018-03-11 09:35:02 +08:00
|
|
|
DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
|
|
|
|
<< " addend=" << Rel.Addend << " index=" << Rel.Index
|
2018-02-20 12:26:26 +08:00
|
|
|
<< " value=" << Value << " offset=" << Rel.Offset << "\n");
|
|
|
|
|
|
|
|
switch (Rel.Type) {
|
|
|
|
case R_WEBASSEMBLY_TYPE_INDEX_LEB:
|
|
|
|
case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
|
|
|
|
case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
2018-03-13 03:54:26 +08:00
|
|
|
ExistingValue = decodeULEB128(Loc);
|
2018-02-20 12:26:26 +08:00
|
|
|
encodeULEB128(Value, Loc, 5);
|
|
|
|
break;
|
|
|
|
case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
2018-03-13 03:54:26 +08:00
|
|
|
ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc));
|
2018-02-20 12:26:26 +08:00
|
|
|
encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
|
|
|
|
break;
|
|
|
|
case R_WEBASSEMBLY_TABLE_INDEX_I32:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
2018-03-13 03:54:26 +08:00
|
|
|
ExistingValue = static_cast<uint32_t>(read32le(Loc));
|
2018-02-20 12:26:26 +08:00
|
|
|
write32le(Loc, Value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("unknown relocation type");
|
|
|
|
}
|
2018-03-13 03:54:26 +08:00
|
|
|
|
|
|
|
uint32_t ExpectedValue = File->calcExpectedValue(Rel);
|
|
|
|
if (ExpectedValue != ExistingValue)
|
|
|
|
error("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
|
|
|
|
": existing=" + Twine(ExistingValue) +
|
|
|
|
" expected=" + Twine(ExpectedValue));
|
2018-02-20 12:26:26 +08:00
|
|
|
}
|
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();
|
|
|
|
DEBUG(dbgs() << "writeRelocations: " << File->getName()
|
2018-01-12 08:34:04 +08:00
|
|
|
<< " 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");
|
|
|
|
|
|
|
|
switch (Rel.Type) {
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
|
|
|
|
writeUleb128(OS, Rel.Addend, "reloc addend");
|
|
|
|
break;
|
|
|
|
}
|
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) {
|
|
|
|
DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> "
|
2018-03-07 21:28:16 +08:00
|
|
|
<< 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-03-07 21:28:16 +08:00
|
|
|
DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
|
|
|
|
<< Index << "\n");
|
2018-01-25 05:45:25 +08:00
|
|
|
assert(!hasTableIndex());
|
|
|
|
TableIndex = Index;
|
|
|
|
}
|