blob: 526e29870b21eead77588c218a96f78e7e3b8d6f [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 Clegg80ba4382018-04-10 16:12:49 +000047 enum Kind { DataSegment, Function, SyntheticFunction, Section };
Sam Clegg24b3dcd2018-01-28 19:57:01 +000048
Sam Clegg3dc44a62018-02-09 07:12:29 +000049 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000050
Sam Cleggfb983cd2018-05-18 23:28:05 +000051 virtual 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 Cleggfb983cd2018-05-18 23:28:05 +000055 virtual void writeTo(uint8_t *SectionStart) const;
Sam Cleggd96d9352018-01-10 19:22:42 +000056
Sam Cleggab604a92018-01-23 01:25:56 +000057 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000058
Sam Cleggfadf5182018-01-28 19:57:03 +000059 virtual StringRef getName() const = 0;
Nicholas Wilson6c7fe302018-04-20 17:09:18 +000060 virtual StringRef getDebugName() const = 0;
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000061 virtual uint32_t getComdat() const = 0;
62 StringRef getComdatName() const;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000063
Rui Ueyamabf450d92018-02-20 04:26:26 +000064 size_t NumRelocations() const { return Relocations.size(); }
65 void writeRelocations(llvm::raw_ostream &OS) const;
66
Sam Clegg8f6d2de2018-01-31 23:48:14 +000067 ObjFile *File;
Rui Ueyamabf450d92018-02-20 04:26:26 +000068 int32_t OutputOffset = 0;
Sam Clegg03626332018-01-31 01:45:47 +000069
Sam Clegg447ae402018-02-13 20:29:38 +000070 // Signals that the section is part of the output. The garbage collector,
71 // and COMDAT handling can set a sections' Live bit.
72 // If GC is disabled, all sections start out as live by default.
Sam Clegg03626332018-01-31 01:45:47 +000073 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000074
75protected:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000076 InputChunk(ObjFile *F, Kind K)
Sam Clegg03626332018-01-31 01:45:47 +000077 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000078 virtual ~InputChunk() = default;
Sam Clegg4a379c32018-01-13 00:22:00 +000079 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000080 virtual uint32_t getInputSectionOffset() const = 0;
Sam Cleggfb983cd2018-05-18 23:28:05 +000081 virtual uint32_t getInputSize() const { return getSize(); };
Sam Clegg5fa274b2018-01-10 01:13:34 +000082
Sam Cleggc1953142018-05-05 00:18:43 +000083 // Verifies the existing data at relocation targets matches our expectations.
84 // This is performed only debug builds as an extra sanity check.
85 void verifyRelocTargets() const;
86
Sam Clegg5fa274b2018-01-10 01:13:34 +000087 std::vector<WasmRelocation> Relocations;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000088 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000089};
90
91// Represents a WebAssembly data segment which can be included as part of
92// an output data segments. Note that in WebAssembly, unlike ELF and other
93// formats, used the term "data segment" to refer to the continous regions of
94// memory that make on the data section. See:
95// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
96//
97// For example, by default, clang will produce a separate data section for
98// each global variable.
99class InputSegment : public InputChunk {
100public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000101 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000102 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
103
104 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000105
Sam Clegg5fa274b2018-01-10 01:13:34 +0000106 uint32_t getAlignment() const { return Segment.Data.Alignment; }
Sam Cleggfadf5182018-01-28 19:57:03 +0000107 StringRef getName() const override { return Segment.Data.Name; }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000108 StringRef getDebugName() const override { return StringRef(); }
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000109 uint32_t getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000110
Rui Ueyama28f3b202018-02-28 00:20:29 +0000111 const OutputSegment *OutputSeg = nullptr;
Sam Cleggd96d9352018-01-10 19:22:42 +0000112 int32_t OutputSegmentOffset = 0;
113
Sam Clegg5fa274b2018-01-10 01:13:34 +0000114protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000115 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000116 uint32_t getInputSectionOffset() const override {
117 return Segment.SectionOffset;
118 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000119
Sam Clegg5fa274b2018-01-10 01:13:34 +0000120 const WasmSegment &Segment;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000121};
122
123// Represents a single wasm function within and input file. These are
124// combined to create the final output CODE section.
125class InputFunction : public InputChunk {
126public:
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000127 InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000128 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
129
Sam Cleggfc50c622018-01-28 19:57:02 +0000130 static bool classof(const InputChunk *C) {
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000131 return C->kind() == InputChunk::Function ||
132 C->kind() == InputChunk::SyntheticFunction;
Sam Cleggfc50c622018-01-28 19:57:02 +0000133 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000134
Sam Cleggfb983cd2018-05-18 23:28:05 +0000135 void writeTo(uint8_t *SectionStart) const override;
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000136 StringRef getName() const override { return Function->SymbolName; }
137 StringRef getDebugName() const override { return Function->DebugName; }
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000138 uint32_t getComdat() const override { return Function->Comdat; }
Sam Cleggd177ab22018-05-04 23:14:42 +0000139 uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
Sam Cleggcfeb6462018-05-15 22:27:50 +0000140 uint32_t getFunctionCodeOffset() const { return Function->CodeOffset; }
Sam Cleggfb983cd2018-05-18 23:28:05 +0000141 uint32_t getSize() const override {
142 if (Config->CompressRelocTargets && File) {
143 assert(CompressedSize);
144 return CompressedSize;
145 }
146 return data().size();
147 }
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000148 uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); }
149 bool hasFunctionIndex() const { return FunctionIndex.hasValue(); }
150 void setFunctionIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000151 uint32_t getTableIndex() const { return TableIndex.getValue(); }
152 bool hasTableIndex() const { return TableIndex.hasValue(); }
153 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000154
Sam Cleggfb983cd2018-05-18 23:28:05 +0000155 // The size of a given input function can depend on the values of the
156 // LEB relocations within it. This finalizeContents method is called after
157 // all the symbol values have be calcualted but before getSize() is ever
158 // called.
159 void calculateSize();
160
Sam Clegg5fa274b2018-01-10 01:13:34 +0000161 const WasmSignature &Signature;
162
163protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000164 ArrayRef<uint8_t> data() const override {
Sam Cleggfb983cd2018-05-18 23:28:05 +0000165 assert(!Config->CompressRelocTargets);
Sam Cleggfc0723c2018-01-17 18:49:11 +0000166 return File->CodeSection->Content.slice(getInputSectionOffset(),
167 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000168 }
Sam Cleggfb983cd2018-05-18 23:28:05 +0000169
170 uint32_t getInputSize() const override { return Function->Size; }
171
Sam Cleggd96d9352018-01-10 19:22:42 +0000172 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000173 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000174 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000175
Sam Clegg50686852018-01-12 18:35:13 +0000176 const WasmFunction *Function;
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000177 llvm::Optional<uint32_t> FunctionIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000178 llvm::Optional<uint32_t> TableIndex;
Sam Cleggfb983cd2018-05-18 23:28:05 +0000179 uint32_t CompressedFuncSize = 0;
180 uint32_t CompressedSize = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000181};
182
Sam Clegg50686852018-01-12 18:35:13 +0000183class SyntheticFunction : public InputFunction {
184public:
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000185 SyntheticFunction(const WasmSignature &S, StringRef Name,
186 StringRef DebugName = {})
187 : InputFunction(S, nullptr, nullptr), Name(Name), DebugName(DebugName) {
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000188 SectionKind = InputChunk::SyntheticFunction;
189 }
190
191 static bool classof(const InputChunk *C) {
192 return C->kind() == InputChunk::SyntheticFunction;
193 }
Sam Clegg1963d712018-01-17 20:19:04 +0000194
195 StringRef getName() const override { return Name; }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000196 StringRef getDebugName() const override { return DebugName; }
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +0000197 uint32_t getComdat() const override { return UINT32_MAX; }
Sam Clegg50686852018-01-12 18:35:13 +0000198
Nicholas Wilsonebda41f2018-03-09 16:43:05 +0000199 void setBody(ArrayRef<uint8_t> Body_) { Body = Body_; }
200
Sam Clegg50686852018-01-12 18:35:13 +0000201protected:
Nicholas Wilson8269f372018-03-07 10:37:50 +0000202 ArrayRef<uint8_t> data() const override { return Body; }
Sam Clegg4a379c32018-01-13 00:22:00 +0000203
Sam Clegg1963d712018-01-17 20:19:04 +0000204 StringRef Name;
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000205 StringRef DebugName;
Nicholas Wilson8269f372018-03-07 10:37:50 +0000206 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000207};
208
Sam Clegg80ba4382018-04-10 16:12:49 +0000209// Represents a single Wasm Section within an input file.
210class InputSection : public InputChunk {
211public:
Sam Clegg225c4692018-04-12 20:31:35 +0000212 InputSection(const WasmSection &S, ObjFile *F)
213 : InputChunk(F, InputChunk::Section), Section(S) {
214 assert(Section.Type == llvm::wasm::WASM_SEC_CUSTOM);
215 }
Sam Clegg80ba4382018-04-10 16:12:49 +0000216
217 StringRef getName() const override { return Section.Name; }
Nicholas Wilson6c7fe302018-04-20 17:09:18 +0000218 StringRef getDebugName() const override { return StringRef(); }
Sam Clegg80ba4382018-04-10 16:12:49 +0000219 uint32_t getComdat() const override { return UINT32_MAX; }
220
221protected:
Sam Clegg225c4692018-04-12 20:31:35 +0000222 ArrayRef<uint8_t> data() const override { return Section.Content; }
Sam Clegg80ba4382018-04-10 16:12:49 +0000223
224 // Offset within the input section. This is only zero since this chunk
225 // type represents an entire input section, not part of one.
226 uint32_t getInputSectionOffset() const override { return 0; }
227
228 const WasmSection &Section;
Sam Clegg80ba4382018-04-10 16:12:49 +0000229};
230
Sam Clegg5fa274b2018-01-10 01:13:34 +0000231} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000232
233std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000234} // namespace lld
235
236#endif // LLD_WASM_INPUT_CHUNKS_H