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" |
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; |
| 21 | using namespace lld; |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 22 | using namespace lld::wasm; |
| 23 | |
| 24 | uint32_t InputSegment::translateVA(uint32_t Address) const { |
| 25 | assert(Address >= startVA() && Address < endVA()); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 26 | int32_t Delta = OutputSeg->StartVA + OutputSegmentOffset - startVA(); |
Sam Clegg | c94d393 | 2017-11-17 18:14:09 +0000 | [diff] [blame] | 27 | DEBUG(dbgs() << "translateVA: " << getName() << " Delta=" << Delta |
| 28 | << " Address=" << Address << "\n"); |
| 29 | return Address + Delta; |
| 30 | } |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 31 | |
| 32 | void InputChunk::copyRelocations(const WasmSection &Section) { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 33 | if (Section.Relocations.empty()) |
| 34 | return; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 35 | size_t Start = getInputSectionOffset(); |
| 36 | size_t Size = getSize(); |
| 37 | for (const WasmRelocation &R : Section.Relocations) |
| 38 | if (R.Offset >= Start && R.Offset < Start + Size) |
| 39 | Relocations.push_back(R); |
| 40 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 41 | |
| 42 | static void applyRelocation(uint8_t *Buf, const OutputRelocation &Reloc) { |
| 43 | DEBUG(dbgs() << "write reloc: type=" << Reloc.Reloc.Type |
| 44 | << " index=" << Reloc.Reloc.Index << " value=" << Reloc.Value |
| 45 | << " offset=" << Reloc.Reloc.Offset << "\n"); |
| 46 | Buf += Reloc.Reloc.Offset; |
| 47 | int64_t ExistingValue; |
| 48 | switch (Reloc.Reloc.Type) { |
| 49 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 50 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 51 | ExistingValue = decodeULEB128(Buf); |
| 52 | if (ExistingValue != Reloc.Reloc.Index) { |
| 53 | DEBUG(dbgs() << "existing value: " << decodeULEB128(Buf) << "\n"); |
| 54 | assert(decodeULEB128(Buf) == Reloc.Reloc.Index); |
| 55 | } |
| 56 | LLVM_FALLTHROUGH; |
| 57 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 58 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 59 | encodeULEB128(Reloc.Value, Buf, 5); |
| 60 | break; |
| 61 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 62 | ExistingValue = decodeSLEB128(Buf); |
| 63 | if (ExistingValue != Reloc.Reloc.Index) { |
| 64 | DEBUG(dbgs() << "existing value: " << decodeSLEB128(Buf) << "\n"); |
| 65 | assert(decodeSLEB128(Buf) == Reloc.Reloc.Index); |
| 66 | } |
| 67 | LLVM_FALLTHROUGH; |
| 68 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 69 | encodeSLEB128(static_cast<int32_t>(Reloc.Value), Buf, 5); |
| 70 | break; |
| 71 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 72 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 73 | support::endian::write32<support::little>(Buf, Reloc.Value); |
| 74 | break; |
| 75 | default: |
| 76 | llvm_unreachable("unknown relocation type"); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void applyRelocations(uint8_t *Buf, ArrayRef<OutputRelocation> Relocs) { |
| 81 | if (!Relocs.size()) |
| 82 | return; |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 83 | DEBUG(dbgs() << "applyRelocations: count=" << Relocs.size() << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 84 | for (const OutputRelocation &Reloc : Relocs) |
| 85 | applyRelocation(Buf, Reloc); |
| 86 | } |
| 87 | |
| 88 | void InputChunk::writeTo(uint8_t *SectionStart) const { |
Sam Clegg | 4a379c3 | 2018-01-13 00:22:00 +0000 | [diff] [blame^] | 89 | memcpy(SectionStart + getOutputOffset(), data().data(), data().size()); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 90 | applyRelocations(SectionStart, OutRelocations); |
| 91 | } |
| 92 | |
| 93 | // Populate OutRelocations based on the input relocations and offset within the |
| 94 | // output section. Calculates the updated index and offset for each relocation |
| 95 | // as well as the value to write out in the final binary. |
| 96 | void InputChunk::calcRelocations() { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 97 | if (Relocations.empty()) |
| 98 | return; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 99 | int32_t Off = getOutputOffset() - getInputSectionOffset(); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 100 | DEBUG(dbgs() << "calcRelocations: " << File->getName() |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 101 | << " offset=" << Twine(Off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 102 | for (const WasmRelocation &Reloc : Relocations) { |
| 103 | OutputRelocation NewReloc; |
| 104 | NewReloc.Reloc = Reloc; |
| 105 | assert(Reloc.Offset + Off > 0); |
| 106 | NewReloc.Reloc.Offset += Off; |
| 107 | DEBUG(dbgs() << "reloc: type=" << Reloc.Type << " index=" << Reloc.Index |
| 108 | << " offset=" << Reloc.Offset |
| 109 | << " newOffset=" << NewReloc.Reloc.Offset << "\n"); |
| 110 | |
| 111 | if (Config->EmitRelocs) |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 112 | NewReloc.NewIndex = File->calcNewIndex(Reloc); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 113 | |
| 114 | switch (Reloc.Type) { |
| 115 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 116 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 117 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 118 | NewReloc.Value = File->getRelocatedAddress(Reloc.Index) + Reloc.Addend; |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 119 | break; |
| 120 | default: |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 121 | NewReloc.Value = File->calcNewIndex(Reloc); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
| 125 | OutRelocations.emplace_back(NewReloc); |
| 126 | } |
| 127 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 128 | |
| 129 | void InputFunction::setOutputIndex(uint32_t Index) { |
| 130 | DEBUG(dbgs() << "InputFunction::setOutputIndex: " << Index << "\n"); |
| 131 | assert(!hasOutputIndex()); |
| 132 | OutputIndex = Index; |
| 133 | }; |