blob: ff9a7ea0966f22f055a8d43bb3c8a4849a33493b [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 Cleggd96d9352018-01-10 19:22:42 +000037 virtual uint32_t getSize() const = 0;
38
Sam Clegg5fa274b2018-01-10 01:13:34 +000039 void copyRelocations(const WasmSection &Section);
40
Sam Cleggd96d9352018-01-10 19:22:42 +000041 void writeTo(uint8_t *SectionStart) const;
42
43 void setOutputOffset(uint32_t Offset) {
44 OutputOffset = Offset;
45 calcRelocations();
46 }
47
48 uint32_t getOutputOffset() const { return OutputOffset; }
49
50 std::vector<OutputRelocation> OutRelocations;
51
52protected:
Sam Clegg50686852018-01-12 18:35:13 +000053 InputChunk(const ObjFile *F) : File(F) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000054 virtual ~InputChunk() = default;
55 void calcRelocations();
Sam Clegg5fa274b2018-01-10 01:13:34 +000056 virtual const uint8_t *getData() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000057 virtual uint32_t getInputSectionOffset() const = 0;
58
Sam Clegg5fa274b2018-01-10 01:13:34 +000059 std::vector<WasmRelocation> Relocations;
Sam Cleggd96d9352018-01-10 19:22:42 +000060 int32_t OutputOffset = 0;
Sam Clegg50686852018-01-12 18:35:13 +000061 const ObjFile *File;
Sam Clegg5fa274b2018-01-10 01:13:34 +000062};
63
64// Represents a WebAssembly data segment which can be included as part of
65// an output data segments. Note that in WebAssembly, unlike ELF and other
66// formats, used the term "data segment" to refer to the continous regions of
67// memory that make on the data section. See:
68// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
69//
70// For example, by default, clang will produce a separate data section for
71// each global variable.
72class InputSegment : public InputChunk {
73public:
Sam Clegg50686852018-01-12 18:35:13 +000074 InputSegment(const WasmSegment &Seg, const ObjFile *F)
Sam Clegg5fa274b2018-01-10 01:13:34 +000075 : InputChunk(F), Segment(Seg) {}
76
77 // Translate an offset in the input segment to an offset in the output
78 // segment.
79 uint32_t translateVA(uint32_t Address) const;
80
81 const OutputSegment *getOutputSegment() const { return OutputSeg; }
82
83 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
84 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +000085 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +000086 }
87
88 const uint8_t *getData() const override {
89 return Segment.Data.Content.data();
90 }
91 uint32_t getSize() const override { return Segment.Data.Content.size(); }
Sam Clegg5fa274b2018-01-10 01:13:34 +000092 uint32_t getAlignment() const { return Segment.Data.Alignment; }
93 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
94 uint32_t endVA() const { return startVA() + getSize(); }
95 StringRef getName() const { return Segment.Data.Name; }
96
Sam Cleggd96d9352018-01-10 19:22:42 +000097 int32_t OutputSegmentOffset = 0;
98
Sam Clegg5fa274b2018-01-10 01:13:34 +000099protected:
Sam Cleggd96d9352018-01-10 19:22:42 +0000100 uint32_t getInputSectionOffset() const override {
101 return Segment.SectionOffset;
102 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000103 const WasmSegment &Segment;
104 const OutputSegment *OutputSeg = nullptr;
105};
106
107// Represents a single wasm function within and input file. These are
108// combined to create the final output CODE section.
109class InputFunction : public InputChunk {
110public:
Sam Clegg50686852018-01-12 18:35:13 +0000111 InputFunction(const WasmSignature &S, const WasmFunction *Func,
112 const ObjFile *F)
Sam Clegg408064e2018-01-12 17:56:15 +0000113 : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {}
Sam Clegg5fa274b2018-01-10 01:13:34 +0000114
Sam Clegg50686852018-01-12 18:35:13 +0000115 uint32_t getSize() const override { return Function->Size; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000116 const uint8_t *getData() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000117 return File->CodeSection->Content.data() + getInputSectionOffset();
Sam Clegg5fa274b2018-01-10 01:13:34 +0000118 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000119
120 uint32_t getOutputIndex() const { return OutputIndex.getValue(); };
121 bool hasOutputIndex() const { return OutputIndex.hasValue(); };
Sam Clegg50686852018-01-12 18:35:13 +0000122 void setOutputIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000123
124 const WasmSignature &Signature;
125
Sam Clegg408064e2018-01-12 17:56:15 +0000126 unsigned WrittenToNameSec : 1;
127
Sam Clegg5fa274b2018-01-10 01:13:34 +0000128protected:
Sam Cleggd96d9352018-01-10 19:22:42 +0000129 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000130 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000131 }
Sam Clegg50686852018-01-12 18:35:13 +0000132 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000133 llvm::Optional<uint32_t> OutputIndex;
134};
135
Sam Clegg50686852018-01-12 18:35:13 +0000136class SyntheticFunction : public InputFunction {
137public:
138 SyntheticFunction(const WasmSignature &S, StringRef Body)
139 : InputFunction(S, nullptr, nullptr), Body(Body) {}
140
141 uint32_t getSize() const override { return Body.size(); }
142 const uint8_t *getData() const override {
143 return reinterpret_cast<const uint8_t *>(Body.data());
144 }
145
146protected:
147 StringRef Body;
148};
149
Sam Clegg5fa274b2018-01-10 01:13:34 +0000150} // namespace wasm
151} // namespace lld
152
153#endif // LLD_WASM_INPUT_CHUNKS_H