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 | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame^] | 37 | enum Kind { DataSegment, Function }; |
| 38 | |
| 39 | Kind kind() const { return SectionKind; }; |
| 40 | |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 41 | uint32_t getSize() const { return data().size(); } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 42 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 43 | void copyRelocations(const WasmSection &Section); |
| 44 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 45 | void writeTo(uint8_t *SectionStart) const; |
| 46 | |
| 47 | void setOutputOffset(uint32_t Offset) { |
| 48 | OutputOffset = Offset; |
| 49 | calcRelocations(); |
| 50 | } |
| 51 | |
| 52 | uint32_t getOutputOffset() const { return OutputOffset; } |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 53 | ArrayRef<WasmRelocation> getRelocations() const { return Relocations; } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 54 | |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 55 | virtual StringRef getComdat() const = 0; |
| 56 | |
| 57 | bool Discarded = false; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 58 | std::vector<OutputRelocation> OutRelocations; |
| 59 | |
| 60 | protected: |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame^] | 61 | InputChunk(const ObjFile *F, Kind K) : File(F), SectionKind(K) {} |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 62 | virtual ~InputChunk() = default; |
| 63 | void calcRelocations(); |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 64 | virtual ArrayRef<uint8_t> data() const = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 65 | virtual uint32_t getInputSectionOffset() const = 0; |
| 66 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 67 | std::vector<WasmRelocation> Relocations; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 68 | int32_t OutputOffset = 0; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 69 | const ObjFile *File; |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame^] | 70 | Kind SectionKind; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | // Represents a WebAssembly data segment which can be included as part of |
| 74 | // an output data segments. Note that in WebAssembly, unlike ELF and other |
| 75 | // formats, used the term "data segment" to refer to the continous regions of |
| 76 | // memory that make on the data section. See: |
| 77 | // https://webassembly.github.io/spec/syntax/modules.html#syntax-data |
| 78 | // |
| 79 | // For example, by default, clang will produce a separate data section for |
| 80 | // each global variable. |
| 81 | class InputSegment : public InputChunk { |
| 82 | public: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 83 | InputSegment(const WasmSegment &Seg, const ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame^] | 84 | : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {} |
| 85 | |
| 86 | static bool classof(const InputChunk *C) { return C->kind() == DataSegment; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 87 | |
| 88 | // Translate an offset in the input segment to an offset in the output |
| 89 | // segment. |
| 90 | uint32_t translateVA(uint32_t Address) const; |
| 91 | |
| 92 | const OutputSegment *getOutputSegment() const { return OutputSeg; } |
| 93 | |
| 94 | void setOutputSegment(const OutputSegment *Segment, uint32_t Offset) { |
| 95 | OutputSeg = Segment; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 96 | OutputSegmentOffset = Offset; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 99 | uint32_t getAlignment() const { return Segment.Data.Alignment; } |
| 100 | uint32_t startVA() const { return Segment.Data.Offset.Value.Int32; } |
| 101 | uint32_t endVA() const { return startVA() + getSize(); } |
| 102 | StringRef getName() const { return Segment.Data.Name; } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 103 | StringRef getComdat() const override { return Segment.Data.Comdat; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 104 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 105 | int32_t OutputSegmentOffset = 0; |
| 106 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 107 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 108 | ArrayRef<uint8_t> data() const override { return Segment.Data.Content; } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 109 | uint32_t getInputSectionOffset() const override { |
| 110 | return Segment.SectionOffset; |
| 111 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 112 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 113 | const WasmSegment &Segment; |
| 114 | const OutputSegment *OutputSeg = nullptr; |
| 115 | }; |
| 116 | |
| 117 | // Represents a single wasm function within and input file. These are |
| 118 | // combined to create the final output CODE section. |
| 119 | class InputFunction : public InputChunk { |
| 120 | public: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 121 | InputFunction(const WasmSignature &S, const WasmFunction *Func, |
| 122 | const ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame^] | 123 | : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {} |
| 124 | |
| 125 | static bool classof(const InputChunk *C) { return C->kind() == InputChunk::Function; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 126 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 127 | virtual StringRef getName() const { return Function->Name; } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 128 | StringRef getComdat() const override { return Function->Comdat; } |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 129 | uint32_t getOutputIndex() const { return OutputIndex.getValue(); } |
| 130 | bool hasOutputIndex() const { return OutputIndex.hasValue(); } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 131 | void setOutputIndex(uint32_t Index); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 132 | uint32_t getTableIndex() const { return TableIndex.getValue(); } |
| 133 | bool hasTableIndex() const { return TableIndex.hasValue(); } |
| 134 | void setTableIndex(uint32_t Index); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 135 | |
| 136 | const WasmSignature &Signature; |
| 137 | |
| 138 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 139 | ArrayRef<uint8_t> data() const override { |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 140 | return File->CodeSection->Content.slice(getInputSectionOffset(), |
| 141 | Function->Size); |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 142 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 143 | uint32_t getInputSectionOffset() const override { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 144 | return Function->CodeSectionOffset; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 145 | } |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 146 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 147 | const WasmFunction *Function; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 148 | llvm::Optional<uint32_t> OutputIndex; |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 149 | llvm::Optional<uint32_t> TableIndex; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 152 | class SyntheticFunction : public InputFunction { |
| 153 | public: |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 154 | SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body, |
| 155 | StringRef Name) |
| 156 | : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {} |
| 157 | |
| 158 | StringRef getName() const override { return Name; } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 159 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 160 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 161 | ArrayRef<uint8_t> data() const override { return Body; } |
| 162 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 163 | StringRef Name; |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 164 | ArrayRef<uint8_t> Body; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 165 | }; |
| 166 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 167 | } // namespace wasm |
| 168 | } // namespace lld |
| 169 | |
| 170 | #endif // LLD_WASM_INPUT_CHUNKS_H |