Sam Clegg | f61910d | 2018-01-12 22:18:22 +0000 | [diff] [blame] | 1 | //===- InputChunks.cpp ----------------------------------------------------===// |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 9 | #include "InputChunks.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 10 | #include "Config.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 11 | #include "OutputSegment.h" |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 12 | #include "WriterUtils.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 13 | #include "lld/Common/ErrorHandler.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 14 | #include "lld/Common/LLVM.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 15 | #include "llvm/Support/LEB128.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 16 | |
| 17 | #define DEBUG_TYPE "lld" |
| 18 | |
| 19 | using namespace llvm; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 20 | using namespace llvm::wasm; |
Rui Ueyama | e351c3a | 2018-02-16 20:38:15 +0000 | [diff] [blame] | 21 | using namespace llvm::support::endian; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 22 | using namespace lld; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 23 | using namespace lld::wasm; |
| 24 | |
Sam Clegg | e3a845e | 2019-03-29 22:56:39 +0000 | [diff] [blame] | 25 | StringRef lld::relocTypeToString(uint8_t RelocType) { |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 26 | switch (RelocType) { |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 27 | #define WASM_RELOC(NAME, REL) \ |
| 28 | case REL: \ |
| 29 | return #NAME; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 30 | #include "llvm/BinaryFormat/WasmRelocs.def" |
| 31 | #undef WASM_RELOC |
| 32 | } |
| 33 | llvm_unreachable("unknown reloc type"); |
| 34 | } |
| 35 | |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 36 | std::string lld::toString(const InputChunk *C) { |
| 37 | return (toString(C->File) + ":(" + C->getName() + ")").str(); |
| 38 | } |
| 39 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 40 | StringRef InputChunk::getComdatName() const { |
| 41 | uint32_t Index = getComdat(); |
| 42 | if (Index == UINT32_MAX) |
| 43 | return StringRef(); |
| 44 | return File->getWasmObj()->linkingData().Comdats[Index]; |
| 45 | } |
| 46 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 47 | void InputChunk::verifyRelocTargets() const { |
| 48 | for (const WasmRelocation &Rel : Relocations) { |
| 49 | uint32_t ExistingValue; |
| 50 | unsigned BytesRead = 0; |
| 51 | uint32_t Offset = Rel.Offset - getInputSectionOffset(); |
| 52 | const uint8_t *Loc = data().data() + Offset; |
| 53 | switch (Rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 54 | case R_WASM_TYPE_INDEX_LEB: |
| 55 | case R_WASM_FUNCTION_INDEX_LEB: |
| 56 | case R_WASM_GLOBAL_INDEX_LEB: |
| 57 | case R_WASM_EVENT_INDEX_LEB: |
| 58 | case R_WASM_MEMORY_ADDR_LEB: |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 59 | ExistingValue = decodeULEB128(Loc, &BytesRead); |
| 60 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 61 | case R_WASM_TABLE_INDEX_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 62 | case R_WASM_TABLE_INDEX_REL_SLEB: |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 63 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 64 | case R_WASM_MEMORY_ADDR_REL_SLEB: |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 65 | ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead)); |
| 66 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 67 | case R_WASM_TABLE_INDEX_I32: |
| 68 | case R_WASM_MEMORY_ADDR_I32: |
| 69 | case R_WASM_FUNCTION_OFFSET_I32: |
| 70 | case R_WASM_SECTION_OFFSET_I32: |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 71 | ExistingValue = static_cast<uint32_t>(read32le(Loc)); |
| 72 | break; |
| 73 | default: |
| 74 | llvm_unreachable("unknown relocation type"); |
| 75 | } |
| 76 | |
| 77 | if (BytesRead && BytesRead != 5) |
| 78 | warn("expected LEB at relocation site be 5-byte padded"); |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 79 | |
| 80 | if (Rel.Type != R_WASM_GLOBAL_INDEX_LEB) { |
| 81 | uint32_t ExpectedValue = File->calcExpectedValue(Rel); |
| 82 | if (ExpectedValue != ExistingValue) |
Sam Clegg | e3a845e | 2019-03-29 22:56:39 +0000 | [diff] [blame] | 83 | warn("unexpected existing value for " + relocTypeToString(Rel.Type) + |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 84 | ": existing=" + Twine(ExistingValue) + |
| 85 | " expected=" + Twine(ExpectedValue)); |
| 86 | } |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 90 | // Copy this input chunk to an mmap'ed output file and apply relocations. |
| 91 | void InputChunk::writeTo(uint8_t *Buf) const { |
| 92 | // Copy contents |
| 93 | memcpy(Buf + OutputOffset, data().data(), data().size()); |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 94 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 95 | // Apply relocations |
| 96 | if (Relocations.empty()) |
| 97 | return; |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 98 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 99 | #ifndef NDEBUG |
| 100 | verifyRelocTargets(); |
| 101 | #endif |
| 102 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 103 | LLVM_DEBUG(dbgs() << "applying relocations: " << getName() |
| 104 | << " count=" << Relocations.size() << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 105 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 106 | |
| 107 | for (const WasmRelocation &Rel : Relocations) { |
| 108 | uint8_t *Loc = Buf + Rel.Offset + Off; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 109 | uint32_t Value = File->calcNewValue(Rel); |
Sam Clegg | e3a845e | 2019-03-29 22:56:39 +0000 | [diff] [blame] | 110 | LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(Rel.Type) |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 111 | << " addend=" << Rel.Addend << " index=" << Rel.Index |
| 112 | << " value=" << Value << " offset=" << Rel.Offset |
| 113 | << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 114 | |
| 115 | switch (Rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 116 | case R_WASM_TYPE_INDEX_LEB: |
| 117 | case R_WASM_FUNCTION_INDEX_LEB: |
| 118 | case R_WASM_GLOBAL_INDEX_LEB: |
| 119 | case R_WASM_EVENT_INDEX_LEB: |
| 120 | case R_WASM_MEMORY_ADDR_LEB: |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 121 | encodeULEB128(Value, Loc, 5); |
| 122 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 123 | case R_WASM_TABLE_INDEX_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 124 | case R_WASM_TABLE_INDEX_REL_SLEB: |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 125 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 126 | case R_WASM_MEMORY_ADDR_REL_SLEB: |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 127 | encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); |
| 128 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 129 | case R_WASM_TABLE_INDEX_I32: |
| 130 | case R_WASM_MEMORY_ADDR_I32: |
| 131 | case R_WASM_FUNCTION_OFFSET_I32: |
| 132 | case R_WASM_SECTION_OFFSET_I32: |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 133 | write32le(Loc, Value); |
| 134 | break; |
| 135 | default: |
| 136 | llvm_unreachable("unknown relocation type"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 137 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 141 | // Copy relocation entries to a given output stream. |
| 142 | // This function is used only when a user passes "-r". For a regular link, |
| 143 | // we consume relocations instead of copying them to an output file. |
| 144 | void InputChunk::writeRelocations(raw_ostream &OS) const { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 145 | if (Relocations.empty()) |
| 146 | return; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 147 | |
| 148 | int32_t Off = OutputOffset - getInputSectionOffset(); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 149 | LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName() |
| 150 | << " offset=" << Twine(Off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 151 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 152 | for (const WasmRelocation &Rel : Relocations) { |
| 153 | writeUleb128(OS, Rel.Type, "reloc type"); |
| 154 | writeUleb128(OS, Rel.Offset + Off, "reloc offset"); |
| 155 | writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 156 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 157 | switch (Rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 158 | case R_WASM_MEMORY_ADDR_LEB: |
| 159 | case R_WASM_MEMORY_ADDR_SLEB: |
| 160 | case R_WASM_MEMORY_ADDR_I32: |
| 161 | case R_WASM_FUNCTION_OFFSET_I32: |
| 162 | case R_WASM_SECTION_OFFSET_I32: |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 163 | writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 164 | break; |
| 165 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 166 | } |
| 167 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 168 | |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 169 | void InputFunction::setFunctionIndex(uint32_t Index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 170 | LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() |
| 171 | << " -> " << Index << "\n"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 172 | assert(!hasFunctionIndex()); |
| 173 | FunctionIndex = Index; |
Eric Christopher | 9ea500b | 2018-01-13 00:44:45 +0000 | [diff] [blame] | 174 | } |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 175 | |
| 176 | void InputFunction::setTableIndex(uint32_t Index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 177 | LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
| 178 | << Index << "\n"); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 179 | assert(!hasTableIndex()); |
| 180 | TableIndex = Index; |
| 181 | } |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 182 | |
| 183 | // Write a relocation value without padding and return the number of bytes |
| 184 | // witten. |
| 185 | static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel, |
| 186 | uint32_t Value) { |
| 187 | switch (Rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 188 | case R_WASM_TYPE_INDEX_LEB: |
| 189 | case R_WASM_FUNCTION_INDEX_LEB: |
| 190 | case R_WASM_GLOBAL_INDEX_LEB: |
| 191 | case R_WASM_EVENT_INDEX_LEB: |
| 192 | case R_WASM_MEMORY_ADDR_LEB: |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 193 | return encodeULEB128(Value, Buf); |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 194 | case R_WASM_TABLE_INDEX_SLEB: |
| 195 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 196 | return encodeSLEB128(static_cast<int32_t>(Value), Buf); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 197 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 198 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
| 202 | static unsigned getRelocWidthPadded(const WasmRelocation &Rel) { |
| 203 | switch (Rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 204 | case R_WASM_TYPE_INDEX_LEB: |
| 205 | case R_WASM_FUNCTION_INDEX_LEB: |
| 206 | case R_WASM_GLOBAL_INDEX_LEB: |
| 207 | case R_WASM_EVENT_INDEX_LEB: |
| 208 | case R_WASM_MEMORY_ADDR_LEB: |
| 209 | case R_WASM_TABLE_INDEX_SLEB: |
| 210 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 211 | return 5; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 212 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 213 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 214 | } |
| 215 | } |
| 216 | |
| 217 | static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) { |
| 218 | uint8_t Buf[5]; |
| 219 | return writeCompressedReloc(Buf, Rel, Value); |
| 220 | } |
| 221 | |
| 222 | // Relocations of type LEB and SLEB in the code section are padded to 5 bytes |
| 223 | // so that a fast linker can blindly overwrite them without needing to worry |
| 224 | // about the number of bytes needed to encode the values. |
| 225 | // However, for optimal output the code section can be compressed to remove |
| 226 | // the padding then outputting non-relocatable files. |
| 227 | // In this case we need to perform a size calculation based on the value at each |
| 228 | // relocation. At best we end up saving 4 bytes for each relocation entry. |
| 229 | // |
| 230 | // This function only computes the final output size. It must be called |
| 231 | // before getSize() is used to calculate of layout of the code section. |
| 232 | void InputFunction::calculateSize() { |
Sam Clegg | 4aad12c | 2018-09-27 00:46:54 +0000 | [diff] [blame] | 233 | if (!File || !Config->CompressRelocations) |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 234 | return; |
| 235 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 236 | LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 237 | |
| 238 | const uint8_t *SecStart = File->CodeSection->Content.data(); |
| 239 | const uint8_t *FuncStart = SecStart + getInputSectionOffset(); |
| 240 | uint32_t FunctionSizeLength; |
| 241 | decodeULEB128(FuncStart, &FunctionSizeLength); |
| 242 | |
| 243 | uint32_t Start = getInputSectionOffset(); |
| 244 | uint32_t End = Start + Function->Size; |
| 245 | |
| 246 | uint32_t LastRelocEnd = Start + FunctionSizeLength; |
Sam Clegg | 47078f5 | 2018-08-22 17:50:51 +0000 | [diff] [blame] | 247 | for (const WasmRelocation &Rel : Relocations) { |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 248 | LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 249 | CompressedFuncSize += Rel.Offset - LastRelocEnd; |
| 250 | CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel)); |
| 251 | LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel); |
| 252 | } |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 253 | LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 254 | CompressedFuncSize += End - LastRelocEnd; |
| 255 | |
| 256 | // Now we know how long the resulting function is we can add the encoding |
| 257 | // of its length |
| 258 | uint8_t Buf[5]; |
| 259 | CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf); |
| 260 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 261 | LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n"); |
| 262 | LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | // Override the default writeTo method so that we can (optionally) write the |
| 266 | // compressed version of the function. |
| 267 | void InputFunction::writeTo(uint8_t *Buf) const { |
Sam Clegg | 4aad12c | 2018-09-27 00:46:54 +0000 | [diff] [blame] | 268 | if (!File || !Config->CompressRelocations) |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 269 | return InputChunk::writeTo(Buf); |
| 270 | |
| 271 | Buf += OutputOffset; |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 272 | uint8_t *Orig = Buf; |
| 273 | (void)Orig; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 274 | |
| 275 | const uint8_t *SecStart = File->CodeSection->Content.data(); |
| 276 | const uint8_t *FuncStart = SecStart + getInputSectionOffset(); |
| 277 | const uint8_t *End = FuncStart + Function->Size; |
| 278 | uint32_t Count; |
Sam Clegg | 65a9128 | 2018-05-22 17:06:55 +0000 | [diff] [blame] | 279 | decodeULEB128(FuncStart, &Count); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 280 | FuncStart += Count; |
| 281 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 282 | LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 283 | Buf += encodeULEB128(CompressedFuncSize, Buf); |
| 284 | const uint8_t *LastRelocEnd = FuncStart; |
| 285 | for (const WasmRelocation &Rel : Relocations) { |
| 286 | unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd; |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 287 | LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 288 | memcpy(Buf, LastRelocEnd, ChunkSize); |
| 289 | Buf += ChunkSize; |
| 290 | Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel)); |
| 291 | LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel); |
| 292 | } |
| 293 | |
| 294 | unsigned ChunkSize = End - LastRelocEnd; |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 295 | LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 296 | memcpy(Buf, LastRelocEnd, ChunkSize); |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 297 | LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 298 | } |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 299 | |
| 300 | // Generate code to apply relocations to the data section at runtime. |
| 301 | // This is only called when generating shared libaries (PIC) where address are |
| 302 | // not known at static link time. |
| 303 | void InputSegment::generateRelocationCode(raw_ostream &OS) const { |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 304 | LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName() |
| 305 | << " count=" << Relocations.size() << "\n"); |
| 306 | |
| 307 | // TODO(sbc): Encode the relocations in the data section and write a loop |
| 308 | // here to apply them. |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 309 | uint32_t SegmentVA = OutputSeg->StartVA + OutputSegmentOffset; |
| 310 | for (const WasmRelocation &Rel : Relocations) { |
| 311 | uint32_t Offset = Rel.Offset - getInputSectionOffset(); |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 312 | uint32_t OutputOffset = SegmentVA + Offset; |
| 313 | |
| 314 | LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(Rel.Type) |
| 315 | << " addend=" << Rel.Addend << " index=" << Rel.Index |
| 316 | << " output offset=" << OutputOffset << "\n"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 317 | |
| 318 | // Get __memory_base |
| 319 | writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 320 | writeUleb128(OS, WasmSym::MemoryBase->getGlobalIndex(), "memory_base"); |
| 321 | |
| 322 | // Add the offset of the relocation |
| 323 | writeU8(OS, WASM_OPCODE_I32_CONST, "I32_CONST"); |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 324 | writeSleb128(OS, OutputOffset, "offset"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 325 | writeU8(OS, WASM_OPCODE_I32_ADD, "ADD"); |
| 326 | |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 327 | Symbol *Sym = File->getSymbol(Rel); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 328 | // Now figure out what we want to store |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 329 | if (Sym->hasGOTIndex()) { |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 330 | writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 331 | writeUleb128(OS, Sym->getGOTIndex(), "global index"); |
| 332 | if (Rel.Addend) { |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 333 | writeU8(OS, WASM_OPCODE_I32_CONST, "CONST"); |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 334 | writeSleb128(OS, Rel.Addend, "addend"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 335 | writeU8(OS, WASM_OPCODE_I32_ADD, "ADD"); |
| 336 | } |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame^] | 337 | } else { |
| 338 | const GlobalSymbol* BaseSymbol = WasmSym::MemoryBase; |
| 339 | if (Rel.Type == R_WASM_TABLE_INDEX_I32) |
| 340 | BaseSymbol = WasmSym::TableBase; |
| 341 | writeU8(OS, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 342 | writeUleb128(OS, BaseSymbol->getGlobalIndex(), "base"); |
| 343 | writeU8(OS, WASM_OPCODE_I32_CONST, "CONST"); |
| 344 | writeSleb128(OS, File->calcNewValue(Rel), "offset"); |
| 345 | writeU8(OS, WASM_OPCODE_I32_ADD, "ADD"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | // Store that value at the virtual address |
| 349 | writeU8(OS, WASM_OPCODE_I32_STORE, "I32_STORE"); |
| 350 | writeUleb128(OS, 2, "align"); |
| 351 | writeUleb128(OS, 0, "offset"); |
| 352 | } |
| 353 | } |