blob: 7220309591877bd86c36e6a44584e32073382042 [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"
27#include "llvm/Object/Wasm.h"
28
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +000029using llvm::object::WasmSection;
Sam Clegg5fa274b2018-01-10 01:13:34 +000030using llvm::object::WasmSegment;
31using llvm::wasm::WasmFunction;
32using llvm::wasm::WasmRelocation;
33using llvm::wasm::WasmSignature;
Sam Clegg5fa274b2018-01-10 01:13:34 +000034
Rui Ueyamabf450d92018-02-20 04:26:26 +000035namespace llvm {
36class raw_ostream;
37}
38
Sam Clegg5fa274b2018-01-10 01:13:34 +000039namespace lld {
40namespace wasm {
41
42class ObjFile;
43class OutputSegment;
44
45class InputChunk {
46public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000047 enum Kind { DataSegment, Function };
48
Sam Clegg3dc44a62018-02-09 07:12:29 +000049 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000050
Sam Cleggfc0723c2018-01-17 18:49:11 +000051 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000052
Sam Clegg5fa274b2018-01-10 01:13:34 +000053 void copyRelocations(const WasmSection &Section);
54
Sam Cleggd96d9352018-01-10 19:22:42 +000055 void writeTo(uint8_t *SectionStart) const;
56
Sam Cleggab604a92018-01-23 01:25:56 +000057 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000058
Sam Clegge0f6fcd2018-01-12 22:25:17 +000059 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000060 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000061
Rui Ueyamabf450d92018-02-20 04:26:26 +000062 size_t NumRelocations() const { return Relocations.size(); }
63 void writeRelocations(llvm::raw_ostream &OS) const;
64
Sam Clegg8f6d2de2018-01-31 23:48:14 +000065 ObjFile *File;
Rui Ueyamabf450d92018-02-20 04:26:26 +000066 int32_t OutputOffset = 0;
Sam Clegg03626332018-01-31 01:45:47 +000067
Sam Clegg447ae402018-02-13 20:29:38 +000068 // Signals that the section is part of the output. The garbage collector,
69 // and COMDAT handling can set a sections' Live bit.
70 // If GC is disabled, all sections start out as live by default.
Sam Clegg03626332018-01-31 01:45:47 +000071 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000072
73protected:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000074 InputChunk(ObjFile *F, Kind K)
Sam Clegg03626332018-01-31 01:45:47 +000075 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000076 virtual ~InputChunk() = default;
Sam Clegg4a379c32018-01-13 00:22:00 +000077 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000078 virtual uint32_t getInputSectionOffset() const = 0;
79
Sam Clegg5fa274b2018-01-10 01:13:34 +000080 std::vector<WasmRelocation> Relocations;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000081 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000082};
83
84// Represents a WebAssembly data segment which can be included as part of
85// an output data segments. Note that in WebAssembly, unlike ELF and other
86// formats, used the term "data segment" to refer to the continous regions of
87// memory that make on the data section. See:
88// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
89//
90// For example, by default, clang will produce a separate data section for
91// each global variable.
92class InputSegment : public InputChunk {
93public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000094 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000095 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
96
97 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000098
Sam Clegg5fa274b2018-01-10 01:13:34 +000099 uint32_t getAlignment() const { return Segment.Data.Alignment; }
Sam Cleggfadf5182018-01-28 19:57:03 +0000100 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000101 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000102
Rui Ueyama28f3b202018-02-28 00:20:29 +0000103 const OutputSegment *OutputSeg = nullptr;
Sam Cleggd96d9352018-01-10 19:22:42 +0000104 int32_t OutputSegmentOffset = 0;
105
Sam Clegg5fa274b2018-01-10 01:13:34 +0000106protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000107 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000108 uint32_t getInputSectionOffset() const override {
109 return Segment.SectionOffset;
110 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000111
Sam Clegg5fa274b2018-01-10 01:13:34 +0000112 const WasmSegment &Segment;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000113};
114
115// Represents a single wasm function within and input file. These are
116// combined to create the final output CODE section.
117class InputFunction : public InputChunk {
118public:
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000119 InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000120 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
121
Sam Cleggfc50c622018-01-28 19:57:02 +0000122 static bool classof(const InputChunk *C) {
123 return C->kind() == InputChunk::Function;
124 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000125
Sam Cleggfadf5182018-01-28 19:57:03 +0000126 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000127 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000128 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
129 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000130 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000131 uint32_t getTableIndex() const { return TableIndex.getValue(); }
132 bool hasTableIndex() const { return TableIndex.hasValue(); }
133 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000134
135 const WasmSignature &Signature;
136
137protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000138 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000139 return File->CodeSection->Content.slice(getInputSectionOffset(),
140 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000141 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000142 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000143 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000144 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000145
Sam Clegg50686852018-01-12 18:35:13 +0000146 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000147 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000148 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000149};
150
Sam Clegg50686852018-01-12 18:35:13 +0000151class SyntheticFunction : public InputFunction {
152public:
Nicholas Wilson8269f372018-03-07 10:37:50 +0000153 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
154 StringRef Name)
155 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
Sam Clegg1963d712018-01-17 20:19:04 +0000156
157 StringRef getName() const override { return Name; }
Nicholas Wilson8269f372018-03-07 10:37:50 +0000158 StringRef getComdat() const override { return StringRef(); }
Sam Clegg50686852018-01-12 18:35:13 +0000159
Sam Clegg50686852018-01-12 18:35:13 +0000160protected:
Nicholas Wilson8269f372018-03-07 10:37:50 +0000161 ArrayRef<uint8_t> data() const override { return Body; }
Sam Clegg4a379c32018-01-13 00:22:00 +0000162
Sam Clegg1963d712018-01-17 20:19:04 +0000163 StringRef Name;
Nicholas Wilson8269f372018-03-07 10:37:50 +0000164 ArrayRef<uint8_t> 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