blob: 1aadbe90d49f61771bfe5a84797ad266ba683dcb [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
29using llvm::object::WasmSegment;
30using llvm::wasm::WasmFunction;
31using llvm::wasm::WasmRelocation;
32using llvm::wasm::WasmSignature;
33using llvm::object::WasmSection;
34
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:
Sam Clegg50686852018-01-12 18:35:13 +0000119 InputFunction(const WasmSignature &S, const WasmFunction *Func,
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000120 ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000121 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
122
Sam Cleggfc50c622018-01-28 19:57:02 +0000123 static bool classof(const InputChunk *C) {
124 return C->kind() == InputChunk::Function;
125 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000126
Sam Cleggfadf5182018-01-28 19:57:03 +0000127 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000128 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000129 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
130 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000131 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000132 uint32_t getTableIndex() const { return TableIndex.getValue(); }
133 bool hasTableIndex() const { return TableIndex.hasValue(); }
134 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000135
136 const WasmSignature &Signature;
137
138protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000139 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000140 return File->CodeSection->Content.slice(getInputSectionOffset(),
141 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000142 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000143 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000144 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000145 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000146
Sam Clegg50686852018-01-12 18:35:13 +0000147 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000148 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000149 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000150};
151
Sam Clegg50686852018-01-12 18:35:13 +0000152class SyntheticFunction : public InputFunction {
153public:
Nicholas Wilson8269f372018-03-07 10:37:50 +0000154 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
155 StringRef Name)
156 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
Sam Clegg1963d712018-01-17 20:19:04 +0000157
158 StringRef getName() const override { return Name; }
Nicholas Wilson8269f372018-03-07 10:37:50 +0000159 StringRef getComdat() const override { return StringRef(); }
Sam Clegg50686852018-01-12 18:35:13 +0000160
Sam Clegg50686852018-01-12 18:35:13 +0000161protected:
Nicholas Wilson8269f372018-03-07 10:37:50 +0000162 ArrayRef<uint8_t> data() const override { return Body; }
Sam Clegg4a379c32018-01-13 00:22:00 +0000163
Sam Clegg1963d712018-01-17 20:19:04 +0000164 StringRef Name;
Nicholas Wilson8269f372018-03-07 10:37:50 +0000165 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000166};
167
Sam Clegg5fa274b2018-01-10 01:13:34 +0000168} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000169
170std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000171} // namespace lld
172
173#endif // LLD_WASM_INPUT_CHUNKS_H