blob: 15b1ed1f803911ecc8fa4bb4d0167a7857386992 [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
Nicholas Wilsonc4d9aa12018-03-14 15:45:11 +000039StringRef 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 Clegg5fa274b2018-01-10 01:13:34 +000046void InputChunk::copyRelocations(const WasmSection &Section) {
Sam Clegg50686852018-01-12 18:35:13 +000047 if (Section.Relocations.empty())
48 return;
Sam Clegg5fa274b2018-01-10 01:13:34 +000049 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 Cleggd96d9352018-01-10 19:22:42 +000055
Sam Cleggc1953142018-05-05 00:18:43 +000056void 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 Ueyamabf450d92018-02-20 04:26:26 +000093// Copy this input chunk to an mmap'ed output file and apply relocations.
94void InputChunk::writeTo(uint8_t *Buf) const {
95 // Copy contents
96 memcpy(Buf + OutputOffset, data().data(), data().size());
Rui Ueyamac06d94a2018-02-19 22:39:52 +000097
Rui Ueyamabf450d92018-02-20 04:26:26 +000098 // Apply relocations
99 if (Relocations.empty())
100 return;
Rui Ueyamac06d94a2018-02-19 22:39:52 +0000101
Sam Cleggc1953142018-05-05 00:18:43 +0000102#ifndef NDEBUG
103 verifyRelocTargets();
104#endif
105
Sam Clegg37fbfc62018-03-14 00:53:34 +0000106 DEBUG(dbgs() << "applying relocations: " << getName()
107 << " count=" << Relocations.size() << "\n");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000108 int32_t Off = OutputOffset - getInputSectionOffset();
109
110 for (const WasmRelocation &Rel : Relocations) {
111 uint8_t *Loc = Buf + Rel.Offset + Off;
Sam Cleggc1be8232018-03-11 01:35:02 +0000112 uint32_t Value = File->calcNewValue(Rel);
113 DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
114 << " addend=" << Rel.Addend << " index=" << Rel.Index
Rui Ueyamabf450d92018-02-20 04:26:26 +0000115 << " 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 Cleggd177ab22018-05-04 23:14:42 +0000130 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
131 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
Rui Ueyamabf450d92018-02-20 04:26:26 +0000132 write32le(Loc, Value);
133 break;
134 default:
135 llvm_unreachable("unknown relocation type");
Sam Cleggd96d9352018-01-10 19:22:42 +0000136 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000137 }
138}
139
Rui Ueyamabf450d92018-02-20 04:26:26 +0000140// 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.
143void InputChunk::writeRelocations(raw_ostream &OS) const {
Sam Clegg50686852018-01-12 18:35:13 +0000144 if (Relocations.empty())
145 return;
Rui Ueyamabf450d92018-02-20 04:26:26 +0000146
147 int32_t Off = OutputOffset - getInputSectionOffset();
148 DEBUG(dbgs() << "writeRelocations: " << File->getName()
Sam Clegg7ed293e2018-01-12 00:34:04 +0000149 << " offset=" << Twine(Off) << "\n");
Sam Cleggd96d9352018-01-10 19:22:42 +0000150
Rui Ueyamabf450d92018-02-20 04:26:26 +0000151 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 Cleggd96d9352018-01-10 19:22:42 +0000155
Rui Ueyamabf450d92018-02-20 04:26:26 +0000156 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 Cleggd177ab22018-05-04 23:14:42 +0000160 case R_WEBASSEMBLY_FUNCTION_OFFSET_I32:
161 case R_WEBASSEMBLY_SECTION_OFFSET_I32:
162 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend");
Rui Ueyamabf450d92018-02-20 04:26:26 +0000163 break;
164 }
Sam Cleggd96d9352018-01-10 19:22:42 +0000165 }
166}
Sam Clegg50686852018-01-12 18:35:13 +0000167
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000168void InputFunction::setFunctionIndex(uint32_t Index) {
169 DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> "
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000170 << Index << "\n");
Sam Clegge3f3ccf2018-03-12 19:56:23 +0000171 assert(!hasFunctionIndex());
172 FunctionIndex = Index;
Eric Christopher9ea500b2018-01-13 00:44:45 +0000173}
Sam Clegg67abf532018-01-24 21:45:25 +0000174
175void InputFunction::setTableIndex(uint32_t Index) {
Nicholas Wilsondbd90bf2018-03-07 13:28:16 +0000176 DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
177 << Index << "\n");
Sam Clegg67abf532018-01-24 21:45:25 +0000178 assert(!hasTableIndex());
179 TableIndex = Index;
180}