macossymbolsmach-ootool

How to print all symbols that a mach-o binary imports from dylibs?


I am new to MacOS & MacOS programming. I would like to know how an application is working (by simply having a glimpse of the binary's imported system APIs for learning).

I tried otool, printed the help message, I saw the message says -L prints the shared libraries used. Then I ran otool -L <binary-file>, but it only printed the names of the used libraries, without any imported symbols.

Maybe I missed something in otool, or is there any other tool that can help get import symbols in a mach-o binary?


Solution

  • You can get a simple list of imported symbols with nm -u:

    % nm -u /bin/echo
    ___mb_cur_max
    ___stdoutp
    _err
    _exit
    _fflush
    _getenv
    _mbtowc
    _putchar
    _putwchar
    _strcmp
    _strlen
    dyld_stub_binder
    

    For more detailed information, including the library that each symbol is expected to come from, you can use dyldinfo. This requires Xcode though, and has to be invoked as xcrun dyldinfo. You'll want the union of -bind, -weak_bind and -lazy_bind:

    % xcrun dyldinfo -bind /bin/echo 
    bind information:
    segment section          address        type    addend dylib            symbol
    __DATA_CONST __got            0x100004000    pointer      0 libSystem        ___mb_cur_max
    __DATA_CONST __got            0x100004008    pointer      0 libSystem        ___stdoutp
    __DATA_CONST __got            0x100004010    pointer      0 libSystem        dyld_stub_binder
    % xcrun dyldinfo -weak_bind /bin/echo
    no weak binding
    % xcrun dyldinfo -lazy_bind /bin/echo
    lazy binding information (from lazy_bind part of dyld info):
    segment section          address    index  dylib            symbol
    __DATA  __la_symbol_ptr  0x100008000 0x0000 libSystem        _err
    __DATA  __la_symbol_ptr  0x100008008 0x000B libSystem        _exit
    __DATA  __la_symbol_ptr  0x100008010 0x0017 libSystem        _fflush
    __DATA  __la_symbol_ptr  0x100008018 0x0025 libSystem        _getenv
    __DATA  __la_symbol_ptr  0x100008020 0x0033 libSystem        _mbtowc
    __DATA  __la_symbol_ptr  0x100008028 0x0041 libSystem        _putchar
    __DATA  __la_symbol_ptr  0x100008030 0x0050 libSystem        _putwchar
    __DATA  __la_symbol_ptr  0x100008038 0x0060 libSystem        _strcmp
    __DATA  __la_symbol_ptr  0x100008040 0x006E libSystem        _strlen