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 | |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 29 | using llvm::object::WasmSection; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 30 | using llvm::object::WasmSegment; |
| 31 | using llvm::wasm::WasmFunction; |
| 32 | using llvm::wasm::WasmRelocation; |
| 33 | using llvm::wasm::WasmSignature; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 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 | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 47 | enum Kind { DataSegment, Function, SyntheticFunction, Section }; |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 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 | fadf518 | 2018-01-28 19:57:03 +0000 | [diff] [blame] | 59 | virtual StringRef getName() const = 0; |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 60 | virtual StringRef getDebugName() const = 0; |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 61 | virtual uint32_t getComdat() const = 0; |
| 62 | StringRef getComdatName() const; |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 63 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 64 | size_t NumRelocations() const { return Relocations.size(); } |
| 65 | void writeRelocations(llvm::raw_ostream &OS) const; |
| 66 | |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 67 | ObjFile *File; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 68 | int32_t OutputOffset = 0; |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 69 | |
Sam Clegg | 447ae40 | 2018-02-13 20:29:38 +0000 | [diff] [blame] | 70 | // Signals that the section is part of the output. The garbage collector, |
| 71 | // and COMDAT handling can set a sections' Live bit. |
| 72 | // If GC is disabled, all sections start out as live by default. |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 73 | unsigned Live : 1; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 74 | |
| 75 | protected: |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 76 | InputChunk(ObjFile *F, Kind K) |
Sam Clegg | 0362633 | 2018-01-31 01:45:47 +0000 | [diff] [blame] | 77 | : File(F), Live(!Config->GcSections), SectionKind(K) {} |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 78 | virtual ~InputChunk() = default; |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 79 | virtual ArrayRef<uint8_t> data() const = 0; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 80 | virtual uint32_t getInputSectionOffset() const = 0; |
| 81 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 82 | // Verifies the existing data at relocation targets matches our expectations. |
| 83 | // This is performed only debug builds as an extra sanity check. |
| 84 | void verifyRelocTargets() const; |
| 85 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 86 | std::vector<WasmRelocation> Relocations; |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 87 | Kind SectionKind; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | // Represents a WebAssembly data segment which can be included as part of |
| 91 | // an output data segments. Note that in WebAssembly, unlike ELF and other |
| 92 | // formats, used the term "data segment" to refer to the continous regions of |
| 93 | // memory that make on the data section. See: |
| 94 | // https://webassembly.github.io/spec/syntax/modules.html#syntax-data |
| 95 | // |
| 96 | // For example, by default, clang will produce a separate data section for |
| 97 | // each global variable. |
| 98 | class InputSegment : public InputChunk { |
| 99 | public: |
Sam Clegg | 8f6d2de | 2018-01-31 23:48:14 +0000 | [diff] [blame] | 100 | InputSegment(const WasmSegment &Seg, ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 101 | : InputChunk(F, InputChunk::DataSegment), Segment(Seg) {} |
| 102 | |
| 103 | static bool classof(const InputChunk *C) { return C->kind() == DataSegment; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 104 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 105 | uint32_t getAlignment() const { return Segment.Data.Alignment; } |
Sam Clegg | fadf518 | 2018-01-28 19:57:03 +0000 | [diff] [blame] | 106 | StringRef getName() const override { return Segment.Data.Name; } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 107 | StringRef getDebugName() const override { return StringRef(); } |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 108 | uint32_t getComdat() const override { return Segment.Data.Comdat; } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 109 | |
Rui Ueyama | 28f3b20 | 2018-02-28 00:20:29 +0000 | [diff] [blame] | 110 | const OutputSegment *OutputSeg = nullptr; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 111 | int32_t OutputSegmentOffset = 0; |
| 112 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 113 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 114 | ArrayRef<uint8_t> data() const override { return Segment.Data.Content; } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 115 | uint32_t getInputSectionOffset() const override { |
| 116 | return Segment.SectionOffset; |
| 117 | } |
Sam Clegg | e0f6fcd | 2018-01-12 22:25:17 +0000 | [diff] [blame] | 118 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 119 | const WasmSegment &Segment; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | // Represents a single wasm function within and input file. These are |
| 123 | // combined to create the final output CODE section. |
| 124 | class InputFunction : public InputChunk { |
| 125 | public: |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 126 | InputFunction(const WasmSignature &S, const WasmFunction *Func, ObjFile *F) |
Sam Clegg | 24b3dcd | 2018-01-28 19:57:01 +0000 | [diff] [blame] | 127 | : InputChunk(F, InputChunk::Function), Signature(S), Function(Func) {} |
| 128 | |
Sam Clegg | fc50c62 | 2018-01-28 19:57:02 +0000 | [diff] [blame] | 129 | static bool classof(const InputChunk *C) { |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 130 | return C->kind() == InputChunk::Function || |
| 131 | C->kind() == InputChunk::SyntheticFunction; |
Sam Clegg | fc50c62 | 2018-01-28 19:57:02 +0000 | [diff] [blame] | 132 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 133 | |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 134 | StringRef getName() const override { return Function->SymbolName; } |
| 135 | StringRef getDebugName() const override { return Function->DebugName; } |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 136 | uint32_t getComdat() const override { return Function->Comdat; } |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 137 | uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); } |
Sam Clegg | cfeb646 | 2018-05-15 22:27:50 +0000 | [diff] [blame^] | 138 | uint32_t getFunctionCodeOffset() const { return Function->CodeOffset; } |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 139 | uint32_t getFunctionIndex() const { return FunctionIndex.getValue(); } |
| 140 | bool hasFunctionIndex() const { return FunctionIndex.hasValue(); } |
| 141 | void setFunctionIndex(uint32_t Index); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 142 | uint32_t getTableIndex() const { return TableIndex.getValue(); } |
| 143 | bool hasTableIndex() const { return TableIndex.hasValue(); } |
| 144 | void setTableIndex(uint32_t Index); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 145 | |
| 146 | const WasmSignature &Signature; |
| 147 | |
| 148 | protected: |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 149 | ArrayRef<uint8_t> data() const override { |
Sam Clegg | fc0723c | 2018-01-17 18:49:11 +0000 | [diff] [blame] | 150 | return File->CodeSection->Content.slice(getInputSectionOffset(), |
| 151 | Function->Size); |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 152 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 153 | uint32_t getInputSectionOffset() const override { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 154 | return Function->CodeSectionOffset; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 155 | } |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 156 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 157 | const WasmFunction *Function; |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 158 | llvm::Optional<uint32_t> FunctionIndex; |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 159 | llvm::Optional<uint32_t> TableIndex; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 160 | }; |
| 161 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 162 | class SyntheticFunction : public InputFunction { |
| 163 | public: |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 164 | SyntheticFunction(const WasmSignature &S, StringRef Name, |
| 165 | StringRef DebugName = {}) |
| 166 | : InputFunction(S, nullptr, nullptr), Name(Name), DebugName(DebugName) { |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 167 | SectionKind = InputChunk::SyntheticFunction; |
| 168 | } |
| 169 | |
| 170 | static bool classof(const InputChunk *C) { |
| 171 | return C->kind() == InputChunk::SyntheticFunction; |
| 172 | } |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 173 | |
| 174 | StringRef getName() const override { return Name; } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 175 | StringRef getDebugName() const override { return DebugName; } |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 176 | uint32_t getComdat() const override { return UINT32_MAX; } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 177 | |
Nicholas Wilson | ebda41f | 2018-03-09 16:43:05 +0000 | [diff] [blame] | 178 | void setBody(ArrayRef<uint8_t> Body_) { Body = Body_; } |
| 179 | |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 180 | protected: |
Nicholas Wilson | 8269f37 | 2018-03-07 10:37:50 +0000 | [diff] [blame] | 181 | ArrayRef<uint8_t> data() const override { return Body; } |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame] | 182 | |
Sam Clegg | 1963d71 | 2018-01-17 20:19:04 +0000 | [diff] [blame] | 183 | StringRef Name; |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 184 | StringRef DebugName; |
Nicholas Wilson | 8269f37 | 2018-03-07 10:37:50 +0000 | [diff] [blame] | 185 | ArrayRef<uint8_t> Body; |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 188 | // Represents a single Wasm Section within an input file. |
| 189 | class InputSection : public InputChunk { |
| 190 | public: |
Sam Clegg | 225c469 | 2018-04-12 20:31:35 +0000 | [diff] [blame] | 191 | InputSection(const WasmSection &S, ObjFile *F) |
| 192 | : InputChunk(F, InputChunk::Section), Section(S) { |
| 193 | assert(Section.Type == llvm::wasm::WASM_SEC_CUSTOM); |
| 194 | } |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 195 | |
| 196 | StringRef getName() const override { return Section.Name; } |
Nicholas Wilson | 6c7fe30 | 2018-04-20 17:09:18 +0000 | [diff] [blame] | 197 | StringRef getDebugName() const override { return StringRef(); } |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 198 | uint32_t getComdat() const override { return UINT32_MAX; } |
| 199 | |
| 200 | protected: |
Sam Clegg | 225c469 | 2018-04-12 20:31:35 +0000 | [diff] [blame] | 201 | ArrayRef<uint8_t> data() const override { return Section.Content; } |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 202 | |
| 203 | // Offset within the input section. This is only zero since this chunk |
| 204 | // type represents an entire input section, not part of one. |
| 205 | uint32_t getInputSectionOffset() const override { return 0; } |
| 206 | |
| 207 | const WasmSection &Section; |
Sam Clegg | 80ba438 | 2018-04-10 16:12:49 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 210 | } // namespace wasm |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 211 | |
| 212 | std::string toString(const wasm::InputChunk *); |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 213 | } // namespace lld |
| 214 | |
| 215 | #endif // LLD_WASM_INPUT_CHUNKS_H |