2016-08-05 04:32:37 +08:00
|
|
|
//===----------- JITSymbol.cpp - JITSymbol class implementation -----------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2016-08-05 04:32:37 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// JITSymbol class implementation plus helper functions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/ExecutionEngine/JITSymbol.h"
|
2018-08-02 06:42:23 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
2018-09-30 07:49:57 +08:00
|
|
|
#include "llvm/IR/GlobalAlias.h"
|
2016-08-05 04:32:37 +08:00
|
|
|
#include "llvm/IR/GlobalValue.h"
|
2018-08-02 06:42:23 +08:00
|
|
|
#include "llvm/Object/ObjectFile.h"
|
2016-08-05 04:32:37 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
JITSymbolFlags llvm::JITSymbolFlags::fromGlobalValue(const GlobalValue &GV) {
|
|
|
|
JITSymbolFlags Flags = JITSymbolFlags::None;
|
2016-08-07 06:36:26 +08:00
|
|
|
if (GV.hasWeakLinkage() || GV.hasLinkOnceLinkage())
|
2016-08-05 04:32:37 +08:00
|
|
|
Flags |= JITSymbolFlags::Weak;
|
|
|
|
if (GV.hasCommonLinkage())
|
|
|
|
Flags |= JITSymbolFlags::Common;
|
|
|
|
if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility())
|
|
|
|
Flags |= JITSymbolFlags::Exported;
|
2018-09-30 07:49:57 +08:00
|
|
|
|
2018-08-02 06:42:23 +08:00
|
|
|
if (isa<Function>(GV))
|
|
|
|
Flags |= JITSymbolFlags::Callable;
|
2018-09-30 07:49:57 +08:00
|
|
|
else if (isa<GlobalAlias>(GV) &&
|
|
|
|
isa<Function>(cast<GlobalAlias>(GV).getAliasee()))
|
|
|
|
Flags |= JITSymbolFlags::Callable;
|
|
|
|
|
2016-08-05 04:32:37 +08:00
|
|
|
return Flags;
|
|
|
|
}
|
|
|
|
|
2018-08-02 06:42:23 +08:00
|
|
|
Expected<JITSymbolFlags>
|
|
|
|
llvm::JITSymbolFlags::fromObjectSymbol(const object::SymbolRef &Symbol) {
|
2016-08-05 04:32:37 +08:00
|
|
|
JITSymbolFlags Flags = JITSymbolFlags::None;
|
|
|
|
if (Symbol.getFlags() & object::BasicSymbolRef::SF_Weak)
|
|
|
|
Flags |= JITSymbolFlags::Weak;
|
|
|
|
if (Symbol.getFlags() & object::BasicSymbolRef::SF_Common)
|
|
|
|
Flags |= JITSymbolFlags::Common;
|
|
|
|
if (Symbol.getFlags() & object::BasicSymbolRef::SF_Exported)
|
|
|
|
Flags |= JITSymbolFlags::Exported;
|
2018-08-02 06:42:23 +08:00
|
|
|
|
|
|
|
auto SymbolType = Symbol.getType();
|
|
|
|
if (!SymbolType)
|
|
|
|
return SymbolType.takeError();
|
|
|
|
|
|
|
|
if (*SymbolType & object::SymbolRef::ST_Function)
|
|
|
|
Flags |= JITSymbolFlags::Callable;
|
|
|
|
|
2016-08-05 04:32:37 +08:00
|
|
|
return Flags;
|
|
|
|
}
|
2017-08-10 04:19:27 +08:00
|
|
|
|
2018-08-02 06:42:23 +08:00
|
|
|
ARMJITSymbolFlags
|
|
|
|
llvm::ARMJITSymbolFlags::fromObjectSymbol(const object::SymbolRef &Symbol) {
|
2017-08-10 04:19:27 +08:00
|
|
|
ARMJITSymbolFlags Flags;
|
|
|
|
if (Symbol.getFlags() & object::BasicSymbolRef::SF_Thumb)
|
|
|
|
Flags |= ARMJITSymbolFlags::Thumb;
|
|
|
|
return Flags;
|
|
|
|
}
|
2018-01-20 06:24:13 +08:00
|
|
|
|
2018-05-02 00:10:38 +08:00
|
|
|
/// Performs lookup by, for each symbol, first calling
|
2018-01-20 06:24:13 +08:00
|
|
|
/// findSymbolInLogicalDylib and if that fails calling
|
|
|
|
/// findSymbol.
|
2018-09-26 03:48:46 +08:00
|
|
|
void LegacyJITSymbolResolver::lookup(const LookupSet &Symbols,
|
|
|
|
OnResolvedFunction OnResolved) {
|
2018-01-20 06:24:13 +08:00
|
|
|
JITSymbolResolver::LookupResult Result;
|
|
|
|
for (auto &Symbol : Symbols) {
|
|
|
|
std::string SymName = Symbol.str();
|
|
|
|
if (auto Sym = findSymbolInLogicalDylib(SymName)) {
|
|
|
|
if (auto AddrOrErr = Sym.getAddress())
|
|
|
|
Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
|
2018-09-26 03:48:46 +08:00
|
|
|
else {
|
|
|
|
OnResolved(AddrOrErr.takeError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (auto Err = Sym.takeError()) {
|
|
|
|
OnResolved(std::move(Err));
|
|
|
|
return;
|
|
|
|
} else {
|
2018-01-20 06:24:13 +08:00
|
|
|
// findSymbolInLogicalDylib failed. Lets try findSymbol.
|
|
|
|
if (auto Sym = findSymbol(SymName)) {
|
|
|
|
if (auto AddrOrErr = Sym.getAddress())
|
|
|
|
Result[Symbol] = JITEvaluatedSymbol(*AddrOrErr, Sym.getFlags());
|
2018-09-26 03:48:46 +08:00
|
|
|
else {
|
|
|
|
OnResolved(AddrOrErr.takeError());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (auto Err = Sym.takeError()) {
|
|
|
|
OnResolved(std::move(Err));
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
OnResolved(make_error<StringError>("Symbol not found: " + Symbol,
|
|
|
|
inconvertibleErrorCode()));
|
|
|
|
return;
|
|
|
|
}
|
2018-01-20 06:24:13 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-26 03:48:46 +08:00
|
|
|
OnResolved(std::move(Result));
|
2018-01-20 06:24:13 +08:00
|
|
|
}
|
|
|
|
|
2018-05-02 00:10:38 +08:00
|
|
|
/// Performs flags lookup by calling findSymbolInLogicalDylib and
|
2018-01-20 06:24:13 +08:00
|
|
|
/// returning the flags value for that symbol.
|
2018-08-29 05:18:05 +08:00
|
|
|
Expected<JITSymbolResolver::LookupSet>
|
|
|
|
LegacyJITSymbolResolver::getResponsibilitySet(const LookupSet &Symbols) {
|
|
|
|
JITSymbolResolver::LookupSet Result;
|
2018-01-20 06:24:13 +08:00
|
|
|
|
|
|
|
for (auto &Symbol : Symbols) {
|
|
|
|
std::string SymName = Symbol.str();
|
2018-08-29 05:18:05 +08:00
|
|
|
if (auto Sym = findSymbolInLogicalDylib(SymName)) {
|
|
|
|
// If there's an existing def but it is not strong, then the caller is
|
|
|
|
// responsible for it.
|
|
|
|
if (!Sym.getFlags().isStrong())
|
|
|
|
Result.insert(Symbol);
|
|
|
|
} else if (auto Err = Sym.takeError())
|
2018-01-20 06:24:13 +08:00
|
|
|
return std::move(Err);
|
2018-08-29 05:18:05 +08:00
|
|
|
else {
|
|
|
|
// If there is no existing definition then the caller is responsible for
|
|
|
|
// it.
|
|
|
|
Result.insert(Symbol);
|
|
|
|
}
|
2018-01-20 06:24:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return std::move(Result);
|
|
|
|
}
|