1 //===- Relocations.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 "Relocations.h" 10 11 #include "InputChunks.h" 12 #include "SyntheticSections.h" 13 14 using namespace llvm; 15 using namespace llvm::wasm; 16 17 namespace lld { 18 namespace wasm { 19 static bool requiresGOTAccess(const Symbol *sym) { 20 return config->isPic && !sym->isHidden() && !sym->isLocal(); 21 } 22 23 static bool allowUndefined(const Symbol* sym) { 24 // Undefined functions and globals with explicit import name are allowed to be 25 // undefined at link time. 26 if (auto *f = dyn_cast<UndefinedFunction>(sym)) 27 if (f->importName) 28 return true; 29 if (auto *g = dyn_cast<UndefinedGlobal>(sym)) 30 if (g->importName) 31 return true; 32 return (config->allowUndefined || 33 config->allowUndefinedSymbols.count(sym->getName()) != 0); 34 } 35 36 static void reportUndefined(const Symbol* sym) { 37 assert(sym->isUndefined()); 38 assert(!sym->isWeak()); 39 if (!allowUndefined(sym)) 40 error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym)); 41 } 42 43 static void addGOTEntry(Symbol *sym) { 44 // In PIC mode a GOT entry is an imported global that the dynamic linker 45 // will assign. 46 // In non-PIC mode (i.e. when code compiled as fPIC is linked into a static 47 // binary) we create an internal wasm global with a fixed value that takes the 48 // place of th GOT entry and effectivly acts as an i32 const. This can 49 // potentially be optimized away at runtime or with a post-link tool. 50 // TODO(sbc): Linker relaxation might also be able to optimize this away. 51 if (config->isPic) 52 out.importSec->addGOTEntry(sym); 53 else 54 out.globalSec->addStaticGOTEntry(sym); 55 } 56 57 void scanRelocations(InputChunk *chunk) { 58 if (!chunk->live) 59 return; 60 ObjFile *file = chunk->file; 61 ArrayRef<WasmSignature> types = file->getWasmObj()->types(); 62 for (const WasmRelocation &reloc : chunk->getRelocations()) { 63 if (reloc.Type == R_WASM_TYPE_INDEX_LEB) { 64 // Mark target type as live 65 file->typeMap[reloc.Index] = 66 out.typeSec->registerType(types[reloc.Index]); 67 file->typeIsUsed[reloc.Index] = true; 68 continue; 69 } 70 71 // Other relocation types all have a corresponding symbol 72 Symbol *sym = file->getSymbols()[reloc.Index]; 73 74 switch (reloc.Type) { 75 case R_WASM_TABLE_INDEX_I32: 76 case R_WASM_TABLE_INDEX_I64: 77 case R_WASM_TABLE_INDEX_SLEB: 78 case R_WASM_TABLE_INDEX_SLEB64: 79 case R_WASM_TABLE_INDEX_REL_SLEB: 80 if (requiresGOTAccess(sym)) 81 break; 82 out.elemSec->addEntry(cast<FunctionSymbol>(sym)); 83 break; 84 case R_WASM_GLOBAL_INDEX_LEB: 85 case R_WASM_GLOBAL_INDEX_I32: 86 if (!isa<GlobalSymbol>(sym)) 87 addGOTEntry(sym); 88 break; 89 } 90 91 if (config->isPic) { 92 switch (reloc.Type) { 93 case R_WASM_TABLE_INDEX_SLEB: 94 case R_WASM_TABLE_INDEX_SLEB64: 95 case R_WASM_MEMORY_ADDR_SLEB: 96 case R_WASM_MEMORY_ADDR_LEB: 97 case R_WASM_MEMORY_ADDR_SLEB64: 98 case R_WASM_MEMORY_ADDR_LEB64: 99 // Certain relocation types can't be used when building PIC output, 100 // since they would require absolute symbol addresses at link time. 101 error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) + 102 " cannot be used against symbol " + toString(*sym) + 103 "; recompile with -fPIC"); 104 break; 105 case R_WASM_TABLE_INDEX_I32: 106 case R_WASM_TABLE_INDEX_I64: 107 case R_WASM_MEMORY_ADDR_I32: 108 case R_WASM_MEMORY_ADDR_I64: 109 // These relocation types are only present in the data section and 110 // will be converted into code by `generateRelocationCode`. This code 111 // requires the symbols to have GOT entires. 112 if (requiresGOTAccess(sym)) 113 addGOTEntry(sym); 114 break; 115 } 116 } else { 117 // Report undefined symbols 118 if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) 119 reportUndefined(sym); 120 } 121 122 } 123 } 124 125 } // namespace wasm 126 } // namespace lld 127