linux LD_DEBUG 调试动态库加载

nxdong July 29, 2022 [linux] #linux

LD_DEBUG 调试动态库加载

Linux 指定环境变量

可以用来查看程序搜索库的路径,使用方法如下:
LD_DEBUG=libs ./your_program
LD_DEBUG=help 查看命令使用方法

export LD_DEBUG=files     # 显示库的依赖性和加载的顺序

export LD_DEBUG=bindings  # 符号绑定

export LD_DEBUG=libs   # 显示加载器查找库时使用的路径的顺序

export LD_DEBUG=versions  # 版本依赖性

export LD_DEBUG=help  # LD_DEBUG的帮助信息

Mac

使用otool工具

otool --help

依赖的库查询

otool -L meyers_singleton
meyers_singleton:
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 1300.23.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1311.100.3)

查看汇编

otool -tV add       
add:
(__TEXT,__text) section
_add:
0000000100003f60        pushq   %rbp
0000000100003f61        movq    %rsp, %rbp
0000000100003f64        movl    %edi, -0x4(%rbp)
0000000100003f67        movl    %esi, -0x8(%rbp)
0000000100003f6a        movl    -0x4(%rbp), %eax
0000000100003f6d        addl    -0x8(%rbp), %eax
0000000100003f70        popq    %rbp
0000000100003f71        retq
0000000100003f72        nopw    %cs:(%rax,%rax)
0000000100003f7c        nopl    (%rax)
_main:
0000000100003f80        pushq   %rbp
0000000100003f81        movq    %rsp, %rbp
0000000100003f84        subq    $0x10, %rsp
0000000100003f88        movl    $0x0, -0x4(%rbp)
0000000100003f8f        movl    %edi, -0x8(%rbp)
0000000100003f92        movq    %rsi, -0x10(%rbp)
0000000100003f96        movl    $0x1, %edi
0000000100003f9b        movl    $0x2, %esi
0000000100003fa0        callq   _add
0000000100003fa5        xorl    %eax, %eax
0000000100003fa7        addq    $0x10, %rsp
0000000100003fab        popq    %rbp
0000000100003fac        retq

参考资料

ld man page

otool manpage