Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 23 | using llvm::object::WasmSegment; |
| 24 | using llvm::wasm::WasmFunction; |
| 25 | using llvm::wasm::WasmRelocation; |
| 26 | using llvm::wasm::WasmSignature; |
| 27 | using llvm::object::WasmSection; |
| 28 | |
| 29 | namespace lld { |
| 30 | namespace wasm { |
| 31 | |
| 32 | class ObjFile; |
| 33 | class OutputSegment; |
| 34 | |
| 35 | class InputChunk { |
| 36 | public: |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 37 | virtual uint32_t getSize() const = 0; |
| 38 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 39 | void copyRelocations(const WasmSection &Section); |
| 40 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 41 | 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 Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame^] | 50 | virtual StringRef getComdat() const = 0; |
| 51 | |
| 52 | bool Discarded = false; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 53 | std::vector<OutputRelocation> OutRelocations; |
| 54 | |
| 55 | protected: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 56 | InputChunk(const ObjFile *F) : File(F) {} |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 57 | virtual ~InputChunk() = default; |
| 58 | void calcRelocations(); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 59 | virtual const uint8_t *getData() const = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 60 | virtual uint32_t getInputSectionOffset() const = 0; |
| 61 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 62 | std::vector<WasmRelocation> Relocations; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 63 | int32_t OutputOffset = 0; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 64 | const ObjFile *File; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 65 | }; |
| 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. |
| 75 | class InputSegment : public InputChunk { |
| 76 | public: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 77 | InputSegment(const WasmSegment &Seg, const ObjFile *F) |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 78 | : 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 Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 88 | OutputSegmentOffset = Offset; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 89 | } |
| 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 Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 95 | 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 Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame^] | 99 | StringRef getComdat() const override { return Segment.Data.Comdat; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 100 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 101 | int32_t OutputSegmentOffset = 0; |
| 102 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 103 | protected: |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 104 | uint32_t getInputSectionOffset() const override { |
| 105 | return Segment.SectionOffset; |
| 106 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame^] | 107 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 108 | 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. |
| 114 | class InputFunction : public InputChunk { |
| 115 | public: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 116 | InputFunction(const WasmSignature &S, const WasmFunction *Func, |
| 117 | const ObjFile *F) |
Sam Clegg | 408064e | 2018-01-12 17:56:15 +0000 | [diff] [blame] | 118 | : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {} |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 119 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 120 | uint32_t getSize() const override { return Function->Size; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 121 | const uint8_t *getData() const override { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 122 | return File->CodeSection->Content.data() + getInputSectionOffset(); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 123 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame^] | 124 | StringRef getComdat() const override { return Function->Comdat; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 125 | uint32_t getOutputIndex() const { return OutputIndex.getValue(); }; |
| 126 | bool hasOutputIndex() const { return OutputIndex.hasValue(); }; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 127 | void setOutputIndex(uint32_t Index); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 128 | |
| 129 | const WasmSignature &Signature; |
| 130 | |
Sam Clegg | 408064e | 2018-01-12 17:56:15 +0000 | [diff] [blame] | 131 | unsigned WrittenToNameSec : 1; |
| 132 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 133 | protected: |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 134 | uint32_t getInputSectionOffset() const override { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 135 | return Function->CodeSectionOffset; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 136 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 137 | const WasmFunction *Function; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 138 | llvm::Optional<uint32_t> OutputIndex; |
| 139 | }; |
| 140 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 141 | class SyntheticFunction : public InputFunction { |
| 142 | public: |
| 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 | |
| 151 | protected: |
| 152 | StringRef Body; |
| 153 | }; |
| 154 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 155 | } // namespace wasm |
| 156 | } // namespace lld |
| 157 | |
| 158 | #endif // LLD_WASM_INPUT_CHUNKS_H |