finding the symbol dependencies of a bundle, like dyldinfo -lazy_bind -export

Dear LLDB enthusiasts,

I’m wondering if I can use the lldb library/libraries to replace the certain code running on OSX that now returns two lists of symbols-- similar to the output of (dyldinfo -lazy_bind -exports ); i.e. I need to list the symbols imported and exported by a binary shared object or bundle.

My hope was that by using an lldb library, I would be able to use the same client code on OSX as on linux. (The linux version of the code currently uses libbfd and libdld to do the same thing, but the later is getting little love/maintenance).

I’m looking through include/lldb/, as it seems like lldb would need this same info (imported symbol list, and exported symbol list for a Mach-O file) to function, but it’s not clear which API to use. All suggestions/pointers to example code in lldb would be welcome!

Thank you.
Jason

In case it is unclear what dyldinfo does, here is an example: (but I only need the symbol names; not the addresses or segments or sections):

$ file /tmp/sample_bundle
/tmp/sample_bundle: Mach-O 64-bit bundle x86_64

$dyldinfo -lazy_bind -export /tmp/sample_bundle

lazy binding information (from lazy_bind part of dyld info):
segment section address index dylib symbol
__DATA __la_symbol_ptr 0x00001030 0x0000 flat-namespace __mm_pop_chunk
__DATA __la_symbol_ptr 0x00001038 0x0015 flat-namespace _dh_define
export information (from trie):
0x000008A0 _C_ipair
0x00000920 _init_ipair
0x00000BC0 _C_iprot
0x00000C40 _C_ipi2
0x00000CC0 _C_ipi1
0x00001040 _K_ipair_R43808f40
0x00001160 _K_ipi1_R5cb4475d
0x00001260 _K_ipi2_R5cb4475d
0x00001360 _K_iprot_Rfc8fe739
0x00001460 _majver_ipair
0x00001464 _minver_ipair

Yes you can do this with LLDB. If you load a binary and dump its symbol table, you will see the information you want. For symbols that are lazily bound, you can look for "Trampoline" symbols:

cd lldb/test/lang/objc/foundation
make
lldb a.out
(lldb) target modules dump symtab a.out
Symtab, file = .../lldb/test/lang/objc/foundation/a.out, num_symbols = 54:
               Debug symbol
               >Synthetic symbol
               >>Externally Visible
               >>>
