1 //===- InputChunks.cpp ----------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InputChunks.h" 10 #include "Config.h" 11 #include "OutputSegment.h" 12 #include "WriterUtils.h" 13 #include "lld/Common/ErrorHandler.h" 14 #include "lld/Common/LLVM.h" 15 #include "llvm/Support/LEB128.h" 16 17 #define DEBUG_TYPE "lld" 18 19 using namespace llvm; 20 using namespace llvm::wasm; 21 using namespace llvm::support::endian; 22 using namespace lld; 23 using namespace lld::wasm; 24 25 static StringRef reloctTypeToString(uint8_t RelocType) { 26 switch (RelocType) { 27 #define WASM_RELOC(NAME, REL) \ 28 case REL: \ 29 return #NAME; 30 #include "llvm/BinaryFormat/WasmRelocs.def" 31 #undef WASM_RELOC 32 } 33 llvm_unreachable("unknown reloc type"); 34 } 35 36 std::string lld::toString(const InputChunk *C) { 37 return (toString(C->File) + ":(" + C->getName() + ")").str(); 38 } 39 40 StringRef InputChunk::getComdatName() const { 41 uint32_t Index = getComdat(); 42 if (Index == UINT32_MAX) 43 return StringRef(); 44 return File->getWasmObj()->linkingData().Comdats[Index]; 45 } 46 47 void InputChunk::verifyRelocTargets() const { 48 for (const WasmRelocation &Rel : Relocations) { 49 uint32_t ExistingValue; 50 unsigned BytesRead = 0; 51 uint32_t Offset = Rel.Offset - getInputSectionOffset(); 52 const uint8_t *Loc = data().data() + Offset; 53 switch (Rel.Type) { 54 case R_WASM_TYPE_INDEX_LEB: 55 case R_WASM_FUNCTION_INDEX_LEB: 56 case R_WASM_GLOBAL_INDEX_LEB: 57 case R_WASM_EVENT_INDEX_LEB: 58 case R_WASM_MEMORY_ADDR_LEB: 59 ExistingValue = decodeULEB128(Loc, &BytesRead); 60 break; 61 case R_WASM_TABLE_INDEX_SLEB: 62 case R_WASM_MEMORY_ADDR_SLEB: 63 ExistingValue = static_cast<uint32_t>(decodeSLEB128(Loc, &BytesRead)); 64 break; 65 case R_WASM_TABLE_INDEX_I32: 66 case R_WASM_MEMORY_ADDR_I32: 67 case R_WASM_FUNCTION_OFFSET_I32: 68 case R_WASM_SECTION_OFFSET_I32: 69 ExistingValue = static_cast<uint32_t>(read32le(Loc)); 70 break; 71 default: 72 llvm_unreachable("unknown relocation type"); 73 } 74 75 if (BytesRead && BytesRead != 5) 76 warn("expected LEB at relocation site be 5-byte padded"); 77 uint32_t ExpectedValue = File->calcExpectedValue(Rel); 78 if (ExpectedValue != ExistingValue) 79 warn("unexpected existing value for " + reloctTypeToString(Rel.Type) + 80 ": existing=" + Twine(ExistingValue) + 81 " expected=" + Twine(ExpectedValue)); 82 } 83 } 84 85 // Copy this input chunk to an mmap'ed output file and apply relocations. 86 void InputChunk::writeTo(uint8_t *Buf) const { 87 // Copy contents 88 memcpy(Buf + OutputOffset, data().data(), data().size()); 89 90 // Apply relocations 91 if (Relocations.empty()) 92 return; 93 94 #ifndef NDEBUG 95 verifyRelocTargets(); 96 #endif 97 98 LLVM_DEBUG(dbgs() << "applying relocations: " << getName() 99 << " count=" << Relocations.size() << "\n"); 100 int32_t Off = OutputOffset - getInputSectionOffset(); 101 102 for (const WasmRelocation &Rel : Relocations) { 103 uint8_t *Loc = Buf + Rel.Offset + Off; 104 uint32_t Value = File->calcNewValue(Rel); 105 LLVM_DEBUG(dbgs() << "apply reloc: type=" << reloctTypeToString(Rel.Type) 106 << " addend=" << Rel.Addend << " index=" << Rel.Index 107 << " value=" << Value << " offset=" << Rel.Offset 108 << "\n"); 109 110 switch (Rel.Type) { 111 case R_WASM_TYPE_INDEX_LEB: 112 case R_WASM_FUNCTION_INDEX_LEB: 113 case R_WASM_GLOBAL_INDEX_LEB: 114 case R_WASM_EVENT_INDEX_LEB: 115 case R_WASM_MEMORY_ADDR_LEB: 116 encodeULEB128(Value, Loc, 5); 117 break; 118 case R_WASM_TABLE_INDEX_SLEB: 119 case R_WASM_MEMORY_ADDR_SLEB: 120 encodeSLEB128(static_cast<int32_t>(Value), Loc, 5); 121 break; 122 case R_WASM_TABLE_INDEX_I32: 123 case R_WASM_MEMORY_ADDR_I32: 124 case R_WASM_FUNCTION_OFFSET_I32: 125 case R_WASM_SECTION_OFFSET_I32: 126 write32le(Loc, Value); 127 break; 128 default: 129 llvm_unreachable("unknown relocation type"); 130 } 131 } 132 } 133 134 // Copy relocation entries to a given output stream. 135 // This function is used only when a user passes "-r". For a regular link, 136 // we consume relocations instead of copying them to an output file. 137 void InputChunk::writeRelocations(raw_ostream &OS) const { 138 if (Relocations.empty()) 139 return; 140 141 int32_t Off = OutputOffset - getInputSectionOffset(); 142 LLVM_DEBUG(dbgs() << "writeRelocations: " << File->getName() 143 << " offset=" << Twine(Off) << "\n"); 144 145 for (const WasmRelocation &Rel : Relocations) { 146 writeUleb128(OS, Rel.Type, "reloc type"); 147 writeUleb128(OS, Rel.Offset + Off, "reloc offset"); 148 writeUleb128(OS, File->calcNewIndex(Rel), "reloc index"); 149 150 switch (Rel.Type) { 151 case R_WASM_MEMORY_ADDR_LEB: 152 case R_WASM_MEMORY_ADDR_SLEB: 153 case R_WASM_MEMORY_ADDR_I32: 154 case R_WASM_FUNCTION_OFFSET_I32: 155 case R_WASM_SECTION_OFFSET_I32: 156 writeSleb128(OS, File->calcNewAddend(Rel), "reloc addend"); 157 break; 158 } 159 } 160 } 161 162 void InputFunction::setFunctionIndex(uint32_t Index) { 163 LLVM_DEBUG(dbgs() << "InputFunction::setFunctionIndex: " << getName() 164 << " -> " << Index << "\n"); 165 assert(!hasFunctionIndex()); 166 FunctionIndex = Index; 167 } 168 169 void InputFunction::setTableIndex(uint32_t Index) { 170 LLVM_DEBUG(dbgs() << "InputFunction::setTableIndex: " << getName() << " -> " 171 << Index << "\n"); 172 assert(!hasTableIndex()); 173 TableIndex = Index; 174 } 175 176 // Write a relocation value without padding and return the number of bytes 177 // witten. 178 static unsigned writeCompressedReloc(uint8_t *Buf, const WasmRelocation &Rel, 179 uint32_t Value) { 180 switch (Rel.Type) { 181 case R_WASM_TYPE_INDEX_LEB: 182 case R_WASM_FUNCTION_INDEX_LEB: 183 case R_WASM_GLOBAL_INDEX_LEB: 184 case R_WASM_EVENT_INDEX_LEB: 185 case R_WASM_MEMORY_ADDR_LEB: 186 return encodeULEB128(Value, Buf); 187 case R_WASM_TABLE_INDEX_SLEB: 188 case R_WASM_MEMORY_ADDR_SLEB: 189 return encodeSLEB128(static_cast<int32_t>(Value), Buf); 190 default: 191 llvm_unreachable("unexpected relocation type"); 192 } 193 } 194 195 static unsigned getRelocWidthPadded(const WasmRelocation &Rel) { 196 switch (Rel.Type) { 197 case R_WASM_TYPE_INDEX_LEB: 198 case R_WASM_FUNCTION_INDEX_LEB: 199 case R_WASM_GLOBAL_INDEX_LEB: 200 case R_WASM_EVENT_INDEX_LEB: 201 case R_WASM_MEMORY_ADDR_LEB: 202 case R_WASM_TABLE_INDEX_SLEB: 203 case R_WASM_MEMORY_ADDR_SLEB: 204 return 5; 205 default: 206 llvm_unreachable("unexpected relocation type"); 207 } 208 } 209 210 static unsigned getRelocWidth(const WasmRelocation &Rel, uint32_t Value) { 211 uint8_t Buf[5]; 212 return writeCompressedReloc(Buf, Rel, Value); 213 } 214 215 // Relocations of type LEB and SLEB in the code section are padded to 5 bytes 216 // so that a fast linker can blindly overwrite them without needing to worry 217 // about the number of bytes needed to encode the values. 218 // However, for optimal output the code section can be compressed to remove 219 // the padding then outputting non-relocatable files. 220 // In this case we need to perform a size calculation based on the value at each 221 // relocation. At best we end up saving 4 bytes for each relocation entry. 222 // 223 // This function only computes the final output size. It must be called 224 // before getSize() is used to calculate of layout of the code section. 225 void InputFunction::calculateSize() { 226 if (!File || !Config->CompressRelocations) 227 return; 228 229 LLVM_DEBUG(dbgs() << "calculateSize: " << getName() << "\n"); 230 231 const uint8_t *SecStart = File->CodeSection->Content.data(); 232 const uint8_t *FuncStart = SecStart + getInputSectionOffset(); 233 uint32_t FunctionSizeLength; 234 decodeULEB128(FuncStart, &FunctionSizeLength); 235 236 uint32_t Start = getInputSectionOffset(); 237 uint32_t End = Start + Function->Size; 238 239 uint32_t LastRelocEnd = Start + FunctionSizeLength; 240 for (const WasmRelocation &Rel : Relocations) { 241 LLVM_DEBUG(dbgs() << " region: " << (Rel.Offset - LastRelocEnd) << "\n"); 242 CompressedFuncSize += Rel.Offset - LastRelocEnd; 243 CompressedFuncSize += getRelocWidth(Rel, File->calcNewValue(Rel)); 244 LastRelocEnd = Rel.Offset + getRelocWidthPadded(Rel); 245 } 246 LLVM_DEBUG(dbgs() << " final region: " << (End - LastRelocEnd) << "\n"); 247 CompressedFuncSize += End - LastRelocEnd; 248 249 // Now we know how long the resulting function is we can add the encoding 250 // of its length 251 uint8_t Buf[5]; 252 CompressedSize = CompressedFuncSize + encodeULEB128(CompressedFuncSize, Buf); 253 254 LLVM_DEBUG(dbgs() << " calculateSize orig: " << Function->Size << "\n"); 255 LLVM_DEBUG(dbgs() << " calculateSize new: " << CompressedSize << "\n"); 256 } 257 258 // Override the default writeTo method so that we can (optionally) write the 259 // compressed version of the function. 260 void InputFunction::writeTo(uint8_t *Buf) const { 261 if (!File || !Config->CompressRelocations) 262 return InputChunk::writeTo(Buf); 263 264 Buf += OutputOffset; 265 uint8_t *Orig = Buf; 266 (void)Orig; 267 268 const uint8_t *SecStart = File->CodeSection->Content.data(); 269 const uint8_t *FuncStart = SecStart + getInputSectionOffset(); 270 const uint8_t *End = FuncStart + Function->Size; 271 uint32_t Count; 272 decodeULEB128(FuncStart, &Count); 273 FuncStart += Count; 274 275 LLVM_DEBUG(dbgs() << "write func: " << getName() << "\n"); 276 Buf += encodeULEB128(CompressedFuncSize, Buf); 277 const uint8_t *LastRelocEnd = FuncStart; 278 for (const WasmRelocation &Rel : Relocations) { 279 unsigned ChunkSize = (SecStart + Rel.Offset) - LastRelocEnd; 280 LLVM_DEBUG(dbgs() << " write chunk: " << ChunkSize << "\n"); 281 memcpy(Buf, LastRelocEnd, ChunkSize); 282 Buf += ChunkSize; 283 Buf += writeCompressedReloc(Buf, Rel, File->calcNewValue(Rel)); 284 LastRelocEnd = SecStart + Rel.Offset + getRelocWidthPadded(Rel); 285 } 286 287 unsigned ChunkSize = End - LastRelocEnd; 288 LLVM_DEBUG(dbgs() << " write final chunk: " << ChunkSize << "\n"); 289 memcpy(Buf, LastRelocEnd, ChunkSize); 290 LLVM_DEBUG(dbgs() << " total: " << (Buf + ChunkSize - Orig) << "\n"); 291 } 292