blob: ef3edb8b9cafbcbb70061cf6a81cebde21f916d9 [file] [log] [blame]
Sam Cleggf61910d2018-01-12 22:18:22 +00001//===- InputChunks.cpp ----------------------------------------------------===//
Sam Cleggc94d3932017-11-17 18:14:09 +00002//
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 Clegg5fa274b2018-01-10 01:13:34 +000010#include "InputChunks.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000011#include "Config.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000012#include "OutputSegment.h"
Rui Ueyamabf450d92018-02-20 04:26:26 +000013#include "WriterUtils.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000014#include "lld/Common/ErrorHandler.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000015#include "lld/Common/LLVM.h"
Sam Cleggd96d9352018-01-10 19:22:42 +000016#include "llvm/Support/LEB128.h"
Sam Cleggc94d3932017-11-17 18:14:09 +000017
18#define DEBUG_TYPE "lld"
19
20using namespace llvm;
Sam Cleggd96d9352018-01-10 19:22:42 +000021using namespace llvm::wasm;
Rui Ueyamae351c3a2018-02-16 20:38:15 +000022using namespace llvm::support::endian;
Sam Cleggd96d9352018-01-10 19:22:42 +000023using namespace lld;
Sam Cleggc94d3932017-11-17 18:14:09 +000024using namespace lld::wasm;
25
Sam Cleggc1be8232018-03-11 01:35:02 +000026StringRef 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 Ueyama81bee042018-02-19 22:29:48 +000035std::string lld::toString(const InputChunk *C) {
36 return (toString(C->File) + ":(" + C->getName() + ")").str();
37}
38
Sam Clegg5fa274b2018-01-10 01:13:34 +000039void InputChunk::copyRelocations(const WasmSection &Section) {
Sam Clegg50686852018-01-12 18:35:13 +000040 if (Section.Relocations.empty())
41 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000042 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 Cleggd96d9352018-01-10 19:22:42 +000048
Rui Ueyamabf450d92018-02-20 04:26:26 +000049// Copy this input chunk to an mmap'ed output file and apply relocations.
50void InputChunk::writeTo(uint8_t *Buf) const {
51 // Copy contents
52 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000053
Rui Ueyamabf450d92018-02-20 04:26:26 +000054 // Apply relocations
55 if (Relocations.empty())
56 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +000057
Sam Clegg37fbfc62018-03-14 00:53:34 +000058 DEBUG(dbgs() << "applying relocations: " << getName()
59 << " count=" << Relocations.size() << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +000060 int32_t Off = OutputOffset - getInputSectionOffset();
61
62 for (const WasmRelocation &Rel : Relocations) {
63 uint8_t *Loc = Buf + Rel.Offset + Off;
Sam Cleggc1be8232018-03-11 01:35:02 +000064 uint32_t Value = File->calcNewValue(Rel);
Sam Cleggdbd33b82018-03-12 19:54:26 +000065 uint32_t ExistingValue;
Sam Cleggc1be8232018-03-11 01:35:02 +000066 DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
67 << " addend=" << Rel.Addend << " index=" << Rel.Index
Rui Ueyamabf450d92018-02-20 04:26:26 +000068 << " value=" << Value << " offset=" << Rel.Offset << "\n");
69
70 switch (Rel.Type) {
71 case R_WEBASSEMBLY_TYPE_INDEX_LEB:
72 case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
73 case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
74 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
Sam Cleggdbd33b82018-03-12 19:54:26 +000075 ExistingValue = decodeULEB128(Loc);
Rui Ueyamabf450d92018-02-20 04:26:26 +000076 encodeULEB128(Value, Loc, 5);
77 break;
78 case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
79 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
Sam Cleggdbd33b82018-03-12 19:54:26 +000080 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc));
Rui Ueyamabf450d92018-02-20 04:26:26 +000081 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
82 break;
83 case R_WEBASSEMBLY_TABLE_INDEX_I32:
84 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
Sam Cleggdbd33b82018-03-12 19:54:26 +000085 ExistingValue = static_cast<uint32_t>(read32le(Loc));
Rui Ueyamabf450d92018-02-20 04:26:26 +000086 write32le(Loc, Value);
87 break;
88 default:
89 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +000090 }
Sam Cleggdbd33b82018-03-12 19:54:26 +000091
92 uint32_t ExpectedValue = File->calcExpectedValue(Rel);
93 if (ExpectedValue != ExistingValue)
94 error("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
95 ": existing=" + Twine(ExistingValue) +
96 " expected=" + Twine(ExpectedValue));
Sam Cleggd96d9352018-01-10 19:22:42 +000097 }
98}
99
Rui Ueyamabf450d92018-02-20 04:26:26 +0000100// Copy relocation entries to a given output stream.
101// This function is used only when a user passes "-r". For a regular link,
102// we consume relocations instead of copying them to an output file.
103void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000104 if (Relocations.empty())
105 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000106
107 int32_t Off = OutputOffset - getInputSectionOffset();
108 DEBUG(dbgs() << "writeRelocations: " << File->getName()
Sam Clegg7ed293e2018-01-12 00:34:04 +0000109 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000110
Rui Ueyamabf450d92018-02-20 04:26:26 +0000111 for (const WasmRelocation &Rel : Relocations) {
112 writeUleb128(OS, Rel.Type, "reloc type");
113 writeUleb128(OS, Rel.Offset + Off, "reloc offset");
114 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
Sam Cleggd96d9352018-01-10 19:22:42 +0000115
Rui Ueyamabf450d92018-02-20 04:26:26 +0000116 switch (Rel.Type) {
117 case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
118 case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
119 case R_WEBASSEMBLY_MEMORY_ADDR_I32:
120 writeUleb128(OS, Rel.Addend, "reloc addend");
121 break;
122 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000123 }
124}
Sam Clegg50686852018-01-12 18:35:13 +0000125
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000126void InputFunction::setFunctionIndex(uint32_t Index) {
127 DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> "
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000128 << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000129 assert(!hasFunctionIndex());
130 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000131}
Sam Clegg67abf532018-01-24 21:45:25 +0000132
133void InputFunction::setTableIndex(uint32_t Index) {
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000134 DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
135 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000136 assert(!hasTableIndex());
137 TableIndex = Index;
138}