Do not create a temporary data structure for relocations.

This patch removes `OutRelocations` vector from the InputChunk and
directly consume `Relocations` vector instead. This should make the linker
faster because we don't create a temporary data structure, and it matches
the lld's design principle that we don't create temporary data structures
for object files but instead directly consume mmap'ed data whenever possible.

Differential Revision: https://reviews.llvm.org/D43491

llvm-svn: 325549
This commit is contained in:
Rui Ueyama 2018-02-20 04:26:26 +00:00
parent 41f64c3204
commit bf450d905d
5 changed files with 71 additions and 105 deletions

View File

@ -10,6 +10,7 @@
#include "InputChunks.h"
#include "Config.h"
#include "OutputSegment.h"
#include "WriterUtils.h"
#include "lld/Common/ErrorHandler.h"
#include "lld/Common/LLVM.h"
#include "llvm/Support/LEB128.h"
@ -44,76 +45,69 @@ void InputChunk::copyRelocations(const WasmSection &Section) {
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");
// Copy this input chunk to an mmap'ed output file and apply relocations.
void InputChunk::writeTo(uint8_t *Buf) const {
// Copy contents
memcpy(Buf + OutputOffset, data().data(), data().size());
Buf += Reloc.Reloc.Offset;
// Apply relocations
if (Relocations.empty())
return;
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);
DEBUG(dbgs() << "applyRelocations: count=" << Relocations.size() << "\n");
int32_t Off = OutputOffset - getInputSectionOffset();
for (const WasmRelocation &Rel : Relocations) {
uint8_t *Loc = Buf + Rel.Offset + Off;
uint64_t Value = File->calcNewValue(Rel);
DEBUG(dbgs() << "write reloc: type=" << Rel.Type << " index=" << Rel.Index
<< " 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:
encodeULEB128(Value, Loc, 5);
break;
case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
break;
case R_WEBASSEMBLY_TABLE_INDEX_I32:
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
write32le(Loc, Value);
break;
default:
llvm_unreachable("unknown relocation type");
}
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:
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);
}
// 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() {
// 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 {
if (Relocations.empty())
return;
int32_t Off = getOutputOffset() - getInputSectionOffset();
DEBUG(dbgs() << "calcRelocations: " << File->getName()
int32_t Off = OutputOffset - getInputSectionOffset();
DEBUG(dbgs() << "writeRelocations: " << File->getName()
<< " offset=" << Twine(Off) << "\n");
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");
if (Config->Relocatable)
NewReloc.NewIndex = File->calcNewIndex(Reloc);
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");
NewReloc.Value = File->calcNewValue(Reloc);
OutRelocations.emplace_back(NewReloc);
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;
}
}
}

View File

