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