2018-01-10 01:13:34 +00:00
|
|
|
//===- InputChunks.h --------------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00: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 01:13:34 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
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"
|
2018-11-27 01:08:16 +00:00
|
|
|
#include "lld/Common/LLVM.h"
|
2018-01-10 01:13:34 +00:00
|
|
|
#include "llvm/Object/Wasm.h"
|
|
|
|
|
|
|
|
namespace lld {
|
|
|
|
namespace wasm {
|
|
|
|
|
|
|
|
class ObjFile;
|
|
|
|
class OutputSegment;
|
2019-05-21 09:13:09 +00:00
|
|
|
class OutputSection;
|
2018-01-10 01:13:34 +00:00
|
|
|
|
|
|
|
class InputChunk {
|
|
|
|
public:
|
2018-04-10 16:12:49 +00:00
|
|
|
enum Kind { DataSegment, Function, SyntheticFunction, Section };
|
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-05-18 23:28:05 +00:00
|
|
|
virtual uint32_t getSize() const { return data().size(); }
|
2018-08-22 17:50:51 +00:00
|
|
|
virtual uint32_t getInputSize() const { return getSize(); };
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-05-18 23:28:05 +00:00
|
|
|
virtual void writeTo(uint8_t *sectionStart) const;
|
2018-01-10 19:22:42 +00:00
|
|
|
|
2018-01-23 01:25:56 +00:00
|
|
|
ArrayRef<WasmRelocation> getRelocations() const { return relocations; }
|
2018-08-22 17:50:51 +00:00
|
|
|
void setRelocations(ArrayRef<WasmRelocation> rs) { relocations = rs; }
|
2018-01-10 19:22:42 +00:00
|
|
|
|
2018-01-28 19:57:03 +00:00
|
|
|
virtual StringRef getName() const = 0;
|
2018-04-20 17:09:18 +00:00
|
|
|
virtual StringRef getDebugName() const = 0;
|
2018-03-14 15:45:11 +00:00
|
|
|
virtual uint32_t getComdat() const = 0;
|
|
|
|
StringRef getComdatName() const;
|
2018-08-22 17:50:51 +00:00
|
|
|
virtual uint32_t getInputSectionOffset() const = 0;
|
2018-01-12 22:25:17 +00:00
|
|
|
|
2019-07-10 09:10:01 +00:00
|
|
|
size_t getNumRelocations() const { return relocations.size(); }
|
2018-02-20 04:26:26 +00:00
|
|
|
void writeRelocations(llvm::raw_ostream &os) const;
|
|
|
|
|
2018-01-31 23:48:14 +00:00
|
|
|
ObjFile *file;
|
2020-03-27 23:52:27 +00:00
|
|
|
OutputSection *outputSec = nullptr;
|
|
|
|
// Offset withing the output section
|
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
|
|
|
|
2019-06-07 06:00:46 +00:00
|
|
|
// Signals the chunk was discarded by COMDAT handling.
|
|
|
|
unsigned discarded : 1;
|
|
|
|
|
2018-01-10 19:22:42 +00:00
|
|
|
protected:
|
2018-01-31 23:48:14 +00:00
|
|
|
InputChunk(ObjFile *f, Kind k)
|
2019-06-07 06:00:46 +00:00
|
|
|
: file(f), live(!config->gcSections), discarded(false), 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;
|
2020-12-02 00:01:33 +00:00
|
|
|
virtual uint64_t getTombstone() const { return 0; }
|
2018-01-10 01:13:34 +00:00
|
|
|
|
2018-05-05 00:18:43 +00: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-22 17:50:51 +00:00
|
|
|
ArrayRef<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
|
2020-01-06 18:21:05 +00:00
|
|
|
// formats, used the term "data segment" to refer to the continuous regions of
|
2018-01-10 01:13:34 +00: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-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
|
|
|
|
2019-04-04 18:40:51 +00:00
|
|
|
void generateRelocationCode(raw_ostream &os) const;
|
|
|
|
|
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-04-20 17:09:18 +00:00
|
|
|
StringRef getDebugName() const override { return StringRef(); }
|
2018-03-14 15:45:11 +00:00
|
|
|
uint32_t getComdat() const override { return segment.Data.Comdat; }
|
2018-08-22 17:50:51 +00:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
|
|
|
return segment.SectionOffset;
|
|
|
|
}
|
2021-02-10 18:11:52 +00:00
|
|
|
uint64_t getVA() const;
|
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-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)
|
2020-10-01 00:21:57 +00:00
|
|
|
: InputChunk(f, InputChunk::Function), signature(s), function(func),
|
|
|
|
exportName(func && func->ExportName.hasValue()
|
|
|
|
? (*func->ExportName).str()
|
|
|
|
: llvm::Optional<std::string>()) {}
|
2018-01-28 19:57:01 +00:00
|
|
|
|
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-05-18 23:28:05 +00:00
|
|
|
void writeTo(uint8_t *sectionStart) const override;
|
2018-04-20 17:09:18 +00:00
|
|
|
StringRef getName() const override { return function->SymbolName; }
|
|
|
|
StringRef getDebugName() const override { return function->DebugName; }
|
2019-12-21 05:44:24 +00:00
|
|
|
llvm::Optional<StringRef> getExportName() const {
|
2020-10-01 00:21:57 +00:00
|
|
|
return exportName.hasValue() ? llvm::Optional<StringRef>(*exportName)
|
|
|
|
: llvm::Optional<StringRef>();
|
2019-11-05 18:15:56 +00:00
|
|
|
}
|
2020-10-01 00:21:57 +00:00
|
|
|
void setExportName(std::string exportName) { this->exportName = exportName; }
|
2018-03-14 15:45:11 +00:00
|
|
|
uint32_t getComdat() const override { return function->Comdat; }
|
2018-05-04 23:14:42 +00:00
|
|
|
uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
|
2018-05-15 22:27:50 +00:00
|
|
|
uint32_t getFunctionCodeOffset() const { return function->CodeOffset; }
|
2018-05-18 23:28:05 +00:00
|
|
|
uint32_t getSize() const override {
|
2018-09-27 00:46:54 +00:00
|
|
|
if (config->compressRelocations && file) {
|
2018-05-18 23:28:05 +00:00
|
|
|
assert(compressedSize);
|
|
|
|
return compressedSize;
|
|
|
|
}
|
|
|
|
return data().size();
|
|
|
|
}
|
2018-08-22 17:50:51 +00:00
|
|
|
uint32_t getInputSize() const override { return function->Size; }
|
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-08-22 17:50:51 +00:00
|
|
|
uint32_t getInputSectionOffset() const override {
|
|
|
|
return function->CodeSectionOffset;
|
|
|
|
}
|
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
|
|
|
|
2018-05-18 23:28:05 +00: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-06 18:21:05 +00:00
|
|
|
// all the symbol values have be calculated but before getSize() is ever
|
2018-05-18 23:28:05 +00:00
|
|
|
// called.
|
|
|
|
void calculateSize();
|
|
|
|
|
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-09-27 00:46:54 +00:00
|
|
|
assert(!config->compressRelocations);
|
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-05-18 23:28:05 +00:00
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
const WasmFunction *function;
|
2020-10-01 00:21:57 +00:00
|
|
|
llvm::Optional<std::string> exportName;
|
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-05-18 23:28:05 +00:00
|
|
|
uint32_t compressedFuncSize = 0;
|
|
|
|
uint32_t compressedSize = 0;
|
2018-01-10 01:13:34 +00:00
|
|
|
};
|
|
|
|
|
2018-01-12 18:35:13 +00:00
|
|
|
class SyntheticFunction : public InputFunction {
|
|
|
|
public:
|
2018-04-20 17:09:18 +00:00
|
|
|
SyntheticFunction(const WasmSignature &s, StringRef name,
|
|
|
|
StringRef debugName = {})
|
|
|
|
: InputFunction(s, nullptr, nullptr), name(name), debugName(debugName) {
|
2018-03-09 16:43:05 +00:00
|
|
|
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-04-20 17:09:18 +00:00
|
|
|
StringRef getDebugName() const override { return debugName; }
|
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-04-20 17:09:18 +00:00
|
|
|
StringRef debugName;
|
2018-03-07 10:37:50 +00:00
|
|
|
ArrayRef<uint8_t> body;
|
2018-01-12 18:35:13 +00:00
|
|
|
};
|
|
|
|
|
2018-04-10 16:12:49 +00:00
|
|
|
// Represents a single Wasm Section within an input file.
|
|
|
|
class InputSection : public InputChunk {
|
|
|
|
public:
|
2018-04-12 20:31:35 +00:00
|
|
|
InputSection(const WasmSection &s, ObjFile *f)
|
2020-12-02 00:01:33 +00:00
|
|
|
: InputChunk(f, InputChunk::Section), section(s), tombstoneValue(getTombstoneForSection(s.Name)) {
|
2018-04-12 20:31:35 +00:00
|
|
|
assert(section.Type == llvm::wasm::WASM_SEC_CUSTOM);
|
|
|
|
}
|
2018-04-10 16:12:49 +00:00
|
|
|
|
|
|
|
StringRef getName() const override { return section.Name; }
|
2018-04-20 17:09:18 +00:00
|
|
|
StringRef getDebugName() const override { return StringRef(); }
|
2020-12-09 18:57:27 +00:00
|
|
|
uint32_t getComdat() const override { return section.Comdat; }
|
2018-04-10 16:12:49 +00:00
|
|
|
|
|
|
|
protected:
|
2018-04-12 20:31:35 +00:00
|
|
|
ArrayRef<uint8_t> data() const override { return section.Content; }
|
2018-04-10 16:12:49 +00: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; }
|
2020-12-02 00:01:33 +00:00
|
|
|
uint64_t getTombstone() const override { return tombstoneValue; }
|
|
|
|
static uint64_t getTombstoneForSection(StringRef name);
|
2018-04-10 16:12:49 +00:00
|
|
|
|
|
|
|
const WasmSection §ion;
|
2020-12-02 00:01:33 +00:00
|
|
|
const uint64_t tombstoneValue;
|
2018-04-10 16:12:49 +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 *);
|
2019-03-29 22:56:39 +00:00
|
|
|
StringRef relocTypeToString(uint8_t relocType);
|
|
|
|
|
2018-01-10 01:13:34 +00:00
|
|
|
} // namespace lld
|
|
|
|
|
|
|
|
#endif // LLD_WASM_INPUT_CHUNKS_H
|