blob: adecb3f750a95cfc2676320bafda79bbdc7d5f5d [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"
Sam Clegg5fa274b2018-01-10 01:13:34 +000020#include "lld/Common/ErrorHandler.h"
21#include "llvm/Object/Wasm.h"
22
23using llvm::object::WasmSegment;
24using llvm::wasm::WasmFunction;
25using llvm::wasm::WasmRelocation;
26using llvm::wasm::WasmSignature;
27using llvm::object::WasmSection;
28
Rui Ueyamabf450d92018-02-20 04:26:26 +000029namespace llvm {
30class raw_ostream;
31}
32
Sam Clegg5fa274b2018-01-10 01:13:34 +000033namespace lld {
34namespace wasm {
35
36class ObjFile;
37class OutputSegment;
38
39class InputChunk {
40public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000041 enum Kind { DataSegment, Function };
42
Sam Clegg3dc44a62018-02-09 07:12:29 +000043 Kind kind() const { return SectionKind; }
Sam Clegg24b3dcd2018-01-28 19:57:01 +000044
Sam Cleggfc0723c2018-01-17 18:49:11 +000045 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000046
Sam Clegg5fa274b2018-01-10 01:13:34 +000047 void copyRelocations(const WasmSection &Section);
48
Sam Cleggd96d9352018-01-10 19:22:42 +000049 void writeTo(uint8_t *SectionStart) const;
50
Sam Cleggab604a92018-01-23 01:25:56 +000051 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggd96d9352018-01-10 19:22:42 +000052
Sam Clegge0f6fcd2018-01-12 22:25:17 +000053 virtual StringRef getComdat() const = 0;
Sam Cleggfadf5182018-01-28 19:57:03 +000054 virtual StringRef getName() const = 0;
Sam Clegge0f6fcd2018-01-12 22:25:17 +000055
Rui Ueyamabf450d92018-02-20 04:26:26 +000056 size_t NumRelocations() const { return Relocations.size(); }
57 void writeRelocations(llvm::raw_ostream &OS) const;
58
Sam Clegg8f6d2de2018-01-31 23:48:14 +000059 ObjFile *File;
Rui Ueyamabf450d92018-02-20 04:26:26 +000060 int32_t OutputOffset = 0;
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;
Sam Clegg4a379c32018-01-13 00:22:00 +000071 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000072 virtual uint32_t getInputSectionOffset() const = 0;
73
Sam Clegg5fa274b2018-01-10 01:13:34 +000074 std::vector<WasmRelocation> Relocations;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000075 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000076};
77
78// Represents a WebAssembly data segment which can be included as part of
79// an output data segments. Note that in WebAssembly, unlike ELF and other
80// formats, used the term "data segment" to refer to the continous regions of
81// memory that make on the data section. See:
82// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
83//
84// For example, by default, clang will produce a separate data section for
85// each global variable.
86class InputSegment : public InputChunk {
87public:
Sam Clegg8f6d2de2018-01-31 23:48:14 +000088 InputSegment(const WasmSegment &Seg, ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000089 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
90
91 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000092
93 // Translate an offset in the input segment to an offset in the output
94 // segment.
95 uint32_t translateVA(uint32_t Address) const;
96
97 const OutputSegment *getOutputSegment() const { return OutputSeg; }
98
99 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
100 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +0000101 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000102 }
103
Sam Clegg5fa274b2018-01-10 01:13:34 +0000104 uint32_t getAlignment() const { return Segment.Data.Alignment; }
105 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
106 uint32_t endVA() const { return startVA() + getSize(); }
Sam Cleggfadf5182018-01-28 19:57:03 +0000107 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000108 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000109
Sam Cleggd96d9352018-01-10 19:22:42 +0000110 int32_t OutputSegmentOffset = 0;
111
Sam Clegg5fa274b2018-01-10 01:13:34 +0000112protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000113 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000114 uint32_t getInputSectionOffset() const override {
115 return Segment.SectionOffset;
116 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000117
Sam Clegg5fa274b2018-01-10 01:13:34 +0000118 const WasmSegment &Segment;
119 const OutputSegment *OutputSeg = nullptr;
120};
121
122// Represents a single wasm function within and input file. These are
123// combined to create the final output CODE section.
124class InputFunction : public InputChunk {
125public:
Sam Clegg50686852018-01-12 18:35:13 +0000126 InputFunction(const WasmSignature &S, const WasmFunction *Func,
Sam Clegg8f6d2de2018-01-31 23:48:14 +0000127 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) {
131 return C->kind() == InputChunk::Function;
132 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000133
Sam Cleggfadf5182018-01-28 19:57:03 +0000134 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000135 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000136 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
137 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000138 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000139 uint32_t getTableIndex() const { return TableIndex.getValue(); }
140 bool hasTableIndex() const { return TableIndex.hasValue(); }
141 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000142
143 const WasmSignature &Signature;
144
145protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000146 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000147 return File->CodeSection->Content.slice(getInputSectionOffset(),
148 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000149 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000150 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000151 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000152 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000153
Sam Clegg50686852018-01-12 18:35:13 +0000154 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000155 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000156 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000157};
158
Sam Clegg50686852018-01-12 18:35:13 +0000159class SyntheticFunction : public InputFunction {
160public:
Sam Clegg1963d712018-01-17 20:19:04 +0000161 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
162 StringRef Name)
163 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
164
165 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000166
Sam Clegg50686852018-01-12 18:35:13 +0000167protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000168 ArrayRef<uint8_t> data() const override { return Body; }
169
Sam Clegg1963d712018-01-17 20:19:04 +0000170 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000171 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000172};
173
Sam Clegg5fa274b2018-01-10 01:13:34 +0000174} // namespace wasm
Rui Ueyama81bee042018-02-19 22:29:48 +0000175
176std::string toString(const wasm::InputChunk *);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000177} // namespace lld
178
179#endif // LLD_WASM_INPUT_CHUNKS_H