mirror of
https://github.com/capstone-engine/llvm-capstone.git
synced 2025-01-15 12:39:19 +00:00
[ExecutionEngine] Refactor - Roll JITSymbolFlags functionality into JITSymbol.h
and remove the JITSymbolFlags header. llvm-svn: 277766
This commit is contained in:
parent
f93082e71a
commit
aac59a26a5
@ -14,27 +14,81 @@
|
||||
#ifndef LLVM_EXECUTIONENGINE_JITSYMBOL_H
|
||||
#define LLVM_EXECUTIONENGINE_JITSYMBOL_H
|
||||
|
||||
#include "llvm/ExecutionEngine/JITSymbolFlags.h"
|
||||
#include "llvm/Support/DataTypes.h"
|
||||
#include <cassert>
|
||||
#include <functional>
|
||||
|
||||
namespace llvm {
|
||||
|
||||
class GlobalValue;
|
||||
|
||||
namespace object {
|
||||
class BasicSymbolRef;
|
||||
}
|
||||
|
||||
/// @brief Represents an address in the target process's address space.
|
||||
typedef uint64_t JITTargetAddress;
|
||||
|
||||
/// @brief Flags for symbols in the JIT.
|
||||
class JITSymbolFlags {
|
||||
public:
|
||||
|
||||
typedef uint8_t UnderlyingType;
|
||||
|
||||
enum FlagNames : UnderlyingType {
|
||||
None = 0,
|
||||
Weak = 1U << 0,
|
||||
Common = 1U << 1,
|
||||
Absolute = 1U << 2,
|
||||
Exported = 1U << 3
|
||||
};
|
||||
|
||||
/// @brief Default-construct a JITSymbolFlags instance.
|
||||
JITSymbolFlags() : Flags(None) {}
|
||||
|
||||
/// @brief Construct a JITSymbolFlags instance from the given flags.
|
||||
JITSymbolFlags(FlagNames Flags) : Flags(Flags) {}
|
||||
|
||||
/// @brief Returns true is the Weak flag is set.
|
||||
bool isWeak() const {
|
||||
return (Flags & Weak) == Weak;
|
||||
}
|
||||
|
||||
/// @brief Returns true is the Weak flag is set.
|
||||
bool isCommon() const {
|
||||
return (Flags & Common) == Common;
|
||||
}
|
||||
|
||||
/// @brief Returns true is the Weak flag is set.
|
||||
bool isExported() const {
|
||||
return (Flags & Exported) == Exported;
|
||||
}
|
||||
|
||||
operator UnderlyingType&() { return Flags; }
|
||||
|
||||
/// Construct a JITSymbolFlags value based on the flags of the given global
|
||||
/// value.
|
||||
static JITSymbolFlags fromGlobalValue(const GlobalValue &GV);
|
||||
|
||||
/// Construct a JITSymbolFlags value based on the flags of the given libobject
|
||||
/// symbol.
|
||||
static JITSymbolFlags fromObjectSymbol(const object::BasicSymbolRef &Symbol);
|
||||
|
||||
private:
|
||||
UnderlyingType Flags;
|
||||
};
|
||||
|
||||
/// @brief Represents a symbol that has been evaluated to an address already.
|
||||
class JITEvaluatedSymbol : public JITSymbolBase {
|
||||
class JITEvaluatedSymbol {
|
||||
public:
|
||||
|
||||
/// @brief Create a 'null' symbol.
|
||||
JITEvaluatedSymbol(std::nullptr_t)
|
||||
: JITSymbolBase(JITSymbolFlags::None), Address(0) {}
|
||||
: Address(0) {}
|
||||
|
||||
/// @brief Create a symbol for the given address and flags.
|
||||
JITEvaluatedSymbol(JITTargetAddress Address, JITSymbolFlags Flags)
|
||||
: JITSymbolBase(Flags), Address(Address) {}
|
||||
: Address(Address), Flags(Flags) {}
|
||||
|
||||
/// @brief An evaluated symbol converts to 'true' if its address is non-zero.
|
||||
explicit operator bool() const { return Address != 0; }
|
||||
@ -42,12 +96,16 @@ public:
|
||||
/// @brief Return the address of this symbol.
|
||||
JITTargetAddress getAddress() const { return Address; }
|
||||
|
||||
/// @brief Return the flags for this symbol.
|
||||
JITSymbolFlags getFlags() const { return Flags; }
|
||||
|
||||
private:
|
||||
JITTargetAddress Address;
|
||||
JITSymbolFlags Flags;
|
||||
};
|
||||
|
||||
/// @brief Represents a symbol in the JIT.
|
||||
class JITSymbol : public JITSymbolBase {
|
||||
class JITSymbol {
|
||||
public:
|
||||
|
||||
typedef std::function<JITTargetAddress()> GetAddressFtor;
|
||||
@ -55,15 +113,15 @@ public:
|
||||
/// @brief Create a 'null' symbol that represents failure to find a symbol
|
||||
/// definition.
|
||||
JITSymbol(std::nullptr_t)
|
||||
: JITSymbolBase(JITSymbolFlags::None), CachedAddr(0) {}
|
||||
: CachedAddr(0) {}
|
||||
|
||||
/// @brief Create a symbol for a definition with a known address.
|
||||
JITSymbol(JITTargetAddress Addr, JITSymbolFlags Flags)
|
||||
: JITSymbolBase(Flags), CachedAddr(Addr) {}
|
||||
: CachedAddr(Addr), Flags(Flags) {}
|
||||
|
||||
/// @brief Construct a JITSymbol from a JITEvaluatedSymbol.
|
||||
JITSymbol(JITEvaluatedSymbol Sym)
|
||||
: JITSymbolBase(Sym.getFlags()), CachedAddr(Sym.getAddress()) {}
|
||||
: CachedAddr(Sym.getAddress()), Flags(Sym.getFlags()) {}
|
||||
|
||||
/// @brief Create a symbol for a definition that doesn't have a known address
|
||||
/// yet.
|
||||
@ -75,7 +133,7 @@ public:
|
||||
/// user can materialize the definition at any time by calling the getAddress
|
||||
/// method.
|
||||
JITSymbol(GetAddressFtor GetAddress, JITSymbolFlags Flags)
|
||||
: JITSymbolBase(Flags), GetAddress(std::move(GetAddress)), CachedAddr(0) {}
|
||||
: GetAddress(std::move(GetAddress)), CachedAddr(0), Flags(Flags) {}
|
||||
|
||||
/// @brief Returns true if the symbol exists, false otherwise.
|
||||
explicit operator bool() const { return CachedAddr || GetAddress; }
|
||||
@ -91,9 +149,12 @@ public:
|
||||
return CachedAddr;
|
||||
}
|
||||
|
||||
JITSymbolFlags getFlags() const { return Flags; }
|
||||
|
||||
private:
|
||||
GetAddressFtor GetAddress;
|
||||
JITTargetAddress CachedAddr;
|
||||
JITSymbolFlags Flags;
|
||||
};
|
||||
|
||||
/// \brief Symbol resolution.
|
||||
|
@ -1,100 +0,0 @@
|
||||
//===------ JITSymbolFlags.h - Flags for symbols in the JIT -----*- C++ -*-===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// Symbol flags for symbols in the JIT (e.g. weak, exported).
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#ifndef LLVM_EXECUTIONENGINE_JITSYMBOLFLAGS_H
|
||||
#define LLVM_EXECUTIONENGINE_JITSYMBOLFLAGS_H
|
||||
|
||||
#include "llvm/IR/GlobalValue.h"
|
||||
#include "llvm/Object/SymbolicFile.h"
|
||||
|
||||
namespace llvm {
|
||||
|
||||
/// @brief Flags for symbols in the JIT.
|
||||
enum class JITSymbolFlags : char {
|
||||
None = 0,
|
||||
Weak = 1U << 0,
|
||||
Common = 1U << 1,
|
||||
Exported = 1U << 2
|
||||
};
|
||||
|
||||
inline JITSymbolFlags operator|(JITSymbolFlags LHS, JITSymbolFlags RHS) {
|
||||
typedef std::underlying_type<JITSymbolFlags>::type UT;
|
||||
return static_cast<JITSymbolFlags>(
|
||||
static_cast<UT>(LHS) | static_cast<UT>(RHS));
|
||||
}
|
||||
|
||||
inline JITSymbolFlags& operator |=(JITSymbolFlags &LHS, JITSymbolFlags RHS) {
|
||||
LHS = LHS | RHS;
|
||||
return LHS;
|
||||
}
|
||||
|
||||
inline JITSymbolFlags operator&(JITSymbolFlags LHS, JITSymbolFlags RHS) {
|
||||
typedef std::underlying_type<JITSymbolFlags>::type UT;
|
||||
return static_cast<JITSymbolFlags>(
|
||||
static_cast<UT>(LHS) & static_cast<UT>(RHS));
|
||||
}
|
||||
|
||||
inline JITSymbolFlags& operator &=(JITSymbolFlags &LHS, JITSymbolFlags RHS) {
|
||||
LHS = LHS & RHS;
|
||||
return LHS;
|
||||
}
|
||||
|
||||
/// @brief Base class for symbols in the JIT.
|
||||
class JITSymbolBase {
|
||||
public:
|
||||
JITSymbolBase(JITSymbolFlags Flags) : Flags(Flags) {}
|
||||
|
||||
JITSymbolFlags getFlags() const { return Flags; }
|
||||
|
||||
bool isWeak() const {
|
||||
return (Flags & JITSymbolFlags::Weak) == JITSymbolFlags::Weak;
|
||||
}
|
||||
|
||||
bool isCommon() const {
|
||||
return (Flags & JITSymbolFlags::Common) == JITSymbolFlags::Common;
|
||||
}
|
||||
|
||||
bool isExported() const {
|
||||
return (Flags & JITSymbolFlags::Exported) == JITSymbolFlags::Exported;
|
||||
}
|
||||
|
||||
static JITSymbolFlags flagsFromGlobalValue(const GlobalValue &GV) {
|
||||
JITSymbolFlags Flags = JITSymbolFlags::None;
|
||||
if (GV.hasWeakLinkage())
|
||||
Flags |= JITSymbolFlags::Weak;
|
||||
if (GV.hasCommonLinkage())
|
||||
Flags |= JITSymbolFlags::Common;
|
||||
if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility())
|
||||
Flags |= JITSymbolFlags::Exported;
|
||||
return Flags;
|
||||
}
|
||||
|
||||
static JITSymbolFlags
|
||||
flagsFromObjectSymbol(const object::BasicSymbolRef &Symbol) {
|
||||
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;
|
||||
return Flags;
|
||||
}
|
||||
|
||||
private:
|
||||
JITSymbolFlags Flags;
|
||||
};
|
||||
|
||||
} // end namespace llvm
|
||||
|
||||
#endif
|
@ -297,7 +297,7 @@ private:
|
||||
auto CCInfo = CompileCallbackMgr.getCompileCallback();
|
||||
StubInits[mangle(F.getName(), DL)] =
|
||||
std::make_pair(CCInfo.getAddress(),
|
||||
JITSymbolBase::flagsFromGlobalValue(F));
|
||||
JITSymbolFlags::fromGlobalValue(F));
|
||||
CCInfo.setCompileAction([this, &LD, LMH, &F]() {
|
||||
return this->extractAndCompile(LD, LMH, F);
|
||||
});
|
||||
|
@ -271,7 +271,7 @@ public:
|
||||
auto StubTargetAddr =
|
||||
static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(StubAddr));
|
||||
auto StubSymbol = JITSymbol(StubTargetAddr, I->second.second);
|
||||
if (ExportedStubsOnly && !StubSymbol.isExported())
|
||||
if (ExportedStubsOnly && !StubSymbol.getFlags().isExported())
|
||||
return nullptr;
|
||||
return StubSymbol;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ private:
|
||||
// (a StringRef) may go away before the lambda is executed.
|
||||
// FIXME: Use capture-init when we move to C++14.
|
||||
std::string PName = Name;
|
||||
JITSymbolFlags Flags = JITSymbolBase::flagsFromGlobalValue(*GV);
|
||||
JITSymbolFlags Flags = JITSymbolFlags::fromGlobalValue(*GV);
|
||||
auto GetAddress =
|
||||
[this, ExportedSymbolsOnly, PName, &B]() -> JITTargetAddress {
|
||||
if (this->EmitState == Emitting)
|
||||
|
@ -52,7 +52,7 @@ protected:
|
||||
auto SymEntry = SymbolTable.find(Name);
|
||||
if (SymEntry == SymbolTable.end())
|
||||
return nullptr;
|
||||
if (!SymEntry->second.isExported() && ExportedSymbolsOnly)
|
||||
if (!SymEntry->second.getFlags().isExported() && ExportedSymbolsOnly)
|
||||
return nullptr;
|
||||
if (!Finalized)
|
||||
return JITSymbol(getSymbolMaterializer(Name),
|
||||
@ -163,7 +163,7 @@ private:
|
||||
consumeError(SymbolName.takeError());
|
||||
continue;
|
||||
}
|
||||
auto Flags = JITSymbol::flagsFromObjectSymbol(Symbol);
|
||||
auto Flags = JITSymbolFlags::fromObjectSymbol(Symbol);
|
||||
SymbolTable.insert(
|
||||
std::make_pair(*SymbolName, JITEvaluatedSymbol(0, Flags)));
|
||||
}
|
||||
|
@ -477,7 +477,7 @@ public:
|
||||
auto Key = I->second.first;
|
||||
auto Flags = I->second.second;
|
||||
auto StubSymbol = JITSymbol(getStubAddr(Key), Flags);
|
||||
if (ExportedStubsOnly && !StubSymbol.isExported())
|
||||
if (ExportedStubsOnly && !StubSymbol.getFlags().isExported())
|
||||
return nullptr;
|
||||
return StubSymbol;
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ add_llvm_library(LLVMExecutionEngine
|
||||
ExecutionEngine.cpp
|
||||
ExecutionEngineBindings.cpp
|
||||
GDBRegistrationListener.cpp
|
||||
JITSymbol.cpp
|
||||
SectionMemoryManager.cpp
|
||||
TargetSelect.cpp
|
||||
|
||||
|
41
llvm/lib/ExecutionEngine/JITSymbol.cpp
Normal file
41
llvm/lib/ExecutionEngine/JITSymbol.cpp
Normal file
@ -0,0 +1,41 @@
|
||||
//===----------- JITSymbol.cpp - JITSymbol class implementation -----------===//
|
||||
//
|
||||
// The LLVM Compiler Infrastructure
|
||||
//
|
||||
// This file is distributed under the University of Illinois Open Source
|
||||
// License. See LICENSE.TXT for details.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// JITSymbol class implementation plus helper functions.
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "llvm/ExecutionEngine/JITSymbol.h"
|
||||
#include "llvm/IR/GlobalValue.h"
|
||||
#include "llvm/Object/SymbolicFile.h"
|
||||
|
||||
using namespace llvm;
|
||||
|
||||
JITSymbolFlags llvm::JITSymbolFlags::fromGlobalValue(const GlobalValue &GV) {
|
||||
JITSymbolFlags Flags = JITSymbolFlags::None;
|
||||
if (GV.hasWeakLinkage())
|
||||
Flags |= JITSymbolFlags::Weak;
|
||||
if (GV.hasCommonLinkage())
|
||||
Flags |= JITSymbolFlags::Common;
|
||||
if (!GV.hasLocalLinkage() && !GV.hasHiddenVisibility())
|
||||
Flags |= JITSymbolFlags::Exported;
|
||||
return Flags;
|
||||
}
|
||||
|
||||
JITSymbolFlags
|
||||
llvm::JITSymbolFlags::fromObjectSymbol(const object::BasicSymbolRef &Symbol) {
|
||||
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;
|
||||
return Flags;
|
||||
}
|
@ -591,7 +591,7 @@ Error RuntimeDyldImpl::emitCommonSymbols(const ObjectFile &Obj,
|
||||
}
|
||||
|
||||
if (auto Sym = Resolver.findSymbolInLogicalDylib(Name)) {
|
||||
if (!Sym.isCommon()) {
|
||||
if (!Sym.getFlags().isCommon()) {
|
||||
DEBUG(dbgs() << "\tSkipping common symbol '" << Name
|
||||
<< "' in favor of stronger definition.\n");
|
||||
continue;
|
||||
|
@ -199,20 +199,23 @@ public:
|
||||
};
|
||||
|
||||
/// @brief Symbol info for RuntimeDyld.
|
||||
class SymbolTableEntry : public JITSymbolBase {
|
||||
class SymbolTableEntry {
|
||||
public:
|
||||
SymbolTableEntry()
|
||||
: JITSymbolBase(JITSymbolFlags::None), Offset(0), SectionID(0) {}
|
||||
: Offset(0), SectionID(0) {}
|
||||
|
||||
SymbolTableEntry(unsigned SectionID, uint64_t Offset, JITSymbolFlags Flags)
|
||||
: JITSymbolBase(Flags), Offset(Offset), SectionID(SectionID) {}
|
||||
: Offset(Offset), SectionID(SectionID), Flags(Flags) {}
|
||||
|
||||
unsigned getSectionID() const { return SectionID; }
|
||||
uint64_t getOffset() const { return Offset; }
|
||||
|
||||
JITSymbolFlags getFlags() const { return Flags; }
|
||||
|
||||
private:
|
||||
uint64_t Offset;
|
||||
unsigned SectionID;
|
||||
JITSymbolFlags Flags;
|
||||
};
|
||||
|
||||
typedef StringMap<SymbolTableEntry> RTDyldSymbolTable;
|
||||
|
Loading…
x
Reference in New Issue
Block a user