Skip to content

Commit

Permalink
[cling] Inject symbols from libc_nonshared.a
Browse files Browse the repository at this point in the history
These symbols may not be found automatically. See also upstream issue
llvm/llvm-project#61289. This fixes the
test DynamicLibraryManager/cached_realpath.C, approach by Lang Hames.
  • Loading branch information
hahnjo committed Jul 21, 2023
1 parent 974ad05 commit 4b6075b
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions interpreter/cling/lib/Interpreter/IncrementalJIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Target/TargetMachine.h>

#ifdef __linux__
#include <sys/stat.h>
#endif

using namespace llvm;
using namespace llvm::jitlink;
using namespace llvm::orc;
Expand Down Expand Up @@ -489,6 +493,32 @@ CreateTargetMachine(const clang::CompilerInstance& CI, bool JITLink) {

return cantFail(JTMB.createTargetMachine());
}

#if defined(__linux__) && defined(__GLIBC__)
static SymbolMap GetListOfLibcNonsharedSymbols(const LLJIT& Jit) {
// Inject a number of symbols that may be in libc_nonshared.a where they are
// not found automatically. Before DefinitionGenerators in ORCv2, this used
// to be done by RTDyldMemoryManager::getSymbolAddressInProcess See also the
// upstream issue https://github.com/llvm/llvm-project/issues/61289.

static const std::pair<const char*, const void*> NamePtrList[] = {
{"stat", (void*)&stat}, {"fstat", (void*)&fstat},
{"lstat", (void*)&lstat}, {"stat64", (void*)&stat64},
{"fstat64", (void*)&fstat64}, {"lstat64", (void*)&lstat64},
{"fstatat", (void*)&fstatat}, {"fstatat64", (void*)&fstatat64},
{"mknod", (void*)&mknod}, {"mknodat", (void*)&mknodat},
};

SymbolMap LibcNonsharedSymbols;
for (const auto& NamePtr : NamePtrList) {
auto Addr = static_cast<JITTargetAddress>(
reinterpret_cast<uintptr_t>(NamePtr.second));
LibcNonsharedSymbols[Jit.mangleAndIntern(NamePtr.first)] =
JITEvaluatedSymbol(Addr, JITSymbolFlags::Exported);
}
return LibcNonsharedSymbols;
}
#endif
} // unnamed namespace

namespace cling {
Expand Down Expand Up @@ -598,6 +628,12 @@ IncrementalJIT::IncrementalJIT(
return !m_ForbidDlSymbols.contains(*Sym); });
Jit->getMainJITDylib().addGenerator(std::move(LibLookup));

#if defined(__linux__) && defined(__GLIBC__)
// See comment in ListOfLibcNonsharedSymbols.
cantFail(Jit->getMainJITDylib().define(
absoluteSymbols(GetListOfLibcNonsharedSymbols(*Jit))));
#endif

// This replaces llvm::orc::ExecutionSession::logErrorsToStdErr:
auto&& ErrorReporter = [&Executor, LinkerPrefix, Verbose](Error Err) {
Err = handleErrors(std::move(Err),
Expand Down

0 comments on commit 4b6075b

Please sign in to comment.