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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-02-23 05:08:53 +00:00
|
|
|
// An InputChunks represents an indivisible opaque region of a input wasm file.
|
|
|
|
// i.e. a single wasm data segment or a single wasm function.
|
|
|
|
//
|
|
|
|
// They are written directly to the mmap'd output file after which relocations
|
|
|
|
// are applied. Because each Chunk is independent they can be written in
|
|
|
|
// parallel.
|
|
|
|
//
|
|
|
|
// Chunks are also unit on which garbage collection (--gc-sections) operates.
|
2018-01-10 01:13:34 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
#define LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
|
2018-01-31 01:45:47 +00:00
|
|
|
#include "Config.h"
|
2018-01-10 01:13:34 +00:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
2018-03-07 13:28:16 +00:00
|
|
|
using llvm::object::WasmSection;
|
2018-01-10 01:13:34 +00:00
|
|
|
using llvm::object::WasmSegment;
|
|
|
|
using llvm::wasm::WasmFunction;
|
|
|
|
using llvm::wasm::WasmRelocation;
|
|
|
|
using llvm::wasm::WasmSignature;
|
|
|
|
|
2018-02-20 04:26:26 +00:00
|
|
|
namespace llvm {
|
|
|
|
class raw_ostream;
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class ObjFile;
|
|
|
|
class OutputSegment;
|
|
|
|
|
|
|
|
class InputChunk {
|
|
|
|
public:
|
2018-03-09 16:43:05 +00:00
|
|
|
enum Kind { DataSegment, Function, SyntheticFunction };
|
2018-01-28 19:57:01 +00:00
|
|
|
|
2018-02-09 07:12:29 +00:00
|
|
|
Kind kind() const { return SectionKind; }
|
2018-01-28 19:57:01 +00:00
|
|
|
|
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;
|
|
|
|
|
2018-01-23 01:25:56 +00:00
|
|
|
ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
|
2018-01-10 19:22:42 +00:00
|
|
|
|
2018-01-28 19:57:03 +00:00
|
|
|
virtual StringRef getName() const = 0;
|
2018-03-14 15:45:11 +00:00
|
|
|
virtual uint32_t getComdat() const = 0;
|
|
|
|
StringRef getComdatName() const;
|
2018-01-12 22:25:17 +00:00
|
|
|
|
2018-02-20 04:26:26 +00:00
|
|
|
size_t NumRelocations() const { return Relocations.size(); }
|
|
|
|
void writeRelocations(llvm::raw_ostream &OS) const;
|
|
|
|
|
2018-01-31 23:48:14 +00:00
|
|
|
ObjFile *File;
|
2018-02-20 04:26:26 +00:00
|
|
|
int32_t OutputOffset = 0;
|
2018-01-31 01:45:47 +00:00
|
|
|
|
2018-02-13 20:29:38 +00:00
|
|
|
// Signals that the section is part of the output. The garbage collector,
|
|
|
|
// and COMDAT handling can set a sections' Live bit.
|
|
|
|
// If GC is disabled, all sections start out as live by default.
|
2018-01-31 01:45:47 +00:00
|
|
|
unsigned Live : 1;
|
2018-01-10 19:22:42 +00:00
|
|
|
|
|
|
|
protected:
|
2018-01-31 23:48:14 +00:00
|
|
|
InputChunk(ObjFile *F, Kind K)
|
2018-01-31 01:45:47 +00:00
|
|
|
: File(F), Live(!Config->GcSections), SectionKind(K) {}
|
2018-01-10 19:22:42 +00:00
|
|
|
virtual ~InputChunk() = default;
|
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-28 19:57:01 +00:00
|
|
|
Kind SectionKind;
|
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-31 23:48:14 +00:00
|
|
|
InputSegment(const WasmSegment &Seg, ObjFile *F)
|
2018-01-28 19:57:01 +00:00
|
|
|
: InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
|
|
|
|
|
|
|
|
static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
|
2018-01-10 01:13:34 +00:00
|
|
|
|
|
|
|
uint32_t getAlignment() const { return Segment.Data.Alignment; }
|
2018-01-28 19:57:03 +00:00
|
|
|
StringRef getName() const override { return Segment.Data.Name; }
|
2018-03-14 15:45:11 +00:00
|
|
|
uint32_t getComdat() const override { return Segment.Data.Comdat; }
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-02-28 00:20:29 +00:00
|
|
|
const OutputSegment *OutputSeg = nullptr;
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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-03-07 13:28:16 +00:00
|
|
|
InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F)
|
2018-01-28 19:57:01 +00:00
|
|
|
: InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
|
|
|
|
|
2018-01-28 19:57:02 +00:00
|
|
|
static bool classof(const InputChunk *C) {
|
2018-03-09 16:43:05 +00:00
|
|
|
return C->kind() == InputChunk::Function ||
|
|
|
|
C->kind() == InputChunk::SyntheticFunction;
|
2018-01-28 19:57:02 +00:00
|
|
|
}
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-01-28 19:57:03 +00:00
|
|
|
StringRef getName() const override { return Function->Name; }
|
2018-03-14 15:45:11 +00:00
|
|
|
uint32_t getComdat() const override { return Function->Comdat; }
|
2018-03-12 19:56:23 +00:00
|
|
|
uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); }
|
|
|
|
bool hasFunctionIndex() const { return FunctionIndex.hasValue(); }
|
|
|
|
void setFunctionIndex(uint32_t Index);
|
2018-01-24 21:45:25 +00:00
|
|
|
uint32_t getTableIndex() const { return TableIndex.getValue(); }
|
|
|
|
bool hasTableIndex() const { return TableIndex.hasValue(); }
|
|
|
|
void setTableIndex(uint32_t Index);
|
2018-01-10 01:13:34 +00:00
|
|
|
|
|
|
|
const WasmSignature &Signature;
|
|
|
|
|
|
|
|
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-03-12 19:56:23 +00:00
|
|
|
llvm::Optional<uint32_t> FunctionIndex;
|
2018-01-24 21:45:25 +00:00
|
|
|
llvm::Optional<uint32_t> TableIndex;
|
2018-01-10 01:13:34 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
class SyntheticFunction : public InputFunction {
|
|
|
|
public:
|
2018-03-09 16:43:05 +00:00
|
|
|
SyntheticFunction(const WasmSignature &S, StringRef Name)
|
|
|
|
: InputFunction(S, nullptr, nullptr), Name(Name) {
|
|
|
|
SectionKind = InputChunk::SyntheticFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const InputChunk *C) {
|
|
|
|
return C->kind() == InputChunk::SyntheticFunction;
|
|
|
|
}
|
2018-01-17 20:19:04 +00:00
|
|
|
|
|
|
|
StringRef getName() const override { return Name; }
|
2018-03-14 15:45:11 +00:00
|
|
|
uint32_t getComdat() const override { return UINT32_MAX; }
|
2018-01-12 18:35:13 +00:00
|
|
|
|
2018-03-09 16:43:05 +00:00
|
|
|
void setBody(ArrayRef<uint8_t> Body_) { Body = Body_; }
|
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
protected:
|
2018-03-07 10:37:50 +00:00
|
|
|
ArrayRef<uint8_t> data() const override { return Body; }
|
2018-01-13 00:22:00 +00:00
|
|
|
|
2018-01-17 20:19:04 +00:00
|
|
|
StringRef Name;
|
2018-03-07 10:37:50 +00:00
|
|
|
ArrayRef<uint8_t> Body;
|
2018-01-12 18:35:13 +00:00
|
|
|
};
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
} // namespace wasm
|
2018-02-19 22:29:48 +00:00
|
|
|
|
|
|
|
std::string toString(const wasm::InputChunk *);
|
2018-01-10 01:13:34 +00:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_WASM_INPUT_CHUNKS_H
|