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 || config->importUndefined)
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       LLVM_DEBUG(dbgs() << "ignoring undefined symbol: " + toString(*sym) +
60                                "\n");
61       if (!config->importUndefined) {
62         if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
63           if (!f->stubFunction) {
64             f->stubFunction = symtab->createUndefinedStub(*f->getSignature());
65             f->stubFunction->markLive();
66             // Mark the function itself as a stub which prevents it from being
67             // assigned a table entry.
68             f->isStub = true;
69           }
70         }
71       }
72       break;
73     }
74   }
75 }
76 
77 static void addGOTEntry(Symbol *sym) {
78   if (requiresGOTAccess(sym))
79     out.importSec->addGOTEntry(sym);
80   else
81     out.globalSec->addInternalGOTEntry(sym);
82 }
83 
84 void scanRelocations(InputChunk *chunk) {
85   if (!chunk->live)
86     return;
87   ObjFile *file = chunk->file;
88   ArrayRef<WasmSignature> types = file->getWasmObj()->types();
89   for (const WasmRelocation &reloc : chunk->getRelocations()) {
90     if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
91       // Mark target type as live
92       file->typeMap[reloc.Index] =
93           out.typeSec->registerType(types[reloc.Index]);
94       file->typeIsUsed[reloc.Index] = true;
95       continue;
96     }
97 
98     // Other relocation types all have a corresponding symbol
99     Symbol *sym = file->getSymbols()[reloc.Index];
100 
101     switch (reloc.Type) {
102     case R_WASM_TABLE_INDEX_I32:
103     case R_WASM_TABLE_INDEX_I64:
104     case R_WASM_TABLE_INDEX_SLEB:
105     case R_WASM_TABLE_INDEX_SLEB64:
106     case R_WASM_TABLE_INDEX_REL_SLEB:
107     case R_WASM_TABLE_INDEX_REL_SLEB64:
108       if (requiresGOTAccess(sym))
109         break;
110       out.elemSec->addEntry(cast<FunctionSymbol>(sym));
111       break;
112     case R_WASM_GLOBAL_INDEX_LEB:
113     case R_WASM_GLOBAL_INDEX_I32:
114       if (!isa<GlobalSymbol>(sym))
115         addGOTEntry(sym);
116       break;
117     case R_WASM_MEMORY_ADDR_TLS_SLEB:
118     case R_WASM_MEMORY_ADDR_TLS_SLEB64:
119       // In single-threaded builds TLS is lowered away and TLS data can be
120       // merged with normal data and allowing TLS relocation in non-TLS
121       // segments.
122       if (config->sharedMemory) {
123         if (!sym->isTLS()) {
124           error(toString(file) + ": relocation " +
125                 relocTypeToString(reloc.Type) +
126                 " cannot be used against non-TLS symbol `" + toString(*sym) +
127                 "`");
128         }
129         if (auto *D = dyn_cast<DefinedData>(sym)) {
130           if (!D->segment->outputSeg->isTLS()) {
131             error(toString(file) + ": relocation " +
132                   relocTypeToString(reloc.Type) + " cannot be used against `" +
133                   toString(*sym) +
134                   "` in non-TLS section: " + D->segment->outputSeg->name);
135           }
136         }
137       }
138       break;
139     }
140 
141     if (config->isPic) {
142       switch (reloc.Type) {
143       case R_WASM_TABLE_INDEX_SLEB:
144       case R_WASM_TABLE_INDEX_SLEB64:
145       case R_WASM_MEMORY_ADDR_SLEB:
146       case R_WASM_MEMORY_ADDR_LEB:
147       case R_WASM_MEMORY_ADDR_SLEB64:
148       case R_WASM_MEMORY_ADDR_LEB64:
149         // Certain relocation types can't be used when building PIC output,
150         // since they would require absolute symbol addresses at link time.
151         error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
152               " cannot be used against symbol " + toString(*sym) +
153               "; recompile with -fPIC");
154         break;
155       case R_WASM_TABLE_INDEX_I32:
156       case R_WASM_TABLE_INDEX_I64:
157       case R_WASM_MEMORY_ADDR_I32:
158       case R_WASM_MEMORY_ADDR_I64:
159         // These relocation types are only present in the data section and
160         // will be converted into code by `generateRelocationCode`.  This code
161         // requires the symbols to have GOT entires.
162         if (requiresGOTAccess(sym))
163           addGOTEntry(sym);
164         break;
165       }
166     } else if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
167       // Report undefined symbols
168       reportUndefined(sym);
169     }
170   }
171 }
172 
173 } // namespace wasm
174 } // namespace lld
175