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 | // |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 10 | // An InputChunks represents an indivisible opaque region of a input wasm file. |
| 11 | // i.e. a single wasm data segment or a single wasm function. |
| 12 | // |
| 13 | // They are written directly to the mmap'd output file after which relocations |
| 14 | // are applied. Because each Chunk is independent they can be written in |
| 15 | // parallel. |
| 16 | // |
| 17 | // Chunks are also unit on which garbage collection (--gc-sections) operates. |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #ifndef LLD_WASM_INPUT_CHUNKS_H |
| 22 | #define LLD_WASM_INPUT_CHUNKS_H |
| 23 | |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 24 | #include "Config.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 25 | #include "InputFiles.h" |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 26 | #include "lld/Common/ErrorHandler.h" |
| 27 | #include "llvm/Object/Wasm.h" |
| 28 | |
| 29 | using llvm::object::WasmSegment; |
| 30 | using llvm::wasm::WasmFunction; |
| 31 | using llvm::wasm::WasmRelocation; |
| 32 | using llvm::wasm::WasmSignature; |
| 33 | using llvm::object::WasmSection; |
| 34 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 35 | namespace llvm { |
| 36 | class raw_ostream; |
| 37 | } |
| 38 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 39 | namespace lld { |
| 40 | namespace wasm { |
| 41 | |
| 42 | class ObjFile; |
| 43 | class OutputSegment; |
| 44 | |
| 45 | class InputChunk { |
| 46 | public: |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 47 | enum Kind { DataSegment, Function }; |
| 48 | |
Sam Clegg | 3dc44a6 | 2018-02-09 07:12:29 +0000 | [diff] [blame] | 49 | Kind kind() const { return SectionKind; } |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 50 | |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 51 | uint32_t getSize() const { return data().size(); } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 52 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 53 | void copyRelocations(const WasmSection &Section); |
| 54 | |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 55 | void writeTo(uint8_t *SectionStart) const; |
| 56 | |
Sam Clegg | ab604a9 | 2018-01-23 01:25:56 +0000 | [diff] [blame] | 57 | ArrayRef<WasmRelocation> getRelocations() const { return Relocations; } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 58 | |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 59 | virtual StringRef getComdat() const = 0; |
Sam Clegg | fadf518 | 2018-01-28 19:57:03 +0000 | [diff] [blame] | 60 | virtual StringRef getName() const = 0; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 61 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 62 | size_t NumRelocations() const { return Relocations.size(); } |
| 63 | void writeRelocations(llvm::raw_ostream &OS) const; |
| 64 | |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 65 | ObjFile *File; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 66 | int32_t OutputOffset = 0; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 67 | |
Sam Clegg | 447ae40 | 2018-02-13 20:29:38 +0000 | [diff] [blame] | 68 | // Signals that the section is part of the output. The garbage collector, |
| 69 | // and COMDAT handling can set a sections' Live bit. |
| 70 | // If GC is disabled, all sections start out as live by default. |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 71 | unsigned Live : 1; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 72 | |
| 73 | protected: |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 74 | InputChunk(ObjFile *F, Kind K) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 75 | : File(F), Live(!Config->GcSections), SectionKind(K) {} |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 76 | virtual ~InputChunk() = default; |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 77 | virtual ArrayRef<uint8_t> data() const = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 78 | virtual uint32_t getInputSectionOffset() const = 0; |
| 79 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 80 | std::vector<WasmRelocation> Relocations; |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 81 | Kind SectionKind; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | // Represents a WebAssembly data segment which can be included as part of |
| 85 | // an output data segments. Note that in WebAssembly, unlike ELF and other |
| 86 | // formats, used the term "data segment" to refer to the continous regions of |
| 87 | // memory that make on the data section. See: |
| 88 | // https://webassembly.github.io/spec/syntax/modules.html#syntax-data |
| 89 | // |
| 90 | // For example, by default, clang will produce a separate data section for |
| 91 | // each global variable. |
| 92 | class InputSegment : public InputChunk { |
| 93 | public: |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 94 | InputSegment(const WasmSegment &Seg, ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 95 | : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {} |
| 96 | |
| 97 | static bool classof(const InputChunk *C) { return C->kind() == DataSegment; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 98 | |
| 99 | // Translate an offset in the input segment to an offset in the output |
| 100 | // segment. |
Sam Clegg | 9310297 | 2018-02-23 05:08:53 +0000 | [diff] [blame] | 101 | uint32_t translateVA(uint32_t Offset) const; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 102 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 103 | uint32_t getAlignment() const { return Segment.Data.Alignment; } |
Sam Clegg | fadf518 | 2018-01-28 19:57:03 +0000 | [diff] [blame] | 104 | StringRef getName() const override { return Segment.Data.Name; } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 105 | StringRef getComdat() const override { return Segment.Data.Comdat; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 106 | |
Rui Ueyama | 28f3b20 | 2018-02-28 00:20:29 +0000 | [diff] [blame^] | 107 | const OutputSegment *OutputSeg = nullptr; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 108 | int32_t OutputSegmentOffset = 0; |
| 109 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 110 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 111 | ArrayRef<uint8_t> data() const override { return Segment.Data.Content; } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 112 | uint32_t getInputSectionOffset() const override { |
| 113 | return Segment.SectionOffset; |
| 114 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 115 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 116 | const WasmSegment &Segment; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | // Represents a single wasm function within and input file. These are |
| 120 | // combined to create the final output CODE section. |
| 121 | class InputFunction : public InputChunk { |
| 122 | public: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 123 | InputFunction(const WasmSignature &S, const WasmFunction *Func, |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 124 | ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 125 | : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {} |
| 126 | |
Sam Clegg | fc50c62 | 2018-01-28 19:57:02 +0000 | [diff] [blame] | 127 | static bool classof(const InputChunk *C) { |
| 128 | return C->kind() == InputChunk::Function; |
| 129 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 130 | |
Sam Clegg | fadf518 | 2018-01-28 19:57:03 +0000 | [diff] [blame] | 131 | StringRef getName() const override { return Function->Name; } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 132 | StringRef getComdat() const override { return Function->Comdat; } |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 133 | uint32_t getOutputIndex() const { return OutputIndex.getValue(); } |
| 134 | bool hasOutputIndex() const { return OutputIndex.hasValue(); } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 135 | void setOutputIndex(uint32_t Index); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 136 | uint32_t getTableIndex() const { return TableIndex.getValue(); } |
| 137 | bool hasTableIndex() const { return TableIndex.hasValue(); } |
| 138 | void setTableIndex(uint32_t Index); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 139 | |
| 140 | const WasmSignature &Signature; |
| 141 | |
| 142 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 143 | ArrayRef<uint8_t> data() const override { |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 144 | return File->CodeSection->Content.slice(getInputSectionOffset(), |
| 145 | Function->Size); |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 146 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 147 | uint32_t getInputSectionOffset() const override { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 148 | return Function->CodeSectionOffset; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 149 | } |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 150 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 151 | const WasmFunction *Function; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 152 | llvm::Optional<uint32_t> OutputIndex; |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 153 | llvm::Optional<uint32_t> TableIndex; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 156 | class SyntheticFunction : public InputFunction { |
| 157 | public: |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 158 | SyntheticFunction(const WasmSignature &S, ArrayRef<uint8_t> Body, |
| 159 | StringRef Name) |
| 160 | : InputFunction(S, nullptr, nullptr), Name(Name), Body(Body) {} |
| 161 | |
| 162 | StringRef getName() const override { return Name; } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 163 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 164 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 165 | ArrayRef<uint8_t> data() const override { return Body; } |
| 166 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 167 | StringRef Name; |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 168 | ArrayRef<uint8_t> Body; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 169 | }; |
| 170 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 171 | } // namespace wasm |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 172 | |
| 173 | std::string toString(const wasm::InputChunk *); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 174 | } // namespace lld |
| 175 | |
| 176 | #endif // LLD_WASM_INPUT_CHUNKS_H |