TExcelTAD
Home › Build Fundamentals: From Source to Binary › Toolkit: binutils, strace & ltrace

Toolkit: binutils, strace & ltrace

When a build produces something unexpected (a binary that is too large, a missing symbol, a library that loads from the wrong place), you look inside the output and at what it does. GNU binutils (shipped with every Linux toolchain; LLVM has llvm- equivalents of each) do the first, strace and ltrace the second.

binutils at a glance

Tool Answers Example
nm which symbols does it define or need? nm -C --defined-only libfoo.a
objdump disassembly, headers, relocations objdump -d -r -M intel foo.o
readelf every ELF detail: headers, sections, dynamic section, notes readelf -d app (NEEDED, RUNPATH, SONAME)
size how big are text, data, bss? size app
strings printable text inside a binary strings -a app | grep /home
ar create and list static libraries ar t libfoo.a
strip remove symbols and debug info strip --strip-unneeded libfoo.so
objcopy copy/convert, split off debug info objcopy --only-keep-debug app app.debug
addr2line which source line is this address? addr2line -f -e app 0x1149
c++filt demangle C++ names echo _ZN3geo4areaEd | c++filt

Most of them appeared earlier in this course. Three more are worth a session each.

strip and separate debug files

Debug info is often larger than the code. Release packages ship stripped binaries and keep the debug info in a separate file (Linux distributions publish them as -dbg/-debuginfo packages, or via a debuginfod server), linked by a .gnu_debuglink section so debuggers can find it:

bash · try it yourself
mkdir -p tools && cd tools
cat > app.c <<'SRC'
#include <stdio.h>
static int square(int x) { return x * x; }
int main(void) { for (int i = 1; i <= 3; i++) printf("%d ", square(i)); puts(""); return 0; }
SRC
gcc -g -O2 app.c -o app
objcopy --only-keep-debug app app.debug
cp app app.stripped
strip --strip-debug --strip-unneeded app.stripped
objcopy --add-gnu-debuglink=app.debug app.stripped
for f in app app.stripped app.debug; do printf '%-13s %6s bytes  ' $f $(stat -c %s $f); file -b $f | grep -oE '(not )?stripped|with debug_info' | paste -sd' '; done
./app.stripped
readelf --string-dump=.gnu_debuglink app.stripped | grep app.debug
app            18256 bytes  with debug_info not stripped
app.stripped   14576 bytes  stripped
app.debug       6896 bytes  with debug_info not stripped
1 4 9
  [     0]  app.debug

addr2line: from an address back to the source

Crash reports and profilers give you addresses. With debug info, addr2line turns them into function names and line numbers. Here the address of main comes from nm:

bash · try it yourself
mkdir -p tools && cd tools
cat > app.c <<'SRC'
#include <stdio.h>
int helper(int x) {
    return x + 1;
}
int main(void) {
    printf("%d\n", helper(41));
    return 0;
}
SRC
gcc -g -O0 -ffile-prefix-map="$PWD"=. app.c -o app
for fn in main helper; do
  addr=0x$(nm app | awk -v f=$fn '$3 == f { print $1 }')
  printf '%s at %s -> ' $fn $addr; addr2line -f -e app $addr | paste -sd' '
done
main at 0x000000000000115c -> main ././app.c:5
helper at 0x0000000000001149 -> helper ././app.c:2

The -ffile-prefix-map from Reproducible Builds is why the path is relative: ././app.c is the compilation directory (.) joined with the file name (./app.c), instead of /home/learner/tools/app.c.

strace: what a program asks the kernel

strace prints every system call: files opened, processes started, memory mapped. For build problems the most useful filters are -e trace=execve (which programs ran: seen in The Compiler Driver) and -e trace=openat (which files were read, which shows exactly which library or header was picked up):

bash · try it yourself
mkdir -p tools && cd tools
printf '#include <stdio.h>\nint main(void) { puts("traced"); return 0; }\n' > app.c
gcc app.c -o app
strace -e trace=openat ./app
echo "--- files the compiler opened, headers only"
strace -f -e trace=openat -o trace.txt gcc -c app.c
grep -v ENOENT trace.txt | grep -oE '"[^"]+\.h"' | sort -u | head -5
openat(AT_FDCWD, "/etc/ld.so.cache", O_RDONLY|O_CLOEXEC) = 3
openat(AT_FDCWD, "/lib/x86_64-linux-gnu/libc.so.6", O_RDONLY|O_CLOEXEC) = 3
traced
+++ exited with 0 +++
--- files the compiler opened, headers only
"/usr/include/features-time64.h"
"/usr/include/features.h"
"/usr/include/stdc-predef.h"
"/usr/include/stdio.h"
"/usr/include/x86_64-linux-gnu/bits/floatn-common.h"

The first trace is the dynamic loader at work: it reads the ldconfig cache, then opens libc.so.6. The second shows the headers cc1 opened (failed attempts in other search directories are filtered out with grep -v ENOENT). strace -f follows child processes, which you need for anything started by a driver or a build tool. strace -c prints a summary table of calls and time instead of each call.

ltrace: which library functions are called

ltrace intercepts calls from a program into shared libraries. It shows arguments and return values, which is handy to see what a binary does without its source. Pointer values change on every run (address space layout randomisation), so the sed masks them:

bash · try it yourself
mkdir -p tools && cd tools
cat > app.c <<'SRC'
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
    char *name = malloc(16);
    strcpy(name, "tad");
    printf("hello %s (%zu)\n", name, strlen(name));
    free(name);
    return 0;
}
SRC
gcc -O0 app.c -o app
ltrace ./app 2>&1 | sed -E 's/0x[0-9a-f]+/0x…/g'
malloc(16)                                       = 0x…
strlen("tad")                                    = 3
printf("hello %s (%zu)\n", "tad", 3)             = 14
free(0x…)                             = <void>
hello tad (3)
+++ exited (status 0) +++

Two things to notice: strcpy doesn't appear, because GCC replaced the copy of a short constant string with plain stores (calls you write are not always calls in the binary); and the program's own output (hello tad (3)) is interleaved with the trace. ltrace works by patching the PLT, so it can't see calls in a static binary, and it misses calls that don't go through the PLT (code built with -fno-plt, for example); uftrace and perf are the heavier alternatives.

On macOS and Windows

Linux macOS Windows
readelf -d / ldd otool -L dumpbin /dependents, Dependencies
nm nm (Mach-O) dumpbin /symbols
objdump -d otool -tV, objdump (LLVM) dumpbin /disasm
strace dtruss (needs SIP adjustments) Process Monitor
ltrace DTrace probes API Monitor
LD_DEBUG=libs DYLD_PRINT_LIBRARIES=1 gflags "show loader snaps"

Test yourself

Which tool turns an address from a crash report into a function name and source line?

  1. nm
  2. addr2line (with debug info)
  3. strip
  4. ar

You want to know exactly which libz.so a program loaded. Which command shows it?

  1. nm -D app
  2. strace -e trace=openat ./app
  3. size app
  4. strings app

Why didn't strcpy appear in the ltrace output?

  1. ltrace hides string functions
  2. The compiler replaced the call with inline stores, so no library call happened
  3. strcpy is a system call
  4. It failed