$npx -y skills add mohitmishra786/low-level-dev-skills --skill dynamic-linkingDynamic linking skill for Linux/ELF shared libraries. Use when debugging library loading failures, configuring RPATH vs RUNPATH, understanding soname versioning, using dlopen/dlsym for plugin systems, LD_PRELOAD interposition, or controlling symbol visibility. Activates on querie
| 1 | # Dynamic Linking |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide agents through Linux dynamic linking: shared library creation, RPATH/RUNPATH configuration, soname versioning, `dlopen`/`dlsym` plugin patterns, `LD_PRELOAD` interposition, and symbol visibility control. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "Cannot open shared object file: No such file or directory" |
| 10 | - "How do I set RPATH so my binary finds its shared library?" |
| 11 | - "How do I use dlopen/dlsym for a plugin system?" |
| 12 | - "What's the difference between RPATH and RUNPATH?" |
| 13 | - "How do I use LD_PRELOAD to intercept a function?" |
| 14 | - "How do I version my shared library with soname?" |
| 15 | |
| 16 | ## Workflow |
| 17 | |
| 18 | ### 1. Creating a shared library |
| 19 | |
| 20 | ```bash |
| 21 | # Compile with -fPIC (position-independent code) |
| 22 | gcc -fPIC -c src/mylib.c -o mylib.o |
| 23 | |
| 24 | # Link shared library with soname |
| 25 | gcc -shared -Wl,-soname,libmylib.so.1 \ |
| 26 | mylib.o -o libmylib.so.1.2.3 |
| 27 | |
| 28 | # Create symlinks (standard convention) |
| 29 | ln -s libmylib.so.1.2.3 libmylib.so.1 # soname link (used by ldconfig) |
| 30 | ln -s libmylib.so.1 libmylib.so # link link (used at compile time) |
| 31 | |
| 32 | # Register with ldconfig (system-wide) |
| 33 | sudo cp libmylib.so.1.2.3 /usr/local/lib/ |
| 34 | sudo ldconfig |
| 35 | ``` |
| 36 | |
| 37 | ### 2. Soname versioning convention |
| 38 | |
| 39 | ```text |
| 40 | libfoo.so.MAJOR.MINOR.PATCH |
| 41 | │ |
| 42 | └── soname = libfoo.so.MAJOR |
| 43 | ``` |
| 44 | |
| 45 | | Version bump | When | |
| 46 | |-------------|------| |
| 47 | | PATCH | Bug fix, ABI unchanged | |
| 48 | | MINOR | New symbols added, backwards compatible | |
| 49 | | MAJOR | ABI break — existing binaries will break | |
| 50 | |
| 51 | Inspect soname: |
| 52 | |
| 53 | ```bash |
| 54 | readelf -d libmylib.so.1.2.3 | grep SONAME |
| 55 | objdump -p libmylib.so.1.2.3 | grep SONAME |
| 56 | ``` |
| 57 | |
| 58 | ### 3. RPATH vs RUNPATH |
| 59 | |
| 60 | ```text |
| 61 | Both embed a library search path in the binary. |
| 62 | |
| 63 | RPATH → searched BEFORE LD_LIBRARY_PATH |
| 64 | RUNPATH → searched AFTER LD_LIBRARY_PATH (controllable at runtime) |
| 65 | |
| 66 | Recommendation: prefer RUNPATH (-Wl,--enable-new-dtags) |
| 67 | for deployment flexibility. |
| 68 | ``` |
| 69 | |
| 70 | ```bash |
| 71 | # Embed RPATH (old default) |
| 72 | gcc main.c -L./lib -lmylib \ |
| 73 | -Wl,-rpath,'$ORIGIN/../lib' -o myapp |
| 74 | |
| 75 | # Embed RUNPATH (new default with --enable-new-dtags) |
| 76 | gcc main.c -L./lib -lmylib \ |
| 77 | -Wl,-rpath,'$ORIGIN/../lib' \ |
| 78 | -Wl,--enable-new-dtags -o myapp |
| 79 | |
| 80 | # Inspect |
| 81 | readelf -d myapp | grep -E 'RPATH|RUNPATH' |
| 82 | chrpath -l myapp # show |
| 83 | chrpath -r '/new/path' myapp # modify existing |
| 84 | ``` |
| 85 | |
| 86 | `$ORIGIN` resolves to the directory of the binary at runtime — use it for relocatable installations. |
| 87 | |
| 88 | ### 4. Library search order |
| 89 | |
| 90 | ```text |
| 91 | 1. DT_RPATH (if no DT_RUNPATH present) |
| 92 | 2. LD_LIBRARY_PATH (env var, ignored for suid binaries) |
| 93 | 3. DT_RUNPATH |
| 94 | 4. /etc/ld.so.cache (populated by ldconfig from /etc/ld.so.conf) |
| 95 | 5. /lib, /usr/lib |
| 96 | ``` |
| 97 | |
| 98 | Debug with: |
| 99 | |
| 100 | ```bash |
| 101 | LD_DEBUG=libs ./myapp # trace library loading decisions |
| 102 | ldd myapp # show resolved libraries |
| 103 | ldd -v myapp # verbose with version requirements |
| 104 | ``` |
| 105 | |
| 106 | ### 5. dlopen / dlsym plugin pattern |
| 107 | |
| 108 | ```c |
| 109 | #include <dlfcn.h> |
| 110 | |
| 111 | typedef int (*plugin_fn_t)(const char *input); |
| 112 | |
| 113 | void load_plugin(const char *path) { |
| 114 | // RTLD_NOW: resolve all symbols immediately (fail fast) |
| 115 | // RTLD_LAZY: resolve on first call (default) |
| 116 | // RTLD_LOCAL: symbols not visible to other loaded libs |
| 117 | // RTLD_GLOBAL: symbols visible globally |
| 118 | void *handle = dlopen(path, RTLD_NOW | RTLD_LOCAL); |
| 119 | if (!handle) { |
| 120 | fprintf(stderr, "dlopen: %s\n", dlerror()); |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // Clear previous errors |
| 125 | dlerror(); |
| 126 | |
| 127 | plugin_fn_t fn = (plugin_fn_t)dlsym(handle, "plugin_run"); |
| 128 | const char *err = dlerror(); |
| 129 | if (err) { |
| 130 | fprintf(stderr, "dlsym: %s\n", err); |
| 131 | dlclose(handle); |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | fn("hello"); |
| 136 | dlclose(handle); |
| 137 | } |
| 138 | ``` |
| 139 | |
| 140 | Link with `-ldl`: |
| 141 | |
| 142 | ```bash |
| 143 | gcc main.c -ldl -o myapp |
| 144 | ``` |
| 145 | |
| 146 | ### 6. LD_PRELOAD interposition |
| 147 | |
| 148 | `LD_PRELOAD` loads a library before all others — its symbols override the application's. |
| 149 | |
| 150 | ```c |
| 151 | // myinterpose.c — intercept malloc |
| 152 | #define _GNU_SOURCE |
| 153 | #include <stdio.h> |
| 154 | #include <dlfcn.h> |
| 155 | |
| 156 | void *malloc(size_t size) { |
| 157 | static void *(*real_malloc)(size_t) = NULL; |
| 158 | if (!real_malloc) |
| 159 | real_malloc = dlsym(RTLD_NEXT, "malloc"); // find next malloc in chain |
| 160 | |
| 161 | void *ptr = real_malloc(size); |
| 162 | fprintf(stderr, "malloc(%zu) = %p\n", size, ptr); |
| 163 | return ptr; |
| 164 | } |
| 165 | ``` |
| 166 | |
| 167 | ```bash |
| 168 | gcc -shared -fPIC -o myinterpose.so myinterpose.c -ldl |
| 169 | |
| 170 | # Apply to any binary |
| 171 | LD_PRELOAD=./myinterpose.so ./myapp |
| 172 | LD_PRELOAD=/path/to/libfaketime.so ./myapp # time manipulation |
| 173 | ``` |
| 174 | |
| 175 | ### 7. Symbol visibility control |
| 176 | |
| 177 | Limit exported symbols to reduce binary size and avoid clashes: |
| 178 | |
| 179 | ```c |
| 180 | // Mark default: visible to linker |
| 181 | __attribute__((visibility("defaul |