CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Static Public Member Functions | List of all members
CppInternal::DynamicLibraryManager Class Reference

A helper class managing dynamic shared objects. More...

#include "lib/CppInterOp/DynamicLibraryManager.h"

Classes

struct  SearchPathInfo
 Describes the library search paths. More...
 

Public Types

enum  LoadLibResult {
  kLoadLibSuccess , kLoadLibAlreadyLoaded , kLoadLibNotFound , kLoadLibLoadError ,
  kLoadLibNumResults
}
 Describes the result of loading a library. More...
 
using SearchPathInfos = llvm::SmallVector< SearchPathInfo, 32 >
 

Public Member Functions

 DynamicLibraryManager ()
 
 ~DynamicLibraryManager ()
 
 DynamicLibraryManager (const DynamicLibraryManager &)=delete
 
DynamicLibraryManageroperator= (const DynamicLibraryManager &)=delete
 
InterpreterCallbacks * getCallbacks ()
 
const InterpreterCallbacks * getCallbacks () const
 
void setCallbacks (InterpreterCallbacks *C)
 
const SearchPathInfosgetSearchPaths () const
 Returns the system include paths.
 
void addSearchPath (llvm::StringRef dir, bool isUser=true, bool prepend=false)
 
std::string lookupLibrary (llvm::StringRef libStem, llvm::SmallVector< llvm::StringRef, 2 > RPath={}, llvm::SmallVector< llvm::StringRef, 2 > RunPath={}, llvm::StringRef libLoader="", bool variateLibStem=true) const
 Looks up a library taking into account the current include paths and the system include paths.
 
LoadLibResult loadLibrary (llvm::StringRef, bool permanent, bool resolved=false)
 Loads a shared library.
 
void unloadLibrary (llvm::StringRef libStem)
 
bool isLibraryLoaded (llvm::StringRef fullPath) const
 Returns true if the file was a dynamic library and it was already loaded.
 
void initializeDyld (std::function< bool(llvm::StringRef)> shouldPermanentlyIgnore)
 Initialize the dyld.
 
std::string searchLibrariesForSymbol (llvm::StringRef mangledName, bool searchSystem=true) const
 Find the first not-yet-loaded shared object that contains the symbol.
 
void dump (llvm::raw_ostream *S=nullptr) const
 

Static Public Member Functions

template<class T >
static std::string getSymbolLocation (T func)
 On a success returns to full path to a shared object that holds the symbol pointed by func.
 
static std::string normalizePath (llvm::StringRef path)
 
static bool isSharedLibrary (llvm::StringRef libFullPath, bool *exists=nullptr)
 Returns true if file is a shared library.
 

Detailed Description

A helper class managing dynamic shared objects.

Definition at line 26 of file DynamicLibraryManager.h.

Member Typedef Documentation

◆ SearchPathInfos

Definition at line 52 of file DynamicLibraryManager.h.

Member Enumeration Documentation

◆ LoadLibResult

Describes the result of loading a library.

Enumerator
kLoadLibSuccess 

library loaded successfully

kLoadLibAlreadyLoaded 

library was already loaded

kLoadLibNotFound 

library was not found

kLoadLibLoadError 

loading the library failed

kLoadLibNumResults 

Definition at line 30 of file DynamicLibraryManager.h.

Constructor & Destructor Documentation

◆ DynamicLibraryManager() [1/2]

CppInternal::DynamicLibraryManager::DynamicLibraryManager ( )

◆ ~DynamicLibraryManager()

CppInternal::DynamicLibraryManager::~DynamicLibraryManager ( )

Definition at line 1306 of file DynamicLibraryManagerSymbol.cpp.

◆ DynamicLibraryManager() [2/2]

CppInternal::DynamicLibraryManager::DynamicLibraryManager ( const DynamicLibraryManager )
delete

Member Function Documentation

◆ addSearchPath()

void CppInternal::DynamicLibraryManager::addSearchPath ( llvm::StringRef  dir,
bool  isUser = true,
bool  prepend = false 
)
inline

Definition at line 129 of file DynamicLibraryManager.h.

Referenced by Cpp::AddSearchPath(), and DynamicLibraryManager().

◆ dump()

void CppInternal::DynamicLibraryManager::dump ( llvm::raw_ostream *  S = nullptr) const

Definition at line 425 of file DynamicLibraryManager.cpp.

References getSearchPaths().

◆ getCallbacks() [1/2]

InterpreterCallbacks * CppInternal::DynamicLibraryManager::getCallbacks ( )
inline

Definition at line 119 of file DynamicLibraryManager.h.

◆ getCallbacks() [2/2]

const InterpreterCallbacks * CppInternal::DynamicLibraryManager::getCallbacks ( ) const
inline

Definition at line 120 of file DynamicLibraryManager.h.

◆ getSearchPaths()

const SearchPathInfos & CppInternal::DynamicLibraryManager::getSearchPaths ( ) const
inline

Returns the system include paths.

Returns
System include paths.

Definition at line 127 of file DynamicLibraryManager.h.

