blob: d5a9e5c668aacb893fdbbdb5c17eb6d528f8a537 [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 Cleggfc0723c2018-01-17 18:49:11 +000037 uint32_t getSize() const { return data().size(); }
Sam Cleggd96d9352018-01-10 19:22:42 +000038
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 Clegg4a379c32018-01-13 00:22:00 +000059 virtual ArrayRef<uint8_t> data() 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
Sam Clegg5fa274b2018-01-10 01:13:34 +000091 uint32_t getAlignment() const { return Segment.Data.Alignment; }
92 uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; }
93 uint32_t endVA() const { return startVA() + getSize(); }
94 StringRef getName() const { return Segment.Data.Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +000095 StringRef getComdat() const override { return Segment.Data.Comdat; }
Sam Clegg5fa274b2018-01-10 01:13:34 +000096
Sam Cleggd96d9352018-01-10 19:22:42 +000097 int32_t OutputSegmentOffset = 0;
98
Sam Clegg5fa274b2018-01-10 01:13:34 +000099protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000100 ArrayRef<uint8_t> data() const override { return Segment.Data.Content; }
Sam Cleggd96d9352018-01-10 19:22:42 +0000101 uint32_t getInputSectionOffset() const override {
102 return Segment.SectionOffset;
103 }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000104
Sam Clegg5fa274b2018-01-10 01:13:34 +0000105 const WasmSegment &Segment;
106 const OutputSegment *OutputSeg = nullptr;
107};
108
109// Represents a single wasm function within and input file. These are
110// combined to create the final output CODE section.
111class InputFunction : public InputChunk {
112public:
Sam Clegg50686852018-01-12 18:35:13 +0000113 InputFunction(const WasmSignature &S, const WasmFunction *Func,
114 const ObjFile *F)
Sam Clegg408064e2018-01-12 17:56:15 +0000115 : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {}
Sam Clegg5fa274b2018-01-10 01:13:34 +0000116
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000117 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000118 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
119 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000120 void setOutputIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000121
122 const WasmSignature &Signature;
123
Sam Clegg408064e2018-01-12 17:56:15 +0000124 unsigned WrittenToNameSec : 1;
125
Sam Clegg5fa274b2018-01-10 01:13:34 +0000126protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000127 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000128 return File->CodeSection->Content.slice(getInputSectionOffset(),
129 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000130 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000131 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000132 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000133 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000134
Sam Clegg50686852018-01-12 18:35:13 +0000135 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000136 llvm::Optional<uint32_t> OutputIndex;
137};
138
Sam Clegg50686852018-01-12 18:35:13 +0000139class SyntheticFunction : public InputFunction {
140public:
Sam Clegg4a379c32018-01-13 00:22:00 +0000141 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body)
Sam Clegg50686852018-01-12 18:35:13 +0000142 : InputFunction(S, nullptr, nullptr), Body(Body) {}
143
Sam Clegg50686852018-01-12 18:35:13 +0000144protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000145 ArrayRef<uint8_t> data() const override { return Body; }
146
147 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000148};
149
Sam Clegg5fa274b2018-01-10 01:13:34 +0000150} // namespace wasm
151} // namespace lld
152
153#endif // LLD_WASM_INPUT_CHUNKS_H