Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用cmake依赖brpc编译成功,运行报undefined symbol: pthread_mutex_lock #1086

Closed
bai-charisu opened this issue Apr 15, 2020 · 7 comments

Comments

@bai-charisu
Copy link

使用cmake动态依赖brpc,编译显示正常,但是运行的时候报错:
image

nm查看libbrpc.so是有这个符号的
image

ldd查看,可执行程序也确实链接了brpc和pthread,所以不知道问题出在哪里:
image

@lils11
Copy link

lils11 commented Nov 10, 2020

遇到了同样的问题

@kungf
Copy link

kungf commented Dec 1, 2020

+1

@kungf
Copy link

kungf commented Dec 3, 2020

@bai-charisu have you solve this problem?

@bai-charisu
Copy link
Author

解决了,怀疑是加载顺序的问题,在编译的时候把-lbrpc放在最前面之后ok了

@kungf
Copy link

kungf commented Dec 4, 2020

解决了,怀疑是加载顺序的问题,在编译的时候把-lbrpc放在最前面之后ok了

ok,thanks very much.

@starrysky9959
Copy link

starrysky9959 commented Apr 14, 2023

./mongodb/build/install/bin/mongod: symbol lookup error: /usr/lib/libbrpc.so: undefined symbol: pthread_mutex_lock

用静态链接一切正常,用动态链接能通过编译链接,但一运行就出现上述报错。nm看到的symbol和原issue一样,ldd看到的链接也都是正确的,尝试修改链接顺序还是没解决。

使用cmake动态依赖brpc,编译显示正常,但是运行的时候报错: image

nm查看libbrpc.so是有这个符号的 image

ldd查看,可执行程序也确实链接了brpc和pthread,所以不知道问题出在哪里: image

@starrysky9959
Copy link

感觉本质是因为brpc通过dlsym(RTLD_NEXT, "pthread_mutex_lock");hook了libpthread中的pthread_mutex_lock方法导致的. RTLD_NEXT意味着在load dynamic libraries时, libpthread必须在libbrpc之后被load, 且两者中间不能有其他含有symbol为pthread_mutex_lock的library(虽然一般不会再有了). 以下几篇文章介绍了相关概念和实践.
https://optumsoft.com/dangers-of-using-dlsym-with-rtld_next/

https://csstormq.github.io/blog/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%AF%87%E4%B9%8B%E9%93%BE%E6%8E%A5%EF%BC%8816%EF%BC%89%EF%BC%9A%E7%9C%9F%E6%AD%A3%E7%90%86%E8%A7%A3%20RTLD_NEXT%20%E7%9A%84%E4%BD%9C%E7%94%A8

https://zhuanlan.zhihu.com/p/44132805

我的项目中不方便修改pthread的链接位置,可以利用LD_PRELOAD来提前加载libbrpc以满足这一要求.

export LD_PRELOAD=/lib/libbrpc.so

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants