LD_PRELOAD & Dynamic Symbol Resolution
Developer toolingdeveloper tooling / debugging & linking
Today: build a function interceptor in ~8 lines, then unpack what each line teaches about how Linux actually finds your code at runtime.
Build it
shim.c — log every outbound TCP connect() a program makes:
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <sys/socket.h>
int connect(int fd, const struct sockaddr *a, socklen_t l) {
static int (*real)(int, const struct sockaddr *, socklen_t);
if (!real) real = dlsym(RTLD_NEXT, "connect");
fprintf(stderr, "[shim] connect fd=%d\n", fd);
return real(fd, a, l);
}
gcc -shared -fPIC -o shim.so shim.c
LD_PRELOAD=./shim.so curl -s https://example.com > /dev/null
# [shim] connect fd=5
No recompile of curl. No root. No ptrace.
What each line teaches
1️⃣ Your connect wins because symbol lookup is first-match-wins in scope order: preloaded libs, then DT_NEEDED libs in link order. The loader isn't matching "the right" connect — it stops at the first one named connect. That's all overriding is.
2️⃣ dlsym(RTLD_NEXT, ...) = "the next definition after me in that scope." Skip it and call connect() directly and you call yourself → stack overflow in 3 frames. This is the #1 mistake with interposition: replacing when you meant to wrap.
3️⃣ -fPIC -shared isn't ceremony — the loader maps your .so at an arbitrary address, so all references must be position-independent.
4️⃣ It only hooks calls that go through the PLT at runtime. So it does NOT work on: static binaries, Go binaries (they issue raw syscalls), or calls glibc makes to itself internally. If your shim "does nothing," check ldd ./app first — "not a dynamic executable" is your answer.
5️⃣ LD_PRELOAD is stripped for setuid/setgid binaries (secure-execution mode). It's a debugging tool, not a privilege escalation.
The gotcha that actually bites: RPATH vs RUNPATH
Search order for a needed library is:
DT_RPATH → LD_LIBRARY_PATH → DT_RUNPATH → /etc/ld.so.cache → default dirs
Read that again. LD_LIBRARY_PATH cannot override old-style DT_RPATH, but it does override DT_RUNPATH. So "I set LD_LIBRARY_PATH and it still loads the wrong libssl" is usually a binary baked with RPATH.
Diagnose, don't guess:
patchelf --print-rpath ./app # which one is it?
LD_DEBUG=libs ./app 2>&1 | head # every path tried, in order
LD_DEBUG=bindings ./app # which .so won each symbol
Also worth knowing
glibc versions its symbols (connect@GLIBC_2.2.5). Build on Ubuntu 24.04, run on 20.04, and you get version GLIBC_2.34 not found — not a missing library, a missing version node. Build in a container matching your oldest target.
Your exercise
Take any dynamically linked binary you use daily (curl, psql, python3):
- Run
LD_DEBUG=libs <binary> 2>&1 | grep -c "trying file"— how many paths did the loader probe before it found things? - Then
patchelf --print-rpathon it. RPATH, RUNPATH, or neither?
Bonus: write a shim over getaddrinfo that returns EAI_AGAIN every other call — instant DNS-flakiness chaos test, no network config needed. What breaks in your app?