blob: 0ddf5a923a61d5393b81f63e25f26ca093c1ae07 [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:
53 InputChunk(const ObjFile &F) : File(F) {}
54 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 Clegg5fa274b2018-01-10 01:13:34 +000061 const ObjFile &File;
62};
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:
74 InputSegment(const WasmSegment &Seg, const ObjFile &F)
75 : 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:
111 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
115 uint32_t getSize() const override { return Function.Size; }
116 const uint8_t *getData() const override {
Sam Cleggd96d9352018-01-10 19:22:42 +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(); };
122 void setOutputIndex(uint32_t Index) {
123 assert(!hasOutputIndex());
124 OutputIndex = Index;
125 };
126
127 const WasmSignature &Signature;
128
Sam Clegg408064e2018-01-12 17:56:15 +0000129 unsigned WrittenToNameSec : 1;
130
Sam Clegg5fa274b2018-01-10 01:13:34 +0000131protected:
Sam Cleggd96d9352018-01-10 19:22:42 +0000132 uint32_t getInputSectionOffset() const override {
133 return Function.CodeSectionOffset;
134 }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000135 const WasmFunction &Function;
136 llvm::Optional<uint32_t> OutputIndex;
137};
138
139} // namespace wasm
140} // namespace lld
141
142#endif // LLD_WASM_INPUT_CHUNKS_H