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 "OutputSegment.h"
13 #include "SymbolTable.h"
14 #include "SyntheticSections.h"
15 
16 using namespace llvm;
17 using namespace llvm::wasm;
18 
19 namespace lld {
20 namespace wasm {
21 
22 static bool requiresGOTAccess(const Symbol *sym) {
23   if (!config->isPic)
24     return false;
25   if (sym->isHidden() || sym->isLocal())
26     return false;
27   // With `-Bsymbolic` (or when building an executable) as don't need to use
28   // the GOT for symbols that are defined within the current module.
29   if (sym->isDefined() && (!config->shared || config->bsymbolic))
30     return false;
31   return true;
32 }
33 
34 static bool allowUndefined(const Symbol* sym) {
35   // Undefined functions and globals with explicit import name are allowed to be
36   // undefined at link time.
37   if (auto *f = dyn_cast<UndefinedFunction>(sym))
38     if (f->importName)
39       return true;
40   if (auto *g = dyn_cast<UndefinedGlobal>(sym))
41     if (g->importName)
42       return true;
43   if (auto *g = dyn_cast<UndefinedGlobal>(sym))
44     if (g->importName)
45       return true;
46   return config->allowUndefinedSymbols.count(sym->getName()) != 0;
47 }
48 
49 static void reportUndefined(Symbol *sym) {
50   if (!allowUndefined(sym)) {
51     switch (config->unresolvedSymbols) {
52     case UnresolvedPolicy::ReportError:
53       error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
54       break;
55     case UnresolvedPolicy::Warn:
56       warn(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
57       break;
58     case UnresolvedPolicy::Ignore:
59       if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
60         if (!f->stubFunction) {
61           LLVM_DEBUG(dbgs()
62                      << "ignoring undefined symbol: " + toString(*sym) + "\n");
63           f->stubFunction = symtab->createUndefinedStub(*f->getSignature());
64           f->stubFunction->markLive();
65           f->setTableIndex(0);
66         }
67       }
68       break;
69     case UnresolvedPolicy::ImportFuncs:
70       break;
71     }
72   }
73 }
74 
75 static void addGOTEntry(Symbol *sym) {
76   if (requiresGOTAccess(sym))
77     out.importSec->addGOTEntry(sym);
78   else
79     out.globalSec->addInternalGOTEntry(sym);
80 }
81 
82 void scanRelocations(InputChunk *chunk) {
83   if (!chunk->live)
84     return;
85   ObjFile *file = chunk->file;
86   ArrayRef<WasmSignature> types = file->getWasmObj()->types();
87   for (const WasmRelocation &reloc : chunk->getRelocations()) {
88     if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
89       // Mark target type as live
90       file->typeMap[reloc.Index] =
91           out.typeSec->registerType(types[reloc.Index]);
92       file->typeIsUsed[reloc.Index] = true;
93       continue;
94     }
95 
96     // Other relocation types all have a corresponding symbol
97     Symbol *sym = file->getSymbols()[reloc.Index];
98 
99     switch (reloc.Type) {
100     case R_WASM_TABLE_INDEX_I32:
101     case R_WASM_TABLE_INDEX_I64:
102     case R_WASM_TABLE_INDEX_SLEB:
103     case R_WASM_TABLE_INDEX_SLEB64:
104     case R_WASM_TABLE_INDEX_REL_SLEB:
105       if (requiresGOTAccess(sym))
106         break;
107       out.elemSec->addEntry(cast<FunctionSymbol>(sym));
108       break;
109     case R_WASM_GLOBAL_INDEX_LEB:
110     case R_WASM_GLOBAL_INDEX_I32:
111       if (!isa<GlobalSymbol>(sym))
112         addGOTEntry(sym);
113       break;
114     case R_WASM_MEMORY_ADDR_TLS_SLEB:
115       if (auto *D = dyn_cast<DefinedData>(sym)) {
116         if (D->segment->outputSeg->name != ".tdata") {
117           error(toString(file) + ": relocation " +
118                 relocTypeToString(reloc.Type) + " cannot be used against `" +
119                 toString(*sym) +
120                 "` in non-TLS section: " + D->segment->outputSeg->name);
121         }
122       }
123       break;
124     }
125 
126     if (config->isPic) {
127       switch (reloc.Type) {
128       case R_WASM_TABLE_INDEX_SLEB:
129       case R_WASM_TABLE_INDEX_SLEB64:
130       case R_WASM_MEMORY_ADDR_SLEB:
131       case R_WASM_MEMORY_ADDR_LEB:
132       case R_WASM_MEMORY_ADDR_SLEB64:
133       case R_WASM_MEMORY_ADDR_LEB64:
134         // Certain relocation types can't be used when building PIC output,
135         // since they would require absolute symbol addresses at link time.
136         error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
137               " cannot be used against symbol " + toString(*sym) +
138               "; recompile with -fPIC");
139         break;
140       case R_WASM_TABLE_INDEX_I32:
141       case R_WASM_TABLE_INDEX_I64:
142       case R_WASM_MEMORY_ADDR_I32:
143       case R_WASM_MEMORY_ADDR_I64:
144         // These relocation types are only present in the data section and
145         // will be converted into code by `generateRelocationCode`.  This code
146         // requires the symbols to have GOT entires.
147         if (requiresGOTAccess(sym))
148           addGOTEntry(sym);
149         break;
150       }
151     } else {
152       // Report undefined symbols
153       if (sym->isUndefined() && !config->relocatable && !sym->isWeak())
154         reportUndefined(sym);
155     }
156   }
157 }
158 
159 } // namespace wasm
160 } // namespace lld
161