2018-01-10 09:13:34 +08:00
|
|
|
//===- InputChunks.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
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
|
2018-01-10 09:13:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2018-02-23 13:08:53 +08: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 09:13:34 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
#define LLD_WASM_INPUT_CHUNKS_H
|
|
|
|
|
2018-01-31 09:45:47 +08:00
|
|
|
#include "Config.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "InputFiles.h"
|
|
|
|
#include "lld/Common/ErrorHandler.h"
|
2018-11-27 09:08:16 +08:00
|
|
|
#include "lld/Common/LLVM.h"
|
2018-01-10 09:13:34 +08:00
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class ObjFile;
|
|
|
|
class OutputSegment;
|
2019-05-21 17:13:09 +08:00
|
|
|
class OutputSection;
|
2018-01-10 09:13:34 +08:00
|
|
|
|
|
|
|
class InputChunk {
|
|
|
|
public:
|
2018-04-11 00:12:49 +08:00
|
|
|
enum Kind { DataSegment, Function, SyntheticFunction, Section };
|
2018-01-29 03:57:01 +08:00
|
|
|
|
2018-02-09 15:12:29 +08:00
|
|
|
Kind kind() const { return sectionKind; }
|
2018-01-29 03:57:01 +08:00
|
|
|
|
2018-05-19 07:28:05 +08:00
|
|
|
virtual uint32_t getSize() const { return data().size(); }
|
2018-08-23 01:50:51 +08:00
|
|
|
virtual uint32_t getInputSize() const { return getSize(); };
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2018-05-19 07:28:05 +08:00
|
|
|
virtual void writeTo(uint8_t *sectionStart) const;
|
2018-01-11 03:22:42 +08:00
|
|
|
|
2018-01-23 09:25:56 +08:00
|
|
|
ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
|
2018-08-23 01:50:51 +08:00
|
|
|
void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
|
2018-01-11 03:22:42 +08:00
|
|
|
|
2018-01-29 03:57:03 +08:00
|
|
|
virtual StringRef getName() const = 0;
|
2018-04-21 01:09:18 +08:00
|
|
|
virtual StringRef getDebugName() const = 0;
|
2018-03-14 23:45:11 +08:00
|
|
|
virtual uint32_t getComdat() const = 0;
|
|
|
|
StringRef getComdatName() const;
|
2018-08-23 01:50:51 +08:00
|
|
|
virtual uint32_t getInputSectionOffset() const = 0;
|
2018-01-13 06:25:17 +08:00
|
|
|
|
2019-07-10 17:10:01 +08:00
|
|
|
size_t getNumRelocations() const { return relocations.size(); }
|
2018-02-20 12:26:26 +08:00
|
|
|
void writeRelocations(llvm::raw_ostream &os) const;
|
|
|
|
|
2018-02-01 07:48:14 +08:00
|
|
|
ObjFile *file;
|
2018-02-20 12:26:26 +08:00
|
|
|
int32_t outputOffset = 0;
|
2018-01-31 09:45:47 +08:00
|
|
|
|
2018-02-14 04:29:38 +08: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 09:45:47 +08:00
|
|
|
unsigned live : 1;
|
2018-01-11 03:22:42 +08:00
|
|
|
|
2019-06-07 14:00:46 +08:00
|
|
|
// Signals the chunk was discarded by COMDAT handling.
|
|
|
|
unsigned discarded : 1;
|
|
|
|
|
2018-01-11 03:22:42 +08:00
|
|
|
protected:
|
2018-02-01 07:48:14 +08:00
|
|
|
InputChunk(ObjFile *f, Kind k)
|
2019-06-07 14:00:46 +08:00
|
|
|
: file(f), live(!config->gcSections), discarded(false), sectionKind(k) {}
|
2018-01-11 03:22:42 +08:00
|
|
|
virtual ~InputChunk() = default;
|
2018-01-13 08:22:00 +08:00
|
|
|
virtual ArrayRef<uint8_t> data() const = 0;
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2018-05-05 08:18:43 +08:00
|
|
|
// Verifies the existing data at relocation targets matches our expectations.
|
|
|
|
// This is performed only debug builds as an extra sanity check.
|
|
|
|
void verifyRelocTargets() const;
|
|
|
|
|
2018-08-23 01:50:51 +08:00
|
|
|
ArrayRef<WasmRelocation> relocations;
|
2018-01-29 03:57:01 +08:00
|
|
|
Kind sectionKind;
|
2018-01-10 09:13:34 +08: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
|
2020-01-07 02:21:05 +08:00
|
|
|
// formats, used the term "data segment" to refer to the continuous regions of
|
2018-01-10 09:13:34 +08:00
|
|
|
// 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-02-01 07:48:14 +08:00
|
|
|
InputSegment(const WasmSegment &seg, ObjFile *f)
|
2018-01-29 03:57:01 +08:00
|
|
|
: InputChunk(f, InputChunk::DataSegment), segment(seg) {}
|
|
|
|
|
|
|
|
static bool classof(const InputChunk *c) { return c->kind() == DataSegment; }
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2019-04-05 02:40:51 +08:00
|
|
|
void generateRelocationCode(raw_ostream &os) const;
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
uint32_t getAlignment() const { return segment.Data.Alignment; }
|
2018-01-29 03:57:03 +08:00
|
|
|
StringRef getName() const override { return segment.Data.Name; }
|
2018-04-21 01:09:18 +08:00
|
|
|
StringRef getDebugName() const override { return StringRef(); }
|
2018-03-14 23:45:11 +08:00
|
|
|
uint32_t getComdat() const override { return segment.Data.Comdat; }
|
2018-08-23 01:50:51 +08:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
|
|
|
return segment.SectionOffset;
|
|
|
|
}
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2018-02-28 08:20:29 +08:00
|
|
|
const OutputSegment *outputSeg = nullptr;
|
2018-01-11 03:22:42 +08:00
|
|
|
int32_t outputSegmentOffset = 0;
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
protected:
|
2018-01-13 08:22:00 +08:00
|
|
|
ArrayRef<uint8_t> data() const override { return segment.Data.Content; }
|
2018-01-13 06:25:17 +08:00
|
|
|
|
2018-01-10 09:13:34 +08: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 21:28:16 +08:00
|
|
|
InputFunction(const WasmSignature &s, const WasmFunction *func, ObjFile *f)
|
2018-01-29 03:57:01 +08:00
|
|
|
: InputChunk(f, InputChunk::Function), signature(s), function(func) {}
|
|
|
|
|
2018-01-29 03:57:02 +08:00
|
|
|
static bool classof(const InputChunk *c) {
|
2018-03-10 00:43:05 +08:00
|
|
|
return c->kind() == InputChunk::Function ||
|
|
|
|
c->kind() == InputChunk::SyntheticFunction;
|
2018-01-29 03:57:02 +08:00
|
|
|
}
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2018-05-19 07:28:05 +08:00
|
|
|
void writeTo(uint8_t *sectionStart) const override;
|
2018-04-21 01:09:18 +08:00
|
|
|
StringRef getName() const override { return function->SymbolName; }
|
|
|
|
StringRef getDebugName() const override { return function->DebugName; }
|
2019-11-06 02:15:56 +08:00
|
|
|
StringRef getExportName() const {
|
|
|
|
return function ? function->ExportName : "";
|
|
|
|
}
|
2018-03-14 23:45:11 +08:00
|
|
|
uint32_t getComdat() const override { return function->Comdat; }
|
2018-05-05 07:14:42 +08:00
|
|
|
uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
|
2018-05-16 06:27:50 +08:00
|
|
|
uint32_t getFunctionCodeOffset() const { return function->CodeOffset; }
|
2018-05-19 07:28:05 +08:00
|
|
|
uint32_t getSize() const override {
|
2018-09-27 08:46:54 +08:00
|
|
|
if (config->compressRelocations && file) {
|
2018-05-19 07:28:05 +08:00
|
|
|
assert(compressedSize);
|
|
|
|
return compressedSize;
|
|
|
|
}
|
|
|
|
return data().size();
|
|
|
|
}
|
2018-08-23 01:50:51 +08:00
|
|
|
uint32_t getInputSize() const override { return function->Size; }
|
2018-03-13 03:56:23 +08:00
|
|
|
uint32_t getFunctionIndex() const { return functionIndex.getValue(); }
|
|
|
|
bool hasFunctionIndex() const { return functionIndex.hasValue(); }
|
|
|
|
void setFunctionIndex(uint32_t index);
|
2018-08-23 01:50:51 +08:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
|
|
|
return function->CodeSectionOffset;
|
|
|
|
}
|
2018-01-25 05:45:25 +08:00
|
|
|
uint32_t getTableIndex() const { return tableIndex.getValue(); }
|
|
|
|
bool hasTableIndex() const { return tableIndex.hasValue(); }
|
|
|
|
void setTableIndex(uint32_t index);
|
2018-01-10 09:13:34 +08:00
|
|
|
|
2018-05-19 07:28:05 +08:00
|
|
|
// The size of a given input function can depend on the values of the
|
|
|
|
// LEB relocations within it. This finalizeContents method is called after
|
2020-01-07 02:21:05 +08:00
|
|
|
// all the symbol values have be calculated but before getSize() is ever
|
2018-05-19 07:28:05 +08:00
|
|
|
// called.
|
|
|
|
void calculateSize();
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
const WasmSignature &signature;
|
|
|
|
|
|
|
|
protected:
|
2018-01-13 08:22:00 +08:00
|
|
|
ArrayRef<uint8_t> data() const override {
|
2018-09-27 08:46:54 +08:00
|
|
|
assert(!config->compressRelocations);
|
2018-01-18 02:49:11 +08:00
|
|
|
return file->codeSection->Content.slice(getInputSectionOffset(),
|
|
|
|
function->Size);
|
2018-01-13 08:22:00 +08:00
|
|
|
}
|
2018-05-19 07:28:05 +08:00
|
|
|
|
2018-01-13 02:35:13 +08:00
|
|
|
const WasmFunction *function;
|
2018-03-13 03:56:23 +08:00
|
|
|
llvm::Optional<uint32_t> functionIndex;
|
2018-01-25 05:45:25 +08:00
|
|
|
llvm::Optional<uint32_t> tableIndex;
|
2018-05-19 07:28:05 +08:00
|
|
|
uint32_t compressedFuncSize = 0;
|
|
|
|
uint32_t compressedSize = 0;
|
2018-01-10 09:13:34 +08:00
|
|
|
};
|
|
|
|
|
2018-01-13 02:35:13 +08:00
|
|
|
class SyntheticFunction : public InputFunction {
|
|
|
|
public:
|
2018-04-21 01:09:18 +08:00
|
|
|
SyntheticFunction(const WasmSignature &s, StringRef name,
|
|
|
|
StringRef debugName = {})
|
|
|
|
: InputFunction(s, nullptr, nullptr), name(name), debugName(debugName) {
|
2018-03-10 00:43:05 +08:00
|
|
|
sectionKind = InputChunk::SyntheticFunction;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool classof(const InputChunk *c) {
|
|
|
|
return c->kind() == InputChunk::SyntheticFunction;
|
|
|
|
}
|
2018-01-18 04:19:04 +08:00
|
|
|
|
|
|
|
StringRef getName() const override { return name; }
|
2018-04-21 01:09:18 +08:00
|
|
|
StringRef getDebugName() const override { return debugName; }
|
2018-03-14 23:45:11 +08:00
|
|
|
uint32_t getComdat() const override { return UINT32_MAX; }
|
2018-01-13 02:35:13 +08:00
|
|
|
|
2018-03-10 00:43:05 +08:00
|
|
|
void setBody(ArrayRef<uint8_t> body_) { body = body_; }
|
|
|
|
|
2018-01-13 02:35:13 +08:00
|
|
|
protected:
|
2018-03-07 18:37:50 +08:00
|
|
|
ArrayRef<uint8_t> data() const override { return body; }
|
2018-01-13 08:22:00 +08:00
|
|
|
|
2018-01-18 04:19:04 +08:00
|
|
|
StringRef name;
|
2018-04-21 01:09:18 +08:00
|
|
|
StringRef debugName;
|
2018-03-07 18:37:50 +08:00
|
|
|
ArrayRef<uint8_t> body;
|
2018-01-13 02:35:13 +08:00
|
|
|
};
|
|
|
|
|
2018-04-11 00:12:49 +08:00
|
|
|
// Represents a single Wasm Section within an input file.
|
|
|
|
class InputSection : public InputChunk {
|
|
|
|
public:
|
2018-04-13 04:31:35 +08:00
|
|
|
InputSection(const WasmSection &s, ObjFile *f)
|
|
|
|
: InputChunk(f, InputChunk::Section), section(s) {
|
|
|
|
assert(section.Type == llvm::wasm::WASM_SEC_CUSTOM);
|
|
|
|
}
|
2018-04-11 00:12:49 +08:00
|
|
|
|
|
|
|
StringRef getName() const override { return section.Name; }
|
2018-04-21 01:09:18 +08:00
|
|
|
StringRef getDebugName() const override { return StringRef(); }
|
2018-04-11 00:12:49 +08:00
|
|
|
uint32_t getComdat() const override { return UINT32_MAX; }
|
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
OutputSection *outputSec = nullptr;
|
|
|
|
|
2018-04-11 00:12:49 +08:00
|
|
|
protected:
|
2018-04-13 04:31:35 +08:00
|
|
|
ArrayRef<uint8_t> data() const override { return section.Content; }
|
2018-04-11 00:12:49 +08:00
|
|
|
|
|
|
|
// Offset within the input section. This is only zero since this chunk
|
|
|
|
// type represents an entire input section, not part of one.
|
|
|
|
uint32_t getInputSectionOffset() const override { return 0; }
|
|
|
|
|
|
|
|
const WasmSection §ion;
|
|
|
|
};
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
} // namespace wasm
|
2018-02-20 06:29:48 +08:00
|
|
|
|
|
|
|
std::string toString(const wasm::InputChunk *);
|
2019-03-30 06:56:39 +08:00
|
|
|
StringRef relocTypeToString(uint8_t relocType);
|
|
|
|
|
2018-01-10 09:13:34 +08:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_WASM_INPUT_CHUNKS_H
|