Index UserID DSX Type File Address/Value Load Address Size Flags Name
------- ------ --- ------------ ------------------ ------------------ ------------------ ---------- ----------------------------------
[ 0] 0 D SourceFile 0x0000000000000000 Sibling -> [ 15] 0x00640000 /Volumes/work/gclayton/Documents/src/lldb/test/lang/objc/foundation/main.m
[ 1] 2 D ObjectFile 0x000000004f79f1ca 0x0000000000000000 0x00660001 /Volumes/work/gclayton/Documents/src/lldb/test/lang/objc/foundation/main.o
[ 2] 4 D Code 0x00000001000010f0 0x00000000000000c0 0x000e0000 -[MyString initWithNSString:]
[ 3] 8 D Code 0x00000001000011b0 0x0000000000000090 0x000e0000 -[MyString dealloc]
[ 4] 12 D Code 0x0000000100001240 0x00000000000000a0 0x000e0000 -[MyString description]
[ 5] 16 D Code 0x00000001000012e0 0x0000000000000020 0x000e0000 -[MyString descriptionPauses]
[ 6] 20 D Code 0x0000000100001300 0x0000000000000030 0x000e0000 -[MyString setDescriptionPauses:]
[ 7] 24 D Code 0x0000000100001330 0x0000000000000030 0x000e0000 -[MyString str_property]
[ 8] 28 D Code 0x0000000100001360 0x0000000000000050 0x000e0000 -[MyString setStr_property:]
[ 9] 32 D Code 0x00000001000013b0 0x0000000000000040 0x000f0000 Test_Selector
[ 10] 36 D Code 0x00000001000013f0 0x0000000000000130 0x000f0000 Test_NSString
[ 11] 40 D Code 0x0000000100001520 0x0000000000000120 0x000f0000 Test_MyString
[ 12] 44 D Code 0x0000000100001640 0x00000000000001b0 0x000f0000 Test_NSArray
[ 13] 48 D Code 0x00000001000017f0 0x00000000000000e1 0x000f0000 main
[ 14] 56 D X Data 0x0000000100002680 0x0000000000000000 0x00200000 my_global_str
[ 15] 58 D SourceFile 0x0000000000000000 Sibling -> [ 19] 0x00640000 /Volumes/work/gclayton/Documents/src/lldb/test/lang/objc/foundation/my-base.m
[ 16] 60 D ObjectFile 0x000000004f79f1ca 0x0000000000000000 0x00660001 /Volumes/work/gclayton/Documents/src/lldb/test/lang/objc/foundation/my-base.o
[ 17] 62 D Code 0x00000001000018e0 0x0000000000000020 0x000e0000 -[MyBase propertyMovesThings]
[ 18] 66 D Code 0x0000000100001900 0x000000000000001f 0x000e0000 -[MyBase setPropertyMovesThings:]
[ 19] 82 Data 0x0000000100002000 0x0000000000000460 0x000e0000 pvars
[ 20] 83 ObjCIVar 0x0000000100002518 0x0000000000000148 0x001e0000 MyBase.propertyMovesThings
[ 21] 84 X Data 0x0000000100002660 0x0000000000000008 0x000f0000 NXArgc
[ 22] 85 X Data 0x0000000100002668 0x0000000000000008 0x000f0000 NXArgv
[ 23] 86 X ObjCClass 0x00000001000024d8 0x0000000000000028 0x000f0000 MyBase
[ 24] 87 X ObjCClass 0x0000000100002460 0x0000000000000028 0x000f0000 MyString
[ 25] 88 X ObjCIVar 0x0000000100002510 0x0000000000000008 0x000f0000 MyString._desc_pauses
[ 26] 89 X ObjCIVar 0x0000000100002508 0x0000000000000008 0x000f0000 MyString.date
[ 27] 90 X ObjCIVar 0x0000000100002500 0x0000000000000008 0x000f0000 MyString.str
[ 28] 91 X ObjCMetaClass 0x00000001000024b0 0x0000000000000028 0x000f0000 MyBase
[ 29] 92 X ObjCMetaClass 0x0000000100002488 0x0000000000000028 0x000f0000 MyString
[ 30] 97 X Data 0x0000000100002678 0x0000000000000008 0x000f0000 __progname
[ 31] 98 X Data 0x0000000100000000 0x00000000000010b0 0x000f0010 _mh_execute_header
[ 32] 99 X Data 0x0000000100002670 0x0000000000000008 0x000f0000 environ
[ 33] 101 X Data 0x0000000100002680 0x0000000000000000 0x000f0000 my_global_str
[ 34] 102 X Code 0x00000001000010b0 0x0000000000000040 0x000f0000 start
[ 35] 103 Trampoline 0x0000000100001938 0x0000000000000006 0x00010200 NSLog
[ 36] 104 X Undefined 0x0000000000000000 0x0000000000000000 0x00010400 OBJC_CLASS_$_NSArray
[ 37] 105 X Undefined 0x0000000000000000 0x0000000000000000 0x00010200 OBJC_CLASS_$_NSAutoreleasePool
[ 38] 106 X Undefined 0x0000000000000000 0x0000000000000000 0x00010400 OBJC_CLASS_$_NSDate
[ 39] 107 X Undefined 0x0000000000000000 0x0000000000000000 0x00010400 OBJC_CLASS_$_NSObject
[ 40] 108 X Undefined 0x0000000000000000 0x0000000000000000 0x00010200 OBJC_CLASS_$_NSString
[ 41] 109 X Undefined 0x0000000000000000 0x0000000000000000 0x00010400 OBJC_METACLASS_$_NSObject
[ 42] 110 X Undefined 0x0000000000000000 0x0000000000000000 0x00010400 __CFConstantStringClassReference
[ 43] 111 X Undefined 0x0000000000000000 0x0000000000000000 0x00010100 _objc_empty_cache
[ 44] 112 X Undefined 0x0000000000000000 0x0000000000000000 0x00010100 _objc_empty_vtable
[ 45] 113 Trampoline 0x000000010000193e 0x0000000000000006 0x00010300 exit
[ 46] 114 Trampoline 0x0000000100001920 0x0000000000000006 0x00010100 objc_getProperty
[ 47] 115 Trampoline 0x0000000100001926 0x0000000000000006 0x00010100 objc_msgSend
[ 48] 116 Trampoline 0x000000010000192c 0x0000000000000006 0x00010100 objc_msgSendSuper2
[ 49] 117 X Undefined 0x0000000000000000 0x0000000000000000 0x00010100 objc_msgSend_fixup
[ 50] 118 Trampoline 0x0000000100001932 0x0000000000000006 0x00010100 objc_setProperty
[ 51] 119 Trampoline 0x0000000100001944 0x0000000000000006 0x00010300 printf
[ 52] 120 Trampoline 0x000000010000194a 0x0000000000000006 0x00010300 usleep
[ 53] 121 X Undefined 0x0000000000000000 0x0000000000000000 0x00010300 dyld_stub_binder
(lldb)

