blob: ae2d0733738913c6e18f0ec8b8997e08de7733b5 [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//
10// An input chunk represents an indivisible blocks of code or data from an input
11// file. i.e. a single wasm data segment or a single wasm function.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLD_WASM_INPUT_CHUNKS_H
16#define LLD_WASM_INPUT_CHUNKS_H
17
Sam Clegg03626332018-01-31 01:45:47 +000018#include "Config.h"
Sam Clegg5fa274b2018-01-10 01:13:34 +000019#include "InputFiles.h"
20#include "WriterUtils.h"
21#include "lld/Common/ErrorHandler.h"
22#include "llvm/Object/Wasm.h"
23
24using llvm::object::WasmSegment;
25using llvm::wasm::WasmFunction;
26using llvm::wasm::WasmRelocation;
27using llvm::wasm::WasmSignature;
28using llvm::object::WasmSection;
29
Rui Ueyamabf450d92018-02-20 04:26:26 +000030namespace llvm {
31class raw_ostream;
32}
33
Sam Clegg5fa274b2018-01-10 01:13:34 +000034namespace lld {
35namespace wasm {
36
37class ObjFile;
38class OutputSegment;
39
40class InputChunk {
41public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000042 enum Kind { DataSegment, Function };
43
Sam Clegg3dc44a62018-02-09 07:12:29 +000044 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000045
Sam Cleggfc0723c2018-01-17 18:49:11 +000046 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000047
Sam Clegg5fa274b2018-01-10 01:13:34 +000048 void copyRelocations(const WasmSection &Section);
49
Sam Cleggd96d9352018-01-10 19:22:42 +000050 void writeTo(uint8_t *SectionStart) const;
51
Sam Cleggab604a92018-01-23 01:25:56 +000052 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000053
Sam Clegge0f6fcd2018-01-12 22:25:17 +000054 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000055 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000056
Rui Ueyamabf450d92018-02-20 04:26:26 +000057 size_t NumRelocations() const { return Relocations.size(); }
58 void writeRelocations(llvm::raw_ostream &OS) const;
59
Sam Clegg8f6d2de2018-01-31 23:48:14 +000060 ObjFile *File;
Rui Ueyamabf450d92018-02-20 04:26:26 +000061 int32_t OutputOffset = 0;
Sam Clegg03626332018-01-31 01:45:47 +000062
Sam Clegg447ae402018-02-13 20:29:38 +000063 // Signals that the section is part of the output. The garbage collector,
64 // and COMDAT handling can set a sections' Live bit.
65 // If GC is disabled, all sections start out as live by default.
Sam Clegg03626332018-01-31 01:45:47 +000066 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000067
68protected:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000069 InputChunk(ObjFile *F, Kind K)
Sam Clegg03626332018-01-31 01:45:47 +000070 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000071 virtual ~InputChunk() = default;
Sam Clegg4a379c32018-01-13 00:22:00 +000072 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000073 virtual uint32_t getInputSectionOffset() const = 0;
74
Sam Clegg5fa274b2018-01-10 01:13:34 +000075 std::vector<WasmRelocation> Relocations;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000076 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000077};
78
79// Represents a WebAssembly data segment which can be included as part of
80// an output data segments. Note that in WebAssembly, unlike ELF and other
81// formats, used the term "data segment" to refer to the continous regions of
82// memory that make on the data section. See:
83// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
84//
85// For example, by default, clang will produce a separate data section for
86// each global variable.
87class InputSegment : public InputChunk {
88public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000089 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000090 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
91
92 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000093
94 // Translate an offset in the input segment to an offset in the output
95 // segment.
96 uint32_t translateVA(uint32_t Address) const;
97
98 const OutputSegment *getOutputSegment() const { return OutputSeg; }
99
100 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
101 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +0000102 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000103 }
104
Sam Clegg5fa274b2018-01-10 01:13:34 +0000105 uint32_t getAlignment() const { return Segment.Data.Alignment; }
106 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
107 uint32_t endVA() const { return startVA() + getSize(); }
Sam Cleggfadf5182018-01-28 19:57:03 +0000108 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000109 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000110
Sam Cleggd96d9352018-01-10 19:22:42 +0000111 int32_t OutputSegmentOffset = 0;
112
Sam Clegg5fa274b2018-01-10 01:13:34 +0000113protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000114 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000115 uint32_t getInputSectionOffset() const override {
116 return Segment.SectionOffset;
117 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000118
Sam Clegg5fa274b2018-01-10 01:13:34 +0000119 const WasmSegment &Segment;
120 const OutputSegment *OutputSeg = nullptr;
121};
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:
Sam Clegg50686852018-01-12 18:35:13 +0000127 InputFunction(const WasmSignature &S, const WasmFunction *Func,
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000128 ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000129 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
130
Sam Cleggfc50c622018-01-28 19:57:02 +0000131 static bool classof(const InputChunk *C) {
132 return C->kind() == InputChunk::Function;
133 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000134
Sam Cleggfadf5182018-01-28 19:57:03 +0000135 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000136 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000137 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
138 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000139 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000140 uint32_t getTableIndex() const { return TableIndex.getValue(); }
141 bool hasTableIndex() const { return TableIndex.hasValue(); }
142 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000143
144 const WasmSignature &Signature;
145
146protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000147 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000148 return File->CodeSection->Content.slice(getInputSectionOffset(),
149 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000150 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000151 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000152 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000153 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000154
Sam Clegg50686852018-01-12 18:35:13 +0000155 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000156 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000157 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000158};
159
Sam Clegg50686852018-01-12 18:35:13 +0000160class SyntheticFunction : public InputFunction {
161public:
Sam Clegg1963d712018-01-17 20:19:04 +0000162 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
163 StringRef Name)
164 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
165
166 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000167
Sam Clegg50686852018-01-12 18:35:13 +0000168protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000169 ArrayRef<uint8_t> data() const override { return Body; }
170
Sam Clegg1963d712018-01-17 20:19:04 +0000171 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000172 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000173};
174
Sam Clegg5fa274b2018-01-10 01:13:34 +0000175} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000176
177std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000178} // namespace lld
179
180#endif // LLD_WASM_INPUT_CHUNKS_H