@ -27,6 +27,10 @@ using llvm::wasm::WasmRelocation;
using llvm::wasm::WasmSignature;
using llvm::object::WasmSection;
namespace llvm {
class raw_ostream;
}
namespace lld {
namespace wasm {
@ -45,19 +49,16 @@ public:
void writeTo(uint8_t *SectionStart) const;
void setOutputOffset(uint32_t Offset) {
OutputOffset = Offset;
calcRelocations();
}
uint32_t getOutputOffset() const { return OutputOffset; }
ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
virtual StringRef getComdat() const = 0;
virtual StringRef getName() const = 0;
std::vector<OutputRelocation> OutRelocations;
size_t NumRelocations() const { return Relocations.size(); }
void writeRelocations(llvm::raw_ostream &OS) const;
ObjFile *File;
int32_t OutputOffset = 0;
// Signals that the section is part of the output. The garbage collector,
// and COMDAT handling can set a sections' Live bit.
@ -68,12 +69,10 @@ protected:
InputChunk(ObjFile *F, Kind K)
: File(F), Live(!Config->GcSections), SectionKind(K) {}
virtual ~InputChunk() = default;
void calcRelocations();
virtual ArrayRef<uint8_t> data() const = 0;
virtual uint32_t getInputSectionOffset() const = 0;
std::vector<WasmRelocation> Relocations;
int32_t OutputOffset = 0;
Kind SectionKind;
};

View File

@ -90,7 +90,7 @@ CodeSection::CodeSection(ArrayRef<InputFunction *> Functions)
BodySize = CodeSectionHeader.size();
for (InputChunk *Func : Functions) {
Func->setOutputOffset(BodySize);
Func->OutputOffset = BodySize;
BodySize += Func->getSize();
}
@ -123,14 +123,13 @@ void CodeSection::writeTo(uint8_t *Buf) {
uint32_t CodeSection::numRelocations() const {
uint32_t Count = 0;
for (const InputChunk *Func : Functions)
Count += Func->OutRelocations.size();
Count += Func->NumRelocations();
return Count;
}
void CodeSection::writeRelocations(raw_ostream &OS) const {
for (const InputChunk *Func : Functions)
for (const OutputRelocation &Reloc : Func->OutRelocations)
writeReloc(OS, Reloc);
for (const InputChunk *C : Functions)
C->writeRelocations(OS);
}
DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
@ -153,9 +152,9 @@ DataSection::DataSection(ArrayRef<OutputSegment *> Segments)
BodySize += Segment->Header.size() + Segment->Size;
log("Data segment: size=" + Twine(Segment->Size));
for (InputSegment *InputSeg : Segment->InputSegments)
InputSeg->setOutputOffset(Segment->getSectionOffset() +
Segment->Header.size() +
InputSeg->OutputSegmentOffset);
InputSeg->OutputOffset = Segment->getSectionOffset() +
Segment->Header.size() +
InputSeg->OutputSegmentOffset;
}
createHeader(BodySize);
@ -190,13 +189,12 @@ uint32_t DataSection::numRelocations() const {
uint32_t Count = 0;
for (const OutputSegment *Seg : Segments)
for (const InputChunk *InputSeg : Seg->InputSegments)
Count += InputSeg->OutRelocations.size();
Count += InputSeg->NumRelocations();
return Count;
}
void DataSection::writeRelocations(raw_ostream &OS) const {
for (const OutputSegment *Seg : Segments)
for (const InputChunk *InputSeg : Seg->InputSegments)
for (const OutputRelocation &Reloc : InputSeg->OutRelocations)
writeReloc(OS, Reloc);
for (const InputChunk *C : Seg->InputSegments)
C->writeRelocations(OS);
}

View File

@ -162,23 +162,6 @@ void wasm::writeExport(raw_ostream &OS, const WasmExport &Export) {
fatal("unsupported export type: " + Twine(Export.Kind));
}
}
void wasm::writeReloc(raw_ostream &OS, const OutputRelocation &Reloc) {
writeUleb128(OS, Reloc.Reloc.Type, "reloc type");
writeUleb128(OS, Reloc.Reloc.Offset, "reloc offset");
writeUleb128(OS, Reloc.NewIndex, "reloc index");
switch (Reloc.Reloc.Type) {
case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
case R_WEBASSEMBLY_MEMORY_ADDR_I32:
writeUleb128(OS, Reloc.Reloc.Addend, "reloc addend");
break;
default:
break;
}
}
} // namespace lld
std::string lld::toString(ValType Type) {

View File

@ -31,12 +31,6 @@ inline bool operator!=(const llvm::wasm::WasmSignature &LHS,
namespace lld {
namespace wasm {
struct OutputRelocation {
llvm::wasm::WasmRelocation Reloc;
uint32_t NewIndex;
uint32_t Value;
};
void debugWrite(uint64_t Offset, const Twine &Msg);
void writeUleb128(raw_ostream &OS, uint32_t Number, StringRef Msg);
@ -67,8 +61,6 @@ void writeImport(raw_ostream &OS, const llvm::wasm::WasmImport &Import);
void writeExport(raw_ostream &OS, const llvm::wasm::WasmExport &Export);
void writeReloc(raw_ostream &OS, const OutputRelocation &Reloc);
} // namespace wasm
std::string toString(const llvm::wasm::ValType Type);