blob: c02ebd6028e915eaa5be44739df53b8e240e1c67 [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
Sam Clegge0f6fcd2018-01-12 22:25:17 +000050 virtual StringRef getComdat() const = 0;
51
52 bool Discarded = false;
Sam Cleggd96d9352018-01-10 19:22:42 +000053 std::vector<OutputRelocation> OutRelocations;
54
55protected:
Sam Clegg50686852018-01-12 18:35:13 +000056 InputChunk(const ObjFile *F) : File(F) {}
Sam Cleggd96d9352018-01-10 19:22:42 +000057 virtual ~InputChunk() = default;
58 void calcRelocations();
Sam Clegg5fa274b2018-01-10 01:13:34 +000059 virtual const uint8_t *getData() const = 0;
Sam Clegg5fa274b2018-01-10 01:13:34 +000060 virtual uint32_t getInputSectionOffset() const = 0;
61
Sam Clegg5fa274b2018-01-10 01:13:34 +000062 std::vector<WasmRelocation> Relocations;
Sam Cleggd96d9352018-01-10 19:22:42 +000063 int32_t OutputOffset = 0;
Sam Clegg50686852018-01-12 18:35:13 +000064 const ObjFile *File;
Sam Clegg5fa274b2018-01-10 01:13:34 +000065};
66
67// Represents a WebAssembly data segment which can be included as part of
68// an output data segments. Note that in WebAssembly, unlike ELF and other
69// formats, used the term "data segment" to refer to the continous regions of
70// memory that make on the data section. See:
71// https://webassembly.github.io/spec/syntax/modules.html#syntax-data
72//
73// For example, by default, clang will produce a separate data section for
74// each global variable.
75class InputSegment : public InputChunk {
76public:
Sam Clegg50686852018-01-12 18:35:13 +000077 InputSegment(const WasmSegment &Seg, const ObjFile *F)
Sam Clegg5fa274b2018-01-10 01:13:34 +000078 : InputChunk(F), Segment(Seg) {}
79
80 // Translate an offset in the input segment to an offset in the output
81 // segment.
82 uint32_t translateVA(uint32_t Address) const;
83
84 const OutputSegment *getOutputSegment() const { return OutputSeg; }
85
86 void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) {
87 OutputSeg = Segment;
Sam Cleggd96d9352018-01-10 19:22:42 +000088 OutputSegmentOffset = Offset;
Sam Clegg5fa274b2018-01-10 01:13:34 +000089 }
90
91 const uint8_t *getData() const override {
92 return Segment.Data.Content.data();
93 }
94 uint32_t getSize() const override { return Segment.Data.Content.size(); }
Sam Clegg5fa274b2018-01-10 01:13:34 +000095 uint32_t getAlignment() const { return Segment.Data.Alignment; }
96 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
97 uint32_t endVA() const { return startVA() + getSize(); }
98 StringRef getName() const { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +000099 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000100
Sam Cleggd96d9352018-01-10 19:22:42 +0000101 int32_t OutputSegmentOffset = 0;
102
Sam Clegg5fa274b2018-01-10 01:13:34 +0000103protected:
Sam Cleggd96d9352018-01-10 19:22:42 +0000104 uint32_t getInputSectionOffset() const override {
105 return Segment.SectionOffset;
106 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000107
Sam Clegg5fa274b2018-01-10 01:13:34 +0000108 const WasmSegment &Segment;
109 const OutputSegment *OutputSeg = nullptr;
110};
111
112// Represents a single wasm function within and input file. These are
113// combined to create the final output CODE section.
114class InputFunction : public InputChunk {
115public:
Sam Clegg50686852018-01-12 18:35:13 +0000116 InputFunction(const WasmSignature &S, const WasmFunction *Func,
117 const ObjFile *F)
Sam Clegg408064e2018-01-12 17:56:15 +0000118 : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {}
Sam Clegg5fa274b2018-01-10 01:13:34 +0000119
Sam Clegg50686852018-01-12 18:35:13 +0000120 uint32_t getSize() const override { return Function->Size; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000121 const uint8_t *getData() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000122 return File->CodeSection->Content.data() + getInputSectionOffset();
Sam Clegg5fa274b2018-01-10 01:13:34 +0000123 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000124 StringRef getComdat() const override { return Function->Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +0000125 uint32_t getOutputIndex() const { return OutputIndex.getValue(); };
126 bool hasOutputIndex() const { return OutputIndex.hasValue(); };
Sam Clegg50686852018-01-12 18:35:13 +0000127 void setOutputIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000128
129 const WasmSignature &Signature;
130
Sam Clegg408064e2018-01-12 17:56:15 +0000131 unsigned WrittenToNameSec : 1;
132
Sam Clegg5fa274b2018-01-10 01:13:34 +0000133protected:
Sam Cleggd96d9352018-01-10 19:22:42 +0000134 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000135 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000136 }
Sam Clegg50686852018-01-12 18:35:13 +0000137 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000138 llvm::Optional<uint32_t> OutputIndex;
139};
140
Sam Clegg50686852018-01-12 18:35:13 +0000141class SyntheticFunction : public InputFunction {
142public:
143 SyntheticFunction(const WasmSignature &S, StringRef Body)
144 : InputFunction(S, nullptr, nullptr), Body(Body) {}
145
146 uint32_t getSize() const override { return Body.size(); }
147 const uint8_t *getData() const override {
148 return reinterpret_cast<const uint8_t *>(Body.data());
149 }
150
151protected:
152 StringRef Body;
153};
154
Sam Clegg5fa274b2018-01-10 01:13:34 +0000155} // namespace wasm
156} // namespace lld
157
158#endif // LLD_WASM_INPUT_CHUNKS_H