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 | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 22 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 23 | namespace lld { |
| 24 | StringRef relocTypeToString(uint8_t relocType) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 25 | switch (relocType) { |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame] | 26 | #define WASM_RELOC(NAME, REL) \ |
| 27 | case REL: \ |
| 28 | return #NAME; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 29 | #include "llvm/BinaryFormat/WasmRelocs.def" |
| 30 | #undef WASM_RELOC |
| 31 | } |
| 32 | llvm_unreachable("unknown reloc type"); |
| 33 | } |
| 34 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 35 | std::string toString(const wasm::InputChunk *c) { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 36 | return (toString(c->file) + ":(" + c->getName() + ")").str(); |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 39 | namespace wasm { |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 40 | StringRef InputChunk::getComdatName() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 41 | uint32_t index = getComdat(); |
| 42 | if (index == UINT32_MAX) |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 43 | return StringRef(); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 44 | return file->getWasmObj()->linkingData().Comdats[index]; |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 45 | } |
| 46 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 47 | void InputChunk::verifyRelocTargets() const { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 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: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 59 | existingValue = decodeULEB128(loc, &bytesRead); |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 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: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 65 | existingValue = static_cast<uint32_t>(decodeSLEB128(loc, &bytesRead)); |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 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: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 71 | existingValue = static_cast<uint32_t>(read32le(loc)); |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 72 | break; |
| 73 | default: |
| 74 | llvm_unreachable("unknown relocation type"); |
| 75 | } |
| 76 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 77 | if (bytesRead && bytesRead != 5) |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 78 | warn("expected LEB at relocation site be 5-byte padded"); |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 79 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 80 | if (rel.Type != R_WASM_GLOBAL_INDEX_LEB) { |
| 81 | uint32_t expectedValue = file->calcExpectedValue(rel); |
| 82 | if (expectedValue != existingValue) |
| 83 | warn("unexpected existing value for " + relocTypeToString(rel.Type) + |
| 84 | ": existing=" + Twine(existingValue) + |
| 85 | " expected=" + Twine(expectedValue)); |
Sam Clegg | 492f752 | 2019-03-26 19:46:15 +0000 | [diff] [blame] | 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. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 91 | void InputChunk::writeTo(uint8_t *buf) const { |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 92 | // Copy contents |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 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 |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 96 | if (relocations.empty()) |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 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 | |
Sam Clegg | cf2b872 | 2019-08-20 18:39:24 +0000 | [diff] [blame] | 103 | LLVM_DEBUG(dbgs() << "applying relocations: " << toString(this) |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 104 | << " count=" << relocations.size() << "\n"); |
| 105 | int32_t off = outputOffset - getInputSectionOffset(); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 106 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 107 | for (const WasmRelocation &rel : relocations) { |
| 108 | uint8_t *loc = buf + rel.Offset + off; |
| 109 | uint32_t value = file->calcNewValue(rel); |
| 110 | LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(rel.Type)); |
| 111 | if (rel.Type != R_WASM_TYPE_INDEX_LEB) |
| 112 | LLVM_DEBUG(dbgs() << " sym=" << file->getSymbols()[rel.Index]->getName()); |
| 113 | LLVM_DEBUG(dbgs() << " addend=" << rel.Addend << " index=" << rel.Index |
| 114 | << " value=" << value << " offset=" << rel.Offset |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 115 | << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 116 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 117 | switch (rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 118 | case R_WASM_TYPE_INDEX_LEB: |
| 119 | case R_WASM_FUNCTION_INDEX_LEB: |
| 120 | case R_WASM_GLOBAL_INDEX_LEB: |
| 121 | case R_WASM_EVENT_INDEX_LEB: |
| 122 | case R_WASM_MEMORY_ADDR_LEB: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 123 | encodeULEB128(value, loc, 5); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 124 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 125 | case R_WASM_TABLE_INDEX_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 126 | case R_WASM_TABLE_INDEX_REL_SLEB: |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 127 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | 2a7cac9 | 2019-04-04 17:43:50 +0000 | [diff] [blame] | 128 | case R_WASM_MEMORY_ADDR_REL_SLEB: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 129 | encodeSLEB128(static_cast<int32_t>(value), loc, 5); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 130 | break; |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 131 | case R_WASM_TABLE_INDEX_I32: |
| 132 | case R_WASM_MEMORY_ADDR_I32: |
| 133 | case R_WASM_FUNCTION_OFFSET_I32: |
| 134 | case R_WASM_SECTION_OFFSET_I32: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 135 | write32le(loc, value); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 136 | break; |
| 137 | default: |
| 138 | llvm_unreachable("unknown relocation type"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 139 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 143 | // Copy relocation entries to a given output stream. |
| 144 | // This function is used only when a user passes "-r". For a regular link, |
| 145 | // we consume relocations instead of copying them to an output file. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 146 | void InputChunk::writeRelocations(raw_ostream &os) const { |
| 147 | if (relocations.empty()) |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 148 | return; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 149 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 150 | int32_t off = outputOffset - getInputSectionOffset(); |
| 151 | LLVM_DEBUG(dbgs() << "writeRelocations: " << file->getName() |
| 152 | << " offset=" << Twine(off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 153 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 154 | for (const WasmRelocation &rel : relocations) { |
| 155 | writeUleb128(os, rel.Type, "reloc type"); |
| 156 | writeUleb128(os, rel.Offset + off, "reloc offset"); |
| 157 | writeUleb128(os, file->calcNewIndex(rel), "reloc index"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 158 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 159 | if (relocTypeHasAddend(rel.Type)) |
| 160 | writeSleb128(os, file->calcNewAddend(rel), "reloc addend"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 163 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 164 | void InputFunction::setFunctionIndex(uint32_t index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 165 | LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 166 | << " -> " << index << "\n"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 167 | assert(!hasFunctionIndex()); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 168 | functionIndex = index; |
Eric Christopher | 9ea500b | 2018-01-13 00:44:45 +0000 | [diff] [blame] | 169 | } |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 170 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 171 | void InputFunction::setTableIndex(uint32_t index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 172 | LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 173 | << index << "\n"); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 174 | assert(!hasTableIndex()); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 175 | tableIndex = index; |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 176 | } |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 177 | |
| 178 | // Write a relocation value without padding and return the number of bytes |
| 179 | // witten. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 180 | static unsigned writeCompressedReloc(uint8_t *buf, const WasmRelocation &rel, |
| 181 | uint32_t value) { |
| 182 | switch (rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 183 | case R_WASM_TYPE_INDEX_LEB: |
| 184 | case R_WASM_FUNCTION_INDEX_LEB: |
| 185 | case R_WASM_GLOBAL_INDEX_LEB: |
| 186 | case R_WASM_EVENT_INDEX_LEB: |
| 187 | case R_WASM_MEMORY_ADDR_LEB: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 188 | return encodeULEB128(value, buf); |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 189 | case R_WASM_TABLE_INDEX_SLEB: |
| 190 | case R_WASM_MEMORY_ADDR_SLEB: |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 191 | return encodeSLEB128(static_cast<int32_t>(value), buf); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 192 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 193 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 194 | } |
| 195 | } |
| 196 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 197 | static unsigned getRelocWidthPadded(const WasmRelocation &rel) { |
| 198 | switch (rel.Type) { |
Sam Clegg | 79e3317 | 2019-02-04 17:49:33 +0000 | [diff] [blame] | 199 | case R_WASM_TYPE_INDEX_LEB: |
| 200 | case R_WASM_FUNCTION_INDEX_LEB: |
| 201 | case R_WASM_GLOBAL_INDEX_LEB: |
| 202 | case R_WASM_EVENT_INDEX_LEB: |
| 203 | case R_WASM_MEMORY_ADDR_LEB: |
| 204 | case R_WASM_TABLE_INDEX_SLEB: |
| 205 | case R_WASM_MEMORY_ADDR_SLEB: |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 206 | return 5; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 207 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 208 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 212 | static unsigned getRelocWidth(const WasmRelocation &rel, uint32_t value) { |
| 213 | uint8_t buf[5]; |
| 214 | return writeCompressedReloc(buf, rel, value); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // Relocations of type LEB and SLEB in the code section are padded to 5 bytes |
| 218 | // so that a fast linker can blindly overwrite them without needing to worry |
| 219 | // about the number of bytes needed to encode the values. |
| 220 | // However, for optimal output the code section can be compressed to remove |
| 221 | // the padding then outputting non-relocatable files. |
| 222 | // In this case we need to perform a size calculation based on the value at each |
| 223 | // relocation. At best we end up saving 4 bytes for each relocation entry. |
| 224 | // |
| 225 | // This function only computes the final output size. It must be called |
| 226 | // before getSize() is used to calculate of layout of the code section. |
| 227 | void InputFunction::calculateSize() { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 228 | if (!file || !config->compressRelocations) |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 229 | return; |
| 230 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 231 | LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 232 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 233 | const uint8_t *secStart = file->codeSection->Content.data(); |
| 234 | const uint8_t *funcStart = secStart + getInputSectionOffset(); |
| 235 | uint32_t functionSizeLength; |
| 236 | decodeULEB128(funcStart, &functionSizeLength); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 237 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 238 | uint32_t start = getInputSectionOffset(); |
| 239 | uint32_t end = start + function->Size; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 240 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 241 | uint32_t lastRelocEnd = start + functionSizeLength; |
| 242 | for (const WasmRelocation &rel : relocations) { |
| 243 | LLVM_DEBUG(dbgs() << " region: " << (rel.Offset - lastRelocEnd) << "\n"); |
| 244 | compressedFuncSize += rel.Offset - lastRelocEnd; |
| 245 | compressedFuncSize += getRelocWidth(rel, file->calcNewValue(rel)); |
| 246 | lastRelocEnd = rel.Offset + getRelocWidthPadded(rel); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 247 | } |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 248 | LLVM_DEBUG(dbgs() << " final region: " << (end - lastRelocEnd) << "\n"); |
| 249 | compressedFuncSize += end - lastRelocEnd; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 250 | |
| 251 | // Now we know how long the resulting function is we can add the encoding |
| 252 | // of its length |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 253 | uint8_t buf[5]; |
| 254 | compressedSize = compressedFuncSize + encodeULEB128(compressedFuncSize, buf); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 255 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 256 | LLVM_DEBUG(dbgs() << " calculateSize orig: " << function->Size << "\n"); |
| 257 | LLVM_DEBUG(dbgs() << " calculateSize new: " << compressedSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Override the default writeTo method so that we can (optionally) write the |
| 261 | // compressed version of the function. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 262 | void InputFunction::writeTo(uint8_t *buf) const { |
| 263 | if (!file || !config->compressRelocations) |
| 264 | return InputChunk::writeTo(buf); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 265 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 266 | buf += outputOffset; |
| 267 | uint8_t *orig = buf; |
| 268 | (void)orig; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 269 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 270 | const uint8_t *secStart = file->codeSection->Content.data(); |
| 271 | const uint8_t *funcStart = secStart + getInputSectionOffset(); |
| 272 | const uint8_t *end = funcStart + function->Size; |
| 273 | uint32_t count; |
| 274 | decodeULEB128(funcStart, &count); |
| 275 | funcStart += count; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 276 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 277 | LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 278 | buf += encodeULEB128(compressedFuncSize, buf); |
| 279 | const uint8_t *lastRelocEnd = funcStart; |
| 280 | for (const WasmRelocation &rel : relocations) { |
| 281 | unsigned chunkSize = (secStart + rel.Offset) - lastRelocEnd; |
| 282 | LLVM_DEBUG(dbgs() << " write chunk: " << chunkSize << "\n"); |
| 283 | memcpy(buf, lastRelocEnd, chunkSize); |
| 284 | buf += chunkSize; |
| 285 | buf += writeCompressedReloc(buf, rel, file->calcNewValue(rel)); |
| 286 | lastRelocEnd = secStart + rel.Offset + getRelocWidthPadded(rel); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 289 | unsigned chunkSize = end - lastRelocEnd; |
| 290 | LLVM_DEBUG(dbgs() << " write final chunk: " << chunkSize << "\n"); |
| 291 | memcpy(buf, lastRelocEnd, chunkSize); |
| 292 | LLVM_DEBUG(dbgs() << " total: " << (buf + chunkSize - orig) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 293 | } |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 294 | |
| 295 | // Generate code to apply relocations to the data section at runtime. |
| 296 | // This is only called when generating shared libaries (PIC) where address are |
| 297 | // not known at static link time. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 298 | void InputSegment::generateRelocationCode(raw_ostream &os) const { |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame] | 299 | LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName() |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 300 | << " count=" << relocations.size() << "\n"); |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame] | 301 | |
| 302 | // TODO(sbc): Encode the relocations in the data section and write a loop |
| 303 | // here to apply them. |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 304 | uint32_t segmentVA = outputSeg->startVA + outputSegmentOffset; |
| 305 | for (const WasmRelocation &rel : relocations) { |
| 306 | uint32_t offset = rel.Offset - getInputSectionOffset(); |
| 307 | uint32_t outputOffset = segmentVA + offset; |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame] | 308 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 309 | LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type) |
| 310 | << " addend=" << rel.Addend << " index=" << rel.Index |
| 311 | << " output offset=" << outputOffset << "\n"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 312 | |
| 313 | // Get __memory_base |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 314 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 315 | writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 316 | |
| 317 | // Add the offset of the relocation |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 318 | writeU8(os, WASM_OPCODE_I32_CONST, "I32_CONST"); |
| 319 | writeSleb128(os, outputOffset, "offset"); |
| 320 | writeU8(os, WASM_OPCODE_I32_ADD, "ADD"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 321 | |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 322 | Symbol *sym = file->getSymbol(rel); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 323 | // Now figure out what we want to store |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 324 | if (sym->hasGOTIndex()) { |
| 325 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 326 | writeUleb128(os, sym->getGOTIndex(), "global index"); |
| 327 | if (rel.Addend) { |
| 328 | writeU8(os, WASM_OPCODE_I32_CONST, "CONST"); |
| 329 | writeSleb128(os, rel.Addend, "addend"); |
| 330 | writeU8(os, WASM_OPCODE_I32_ADD, "ADD"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 331 | } |
Sam Clegg | b685ddf | 2019-04-25 17:11:54 +0000 | [diff] [blame] | 332 | } else { |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 333 | const GlobalSymbol* baseSymbol = WasmSym::memoryBase; |
| 334 | if (rel.Type == R_WASM_TABLE_INDEX_I32) |
| 335 | baseSymbol = WasmSym::tableBase; |
| 336 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 337 | writeUleb128(os, baseSymbol->getGlobalIndex(), "base"); |
| 338 | writeU8(os, WASM_OPCODE_I32_CONST, "CONST"); |
| 339 | writeSleb128(os, file->calcNewValue(rel), "offset"); |
| 340 | writeU8(os, WASM_OPCODE_I32_ADD, "ADD"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | // Store that value at the virtual address |
Rui Ueyama | 136d27a | 2019-07-11 05:40:30 +0000 | [diff] [blame] | 344 | writeU8(os, WASM_OPCODE_I32_STORE, "I32_STORE"); |
| 345 | writeUleb128(os, 2, "align"); |
| 346 | writeUleb128(os, 0, "offset"); |
Sam Clegg | 09137be | 2019-04-04 18:40:51 +0000 | [diff] [blame] | 347 | } |
| 348 | } |
Fangrui Song | 33c59ab | 2019-10-10 05:25:39 +0000 | [diff] [blame] | 349 | |
| 350 | } // namespace wasm |
| 351 | } // namespace lld |