Skip to content

Commit 4b6075b

Browse files
committed
[cling] Inject symbols from libc_nonshared.a
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.
1 parent 974ad05 commit 4b6075b

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

interpreter/cling/lib/Interpreter/IncrementalJIT.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
#include <llvm/Support/TargetRegistry.h>
3232
#include <llvm/Target/TargetMachine.h>
3333

34+
#ifdef __linux__
35+
#include <sys/stat.h>
36+
#endif
37+
3438
using namespace llvm;
3539
using namespace llvm::jitlink;
3640
using namespace llvm::orc;
@@ -489,6 +493,32 @@ CreateTargetMachine(const clang::CompilerInstance& CI, bool JITLink) {
489493

490494
return cantFail(JTMB.createTargetMachine());
491495
}
496+
497+
#if defined(__linux__) && defined(__GLIBC__)
498+
static SymbolMap GetListOfLibcNonsharedSymbols(const LLJIT& Jit) {
499+
// Inject a number of symbols that may be in libc_nonshared.a where they are
500+
// not found automatically. Before DefinitionGenerators in ORCv2, this used
501+
// to be done by RTDyldMemoryManager::getSymbolAddressInProcess See also the
502+
// upstream issue https://github.com/llvm/llvm-project/issues/61289.
503+
504+
static const std::pair<const char*, const void*> NamePtrList[] = {
505+
{"stat", (void*)&stat}, {"fstat", (void*)&fstat},
506+
{"lstat", (void*)&lstat}, {"stat64", (void*)&stat64},
507+
{"fstat64", (void*)&fstat64}, {"lstat64", (void*)&lstat64},
508+
{"fstatat", (void*)&fstatat}, {"fstatat64", (void*)&fstatat64},
509+
{"mknod", (void*)&mknod}, {"mknodat", (void*)&mknodat},
510+
};
511+
512+
SymbolMap LibcNonsharedSymbols;
513+
for (const auto& NamePtr : NamePtrList) {
514+
auto Addr = static_cast<JITTargetAddress>(
515+
reinterpret_cast<uintptr_t>(NamePtr.second));
516+
LibcNonsharedSymbols[Jit.mangleAndIntern(NamePtr.first)] =
517+
JITEvaluatedSymbol(Addr, JITSymbolFlags::Exported);
518+
}
519+
return LibcNonsharedSymbols;
520+
}
521+
#endif
492522
} // unnamed namespace
493523

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

631+
#if defined(__linux__) && defined(__GLIBC__)
632+
// See comment in ListOfLibcNonsharedSymbols.
633+
cantFail(Jit->getMainJITDylib().define(
634+
absoluteSymbols(GetListOfLibcNonsharedSymbols(*Jit))));
635+
#endif
636+
601637
// This replaces llvm::orc::ExecutionSession::logErrorsToStdErr:
602638
auto&& ErrorReporter = [&Executor, LinkerPrefix, Verbose](Error Err) {
603639
Err = handleErrors(std::move(Err),

0 commit comments

Comments
 (0)