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