1 //===- InputChunks.cpp ----------------------------------------------------===//
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 
10 #include "InputChunks.h"
11 #include "Config.h"
12 #include "OutputSegment.h"
13 #include "WriterUtils.h"
14 #include "lld/Common/ErrorHandler.h"
15 #include "lld/Common/LLVM.h"
16 #include "llvm/Support/LEB128.h"
17 
18 #define DEBUG_TYPE "lld"
19 
20 using namespace llvm;
21 using namespace llvm::wasm;
22 using namespace llvm::support::endian;
23 using namespace lld;
24 using namespace lld::wasm;
25 
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 
35 std::string lld::toString(const InputChunk *C) {
36   return (toString(C->File) + ":(" + C->getName() + ")").str();
37 }
38 
39 StringRef 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 
46 void InputChunk::copyRelocations(const WasmSection &Section) {
47   if (Section.Relocations.empty())
48     return;
49   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 }
55 
56 // Copy this input chunk to an mmap'ed output file and apply relocations.
57 void InputChunk::writeTo(uint8_t *Buf) const {
58   // Copy contents
59   memcpy(Buf + OutputOffset, data().data(), data().size());
60 
61   // Apply relocations
62   if (Relocations.empty())
63     return;
64 
65   DEBUG(dbgs() << "applying relocations: " << getName()
66                << " count=" << Relocations.size() << "\n");
67   int32_t Off = OutputOffset - getInputSectionOffset();
68 
69   for (const WasmRelocation &Rel : Relocations) {
70     uint8_t *Loc = Buf + Rel.Offset + Off;
71     uint32_t Value = File->calcNewValue(Rel);
72     uint32_t ExistingValue;
73     DEBUG(dbgs() << "apply reloc: type=" << ReloctTypeToString(Rel.Type)
74                  << " addend=" << Rel.Addend << " index=" << Rel.Index
75                  << " value=" << Value << " offset=" << Rel.Offset << "\n");
76 
77     switch (Rel.Type) {
78     case R_WEBASSEMBLY_TYPE_INDEX_LEB:
79     case R_WEBASSEMBLY_FUNCTION_INDEX_LEB:
80     case R_WEBASSEMBLY_GLOBAL_INDEX_LEB:
81     case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
82       ExistingValue = decodeULEB128(Loc);
83       encodeULEB128(Value, Loc, 5);
84       break;
85     case R_WEBASSEMBLY_TABLE_INDEX_SLEB:
86     case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
87       ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc));
88       encodeSLEB128(static_cast<int32_t>(Value), Loc, 5);
89       break;
90     case R_WEBASSEMBLY_TABLE_INDEX_I32:
91     case R_WEBASSEMBLY_MEMORY_ADDR_I32:
92       ExistingValue = static_cast<uint32_t>(read32le(Loc));
93       write32le(Loc, Value);
94       break;
95     default:
96       llvm_unreachable("unknown relocation type");
97     }
98 
99     uint32_t ExpectedValue = File->calcExpectedValue(Rel);
100     if (ExpectedValue != ExistingValue)
101       error("unexpected existing value for " + ReloctTypeToString(Rel.Type) +
102             ": existing=" + Twine(ExistingValue) +
103             " expected=" + Twine(ExpectedValue));
104   }
105 }
106 
107 // Copy relocation entries to a given output stream.
108 // This function is used only when a user passes "-r". For a regular link,
109 // we consume relocations instead of copying them to an output file.
110 void InputChunk::writeRelocations(raw_ostream &OS) const {
111   if (Relocations.empty())
112     return;
113 
114   int32_t Off = OutputOffset - getInputSectionOffset();
115   DEBUG(dbgs() << "writeRelocations: " << File->getName()
116                << " offset=" << Twine(Off) << "\n");
117 
118   for (const WasmRelocation &Rel : Relocations) {
119     writeUleb128(OS, Rel.Type, "reloc type");
120     writeUleb128(OS, Rel.Offset + Off, "reloc offset");
121     writeUleb128(OS, File->calcNewIndex(Rel), "reloc index");
122 
123     switch (Rel.Type) {
124     case R_WEBASSEMBLY_MEMORY_ADDR_LEB:
125     case R_WEBASSEMBLY_MEMORY_ADDR_SLEB:
126     case R_WEBASSEMBLY_MEMORY_ADDR_I32:
127       writeUleb128(OS, Rel.Addend, "reloc addend");
128       break;
129     }
130   }
131 }
132 
133 void InputFunction::setFunctionIndex(uint32_t Index) {
134   DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() << " -> "
135                << Index << "\n");
136   assert(!hasFunctionIndex());
137   FunctionIndex = Index;
138 }
139 
140 void InputFunction::setTableIndex(uint32_t Index) {
141   DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> "
142                << Index << "\n");
143   assert(!hasTableIndex());
144   TableIndex = Index;
145 }
146