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 // Historically --allow-undefined doesn't work for data symbols since we don't 25 // have any way to represent these as imports in the final binary. The idea 26 // behind allowing undefined symbols is to allow importing these symbols from 27 // the embedder and we can't do this for data symbols (at least not without 28 // compiling with -fPIC) 29 if (isa<DataSymbol>(sym)) 30 return false; 31 return (config->allowUndefined || 32 config->allowUndefinedSymbols.count(sym->getName()) != 0); 33 } 34 35 static void reportUndefined(const Symbol* sym) { 36 assert(sym->isUndefined()); 37 assert(!sym->isWeak()); 38 if (!allowUndefined(sym)) 39 error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym)); 40 } 41 42 static void addGOTEntry(Symbol *sym) { 43 // In PIC mode a GOT entry is an imported global that the dynamic linker 44 // will assign. 45 // In non-PIC mode (i.e. when code compiled as fPIC is linked into a static 46 // binary) we create an internal wasm global with a fixed value that takes the 47 // place of th GOT entry and effectivly acts as an i32 const. This can 48 // potentially be optimized away at runtime or with a post-link tool. 49 // TODO(sbc): Linker relaxation might also be able to optimize this away. 50 if (config->isPic) 51 out.importSec->addGOTEntry(sym); 52 else 53 out.globalSec->addStaticGOTEntry(sym); 54 } 55 56 void scanRelocations(InputChunk *chunk) { 57 if (!chunk->live) 58 return; 59 ObjFile *file = chunk->file; 60 ArrayRef<WasmSignature> types = file->getWasmObj()->types(); 61 for (const WasmRelocation &reloc : chunk->getRelocations()) { 62 if (reloc.Type == R_WASM_TYPE_INDEX_LEB) { 63 // Mark target type as live 64 file->typeMap[reloc.Index] = 65 out.typeSec->registerType(types[reloc.Index]); 66 file->typeIsUsed[reloc.Index] = true; 67 continue; 68 } 69 70 // Other relocation types all have a corresponding symbol 71 Symbol *sym = file->getSymbols()[reloc.Index]; 72 73 switch (reloc.Type) { 74 case R_WASM_TABLE_INDEX_I32: 75 case R_WASM_TABLE_INDEX_SLEB: 76 case R_WASM_TABLE_INDEX_REL_SLEB: 77 if (requiresGOTAccess(sym)) 78 break; 79 out.elemSec->addEntry(cast<FunctionSymbol>(sym)); 80 break; 81 case R_WASM_GLOBAL_INDEX_LEB: 82 if (!isa<GlobalSymbol>(sym)) 83 addGOTEntry(sym); 84 break; 85 } 86 87 if (config->isPic) { 88 switch (reloc.Type) { 89 case R_WASM_TABLE_INDEX_SLEB: 90 case R_WASM_MEMORY_ADDR_SLEB: 91 case R_WASM_MEMORY_ADDR_LEB: 92 // Certain relocation types can't be used when building PIC output, 93 // since they would require absolute symbol addresses at link time. 94 error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) + 95 " cannot be used against symbol " + toString(*sym) + 96 "; recompile with -fPIC"); 97 break; 98 case R_WASM_TABLE_INDEX_I32: 99 case R_WASM_MEMORY_ADDR_I32: 100 // These relocation types are only present in the data section and 101 // will be converted into code by `generateRelocationCode`. This code 102 // requires the symbols to have GOT entires. 103 if (requiresGOTAccess(sym)) 104 addGOTEntry(sym); 105 break; 106 } 107 } else { 108 // Report undefined symbols 109 if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) 110 reportUndefined(sym); 111 } 112 113 } 114 } 115 116 } // namespace wasm 117 } // namespace lld 118