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;
|
2018-01-11 03:22:42 +08:00
|
|
|
using namespace lld;
|
2017-11-18 02:14:09 +08:00
|
|
|
using namespace lld::wasm;
|
|
|
|
|
2019-03-29 08:05:00 +08:00
|
|
|
static StringRef reloctTypeToString(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");
|
|
|
|
}
|
|
|
|
|
2018-02-20 06:29:48 +08:00
|
|
|
std::string lld::toString(const InputChunk *C) {
|
|
|
|
return (toString(C->File) + ":(" + C->getName() + ")").str();
|
|
|
|
}
|
|
|
|
|
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:
|
|
|
|
case R_WASM_MEMORY_ADDR_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-29 08:04:58 +08:00
|
|
|
warn("unexpected existing value for " + reloctTypeToString(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
|
|
|
|
|
2018-05-15 21:36:20 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "applying relocations: " << getName()
|
|
|
|
<< " 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);
|
2019-03-29 08:04:58 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "apply reloc: type=" << reloctTypeToString(Rel.Type)
|
2018-05-15 21:36:20 +08:00
|
|
|
<< " addend=" << Rel.Addend << " index=" << Rel.Index
|
|
|
|
<< " 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:
|
|
|
|
case R_WASM_MEMORY_ADDR_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");
|
|
|
|
|
|
|
|
switch (Rel.Type) {
|
2019-02-05 01:49:33 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
|
|
|
case R_WASM_FUNCTION_OFFSET_I32:
|
|
|
|
case R_WASM_SECTION_OFFSET_I32:
|
2018-05-05 07:14:42 +08:00
|
|
|
writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
|
2018-02-20 12:26:26 +08:00
|
|
|
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) {
|
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
|
|
|
}
|