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 | |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 39 | void InputChunk::copyRelocations(const WasmSection &Section) { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 40 | if (Section.Relocations.empty()) |
| 41 | return; |
Sam Clegg | 5fa274b | 2018-01-10 01:13:34 +0000 | [diff] [blame] | 42 | size_t Start = getInputSectionOffset(); |
| 43 | size_t Size = getSize(); |
| 44 | for (const WasmRelocation &R : Section.Relocations) |
| 45 | if (R.Offset >= Start && R.Offset < Start + Size) |
| 46 | Relocations.push_back(R); |
| 47 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 48 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 49 | // Copy this input chunk to an mmap'ed output file and apply relocations. |
| 50 | void InputChunk::writeTo(uint8_t *Buf) const { |
| 51 | // Copy contents |
| 52 | memcpy(Buf + OutputOffset, data().data(), data().size()); |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 53 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 54 | // Apply relocations |
| 55 | if (Relocations.empty()) |
| 56 | return; |
Rui Ueyama | c06d94a | 2018-02-19 22:39:52 +0000 | [diff] [blame] | 57 | |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame^] | 58 | DEBUG(dbgs() << "applying relocations: count=" << Relocations.size() << "\n"); |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 59 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 60 | |
| 61 | for (const WasmRelocation &Rel : Relocations) { |
| 62 | uint8_t *Loc = Buf + Rel.Offset + Off; |
Sam Clegg | c1be823 | 2018-03-11 01:35:02 +0000 | [diff] [blame^] | 63 | uint32_t Value = File->calcNewValue(Rel); |
| 64 | DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type) |
| 65 | << " addend=" << Rel.Addend << " index=" << Rel.Index |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 66 | << " value=" << Value << " offset=" << Rel.Offset << "\n"); |
| 67 | |
| 68 | switch (Rel.Type) { |
| 69 | case R_WEBASSEMBLY_TYPE_INDEX_LEB: |
| 70 | case R_WEBASSEMBLY_FUNCTION_INDEX_LEB: |
| 71 | case R_WEBASSEMBLY_GLOBAL_INDEX_LEB: |
| 72 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 73 | encodeULEB128(Value, Loc, 5); |
| 74 | break; |
| 75 | case R_WEBASSEMBLY_TABLE_INDEX_SLEB: |
| 76 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 77 | encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); |
| 78 | break; |
| 79 | case R_WEBASSEMBLY_TABLE_INDEX_I32: |
| 80 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 81 | write32le(Loc, Value); |
| 82 | break; |
| 83 | default: |
| 84 | llvm_unreachable("unknown relocation type"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 85 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 89 | // Copy relocation entries to a given output stream. |
| 90 | // This function is used only when a user passes "-r". For a regular link, |
| 91 | // we consume relocations instead of copying them to an output file. |
| 92 | void InputChunk::writeRelocations(raw_ostream &OS) const { |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 93 | if (Relocations.empty()) |
| 94 | return; |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 95 | |
| 96 | int32_t Off = OutputOffset - getInputSectionOffset(); |
| 97 | DEBUG(dbgs() << "writeRelocations: " << File->getName() |
Sam Clegg | 7ed293e | 2018-01-12 00:34:04 +0000 | [diff] [blame] | 98 | << " offset=" << Twine(Off) << "\n"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 99 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 100 | for (const WasmRelocation &Rel : Relocations) { |
| 101 | writeUleb128(OS, Rel.Type, "reloc type"); |
| 102 | writeUleb128(OS, Rel.Offset + Off, "reloc offset"); |
| 103 | writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 104 | |
Rui Ueyama | bf450d9 | 2018-02-20 04:26:26 +0000 | [diff] [blame] | 105 | switch (Rel.Type) { |
| 106 | case R_WEBASSEMBLY_MEMORY_ADDR_LEB: |
| 107 | case R_WEBASSEMBLY_MEMORY_ADDR_SLEB: |
| 108 | case R_WEBASSEMBLY_MEMORY_ADDR_I32: |
| 109 | writeUleb128(OS, Rel.Addend, "reloc addend"); |
| 110 | break; |
| 111 | } |
Sam Clegg | d96d935 | 2018-01-10 19:22:42 +0000 | [diff] [blame] | 112 | } |
| 113 | } |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 114 | |
| 115 | void InputFunction::setOutputIndex(uint32_t Index) { |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 116 | DEBUG(dbgs() << "InputFunction::setOutputIndex: " << getName() << " -> " |
| 117 | << Index << "\n"); |
Sam Clegg | 5068685 | 2018-01-12 18:35:13 +0000 | [diff] [blame] | 118 | assert(!hasOutputIndex()); |
| 119 | OutputIndex = Index; |
Eric Christopher | 9ea500b | 2018-01-13 00:44:45 +0000 | [diff] [blame] | 120 | } |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 121 | |
| 122 | void InputFunction::setTableIndex(uint32_t Index) { |
Nicholas Wilson | dbd90bf | 2018-03-07 13:28:16 +0000 | [diff] [blame] | 123 | DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " |
| 124 | << Index << "\n"); |
Sam Clegg | 67abf53 | 2018-01-24 21:45:25 +0000 | [diff] [blame] | 125 | assert(!hasTableIndex()); |
| 126 | TableIndex = Index; |
| 127 | } |