Referenced by dump().

◆ getSymbolLocation()

template<class T >
static std::string CppInternal::DynamicLibraryManager::getSymbolLocation ( func)
inlinestatic

On a success returns to full path to a shared object that holds the symbol pointed by func.

Definition at line 205 of file DynamicLibraryManager.h.

◆ initializeDyld()

void CppInternal::DynamicLibraryManager::initializeDyld ( std::function< bool(llvm::StringRef)>  shouldPermanentlyIgnore)

Initialize the dyld.

Parameters
[in]shouldPermanentlyIgnore- a callback deciding if a library should be ignored from the result set. Useful for ignoring dangerous libraries such as the ones overriding malloc.

Definition at line 1311 of file DynamicLibraryManagerSymbol.cpp.

References CppInternal::GetExecutablePath().

◆ isLibraryLoaded()

bool CppInternal::DynamicLibraryManager::isLibraryLoaded ( llvm::StringRef  fullPath) const

Returns true if the file was a dynamic library and it was already loaded.

Definition at line 418 of file DynamicLibraryManager.cpp.

References normalizePath().

Referenced by unloadLibrary().

◆ isSharedLibrary()

bool CppInternal::DynamicLibraryManager::isSharedLibrary ( llvm::StringRef  libFullPath,
bool *  exists = nullptr 
)
static

Returns true if file is a shared library.

Parameters
[in]libFullPath- the full path to file.
[out]exists- sets if the file exists. Useful to distinguish if it is a library but of incompatible file format.

Definition at line 451 of file DynamicLibraryManager.cpp.

Referenced by lookupLibrary().

◆ loadLibrary()

DynamicLibraryManager::LoadLibResult CppInternal::DynamicLibraryManager::loadLibrary ( llvm::StringRef  ,
bool  permanent,
bool  resolved = false 
)

Loads a shared library.

Parameters
[in]libStem- The file to load.
[in]permanent- If false, the file can be unloaded later.
[in]resolved- Whether libStem is an absolute path or resolved from a previous call to DynamicLibraryManager::lookupLibrary
Returns
kLoadLibSuccess on success, kLoadLibAlreadyLoaded if the library was already loaded, kLoadLibError if the library cannot be found or any other error was encountered.

Definition at line 345 of file DynamicLibraryManager.cpp.

References CppInternal::utils::platform::DLOpen(), kLoadLibAlreadyLoaded, kLoadLibLoadError, kLoadLibNotFound, kLoadLibSuccess, and lookupLibrary().

Referenced by CppInternal::Interpreter::loadLibrary().

◆ lookupLibrary()

std::string CppInternal::DynamicLibraryManager::lookupLibrary ( llvm::StringRef  libStem,
llvm::SmallVector< llvm::StringRef, 2 >  RPath = {},
llvm::SmallVector< llvm::StringRef, 2 >  RunPath = {},
llvm::StringRef  libLoader = "",
bool  variateLibStem = true 
) const

Looks up a library taking into account the current include paths and the system include paths.

See more information for RPATH and RUNPATH: https://en.wikipedia.org/wiki/Rpath

Parameters
[in]libStem- The filename being looked up
[in]RPath- RPATH as provided by loader library, searching for libStem
[in]RunPath- RUNPATH as provided by loader library, searching for libStem
[in]libLoader- The library that loads libStem. Use "" for main program.
[in]variateLibStem- If this param is true, and libStem is "L", then we search for "L", "libL", "L.so", "libL.so"", etc.
Returns
the canonical path to the file or empty string if not found

Definition at line 283 of file DynamicLibraryManager.cpp.

References isSharedLibrary(), CppInternal::utils::platform::NormalizePath(), normalizePath(), CppInternal::RPathToStr2(), CppInternal::substAll(), and CppInternal::substFront().

Referenced by CppInternal::Interpreter::loadLibrary(), loadLibrary(), and unloadLibrary().

◆ normalizePath()

std::string CppInternal::DynamicLibraryManager::normalizePath ( llvm::StringRef  path)
static

◆ operator=()

DynamicLibraryManager & CppInternal::DynamicLibraryManager::operator= ( const DynamicLibraryManager )
delete

◆ searchLibrariesForSymbol()

std::string CppInternal::DynamicLibraryManager::searchLibrariesForSymbol ( llvm::StringRef  mangledName,
bool  searchSystem = true 
) const

Find the first not-yet-loaded shared object that contains the symbol.

Parameters
[in]mangledName- the mangled name to look for.
[in]searchSystem- whether to descend into system libraries.
Returns
the library name if found, and empty string otherwise.

Definition at line 1324 of file DynamicLibraryManagerSymbol.cpp.

◆ setCallbacks()

void CppInternal::DynamicLibraryManager::setCallbacks ( InterpreterCallbacks *  C)
inline

Definition at line 121 of file DynamicLibraryManager.h.

◆ unloadLibrary()

void CppInternal::DynamicLibraryManager::unloadLibrary ( llvm::StringRef  libStem)

The documentation for this class was generated from the following files: