Provide Overridable Default Functions With Weak Symbols
Define a default function with __attribute__((weak)) so another object file can replace it at link time, and check optional symbols with weak references.

A library often needs a function that applications may replace: a hook that logs, an allocator, a board initialization routine. The classic C approach is a function pointer that the application sets at start-up. Weak symbols offer a link-time alternative with no pointer and no registration call. You define a default in the library, marked weak, and any object file that defines a strong function of the same name wins at link time. This post explains the binding rules in ELF, shows the override working, covers the weak reference form that tests whether a symbol exists, and lists the traps with static libraries, inlining and shared objects. Examples use GCC 14 and GNU ld from binutils 2.42 on x86-64 Linux.
Symbol binding in one paragraph
Every global symbol in an ELF object has a binding. A global symbol, STB_GLOBAL, is strong: two strong definitions of one name in the same link are a "multiple definition" error. A weak symbol, STB_WEAK, is allowed to be overridden: if the link contains a strong definition of the name, the linker uses it and ignores the weak ones; if there is none, the linker uses the weak definition; if there are two weak definitions, it takes the first it sees. nm prints weak defined symbols with the letter W (or V for weak objects), against T for a strong function.
A default and an override
The library file supplies a default implementation of a hook:
#include <stdio.h>
__attribute__((weak)) void on_start(void)
{
puts("default on_start");
}
void run(void)
{
on_start();
puts("running");
}void run(void);
int main(void)
{
run();
return 0;
}Link the two and run:
gcc -O2 main.c lib.c -o plain && ./plaindefault on_start
runningNow add a third file that defines the same name with no attribute, which makes it strong:
#include <stdio.h>
void on_start(void)
{
puts("custom on_start");
}gcc -O2 main.c lib.c custom.c -o custom && ./customcustom on_start
runningThere was no error and no change to lib.c: the linker chose custom.c's definition. You can confirm which one won with nm:
nm plain | grep on_start
nm custom | grep on_start0000000000001180 W on_start
0000000000001190 T on_startThe first line is the weak default, the second is the strong override. The addresses depend on your build and are only an example.
The inlining trap
A weak function cannot be inlined into callers in the same file, because the definition might be replaced at link time. GCC knows this and leaves a real call, which is the correct behavior and costs a call. The reverse problem is subtler: if you put the weak definition in the same file as its caller, the optimizer must still treat the call as opaque. For performance-sensitive hooks, keep the default in a separate translation unit anyway, so intent is clear. Under -flto the rule is the same: LTO respects weak resolution, because it works on the symbol resolution the linker decided.
On some compilers, weak plus inline produces surprising results. Avoid the combination.
Weak references: is this symbol present?
The other use of the attribute goes on a declaration, not a definition. A weak reference to a function that may or may not exist links successfully when the function is missing, and its address is then null:
#include <stdio.h>
extern void optional_feature(void) __attribute__((weak));
int main(void)
{
if (optional_feature)
optional_feature();
else
puts("feature not linked in");
return 0;
}gcc -O2 opt.c -o opt && ./optfeature not linked inWithout the weak attribute the link would fail with an undefined reference. The check if (optional_feature) compares the address with null, and GCC keeps the test because the symbol is weak. This is how libraries probe for optional dependencies, for example glibc's pthread_create check in older programs that wanted to work without libpthread. If a strong definition of optional_feature is linked, the branch runs it.
On modern systems with PIE executables, a weak undefined reference resolves to null at link time for a static link, but through the dynamic loader at run time for shared libraries, which is the case that needs careful testing on each platform.
Static libraries change the rules
Archive handling is where weak symbols bite. The linker pulls a member out of a static archive only to resolve an undefined symbol, and a weak undefined reference does not count as undefined for that purpose. Suppose libhooks.a contains a strong on_start and your program has only a weak reference to it. The linker will not extract that member, and the weak reference stays null. The fix is to make the reference strong, or to name the member explicitly on the link line, or to use -Wl,--whole-archive for that library, accepting that every member is linked in.
The same logic applies to a weak definition inside an archive member with a strong definition in your objects: no problem, the strong one wins, and the weak member is only pulled if something else needs it. A strong definition inside an archive loses to a weak definition already in an earlier object if the archive member is never extracted, because the linker already has a definition and no undefined symbol that would pull the member in.
Embedded interrupt handlers
Bare-metal startup files use the pattern at scale. A vector table lists every interrupt handler, and the startup file gives each a weak default that loops forever or calls a common handler:
void Default_Handler(void) { for (;;) { } }
void USART1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
void TIM2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));Your application file defines a strong USART1_IRQHandler, and the linker puts it in the vector table in place of the default, while handlers you never define fall through to Default_Handler. The alias attribute makes each name another label for the same code, so there is no per-handler stub. CMSIS and most vendor startup files for Cortex-M are built this way, and so are the weak HAL callbacks such as HAL_UART_RxCpltCallback in the STM32 libraries.
Pitfalls
Misspelled override. If your strong definition has a different name or signature from the weak one, the weak default silently remains in effect. C does not check signatures at link time. Put the declaration in a shared header and include it in both files so the compiler catches a mismatch.
Overriding in a shared library. For shared objects, symbol interposition lets an executable's strong definition replace a library's, which is what
LD_PRELOADrelies on. A weak definition in your own library behaves the same, but-fvisibility=hiddenor-Bsymbolicbinds calls early and prevents override.Two weak definitions. The linker takes the first one without warning. Link order then decides behavior, which makes the build fragile.
Weak data. The same attribute works on variables, and a weak variable used as a configuration default is convenient, but
-fno-commonand tentative-definition rules interact, so test it.Not portable. The attribute is GNU C, supported by Clang. MSVC has
/ALTERNATENAMEand__declspec(selectany)with different semantics.
Alternatives
A function pointer table gives runtime control and works on any compiler. Link-time wrapping with -Wl,--wrap=symbol redirects calls to __wrap_symbol and exposes the original as __real_symbol, which suits intercepting a library function you cannot edit. Weak symbols fit best where the override is decided at build time by what is linked, and the default should cost nothing.


