Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It’s possible to load a symbol file in gdb with the <code>add-symbol-file</code> command. The hardest part is to produce this symbol file.</p> <p>With the help of libMachObjC (which is part of <a href="https://github.com/nygard/class-dump" rel="noreferrer">class-dump</a>), it’s very easy to dump all addresses and their corresponding Objective-C methods. I have written a small tool, <a href="https://github.com/0xced/class-dump/tree/objc-symbols" rel="noreferrer">objc-symbols</a> which does exactly this.</p> <p>Let’s use Calendar.app as an example. If you try to list the symbols with the <code>nm</code> tool, you will notice that the Calendar app has been stripped:</p> <pre><code>$ nm -U /Applications/Calendar.app/Contents/MacOS/Calendar 0000000100000000 T __mh_execute_header 0000000005614542 - 00 0000 OPT radr://5614542 </code></pre> <p>But with <code>objc-symbols</code> you can easily retrieve the addresses of all the missing Objective-C methods:</p> <pre><code>$ objc-symbols /Applications/Calendar.app 00000001000c774c +[CALCanvasAttributedText textWithPosition:size:text:] 00000001000c8936 -[CALCanvasAttributedText createTextureIfNeeded] 00000001000c8886 -[CALCanvasAttributedText bounds] 00000001000c883b -[CALCanvasAttributedText updateBezierRepresentation] ... 00000001000309eb -[CALApplication applicationDidFinishLaunching:] ... </code></pre> <p>Then, with <a href="https://github.com/0xced/SymTabCreator/" rel="noreferrer">SymTabCreator</a> you can create a symbol file, which is just actually an empty dylib with all the symbols.</p> <p>Using <code>objc-symbols</code> and <code>SymTabCreator</code> together is straightforward:</p> <pre><code>$ objc-symbols /Applications/Calendar.app | SymTabCreator -o Calendar.stabs </code></pre> <p>You can check that <code>Calendar.stabs</code> contains all the symbols:</p> <pre><code>$ nm Calendar.stabs 000000010014a58b T +[APLCALSource printingCachedTextSize] 000000010013e7c5 T +[APLColorSource alternateGenerator] 000000010013e780 T +[APLColorSource defaultColorSource] 000000010013e7bd T +[APLColorSource defaultGenerator] 000000010011eb12 T +[APLConstraint constraintOfClass:withProperties:] ... 00000001000309eb T -[CALApplication applicationDidFinishLaunching:] ... </code></pre> <p>Now let’s see what happens in gdb:</p> <pre><code>$ gdb --silent /Applications/Calendar.app Reading symbols for shared libraries ................................. done </code></pre> <p>Without the symbol file:</p> <pre><code>(gdb) b -[CALApplication applicationDidFinishLaunching:] Function "-[CALApplication applicationDidFinishLaunching:]" not defined. Make breakpoint pending on future shared library load? (y or [n]) n </code></pre> <p>And after loading the symbol file:</p> <pre><code>(gdb) add-symbol-file Calendar.stabs add symbol table from file "Calendar.stabs"? (y or n) y Reading symbols from /Users/0xced/Calendar.stabs...done. (gdb) b -[CALApplication applicationDidFinishLaunching:] Breakpoint 1 at 0x1000309f2 </code></pre> <p>You will notice that the breakpoint address does not exactly match the symbol address (0x1000309f2 vs 0x1000309eb, 7 bytes of difference), this is because gdb automatically recognizes the function prologue and sets the breakpoint just after.</p> <p><hr></p> <h2>GDB script</h2> <p>You can use this GDB script to automate this, given that the stripped executable is the current target.</p> <p>Add the script from below to your <code>.gdbinit</code>, target the stripped executable and run the command <code>objc_symbols</code> in gdb:</p> <pre><code>$ gdb test ... (gdb) b +[TestClass randomNum] Function "+[TestClass randomNum]" not defined. (gdb) objc_symbols (gdb) b +[TestClass randomNum] Breakpoint 1 at 0x100000ee1 (gdb) ^D </code></pre> <hr> <pre><code>define objc_symbols shell rm -f /tmp/gdb-objc_symbols set logging redirect on set logging file /tmp/gdb-objc_symbols set logging on info target set logging off shell target="$(head -1 /tmp/gdb-objc_symbols | head -1 | awk -F '"' '{ print $2 }')"; objc-symbols "$target" | SymTabCreator -o /tmp/gdb-symtab set logging on add-symbol-file /tmp/gdb-symtab set logging off end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload