blob: 6f60daac51a0ef0a43078c72760043e53bee4f41 [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:
37 InputChunk(const ObjFile &F) : File(F) {}
38 virtual ~InputChunk() = default;
39 void copyRelocations(const WasmSection &Section);
40
41 virtual const uint8_t *getData() const = 0;
42 virtual uint32_t getSize() const = 0;
43 virtual uint32_t getInputSectionOffset() const = 0;
44
45 int32_t OutputOffset = 0;
46 std::vector<WasmRelocation> Relocations;
47 std::vector<OutputRelocation> OutRelocations;
48 const ObjFile &File;
49};
50
51// Represents a WebAssembly data segment which can be included as part of
52// an output data segments. Note that in WebAssembly, unlike ELF and other
53// formats, used the term "data segment" to refer to the continous regions of
54// memory that make on the data section. See:
55// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
56//
57// For example, by default, clang will produce a separate data section for
58// each global variable.
59class InputSegment : public InputChunk {
60public:
61 InputSegment(const WasmSegment &Seg, const ObjFile &F)
62 : InputChunk(F), Segment(Seg) {}
63
64 // Translate an offset in the input segment to an offset in the output
65 // segment.
66 uint32_t translateVA(uint32_t Address) const;
67
68 const OutputSegment *getOutputSegment() const { return OutputSeg; }
69
70 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
71 OutputSeg = Segment;
72 OutputOffset = Offset;
73 }
74
75 const uint8_t *getData() const override {
76 return Segment.Data.Content.data();
77 }
78 uint32_t getSize() const override { return Segment.Data.Content.size(); }
79 uint32_t getInputSectionOffset() const override {
80 return Segment.SectionOffset;
81 }
82 uint32_t getAlignment() const { return Segment.Data.Alignment; }
83 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
84 uint32_t endVA() const { return startVA() + getSize(); }
85 StringRef getName() const { return Segment.Data.Name; }
86
87protected:
88 const WasmSegment &Segment;
89 const OutputSegment *OutputSeg = nullptr;
90};
91
92// Represents a single wasm function within and input file. These are
93// combined to create the final output CODE section.
94class InputFunction : public InputChunk {
95public:
96 InputFunction(const WasmSignature &S, const WasmFunction &Func,
97 const ObjFile &F)
98 : InputChunk(F), Signature(S), Function(Func) {}
99
100 uint32_t getSize() const override { return Function.Size; }
101 const uint8_t *getData() const override {
102 return File.CodeSection->Content.data() + Function.CodeSectionOffset;
103 }
104 uint32_t getInputSectionOffset() const override {
105 return Function.CodeSectionOffset;
106 };
107
108 uint32_t getOutputIndex() const { return OutputIndex.getValue(); };
109 bool hasOutputIndex() const { return OutputIndex.hasValue(); };
110 void setOutputIndex(uint32_t Index) {
111 assert(!hasOutputIndex());
112 OutputIndex = Index;
113 };
114
115 const WasmSignature &Signature;
116
117protected:
118 const WasmFunction &Function;
119 llvm::Optional<uint32_t> OutputIndex;
120};
121
122} // namespace wasm
123} // namespace lld
124
125#endif // LLD_WASM_INPUT_CHUNKS_H