2018-01-10 01:13:34 +00:00
|
|
|
//===- InputChunks.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Linker
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// An input chunk represents an indivisible blocks of code or data from an input
|
|
|
|
// file. i.e. a single wasm data segment or a single wasm function.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
#define LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
|
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "WriterUtils.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
|
|
|
using llvm::object::WasmSegment;
|
|
|
|
using llvm::wasm::WasmFunction;
|
|
|
|
using llvm::wasm::WasmRelocation;
|
|
|
|
using llvm::wasm::WasmSignature;
|
|
|
|
using llvm::object::WasmSection;
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class ObjFile;
|
|
|
|
class OutputSegment;
|
|
|
|
|
|
|
|
class InputChunk {
|
|
|
|
public:
|
2018-01-17 18:49:11 +00:00
|
|
|
uint32_t getSize() const { return data().size(); }
|
2018-01-10 19:22:42 +00:00
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
void copyRelocations(const WasmSection &Section);
|
|
|
|
|
2018-01-10 19:22:42 +00:00
|
|
|
void writeTo(uint8_t *SectionStart) const;
|
|
|
|
|
|
|
|
void setOutputOffset(uint32_t Offset) {
|
|
|
|
OutputOffset = Offset;
|
|
|
|
calcRelocations();
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getOutputOffset() const { return OutputOffset; }
|
|
|
|
|
2018-01-12 22:25:17 +00:00
|
|
|
virtual StringRef getComdat() const = 0;
|
|
|
|
|
|
|
|
bool Discarded = false;
|
2018-01-10 19:22:42 +00:00
|
|
|
std::vector<OutputRelocation> OutRelocations;
|
|
|
|
|
|
|
|
protected:
|
2018-01-12 18:35:13 +00:00
|
|
|
InputChunk(const ObjFile *F) : File(F) {}
|
2018-01-10 19:22:42 +00:00
|
|
|
virtual ~InputChunk() = default;
|
|
|
|
void calcRelocations();
|
2018-01-13 00:22:00 +00:00
|
|
|
virtual ArrayRef<uint8_t> data() const = 0;
|
2018-01-10 01:13:34 +00:00
|
|
|
virtual uint32_t getInputSectionOffset() const = 0;
|
|
|
|
|
|
|
|
std::vector<WasmRelocation> Relocations;
|
2018-01-10 19:22:42 +00:00
|
|
|
int32_t OutputOffset = 0;
|
2018-01-12 18:35:13 +00:00
|
|
|
const ObjFile *File;
|
2018-01-10 01:13:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Represents a WebAssembly data segment which can be included as part of
|
|
|
|
// an output data segments. Note that in WebAssembly, unlike ELF and other
|
|
|
|
// formats, used the term "data segment" to refer to the continous regions of
|
|
|
|
// memory that make on the data section. See:
|
|
|
|
// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
|
|
|
|
//
|
|
|
|
// For example, by default, clang will produce a separate data section for
|
|
|
|
// each global variable.
|
|
|
|
class InputSegment : public InputChunk {
|
|
|
|
public:
|
2018-01-12 18:35:13 +00:00
|
|
|
InputSegment(const WasmSegment &Seg, const ObjFile *F)
|
2018-01-10 01:13:34 +00:00
|
|
|
: InputChunk(F), Segment(Seg) {}
|
|
|
|
|
|
|
|
// Translate an offset in the input segment to an offset in the output
|
|
|
|
// segment.
|
|
|
|
uint32_t translateVA(uint32_t Address) const;
|
|
|
|
|
|
|
|
const OutputSegment *getOutputSegment() const { return OutputSeg; }
|
|
|
|
|
|
|
|
void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
|
|
|
|
OutputSeg = Segment;
|
2018-01-10 19:22:42 +00:00
|
|
|
OutputSegmentOffset = Offset;
|
2018-01-10 01:13:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t getAlignment() const { return Segment.Data.Alignment; }
|
|
|
|
uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
|
|
|
|
uint32_t endVA() const { return startVA() + getSize(); }
|
|
|
|
StringRef getName() const { return Segment.Data.Name; }
|
2018-01-12 22:25:17 +00:00
|
|
|
StringRef getComdat() const override { return Segment.Data.Comdat; }
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-01-10 19:22:42 +00:00
|
|
|
int32_t OutputSegmentOffset = 0;
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
protected:
|
2018-01-13 00:22:00 +00:00
|
|
|
ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
|
2018-01-10 19:22:42 +00:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
|
|
|
return Segment.SectionOffset;
|
|
|
|
}
|
2018-01-12 22:25:17 +00:00
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
const WasmSegment &Segment;
|
|
|
|
const OutputSegment *OutputSeg = nullptr;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Represents a single wasm function within and input file. These are
|
|
|
|
// combined to create the final output CODE section.
|
|
|
|
class InputFunction : public InputChunk {
|
|
|
|
public:
|
2018-01-12 18:35:13 +00:00
|
|
|
InputFunction(const WasmSignature &S, const WasmFunction *Func,
|
|
|
|
const ObjFile *F)
|
2018-01-12 17:56:15 +00:00
|
|
|
: InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {}
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-01-12 22:25:17 +00:00
|
|
|
StringRef getComdat() const override { return Function->Comdat; }
|
2018-01-17 18:49:11 +00:00
|
|
|
uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
|
|
|
|
bool hasOutputIndex() const { return OutputIndex.hasValue(); }
|
2018-01-12 18:35:13 +00:00
|
|
|
void setOutputIndex(uint32_t Index);
|
2018-01-10 01:13:34 +00:00
|
|
|
|
|
|
|
const WasmSignature &Signature;
|
|
|
|
|
2018-01-12 17:56:15 +00:00
|
|
|
unsigned WrittenToNameSec : 1;
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
protected:
|
2018-01-13 00:22:00 +00:00
|
|
|
ArrayRef<uint8_t> data() const override {
|
2018-01-17 18:49:11 +00:00
|
|
|
return File->CodeSection->Content.slice(getInputSectionOffset(),
|
|
|
|
Function->Size);
|
2018-01-13 00:22:00 +00:00
|
|
|
}
|
2018-01-10 19:22:42 +00:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
2018-01-12 18:35:13 +00:00
|
|
|
return Function->CodeSectionOffset;
|
2018-01-10 19:22:42 +00:00
|
|
|
}
|
2018-01-13 00:22:00 +00:00
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
const WasmFunction *Function;
|
2018-01-10 01:13:34 +00:00
|
|
|
llvm::Optional<uint32_t> OutputIndex;
|
|
|
|
};
|
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
class SyntheticFunction : public InputFunction {
|
|
|
|
public:
|
2018-01-13 00:22:00 +00:00
|
|
|
SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body)
|
2018-01-12 18:35:13 +00:00
|
|
|
: InputFunction(S, nullptr, nullptr), Body(Body) {}
|
|
|
|
|
|
|
|
protected:
|
2018-01-13 00:22:00 +00:00
|
|
|
ArrayRef<uint8_t> data() const override { return Body; }
|
|
|
|
|
|
|
|
ArrayRef<uint8_t> Body;
|
2018-01-12 18:35:13 +00:00
|
|
|
};
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
} // namespace wasm
|
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_WASM_INPUT_CHUNKS_H
|