ArticleslgStudy

science

Weak symbol

Weak symbol is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Weak symbol rather than just read about it. In short: A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong.

Key takeaways

  • Weak symbol belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Weak symbol to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Weak symbol from memory before moving on to harder problems.

Reference excerpt

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong. During linking, a strong symbol can override a weak symbol of the same name. This behavior allows an executable to override standard library functions, such as malloc(3). When linking a binary executable, a weakly declared symbol does not need a definition. In comparison, (by default) a declared strong symbol without a definition triggers an undefined symbol link error. Weak symbols are not mentioned by the C or C++ language standards; as such, inserting them into code is not very portable. Even if two platforms support the same or similar syntax for marking symbols as weak, the semantics may differ in subtle points, e.g. whether weak symbols during dynamic linking at runtime lose their semantics or not.

Syntax The GNU Compiler Collection and the Solaris Studio C compiler share the same syntax for annotating symbols as weak, namely a special #pragma, #pragma weak, and, alternatively, a function and variable attribute, __attribute__((weak)).

Pragma

Attribute

Tools support The nm command identifies weak symbols in object files, libraries, and executables. On Linux a weak function symbol is marked with "W" if a weak default definition is available, and with "w" if it is not. Weakly defined variable symbols are marked with "V" and "v". On Solaris "nm" prints "WEAK" instead of "GLOB" for a weak symbol.

Examples The following examples work on Linux and Solaris with GCC and Solaris Studio.

Static example main.c:

power_slow.h:

power_slow.c:

power.c:

Build commands:

cc -g -c -o main.o main.c cc -g -c -o power_slow.o power_slow.c cc -g -c -o power.o power.c cc main.o power_slow.o -o slow cc main.o power_slow.o power.o -o fast

Output:

When removing the weak attribute and re-executing the build commands, the last one fails with the following error message (on Linux):

multiple definition of `power2'

The second-last one still succeeds, and ./slow has the same output. If there are no definition for a weak symbol function as a default implementation or as another weak symbol function definition or a strong symbol function definition in any of the object files linked together, the linking will be done successfully without any undefined symbol error for that weak symbol, but the execution may cause runtime crash.

Shared example Taking main.c from the preceding example and adding:

Replacing power_slow.c with:

Build commands:

Output:

Removing the weak attribute and re-executing the build commands does not yield build errors and leads to the same output (on Linux) for main and main2. The build commands for the main3 lead to following warning and error messages (on Linux):

warning: the address of ‘user_hook’ will always evaluate as ‘true’ libpowerslow.so: undefined reference to `user_hook'

The warning is issued by the compiler because it can statically determine that in if (user_hook) the expression user_hook evaluates always to true, because it contains an ELF jump table entry. The error message is issued by the linker. The build for main4 includes the same warning but no link error.

Use cases Weak symbols can be used as a mechanism to provide default implementations of functions that can be replaced by more specialized (e.g. optimized) ones at link-time. The default implementation is then declared as weak, and, on certain targets, object files with strongly declared symbols are added to the linker command line. If a library defines a symbol as weak, a program that links that library is free to provide a strong one for, say, customization purposes. Another use case for weak symbols is the maintenance of binary backward compatibility.

Limitations On UNIX System V descendent systems, during program runtime the dynamic linker resolves weak symbols definitions like strong ones. For example, a binary is dynamically linked against libraries libfoo.so and libbar.so. libfoo defines symbol f and declares it as weak. libbar also defines f and declares it as strong. Depending on the library ordering on the link command line (i.e. -lfoo -lbar) the dynamic linker uses the weak f from libfoo.so although a strong version is available at runtime. The GNU ld provides the environment variable LD_DYNAMIC_WEAK to provide weak semantics for the dynamic linker. When using constructs like

, depending on the compiler and used optimization level, the compiler may interpret the conditional as always true (because func can be seen as undefined from a standards point of view). An alternative to the above construct is using a system API to check if func is defined (e.g. dlsym with RTLD_DEFAULT). The above check may also fail for other reasons, e.g. when func contains an elf jump table entry. Using weak symbols in static libraries has other semantics than in shared ones, i.e. with a static library the symbol lookup stops at the first symbol – even if it is just weak and an object file with a strong symbol is also included in the library archive. On Linux, the linker option --whole-archive changes that behavior. The weak function attribute is supposed to be used on function declarations. Using it on a function definition may yield unexpected results, depending on the compiler and optimization level. In Solaris, the weak symbols are also used within kernel. The generic part of kernel (called genunix) specifies weak functions that are overridden in platform specific part of the kernel (called unix) such as Virtual memory routines. The kernel runtime linker sets the addresses of these functions when the kernel is combined in memory during boot. This does not work for kernel loadable modules though - weak symbol in the kernel is not replaced with kernel module symbol when the module is loaded.

Related methods C preprocessor (CPP) conditional constructs can also be used to switch between different versions of a symbol. The difference from weak symbols is that weak symbols are interpreted by the linker. The CPP is run during the compilation of each translation unit before the C compiler. The build process (e.g. make) can be implemented in a conditional way such that just different versions of a symbol are created or different (specialized) libraries are used and linked depending on the target.

See also Dynamic linking Link editor Program loader

References

Worked examples

Example 1 — a first encounter with Weak symbol

Start with the simplest possible case. Write down what Weak symbol claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Weak symbol before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Weak symbol ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Weak symbol

In research
Weak symbol appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Weak symbol in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Weak symbol is common in secondary-school and first-year university syllabi. It links to neighbouring topics Data types, Debugging, so understanding it makes those chapters shorter.
In everyday life
Look for Weak symbol outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Weak symbol” →

How to study Weak symbol in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Weak symbol means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Weak symbol out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Weak symbol in simple terms?

A weak symbol denotes a specially annotated symbol during linking of Executable and Linkable Format (ELF) object files. By default, without any annotation, a symbol in an object file is strong.

Why does Weak symbol matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Weak symbol?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Weak symbol.

Tags

  • Data types
  • Debugging

Keep exploring