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