blob: d8d03582cc3f934dcefb55258f8e63e6c625d69c [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 Clegg1963d712018-01-17 20:19:04 +0000115 : InputChunk(F), Signature(S), Function(Func) {}
Sam Clegg5fa274b2018-01-10 01:13:34 +0000116
Sam Clegg1963d712018-01-17 20:19:04 +0000117 virtual StringRef getName() const { return Function->Name; }
Sam Clegge0f6fcd2018-01-12 22:25:17 +0000118 StringRef getComdat() const override { return Function->Comdat; }
Sam Cleggfc0723c2018-01-17 18:49:11 +0000119 uint32_t getOutputIndex() const { return OutputIndex.getValue(); }
120 bool hasOutputIndex() const { return OutputIndex.hasValue(); }
Sam Clegg50686852018-01-12 18:35:13 +0000121 void setOutputIndex(uint32_t Index);
Sam Clegg5fa274b2018-01-10 01:13:34 +0000122
123 const WasmSignature &Signature;
124
125protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000126 ArrayRef<uint8_t> data() const override {
Sam Cleggfc0723c2018-01-17 18:49:11 +0000127 return File->CodeSection->Content.slice(getInputSectionOffset(),
128 Function->Size);
Sam Clegg4a379c32018-01-13 00:22:00 +0000129 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000130 uint32_t getInputSectionOffset() const override {
Sam Clegg50686852018-01-12 18:35:13 +0000131 return Function->CodeSectionOffset;
Sam Cleggd96d9352018-01-10 19:22:42 +0000132 }
Sam Clegg4a379c32018-01-13 00:22:00 +0000133
Sam Clegg50686852018-01-12 18:35:13 +0000134 const WasmFunction *Function;
Sam Clegg5fa274b2018-01-10 01:13:34 +0000135 llvm::Optional<uint32_t> OutputIndex;
136};
137
Sam Clegg50686852018-01-12 18:35:13 +0000138class SyntheticFunction : public InputFunction {
139public:
Sam Clegg1963d712018-01-17 20:19:04 +0000140 SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body,
141 StringRef Name)
142 : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {}
143
144 StringRef getName() const override { return Name; }
Sam Clegg50686852018-01-12 18:35:13 +0000145
Sam Clegg50686852018-01-12 18:35:13 +0000146protected:
Sam Clegg4a379c32018-01-13 00:22:00 +0000147 ArrayRef<uint8_t> data() const override { return Body; }
148
Sam Clegg1963d712018-01-17 20:19:04 +0000149 StringRef Name;
Sam Clegg4a379c32018-01-13 00:22:00 +0000150 ArrayRef<uint8_t> Body;
Sam Clegg50686852018-01-12 18:35:13 +0000151};
152
Sam Clegg5fa274b2018-01-10 01:13:34 +0000153} // namespace wasm
154} // namespace lld
155
156#endif // LLD_WASM_INPUT_CHUNKS_H