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 | |
| 50 | std::vector<OutputRelocation> OutRelocations; |
| 51 | |
| 52 | protected: |
| 53 | InputChunk(const ObjFile &F) : File(F) {} |
| 54 | virtual ~InputChunk() = default; |
| 55 | void calcRelocations(); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 56 | virtual const uint8_t *getData() const = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 57 | virtual uint32_t getInputSectionOffset() const = 0; |
| 58 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 59 | std::vector<WasmRelocation> Relocations; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 60 | int32_t OutputOffset = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 61 | const ObjFile &File; |
| 62 | }; |
| 63 | |
| 64 | // Represents a WebAssembly data segment which can be included as part of |
| 65 | // an output data segments. Note that in WebAssembly, unlike ELF and other |
| 66 | // formats, used the term "data segment" to refer to the continous regions of |
| 67 | // memory that make on the data section. See: |
| 68 | // https://webassembly.github.io/spec/syntax/modules.html#syntax-data |
| 69 | // |
| 70 | // For example, by default, clang will produce a separate data section for |
| 71 | // each global variable. |
| 72 | class InputSegment : public InputChunk { |
| 73 | public: |
| 74 | InputSegment(const WasmSegment &Seg, const ObjFile &F) |
| 75 | : InputChunk(F), Segment(Seg) {} |
| 76 | |
| 77 | // Translate an offset in the input segment to an offset in the output |
| 78 | // segment. |
| 79 | uint32_t translateVA(uint32_t Address) const; |
| 80 | |
| 81 | const OutputSegment *getOutputSegment() const { return OutputSeg; } |
| 82 | |
| 83 | void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) { |
| 84 | OutputSeg = Segment; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 85 | OutputSegmentOffset = Offset; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | const uint8_t *getData() const override { |
| 89 | return Segment.Data.Content.data(); |
| 90 | } |
| 91 | uint32_t getSize() const override { return Segment.Data.Content.size(); } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 92 | uint32_t getAlignment() const { return Segment.Data.Alignment; } |
| 93 | uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; } |
| 94 | uint32_t endVA() const { return startVA() + getSize(); } |
| 95 | StringRef getName() const { return Segment.Data.Name; } |
| 96 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 97 | int32_t OutputSegmentOffset = 0; |
| 98 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 99 | protected: |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 100 | uint32_t getInputSectionOffset() const override { |
| 101 | return Segment.SectionOffset; |
| 102 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 103 | const WasmSegment &Segment; |
| 104 | const OutputSegment *OutputSeg = nullptr; |
| 105 | }; |
| 106 | |
| 107 | // Represents a single wasm function within and input file. These are |
| 108 | // combined to create the final output CODE section. |
| 109 | class InputFunction : public InputChunk { |
| 110 | public: |
| 111 | InputFunction(const WasmSignature &S, const WasmFunction &Func, |
| 112 | const ObjFile &F) |
Sam Clegg | 408064e | 2018-01-12 17:56:15 +0000 | [diff] [blame^] | 113 | : InputChunk(F), Signature(S), WrittenToNameSec(false), Function(Func) {} |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 114 | |
| 115 | uint32_t getSize() const override { return Function.Size; } |
| 116 | const uint8_t *getData() const override { |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 117 | return File.CodeSection->Content.data() + getInputSectionOffset(); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 118 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 119 | |
| 120 | uint32_t getOutputIndex() const { return OutputIndex.getValue(); }; |
| 121 | bool hasOutputIndex() const { return OutputIndex.hasValue(); }; |
| 122 | void setOutputIndex(uint32_t Index) { |
| 123 | assert(!hasOutputIndex()); |
| 124 | OutputIndex = Index; |
| 125 | }; |
| 126 | |
| 127 | const WasmSignature &Signature; |
| 128 | |
Sam Clegg | 408064e | 2018-01-12 17:56:15 +0000 | [diff] [blame^] | 129 | unsigned WrittenToNameSec : 1; |
| 130 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 131 | protected: |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 132 | uint32_t getInputSectionOffset() const override { |
| 133 | return Function.CodeSectionOffset; |
| 134 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 135 | const WasmFunction &Function; |
| 136 | llvm::Optional<uint32_t> OutputIndex; |
| 137 | }; |
| 138 | |
| 139 | } // namespace wasm |
| 140 | } // namespace lld |
| 141 | |
| 142 | #endif // LLD_WASM_INPUT_CHUNKS_H |