blob: 388e3b980574243cdb6747c4a47e8c0bdbb789ec [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
30namespace lld {
31namespace wasm {
32
33class ObjFile;
34class OutputSegment;
35
36class InputChunk {
37public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000038 enum Kind { DataSegment, Function };
39
Sam Clegg3dc44a62018-02-09 07:12:29 +000040 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000041
Sam Cleggfc0723c2018-01-17 18:49:11 +000042 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000043
Sam Clegg5fa274b2018-01-10 01:13:34 +000044 void copyRelocations(const WasmSection &Section);
45
Sam Cleggd96d9352018-01-10 19:22:42 +000046 void writeTo(uint8_t *SectionStart) const;
47
48 void setOutputOffset(uint32_t Offset) {
49 OutputOffset = Offset;
50 calcRelocations();
51 }
52
53 uint32_t getOutputOffset() const { return OutputOffset; }
Sam Cleggab604a92018-01-23 01:25:56 +000054 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000055
Sam Clegge0f6fcd2018-01-12 22:25:17 +000056 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000057 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000058
Sam Cleggd96d9352018-01-10 19:22:42 +000059 std::vector<OutputRelocation> OutRelocations;
Sam Clegg8f6d2de2018-01-31 23:48:14 +000060 ObjFile *File;
Sam Clegg03626332018-01-31 01:45:47 +000061
Sam Clegg447ae402018-02-13 20:29:38 +000062 // Signals that the section is part of the output. The garbage collector,
63 // and COMDAT handling can set a sections' Live bit.
64 // If GC is disabled, all sections start out as live by default.
Sam Clegg03626332018-01-31 01:45:47 +000065 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000066
67protected:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000068 InputChunk(ObjFile *F, Kind K)
Sam Clegg03626332018-01-31 01:45:47 +000069 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000070 virtual ~InputChunk() = default;
71 void calcRelocations();
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 Cleggd96d9352018-01-10 19:22:42 +000076 int32_t OutputOffset = 0;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000077 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000078};
79
80// Represents a WebAssembly data segment which can be included as part of
81// an output data segments. Note that in WebAssembly, unlike ELF and other
82// formats, used the term "data segment" to refer to the continous regions of
83// memory that make on the data section. See:
84// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
85//
86// For example, by default, clang will produce a separate data section for
87// each global variable.
88class InputSegment : public InputChunk {
89public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000090 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000091 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
92
93 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000094
95 // Translate an offset in the input segment to an offset in the output
96 // segment.
97 uint32_t translateVA(uint32_t Address) const;
98
99 const OutputSegment *getOutputSegment() const { return OutputSeg; }
100
101 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
102 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +0000103 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000104 }
105
Sam Clegg5fa274b2018-01-10 01:13:34 +0000106 uint32_t getAlignment() const { return Segment.Data.Alignment; }
107 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
108 uint32_t endVA() const { return startVA() + getSize(); }
Sam Cleggfadf5182018-01-28 19:57:03 +0000109 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000110 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000111
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;
121 const OutputSegment *OutputSeg = nullptr;
122};
123
124// Represents a single wasm function within and input file. These are
125// combined to create the final output CODE section.
126class InputFunction : public InputChunk {
127public:
Sam Clegg50686852018-01-12 18:35:13 +0000128 InputFunction(const WasmSignature &S, const WasmFunction *Func,
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000129 ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000130 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
131
Sam Cleggfc50c622018-01-28 19:57:02 +0000132 static bool classof(const InputChunk *C) {
133 return C->kind() == InputChunk::Function;
134 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000135
Sam Cleggfadf5182018-01-28 19:57:03 +0000136 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000137 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000138 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
139 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000140 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000141 uint32_t getTableIndex() const { return TableIndex.getValue(); }
142 bool hasTableIndex() const { return TableIndex.hasValue(); }
143 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000144
145 const WasmSignature &Signature;
146
147protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000148 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000149 return File->CodeSection->Content.slice(getInputSectionOffset(),
150 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000151 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000152 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000153 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000154 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000155
Sam Clegg50686852018-01-12 18:35:13 +0000156 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000157 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000158 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000159};
160
Sam Clegg50686852018-01-12 18:35:13 +0000161class SyntheticFunction : public InputFunction {
162public:
Sam Clegg1963d712018-01-17 20:19:04 +0000163 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
164 StringRef Name)
165 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
166
167 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000168
Sam Clegg50686852018-01-12 18:35:13 +0000169protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000170 ArrayRef<uint8_t> data() const override { return Body; }
171
Sam Clegg1963d712018-01-17 20:19:04 +0000172 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000173 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000174};
175
Sam Clegg5fa274b2018-01-10 01:13:34 +0000176} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000177
178std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000179} // namespace lld
180
181#endif // LLD_WASM_INPUT_CHUNKS_H