• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
requiresGOTAccess(const Symbol * sym)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 
allowUndefined(const Symbol * sym)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 
reportUndefined(Symbol * sym)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           // Mark the function itself as a stub which prevents it from being
66           // assigned a table entry.
67           f->isStub = true;
68         }
69       }
70       break;
71     case UnresolvedPolicy::ImportFuncs:
72       break;
73     }
74   }
75 }
76 
addGOTEntry(Symbol * sym)77 static void addGOTEntry(Symbol *sym) {
78   if (requiresGOTAccess(sym))
79     out.importSec->addGOTEntry(sym);
80   else
81     out.globalSec->addInternalGOTEntry(sym);
82 }
83 
scanRelocations(InputChunk * chunk)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       if (requiresGOTAccess(sym))
108         break;
109       out.elemSec->addEntry(cast<FunctionSymbol>(sym));
110       break;
111     case R_WASM_GLOBAL_INDEX_LEB:
112     case R_WASM_GLOBAL_INDEX_I32:
113       if (!isa<GlobalSymbol>(sym))
114         addGOTEntry(sym);
115       break;
116     case R_WASM_MEMORY_ADDR_TLS_SLEB:
117       if (auto *D = dyn_cast<DefinedData>(sym)) {
118         if (D->segment->outputSeg->name != ".tdata") {
119           error(toString(file) + ": relocation " +
120                 relocTypeToString(reloc.Type) + " cannot be used against `" +
121                 toString(*sym) +
122                 "` in non-TLS section: " + D->segment->outputSeg->name);
123         }
124       }
125       break;
126     }
127 
128     if (config->isPic) {
129       switch (reloc.Type) {
130       case R_WASM_TABLE_INDEX_SLEB:
131       case R_WASM_TABLE_INDEX_SLEB64:
132       case R_WASM_MEMORY_ADDR_SLEB:
133       case R_WASM_MEMORY_ADDR_LEB:
134       case R_WASM_MEMORY_ADDR_SLEB64:
135       case R_WASM_MEMORY_ADDR_LEB64:
136         // Certain relocation types can't be used when building PIC output,
137         // since they would require absolute symbol addresses at link time.
138         error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
139               " cannot be used against symbol " + toString(*sym) +
140               "; recompile with -fPIC");
141         break;
142       case R_WASM_TABLE_INDEX_I32:
143       case R_WASM_TABLE_INDEX_I64:
144       case R_WASM_MEMORY_ADDR_I32:
145       case R_WASM_MEMORY_ADDR_I64:
146         // These relocation types are only present in the data section and
147         // will be converted into code by `generateRelocationCode`.  This code
148         // requires the symbols to have GOT entires.
149         if (requiresGOTAccess(sym))
150           addGOTEntry(sym);
151         break;
152       }
153     } else {
154       // Report undefined symbols
155       if (sym->isUndefined() && !config->relocatable && !sym->isWeak())
156         reportUndefined(sym);
157     }
158   }
159 }
160 
161 } // namespace wasm
162 } // namespace lld
163