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 | // |
| 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 | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 10 | #include "InputChunks.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 11 | #include "Config.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 12 | #include "OutputSegment.h" |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 13 | #include "WriterUtils.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 14 | #include "lld/Common/ErrorHandler.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 15 | #include "lld/Common/LLVM.h" |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 16 | #include "llvm/Support/LEB128.h" |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 17 | |
| 18 | #define DEBUG_TYPE "lld" |
| 19 | |
| 20 | using namespace llvm; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 21 | using namespace llvm::wasm; |
Rui Ueyama | e351c3a | 2018-02-16 20:38:15 +0000 | [diff] [blame] | 22 | using namespace llvm::support::endian; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 23 | using namespace lld; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 24 | using namespace lld::wasm; |
| 25 | |
Benjamin Kramer | 5455038 | 2018-05-15 22:01:54 +0000 | [diff] [blame] | 26 | static StringRef ReloctTypeToString(uint8_t RelocType) { |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 27 | switch (RelocType) { |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame^] | 28 | #define WASM_RELOC(NAME, REL) \ |
| 29 | case REL: \ |
| 30 | return #NAME; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 31 | #include "llvm/BinaryFormat/WasmRelocs.def" |
| 32 | #undef WASM_RELOC |
| 33 | } |
| 34 | llvm_unreachable("unknown reloc type"); |
| 35 | } |
| 36 | |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 37 | std::string lld::toString(const InputChunk *C) { |
| 38 | return (toString(C->File) + ":(" + C->getName() + ")").str(); |
| 39 | } |
| 40 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 41 | StringRef InputChunk::getComdatName() const { |
| 42 | uint32_t Index = getComdat(); |
| 43 | if (Index == UINT32_MAX) |
| 44 | return StringRef(); |
| 45 | return File->getWasmObj()->linkingData().Comdats[Index]; |
| 46 | } |
| 47 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 48 | void InputChunk::verifyRelocTargets() const { |
| 49 | for (const WasmRelocation &Rel : Relocations) { |
| 50 | uint32_t ExistingValue; |
| 51 | unsigned BytesRead = 0; |
| 52 | uint32_t Offset = Rel.Offset - getInputSectionOffset(); |
| 53 | const uint8_t *Loc = data().data() + Offset; |
| 54 | switch (Rel.Type) { |
| 55 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 56 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 57 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 58 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 59 | ExistingValue = decodeULEB128(Loc, &BytesRead); |
| 60 | break; |
| 61 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 62 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 63 | ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead)); |
| 64 | break; |
| 65 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 66 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 67 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 68 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
| 69 | ExistingValue = static_cast<uint32_t>(read32le(Loc)); |
| 70 | break; |
| 71 | default: |
| 72 | llvm_unreachable("unknown relocation type"); |
| 73 | } |
| 74 | |
| 75 | if (BytesRead && BytesRead != 5) |
| 76 | warn("expected LEB at relocation site be 5-byte padded"); |
| 77 | uint32_t ExpectedValue = File->calcExpectedValue(Rel); |
| 78 | if (ExpectedValue != ExistingValue) |
| 79 | warn("unexpected existing value for " + ReloctTypeToString(Rel.Type) + |
| 80 | ": existing=" + Twine(ExistingValue) + |
| 81 | " expected=" + Twine(ExpectedValue)); |
| 82 | } |
| 83 | } |
| 84 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 85 | // Copy this input chunk to an mmap'ed output file and apply relocations. |
| 86 | void InputChunk::writeTo(uint8_t *Buf) const { |
| 87 | // Copy contents |
| 88 | memcpy(Buf + OutputOffset, data().data(), data().size()); |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 89 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 90 | // Apply relocations |
| 91 | if (Relocations.empty()) |
| 92 | return; |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 93 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 94 | #ifndef NDEBUG |
| 95 | verifyRelocTargets(); |
| 96 | #endif |
| 97 | |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 98 | LLVM_DEBUG(dbgs() << "applying relocations: " << getName() |
| 99 | << " count=" << Relocations.size() << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 100 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 101 | |
| 102 | for (const WasmRelocation &Rel : Relocations) { |
| 103 | uint8_t *Loc = Buf + Rel.Offset + Off; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 104 | uint32_t Value = File->calcNewValue(Rel); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 105 | LLVM_DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type) |
| 106 | << " addend=" << Rel.Addend << " index=" << Rel.Index |
| 107 | << " value=" << Value << " offset=" << Rel.Offset |
| 108 | << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 109 | |
| 110 | switch (Rel.Type) { |
| 111 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 112 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 113 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 114 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 115 | encodeULEB128(Value, Loc, 5); |
| 116 | break; |
| 117 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 118 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 119 | encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); |
| 120 | break; |
| 121 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 122 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 123 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 124 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 125 | write32le(Loc, Value); |
| 126 | break; |
| 127 | default: |
| 128 | llvm_unreachable("unknown relocation type"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 129 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 133 | // Copy relocation entries to a given output stream. |
| 134 | // This function is used only when a user passes "-r". For a regular link, |
| 135 | // we consume relocations instead of copying them to an output file. |
| 136 | void InputChunk::writeRelocations(raw_ostream &OS) const { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 137 | if (Relocations.empty()) |
| 138 | return; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 139 | |
| 140 | int32_t Off = OutputOffset - getInputSectionOffset(); |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 141 | LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName() |
| 142 | << " offset=" << Twine(Off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 143 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 144 | for (const WasmRelocation &Rel : Relocations) { |
| 145 | writeUleb128(OS, Rel.Type, "reloc type"); |
| 146 | writeUleb128(OS, Rel.Offset + Off, "reloc offset"); |
| 147 | writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 148 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 149 | switch (Rel.Type) { |
| 150 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 151 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 152 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 153 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 154 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
| 155 | writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 156 | break; |
| 157 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 160 | |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 161 | void InputFunction::setFunctionIndex(uint32_t Index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 162 | LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() |
| 163 | << " -> " << Index << "\n"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 164 | assert(!hasFunctionIndex()); |
| 165 | FunctionIndex = Index; |
Eric Christopher | 9ea500b | 2018-01-13 00:44:45 +0000 | [diff] [blame] | 166 | } |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 167 | |
| 168 | void InputFunction::setTableIndex(uint32_t Index) { |
Nicola Zaghen | e7245b4 | 2018-05-15 13:36:20 +0000 | [diff] [blame] | 169 | LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
| 170 | << Index << "\n"); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 171 | assert(!hasTableIndex()); |
| 172 | TableIndex = Index; |
| 173 | } |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 174 | |
| 175 | // Write a relocation value without padding and return the number of bytes |
| 176 | // witten. |
| 177 | static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel, |
| 178 | uint32_t Value) { |
| 179 | switch (Rel.Type) { |
| 180 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 181 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 182 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 183 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 184 | return encodeULEB128(Value, Buf); |
| 185 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 186 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 187 | return encodeSLEB128(static_cast<int32_t>(Value), Buf); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 188 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 189 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
| 193 | static unsigned getRelocWidthPadded(const WasmRelocation &Rel) { |
| 194 | switch (Rel.Type) { |
| 195 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 196 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 197 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 198 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 199 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 200 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 201 | return 5; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 202 | default: |
Sam Clegg | f377030 | 2018-05-22 20:52:20 +0000 | [diff] [blame] | 203 | llvm_unreachable("unexpected relocation type"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
| 207 | static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) { |
| 208 | uint8_t Buf[5]; |
| 209 | return writeCompressedReloc(Buf, Rel, Value); |
| 210 | } |
| 211 | |
| 212 | // Relocations of type LEB and SLEB in the code section are padded to 5 bytes |
| 213 | // so that a fast linker can blindly overwrite them without needing to worry |
| 214 | // about the number of bytes needed to encode the values. |
| 215 | // However, for optimal output the code section can be compressed to remove |
| 216 | // the padding then outputting non-relocatable files. |
| 217 | // In this case we need to perform a size calculation based on the value at each |
| 218 | // relocation. At best we end up saving 4 bytes for each relocation entry. |
| 219 | // |
| 220 | // This function only computes the final output size. It must be called |
| 221 | // before getSize() is used to calculate of layout of the code section. |
| 222 | void InputFunction::calculateSize() { |
| 223 | if (!File || !Config->CompressRelocTargets) |
| 224 | return; |
| 225 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 226 | LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 227 | |
| 228 | const uint8_t *SecStart = File->CodeSection->Content.data(); |
| 229 | const uint8_t *FuncStart = SecStart + getInputSectionOffset(); |
| 230 | uint32_t FunctionSizeLength; |
| 231 | decodeULEB128(FuncStart, &FunctionSizeLength); |
| 232 | |
| 233 | uint32_t Start = getInputSectionOffset(); |
| 234 | uint32_t End = Start + Function->Size; |
| 235 | |
| 236 | uint32_t LastRelocEnd = Start + FunctionSizeLength; |
Sam Clegg | 47078f5 | 2018-08-22 17:50:51 +0000 | [diff] [blame] | 237 | for (const WasmRelocation &Rel : Relocations) { |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 238 | LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 239 | CompressedFuncSize += Rel.Offset - LastRelocEnd; |
| 240 | CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel)); |
| 241 | LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel); |
| 242 | } |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 243 | LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 244 | CompressedFuncSize += End - LastRelocEnd; |
| 245 | |
| 246 | // Now we know how long the resulting function is we can add the encoding |
| 247 | // of its length |
| 248 | uint8_t Buf[5]; |
| 249 | CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf); |
| 250 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 251 | LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n"); |
| 252 | LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Override the default writeTo method so that we can (optionally) write the |
| 256 | // compressed version of the function. |
| 257 | void InputFunction::writeTo(uint8_t *Buf) const { |
| 258 | if (!File || !Config->CompressRelocTargets) |
| 259 | return InputChunk::writeTo(Buf); |
| 260 | |
| 261 | Buf += OutputOffset; |
Heejin Ahn | 4821ebf | 2018-08-29 21:03:16 +0000 | [diff] [blame^] | 262 | uint8_t *Orig = Buf; |
| 263 | (void)Orig; |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 264 | |
| 265 | const uint8_t *SecStart = File->CodeSection->Content.data(); |
| 266 | const uint8_t *FuncStart = SecStart + getInputSectionOffset(); |
| 267 | const uint8_t *End = FuncStart + Function->Size; |
| 268 | uint32_t Count; |
Sam Clegg | 65a9128 | 2018-05-22 17:06:55 +0000 | [diff] [blame] | 269 | decodeULEB128(FuncStart, &Count); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 270 | FuncStart += Count; |
| 271 | |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 272 | LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 273 | Buf += encodeULEB128(CompressedFuncSize, Buf); |
| 274 | const uint8_t *LastRelocEnd = FuncStart; |
| 275 | for (const WasmRelocation &Rel : Relocations) { |
| 276 | unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd; |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 277 | LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 278 | memcpy(Buf, LastRelocEnd, ChunkSize); |
| 279 | Buf += ChunkSize; |
| 280 | Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel)); |
| 281 | LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel); |
| 282 | } |
| 283 | |
| 284 | unsigned ChunkSize = End - LastRelocEnd; |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 285 | LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 286 | memcpy(Buf, LastRelocEnd, ChunkSize); |
Nicola Zaghen | 5c4fb45 | 2018-05-23 14:03:01 +0000 | [diff] [blame] | 287 | LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n"); |
Sam Clegg | fb983cd | 2018-05-18 23:28:05 +0000 | [diff] [blame] | 288 | } |