blob: bff574b168311eef9cf16c6cb273e12338d2906a [file] [log] [blame]
Sam Clegg5fa274b2018-01-10 01:13:34 +00001//===- InputChunks.h --------------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Sam Clegg93102972018-02-23 05:08:53 +000010// An InputChunks represents an indivisible opaque region of a input wasm file.
11// i.e. a single wasm data segment or a single wasm function.
12//
13// They are written directly to the mmap'd output file after which relocations
14// are applied. Because each Chunk is independent they can be written in
15// parallel.
16//
17// Chunks are also unit on which garbage collection (--gc-sections) operates.
Sam Clegg5fa274b2018-01-10 01:13:34 +000018//
19//===----------------------------------------------------------------------===//
20
21#ifndef LLD_WASM_INPUT_CHUNKS_H
22#define LLD_WASM_INPUT_CHUNKS_H
23
Sam Clegg03626332018-01-31 01:45:47 +000024#include "Config.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000025#include "InputFiles.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000026#include "lld/Common/ErrorHandler.h"
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +000027#include "lld/Common/Strings.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000028#include "llvm/Object/Wasm.h"
29
30using llvm::object::WasmSegment;
31using llvm::wasm::WasmFunction;
32using llvm::wasm::WasmRelocation;
33using llvm::wasm::WasmSignature;
34using llvm::object::WasmSection;
35
Rui Ueyamabf450d92018-02-20 04:26:26 +000036namespace llvm {
37class raw_ostream;
38}
39
Sam Clegg5fa274b2018-01-10 01:13:34 +000040namespace lld {
41namespace wasm {
42
43class ObjFile;
44class OutputSegment;
45
46class InputChunk {
47public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000048 enum Kind { DataSegment, Function };
49
Sam Clegg3dc44a62018-02-09 07:12:29 +000050 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000051
Sam Cleggfc0723c2018-01-17 18:49:11 +000052 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000053
Sam Clegg5fa274b2018-01-10 01:13:34 +000054 void copyRelocations(const WasmSection &Section);
55
Sam Cleggd96d9352018-01-10 19:22:42 +000056 void writeTo(uint8_t *SectionStart) const;
57
Sam Cleggab604a92018-01-23 01:25:56 +000058 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000059
Sam Clegge0f6fcd2018-01-12 22:25:17 +000060 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000061 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000062
Rui Ueyamabf450d92018-02-20 04:26:26 +000063 size_t NumRelocations() const { return Relocations.size(); }
64 void writeRelocations(llvm::raw_ostream &OS) const;
65
Sam Clegg8f6d2de2018-01-31 23:48:14 +000066 ObjFile *File;
Rui Ueyamabf450d92018-02-20 04:26:26 +000067 int32_t OutputOffset = 0;
Sam Clegg03626332018-01-31 01:45:47 +000068
Sam Clegg447ae402018-02-13 20:29:38 +000069 // Signals that the section is part of the output. The garbage collector,
70 // and COMDAT handling can set a sections' Live bit.
71 // If GC is disabled, all sections start out as live by default.
Sam Clegg03626332018-01-31 01:45:47 +000072 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000073
74protected:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000075 InputChunk(ObjFile *F, Kind K)
Sam Clegg03626332018-01-31 01:45:47 +000076 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000077 virtual ~InputChunk() = default;
Sam Clegg4a379c32018-01-13 00:22:00 +000078 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000079 virtual uint32_t getInputSectionOffset() const = 0;
80
Sam Clegg5fa274b2018-01-10 01:13:34 +000081 std::vector<WasmRelocation> Relocations;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000082 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000083};
84
85// Represents a WebAssembly data segment which can be included as part of
86// an output data segments. Note that in WebAssembly, unlike ELF and other
87// formats, used the term "data segment" to refer to the continous regions of
88// memory that make on the data section. See:
89// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
90//
91// For example, by default, clang will produce a separate data section for
92// each global variable.
93class InputSegment : public InputChunk {
94public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000095 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000096 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
97
98 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000099
Sam Clegg5fa274b2018-01-10 01:13:34 +0000100 uint32_t getAlignment() const { return Segment.Data.Alignment; }
Sam Cleggfadf5182018-01-28 19:57:03 +0000101 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000102 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000103
Rui Ueyama28f3b202018-02-28 00:20:29 +0000104 const OutputSegment *OutputSeg = nullptr;
Sam Cleggd96d9352018-01-10 19:22:42 +0000105 int32_t OutputSegmentOffset = 0;
106
Sam Clegg5fa274b2018-01-10 01:13:34 +0000107protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000108 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000109 uint32_t getInputSectionOffset() const override {
110 return Segment.SectionOffset;
111 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000112
Sam Clegg5fa274b2018-01-10 01:13:34 +0000113 const WasmSegment &Segment;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000114};
115
116// Represents a single wasm function within and input file. These are
117// combined to create the final output CODE section.
118class InputFunction : public InputChunk {
119public:
Sam Clegg50686852018-01-12 18:35:13 +0000120 InputFunction(const WasmSignature &S, const WasmFunction *Func,
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000121 ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000122 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
123
Sam Cleggfc50c622018-01-28 19:57:02 +0000124 static bool classof(const InputChunk *C) {
125 return C->kind() == InputChunk::Function;
126 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000127
Sam Cleggfadf5182018-01-28 19:57:03 +0000128 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000129 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000130 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
131 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000132 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000133 uint32_t getTableIndex() const { return TableIndex.getValue(); }
134 bool hasTableIndex() const { return TableIndex.hasValue(); }
135 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000136
137 const WasmSignature &Signature;
138
139protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000140 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000141 return File->CodeSection->Content.slice(getInputSectionOffset(),
142 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000143 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000144 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000145 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000146 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000147
Sam Clegg50686852018-01-12 18:35:13 +0000148 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000149 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000150 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000151};
152
Sam Clegg50686852018-01-12 18:35:13 +0000153class SyntheticFunction : public InputFunction {
154public:
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000155 SyntheticFunction(const WasmSignature &S, std::string Body, StringRef Name)
156 : InputFunction(S, nullptr, nullptr), Name(Name), Body(std::move(Body)) {}
Sam Clegg1963d712018-01-17 20:19:04 +0000157
158 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000159
Sam Clegg50686852018-01-12 18:35:13 +0000160protected:
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000161 ArrayRef<uint8_t> data() const override { return toArrayRef(Body); }
Sam Clegg4a379c32018-01-13 00:22:00 +0000162
Sam Clegg1963d712018-01-17 20:19:04 +0000163 StringRef Name;
Nicholas Wilsonf6dbc2e2018-03-02 14:48:50 +0000164 std::string Body;
Sam Clegg50686852018-01-12 18:35:13 +0000165};
166
Sam Clegg5fa274b2018-01-10 01:13:34 +0000167} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000168
169std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000170} // namespace lld
171
172#endif // LLD_WASM_INPUT_CHUNKS_H