Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 1 | //===- InputChunks.cpp ----------------------------------------------------===// |
| 2 | // |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [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 |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "InputChunks.h" |
| 10 | #include "Config.h" |
| 11 | #include "OutputSegment.h" |
| 12 | #include "WriterUtils.h" |
| 13 | #include "lld/Common/ErrorHandler.h" |
| 14 | #include "lld/Common/LLVM.h" |
| 15 | #include "llvm/Support/LEB128.h" |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 16 | #include "llvm/Support/xxhash.h" |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 17 | |
| 18 | #define DEBUG_TYPE "lld" |
| 19 | |
| 20 | using namespace llvm; |
| 21 | using namespace llvm::wasm; |
| 22 | using namespace llvm::support::endian; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 23 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 24 | namespace lld { |
| 25 | StringRef relocTypeToString(uint8_t relocType) { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 26 | switch (relocType) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 27 | #define WASM_RELOC(NAME, REL) \ |
| 28 | case REL: \ |
| 29 | return #NAME; |
| 30 | #include "llvm/BinaryFormat/WasmRelocs.def" |
| 31 | #undef WASM_RELOC |
| 32 | } |
| 33 | llvm_unreachable("unknown reloc type"); |
| 34 | } |
| 35 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 36 | bool relocIs64(uint8_t relocType) { |
| 37 | switch (relocType) { |
| 38 | case R_WASM_MEMORY_ADDR_LEB64: |
| 39 | case R_WASM_MEMORY_ADDR_SLEB64: |
| 40 | case R_WASM_MEMORY_ADDR_REL_SLEB64: |
| 41 | case R_WASM_MEMORY_ADDR_I64: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 42 | case R_WASM_TABLE_INDEX_SLEB64: |
| 43 | case R_WASM_TABLE_INDEX_I64: |
| 44 | case R_WASM_FUNCTION_OFFSET_I64: |
| 45 | case R_WASM_TABLE_INDEX_REL_SLEB64: |
| 46 | case R_WASM_MEMORY_ADDR_TLS_SLEB64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 47 | return true; |
| 48 | default: |
| 49 | return false; |
| 50 | } |
| 51 | } |
| 52 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 53 | std::string toString(const wasm::InputChunk *c) { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 54 | return (toString(c->file) + ":(" + c->getName() + ")").str(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 55 | } |
| 56 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 57 | namespace wasm { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 58 | StringRef InputChunk::getComdatName() const { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 59 | uint32_t index = getComdat(); |
| 60 | if (index == UINT32_MAX) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 61 | return StringRef(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 62 | return file->getWasmObj()->linkingData().Comdats[index]; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 65 | uint32_t InputChunk::getSize() const { |
| 66 | if (const auto *ms = dyn_cast<SyntheticMergedChunk>(this)) |
| 67 | return ms->builder.getSize(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 68 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 69 | if (const auto *f = dyn_cast<InputFunction>(this)) { |
| 70 | if (config->compressRelocations && f->file) { |
| 71 | return f->getCompressedSize(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 72 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 73 | } |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 74 | |
| 75 | return data().size(); |
| 76 | } |
| 77 | |
| 78 | uint32_t InputChunk::getInputSize() const { |
| 79 | if (const auto *f = dyn_cast<InputFunction>(this)) |
| 80 | return f->function->Size; |
| 81 | return getSize(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // Copy this input chunk to an mmap'ed output file and apply relocations. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 85 | void InputChunk::writeTo(uint8_t *buf) const { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 86 | if (const auto *f = dyn_cast<InputFunction>(this)) { |
| 87 | if (file && config->compressRelocations) |
| 88 | return f->writeCompressed(buf); |
| 89 | } else if (const auto *ms = dyn_cast<SyntheticMergedChunk>(this)) { |
| 90 | ms->builder.write(buf + outSecOff); |
| 91 | // Apply relocations |
| 92 | ms->relocate(buf + outSecOff); |
| 93 | return; |
| 94 | } |
| 95 | |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 96 | // Copy contents |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 97 | memcpy(buf + outSecOff, data().data(), data().size()); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 98 | |
| 99 | // Apply relocations |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 100 | relocate(buf + outSecOff); |
| 101 | } |
| 102 | |
| 103 | void InputChunk::relocate(uint8_t *buf) const { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 104 | if (relocations.empty()) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 105 | return; |
| 106 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 107 | LLVM_DEBUG(dbgs() << "applying relocations: " << toString(this) |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 108 | << " count=" << relocations.size() << "\n"); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 109 | int32_t inputSectionOffset = getInputSectionOffset(); |
| 110 | uint64_t tombstone = getTombstone(); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 111 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 112 | for (const WasmRelocation &rel : relocations) { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 113 | uint8_t *loc = buf + rel.Offset - inputSectionOffset; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 114 | LLVM_DEBUG(dbgs() << "apply reloc: type=" << relocTypeToString(rel.Type)); |
| 115 | if (rel.Type != R_WASM_TYPE_INDEX_LEB) |
| 116 | LLVM_DEBUG(dbgs() << " sym=" << file->getSymbols()[rel.Index]->getName()); |
| 117 | LLVM_DEBUG(dbgs() << " addend=" << rel.Addend << " index=" << rel.Index |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 118 | << " offset=" << rel.Offset << "\n"); |
| 119 | auto value = file->calcNewValue(rel, tombstone, this); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 120 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 121 | switch (rel.Type) { |
| 122 | case R_WASM_TYPE_INDEX_LEB: |
| 123 | case R_WASM_FUNCTION_INDEX_LEB: |
| 124 | case R_WASM_GLOBAL_INDEX_LEB: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 125 | case R_WASM_TAG_INDEX_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 126 | case R_WASM_MEMORY_ADDR_LEB: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 127 | case R_WASM_TABLE_NUMBER_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 128 | encodeULEB128(value, loc, 5); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 129 | break; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 130 | case R_WASM_MEMORY_ADDR_LEB64: |
| 131 | encodeULEB128(value, loc, 10); |
| 132 | break; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 133 | case R_WASM_TABLE_INDEX_SLEB: |
| 134 | case R_WASM_TABLE_INDEX_REL_SLEB: |
| 135 | case R_WASM_MEMORY_ADDR_SLEB: |
| 136 | case R_WASM_MEMORY_ADDR_REL_SLEB: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 137 | case R_WASM_MEMORY_ADDR_TLS_SLEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 138 | encodeSLEB128(static_cast<int32_t>(value), loc, 5); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 139 | break; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 140 | case R_WASM_TABLE_INDEX_SLEB64: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 141 | case R_WASM_TABLE_INDEX_REL_SLEB64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 142 | case R_WASM_MEMORY_ADDR_SLEB64: |
| 143 | case R_WASM_MEMORY_ADDR_REL_SLEB64: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 144 | case R_WASM_MEMORY_ADDR_TLS_SLEB64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 145 | encodeSLEB128(static_cast<int64_t>(value), loc, 10); |
| 146 | break; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 147 | case R_WASM_TABLE_INDEX_I32: |
| 148 | case R_WASM_MEMORY_ADDR_I32: |
| 149 | case R_WASM_FUNCTION_OFFSET_I32: |
| 150 | case R_WASM_SECTION_OFFSET_I32: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 151 | case R_WASM_GLOBAL_INDEX_I32: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 152 | case R_WASM_MEMORY_ADDR_LOCREL_I32: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 153 | write32le(loc, value); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 154 | break; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 155 | case R_WASM_TABLE_INDEX_I64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 156 | case R_WASM_MEMORY_ADDR_I64: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 157 | case R_WASM_FUNCTION_OFFSET_I64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 158 | write64le(loc, value); |
| 159 | break; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 160 | default: |
| 161 | llvm_unreachable("unknown relocation type"); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Copy relocation entries to a given output stream. |
| 167 | // This function is used only when a user passes "-r". For a regular link, |
| 168 | // we consume relocations instead of copying them to an output file. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 169 | void InputChunk::writeRelocations(raw_ostream &os) const { |
| 170 | if (relocations.empty()) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 171 | return; |
| 172 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 173 | int32_t off = outSecOff - getInputSectionOffset(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 174 | LLVM_DEBUG(dbgs() << "writeRelocations: " << file->getName() |
| 175 | << " offset=" << Twine(off) << "\n"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 176 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 177 | for (const WasmRelocation &rel : relocations) { |
| 178 | writeUleb128(os, rel.Type, "reloc type"); |
| 179 | writeUleb128(os, rel.Offset + off, "reloc offset"); |
| 180 | writeUleb128(os, file->calcNewIndex(rel), "reloc index"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 181 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 182 | if (relocTypeHasAddend(rel.Type)) |
| 183 | writeSleb128(os, file->calcNewAddend(rel), "reloc addend"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 184 | } |
| 185 | } |
| 186 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 187 | uint64_t InputChunk::getTombstone() const { |
| 188 | if (const auto *s = dyn_cast<InputSection>(this)) { |
| 189 | return s->tombstoneValue; |
| 190 | } |
| 191 | |
| 192 | return 0; |
| 193 | } |
| 194 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 195 | void InputFunction::setFunctionIndex(uint32_t index) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 196 | LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 197 | << " -> " << index << "\n"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 198 | assert(!hasFunctionIndex()); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 199 | functionIndex = index; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 202 | void InputFunction::setTableIndex(uint32_t index) { |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 203 | LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 204 | << index << "\n"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 205 | assert(!hasTableIndex()); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 206 | tableIndex = index; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | // Write a relocation value without padding and return the number of bytes |
| 210 | // witten. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 211 | static unsigned writeCompressedReloc(uint8_t *buf, const WasmRelocation &rel, |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 212 | uint64_t value) { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 213 | switch (rel.Type) { |
| 214 | case R_WASM_TYPE_INDEX_LEB: |
| 215 | case R_WASM_FUNCTION_INDEX_LEB: |
| 216 | case R_WASM_GLOBAL_INDEX_LEB: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 217 | case R_WASM_TAG_INDEX_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 218 | case R_WASM_MEMORY_ADDR_LEB: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 219 | case R_WASM_MEMORY_ADDR_LEB64: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 220 | case R_WASM_TABLE_NUMBER_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 221 | return encodeULEB128(value, buf); |
| 222 | case R_WASM_TABLE_INDEX_SLEB: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 223 | case R_WASM_TABLE_INDEX_SLEB64: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 224 | case R_WASM_MEMORY_ADDR_SLEB: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 225 | case R_WASM_MEMORY_ADDR_SLEB64: |
| 226 | return encodeSLEB128(static_cast<int64_t>(value), buf); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 227 | default: |
| 228 | llvm_unreachable("unexpected relocation type"); |
| 229 | } |
| 230 | } |
| 231 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 232 | static unsigned getRelocWidthPadded(const WasmRelocation &rel) { |
| 233 | switch (rel.Type) { |
| 234 | case R_WASM_TYPE_INDEX_LEB: |
| 235 | case R_WASM_FUNCTION_INDEX_LEB: |
| 236 | case R_WASM_GLOBAL_INDEX_LEB: |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 237 | case R_WASM_TAG_INDEX_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 238 | case R_WASM_MEMORY_ADDR_LEB: |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 239 | case R_WASM_TABLE_NUMBER_LEB: |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 240 | case R_WASM_TABLE_INDEX_SLEB: |
| 241 | case R_WASM_MEMORY_ADDR_SLEB: |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 242 | return 5; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 243 | case R_WASM_TABLE_INDEX_SLEB64: |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 244 | case R_WASM_MEMORY_ADDR_LEB64: |
| 245 | case R_WASM_MEMORY_ADDR_SLEB64: |
| 246 | return 10; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 247 | default: |
| 248 | llvm_unreachable("unexpected relocation type"); |
| 249 | } |
| 250 | } |
| 251 | |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 252 | static unsigned getRelocWidth(const WasmRelocation &rel, uint64_t value) { |
| 253 | uint8_t buf[10]; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 254 | return writeCompressedReloc(buf, rel, value); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Relocations of type LEB and SLEB in the code section are padded to 5 bytes |
| 258 | // so that a fast linker can blindly overwrite them without needing to worry |
| 259 | // about the number of bytes needed to encode the values. |
| 260 | // However, for optimal output the code section can be compressed to remove |
| 261 | // the padding then outputting non-relocatable files. |
| 262 | // In this case we need to perform a size calculation based on the value at each |
| 263 | // relocation. At best we end up saving 4 bytes for each relocation entry. |
| 264 | // |
| 265 | // This function only computes the final output size. It must be called |
| 266 | // before getSize() is used to calculate of layout of the code section. |
| 267 | void InputFunction::calculateSize() { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 268 | if (!file || !config->compressRelocations) |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 269 | return; |
| 270 | |
| 271 | LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); |
| 272 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 273 | const uint8_t *secStart = file->codeSection->Content.data(); |
| 274 | const uint8_t *funcStart = secStart + getInputSectionOffset(); |
| 275 | uint32_t functionSizeLength; |
| 276 | decodeULEB128(funcStart, &functionSizeLength); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 277 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 278 | uint32_t start = getInputSectionOffset(); |
| 279 | uint32_t end = start + function->Size; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 280 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 281 | uint64_t tombstone = getTombstone(); |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 282 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 283 | uint32_t lastRelocEnd = start + functionSizeLength; |
| 284 | for (const WasmRelocation &rel : relocations) { |
| 285 | LLVM_DEBUG(dbgs() << " region: " << (rel.Offset - lastRelocEnd) << "\n"); |
| 286 | compressedFuncSize += rel.Offset - lastRelocEnd; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 287 | compressedFuncSize += |
| 288 | getRelocWidth(rel, file->calcNewValue(rel, tombstone, this)); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 289 | lastRelocEnd = rel.Offset + getRelocWidthPadded(rel); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 290 | } |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 291 | LLVM_DEBUG(dbgs() << " final region: " << (end - lastRelocEnd) << "\n"); |
| 292 | compressedFuncSize += end - lastRelocEnd; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 293 | |
| 294 | // Now we know how long the resulting function is we can add the encoding |
| 295 | // of its length |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 296 | uint8_t buf[5]; |
| 297 | compressedSize = compressedFuncSize + encodeULEB128(compressedFuncSize, buf); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 298 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 299 | LLVM_DEBUG(dbgs() << " calculateSize orig: " << function->Size << "\n"); |
| 300 | LLVM_DEBUG(dbgs() << " calculateSize new: " << compressedSize << "\n"); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | // Override the default writeTo method so that we can (optionally) write the |
| 304 | // compressed version of the function. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 305 | void InputFunction::writeCompressed(uint8_t *buf) const { |
| 306 | buf += outSecOff; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 307 | uint8_t *orig = buf; |
| 308 | (void)orig; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 309 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 310 | const uint8_t *secStart = file->codeSection->Content.data(); |
| 311 | const uint8_t *funcStart = secStart + getInputSectionOffset(); |
| 312 | const uint8_t *end = funcStart + function->Size; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 313 | uint64_t tombstone = getTombstone(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 314 | uint32_t count; |
| 315 | decodeULEB128(funcStart, &count); |
| 316 | funcStart += count; |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 317 | |
| 318 | LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 319 | buf += encodeULEB128(compressedFuncSize, buf); |
| 320 | const uint8_t *lastRelocEnd = funcStart; |
| 321 | for (const WasmRelocation &rel : relocations) { |
| 322 | unsigned chunkSize = (secStart + rel.Offset) - lastRelocEnd; |
| 323 | LLVM_DEBUG(dbgs() << " write chunk: " << chunkSize << "\n"); |
| 324 | memcpy(buf, lastRelocEnd, chunkSize); |
| 325 | buf += chunkSize; |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 326 | buf += writeCompressedReloc(buf, rel, |
| 327 | file->calcNewValue(rel, tombstone, this)); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 328 | lastRelocEnd = secStart + rel.Offset + getRelocWidthPadded(rel); |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 331 | unsigned chunkSize = end - lastRelocEnd; |
| 332 | LLVM_DEBUG(dbgs() << " write final chunk: " << chunkSize << "\n"); |
| 333 | memcpy(buf, lastRelocEnd, chunkSize); |
| 334 | LLVM_DEBUG(dbgs() << " total: " << (buf + chunkSize - orig) << "\n"); |
| 335 | } |
| 336 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 337 | uint64_t InputChunk::getChunkOffset(uint64_t offset) const { |
| 338 | if (const auto *ms = dyn_cast<MergeInputChunk>(this)) { |
| 339 | LLVM_DEBUG(dbgs() << "getChunkOffset(merged): " << getName() << "\n"); |
| 340 | LLVM_DEBUG(dbgs() << "offset: " << offset << "\n"); |
| 341 | LLVM_DEBUG(dbgs() << "parentOffset: " << ms->getParentOffset(offset) |
| 342 | << "\n"); |
| 343 | assert(ms->parent); |
| 344 | return ms->parent->getChunkOffset(ms->getParentOffset(offset)); |
| 345 | } |
| 346 | return outputSegmentOffset + offset; |
| 347 | } |
| 348 | |
| 349 | uint64_t InputChunk::getOffset(uint64_t offset) const { |
| 350 | return outSecOff + getChunkOffset(offset); |
| 351 | } |
| 352 | |
| 353 | uint64_t InputChunk::getVA(uint64_t offset) const { |
| 354 | return (outputSeg ? outputSeg->startVA : 0) + getChunkOffset(offset); |
| 355 | } |
| 356 | |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 357 | // Generate code to apply relocations to the data section at runtime. |
| 358 | // This is only called when generating shared libaries (PIC) where address are |
| 359 | // not known at static link time. |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 360 | void InputChunk::generateRelocationCode(raw_ostream &os) const { |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 361 | LLVM_DEBUG(dbgs() << "generating runtime relocations: " << getName() |
| 362 | << " count=" << relocations.size() << "\n"); |
| 363 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 364 | bool is64 = config->is64.getValueOr(false); |
| 365 | unsigned opcode_ptr_const = is64 ? WASM_OPCODE_I64_CONST |
| 366 | : WASM_OPCODE_I32_CONST; |
| 367 | unsigned opcode_ptr_add = is64 ? WASM_OPCODE_I64_ADD |
| 368 | : WASM_OPCODE_I32_ADD; |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 369 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 370 | uint64_t tombstone = getTombstone(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 371 | // TODO(sbc): Encode the relocations in the data section and write a loop |
| 372 | // here to apply them. |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 373 | for (const WasmRelocation &rel : relocations) { |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 374 | uint64_t offset = getVA(rel.Offset) - getInputSectionOffset(); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 375 | |
| 376 | LLVM_DEBUG(dbgs() << "gen reloc: type=" << relocTypeToString(rel.Type) |
| 377 | << " addend=" << rel.Addend << " index=" << rel.Index |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 378 | << " output offset=" << offset << "\n"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 379 | |
| 380 | // Get __memory_base |
| 381 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 382 | writeUleb128(os, WasmSym::memoryBase->getGlobalIndex(), "memory_base"); |
| 383 | |
| 384 | // Add the offset of the relocation |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 385 | writeU8(os, opcode_ptr_const, "CONST"); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 386 | writeSleb128(os, offset, "offset"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 387 | writeU8(os, opcode_ptr_add, "ADD"); |
| 388 | |
| 389 | bool is64 = relocIs64(rel.Type); |
| 390 | unsigned opcode_reloc_const = |
| 391 | is64 ? WASM_OPCODE_I64_CONST : WASM_OPCODE_I32_CONST; |
| 392 | unsigned opcode_reloc_add = |
| 393 | is64 ? WASM_OPCODE_I64_ADD : WASM_OPCODE_I32_ADD; |
| 394 | unsigned opcode_reloc_store = |
| 395 | is64 ? WASM_OPCODE_I64_STORE : WASM_OPCODE_I32_STORE; |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 396 | |
| 397 | Symbol *sym = file->getSymbol(rel); |
| 398 | // Now figure out what we want to store |
| 399 | if (sym->hasGOTIndex()) { |
| 400 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 401 | writeUleb128(os, sym->getGOTIndex(), "global index"); |
| 402 | if (rel.Addend) { |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 403 | writeU8(os, opcode_reloc_const, "CONST"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 404 | writeSleb128(os, rel.Addend, "addend"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 405 | writeU8(os, opcode_reloc_add, "ADD"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 406 | } |
| 407 | } else { |
| 408 | const GlobalSymbol* baseSymbol = WasmSym::memoryBase; |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 409 | if (rel.Type == R_WASM_TABLE_INDEX_I32 || |
| 410 | rel.Type == R_WASM_TABLE_INDEX_I64) |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 411 | baseSymbol = WasmSym::tableBase; |
| 412 | writeU8(os, WASM_OPCODE_GLOBAL_GET, "GLOBAL_GET"); |
| 413 | writeUleb128(os, baseSymbol->getGlobalIndex(), "base"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 414 | writeU8(os, opcode_reloc_const, "CONST"); |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 415 | writeSleb128(os, file->calcNewValue(rel, tombstone, this), "offset"); |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 416 | writeU8(os, opcode_reloc_add, "ADD"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | // Store that value at the virtual address |
Thiébaud Weksteen | e40e736 | 2020-10-28 15:03:00 +0100 | [diff] [blame] | 420 | writeU8(os, opcode_reloc_store, "I32_STORE"); |
Chih-Hung Hsieh | 0860053 | 2019-12-19 15:55:38 -0800 | [diff] [blame] | 421 | writeUleb128(os, 2, "align"); |
| 422 | writeUleb128(os, 0, "offset"); |
| 423 | } |
Inna Palant | ff3f07a | 2019-07-11 16:15:26 -0700 | [diff] [blame] | 424 | } |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 425 | |
Chris Wailes | bcf972c | 2021-10-21 11:03:28 -0700 | [diff] [blame^] | 426 | // Split WASM_SEG_FLAG_STRINGS section. Such a section is a sequence of |
| 427 | // null-terminated strings. |
| 428 | void MergeInputChunk::splitStrings(ArrayRef<uint8_t> data) { |
| 429 | LLVM_DEBUG(llvm::dbgs() << "splitStrings\n"); |
| 430 | size_t off = 0; |
| 431 | StringRef s = toStringRef(data); |
| 432 | |
| 433 | while (!s.empty()) { |
| 434 | size_t end = s.find(0); |
| 435 | if (end == StringRef::npos) |
| 436 | fatal(toString(this) + ": string is not null terminated"); |
| 437 | size_t size = end + 1; |
| 438 | |
| 439 | pieces.emplace_back(off, xxHash64(s.substr(0, size)), true); |
| 440 | s = s.substr(size); |
| 441 | off += size; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | // This function is called after we obtain a complete list of input sections |
| 446 | // that need to be linked. This is responsible to split section contents |
| 447 | // into small chunks for further processing. |
| 448 | // |
| 449 | // Note that this function is called from parallelForEach. This must be |
| 450 | // thread-safe (i.e. no memory allocation from the pools). |
| 451 | void MergeInputChunk::splitIntoPieces() { |
| 452 | assert(pieces.empty()); |
| 453 | // As of now we only support WASM_SEG_FLAG_STRINGS but in the future we |
| 454 | // could add other types of splitting (see ELF's splitIntoPieces). |
| 455 | assert(flags & WASM_SEG_FLAG_STRINGS); |
| 456 | splitStrings(data()); |
| 457 | } |
| 458 | |
| 459 | SectionPiece *MergeInputChunk::getSectionPiece(uint64_t offset) { |
| 460 | if (this->data().size() <= offset) |
| 461 | fatal(toString(this) + ": offset is outside the section"); |
| 462 | |
| 463 | // If Offset is not at beginning of a section piece, it is not in the map. |
| 464 | // In that case we need to do a binary search of the original section piece |
| 465 | // vector. |
| 466 | auto it = partition_point( |
| 467 | pieces, [=](SectionPiece p) { return p.inputOff <= offset; }); |
| 468 | return &it[-1]; |
| 469 | } |
| 470 | |
| 471 | // Returns the offset in an output section for a given input offset. |
| 472 | // Because contents of a mergeable section is not contiguous in output, |
| 473 | // it is not just an addition to a base output offset. |
| 474 | uint64_t MergeInputChunk::getParentOffset(uint64_t offset) const { |
| 475 | // If Offset is not at beginning of a section piece, it is not in the map. |
| 476 | // In that case we need to search from the original section piece vector. |
| 477 | const SectionPiece *piece = getSectionPiece(offset); |
| 478 | uint64_t addend = offset - piece->inputOff; |
| 479 | return piece->outputOff + addend; |
| 480 | } |
| 481 | |
| 482 | void SyntheticMergedChunk::finalizeContents() { |
| 483 | // Add all string pieces to the string table builder to create section |
| 484 | // contents. |
| 485 | for (MergeInputChunk *sec : chunks) |
| 486 | for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) |
| 487 | if (sec->pieces[i].live) |
| 488 | builder.add(sec->getData(i)); |
| 489 | |
| 490 | // Fix the string table content. After this, the contents will never change. |
| 491 | builder.finalize(); |
| 492 | |
| 493 | // finalize() fixed tail-optimized strings, so we can now get |
| 494 | // offsets of strings. Get an offset for each string and save it |
| 495 | // to a corresponding SectionPiece for easy access. |
| 496 | for (MergeInputChunk *sec : chunks) |
| 497 | for (size_t i = 0, e = sec->pieces.size(); i != e; ++i) |
| 498 | if (sec->pieces[i].live) |
| 499 | sec->pieces[i].outputOff = builder.getOffset(sec->getData(i)); |
| 500 | } |
| 501 | |
Chris Wailes | e3116c4 | 2021-07-13 14:40:48 -0700 | [diff] [blame] | 502 | uint64_t InputSection::getTombstoneForSection(StringRef name) { |
| 503 | // When a function is not live we need to update relocations referring to it. |
| 504 | // If they occur in DWARF debug symbols, we want to change the pc of the |
| 505 | // function to -1 to avoid overlapping with a valid range. However for the |
| 506 | // debug_ranges and debug_loc sections that would conflict with the existing |
| 507 | // meaning of -1 so we use -2. |
| 508 | // Returning 0 means there is no tombstone value for this section, and relocation |
| 509 | // will just use the addend. |
| 510 | if (!name.startswith(".debug_")) |
| 511 | return 0; |
| 512 | if (name.equals(".debug_ranges") || name.equals(".debug_loc")) |
| 513 | return UINT64_C(-2); |
| 514 | return UINT64_C(-1); |
| 515 | } |
| 516 | |
Jeff Vander Stoep | 247d86b | 2020-08-11 14:27:44 +0200 | [diff] [blame] | 517 | } // namespace wasm |
| 518 | } // namespace lld |