All lazily bound symbols will have type Trampoline:

[ 45] 113 Trampoline 0x000000010000193e 0x0000000000000006 0x00010300 exit
[ 46] 114 Trampoline 0x0000000100001920 0x0000000000000006 0x00010100 objc_getProperty
[ 47] 115 Trampoline 0x0000000100001926 0x0000000000000006 0x00010100 objc_msgSend
[ 48] 116 Trampoline 0x000000010000192c 0x0000000000000006 0x00010100 objc_msgSendSuper2
[ 50] 118 Trampoline 0x0000000100001932 0x0000000000000006 0x00010100 objc_setProperty
[ 51] 119 Trampoline 0x0000000100001944 0x0000000000000006 0x00010300 printf
[ 52] 120 Trampoline 0x000000010000194a 0x0000000000000006 0x00010300 usleep

The other symbols that are exernal are marked with an "X" (which is a boolean flag on each symbol).

The symbols can be accessed via the SBModule:

    size_t
    SBModule::GetNumSymbols ();
    
    lldb::SBSymbol
    SBModule::GetSymbolAtIndex (size_t idx);

And then you can get the symbol type from each SBSymbol:

    SymbolType
    SBSymbol::GetType ();

I just added the ability to see if a symbol is externally visible:
% svn commit
Sending include/lldb/API/SBSymbol.h
Sending scripts/Python/interface/SBSymbol.i
Sending source/API/SBSymbol.cpp
Transmitting file data ...
Committed revision 153893.

    bool
    SBSymbol::IsExternal();

So your flow should be:

SBDebugger::Initialize();
SBDebugger debugger(SBDebugger::Create());
SBTarget target (debugger.CreateTarget (const char *filename,
                      const char *target_triple,
                const char *platform_name,
                            bool add_dependent_modules,
                                        lldb::SBError& error));

SBFileSpec exe_file_spec (filename);
SBModule exe_module (target.FindModule(exe_file_spec));
if (exe_module.IsValid()
{
    const size_t num_symbols = exe_module. GetNumSymbols();
    for (size_t i=0; i<num_symbols; ++i)
    {
        SBSymbol symbol (exe_module. GetSymbolAtIndex(i));
        if (symbol.IsExternal())
        {
        }

  if (symbol.GetType() == lldb::eSymbolTypeTrampoline)
        {
        }
    }
}

A quick clarification on the args to CreateTarget:

"filename" is just a full path to the local object file you want to observer. "target_triple" is your <arch>-<vendor>-<os>, or something like "x86_64-apple-darwin" or "i386-pc-linux" and can be NULL if the file is only a single architecture. "platform_name" can be NULL. "add_dependent_modules" should be false, since you are only interested in seeing the one object file itself, an SBError instance should be created and passed in.