blob: 434680a1a092afeac2ec7b412cc680f71e51a417 [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
40 Kind kind() const { return SectionKind; };
41
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 Cleggfadf5182018-01-28 19:57:03 +000055 StringRef getFileName() const { return File->getName(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000056
Sam Clegge0f6fcd2018-01-12 22:25:17 +000057 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000058 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000059
60 bool Discarded = false;
Sam Cleggd96d9352018-01-10 19:22:42 +000061 std::vector<OutputRelocation> OutRelocations;
Sam Clegg03626332018-01-31 01:45:47 +000062 const ObjFile *File;
63
64 // The garbage collector sets sections' Live bits.
65 // If GC is disabled, all sections are considered live by default.
66 unsigned Live : 1;
Sam Cleggd96d9352018-01-10 19:22:42 +000067
68protected:
Sam Clegg03626332018-01-31 01:45:47 +000069 InputChunk(const ObjFile *F, Kind K)
70 : File(F), Live(!Config->GcSections), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000071 virtual ~InputChunk() = default;
72 void calcRelocations();
Sam Clegg4a379c32018-01-13 00:22:00 +000073 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000074 virtual uint32_t getInputSectionOffset() const = 0;
75
Sam Clegg5fa274b2018-01-10 01:13:34 +000076 std::vector<WasmRelocation> Relocations;
Sam Cleggd96d9352018-01-10 19:22:42 +000077 int32_t OutputOffset = 0;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000078 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000079};
80
81// Represents a WebAssembly data segment which can be included as part of
82// an output data segments. Note that in WebAssembly, unlike ELF and other
83// formats, used the term "data segment" to refer to the continous regions of
84// memory that make on the data section. See:
85// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
86//
87// For example, by default, clang will produce a separate data section for
88// each global variable.
89class InputSegment : public InputChunk {
90public:
Sam Clegg50686852018-01-12 18:35:13 +000091 InputSegment(const WasmSegment &Seg, const ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000092 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
93
94 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000095
96 // Translate an offset in the input segment to an offset in the output
97 // segment.
98 uint32_t translateVA(uint32_t Address) const;
99
100 const OutputSegment *getOutputSegment() const { return OutputSeg; }
101
102 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
103 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +0000104 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000105 }
106
Sam Clegg5fa274b2018-01-10 01:13:34 +0000107 uint32_t getAlignment() const { return Segment.Data.Alignment; }
108 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
109 uint32_t endVA() const { return startVA() + getSize(); }
Sam Cleggfadf5182018-01-28 19:57:03 +0000110 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000111 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000112
Sam Cleggd96d9352018-01-10 19:22:42 +0000113 int32_t OutputSegmentOffset = 0;
114
Sam Clegg5fa274b2018-01-10 01:13:34 +0000115protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000116 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000117 uint32_t getInputSectionOffset() const override {
118 return Segment.SectionOffset;
119 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000120
Sam Clegg5fa274b2018-01-10 01:13:34 +0000121 const WasmSegment &Segment;
122 const OutputSegment *OutputSeg = nullptr;
123};
124
125// Represents a single wasm function within and input file. These are
126// combined to create the final output CODE section.
127class InputFunction : public InputChunk {
128public:
Sam Clegg50686852018-01-12 18:35:13 +0000129 InputFunction(const WasmSignature &S, const WasmFunction *Func,
130 const ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000131 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
132
Sam Cleggfc50c622018-01-28 19:57:02 +0000133 static bool classof(const InputChunk *C) {
134 return C->kind() == InputChunk::Function;
135 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000136
Sam Cleggfadf5182018-01-28 19:57:03 +0000137 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000138 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000139 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
140 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000141 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000142 uint32_t getTableIndex() const { return TableIndex.getValue(); }
143 bool hasTableIndex() const { return TableIndex.hasValue(); }
144 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000145
146 const WasmSignature &Signature;
147
148protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000149 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000150 return File->CodeSection->Content.slice(getInputSectionOffset(),
151 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000152 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000153 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000154 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000155 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000156
Sam Clegg50686852018-01-12 18:35:13 +0000157 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000158 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000159 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000160};
161
Sam Clegg50686852018-01-12 18:35:13 +0000162class SyntheticFunction : public InputFunction {
163public:
Sam Clegg1963d712018-01-17 20:19:04 +0000164 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
165 StringRef Name)
166 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
167
168 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000169
Sam Clegg50686852018-01-12 18:35:13 +0000170protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000171 ArrayRef<uint8_t> data() const override { return Body; }
172
Sam Clegg1963d712018-01-17 20:19:04 +0000173 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000174 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000175};
176
Sam Clegg5fa274b2018-01-10 01:13:34 +0000177} // namespace wasm
178} // namespace lld
179
180#endif // LLD_WASM_INPUT_CHUNKS_H