且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么共享库路径是可执行的硬编码?

更新时间:2022-12-22 13:16:12

这三个 .so 文件在测试程序所在的主机上没有SONAME。告诉构建它的人使用 -Wl,soname,liba.so 以及类似的命令重建 liba.so 另外两个,然后重新链接主程序。


Recently I got a test binary. When I checked it using objdump, I observed that it includes hard coded library path. Why it is needed to to hardcode the path like that? Shouldn't the path be taken from SHELL environment variables or -L parameter instead ?

objdump -p testprog

The output includes the hardcoded path to shared libraries:

....
  NEEDED      /home/test/lib/liba.so
  NEEDED      /home/test/lib/libb.so
  NEEDED      /home/test/lib/libc.so
....

This is probably because those three .so files had no SONAME on the host where your test program was built. Tell the person who built it to rebuild liba.so with -Wl,soname,liba.so and similar for the other two, then relink the main program.