Skip to content

Commit 70fdbc1

Browse files
author
weipeng
committed
fix: 用可移植的 dlopen 方案替换内部 _dl_sym 符号
将 _dl_sym(GLIBC_PRIVATE 符号)替换为 dlopen("libc.so.6") + dlsym(), 修复在 glibc 版本不兼容的容器环境中出现的 "undefined symbol: _dl_sym, version GLIBC_PRIVATE" 运行时错误。 涉及文件: - src/libvgpu.c - src/nvml/hook.c - src/include/libvgpu.h Signed-off-by: weipeng <weip3@chinatelecom.cn>
1 parent 94fff56 commit 70fdbc1

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/include/libvgpu.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ extern void load_cuda_libraries();
2929

3030
#define FUNC_OVERRIDE_NAME(fname) overrided_##fname
3131

32-
extern void* _dl_sym(void*, const char*, void*);
33-
3432
#if defined(DLSYM_HOOK_DEBUG)
3533
#define DLSYM_HOOK_FUNC(f) \
3634
if (0 == strcmp(symbol, #f)) { \

src/libvgpu.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,12 @@ FUNC_ATTR_VISIBLE void* dlsym(void* handle, const char* symbol) {
9999
}
100100
if (real_dlsym == NULL) {
101101
LOG_ERROR("real dlsym not found");
102-
real_dlsym = _dl_sym(RTLD_NEXT, "dlsym", dlsym);
102+
void *libc_handle = dlopen("libc.so.6", RTLD_LAZY);
103+
if (libc_handle) {
104+
real_dlsym = dlsym(libc_handle, "dlsym");
105+
}
103106
if (real_dlsym == NULL)
104-
LOG_ERROR("real dlsym not found");
107+
LOG_ERROR("real dlsym not found after trying libc.so.6");
105108
}
106109
}
107110
if (handle == RTLD_NEXT) {
@@ -858,9 +861,12 @@ void preInit(){
858861
real_dlsym = dlvsym(RTLD_NEXT,"dlsym","GLIBC_2.2.5");
859862
if (real_dlsym == NULL) {
860863
LOG_ERROR("real dlsym not found");
861-
real_dlsym = _dl_sym(RTLD_NEXT, "dlsym", dlsym);
862-
if (real_dlsym == NULL)
864+
void *libc_handle = dlopen("libc.so.6", RTLD_LAZY);
865+
if (libc_handle) {
866+
real_dlsym = dlsym(libc_handle, "dlsym");
867+
} else {
863868
LOG_ERROR("real dlsym not found");
869+
}
864870
}
865871
}
866872
real_realpath = NULL;

src/nvml/hook.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,16 +267,17 @@ nvmlReturn_t nvmlDeviceGetIndex(nvmlDevice_t device, unsigned int *index) {
267267
}
268268

269269

270-
extern void* _dl_sym(void*, const char*, void*);
271-
272270
void load_nvml_libraries() {
273271
void *table = NULL;
274272
char driver_filename[FILENAME_MAX];
275273

276274
if (real_dlsym == NULL) {
277275
real_dlsym = dlvsym(RTLD_NEXT,"dlsym","GLIBC_2.2.5");
278276
if (real_dlsym == NULL) {
279-
real_dlsym = _dl_sym(RTLD_NEXT, "dlsym", dlsym);
277+
void *libc_handle = dlopen("libc.so.6", RTLD_LAZY);
278+
if (libc_handle) {
279+
real_dlsym = dlsym(libc_handle, "dlsym");
280+
}
280281
if (real_dlsym == NULL)
281282
LOG_ERROR("real dlsym not found");
282283
}

0 commit comments

Comments
 (0)