2019-05-21 17:13:09 +08:00
|
|
|
//===- Relocations.cpp ----------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "Relocations.h"
|
|
|
|
|
|
|
|
#include "InputChunks.h"
|
2020-11-11 09:46:52 +08:00
|
|
|
#include "OutputSegment.h"
|
2020-05-02 00:14:59 +08:00
|
|
|
#include "SymbolTable.h"
|
2019-05-21 17:13:09 +08:00
|
|
|
#include "SyntheticSections.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace llvm::wasm;
|
|
|
|
|
2023-03-07 01:55:00 +08:00
|
|
|
namespace lld::wasm {
|
2020-10-08 05:48:37 +08:00
|
|
|
|
2019-05-21 17:13:09 +08:00
|
|
|
static bool requiresGOTAccess(const Symbol *sym) {
|
2020-11-17 02:11:37 +08:00
|
|
|
if (!config->isPic &&
|
|
|
|
config->unresolvedSymbols != UnresolvedPolicy::ImportDynamic)
|
2020-10-08 05:48:37 +08:00
|
|
|
return false;
|
|
|
|
if (sym->isHidden() || sym->isLocal())
|
|
|
|
return false;
|
|
|
|
// With `-Bsymbolic` (or when building an executable) as don't need to use
|
|
|
|
// the GOT for symbols that are defined within the current module.
|
|
|
|
if (sym->isDefined() && (!config->shared || config->bsymbolic))
|
|
|
|
return false;
|
|
|
|
return true;
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
2019-07-10 04:45:20 +08:00
|
|
|
static bool allowUndefined(const Symbol* sym) {
|
2021-10-05 06:55:37 +08:00
|
|
|
// Symbols with explicit import names are always allowed to be undefined at
|
|
|
|
// link time.
|
|
|
|
if (sym->importName)
|
|
|
|
return true;
|
|
|
|
if (isa<UndefinedFunction>(sym) && config->importUndefined)
|
|
|
|
return true;
|
|
|
|
|
2020-05-02 00:14:59 +08:00
|
|
|
return config->allowUndefinedSymbols.count(sym->getName()) != 0;
|
2019-07-10 04:45:20 +08:00
|
|
|
}
|
|
|
|
|
2020-05-02 00:14:59 +08:00
|
|
|
static void reportUndefined(Symbol *sym) {
|
|
|
|
if (!allowUndefined(sym)) {
|
|
|
|
switch (config->unresolvedSymbols) {
|
|
|
|
case UnresolvedPolicy::ReportError:
|
|
|
|
error(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
|
|
|
|
break;
|
|
|
|
case UnresolvedPolicy::Warn:
|
|
|
|
warn(toString(sym->getFile()) + ": undefined symbol: " + toString(*sym));
|
|
|
|
break;
|
|
|
|
case UnresolvedPolicy::Ignore:
|
2021-05-28 05:27:10 +08:00
|
|
|
LLVM_DEBUG(dbgs() << "ignoring undefined symbol: " + toString(*sym) +
|
|
|
|
"\n");
|
|
|
|
if (!config->importUndefined) {
|
|
|
|
if (auto *f = dyn_cast<UndefinedFunction>(sym)) {
|
|
|
|
if (!f->stubFunction) {
|
|
|
|
f->stubFunction = symtab->createUndefinedStub(*f->getSignature());
|
|
|
|
f->stubFunction->markLive();
|
|
|
|
// Mark the function itself as a stub which prevents it from being
|
|
|
|
// assigned a table entry.
|
|
|
|
f->isStub = true;
|
|
|
|
}
|
2020-05-02 00:14:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2020-11-17 02:11:37 +08:00
|
|
|
case UnresolvedPolicy::ImportDynamic:
|
|
|
|
break;
|
2020-05-02 00:14:59 +08:00
|
|
|
}
|
|
|
|
}
|
2019-07-10 04:45:20 +08:00
|
|
|
}
|
|
|
|
|
2019-08-14 01:02:02 +08:00
|
|
|
static void addGOTEntry(Symbol *sym) {
|
2020-10-08 05:48:37 +08:00
|
|
|
if (requiresGOTAccess(sym))
|
2019-08-14 01:02:02 +08:00
|
|
|
out.importSec->addGOTEntry(sym);
|
|
|
|
else
|
2020-10-08 05:48:37 +08:00
|
|
|
out.globalSec->addInternalGOTEntry(sym);
|
2019-08-14 01:02:02 +08:00
|
|
|
}
|
|
|
|
|
2019-10-10 13:25:39 +08:00
|
|
|
void scanRelocations(InputChunk *chunk) {
|
2019-05-21 17:13:09 +08:00
|
|
|
if (!chunk->live)
|
|
|
|
return;
|
|
|
|
ObjFile *file = chunk->file;
|
|
|
|
ArrayRef<WasmSignature> types = file->getWasmObj()->types();
|
|
|
|
for (const WasmRelocation &reloc : chunk->getRelocations()) {
|
|
|
|
if (reloc.Type == R_WASM_TYPE_INDEX_LEB) {
|
|
|
|
// Mark target type as live
|
|
|
|
file->typeMap[reloc.Index] =
|
|
|
|
out.typeSec->registerType(types[reloc.Index]);
|
|
|
|
file->typeIsUsed[reloc.Index] = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Other relocation types all have a corresponding symbol
|
|
|
|
Symbol *sym = file->getSymbols()[reloc.Index];
|
|
|
|
|
|
|
|
switch (reloc.Type) {
|
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I64:
|
2019-05-21 17:13:09 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB64:
|
2019-05-21 17:13:09 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB:
|
2021-04-23 07:54:58 +08:00
|
|
|
case R_WASM_TABLE_INDEX_REL_SLEB64:
|
2019-05-21 17:13:09 +08:00
|
|
|
if (requiresGOTAccess(sym))
|
|
|
|
break;
|
|
|
|
out.elemSec->addEntry(cast<FunctionSymbol>(sym));
|
|
|
|
break;
|
|
|
|
case R_WASM_GLOBAL_INDEX_LEB:
|
2020-03-20 10:53:51 +08:00
|
|
|
case R_WASM_GLOBAL_INDEX_I32:
|
2019-05-21 17:13:09 +08:00
|
|
|
if (!isa<GlobalSymbol>(sym))
|
2019-08-14 01:02:02 +08:00
|
|
|
addGOTEntry(sym);
|
2019-05-21 17:13:09 +08:00
|
|
|
break;
|
2020-11-11 09:46:52 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB:
|
2021-07-16 04:24:28 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_TLS_SLEB64:
|
2022-02-12 07:53:28 +08:00
|
|
|
if (!sym->isDefined()) {
|
|
|
|
error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
|
|
|
|
" cannot be used against an undefined symbol `" + toString(*sym) +
|
|
|
|
"`");
|
|
|
|
}
|
2021-05-22 00:58:21 +08:00
|
|
|
// In single-threaded builds TLS is lowered away and TLS data can be
|
|
|
|
// merged with normal data and allowing TLS relocation in non-TLS
|
|
|
|
// segments.
|
|
|
|
if (config->sharedMemory) {
|
2021-09-08 16:53:13 +08:00
|
|
|
if (!sym->isTLS()) {
|
|
|
|
error(toString(file) + ": relocation " +
|
|
|
|
relocTypeToString(reloc.Type) +
|
|
|
|
" cannot be used against non-TLS symbol `" + toString(*sym) +
|
|
|
|
"`");
|
|
|
|
}
|
2021-05-22 00:58:21 +08:00
|
|
|
if (auto *D = dyn_cast<DefinedData>(sym)) {
|
|
|
|
if (!D->segment->outputSeg->isTLS()) {
|
|
|
|
error(toString(file) + ": relocation " +
|
|
|
|
relocTypeToString(reloc.Type) + " cannot be used against `" +
|
|
|
|
toString(*sym) +
|
|
|
|
"` in non-TLS section: " + D->segment->outputSeg->name);
|
|
|
|
}
|
2020-11-11 09:46:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
|
2020-11-17 02:11:37 +08:00
|
|
|
if (config->isPic ||
|
|
|
|
(sym->isUndefined() &&
|
|
|
|
config->unresolvedSymbols == UnresolvedPolicy::ImportDynamic)) {
|
2019-05-21 17:13:09 +08:00
|
|
|
switch (reloc.Type) {
|
|
|
|
case R_WASM_TABLE_INDEX_SLEB:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_SLEB64:
|
2019-05-21 17:13:09 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_SLEB64:
|
|
|
|
case R_WASM_MEMORY_ADDR_LEB64:
|
2019-05-21 17:13:09 +08:00
|
|
|
// Certain relocation types can't be used when building PIC output,
|
|
|
|
// since they would require absolute symbol addresses at link time.
|
|
|
|
error(toString(file) + ": relocation " + relocTypeToString(reloc.Type) +
|
2020-11-17 02:11:37 +08:00
|
|
|
" cannot be used against symbol `" + toString(*sym) +
|
|
|
|
"`; recompile with -fPIC");
|
2019-05-21 17:13:09 +08:00
|
|
|
break;
|
|
|
|
case R_WASM_TABLE_INDEX_I32:
|
2020-07-11 07:51:01 +08:00
|
|
|
case R_WASM_TABLE_INDEX_I64:
|
2019-05-21 17:13:09 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I32:
|
2020-06-06 00:03:12 +08:00
|
|
|
case R_WASM_MEMORY_ADDR_I64:
|
2019-05-21 17:13:09 +08:00
|
|
|
// These relocation types are only present in the data section and
|
|
|
|
// will be converted into code by `generateRelocationCode`. This code
|
2021-10-27 21:52:17 +08:00
|
|
|
// requires the symbols to have GOT entries.
|
2019-05-21 17:13:09 +08:00
|
|
|
if (requiresGOTAccess(sym))
|
2019-08-14 01:02:02 +08:00
|
|
|
addGOTEntry(sym);
|
2019-05-21 17:13:09 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-05-02 06:37:40 +08:00
|
|
|
} else if (sym->isUndefined() && !config->relocatable && !sym->isWeak()) {
|
2019-07-10 04:45:20 +08:00
|
|
|
// Report undefined symbols
|
2021-05-02 06:37:40 +08:00
|
|
|
reportUndefined(sym);
|
2019-05-21 17:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-10 13:25:39 +08:00
|
|
|
|
2023-03-07 01:55:00 +08:00
|
|
|
} // namespace lld::wasm
|