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 | |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 26 | StringRef ReloctTypeToString(uint8_t RelocType) { |
| 27 | switch (RelocType) { |
| 28 | #define WASM_RELOC(NAME, REL) case REL: return #NAME; |
| 29 | #include "llvm/BinaryFormat/WasmRelocs.def" |
| 30 | #undef WASM_RELOC |
| 31 | } |
| 32 | llvm_unreachable("unknown reloc type"); |
| 33 | } |
| 34 | |
Rui Ueyama | 81bee04 | 2018-02-19 22:29:48 +0000 | [diff] [blame] | 35 | std::string lld::toString(const InputChunk *C) { |
| 36 | return (toString(C->File) + ":(" + C->getName() + ")").str(); |
| 37 | } |
| 38 | |
Nicholas Wilson | c4d9aa1 | 2018-03-14 15:45:11 +0000 | [diff] [blame] | 39 | StringRef InputChunk::getComdatName() const { |
| 40 | uint32_t Index = getComdat(); |
| 41 | if (Index == UINT32_MAX) |
| 42 | return StringRef(); |
| 43 | return File->getWasmObj()->linkingData().Comdats[Index]; |
| 44 | } |
| 45 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 46 | void InputChunk::copyRelocations(const WasmSection &Section) { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 47 | if (Section.Relocations.empty()) |
| 48 | return; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 49 | size_t Start = getInputSectionOffset(); |
| 50 | size_t Size = getSize(); |
| 51 | for (const WasmRelocation &R : Section.Relocations) |
| 52 | if (R.Offset >= Start && R.Offset < Start + Size) |
| 53 | Relocations.push_back(R); |
| 54 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 55 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 56 | void InputChunk::verifyRelocTargets() const { |
| 57 | for (const WasmRelocation &Rel : Relocations) { |
| 58 | uint32_t ExistingValue; |
| 59 | unsigned BytesRead = 0; |
| 60 | uint32_t Offset = Rel.Offset - getInputSectionOffset(); |
| 61 | const uint8_t *Loc = data().data() + Offset; |
| 62 | switch (Rel.Type) { |
| 63 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 64 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 65 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 66 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 67 | ExistingValue = decodeULEB128(Loc, &BytesRead); |
| 68 | break; |
| 69 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 70 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 71 | ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead)); |
| 72 | break; |
| 73 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 74 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 75 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 76 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
| 77 | ExistingValue = static_cast<uint32_t>(read32le(Loc)); |
| 78 | break; |
| 79 | default: |
| 80 | llvm_unreachable("unknown relocation type"); |
| 81 | } |
| 82 | |
| 83 | if (BytesRead && BytesRead != 5) |
| 84 | warn("expected LEB at relocation site be 5-byte padded"); |
| 85 | uint32_t ExpectedValue = File->calcExpectedValue(Rel); |
| 86 | if (ExpectedValue != ExistingValue) |
| 87 | warn("unexpected existing value for " + ReloctTypeToString(Rel.Type) + |
| 88 | ": existing=" + Twine(ExistingValue) + |
| 89 | " expected=" + Twine(ExpectedValue)); |
| 90 | } |
| 91 | } |
| 92 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 93 | // Copy this input chunk to an mmap'ed output file and apply relocations. |
| 94 | void InputChunk::writeTo(uint8_t *Buf) const { |
| 95 | // Copy contents |
| 96 | memcpy(Buf + OutputOffset, data().data(), data().size()); |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 97 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 98 | // Apply relocations |
| 99 | if (Relocations.empty()) |
| 100 | return; |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 101 | |
Sam Clegg | c195314 | 2018-05-05 00:18:43 +0000 | [diff] [blame] | 102 | #ifndef NDEBUG |
| 103 | verifyRelocTargets(); |
| 104 | #endif |
| 105 | |
Sam Clegg | 37fbfc6 | 2018-03-14 00:53:34 +0000 | [diff] [blame] | 106 | DEBUG(dbgs() << "applying relocations: " << getName() |
| 107 | << " count=" << Relocations.size() << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 108 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 109 | |
| 110 | for (const WasmRelocation &Rel : Relocations) { |
| 111 | uint8_t *Loc = Buf + Rel.Offset + Off; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame] | 112 | uint32_t Value = File->calcNewValue(Rel); |
| 113 | DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type) |
| 114 | << " addend=" << Rel.Addend << " index=" << Rel.Index |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 115 | << " value=" << Value << " offset=" << Rel.Offset << "\n"); |
| 116 | |
| 117 | switch (Rel.Type) { |
| 118 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 119 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 120 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 121 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 122 | encodeULEB128(Value, Loc, 5); |
| 123 | break; |
| 124 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 125 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 126 | encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); |
| 127 | break; |
| 128 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 129 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 130 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 131 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 132 | write32le(Loc, Value); |
| 133 | break; |
| 134 | default: |
| 135 | llvm_unreachable("unknown relocation type"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 136 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 137 | } |
| 138 | } |
| 139 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 140 | // Copy relocation entries to a given output stream. |
| 141 | // This function is used only when a user passes "-r". For a regular link, |
| 142 | // we consume relocations instead of copying them to an output file. |
| 143 | void InputChunk::writeRelocations(raw_ostream &OS) const { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 144 | if (Relocations.empty()) |
| 145 | return; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 146 | |
| 147 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 148 | DEBUG(dbgs() << "writeRelocations: " << File->getName() |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 149 | << " offset=" << Twine(Off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 150 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 151 | for (const WasmRelocation &Rel : Relocations) { |
| 152 | writeUleb128(OS, Rel.Type, "reloc type"); |
| 153 | writeUleb128(OS, Rel.Offset + Off, "reloc offset"); |
| 154 | writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 155 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 156 | switch (Rel.Type) { |
| 157 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 158 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 159 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
Sam Clegg | d177ab2 | 2018-05-04 23:14:42 +0000 | [diff] [blame] | 160 | case R_WEBASSEMBLY_FUNCTION_OFFSET_I32: |
| 161 | case R_WEBASSEMBLY_SECTION_OFFSET_I32: |
| 162 | writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 163 | break; |
| 164 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 165 | } |
| 166 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 167 | |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 168 | void InputFunction::setFunctionIndex(uint32_t Index) { |
| 169 | DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> " |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 170 | << Index << "\n"); |
Sam Clegg | e3f3ccf | 2018-03-12 19:56:23 +0000 | [diff] [blame] | 171 | assert(!hasFunctionIndex()); |
| 172 | FunctionIndex = Index; |
Eric Christopher | 9ea500b | 2018-01-13 00:44:45 +0000 | [diff] [blame] | 173 | } |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 174 | |
| 175 | void InputFunction::setTableIndex(uint32_t Index) { |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 176 | DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
| 177 | << Index << "\n"); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 178 | assert(!hasTableIndex()); |
| 179 | TableIndex = Index; |
| 180 | } |