Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

12.1 Jump Label Modification

In all computer programs, branch execution is an indispensable control flow instruction. However, executing a branch operation usually demands multiple machine cycles. Frequent invocation of branch instructions—particularly conditional branch instructions—inevitably penalizes execution throughput. In many execution flows, the mathematical probability of a branch being taken versus flowing sequentially varies drastically. In certain execution tracks, the likelihood of a program branching is far greater than that of raw sequential processing, whereas under alternative constraints, sequential tracking happens far more frequently than branching paths. Since branching costs more processor pipeline cycles than straight-line sequential execution, programmers can often anticipate which logical side exhibits the higher probability. Through specialized source structure designs, code paths with lower execution profiles can be shunted to the taken side of the branch, while high-probability hot code sequences can be positioned along the straight-line sequential track. This arrangement accelerates execution velocity and optimizes microarchitectural performance. To support this paradigm, GCC supplies the likely(x) and unlikely(x) macro definitions. When x evaluates to 0, both macros return 0; when x does not evaluate to 0, both macros return 1. The difference lies in their compiler hints: likely() predicts that the value of x will be non-zero, whereas unlikely() predicts that the value of x will resolve to 0. The exact definitions of likely(x) and unlikely(x) are implemented as follows:

#define likely(x)      	__builtin_expect(!!(x), 1) 
#define unlikely(x)    	__builtin_expect(!!(x), 0)

The intrinsic __builtin_expect(x,y) represents a built-in helper function provided by the GCC compiler. It seamlessly returns the actual value of x, while informing the compiler infrastructure that the statistical expectation of x is predicted to match y.

By utilizing likely() and unlikely(), software architects can explicit signal to the compiler infrastructure which execution paths exhibit high probability profiles and which blocks feature low execution frequencies. Armed with these microarchitectural weight hints, the compiler places the high-probability payload (likely code) straight down the sequential pipeline track (the fast path), while segregating the low-probability blocks (unlikely code) over to the taken side of the branch (the slow path). The canonical usage pattern is structured as follows:

if (likely(x)) {		/* Executed identically to standard if (x) logic */
	/* likely code - Positioned down the straight-line sequential track */
} else {
	/* unlikely code - Shunted onto the taken side of the branch */
}

or alternatively:

if (unlikely(x)) {
	/* unlikely code - Shunted onto the taken side of the branch */
} else {
	/* likely code - Positioned down the straight-line sequential track */
}

If the compiler can definitively compute the final value of x at compile time before evaluating the likely(x) or unlikely(x) optimization passes, the conditional branch statement transitions into an unconditional jump instruction, enabling the optimizer to prune dead blocks and emit code solely for the active logical path. Programmers are free to choose between the likely or unlikely format, as both arrive at equivalent structural optimization targets. It must be explicitly emphasized that neither likely() nor unlikely() exerts any influence over the logical behavior of the software flow; their singular technical purpose is to aid the compiler in code generation optimization.

Another optimization paradigm arises when the branching behavior can be definitively resolved at runtime or compile-time without constant evaluation, such as determining whether to print diagnostic debug tracking lines. Building two independent monolithic image variants to handle verbose versus quiet execution states is an inefficient engineering choice. The codebase must simultaneously support both silent and verbose modalities. Another clear analogy is network profiling: the system must execute one specific code block when network functionalities are fully enabled, and trace an entirely separate code path when networking configurations are absent. If this architectural toggle relies on standard conditional branching, the pipeline spends countless cycles continually re-evaluating the tracking variable. To address this overhead, Linux introduces a runtime code modification technology designated as static jump label patching.

The foundational design of this jump label patching technique is to plant an atomic NOP instruction at the explicit control position where a jump might eventually be required, immediately followed by the high-probability sequential code path. If a runtime state change later mandates a branch at that junction, the bootloader or core kernel hot-patches this specific NOP placeholder, converting it into a concrete, executable branch instruction (as illustrated in Figure 12-1). The target instruction address of the NOP placeholder to be modified, alongside its matching destination branch address, is recorded within a dedicated jump entry metadata structure (struct jump_entry). These descriptor blocks are grouped by the linker toolchain within a bounded memory segment situated precisely between the symbols __start_jump_table and __stop_jump_table. The exact boundaries of __start_jump_table and __stop_jump_table, along with the metadata payload of the individual jump entries, are generated automatically by the compiler based on the source code directives.

Figure 12-1 Jump Label Modification

Copyright © 2026 Guangzhou eSmart Information Technology Co., Ltd. All Rights Reserved.
All content in this book is copyrighted by the author. For personal learning and exchange only. Commercial use without authorization is strictly prohibited. Email:

✉ 3348194517@qq.com | hai616878@gmail.com