blob: 45ea677a36c086f0863cb8357ae00ebe6aa8c162 [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
18#include "InputFiles.h"
19#include "WriterUtils.h"
20#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
29namespace lld {
30namespace wasm {
31
32class ObjFile;
33class OutputSegment;
34
35class InputChunk {
36public:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000037 enum Kind { DataSegment, Function };
38
39 Kind kind() const { return SectionKind; };
40
Sam Cleggfc0723c2018-01-17 18:49:11 +000041 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000042
Sam Clegg5fa274b2018-01-10 01:13:34 +000043 void copyRelocations(const WasmSection &Section);
44
Sam Cleggd96d9352018-01-10 19:22:42 +000045 void writeTo(uint8_t *SectionStart) const;
46
47 void setOutputOffset(uint32_t Offset) {
48 OutputOffset = Offset;
49 calcRelocations();
50 }
51
52 uint32_t getOutputOffset() const { return OutputOffset; }
Sam Cleggab604a92018-01-23 01:25:56 +000053 ArrayRef<WasmRelocation> getRelocations() const { return Relocations; }
Sam Cleggfadf5182018-01-28 19:57:03 +000054 StringRef getFileName() const { return File->getName(); }
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
59 bool Discarded = false;
Sam Cleggd96d9352018-01-10 19:22:42 +000060 std::vector<OutputRelocation> OutRelocations;
61
62protected:
Sam Clegg24b3dcd2018-01-28 19:57:01 +000063 InputChunk(const ObjFile *F, Kind K) : File(F), SectionKind(K) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000064 virtual ~InputChunk() = default;
65 void calcRelocations();
Sam Clegg4a379c32018-01-13 00:22:00 +000066 virtual ArrayRef<uint8_t> data() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000067 virtual uint32_t getInputSectionOffset() const = 0;
68
Sam Clegg5fa274b2018-01-10 01:13:34 +000069 std::vector<WasmRelocation> Relocations;
Sam Cleggd96d9352018-01-10 19:22:42 +000070 int32_t OutputOffset = 0;
Sam Clegg50686852018-01-12 18:35:13 +000071 const ObjFile *File;
Sam Clegg24b3dcd2018-01-28 19:57:01 +000072 Kind SectionKind;
Sam Clegg5fa274b2018-01-10 01:13:34 +000073};
74
75// Represents a WebAssembly data segment which can be included as part of
76// an output data segments. Note that in WebAssembly, unlike ELF and other
77// formats, used the term "data segment" to refer to the continous regions of
78// memory that make on the data section. See:
79// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
80//
81// For example, by default, clang will produce a separate data section for
82// each global variable.
83class InputSegment : public InputChunk {
84public:
Sam Clegg50686852018-01-12 18:35:13 +000085 InputSegment(const WasmSegment &Seg, const ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +000086 : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {}
87
88 static bool classof(const InputChunk *C) { return C->kind() == DataSegment; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000089
90 // Translate an offset in the input segment to an offset in the output
91 // segment.
92 uint32_t translateVA(uint32_t Address) const;
93
94 const OutputSegment *getOutputSegment() const { return OutputSeg; }
95
96 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
97 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +000098 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +000099 }
100
Sam Clegg5fa274b2018-01-10 01:13:34 +0000101 uint32_t getAlignment() const { return Segment.Data.Alignment; }
102 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
103 uint32_t endVA() const { return startVA() + getSize(); }
Sam Cleggfadf5182018-01-28 19:57:03 +0000104 StringRef getName() const override { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000105 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000106
Sam Cleggd96d9352018-01-10 19:22:42 +0000107 int32_t OutputSegmentOffset = 0;
108
Sam Clegg5fa274b2018-01-10 01:13:34 +0000109protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000110 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000111 uint32_t getInputSectionOffset() const override {
112 return Segment.SectionOffset;
113 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000114
Sam Clegg5fa274b2018-01-10 01:13:34 +0000115 const WasmSegment &Segment;
116 const OutputSegment *OutputSeg = nullptr;
117};
118
119// Represents a single wasm function within and input file. These are
120// combined to create the final output CODE section.
121class InputFunction : public InputChunk {
122public:
Sam Clegg50686852018-01-12 18:35:13 +0000123 InputFunction(const WasmSignature &S, const WasmFunction *Func,
124 const ObjFile *F)
Sam Clegg24b3dcd2018-01-28 19:57:01 +0000125 : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {}
126
Sam Cleggfc50c622018-01-28 19:57:02 +0000127 static bool classof(const InputChunk *C) {
128 return C->kind() == InputChunk::Function;
129 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000130
Sam Cleggfadf5182018-01-28 19:57:03 +0000131 StringRef getName() const override { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000132 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000133 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
134 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000135 void setOutputIndex(uint32_t Index);
Sam Clegg67abf532018-01-24 21:45:25 +0000136 uint32_t getTableIndex() const { return TableIndex.getValue(); }
137 bool hasTableIndex() const { return TableIndex.hasValue(); }
138 void setTableIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000139
140 const WasmSignature &Signature;
141
142protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000143 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000144 return File->CodeSection->Content.slice(getInputSectionOffset(),
145 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000146 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000147 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000148 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000149 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000150
Sam Clegg50686852018-01-12 18:35:13 +0000151 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000152 llvm::Optional<uint32_t> OutputIndex;
Sam Clegg67abf532018-01-24 21:45:25 +0000153 llvm::Optional<uint32_t> TableIndex;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000154};
155
Sam Clegg50686852018-01-12 18:35:13 +0000156class SyntheticFunction : public InputFunction {
157public:
Sam Clegg1963d712018-01-17 20:19:04 +0000158 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
159 StringRef Name)
160 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
161
162 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000163
Sam Clegg50686852018-01-12 18:35:13 +0000164protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000165 ArrayRef<uint8_t> data() const override { return Body; }
166
Sam Clegg1963d712018-01-17 20:19:04 +0000167 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000168 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000169};
170
Sam Clegg5fa274b2018-01-10 01:13:34 +0000171} // namespace wasm
172} // namespace lld
173
174#endif // LLD_WASM_INPUT_CHUNKS_H