Skip to contentSkip to conversation

objtool/klp: DWARF-based cross-build symbol correlation

RFC PATCH In review Longjun Luo 3 participants 28 messages Nov 4 – Nov 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v2 cover objtool/klp: DWARF-based cross-build symbol correlation

v1 [1] introduced a DWARF-based symbol identity as the representation layer for cross-build symbol correlation. Building on that representation, v2 shows how klp-build's existing single-object checksums and reference context are used to extend symbol-twin matching in stages, and how the resulting twins and remaining unmatched symbols are audited and classified.

Existing ELF-only correlation can produce incorrect patches in three
cases: connecting patched code to a different same-named symbol (incorrect twins), duplicating existing static state as a new variable (copied static state), and missing changes in referenced targets (missed reference changes). Across the regression suite, the baseline fails 13 tests on both GCC and Clang. In 6 cases per compiler, it miscorrelates symbols or misses changes without diagnosing the incorrect result. v2 passes all 13 of those tests on each compiler.

This is a design-review RFC, not a merge-ready series. The identity representation and twin algorithm are presented together for review; the proposed integration path is discussed in "Open Questions and Next Steps". v2 reports an error on ambiguous data references in patched code; Case D describes the state-inheritance boundary and proposed annotations.

The series contains 11 patches:

- Patches 1-3: Standalone fixes independent of DWARF (wide-string truncation, compound-literal content checksums, and preserving trailing digits in identifiers), which can be prepared as a separate fix series.
- Patches 4-6: DWARF identity reader, reduced/split DWARF support, and the CONFIG_DEBUG_INFO build check, without switching the correlation algorithm yet.
- Patches 7-10: Correlating by identity triple and checksum (Patch 7), including identities in reference checksums (Patch 8), extending twins through references from unchanged functions (Patch 9), and checking unmatched data references (Patch 10).
- Patch 11: Regression test suite covering symbol identities, LTO, DWARF variants, and state preservation.

Responses to v1 Review (Guide)
------------------------------

Thanks to Josh for reviewing v1. v2 responds as follows:

1. CONFIG_DEBUG_INFO and split/reduced DWARF: as suggested, klp-build now requires CONFIG_DEBUG_INFO and uses Kbuild's debug flags (Patch 6). Reduced and split DWARF are supported (Patch 5). See "Compatibility Limits and Build Requirements".

2. Repeated macro statics and the one-line test: building on the referencing-function idea, unchanged-function references resolve repeated statics (Patch 9). The tree-wide test reduces v1's no correlation warnings from 682/764 to zero (GCC/Clang). See "Evaluation and Performance Overhead".

3. Upgrading no correlation to an error and explicit annotations:
following this direction, v2 rejects ambiguous data references and allows verified new data (Patch 10). See Case D for the state-inheritance boundary, its kernel-wide impact, and proposed annotations.

4. Referencing functions, ThinLTO naming flags, and Full LTO: in ELF, the referencing function only approximates the defining scope, and STT_FILE and .llvm.<hash> suffixes only approximate the defining TU. Definition identity distinguishes inlined helpers' state and survives LTO renaming and STT_FILE merging; see Cases A-B.

5. Toolchain reliability and performance: v2 cross-checks DWARF against ELF and measures checksum/diff costs, including a separate memory prototype. See "Design Overview", "Evaluation and Performance Overhead", and "Open Questions and Next Steps".

Design Overview: Staged Matching and Verification of Symbol Twins
-----------------------------------------------------------------

Background and Motivation
~~~~~~~~~~~~~~~~~~~~~~~~~

This design grew out of shared challenges in kernel and userspace livepatching. In production kernel livepatching, patches are commonly managed cumulatively to avoid hook conflicts across patches; over a maintenance cycle, a single cumulative patch can accumulate changes from dozens of fixes. Under LTO, these stacked edits can shift global inlining, cross-TU promotion, and function-cloning decisions, perturbing the symbol table far beyond the edited functions and breaking heuristic correlation. Meanwhile, complex C++ scopes and optimizations in userspace livepatching similarly require a strict separation between definition ownership and the actual referencer (v1 summarized my evaluation across 17 security-fix cases in 15 C/C++ projects and 6 distributions [1]).

This motivated a correlation mechanism grounded in definition ownership and reference context. In adapting it to the kernel, v2 directly reuses klp-build's existing single-object checksum pipeline to summarize context, incorporating referenced target identities and compound-literal contents into the digest.

Supplementing and Cross-Checking ELF with DWARF
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This design takes ELF as the ground truth for emitted code and storage, using DWARF to supplement and cross-check ELF information: ELF provides the emitted symbols, sizes, bindings, and relocations, while DWARF supplies the defining TU (source) and lexical scope (scope). Starting from each ELF symbol, the reader looks up the DWARF definition and verifies agreement on symbol names, storage coverage, and STT_FILE versus CU paths; conflicts are reported, while missing information is routed to controlled fallback and reference checks. These cross-checks helped identify both upstream toolchain DWARF issues [2-6] and existing ELF/checksum corner cases (Patches 1-3 and the FIXMEs in Patches 1 and 4).

The reader explicitly handles known DWARF issues affecting kernel builds, so those cases require no compiler changes or backports.

Because the (source, scope, name) triple follows directly from standard DWARF semantics, it also provides a clean target for upstream compiler DWARF tests. Correlation can then rely on the triple and stable optimization representations (such as address pieces and shrunk storage) rather than compiler-specific suffix conventions. This can make livepatch generation substantially less sensitive to changes in compiler naming and optimization decisions.

Symbol Classification by DWARF Attribution
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Ideally, the (source, scope, name) triple alone would uniquely pair symbols across builds in both directions; in practice, the distinguishing power provided by DWARF varies. Before describing the twin algorithm, admitted symbols fall into three categories:

- Complete identity: Named functions and static variables whose triple is unique within their minimal (source, scope). The vast majority of symbols fall into this category and match directly, serving as the trustworthy foundation for subsequent context-based propagation.
- Non-unique identity: Repeated statics within the same named scope, or multiple storage pieces split from a single definition by optimization, can share a triple and require checksum and reference context to disambiguate. Anonymous-block representation choices and state-inheritance boundaries are covered in Case D.
- Absent identity: Symbols with no definition attribution in DWARF, primarily C compound literals with static storage duration (.compoundliteral.N / __compound_literal.N) and a small number of top-level assembly or special-mechanism symbols.

Four-Stage Twin Construction and Unmatched Reference Checks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Based on this classification, symbol twins are built and verified in four successive stages (Stages 1-4):

- Stage 1 (Unique identity match): Within the same identity bucket, pair the unique compatible candidate in both directions.

- Stage 2 (Intra-identity checksum disambiguation): Among remaining candidates in the same identity bucket, use symbol length and checksum to pair the unique unchanged candidate in both directions.

- Stage 3 (Reference extension and verification via unchanged functions):
For paired functions with equal checksums, compare relocation layouts in order and extend twins to compatible referenced targets. Existing twins must agree with these references; any conflict aborts patch generation. This cross-checks Stages 1 and 2, catching switches between instances of the same identity that leave the function checksum unchanged.

- Stage 4 (Controlled ELF FILE/name fallback): For remaining unpaired symbols, pair only when FILE/name is unique in both directions and raw symbol names, bindings, and DWARF availability agree on both sides; fallbacks outside the known whitelist emit a warning.

Data aliases reuse their representative symbol's twin. After the four stages complete and changed functions are identified, the tool checks unmatched data references when constructing the patch (Patch 10) to separate verified new data from ambiguous references that must be rejected. The acceptance rules for ordinary static data and compound objects are detailed in Case D.

Problem Classes and Handling Boundaries
---------------------------------------

Cases A-C illustrate incorrect twins, existing state treated as new, and missed reference changes. Case D discusses the handling boundary for state inheritance and why references beyond current disambiguation evidence must be rejected.

Case A. Incorrect twins: same name, different owner
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Case 2 in v1 [1] showed a real kernel example (Clang/LLD 23.1.0, ThinLTO):
a patch adds modules_seq_ops() and uses it in read_file_mod_stats(). Cross-TU import decisions shift, moving the unsuffixed names of m_start/m_next/m_stop/m_show between fs/namespace.c and kernel/module/procfs.c. The baseline silently pairs the two iterator sets against each other and reports 8 extra changed functions (4 in untouched fs/namespace.c), producing a module that fails to load on a duplicate m_start,1 registration; v2 reports only read_file_mod_stats() as changed and modules_seq_ops() as new.

v2 adds a test covering simultaneous edits in two TUs (Clang/LLD 23.0.0git, ThinLTO). a.c and d.c each define static volatile int state[4], and target() in b.c calls both accessors; the patch adds a bounds check and an index mask:

    /* a.c */
    int a_get(unsigned int index)
    {
+       if (index >= 4)
+           return 0;
        return state[index];
    }

    /* d.c */
    int d_get(unsigned int index)
    {
-       return state[index];
+       return state[index & 3];
    }

Both accessors should keep using their respective old arrays. However, the bounds check stops a_get() from being imported into b.c, turning its array from a promoted global back into a local; under the kernel's -always-rename-promoted-locals=false, the plain global name state previously held by a.c moves to d.c. The baseline wires patched a_get() to d.c's old array and the inlined d_get() in target() to a.c's old array.

To test the effect of reverting that flag directly, I compared keeping and restoring suffixes on the same LLVM 23 toolchain, source, and import-instr-limit=5. The table varies -always-rename-promoted-locals; modifying only a.c gives the same result as modifying both TUs:

  Flag value                Baseline result             v2 result
  ------------------------  --------------------------  --------------------
  false (kernel default)    No warning; swaps arrays    Uses each TU's array
  true                      2 warnings; new arrays      Uses each TU's array

Although reverting the flag prevents the plain-name shift, the baseline's suffix_map relies on another already-paired promoted symbol in the same TU to map old and new .llvm.<hash> suffixes. Here, a.c's array is local and unsuffixed again while d.c's hash changes with the edit, so the baseline still loses correlation and reallocates both states (Clang/LLD 22.1.8 behaves the same way under its default suffix naming; see test-identity-thinlto-name-drift.sh). Toggling the compiler naming flag cannot replace defining-TU attribution, and it also shows why DWARF cannot be consulted only on name collisions: under plain-name drift, the miscorrelated name is already unique.

Miscorrelation is not limited to LTO. In test-identity-static-replace.sh (GCC, non-LTO), a patch removes retired() and adds added(). Each function has a local static volatile long count, as does unchanged():

- long retired(long value)
- {
- static volatile long count;
- return count += value;
- }
-

    long unchanged(long value)
    {
        static volatile long count;
        return count += value;
    }

+
+ long added(long value)
+ {
+ static volatile long count;
+ return count += value;
+ }

GCC emits two count.N symbols before and after the patch. The baseline warns about the removed function but pairs added()'s new counter with unchanged()'s live counter by symbol-table position. In v2, the new counter's scope /added/ has no original candidate, so it gets new storage.

Case B. Existing state treated as new: referencing function != defining scope
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Case 1 in v1 [1] showed JFS's same-named noisy counters (GCC 16.2.1, non-LTO): adding a counter renumbers existing instances, causing the baseline to copy old state into the patch and reviving an exhausted 5-message warning budget; v2 reuses the original storage via function scope (test-identity-static-scope.sh). An inlined-initializer test in v2 further separates "who references it" from "who defines it".

In test-identity-inline-helpers.sh, two always_inline helpers each define a block-local static volatile int done via ONCE_INIT to guard one-time seed initialization:

    static inline __attribute__((always_inline)) long initialize_rx(long value)
    {
        ONCE_INIT(rx_seed, 1);
        return value ^ rx_seed;
    }

    static inline __attribute__((always_inline)) long initialize_tx(long value)
    {
        ONCE_INIT(tx_seed, 2);
        return value ^ tx_seed;
    }

Originally target() only calls RX; the patch adds TX before it, with no other unchanged caller available to recover old state:

long target(long value)
{

+       value = initialize_tx(value);
        return initialize_rx(value);
    }

Only TX should get a new done flag while RX keeps its original flag. Under GCC 16.2.1 non-LTO, the baseline warns and copies both flags, allowing RX initialization to run again; v2 allocates only TX's flag and binds RX's reference to the original storage (under Clang 22.1.8 non-LTO, the baseline also correlates it because the symbol name encodes the function).

After inlining, both done flags are referenced by outer target() in ELF, so grouping by referencing function cannot distinguish them; their DWARF defining scopes remain /initialize_rx/ and /initialize_tx/, allowing v2 to distinguish new and existing state in a modified function without author annotations. Full LTO's merged STT_FILE entries are handled by the same definition identity; see v1 [1] Case 3 and test-identity-full-lto.sh.

Case C. Missed reference changes: identities in checksums and references
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Hashing only the normalized target name misses switches between same-named targets in different TUs or scopes. v2 hashes the target's identity triple into both function and data reference checksums; see test-checksum-identity.sh and test-checksum-identity-source.sh.

Multiple split instances can still share one identity. In test-identity-pieces.sh (Clang 22.1.8, non-LTO), the compiler splits static struct { long first, second; } state into two 8-byte objects, state.0 and state.1. Unchanged set() and sum() access both fields, while the patch switches the field read by target():

    long target(void)
    {
-       return state.first;
+       return state.second;
    }

The instruction bytes are identical, and both state.0 and state.1 have the same normalized name and DWARF triple, so the baseline checksum is unchanged and target() is omitted from the patch. In v2, Stage 3 uses the references from unchanged set() and sum() to establish each piece's twin, and then detects a twin conflict when checking target()'s reference. Equal function checksums alone are not sufficient; relocation references must still agree with established twins.

Case D. Handling boundary: inheritance of static state
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

An anonymous block can narrow visibility without changing storage or use:

        int *a;
-       static int b = 0; a = &b;
+       { static int b = 0; a = &b; }
        return (*a)++;

In this example, GCC 16.2.1 and Clang 22.1.8 (non-LTO, -O0 and -O2) produce byte-identical stripped objects for both forms. A livepatch should keep using the same state. The triple therefore records named scopes, without anonymous-block depth or ordinals.

When multiple same-named statics exist within the same named scope, their state-inheritance mapping must be determined. Denote the old instances by A, B and the new instances by X, Y: positional pairing chooses A->X and B->Y, whereas the author may have deleted A and added X while keeping only B->Y. When their types and initial values are identical, neither source definitions nor compiled positions distinguish the two intents. Whether X should inherit A's live state or start from its initial value requires author intent; even a human reviewer cannot make a unique choice from the binary alone.

v2 first resolves what existing evidence can prove: when a function is unchanged, twins are extended along its references; another unchanged caller can also establish those twins (test-identity-inline-state.sh). For remaining unpaired ordinary data, symbols with a DWARF identity and no old candidate are treated as new, whereas references with existing same-identity candidates report an error.

test-identity-once-insert.sh (GCC 16.2.1, Clang 22.1.8, non-LTO) shows the consequence when a patch inserts a new initializer between two existing ones:

    long target(long value)
    {
        ONCE_INIT(first, 1);
+       ONCE_INIT(extra, 3);
        ONCE_INIT(second, 2);
-       return value ^ first ^ second;
+       return value ^ first ^ second ^ extra;
    }

Each ONCE_INIT expansion defines a block-local static done. The test requires first and second to keep their old flags and only extra to get a new flag. The baseline warns and copies all three done flags, allowing the old initializations to run again; v2 rejects the unresolved reference and reports the function, offset, and old candidate:

    orig.o: error: objtool: target+0x2: unresolved data reference:
        done.2 (.../once_insert.c, /target/, done), old candidate done.0[4]

Symbol-table order itself can also change without adding or removing statics. In test-identity-branch-hint.sh (GCC, Clang, non-LTO), merely flipping branch-probability hints reorders the two counters in the symbol table and causes the baseline to swap them silently. The ACPICA kernel test patch hits the same boundary (Clang/LLD 22.1.8, ThinLTO): the two 1-byte BSS flags _done and _done.2 in acpi_ex_system_do_stall share the same triple and initial value; the baseline pairs them by position, whereas v2 reports an error when the modified function references the ambiguous instances.

Adding another NL_SET_ERR_MSG() call to a function that already has one (GCC 16.2.1, non-LTO) produces two __msg objects with the same identity; the baseline matches the old __msg by unchanged content and copies only the new one, whereas v2 rejects the reference because an old candidate with the same identity exists. Dedicated rules can be added for objects known to be used only for diagnostic messages, provided object usage is well-defined; by contrast, in test-identity-readonly-address.sh, the address of a read-only descriptor is itself part of the interface.

Static compound literals lack a source definition identity and face a similar ambiguity between new allocation and modified storage. v2 currently reuses a unique unchanged candidate within the reference context and rejects the rest, as changed content alone cannot distinguish a new literal from an edit to an existing one.

To quantify how often this boundary is reached in a full kernel, I kept data and references fixed in the audited ThinLTO vmlinux.o build and perturbed each main function's checksum one by one to simulate modifying that function: out of 67,673 main functions, only 62 (0.09%) trigger an unresolved data reference error, and only 33 (0.05%) involve writable data sections.

For these rare cases, I plan a separate RFC for two annotations prototyped locally, following the source-annotation model of kpatch's KPATCH_IGNORE_FUNCTION() [8]. KLP_NEW_STATIC() would mark enclosed state as new; KLP_DEL_STATIC() would omit the operation while retaining a matching placeholder. Remaining instances would be paired by DWARF declaration position, with their relative source order preserved.

Standalone Fixes Independent of DWARF (Patches 1-3)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Patches 1-3 fix wide-string truncation (test-checksum-wide-string.sh), compound-literal contents omitted from reference checksums (test-identity-compound.sh), and identifier digits lost when stripping suffixes (test-identity-thinlto-crc32.sh). On a real DisplayPort PSR table edit, the baseline misses the change, while v2 detects drm_dp_psr_setup_time() as changed.

Evaluation and Performance Overhead
-----------------------------------

One-Line Patch Tests
~~~~~~~~~~~~~~~~~~~~

To test the macro-static issue raised in v1 review, I added a blank line at the top of each .c file, first under drivers/gpu/drm/radeon (103 files), then tree-wide. Both tests used fix-patch-lines, CONFIG_LIVEPATCH=y, CONFIG_DRM_RADEON=m, and DWARF 5. Radeon used non-LTO; the tree-wide builds used GCC non-LTO / Clang ThinLTO and covered vmlinux.o plus 16 modules. v1 and v2 ran checksum and diff on the same build artifacts:

  Build                     v1 no-corr  v2 no-corr  v2 errors  v2 changed/new
  ------------------------  ----------  ----------  ---------  --------------
  GCC 16.2.1, tree-wide            682           0          0           0 / 0
  Clang 22.1.8, tree-wide          764           0         1*         -- / --
  GCC 16.2.1, Radeon               400           0          0           0 / 0
  Clang 22.1.8, Radeon             399           0          0           0 / 0

Radeon audit logs confirm that Stage 3 pairs all 400/399 __print_once symbols, including 45 in r100.o and both flags in r100_reloc_pitch_offset().

* The tree-wide edit includes vDSO sources, which klp-build normally rejects. In this direct objtool test, all 16 Clang modules pass; vmlinux.o stops at changed data: raw_data because only the embedded vDSO build IDs differ. The baseline and v1 reject the same input. -- marks the unfinished changed/new check; correlation completed with zero warnings.

Whole-Object vmlinux.o Audit and Multi-Config Self-Comparison
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Across three full vmlinux.o build pairs, v2 establishes twins for all admitted functions, leaves 0 unmatched ordinary symbols, and leaves only compound objects unused by the patched code, with 0 no correlation or FILE/source warnings.

Both tools recompute checksums and run diff on the same vmlinux.o build artifacts against klp-build at 945537271df1 (audit counters are collected via a separate local patch; exit status, diagnostics, and output bytes are identical to the uninstrumented tool). The table counts nonzero admitted symbols on the patched side (excluding special sections and dont_correlate symbols), recording newly established twins at each stage:

  New twins / final disposition  ThinLTO  Full LTO  GCC non-LTO
  -----------------------------  -------  --------  -----------
  Identity: address match         101414    101172       104204
  Identity: name fallback              4         4         1593
  Identity: assembly CU              182       189           82
  Identity + checksum               1012      1012         1014
  Unchanged-func references         2500      2511         2402

ELF FILE/name fallback 65 65 64
Alias resolution 0 0 7
Unmatched compound objects 334 334 347
Other unmatched symbols 0 0 0
Total admitted symbols 105511 105287 109713

The first three rows break down Stage 1 by DWARF lookup method. In the ThinLTO pair, reference extension pairs 2,215 functions (including 2,210 static-call trampolines) and 285 data symbols, while the ELF fallback adds 65 known-mechanism pairs. All inputs are x86-64 DWARF 5 builds: ThinLTO getpid uses Clang/LLD 22.1.8 on 24a19b953e2a; Full LTO and GCC non-LTO version_show use Clang/LLD 22.1.8 and GCC 16.2.1 on 4403278e4ea2 (object-level audit rerun).

In addition, the fixture suite covers full/reduced DWARF 4/5 and DWARF 4 .dwp lookup with original .dwo files removed; 6 kernel build inputs (GCC non-LTO and Clang ThinLTO, each with normal, reduced, and split DWARF 5) also pass self-comparison with 0 unresolved ordinary symbols and 0 warnings (split kernel self-comparisons read the original .dwo files).

Regression Test Suite Comparison
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Results on the 81-test suite for the baseline and v2 are shown below (all skips are declared compiler restrictions; 0 runtime probe skips):

  Compiler      Tool      Pass  Fail  Skip
  ------------  --------  ----  ----  ----
  GCC 16.2.1    Baseline    58    13    10
  GCC 16.2.1    v2          68     3    10
  Clang 22.1.8  Baseline    65    13     3
  Clang 22.1.8  v2          75     3     3

Expected ambiguity rejections count as passes. All 13 baseline failures pass under v2. In baseline diff, 6 tests per compiler miscorrelate or miss changes without diagnosing the incorrect result.

The table keeps raw test failures: compound fails its function-checksum assertion but reports changed data if baseline diff continues; GCC's weak-cold fails because its assertion does not recognize the baseline's .klp.sym reference format. The 3 v2 failures are intentional compatibility rejections (see "Compatibility Limits and Build Requirements").

The suite can be run on the patched tree with:

    make -C tools/objtool
    CC=gcc OBJTOOL="$PWD/tools/objtool/objtool" \
        bash tools/objtool/tests/run-tests.sh
    CC=clang OBJTOOL="$PWD/tools/objtool/objtool" \
        bash tools/objtool/tests/run-tests.sh

Performance and Memory Overhead
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Performance was measured on the ThinLTO getpid build pair (~1.081 GiB per vmlinux.o) on an Intel Core i7-11850H with 30.6 GiB RAM and libdw 0.196, using 1 warmup and the median of 3 serial alternating runs (audit logging disabled; klp diff reads precomputed checksums from the respective tool, and klp checksum uses --dry-run):

  Operation     Tool                Time (s)  Peak RSS (MiB)
  ------------  ------------------  --------  --------------
  klp diff      Baseline                3.62            3566
  klp diff      v2                     28.71            5604
  klp diff      Memory prototype       26.73            3968
  klp checksum  Baseline                4.48            3971
  klp checksum  v2                     15.15            6006
  klp checksum  Memory prototype       13.88            5149

v2 adds ~25 s to diff and ~11 s to single-side checksum, alongside the two full kernel builds performed by klp-build. A separate memory prototype reduces peak RSS by 29% for diff and 14% for checksum without changing matching rules or DWARF checks. It preserves all 81 per-test outcomes. On 13 retained kernel cases, checksums, recorded twins, diagnostics and output bytes are identical to v2. The prototype remains in a separate branch.

Compatibility Limits and Build Requirements
-------------------------------------------

Two Compatibility Limits (3 Policy Test Failures)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The 3 test failures under v2 reflect deliberate policy boundaries:

- Source-level linkage flips (local-to-global-flip): Converting a TU-local definition into an external one (or vice versa) changes the source qualifier in its identity and affects subsequent name resolution, so this series rejects it. When a patch needs to expose a local helper, it can keep the static definition and add a thin global wrapper.
- Duplicate compilation instances (sympos, sympos-vmlinux): These two tests compile the same source file into multiple objects in one link, producing instances with identical definition identities and raw ELF names. The triple does not distinguish duplicate build instances, and this series avoids depending on link order so correlation remains unaffected by newly added source files and LTO repartitioning.

Build Requirements and Remaining Limitations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

klp-build requires CONFIG_DEBUG_INFO and reuses Kbuild's debug flags. objtool klp now requires libdw in addition to libxxhash. If either library is missing, objtool still builds without klp support.

With CONFIG_DEBUG_INFO_SPLIT, klp-build uses llvm-dwp by default to save matching .dwo files in a .dwp package. The libdw >= 0.191 requirement applies only to DWP lookup.

The remaining toolchain and inherited implementation limitations are:

- llvm-dwp 22.1.8 hangs on some GCC DWARF 5 inputs containing DW_FORM_implicit_const (such as lib/bsearch.dwo), blocking .dwp packaging on those inputs.
- LLD Full LTO with --plugin-opt=dwo_dir requires
-mllvm -split-dwarf-cross-cu-references=true to preserve per-TU paths (Kbuild does not enable this mode).
- The existing ELF reader (elf.c) can attach an overridden weak definition's local foo.cold to the surviving strong foo, causing both the baseline and this series to miss cold-only edits. This series preserves separate CU identities for both cold fragments and records the parent-attachment issue as a FIXME for a separate fix.

Open Questions and Next Steps
-----------------------------

This is a substantial change: 1,755 of the 4,010 added lines are tests, and about 1,300 implement the DWARF reader and identity layer. Before preparing a merge-ready series, I would like explicit maintainer guidance on how to stage its integration. My proposed sequence is:

- Submit Patches 1-3 separately with their regression tests.
- Prepare the identity representation, DWARF reader, and ELF cross-checks as a foundation that can be reviewed separately from twin matching.
- Integrate the matching changes in Patches 7-10 together: reference extension pairs repeated statics, and the reference checks reject remaining ambiguous data references.
- Submit the memory optimizations separately and pursue explicit state annotations in a separate RFC.

This RFC retains upstream expectations for the three policy tests to make the behavior changes visible. For the merge-ready series, I will update their assertions to reflect the agreed policy and move Patch 11's tests alongside the changes they cover.

I plan to add triple-based DWARF correctness tests in upstream compilers, using known source definitions and optimization variants to check coverage, non-conflict, and attribution accuracy (with clones, pieces, anonymous scopes, and folding checked according to their respective semantics) without locking in a specific DIE layout or suffix scheme. My earlier bug reports and fixes on IPA-ICF, weak-override attribution, and assembly CU selection [2-6] provide concrete test inputs, and the same attribution model applies to consumers such as AutoFDO [7].

I would appreciate feedback on four questions:

1. Would maintainers prefer to integrate the identity representation and reader first, then evolve twin matching in a follow-up series, or keep them together for integration?
2. Is constraining candidates by the identity triple and extending twins via checksums and unchanged-function references the right foundation for correlation, and how should the final ELF fallback be bounded?
3. Is the handling boundary in Case D reasonable, and should explicit state annotations (such as KLP_NEW_STATIC() / KLP_DEL_STATIC()) be pursued in a separate RFC?
4. How should we structure upstream compiler tests around the identity triple to protect both DWARF producers and livepatch consumers?

Changes Since v1
----------------

Compared with v1, v2 adds reference-based twin extension through unchanged functions, unmatched data reference checks, compound-literal content checksums, reduced/split DWARF support, assembly and constrained name lookup, data-alias reuse, and explicit ELF fallback diagnostics; it also fixes wide-string truncation, trailing digits in identifiers, and __UNIQUE_ID counter shifts.

Related Work & References
-------------------------

[1] RFC v1 (including userspace evaluation and kernel runtime cases):

    https://lore.kernel.org/all/[email protected]/

[2] GCC PR126355 (address-bearing DIEs for IPA-ICF wrappers, fixed for GCC 17):
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126355
[3] GCC PR126348 (abstract-origin TU attribution across LTO partitions, patch under review):
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126348
[4] GDB PR34741 (overridden weak definition address resolution):

    https://sourceware.org/bugzilla/show_bug.cgi?id=34741
[5] LLDB #233545 (fix overridden weak function lookup):
    https://github.com/llvm/llvm-project/pull/233545
[6] GNU AS (CU source selection for preprocessed assembly):
    https://sourceware.org/pipermail/binutils/2026-October/152209.html
[7] AutoFDO #267 (fix absolute DWARF 5 range handling, merged):
    https://github.com/google/autofdo/pull/267

[8] kpatch source annotations (ignoring specified functions or sections):
https://github.com/dynup/kpatch/blob/master/kmod/patch/kpatch-macros.h

Longjun Luo (11):
objtool/klp: Fix checksums and copying of wide strings objtool/klp: Hash compound literals by content objtool/klp: Preserve identifier digits when stripping suffixes objtool/klp: Read symbol identities from DWARF objtool/klp: Support reduced and split DWARF
klp-build: Require CONFIG_DEBUG_INFO
objtool/klp: Correlate symbols using DWARF identities objtool/klp: Include symbol identities in reference checksums objtool/klp: Extend twins through unchanged function references objtool/klp: Check unmatched data references objtool/klp: Test symbol identity and state preservation

 scripts/livepatch/klp-build                   |   23 +-
 tools/objtool/Build                           |    4 +-
 .../objtool/Documentation/klp-test-design.txt |    6 +-
 tools/objtool/Makefile                        |   12 +-
 tools/objtool/elf.c                           |  100 +-
 tools/objtool/include/objtool/elf.h           |   43 +-
 tools/objtool/include/objtool/klp-dwarf.h     |   22 +
 tools/objtool/include/objtool/klp-identity.h  |   26 +
 tools/objtool/include/objtool/klp.h           |    3 +
 tools/objtool/klp-checksum.c                  |  155 ++-
 tools/objtool/klp-diff.c                      | 1041 ++++++++------
 tools/objtool/klp-dwarf.c                     | 1206 +++++++++++++++++
 tools/objtool/klp-identity.c                  |   94 ++
 .../tests/generic/fixtures/identity/alias.c   |   25 +
 .../generic/fixtures/identity/block_data.c    |   22 +
 .../generic/fixtures/identity/branch_hint.c   |   28 +
 .../generic/fixtures/identity/cold_strong.c   |   13 +
 .../generic/fixtures/identity/cold_user.c     |   15 +
 .../generic/fixtures/identity/cold_weak.c     |   10 +
 .../tests/generic/fixtures/identity/common.h  |   15 +
 .../generic/fixtures/identity/compound.c      |   65 +
 .../generic/fixtures/identity/consumer.c      |   30 +
 .../tests/generic/fixtures/identity/entries.c |   10 +
 .../generic/fixtures/identity/full_left.c     |   16 +
 .../generic/fixtures/identity/full_right.c    |    6 +
 .../generic/fixtures/identity/icf_left/same.c |   16 +
 .../fixtures/identity/icf_right/same.c        |   16 +
 .../fixtures/identity/inline_helpers.c        |   27 +
 .../tests/generic/fixtures/identity/lookup.c  |   13 +
 .../generic/fixtures/identity/metadata.c      |   24 +
 .../generic/fixtures/identity/move_left.c     |    9 +
 .../generic/fixtures/identity/move_right.c    |    8 +
 .../generic/fixtures/identity/new/same.c      |    6 +
 .../generic/fixtures/identity/new_driver.c    |   15 +
 .../generic/fixtures/identity/new_provider.c  |    4 +
 .../generic/fixtures/identity/old/same.c      |    6 +
 .../tests/generic/fixtures/identity/once.h    |   16 +
 .../generic/fixtures/identity/once_inline.c   |   28 +
 .../generic/fixtures/identity/once_insert.c   |   21 +
 .../tests/generic/fixtures/identity/pieces.c  |   28 +
 .../fixtures/identity/promotion_bounds_a.c    |   12 +
 .../fixtures/identity/promotion_bounds_b.c    |   12 +
 .../fixtures/identity/promotion_bounds_d.c    |   12 +
 .../fixtures/identity/promotion_crc32.c       |    8 +
 .../fixtures/identity/promotion_crc32_user.c  |   15 +
 .../generic/fixtures/identity/provider.c      |    9 +
 .../generic/fixtures/identity/readonly.c      |   30 +
 .../tests/generic/fixtures/identity/record.h  |    8 +
 .../tests/generic/fixtures/identity/rx.c      |    4 +
 .../tests/generic/fixtures/identity/shrink.c  |   19 +
 .../generic/fixtures/identity/source_driver.c |   11 +
 .../fixtures/identity/static_replace.c        |   29 +
 .../generic/fixtures/identity/static_scope.c  |   31 +
 .../fixtures/identity/static_scope_reorder.c  |   31 +
 .../tests/generic/fixtures/identity/tx.c      |    4 +
 .../generic/fixtures/identity/unique_id.c     |   50 +
 .../generic/fixtures/identity/wide_string.c   |   36 +
 .../generic/test-checksum-identity-source.sh  |   24 +
 .../tests/generic/test-checksum-identity.sh   |   32 +
 .../generic/test-checksum-wide-string.sh      |   39 +
 .../tests/generic/test-identity-alias.sh      |   26 +
 .../tests/generic/test-identity-block-data.sh |   12 +
 .../generic/test-identity-branch-hint.sh      |   18 +
 .../tests/generic/test-identity-compound.sh   |   33 +
 .../tests/generic/test-identity-dwarf.sh      |   34 +
 .../tests/generic/test-identity-full-lto.sh   |   30 +
 .../tests/generic/test-identity-icf.sh        |   23 +
 .../generic/test-identity-inline-helpers.sh   |   25 +
 .../generic/test-identity-inline-state.sh     |   21 +
 .../tests/generic/test-identity-metadata.sh   |   20 +
 .../tests/generic/test-identity-new-tu.sh     |   24 +
 .../generic/test-identity-once-insert.sh      |   19 +
 .../tests/generic/test-identity-pieces.sh     |   27 +
 .../generic/test-identity-readonly-address.sh |   23 +
 .../tests/generic/test-identity-shrink.sh     |   19 +
 .../tests/generic/test-identity-source.sh     |   29 +
 .../tests/generic/test-identity-split.sh      |   34 +
 .../generic/test-identity-static-replace.sh   |   20 +
 .../generic/test-identity-static-scope.sh     |   35 +
 .../generic/test-identity-thinlto-crc32.sh    |   23 +
 .../test-identity-thinlto-name-drift.sh       |   79 ++
 .../test-identity-thinlto-promotion.sh        |   26 +
 .../tests/generic/test-identity-unique-id.sh  |   31 +
 .../tests/generic/test-identity-weak-cold.sh  |   35 +
 .../tests/generic/test-thinlto-ambiguity.sh   |    2 +-
 .../tests/generic/test-thinlto-local.sh       |    4 +-
 tools/objtool/tests/identity-lib.sh           |   65 +
 tools/objtool/tests/lib.sh                    |    7 +-
 .../tests/x86/fixtures/identity_assembly.S    |   33 +
 .../tests/x86/fixtures/warn_reference.c       |   36 +
 .../tests/x86/test-identity-assembly.sh       |   19 +
 .../objtool/tests/x86/test-warn-reference.sh  |   21 +
 92 files changed, 4010 insertions(+), 486 deletions(-)
 create mode 100644 tools/objtool/include/objtool/klp-dwarf.h
 create mode 100644 tools/objtool/include/objtool/klp-identity.h
 create mode 100644 tools/objtool/klp-dwarf.c
 create mode 100644 tools/objtool/klp-identity.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/alias.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/block_data.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/branch_hint.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_strong.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_user.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_weak.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/common.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/compound.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/consumer.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/entries.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/full_left.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/full_right.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/icf_left/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/icf_right/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/inline_helpers.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/lookup.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/metadata.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/move_left.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/move_right.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new_driver.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new_provider.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/old/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once_inline.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once_insert.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/pieces.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_a.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_b.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_d.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/provider.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/readonly.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/record.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/rx.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/shrink.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/source_driver.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_replace.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_scope.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_scope_reorder.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/tx.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/unique_id.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/wide_string.c
 create mode 100755 tools/objtool/tests/generic/test-checksum-identity-source.sh
 create mode 100755 tools/objtool/tests/generic/test-checksum-identity.sh
 create mode 100755 tools/objtool/tests/generic/test-checksum-wide-string.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-alias.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-block-data.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-branch-hint.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-compound.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-dwarf.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-full-lto.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-icf.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-inline-helpers.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-inline-state.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-metadata.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-new-tu.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-once-insert.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-pieces.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-readonly-address.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-shrink.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-source.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-split.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-static-replace.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-static-scope.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-crc32.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-name-drift.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-promotion.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-unique-id.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-weak-cold.sh
 create mode 100644 tools/objtool/tests/identity-lib.sh
 create mode 100644 tools/objtool/tests/x86/fixtures/identity_assembly.S
 create mode 100644 tools/objtool/tests/x86/fixtures/warn_reference.c
 create mode 100755 tools/objtool/tests/x86/test-identity-assembly.sh
 create mode 100755 tools/objtool/tests/x86/test-warn-reference.sh

base-commit: 945537271df1e3680be9b32af7041ac20f1184fb

Signature · 1 line
2.55.0
#1 lore raw

RFC PATCH v2 1/11 objtool/klp: Fix checksums and copying of wide strings

SHF_STRINGS sections can contain elements wider than one byte. Using strlen() truncates their contents at an embedded zero byte, hiding changes and copying incomplete strings into the patch.

Find the terminating zero element using sh_entsize. Use the resulting length for both reference checksums and copying, and propagate errors through checksum calculation. Keep single-byte string handling unchanged.

Add a test for two- and four-byte strings, including references from data.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

Fixes 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Fixes 873a2208ea31 ("objtool/klp: Calculate object checksums")
Assisted-by LLM
---
 tools/objtool/elf.c                           | 29 +++++++++
 tools/objtool/include/objtool/elf.h           |  1 +
 tools/objtool/klp-checksum.c                  | 44 ++++++++-----
 tools/objtool/klp-diff.c                      | 14 ++--
 .../tests/generic/fixtures/identity/common.h  | 15 +++++
 .../generic/fixtures/identity/wide_string.c   | 36 ++++++++++
 .../generic/test-checksum-wide-string.sh      | 39 +++++++++++
 tools/objtool/tests/identity-lib.sh           | 65 +++++++++++++++++++
 8 files changed, 223 insertions(+), 20 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/common.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/wide_string.c
 create mode 100755 tools/objtool/tests/generic/test-checksum-wide-string.sh
 create mode 100644 tools/objtool/tests/identity-lib.sh
tools/objtool/elf.c +29 -0
index a791f4ea6ec1..71f1a272e468 100644
@@ -1382,6 +1382,35 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
 	return elf;
 }
 
+/* Wide strings, such as the u16 PSR setup table, end at an all-zero element. */
+ssize_t elf_string_len(struct section *sec, unsigned long offset)
+{
+	size_t entsize = sec->sh.sh_entsize, size, len;
+	const char *str;
+
+	/*
+	 * FIXME: GCC's range_string() uses a one-past-end string pointer.
+	 * strlen() reads past the section; preserve the string base and addend.
+	 */
+	if (entsize == 1)
+		return strlen(sec->data->d_buf + offset);
+
+	if (!entsize || offset >= sec_size(sec)) {
+		ERROR("%s+%#lx: invalid string offset or character size", sec->name, offset);
+		return -1;
+	}
+
+	str = sec->data->d_buf + offset;
+	size = sec_size(sec) - offset;
+	for (len = 0; size - len >= entsize; len += entsize) {
+		if (!memchr_inv(str + len, 0, entsize))
+			return len;
+	}
+
+	ERROR("%s+%#lx: unterminated string", sec->name, offset);
+	return -1;
+}
+
 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str)
 {
 	unsigned int offset;
tools/objtool/include/objtool/elf.h +1 -0
index a82517a76a0f..5d7ddadf9471 100644
@@ -179,6 +179,7 @@ struct symbol *elf_create_section_symbol(struct elf *elf, struct section *sec);
 void *elf_add_data(struct elf *elf, struct section *sec, const void *data,
 		   size_t size);
 
+ssize_t elf_string_len(struct section *sec, unsigned long offset);
 unsigned int elf_add_string(struct elf *elf, struct section *strtab, const char *str);
 
 struct reloc *elf_create_reloc(struct elf *elf, struct section *sec,
tools/objtool/klp-checksum.c +28 -16

102 lines not on this page · raw message

tools/objtool/klp-diff.c +10 -4
index 34da55e45609..6321f691adbb 100644
@@ -1548,12 +1548,18 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 	 * version of the string section.  Now copy the string itself.
 	 */
 	if (is_string_sec(patched_sym->sec)) {
-		const char *str = patched_sym->sec->data->d_buf + addend;
+		ssize_t len = elf_string_len(patched_sym->sec, addend);
+		const char *str;
 
-		__dbg_clone("\"%s\"", escape_str(str));
+		if (len < 0)
+			return -1;
+		str = patched_sym->sec->data->d_buf + addend;
+		if (patched_sym->sec->sh.sh_entsize == 1)
+			__dbg_clone("\"%s\"", escape_str(str));
 
-		addend = elf_add_string(e->out, out_sym->sec, str);
-		if (addend == -1)
+		addend = ALIGN(sec_size(out_sym->sec), out_sym->sec->sh.sh_addralign);
+		if (!elf_add_data(e->out, out_sym->sec, str,
+				  len + patched_sym->sec->sh.sh_entsize))
 			return -1;
 	}
 
tools/objtool/tests/generic/fixtures/identity/common.h new file +15 -0
new file mode 100644
index 000000000000..d0f56751fcd9
@@ -0,0 +1,15 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#ifndef KLP_TEST_IDENTITY_H
+#define KLP_TEST_IDENTITY_H
+
+#ifdef __clang__
+/* Use noinline on Clang to keep calls visible in these tests. */
+#define NOIPA __attribute__((noinline))
+#else
+#define NOIPA __attribute__((noipa))
+#endif
+#define MODINFO static const char modinfo[] \
+	__attribute__((used, section(".modinfo"))) = "name=vmlinux"
+
+#endif
tools/objtool/tests/generic/fixtures/identity/wide_string.c new file +36 -0
new file mode 100644
index 000000000000..4b25781e1827
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+#ifdef WIDE32
+#ifdef PATCHED
+#define TEXT U"\x014a\x0113\x00dc\x00a5\x006e\x0038"
+#else
+#define TEXT U"\x014a\x0113\x00dc\x00a5\x006e\x0037"
+#endif
+#else
+#ifdef PATCHED
+#define TEXT u"\x014a\x0113\x00dc\x00a5\x006e\x0038"
+#else
+#define TEXT u"\x014a\x0113\x00dc\x00a5\x006e\x0037"
+#endif
+#endif
+
+#ifdef OBJECT
+struct descriptor {
+	const void *text;
+};
+
+const struct descriptor descriptor = { TEXT };
+
+NOIPA const struct descriptor *target(void)
+{
+	return &descriptor;
+}
+#else
+NOIPA const void *target(void)
+{
+	return TEXT;
+}
+#endif
tools/objtool/tests/generic/test-checksum-wide-string.sh new file +39 -0
new file mode 100755
index 000000000000..426adc42337a
@@ -0,0 +1,39 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Wide strings contain zero bytes before their terminating zero element.
+. "$(dirname "$0")/../identity-lib.sh"
+
+wide_section()
+{
+	in_sections "$1" | sed 's/^ *\[[ 0-9]*\] *//' |
+		awk -v width="$width" '$7 ~ /S/ && $6+0 == width { print $1; exit }'
+}
+
+setup
+for width in 2 4; do
+	flags=()
+	[ "$width" = 4 ] && flags=(-DWIDE32)
+	build_pair identity/wide_string.c "${flags[@]}"
+	input="$(wide_section patched.o)"
+	[ -n "$input" ] || fail "missing $width-byte string section"
+
+	assert_checksum_differs target
+	run_diff
+	assert_patched target
+	output="$(wide_section out.o)"
+	[ -n "$output" ] || fail "wide string was not copied"
+	$OBJCOPY -O binary --only-section="$input" "$workdir/patched.o" "$workdir/expected.bin" ||
+		fail "cannot extract input string"
+	$OBJCOPY -O binary --only-section="$output" "$workdir/out.o" "$workdir/actual.bin" ||
+		fail "cannot extract output string"
+	cmp -s "$workdir/expected.bin" "$workdir/actual.bin" || fail "wide string was truncated"
+	partial_link "$workdir/linked.o" "$workdir/out.o" || fail "wide-string output does not link"
+
+	build_pair identity/wide_string.c "${flags[@]}" -DOBJECT
+	assert_checksum_differs descriptor
+	run_diff 255
+	assert_diff_log 'changed data: descriptor'
+done
+
+pass "wide-string checksums and copies cover every element"
tools/objtool/tests/identity-lib.sh new file +65 -0

65 lines not on this page · raw message

Signature · 1 line
2.55.0
#2 lore raw

Re: [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] objtool/klp: unadjusted PC-relative string addend in clone_reloc() --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] objtool/klp: unadjusted PC-relative string addend in clone_reloc() --

commit fd88c78f1c913d4c955b6342d4e45b4274643124
Author: Longjun Luo <[email protected]>

objtool/klp: Fix checksums and copying of wide strings

This commit adds elf_string_len() to compute string lengths in SHF_STRINGS sections using sh_entsize so that wide strings with embedded zero bytes are not truncated by strlen() during checksum calculation and livepatch relocation cloning.

Quoted 16 lines
tools/objtool/klp-diff.c +5 -2
index 34da55e456097..6321f691adbbd 100644
@@ -1548,12 +1548,18 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 	 * version of the string section.  Now copy the string itself.
 	 */
 	if (is_string_sec(patched_sym->sec)) {
-		const char *str = patched_sym->sec->data->d_buf + addend;
+		ssize_t len = elf_string_len(patched_sym->sec, addend);
+		const char *str;
 
-		__dbg_clone("\"%s\"", escape_str(str));
+		if (len < 0)
+			return -1;
+		str = patched_sym->sec->data->d_buf + addend;

tools/objtool/klp-diff.c static int clone_reloc(struct elfs *e, struct reloc *patched_reloc, · line 1564

Quoted 10 lines
tools/objtool/klp-diff.c +5 -2
+		if (patched_sym->sec->sh.sh_entsize == 1)
+			__dbg_clone("\"%s\"", escape_str(str));
 
-		addend = elf_add_string(e->out, out_sym->sec, str);
-		if (addend == -1)
+		addend = ALIGN(sec_size(out_sym->sec), out_sym->sec->sh.sh_addralign);
+		if (!elf_add_data(e->out, out_sym->sec, str,
+				  len + patched_sym->sec->sh.sh_entsize))
 			return -1;
 	}

[Severity: Medium]
What happens in clone_reloc() when a patched function on x86_64 references a string section via a PC-relative relocation such as R_X86_64_PC32?

In checksum_update_insn(), the relocation addend is adjusted before calling elf_string_len():

tools/objtool/klp-checksum.c:checksum_update_insn() {
	...
	offset = arch_insn_adjusted_addend(insn, reloc);

	if (is_string_sec(sym->sec)) {
		ssize_t size = elf_string_len(sym->sec, sym->offset + offset);
	...
}

However, in clone_sym_relocs() -> convert_reloc_sym() -> convert_reloc_sym_to_secsym() -> clone_reloc(), addend comes directly from reloc_addend(patched_reloc), which still carries the -4 PC-relative bias on x86_64.

When the target string is at offset 0 of the section (so addend is -4):
- For wide strings (sh_entsize > 1), passing addend as unsigned long offset to elf_string_len() wraps to 0xfffffffffffffffcUL, which triggers the offset >= sec_size(sec) error and aborts klp diff.
- For single-byte strings (sh_entsize == 1), elf_string_len() and elf_add_data() read 4 bytes before patched_sym->sec->data->d_buf.

When the target string is at offset >= 4, elf_string_len() starts 4 bytes before the string (inside the preceding string's terminator, returning 0 for 4-byte wide strings), copies the wrong bytes, and then overwrites addend with the aligned output section offset without preserving the PC-relative bias for elf_create_reloc().

Should clone_reloc() use arch_adjusted_addend(patched_reloc) to locate and measure the string in patched_sym->sec, and preserve the bias (reloc_addend(patched_reloc) - arch_adjusted_addend(patched_reloc)) when computing the new addend for elf_create_reloc()?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#3 lore raw

Re: [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings

In reply to Longjun Luo
Quoted 4 lines · Longjun Luo

SHF_STRINGS sections can contain elements wider than one byte. Using strlen() truncates their contents at an embedded zero byte, hiding changes and copying incomplete strings into the patch.

Quoted 8 lines

Find the terminating zero element using sh_entsize. Use the resulting length for both reference checksums and copying, and propagate errors through checksum calculation. Keep single-byte string handling unchanged.

Add a test for two- and four-byte strings, including references from data.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

Please give example(s) of such "wide strings" in the changelog.

Quoted 25 lines
Fixes 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes dd590d4d57eb ("objtool/klp: Introduce klp diff subcommand for diffing object files")
Fixes 873a2208ea31 ("objtool/klp: Calculate object checksums")
Assisted-by LLM
---
 tools/objtool/elf.c                           | 29 +++++++++
 tools/objtool/include/objtool/elf.h           |  1 +
 tools/objtool/klp-checksum.c                  | 44 ++++++++-----
 tools/objtool/klp-diff.c                      | 14 ++--
 .../tests/generic/fixtures/identity/common.h  | 15 +++++
 .../generic/fixtures/identity/wide_string.c   | 36 ++++++++++
 .../generic/test-checksum-wide-string.sh      | 39 +++++++++++
 tools/objtool/tests/identity-lib.sh           | 65 +++++++++++++++++++
 8 files changed, 223 insertions(+), 20 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/common.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/wide_string.c
 create mode 100755 tools/objtool/tests/generic/test-checksum-wide-string.sh
 create mode 100644 tools/objtool/tests/identity-lib.sh
tools/objtool/elf.c +0 -0
index a791f4ea6ec1..71f1a272e468 100644

tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1386

Quoted 5 lines
tools/objtool/elf.c +2 -0
@@ -1382,6 +1382,35 @@ struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name)
 	return elf;
 }
 
+/* Wide strings, such as the u16 PSR setup table, end at an all-zero element. */
+ssize_t elf_string_len(struct section *sec, unsigned long offset)

What is the "u16 PSR setup table"?

tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1396

Quoted 10 lines
tools/objtool/elf.c +10 -0
+{
+	size_t entsize = sec->sh.sh_entsize, size, len;
+	const char *str;
+
+	/*
+	 * FIXME: GCC's range_string() uses a one-past-end string pointer.
+	 * strlen() reads past the section; preserve the string base and addend.
+	 */
+	if (entsize == 1)
+		return strlen(sec->data->d_buf + offset);

I don't understand what this comment means either. Please make sure all comments and changelogs are written by humans, as LLMs tend to write nonsensically.

Signature · 1 line
Josh
#4 lore raw

Re: [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings

Sorry, I left out the debugging context here. Both comments refer to specific kernel build artifacts:

Quoted 6 lines · Josh Poimboeuf

Please give example(s) of such "wide strings" in the changelog.

tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1386

Quoted 2 lines
tools/objtool/elf.c +2 -0
+/* Wide strings, such as the u16 PSR setup table, end at an all-zero element. */
+ssize_t elf_string_len(struct section *sec, unsigned long offset)

What is the "u16 PSR setup table"?

PSR stands for Panel Self Refresh.

Source : drivers/gpu/drm/display/drm_dp_helper.c: drm_dp_psr_setup_time()

            /* macros expanded */
            static const u16 psr_setup_time_us[] = { 330, 275, 220,
165, 110, 55, 0 };
Section   : .rodata.str2.2 (SHF_STRINGS, sh_entsize = 2, Clang ThinLTO)
Bytes     : 4a 01 13 01 dc 00 a5 00 6e 00 37 00 00 00
                        ^^^^^
                        220 (0x00dc) contains a zero byte at offset +5

objtool : strlen() returns 5. The checksum hashes only 5 bytes, and
the copy includes only 6 of the table's 14 bytes.

Quoted 10 lines
Quoted 6 lines

+ /*
+ * FIXME: GCC's range_string() uses a one-past-end string pointer.
+ * strlen() reads past the section; preserve the string base and addend.
+ */
+ if (entsize == 1)
+ return strlen(sec->data->d_buf + offset);

I don't understand what this comment means either. Please make sure all comments and changelogs are written by humans, as LLMs tend to write nonsensically.

Source : lib/vsprintf.c: range_string()

            p = string_nocheck(p, pend, "[range ", default_str_spec);

Section : .rodata.range_string.isra.0.str1.1 (8 bytes: "[range \0")
Reloc : range_string.isra.0+0x9a: R_X86_64_32S .LC22+8

GCC note : Uses .LC22+8 (one-past-the-end pointer) as a loop bound without
dereferencing it.
objtool : Passes offset = 8 to strlen(sec->data->d_buf + 8), reading past
the 8-byte section. Fixing this requires tracking the base string (.LC22) and the +8 addend separately (left as a FIXME).

I came across both while testing on full kernel builds rather than in production livepatching, so I'll leave it to you whether they are worth addressing in practice.

Longjun

maintainer
#5 lore raw

Re: [RFC PATCH v2 01/11] objtool/klp: Fix checksums and copying of wide strings

Quoted 47 lines · Longjun Luo

Sorry, I left out the debugging context here. Both comments refer to specific kernel build artifacts:

Quoted 6 lines · Josh Poimboeuf

Please give example(s) of such "wide strings" in the changelog.

tools/objtool/elf.c struct elf *elf_create_file(GElf_Ehdr *ehdr, const char *name) · line 1386

Quoted 2 lines
tools/objtool/elf.c +2 -0
+/* Wide strings, such as the u16 PSR setup table, end at an all-zero element. */
+ssize_t elf_string_len(struct section *sec, unsigned long offset)

What is the "u16 PSR setup table"?

PSR stands for Panel Self Refresh.

Source : drivers/gpu/drm/display/drm_dp_helper.c: drm_dp_psr_setup_time()

            /* macros expanded */
            static const u16 psr_setup_time_us[] = { 330, 275, 220,
165, 110, 55, 0 };
Section   : .rodata.str2.2 (SHF_STRINGS, sh_entsize = 2, Clang ThinLTO)
Bytes     : 4a 01 13 01 dc 00 a5 00 6e 00 37 00 00 00
                        ^^^^^
                        220 (0x00dc) contains a zero byte at offset +5

objtool : strlen() returns 5. The checksum hashes only 5 bytes, and
the copy includes only 6 of the table's 14 bytes.

Quoted 10 lines
Quoted 6 lines

+ /*
+ * FIXME: GCC's range_string() uses a one-past-end string pointer.
+ * strlen() reads past the section; preserve the string base and addend.
+ */
+ if (entsize == 1)
+ return strlen(sec->data->d_buf + offset);

I don't understand what this comment means either. Please make sure all comments and changelogs are written by humans, as LLMs tend to write nonsensically.

Source : lib/vsprintf.c: range_string()

            p = string_nocheck(p, pend, "[range ", default_str_spec);

Section : .rodata.range_string.isra.0.str1.1 (8 bytes: "[range \0")
Reloc : range_string.isra.0+0x9a: R_X86_64_32S .LC22+8

GCC note : Uses .LC22+8 (one-past-the-end pointer) as a loop bound without
dereferencing it.
objtool : Passes offset = 8 to strlen(sec->data->d_buf + 8), reading past
the 8-byte section. Fixing this requires tracking the base string (.LC22) and the +8 addend separately (left as a FIXME).

Quoted 3 lines

I came across both while testing on full kernel builds rather than in production livepatching, so I'll leave it to you whether they are worth addressing in practice.

It's probably worth fixing, just the comments and commit log need to be clear about what exactly we're fixing.

Signature · 1 line
Josh
#6 lore raw

RFC PATCH v2 2/11 objtool/klp: Hash compound literals by content

In reply to Longjun Luo

Compiler-generated names for compound literals do not describe their contents. Changes to an anonymous array or a callback in an anonymous structure can leave the referring function's checksum unchanged.

Hash compound contents and relocations recursively, using the resulting checksum in references. Preserve target offsets and stop at named objects. Reject cycles and nesting beyond three compound objects.

Recognize GCC and Clang compound literal names in both writable and read-only sections. Add tests for changed array elements and callbacks.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

Assisted-by LLM
---
 tools/objtool/include/objtool/klp.h           |   2 +
 tools/objtool/klp-checksum.c                  | 102 +++++++++++++-----
 tools/objtool/klp-diff.c                      |  31 ++++++
 .../generic/fixtures/identity/compound.c      |  65 +++++++++++
 .../tests/generic/test-identity-compound.sh   |  13 +++
 5 files changed, 187 insertions(+), 26 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/compound.c
 create mode 100755 tools/objtool/tests/generic/test-identity-compound.sh
tools/objtool/include/objtool/klp.h +2 -0
index c57775d78c71..90468e141124 100644
@@ -51,6 +51,8 @@ struct objtool_file;
 struct elf;
 struct symbol;
 
+bool is_compound_constant(struct symbol *sym);
+
 int klp_create_symid_sections(struct objtool_file *file);
 
 int klp_sympos_init(struct elf *orig);
tools/objtool/klp-checksum.c +76 -26

182 lines not on this page · raw message

tools/objtool/klp-diff.c +31 -0
index 6321f691adbb..0391f91a8d31 100644
@@ -438,6 +438,37 @@ static bool is_anonymous_rodata(struct symbol *sym)
 	       (!is_object_sym(sym) || strstarts(sym->sec->name, ".rodata.cst"));
 }
 
+/* GCC and Clang give compound literals generated names, not source identities. */
+bool is_compound_constant(struct symbol *sym)
+{
+	const char *name = sym->name;
+
+	if (!is_object_sym(sym) || is_undef_sym(sym) || !sym->len ||
+	    !sym->sec)
+		return false;
+
+	if (strstarts(name, ".compoundliteral"))
+		name += strlen(".compoundliteral");
+	else if (strstarts(name, "__compound_literal"))
+		name += strlen("__compound_literal");
+	else
+		return false;
+
+	while (*name == '.' && isdigit((unsigned char)name[1])) {
+		name++;
+		while (isdigit((unsigned char)*name))
+			name++;
+	}
+	if (strstarts(name, ".llvm.")) {
+		name += strlen(".llvm.");
+		if (!isdigit((unsigned char)*name))
+			return false;
+		while (isdigit((unsigned char)*name))
+			name++;
+	}
+	return !*name;
+}
+
 /*
  * These symbols should never be correlated, so their local patched versions
  * are used instead of linking to the originals.
tools/objtool/tests/generic/fixtures/identity/compound.c new file +65 -0

65 lines not on this page · raw message

tools/objtool/tests/generic/test-identity-compound.sh new file +13 -0
new file mode 100755
index 000000000000..76b16616b32c
@@ -0,0 +1,13 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Array contents and callbacks contribute to the referring function's checksum.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+for change in CHANGE_LEAF CHANGE_TARGET; do
+	build_pair identity/compound.c "-D$change"
+	assert_checksum_differs target
+done
+
+pass "compound contents contribute to reference checksums"
Signature · 1 line
2.55.0
#7 lore raw

Re: [RFC PATCH v2 02/11] objtool/klp: Hash compound literals by content

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] objtool/klp: non-inclusive symbol lookup in checksum_update_object() - [Medium] objtool/klp…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] objtool/klp: non-inclusive symbol lookup in checksum_update_object()
- [Medium] objtool/klp: out-of-bounds .rodata.cst read in checksum_update_object() --

commit 6efbe1fe552ea13b0bd2f7a21edd04896b97dc17
Author: Longjun Luo <[email protected]>

objtool/klp: Hash compound literals by content

This commit updates klp checksum generation to recursively hash the contents and relocations of compiler-generated compound literal symbols instead of hashing their generated symbol names.

Quoted 4 lines
tools/objtool/klp-checksum.c +0 -0
index efaf083c03a11..2a515c7e2bf60 100644

[ ... ]

Quoted 76 lines
tools/objtool/klp-checksum.c +3 -4
@@ -134,10 +146,9 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 	sym = reloc->sym;
 	offset = arch_insn_adjusted_addend(insn, reloc);
 
-	if (is_string_sec(sym->sec)) {
-		ssize_t size = elf_string_len(sym->sec, sym->offset + offset);
-		const void *data;
-
+	if (is_const_reference(sym)) {
+		size = is_string_sec(sym->sec) ? elf_string_len(sym->sec, sym->offset + offset) :
+					      sym->sec->sh.sh_entsize;
 		if (size < 0)
 			return -1;
 		data = sym->sec->data->d_buf + sym->offset + offset;
tools/objtool/klp-checksum.c +17 -14
 		__checksum_update_insn(func, insn, data, size);
 		goto alts;
 	}
 
-	if (is_anonymous_const_data(sym)) {
-		void *cst;
-
-		cst = sym->sec->data->d_buf + sym->offset + offset;
-		__checksum_update_insn(func, insn, cst, sym->sec->sh.sh_entsize);
-		goto alts;
-	}
-
 	if (is_sec_sym(sym)) {
 		sym = find_symbol_containing(reloc->sym->sec, offset);
 		if (!sym)
 			goto alts;
 
 		offset -= sym->offset;
 	}
 
-	__checksum_update_insn(func, insn, sym->demangled_name,
-			       strlen(sym->demangled_name));
+	if (is_compound_constant(sym)) {
+		if (checksum_object(file, sym, 1))
+			return -1;
+		__checksum_update_insn(func, insn, &sym->csum.checksum,
+				       sizeof(sym->csum.checksum));
+	} else {
+		__checksum_update_insn(func, insn, sym->demangled_name,
+				       strlen(sym->demangled_name));
+	}
 	__checksum_update_insn(func, insn, &offset, sizeof(offset));
 
 alts:
@@ -198,7 +208,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 	return 0;
 }
 
-static int checksum_update_object(struct objtool_file *file, struct symbol *sym)
+static int checksum_update_object(struct objtool_file *file, struct symbol *sym,
+				  unsigned int depth)
 {
 	struct reloc *reloc;
 
@@ -212,38 +223,79 @@ static int checksum_update_object(struct objtool_file *file, struct symbol *sym)
 	sym_for_each_reloc(file->elf, sym, reloc) {
 		unsigned long sym_offset = reloc_offset(reloc) - sym->offset;
 		struct symbol *target = reloc->sym;
+		unsigned int type = reloc_type(reloc);
+		const void *data;
+		ssize_t size;
 		s64 offset;
 
 		offset = reloc_addend(reloc);
 
-		if (is_string_sec(target->sec)) {
-			ssize_t size = elf_string_len(target->sec, target->offset + offset);
-			const void *data;
+		__checksum_update_object(sym, sym_offset, "reloc type",
+					 &type, sizeof(type));
 
+		if (is_const_reference(target)) {

tools/objtool/klp-checksum.c static int checksum_update_object(struct objtool_file *file, struct symbol *sym) · line 245

Quoted 10 lines
tools/objtool/klp-checksum.c +4 -1
+			size = is_string_sec(target->sec) ?
+				elf_string_len(target->sec, target->offset + offset) :
+				target->sec->sh.sh_entsize;
 			if (size < 0)
 				return -1;
 			data = target->sec->data->d_buf + target->offset + offset;
-			__checksum_update_object(sym, sym_offset, "reloc string", data, size);
+			__checksum_update_object(sym, sym_offset, "reloc constant", data, size);
 			continue;
 		}

[Severity: Medium]
Can this read past the end of target->sec->data->d_buf or corrupt the checksum when a relocation into a .rodata.cst section has a non-zero intra-entry offset?

When Clang compiles a small constant compound literal array or struct (such as the 32-byte const unsigned int[8] array in compound.c), it places the object into a mergeable .rodata.cst section with sh_entsize set to the entry size and emits relocations against the section symbol.

If a compound literal, static initializer, or instruction references a non-zero offset within that constant (for example, &arr[1] at offset +4, a struct member at a non-zero offset, or a one-past-the-end pointer at offset +sh_entsize), both checksum_update_object() and checksum_update_insn() match is_const_reference(), set size to sh_entsize, and read a full sh_entsize bytes starting from d_buf + target->offset + offset without checking section bounds or aligning to the entry start.

If the referenced constant is the last (or only) entry in the .rodata.cst section, reading sh_entsize bytes from a non-zero intra-entry offset reads past the end of d_buf. If another entry follows in the section, the hash mixes in trailing bytes from the adjacent constant entry while skipping the leading bytes of the referenced entry, and because this branch returns early before hashing offset, the intra-entry offset itself is omitted from the checksum.

Quoted 5 lines
tools/objtool/klp-checksum.c +1 -1
 		if (is_sec_sym(target)) {
 			target = find_symbol_containing(reloc->sym->sec, offset);
-			if (!target)
+			if (!target) {

tools/objtool/klp-checksum.c static int checksum_update_object(struct objtool_file *file, struct symbol *sym) · line 259

Quoted 10 lines
tools/objtool/klp-checksum.c +6 -0
+				if (is_compound_constant(sym)) {
+					ERROR("%s+%#lx: unresolved compound constant reference",
+					      sym->name, sym_offset);\
+					return -1;
+				}
 				continue;
+			}
 
 			offset -= target->offset;
 		}

[Severity: Medium]
What happens here when a relocation against a section symbol uses a one-past-the-end pointer (where offset equals target->offset + target->len), targets a zero-length local symbol, or references anonymous .rodata?

Unlike convert_reloc_secsym_to_sym() in tools/objtool/klp-diff.c, which checks find_symbol_by_offset(sec, 0) for dedicated sections, falls back to find_symbol_containing_inclusive(), and allows anonymous .rodata references, checksum_update_object() and checksum_update_insn() only call find_symbol_containing(reloc->sym->sec, offset), which strictly requires offset to be in [sym->offset, sym->offset + sym->len - 1] and skips zero-length symbols.

Doesn't that cause two regressions when calculate_checksums() runs:

1. If a compound literal contains a one-past-the-end pointer (even to an object in a dedicated -fdata-sections section), a reference to a zero-length local symbol, or an anonymous .rodata reference, find_symbol_containing() returns NULL and checksum_update_object() aborts the build with an unresolved compound constant reference error.

2. If an instruction in checksum_update_insn() or a named object in checksum_update_object() references the one-past-the-end address of a compound literal (for example, as a loop bound over a compound literal array), find_symbol_containing() returns NULL and the compound literal is silently omitted from the checksum?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#8 lore raw

RFC PATCH v2 3/11 objtool/klp: Preserve identifier digits when stripping suffixes

In reply to Longjun Luo

The name tx.crc32 loses its trailing digits, while tx.crc32.llvm.<hash> keeps them because of an early return. ThinLTO promotion can therefore break correlation and copy existing static state into the patch.

Strip LLVM hashes and complete .<number> suffixes on the same path. Keep the counter-stripping rule for __UNIQUE_ID names, including Clang's function-prefixed names.

Add a ThinLTO test which checks that the existing crc32 state is reused. The available objtool tests pass with GCC 16.2.1 and Clang 22.1.8.

Fixes 0d83da43b1e1 ("objtool/klp: Add --checksum option to generate per-function checksums")
Fixes 020b71dcafee ("objtool/klp: Remove .llvm suffix in demangle_name()")
Assisted-by LLM
---
 tools/objtool/elf.c                           | 33 +++++++++++--------
 .../fixtures/identity/promotion_crc32.c       |  8 +++++
 .../fixtures/identity/promotion_crc32_user.c  | 15 +++++++++
 .../generic/test-identity-thinlto-crc32.sh    | 23 +++++++++++++
 4 files changed, 66 insertions(+), 13 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-crc32.sh
tools/objtool/elf.c +20 -13
index 71f1a272e468..3ddbf5143260 100644
@@ -448,32 +448,39 @@ static int read_sections(struct elf *elf)
  */
 static ssize_t demangled_name_len(const char *name)
 {
-	ssize_t idx;
+	ssize_t idx, len;
 	const char *p;
 
 	p = strstr(name, ".llvm.");
-	if (p)
-		return p - name;
+	len = p ? p - name : strlen(name);
 
-	if (!strstarts(name, "__UNIQUE_ID_") && !strchr(name, '.'))
-		return strlen(name);
+	if (strstarts(name, "__UNIQUE_ID_") || strstr(name, ".__UNIQUE_ID_")) {
+		for (idx = len - 1; idx >= 0; idx--) {
+			char c = name[idx];
 
-	for (idx = strlen(name) - 1; idx >= 0; idx--) {
-		char c = name[idx];
+			if (!isdigit(c) && c != '.' && c != '_')
+				break;
+		}
+		return idx + 1;
+	}
+
+	while (len && isdigit(name[len - 1])) {
+		p = name + len - 1;
+		while (p > name && isdigit(*p))
+			p--;
 
-		if (!isdigit(c) && c != '.' && c != '_')
+		if (p == name || *p != '.')
 			break;
+		len = p - name;
 	}
-	if (idx <= 0)
-		return strlen(name);
-	return idx + 1;
+	return len;
 }
 
 /*
  * Remove number suffix of a symbol.
  *
- * Specifically, remove trailing numbers for "__UNIQUE_ID_" symbols and
- * symbols with '.'.
+ * Remove complete .<number> suffixes and trailing numbers from
+ * "__UNIQUE_ID_" symbols.
  *
  * With CONFIG_LTO_CLANG_THIN, it is possible to have nested __UNIQUE_ID_,
  * such as
tools/objtool/tests/generic/fixtures/identity/promotion_crc32.c new file +8 -0
new file mode 100644
index 000000000000..d95914eacdc0
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+unsigned int tx(unsigned int value)
+{
+	static volatile unsigned int crc32;
+
+	return crc32 += value;
+}
tools/objtool/tests/generic/fixtures/identity/promotion_crc32_user.c new file +15 -0
new file mode 100644
index 000000000000..b44fa1d4a75e
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+unsigned int tx(unsigned int value);
+
+unsigned int target(unsigned int value)
+{
+#ifdef PATCHED
+	return tx(value);
+#else
+	return value;
+#endif
+}
tools/objtool/tests/generic/test-identity-thinlto-crc32.sh new file +23 -0
new file mode 100755
index 000000000000..66988d0b7692
@@ -0,0 +1,23 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# ThinLTO promotion must retain the digits in a static variable's name.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_identity_pair thin "" identity/promotion_crc32.c identity/promotion_crc32_user.c
+in_symbols orig.o | awk '$4 == "OBJECT" && $5 == "LOCAL" && $8 == "tx.crc32"' |
+	grep -q . || probe_skip "CRC state was not local before importing"
+in_symbols patched.o | awk '$4 == "OBJECT" && $5 == "GLOBAL" && $8 ~ /^tx\.crc32\.llvm\.[0-9]+$/' |
+	grep -q . || probe_skip "ThinLTO did not promote the CRC state"
+
+assert_checksum_matches tx
+run_diff
+assert_patched target
+assert_not_patched tx
+out_symbols | awk '$4 == "OBJECT" && $7 != "UND" && $8 ~ /^tx\.crc32(\.|$)/' |
+	grep -q . && fail "promoted CRC state was allocated again"
+run_post_link
+assert_original_reference target tx.crc32
+
+pass "digits in static names survive ThinLTO promotion"
Signature · 1 line
2.55.0
#9 lore raw

RFC PATCH v2 4/11 objtool/klp: Read symbol identities from DWARF

In reply to Longjun Luo

Compiler-generated names and ELF FILE symbols can change between builds. Read each symbol's defining translation unit and named scope from DWARF and combine them with its suffix-stripped name.

- Prefer DIEs matching the symbol's address and use names to narrow them.
- Fall back to names and FILE, using CU ranges to constrain functions.
- Handle address pieces, shrunk storage and assembly CU ranges.
- Cross-check DWARF sources against FILE and reject conflicting identities.
- Keep the resulting identities and release the temporary DIE indexes.

Only omit source for external definitions with GLOBAL or WEAK ELF binding. Local clones retain their CU even when their DIE has DW_AT_external. Record the existing cold-parent ambiguity after weak overrides in a FIXME.

Add the libdw build dependency. This introduces the reader without changing symbol correlation or checksum calculation.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

Assisted-by LLM
---
 scripts/livepatch/klp-build                   |    2 +-
 tools/objtool/Build                           |    4 +-
 .../objtool/Documentation/klp-test-design.txt |    6 +-
 tools/objtool/Makefile                        |   12 +-
 tools/objtool/elf.c                           |   38 +-
 tools/objtool/include/objtool/elf.h           |   42 +-
 tools/objtool/include/objtool/klp-dwarf.h     |   22 +
 tools/objtool/include/objtool/klp-identity.h  |   26 +
 tools/objtool/klp-checksum.c                  |   16 +-
 tools/objtool/klp-diff.c                      |   10 +-
 tools/objtool/klp-dwarf.c                     | 1181 +++++++++++++++++
 tools/objtool/klp-identity.c                  |   94 ++
 tools/objtool/tests/lib.sh                    |    2 +-
 13 files changed, 1406 insertions(+), 49 deletions(-)
 create mode 100644 tools/objtool/include/objtool/klp-dwarf.h
 create mode 100644 tools/objtool/include/objtool/klp-identity.h
 create mode 100644 tools/objtool/klp-dwarf.c
 create mode 100644 tools/objtool/klp-identity.c
scripts/livepatch/klp-build +1 -1
index b52a8489d9f6..e373928123e8 100755
@@ -279,7 +279,7 @@ validate_config() {
 		die "Clang assembler version < 20 not supported"
 
 	[[ -x "$OBJTOOL" ]] && "$OBJTOOL" klp 2>&1 | command grep -q "not implemented" && \
-		die "objtool not built with KLP support; install xxhash-devel/libxxhash-dev (version >= 0.8) and recompile"
+		die "objtool not built with KLP support; install xxhash-devel/libxxhash-dev (version >= 0.8) and elfutils-devel/libdw-dev and recompile"
 
 	return 0
 }
tools/objtool/Build +2 -2
index 59f948628098..84139709a2dd 100644
@@ -13,8 +13,8 @@ objtool-$(BUILD_DISAS) += disas.o
 objtool-$(BUILD_DISAS) += trace.o
 
 objtool-$(BUILD_ORC) += orc_gen.o orc_dump.o
-objtool-$(BUILD_KLP) += builtin-klp.o klp-checksum.o klp-diff.o \
-			klp-post-link.o klp-sympos.o
+objtool-$(BUILD_KLP) += builtin-klp.o klp-checksum.o klp-diff.o klp-dwarf.o \
+			klp-post-link.o klp-sympos.o klp-identity.o
 
 objtool-y += libstring.o
 objtool-y += libctype.o
tools/objtool/Documentation/klp-test-design.txt +3 -3
index 2082c277197f..e63aad0f4324 100644
@@ -17,8 +17,8 @@ TL;DR
 =====
 
 One run covers one compiler and one architecture; CI runs the combinations.
-Build objtool first -- it needs libelf and libxxhash -- and the same ARCH is
-used for both steps.
+Build objtool first -- it needs libelf, libxxhash and libdw -- and use the
+same ARCH for both steps.
 
 Natively, with gcc::
 
@@ -211,7 +211,7 @@ Running
 
 ::
 
-    make -C tools/objtool                 # needs libelf and libxxhash
+    make -C tools/objtool                 # needs libelf, libxxhash and libdw
     make -C tools/objtool tests
 
     CC=clang make -C tools/objtool tests  # the other toolchain
tools/objtool/Makefile +11 -1
index db99c4759b2c..e2fbd778bb74 100644
@@ -14,7 +14,12 @@ endif
 ifeq ($(ARCH_HAS_KLP),y)
 	HAVE_XXHASH = $(shell printf "$(pound)include <xxhash.h>\nXXH3_state_t *state;int main() {}" | \
 		      $(HOSTCC) $(HOSTCFLAGS) -xc - -o /dev/null -lxxhash 2> /dev/null && echo y || echo n)
-	ifeq ($(HAVE_XXHASH),y)
+	LIBDW_CFLAGS := $(shell $(HOSTPKG_CONFIG) libdw --cflags 2>/dev/null)
+	LIBDW_LIBS := $(shell $(HOSTPKG_CONFIG) libdw --libs 2>/dev/null || echo -ldw)
+	HAVE_LIBDW = $(shell printf "$(pound)include <elfutils/libdwfl.h>\nint main(void) { return !dwfl_begin(0); }" | \
+		     $(HOSTCC) $(HOSTCFLAGS) $(LIBDW_CFLAGS) -xc - -o /dev/null \
+		     $(HOSTLDFLAGS) $(LIBDW_LIBS) 2>/dev/null && echo y || echo n)
+	ifeq ($(HAVE_XXHASH)$(HAVE_LIBDW),yy)
 		BUILD_KLP	 := y
 		LIBXXHASH_CFLAGS := $(shell $(HOSTPKG_CONFIG) libxxhash --cflags 2>/dev/null) \
 				    -DBUILD_KLP
@@ -65,6 +70,11 @@ OBJTOOL_CFLAGS  := -std=gnu11 -fomit-frame-pointer -O2 -g $(WARNINGS)	\
 
 OBJTOOL_LDFLAGS := $(LIBSUBCMD) $(LIBELF_LIBS) $(LIBXXHASH_LIBS) $(HOSTLDFLAGS)
 
+ifeq ($(BUILD_KLP),y)
+OBJTOOL_CFLAGS += $(LIBDW_CFLAGS)
+OBJTOOL_LDFLAGS += $(LIBDW_LIBS)
+endif
+
 # Allow old libelf to be used:
 elfshdr := $(shell echo '$(pound)include <libelf.h>' | $(HOSTCC) $(OBJTOOL_CFLAGS) -x c -E - 2>/dev/null | grep elf_getshdr)
 OBJTOOL_CFLAGS += $(if $(elfshdr),,-DLIBELF_USE_DEPRECATED)
tools/objtool/elf.c +12 -26
index 3ddbf5143260..ee7422364810 100644
@@ -9,7 +9,6 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <sys/mman.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -20,7 +19,6 @@
 #include <linux/align.h>
 #include <linux/kernel.h>
 #include <linux/interval_tree_generic.h>
-#include <linux/log2.h>
 #include <objtool/builtin.h>
 #include <objtool/elf.h>
 #include <objtool/klp.h>
@@ -33,13 +31,6 @@ u32 str_hash_demangled(const char *str)
 	return jhash(str, demangled_name_len(str), 0);
 }
 
-#define elf_hash_add(name, node, key)					\
-({									\
-	struct elf_hash_node *__node = node;				\
-	__node->next = __elf_table_entry(elf, name, key);		\
-	__elf_table_entry(elf, name, key) = __node;			\
-})
-
 static inline void __elf_hash_del(struct elf_hash_node *node,
 				  struct elf_hash_node **head)
 {
@@ -61,20 +52,6 @@ static inline void __elf_hash_del(struct elf_hash_node *node,
 #define elf_hash_del(name, node, key) \
 	__elf_hash_del(node, &__elf_table_entry(elf, name, key))
 
-#define elf_alloc_hash(name, size)					\
-({									\
-	__elf_bits(elf, name) = max(10, ilog2(size));			\
-	__elf_table(elf, name) = mmap(NULL,				\
-				 sizeof(struct elf_hash_node *) << __elf_bits(elf, name), \
-				 PROT_READ|PROT_WRITE,			\
-				 MAP_PRIVATE|MAP_ANON, -1, 0);		\
-	if (__elf_table(elf, name) == (void *)-1L) {			\
-		ERROR_GLIBC("mmap fail " #name);			\
-		__elf_table(elf, name) = NULL;				\
-	}								\
-	__elf_table(elf, name);						\
-})
-
 static inline unsigned long __sym_start(struct symbol *s)
 {
 	return s->offset;
@@ -545,13 +522,13 @@ static int elf_add_symbol(struct elf *elf, struct symbol *sym)
 		entry = &sym->sec->symbol_list;
 	list_add(&sym->list, entry);
 
-	sym->demangled_name = demangle_name(sym);
-	if (!sym->demangled_name)
+	sym->identity.demangled_name = demangle_name(sym);
+	if (!sym->identity.demangled_name)
 		return -1;
 
 	list_add_tail(&sym->global_list, &elf->symbols);
 	elf_hash_add(symbol, &sym->hash, sym->idx);
-	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->demangled_name));
+	elf_hash_add(symbol_name, &sym->name_hash, str_hash(sym->identity.demangled_name));
 
 	if (is_func_sym(sym) &&
 	    (strstarts(sym->name, "__pfx_") ||
@@ -667,6 +644,10 @@ static int read_symbols(struct elf *elf)
 		if (elf_add_symbol(elf, sym))
 			return -1;
 
+		/*
+		 * FIXME: Assembly inputs without STT_FILE can inherit the preceding
+		 * input's FILE after partial linking.
+		 */
 		if (is_file_sym(sym))
 			file = sym;
 		else if (sym->bind == STB_LOCAL && !is_sec_sym(sym))
@@ -710,6 +691,11 @@ static int read_symbols(struct elf *elf)
 				return -1;
 			}
 
+			/*
+			 * FIXME: Weak and strong definitions can leave two local foo.cold symbols.
+			 * Both resolve to the surviving foo, so the last one overwrites cfunc and
+			 * can make the checksum miss changes to the active cold fragment.
+			 */
 			sym->pfunc = pfunc->alias;
 			pfunc->cfunc = sym;
 			pfunc->alias->cfunc = sym;
tools/objtool/include/objtool/elf.h +40 -2

99 lines not on this page · raw message

tools/objtool/include/objtool/klp-dwarf.h new file +22 -0
new file mode 100644
index 000000000000..ba6029bd2235
@@ -0,0 +1,22 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2026 Longjun Luo <[email protected]>
+ */
+
+#ifndef _OBJTOOL_KLP_DWARF_H
+#define _OBJTOOL_KLP_DWARF_H
+
+struct elf;
+struct symbol;
+
+int init_identity_cache(struct elf *elf);
+void free_identity_cache(struct elf *elf);
+
+/*
+ * Set source, scope and external in sym->identity from DWARF.
+ * Return 0 if found, 1 if unavailable, or -1 for invalid/conflicting DWARF.
+ * Returned strings belong to elf and must not be freed by the caller.
+ */
+int get_identity_for_symbol(struct elf *elf, struct symbol *sym);
+
+#endif
tools/objtool/include/objtool/klp-identity.h new file +26 -0
new file mode 100644
index 000000000000..ffbc2bb906b1
@@ -0,0 +1,26 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2026 Longjun Luo <[email protected]>
+ */
+
+#ifndef _OBJTOOL_KLP_IDENTITY_H
+#define _OBJTOOL_KLP_IDENTITY_H
+
+#include <linux/types.h>
+
+struct elf;
+
+struct identity {
+	const char *source;		/* physical defining TU path, or "" */
+	const char *scope;		/* enclosing scopes, NULL without DWARF identity */
+	const char *demangled_name;	/* suffix-stripped symbol name */
+	const char *path;		/* source/scope/demangled_name, or NULL */
+	bool external;
+};
+
+/* The caller must set symbol->dont_correlate before preparing identities. */
+int prepare_identities(struct elf *elf);
+u32 identity_hash(const struct identity *identity);
+bool same_identity(const struct identity *a, const struct identity *b);
+
+#endif
tools/objtool/klp-checksum.c +8 -8
index 2a515c7e2bf6..1a5f2fa89baa 100644
@@ -110,8 +110,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 			__checksum_update_insn(func, insn, buf, len);
 
 			if (call_dest) {
-				__checksum_update_insn(func, insn, call_dest->demangled_name,
-						       strlen(call_dest->demangled_name));
+				__checksum_update_insn(func, insn, call_dest->identity.demangled_name,
+						       strlen(call_dest->identity.demangled_name));
 
 			} else if (jump_dest) {
 				struct symbol *dest_sym;
@@ -127,8 +127,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 				if (!dest_sym)
 					goto alts;
 
-				__checksum_update_insn(func, insn, dest_sym->demangled_name,
-						       strlen(dest_sym->demangled_name));
+				__checksum_update_insn(func, insn, dest_sym->identity.demangled_name,
+						       strlen(dest_sym->identity.demangled_name));
 
 				offset = jump_dest->offset - dest_sym->offset;
 				__checksum_update_insn(func, insn, &offset, sizeof(offset));
@@ -170,8 +170,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 		__checksum_update_insn(func, insn, &sym->csum.checksum,
 				       sizeof(sym->csum.checksum));
 	} else {
-		__checksum_update_insn(func, insn, sym->demangled_name,
-				       strlen(sym->demangled_name));
+		__checksum_update_insn(func, insn, sym->identity.demangled_name,
+				       strlen(sym->identity.demangled_name));
 	}
 	__checksum_update_insn(func, insn, &offset, sizeof(offset));
 
@@ -266,8 +266,8 @@ static int checksum_update_object(struct objtool_file *file, struct symbol *sym,
 						 sizeof(target->csum.checksum));
 		} else {
 			__checksum_update_object(sym, sym_offset, "reloc name",
-						 target->demangled_name,
-						 strlen(target->demangled_name));
+						 target->identity.demangled_name,
+						 strlen(target->identity.demangled_name));
 		}
 		__checksum_update_object(sym, sym_offset, "reloc addend",
 					 &offset, sizeof(offset));
tools/objtool/klp-diff.c +5 -5
index 0391f91a8d31..cd5e683d6468 100644
@@ -600,7 +600,7 @@ static struct symbol *find_twin(struct elfs *e, struct symbol *sym1)
 	struct symbol *sym2, *match = NULL;
 
 	/* Count orig candidates */
-	for_each_sym_by_demangled_name(e->orig, sym1->demangled_name, sym2) {
+	for_each_sym_by_demangled_name(e->orig, sym1->identity.demangled_name, sym2) {
 		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
 		    (!maybe_same_file(sym1, sym2)))
 			continue;
@@ -626,7 +626,7 @@ static struct symbol *find_twin(struct elfs *e, struct symbol *sym1)
 	}
 
 	/* Count patched candidates */
-	for_each_sym_by_demangled_name(e->patched, sym1->demangled_name, sym2) {
+	for_each_sym_by_demangled_name(e->patched, sym1->identity.demangled_name, sym2) {
 		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
 		    !maybe_same_file(sym1, sym2))
 			continue;
@@ -766,7 +766,7 @@ static struct symbol *find_twin_suffixed(struct elf *elf, struct symbol *sym1)
 		return NULL;
 
 	if (snprintf_check(name, SYM_NAME_LEN, "%s%s",
-			   sym1->demangled_name, patched_suffix))
+			   sym1->identity.demangled_name, patched_suffix))
 		return NULL;
 
 	for_each_sym_by_name(elf, name, sym2) {
@@ -809,7 +809,7 @@ static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1)
 	unsigned int sym1_pos = 0;
 	struct symbol *sym2, *match = NULL;
 
-	for_each_sym_by_demangled_name(e->orig, sym1->demangled_name, sym2) {
+	for_each_sym_by_demangled_name(e->orig, sym1->identity.demangled_name, sym2) {
 		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
 		    !maybe_same_file(sym1, sym2))
 			continue;
@@ -821,7 +821,7 @@ static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1)
 		idx_orig++;
 	}
 
-	for_each_sym_by_demangled_name(e->patched, sym1->demangled_name, sym2) {
+	for_each_sym_by_demangled_name(e->patched, sym1->identity.demangled_name, sym2) {
 		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
 		    !maybe_same_file(sym1, sym2))
 			continue;
tools/objtool/klp-dwarf.c new file +1181 -0

1,181 lines not on this page · raw message

tools/objtool/klp-identity.c new file +94 -0

94 lines not on this page · raw message

tools/objtool/tests/lib.sh +1 -1
index 4da168d0ddca..8e1aa7614dc6 100644
@@ -57,7 +57,7 @@ klp_preflight()
 	OBJTOOL="$(realpath "$OBJTOOL")"
 
 	"$OBJTOOL" klp 2>&1 | grep -q checksum ||
-		bail "objtool was built without klp support; install libxxhash (>= 0.8) and rebuild"
+		bail "objtool was built without klp support; install libxxhash (>= 0.8) and libdw development files and rebuild"
 
 	command -v "${CC%% *}" >/dev/null || bail "compiler not found: $CC"
 
Signature · 1 line
2.55.0
#10 lore raw

Re: [RFC PATCH v2 04/11] objtool/klp: Read symbol identities from DWARF

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] objtool/klp: broken external and source filtering in fallback_by_name() - [Medium] objtool/klp…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] objtool/klp: broken external and source filtering in fallback_by_name()
- [Medium] objtool/klp: mishandled .data.rel.ro and aliases in get_identity_for_symbol()
- [Medium] objtool/klp: check_relocations() fails on SHN_UNDEF debug relocations
- [Medium] objtool/klp: missing .lto_priv handling in matches_name() and demangle_name() --

commit bb3cdda02c19261801a05f1c255cd9e10ae1e2cb
Author: Longjun Luo <[email protected]>

objtool/klp: Read symbol identities from DWARF

This commit adds a libdw-based DWARF reader to extract defining translation units and enclosing scope chains for ELF symbols in objtool KLP builds. It indexes DIE addresses and names, resolves abstract origins and specifications, and validates DWARF symbol identities against STT_FILE symbols.

Quoted 6 lines
tools/objtool/klp-dwarf.c new file +0 -0
new file mode 100644
index 0000000000000..99b098ee7904f
@@ -0,0 +1,1181 @@

[ ... ]

Quoted 27 lines
tools/objtool/klp-dwarf.c new file +27 -0
+/*
+ * libdwfl permits partial relocation, leaving unapplied entries in place.
+ * Check only sections read for identities; unused indexes may retain relocations.
+ */
+static int check_relocations(Dwarf *dwarf)
+{
+	static const char * const sections[] = {
+		".debug_info", ".debug_types", ".debug_abbrev",
+		".debug_str", ".debug_line_str", ".debug_str_offsets",
+		".debug_addr", ".debug_ranges", ".debug_rnglists",
+	};
+	Elf *elf = dwarf_getelf(dwarf);
+	Elf_Scn *scn = NULL;
+	GElf_Shdr sh, target;
+	const char *name;
+	size_t strings, i;
+
+	if (!elf || elf_getshdrstrndx(elf, &strings))
+		return -1;
+	while ((scn = elf_nextscn(elf, scn))) {
+		if (!gelf_getshdr(scn, &sh))
+			return -1;
+		if (!sh.sh_size || (sh.sh_type != SHT_REL && sh.sh_type != SHT_RELA))
+			continue;
+		if (!gelf_getshdr(elf_getscn(elf, sh.sh_info), &target))
+			return -1;
+		name = elf_strptr(elf, strings, target.sh_name);

tools/objtool/klp-dwarf.c line 716

Quoted 10 lines
tools/objtool/klp-dwarf.c new file +10 -0
+		if (!name)
+			return -1;
+		for (i = 0; i < ARRAY_SIZE(sections); i++)
+			if (!strcmp(name, sections[i])) {
+				ERROR("DWARF identity: unapplied relocations for %s", name);
+				return -1;
+			}
+	}
+	return 0;
+}

[Severity: Medium]
As reported by Sashiko on RFC [1], won't check_relocations() fail on valid ET_REL kernel and module objects whose .debug_info or .debug_addr sections reference external (SHN_UNDEF) symbols?

When build_identities() reports a single offline ET_REL object via dwfl_report_offline(), libdwfl only sets sh_size to 0 on a relocation section when every relocation in that section is resolved.

In ET_REL objects compiled with -O2 -g, .rela.debug_info (DWARF 4) and .rela.debug_addr (DWARF 5) can contain relocations against SHN_UNDEF symbols (for example, optimized-out pointers initialized to the address of an external symbol via DW_OP_addr or DW_OP_addrx with DW_OP_stack_value, or DW_TAG_call_site targets). Because libdwfl leaves those SHN_UNDEF relocations in place with sh.sh_size non-zero, check_relocations() will reject the object with "unapplied relocations for .debug_info" or ".debug_addr" unless it checks whether the remaining relocations only target SHN_UNDEF symbols.

[ ... ]

Quoted 8 lines
tools/objtool/klp-dwarf.c new file +8 -0
+/* Match DW_AT_name verbatim; accept compiler suffixes only on the ELF name. */
+static bool matches_name(const char *name, const char *identifier)
+{
+	size_t len = strlen(identifier);
+
+	if (strncmp(name, identifier, len))
+		return false;
+	name += len;

tools/objtool/klp-dwarf.c line 881

Quoted 10 lines
tools/objtool/klp-dwarf.c new file +10 -0
+	while (*name) {
+		if (strstarts(name, ".llvm."))
+			name += 5;
+		if (*name++ != '.' || !isdigit((unsigned char)*name))
+			return false;
+		while (isdigit((unsigned char)*name))
+			name++;
+	}
+	return true;
+}

[Severity: Medium]
As reported by Sashiko on RFC [2], how are GCC LTO .lto_priv.<N> suffixes handled in matches_name() and in sym->identity.demangled_name?

In matches_name(), the suffix loop only skips .llvm.<digits> and .<digits>, so an ELF symbol renamed by GCC LTO with .lto_priv.<N> will fail to match its DW_AT_name in matches_dwarf_name().

Similarly, demangled_name_len() in tools/objtool/elf.c only strips .llvm.* and trailing .<digits>, so foo demangles to foo while foo.lto_priv.0 demangles to foo.lto_priv, and foo.lto_priv.0.part.0 vs foo.lto_priv.1.part.0 demangle to foo.lto_priv.0.part vs foo.lto_priv.1.part. Because identity_hash(), same_identity(), and identity->path in tools/objtool/klp-identity.c use identity->demangled_name directly, won't symbols whose .lto_priv.<N> suffix is added, removed, or renumbered before a clone tag end up with mismatched identities across builds?

[ ... ]

Quoted 20 lines
tools/objtool/klp-dwarf.c new file +20 -0
+static bool needs_identity_source(struct symbol *sym, bool external)
+{
+	return !external || (!is_global_sym(sym) && !is_weak_sym(sym));
+}
+
+static void get_die_identity(struct identity_die *entry, struct symbol *sym,
+			     struct identity *identity)
+{
+	const char *source = entry->source;
+
+	identity->scope = entry->scope;
+	identity->external = entry->external;
+	if (entry->cu->assembly) {
+		source = entry->cu->path;
+		identity->scope = "/";
+		/* Assembly linkage is not subject to LTO promotion/internalization. */
+		identity->external = sym->bind != STB_LOCAL;
+	}
+	identity->source = needs_identity_source(sym, identity->external) ? source : "";
+}

[ ... ]

Quoted 28 lines
tools/objtool/klp-dwarf.c new file +28 -0
+/* GCC may omit address attributes for IPA-ICF wrappers such as msr_event_del. */
+static int fallback_by_name(struct elf *elf, struct symbol *sym, struct identity_cu **matched_cu)
+{
+	const char *name = sym->pfunc->identity.demangled_name;
+	const char *file = sym->file ? basename(sym->file->name) : NULL;
+	struct identity_die *entry, *match = NULL;
+	struct identity candidate, matched;
+
+	if (!is_func_sym(sym) && !(is_object_sym(sym) && is_rodata_sec(sym->sec)))
+		return 1;
+	elf_hash_for_each_possible(elf, dwarf_name, entry, name_hash, str_hash(name)) {
+		if (entry->type != sym->type || strcmp(name, entry->name))
+			continue;
+		/* Different CUs can have the same FILE basename and function name. */
+		if (is_func_sym(sym) && !cu_contains_symbol(entry->cu, sym))
+			continue;
+		get_die_identity(entry, sym, &candidate);
+		if (!candidate.source)
+			return 1;
+		if (!entry->cu->assembly && !candidate.external && file &&
+		    strcmp(file, basename(candidate.source)) &&
+		    !known_file_name_mismatch(file, basename(candidate.source)))
+			continue;
+		if (match) {
+			get_die_identity(match, sym, &matched);
+			if (strcmp(matched.source, candidate.source) ||
+			    strcmp(matched.scope, candidate.scope) ||
+			    matched.external != candidate.external)

tools/objtool/klp-dwarf.c line 1084

Quoted 10 lines
tools/objtool/klp-dwarf.c new file +10 -0
+				return 1;
+		}
+		match = entry;
+	}
+	if (!match)
+		return 1;
+	*matched_cu = match->cu;
+	get_die_identity(match, sym, &sym->identity);
+	return 0;
+}

[Severity: Medium]
Can the candidate.external and candidate.source checks in fallback_by_name() cause unrelated DIEs from other translation units to match or collide?

In read_symbols(), sym->file is only populated when sym->bind == STB_LOCAL. For an STB_LOCAL symbol, needs_identity_source() always returns true even when entry->external is true, so candidate.source is set to entry->source.

However, the STT_FILE comparison in fallback_by_name() is guarded by !candidate.external:

	if (!entry->cu->assembly && !candidate.external && file &&
	    strcmp(file, basename(candidate.source)) &&
	    !known_file_name_mismatch(file, basename(candidate.source)))
		continue;

Because of this:

1. When looking up an STB_LOCAL symbol (such as a read-only object where cu_contains_symbol() is not called, or a local function in a shared section), any DW_AT_external DIE with the same name in another CU has candidate.external == true and bypasses the file check. It will then either collide with the local symbol's own DIE at the match comparison (causing fallback_by_name() to return 1) or be chosen as the match for the local symbol.

2. Conversely, when sym is STB_GLOBAL or STB_WEAK (where file is NULL), fallback_by_name() does not filter out non-external (!candidate.external) static DIEs from other CUs, allowing a static DIE in another CU to collide with the global DIE.

3. In addition, index_identities() adds external DIEs to dwarf_name even when entry->source is NULL. Checking if (!candidate.source) return 1; inside the loop before filtering out unrelated CUs aborts the entire search on the first sourceless external DIE from another CU rather than continuing to remaining candidates.

Quoted 50 lines
tools/objtool/klp-dwarf.c new file +50 -0
+
+int get_identity_for_symbol(struct elf *elf, struct symbol *sym)
+{
+	struct identity *identity = &sym->identity, candidate;
+	struct section *sec = sym->sec;
+	struct identity_cu *cu = NULL;
+	struct die_address *address;
+	struct identity_die *entry;
+	unsigned int nr = 0, nr_both_matched = 0;
+	bool source_unavailable = false;
+	const char *error, *saved_objname = objname;
+
+	identity->source = "";
+	identity->scope = NULL;
+	identity->external = false;
+	elf_hash_for_each_possible(elf, dwarf_address, address, hash, sym->offset ^ sec->idx) {
+		if (!address_matches_symbol(address, sym))
+			continue;
+		entry = address->die;
+		nr++;
+		if (matches_dwarf_name(entry, sym->name))
+			nr_both_matched++;
+	}
+	if (!nr) {
+		int ret = fallback_by_name(elf, sym, &cu);
+
+		if (ret == 1)
+			ret = check_if_assembly(elf, sym, &cu);
+		if (ret)
+			return ret;
+		goto found;
+	}
+
+	/*
+	 * ICF/constant merging can leave several definitions after name
+	 * filtering. FIXME: Folded objects can remain uncorrelated when
+	 * their checksums change.
+	 */
+	if (nr_both_matched > 1 && strstarts(sec->name, ".rodata"))
+		return 1;
+
+	/*
+	 * Prefer address and name. Compiler clones, syscall/weak aliases and
+	 * objcopy's __pi_ prefix can differ from DW_AT_name. If no name matches,
+	 * accept address candidates only when their identities agree below.
+	 */
+	objname = elf->name;
+	elf_hash_for_each_possible(elf, dwarf_address, address, hash, sym->offset ^ sec->idx) {
+		if (!address_matches_symbol(address, sym))
+			continue;

tools/objtool/klp-dwarf.c line 1144

Quoted 10 lines
tools/objtool/klp-dwarf.c new file +10 -0
+		entry = address->die;
+		if (nr_both_matched && !matches_dwarf_name(entry, sym->name))
+			continue;
+		get_die_identity(entry, sym, &candidate);
+		error = "conflicting declarations";
+		if (identity->scope && strcmp(identity->scope, candidate.scope))
+			goto failed;
+		error = "conflicting external declarations";
+		if (identity->scope && identity->external != candidate.external)
+			goto failed;

[Severity: Medium]
As reported by Sashiko on RFC [3], there appear to be two issues in get_identity_for_symbol() when handling folded read-only objects and aliases:

1. The constant-merging check tests strstarts(sec->name, ".rodata") instead of is_rodata_sec(sec) (which fallback_by_name() uses above). Because mark_rodata() marks both .rodata* and .data.rel.ro* sections as rodata, won't folded static const objects containing relocated pointers in .data.rel.ro or .data.rel.ro.local (or __pi_-prefixed merged read-only objects where nr > 1 and nr_both_matched == 0) fall through into the loop and fail with a fatal "conflicting declarations" or "conflicting source CUs" error?

2. When an STB_GLOBAL or STB_WEAK symbol is an alias of a static (STB_LOCAL) symbol (for example, a __x64_sys_* syscall stub aliasing a static __do_sys_* function), the alias itself often has no concrete DWARF DIE at that address, leaving only the static target DIE (nr == 1, nr_both_matched == 0). In that case, the loop accepts the static target's DIE and get_die_identity() assigns identity->external = false and a non-empty CU source path to the global or weak alias symbol (or fails at "conflicting external declarations" if both the static target DIE and an external alias/weak DIE exist at the same address). Should a global or weak symbol matched to a non-external DIE at the same address still be treated as external?

[1] https://lore.kernel.org/r/[email protected]
[2] https://lore.kernel.org/r/[email protected]
[3] https://lore.kernel.org/r/[email protected]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#11 lore raw

RFC PATCH v2 5/11 objtool/klp: Support reduced and split DWARF

In reply to Longjun Luo

Read split CUs through libdw and save matching DWO files in a DWP before another build overwrites them. Keep the DWP beside each saved object. Reading saved DWP files requires libdw 0.191 or newer.

Read fixed-width array bounds as unsigned so reduced type information does not make nonempty arrays appear empty.

Full LTO with --plugin-opt=dwo_dir requires
-mllvm -split-dwarf-cross-cu-references=true to preserve separate CUs and their source paths. Kbuild does not enable this DWO output mode.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

Assisted-by LLM
---
 scripts/livepatch/klp-build | 18 +++++++++++++++
 tools/objtool/klp-dwarf.c   | 45 ++++++++++++++++++++++++++++---------
 2 files changed, 53 insertions(+), 10 deletions(-)
scripts/livepatch/klp-build +18 -0
index e373928123e8..95bea5b2057e 100755
@@ -34,6 +34,7 @@ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
 FIX_PATCH_LINES="$SCRIPT_DIR/fix-patch-lines"
 
 OBJTOOL="$PWD/tools/objtool/objtool"
+DWP="${DWP:-llvm-dwp}"
 CONFIG="$PWD/.config"
 TMP_DIR="$PWD/klp-tmp"
 
@@ -265,6 +266,11 @@ validate_config() {
 	[[ -v CONFIG_KLP_BUILD ]] ||			\
 		die "CONFIG_KLP_BUILD not enabled"
 
+	if [[ -v CONFIG_DEBUG_INFO_SPLIT ]]; then
+		command -v "$DWP" &>/dev/null ||
+			die "split DWARF requires llvm-dwp (or set DWP)"
+	fi
+
 	[[ -v CONFIG_GCC_PLUGIN_LATENT_ENTROPY ]] &&	\
 		die "kernel option 'CONFIG_GCC_PLUGIN_LATENT_ENTROPY' not supported"
 
@@ -588,6 +594,15 @@ find_objects() {
 		    -printf '%P\n'
 }
 
+# Save the matching DWOs before the next build overwrites them.
+save_split_dwarf() {
+	local file="$1"
+	local dest="$2"
+
+	[[ -v CONFIG_DEBUG_INFO_SPLIT ]] || return 0
+	"$DWP" -e "$file" -o "$dest.dwp" || die "cannot save split DWARF for $file"
+}
+
 # Copy all .o archives to $ORIG_DIR
 copy_orig_objects() {
 	local files=()
@@ -608,6 +623,7 @@ copy_orig_objects() {
 
 		mkdir -p "$orig_dir"
 		cp -f "$file" "$orig_dir"
+		save_split_dwarf "$file" "$orig_file"
 	done
 	xtrace_restore
 
@@ -647,6 +663,7 @@ copy_patched_objects() {
 
 		mkdir -p "$patched_dir"
 		cp -f "$file" "$patched_dir"
+		save_split_dwarf "$file" "$patched_file"
 		found=1
 	done
 	xtrace_restore
@@ -680,6 +697,7 @@ generate_checksums() {
 
 		mkdir -p "$(dirname "$dest")"
 		cp -f "$file" "$dest"
+		[[ -f "$file.dwp" ]] && cp -f "$file.dwp" "$dest.dwp"
 		"$OBJTOOL" klp checksum "$dest"
 	done
 
tools/objtool/klp-dwarf.c +35 -10

87 lines not on this page · raw message

Signature · 1 line
2.55.0
#12 lore raw

RFC PATCH v2 6/11 klp-build: Require CONFIG_DEBUG_INFO

In reply to Longjun Luo

Require the kernel's debug information configuration for DWARF-based symbol correlation. Use the C and assembly flags supplied by Kbuild instead of adding separate -g flags in klp-build.

Reduced and split DWARF are supported by the reader and saved-object handling introduced earlier in this series.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. No failures.

---
 scripts/livepatch/klp-build | 3 +++
 1 file changed, 3 insertions(+)
scripts/livepatch/klp-build +3 -0
index 95bea5b2057e..da55e1028fe3 100755
@@ -266,6 +266,9 @@ validate_config() {
 	[[ -v CONFIG_KLP_BUILD ]] ||			\
 		die "CONFIG_KLP_BUILD not enabled"
 
+	[[ -v CONFIG_DEBUG_INFO ]] ||			\
+		die "CONFIG_DEBUG_INFO not enabled"
+
 	if [[ -v CONFIG_DEBUG_INFO_SPLIT ]]; then
 		command -v "$DWP" &>/dev/null ||
 			die "split DWARF requires llvm-dwp (or set DWP)"
Signature · 1 line
2.55.0
#13 lore raw

RFC PATCH v2 7/11 objtool/klp: Correlate symbols using DWARF identities

In reply to Longjun Luo

Matching local symbols by compiler suffixes or symbol-table position can pair unrelated state or copy existing state into a patch.

Match unique DWARF identities in both directions. Use checksums to resolve remaining candidates within the same identity. Leave ambiguous symbols uncorrelated instead of pairing them by position.

Compare __UNIQUE_ID data names without their counters. Use identity and checksum uniqueness to preserve the original debug descriptors when an earlier macro shifts their counters. Add a regression test.

Reuse data aliases through their representative twins. Retain a separate unique FILE/name fallback with warnings for unknown cases, and exclude link-time metadata from correlation.

Reject linkage changes because later name-based resolution can disagree with the changed identity. Reject duplicate identities when the same source is compiled into multiple objects; distinguishing those instances would require another qualifier such as link order.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM
---
 tools/objtool/include/objtool/klp.h           |   1 +
 tools/objtool/klp-diff.c                      | 728 ++++++++----------
 .../generic/fixtures/identity/unique_id.c     |  50 ++
 .../tests/generic/test-identity-unique-id.sh  |  31 +
 .../tests/generic/test-thinlto-ambiguity.sh   |   2 +-
 .../tests/generic/test-thinlto-local.sh       |   4 +-
 tools/objtool/tests/lib.sh                    |   5 +-
 7 files changed, 429 insertions(+), 392 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/unique_id.c
 create mode 100755 tools/objtool/tests/generic/test-identity-unique-id.sh
tools/objtool/include/objtool/klp.h +1 -0
index 90468e141124..fa794bab116f 100644
@@ -51,6 +51,7 @@ struct objtool_file;
 struct elf;
 struct symbol;
 
+bool dont_correlate(struct symbol *sym);
 bool is_compound_constant(struct symbol *sym);
 
 int klp_create_symid_sections(struct objtool_file *file);
tools/objtool/klp-diff.c +341 -387

905 lines not on this page · raw message

tools/objtool/tests/generic/fixtures/identity/unique_id.c new file +50 -0

50 lines not on this page · raw message

tools/objtool/tests/generic/test-identity-unique-id.sh new file +31 -0
new file mode 100755
index 000000000000..d4dbaafbe575
@@ -0,0 +1,31 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Adding a counter-using macro must preserve existing dynamic debug descriptors.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+for flags in "" "-DTWO" "-DTWO -DCHANGE_REPORT"; do
+	build_pair identity/unique_id.c $flags
+	if [[ "$flags" != *CHANGE_REPORT* ]]; then
+		assert_checksum_matches report
+	fi
+	run_diff
+	assert_patched clamp_value
+	assert_no_section __dyndbg
+	diff_log | grep -q 'no correlation:' && fail "dynamic debug descriptor lost correlation"
+	if [[ "$flags" == *CHANGE_REPORT* ]]; then
+		assert_patched report
+		old="$(in_symbols orig.o |
+			awk '$4 == "OBJECT" && $8 ~ /__UNIQUE_ID_ddebug_/ { print $8 }')"
+		[ "$(echo "$old" | wc -w)" = 2 ] || fail "expected two original descriptors"
+		run_post_link
+		for sym in $old; do
+			assert_original_reference report "$sym"
+		done
+	else
+		assert_not_patched report
+	fi
+done
+
+pass "counter shifts preserve descriptors in unchanged and changed functions"
tools/objtool/tests/generic/test-thinlto-ambiguity.sh +1 -1
index 34f4f3a58e20..4e0e49a9fc56 100755
@@ -38,7 +38,7 @@ build_thinlto()		# $1 output object, $2 extra flags
 {
 	local t
 	for t in "" -DTU_B -DTU_C; do
-		$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections \
+		$THIN_CC -flto=thin -O2 -g -ffunction-sections -fdata-sections \
 			$2 $t -c "$FIXTURES_DIR/thinlto_ambiguity.c" \
 			-o "$workdir/tu$t.o" 2>/dev/null || return 1
 	done
tools/objtool/tests/generic/test-thinlto-local.sh +2 -2
index a266263676ed..71156e0b59ce 100755
@@ -15,9 +15,9 @@ find_thinlto_toolchain ||
 
 build_thinlto()		# $1 output object, $2 extra flags
 {
-	$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 \
+	$THIN_CC -flto=thin -O2 -g -ffunction-sections -fdata-sections $2 \
 		-c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_a.o" 2>/dev/null || return 1
-	$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections $2 -DTU_B \
+	$THIN_CC -flto=thin -O2 -g -ffunction-sections -fdata-sections $2 -DTU_B \
 		-c "$FIXTURES_DIR/thinlto_local.c" -o "$workdir/tu_b.o" 2>/dev/null || return 1
 	"$THIN_LD" -r "$workdir/tu_a.o" "$workdir/tu_b.o" -o "$1" 2>/dev/null || return 1
 }
tools/objtool/tests/lib.sh +3 -2
index 8e1aa7614dc6..44ce670c4a6c 100644
@@ -67,7 +67,7 @@ klp_preflight()
 
 	tmp="$(mktemp -d)" || bail "mktemp failed"
 	echo 'int probe(void) { return 0; }' > "$tmp/probe.c"
-	$CC -c -o "$tmp/probe.o" "$tmp/probe.c" 2>/dev/null ||
+	$CC -g -c -o "$tmp/probe.o" "$tmp/probe.c" 2>/dev/null ||
 		{ rm -rf "$tmp"; bail "$CC cannot compile a trivial object"; }
 
 	# $CC, $ARCH and objtool have to agree about the target, and cross runs
@@ -139,6 +139,7 @@ EOF
 # it lives at the compile where an override cannot drop it.
 #
 #   -O2					the kernel's default
+#   -g					required for DWARF symbol identity
 #   -ffunction-sections -fdata-sections	klp-build passes these itself, through
 #					KCFLAGS, whatever the configuration
 #   -fno-asynchronous-unwind-tables	arch/x86/Makefile sets this always, so
@@ -150,7 +151,7 @@ EOF
 #					checksum
 #
 # A test overrides it; see tools/objtool/Documentation/klp-write-tests.txt.
-FIXTURE_CFLAGS="-O2 -ffunction-sections -fdata-sections -fno-common \
+FIXTURE_CFLAGS="-O2 -g -ffunction-sections -fdata-sections -fno-common \
 		-fno-asynchronous-unwind-tables"
 
 test_name="$(basename "$0" .sh)"
Signature · 1 line
2.55.0
#14 lore raw

Re: [RFC PATCH v2 07/11] objtool/klp: Correlate symbols using DWARF identities

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] objtool/klp: broken checksum and twin resolution for aliased symbols - [High] objtool/klp…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] objtool/klp: broken checksum and twin resolution for aliased symbols
- [High] objtool/klp: mark_changed_functions() treats ambiguous functions as new
- [Medium] objtool/klp: build_symtab_hash() uses sym->file->name instead of DWARF source
- [Medium] objtool/klp: check_linkage_changes() matches unrelated symbols across TUs
- [Medium] objtool/klp: __LINE__ shifts break __UNIQUE_ID_ddebug_* correlation
- [Medium] objtool/klp: unreachable .lto_priv matching in skip_lto_priv()
- [Medium] objtool/klp: broken .llvm and counter matching in compatible_symbol_name()
- [Medium] objtool/klp: zero-length symbols skipped by correlation are cloned as new
- [Medium] objtool/klp: adding -g breaks sympos, linkage-flip, and multi-TU tests --

commit 4e3aabc6f0126e260b43d87b5c01364b588687eb
Author: Longjun Luo <[email protected]>

objtool/klp: Correlate symbols using DWARF identities

This commit replaces positional and suffix-based symbol correlation in objtool klp diff with DWARF identity matching, checksum disambiguation, and a restricted FILE/name fallback.

Quoted 4 lines
tools/objtool/klp-diff.c +0 -0
index cd5e683d6468b..468b8654b6871 100644

[ ... ]

Quoted 106 lines
tools/objtool/klp-diff.c +49 -38
@@ -486,361 +473,357 @@ static bool dont_correlate(struct symbol *sym)
 	       is_anonymous_rodata(sym) ||
 	       is_initcall_sym(sym) ||
 	       is_addressable_sym(sym) ||
+	       is_metadata_section(sym->sec) ||
 	       is_special_section(sym->sec) ||
 	       is_special_section_aux(sym->sec);
 }
 
-static const char *llvm_suffix(const char *name)
+static size_t llvm_name_len(const char *name)
 {
-	return strstr(name, ".llvm.");
+	const char *suffix = strstr(name, ".llvm."), *p;
+
+	if (!suffix || !isdigit((unsigned char)suffix[6]))
+		return strlen(name);
+	for (p = suffix + 6; isdigit((unsigned char)*p); p++)
+		;
+	return *p ? strlen(name) : (size_t)(suffix - name);
 }
 
-static bool is_llvm_sym(struct symbol *sym)
+/* Keep object-fragment numbers, e.g. amd_northbridges.0. */
+static bool same_llvm_name(const char *a, const char *b)
 {
-	return llvm_suffix(sym->name);
+	size_t a_len = llvm_name_len(a), b_len = llvm_name_len(b);
+
+	return a_len == b_len && !strncmp(a, b, a_len);
 }
 
-/*
- * Determine if two symbols have compatible source file origins:
- *
- *   - If both symbols are local, only return true if they belong to the same
- *     ELF file symbol.
- *
- *   - If both symbols are global, always return true, as globals don't have
- *     file associations.
- *
- *   - If they have different scopes, also return true, as the patch might have
- *     changed the symbol's scope.
- *
- * Works for both same-ELF (direct pointer compare) and cross-ELF
- * (compare via file->twin) cases.
- */
-static bool maybe_same_file(struct symbol *sym1, struct symbol *sym2)
+static const char *skip_lto_priv(const char *name)
 {
-	if (!sym1->file || !sym2->file)
-		return true;
-	if (sym1->file == sym2->file)
-		return true;
-	return sym1->file->twin == sym2->file;
+	if (!strstarts(name, ".lto_priv.") || !isdigit((unsigned char)name[10]))
+		return name;
+	name += 10;
+	while (isdigit((unsigned char)*name))
+		name++;
+	return name;
 }
 
-/*
- * Similar to maybe_same_file(), but strict: no scope changes allowed.
- *
- * Works for both same-ELF (direct pointer compare) and cross-ELF
- * (compare via file->twin) cases.
- */
-static bool same_file(struct symbol *sym1, struct symbol *sym2)
+static bool is_numeric_suffix(const char *name)
 {
-	if (llvm_suffix(sym1->name) && llvm_suffix(sym2->name))
-		return true;
-	if (!sym1->file && !sym2->file)
-		return true;
-	if (!sym1->file || !sym2->file)
+	if (*name++ != '.' || !isdigit((unsigned char)*name))
 		return false;
-	if (sym1->file == sym2->file)
+	while (isdigit((unsigned char)*name))
+		name++;
+	return !*name;
+}
+
+/* Allow compiler counters and GCC partition renames to change. */
+static bool same_compiler_name(const char *a, const char *b)
+{
+	for (;;) {
+		const char *next_a = skip_lto_priv(a), *next_b = skip_lto_priv(b);
+
+		if (next_a != a || next_b != b) {
+			a = next_a;
+			b = next_b;
+			continue;
+		}
+		if (*a != *b)
+			break;
+		if (!*a)
+			return true;
+		if (*a == '.' && isdigit((unsigned char)a[1])) {
+			if (!isdigit((unsigned char)b[1]))
+				return false;
+			for (a++; isdigit((unsigned char)*a); a++)
+				;
+			for (b++; isdigit((unsigned char)*b); b++)

tools/objtool/klp-diff.c static bool dont_correlate(struct symbol *sym) · line 549

Quoted 10 lines
tools/objtool/klp-diff.c +10 -0
+				;
+		} else {
+			a++;
+			b++;
+		}
+	}
+
+	/* A trailing counter may be absent on one side. */
+	return (!*a && is_numeric_suffix(b)) || (!*b && is_numeric_suffix(a));
+}

[Severity: Medium]
Can skip_lto_priv() in same_compiler_name() ever match when .lto_priv.<N> is added or removed on one side, or when an internal counter changes before another suffix?

In tools/objtool/elf.c, demangled_name_len() only strips .llvm.<hash> and trailing .<digits> suffixes, stopping at .lto_priv because 'v' is not a digit:

tools/objtool/elf.c:demangled_name_len() {
    ...
    while (p > name) {
        while (p > name && isdigit((unsigned char)p[-1]))
            p--;
        if (p == name || p[-1] != '.')
            break;
        p--;
    }
    ...
}

So demangle_name() produces "foo" for "foo", "foo.lto_priv" for "foo.lto_priv.0", "foo.lto_priv.0.part" for "foo.lto_priv.0.part.0", and "var.0.lto_priv" for "var.0.lto_priv.0".

Because find_twin() and find_twin_by_identity_and_checksum() look up candidates via identity_hash(&sym1->identity) (which hashes identity.demangled_name) and is_twin_candidate() checks same_identity(&sym1->identity, &sym2->identity) before calling compatible_symbol_name(), any pair where .lto_priv.<N> is present on only one side or where an internal .lto_priv.<N> or .<N> changes before another suffix will have different demangled_name strings and be rejected before same_compiler_name() is reached.

tools/objtool/klp-diff.c static bool dont_correlate(struct symbol *sym) · line 559

Quoted 11 lines
tools/objtool/klp-diff.c +8 -1
+
+/* Keep clone tags such as .cold, .isra and .part distinct. */
+static bool compatible_symbol_name(const char *a, const char *b)
+{
+	if (same_llvm_name(a, b))
 		return true;
-	return sym1->file->twin == sym2->file;
+	if (strchr(a, '@') || strchr(b, '@') || strstr(a, ".str1."))
+		return !strcmp(a, b);
+	return same_compiler_name(a, b);
 }

[Severity: Medium]
What happens in compatible_symbol_name() when a symbol has both a .llvm.<digits> suffix (or .lto_priv.<digits> suffix) and a compiler counter suffix that was added or changed?

First, same_llvm_name(a, b) strips .llvm.<digits> via llvm_name_len(), but requires the remaining prefixes to be byte-for-byte identical. When a counter suffix was added or changed (for example, target.done.llvm.111 vs target.done.1.llvm.222, or target.done vs target.done.1.llvm.222 when ThinLTO promotion changes), same_llvm_name() returns false and compatible_symbol_name() falls through to same_compiler_name(a, b) with the unstripped strings.

Because same_compiler_name() does not strip or skip .llvm.<digits>, it compares ".llvm.111" against ".1.llvm.222" (or "" against ".1.llvm.222") and returns false. Similarly, for a pair like foo.lto_priv.0 vs foo.0.lto_priv.0, skip_lto_priv() consumes .lto_priv.0 on the first string, breaks out of the loop at the '.' of .0.lto_priv.0, and calls is_numeric_suffix(".0.lto_priv.0"), which returns false because is_numeric_suffix() does not skip .lto_priv.<digits>.

Second, same_llvm_name() has the comment "Keep object-fragment numbers, e.g. amd_northbridges.0.", but when same_llvm_name() returns false for two different fragment numbers (such as amd_northbridges.0 vs amd_northbridges.1), compatible_symbol_name() falls through to same_compiler_name(a, b), which treats .0 and .1 as matching compiler counters unless one side has a .llvm.<digits> suffix.

Should .llvm.<digits> be stripped before comparing prefixes in same_compiler_name(), or how are object-fragment numbers meant to be distinguished from compiler counters?

Quoted 16 lines
tools/objtool/klp-diff.c +8 -6
-/*
- * Is it a local symbol, or at least was it local in the translation unit
- * before LLVM promoted it?
- */
-static bool is_tu_local_sym(struct symbol *sym)
+static bool is_twin_candidate(struct symbol *sym1, struct symbol *sym2)
 {
-	return is_local_sym(sym) || is_llvm_sym(sym);
+	if (sym1->type != sym2->type)
+		return false;
+	/* Relocations can also reach targets without DWARF identities. */
+	if (has_dwarf_identity(sym1) && has_dwarf_identity(sym2) &&
+	    !same_identity(&sym1->identity, &sym2->identity))
+		return false;
+	/* Don't match zero-length labels with sized symbols. */

tools/objtool/klp-diff.c static bool dont_correlate(struct symbol *sym) · line 579

Quoted 10 lines
tools/objtool/klp-diff.c +9 -0
+	if (!is_undef_sym(sym1) && !is_undef_sym(sym2) &&
+	    (!sym1->len != !sym2->len))
+		return false;
+	/* __UNIQUE_ID counters shift when an earlier macro is added. */
+	if (is_object_sym(sym1) &&
+	    (strstarts(sym1->name, "__UNIQUE_ID_") || strstr(sym1->name, ".__UNIQUE_ID_")) &&
+	    (strstarts(sym2->name, "__UNIQUE_ID_") || strstr(sym2->name, ".__UNIQUE_ID_")))
+		return !strcmp(sym1->identity.demangled_name, sym2->identity.demangled_name);
+	return compatible_symbol_name(sym1->name, sym2->name);
 }

[Severity: Medium]
Does matching __UNIQUE_ID_ddebug_* symbols here work when a patch adds or removes lines earlier in the source file?

Both DEFINE_DYNAMIC_DEBUG_METADATA_CLS() in include/linux/dynamic_debug.h (.lineno = __LINE__) and DEBUG_CALL() in tools/objtool/tests/generic/fixtures/identity/unique_id.c embed __LINE__ in each static descriptor in section __dyndbg, and checksum_update_object() in tools/objtool/klp-checksum.c hashes the raw object bytes including that line number field.

In unique_id.c, the #ifdef PATCHED and #else branches have the same number of lines, so __LINE__ stays identical. However, if a patch inserts or deletes lines above a function containing pr_debug():

1. When a function has a single pr_debug() descriptor, correlate_by_identity() pairs the original and patched __UNIQUE_ID_ddebug_* symbols by their unique DWARF identity, and mark_changed_functions() then sees orig_sym->csum.checksum != patched_sym->csum.checksum (due to the changed line number) and aborts with ERROR("changed data: __UNIQUE_ID_ddebug_...") even if the function containing pr_debug() was not modified.

2. When a changed function has multiple pr_debug() descriptors (which share the same scope and demangled name __UNIQUE_ID_ddebug), correlate_by_identity() defers to extend_same_twins_by_checksum() -> find_twin_by_identity_and_checksum(), which requires sym1->csum.checksum == sym2->csum.checksum and fails to correlate any descriptor whose __LINE__ shifted.

Quoted 181 lines
tools/objtool/klp-diff.c +60 -101
-/*
- * Try to find sym1's twin in patched using deterministic matching.
- *
- * Multiple symbols can share a demangled name (e.g., static functions in
- * different TUs).  This function counts same-named candidates through a
- * funnel of progressively tighter filters.  Each level is a strict subset
- * of the previous one.
- *
- * The widest level that yields a 1:1 match wins.  Narrower levels are only
- * needed when the wider level is ambiguous (count > 1).
- *
- * Candidates are pre-filtered by maybe_same_file(), which narrows most
- * local symbols to their own TU.  For example, 19 different static
- * type_show() functions across vmlinux.o each see only one candidate after
- * pre-filtering, so they match immediately at Level 1.
- *
- * Level 1 (name): Works when the demangled name is unique after
- * pre-filtering.  Handles most symbols: unique globals like copy_signal(),
- * or per-TU locals like pcspkr_probe().
- *
- * Level 2 (scope): Filters by local-vs-global (TU-local-vs-not).  Example:
- * parse_header() exists as both a static and a global function.  Level 1
- * sees both (same demangled name), but Level 2 separates them by scope.
- *
- * Level 3 (file): Strict file matching via same_file(), which rejects scope
- * changes.  Example: LLVM-promoted foo.llvm.12345 (global, no FILE symbol)
- * vs genuine local foo (has FILE symbol).  Both are TU-local so Level 2
- * can't distinguish them, but same_file() rejects the pair because one has
- * a file association and the other doesn't.
- *
- * Level 4 (checksum): Distinguishes by function checksum.  Example:
- * usb_devnode.llvm.AAA and usb_devnode.llvm.BBB are two LLVM-promoted
- * functions from different TUs with the same demangled name.  After a TU
- * change, the .llvm. hashes change but the functions themselves may be
- * unchanged.  Level 4 matches each to the patched candidate with the
- * same checksum.
- */
-static struct symbol *find_twin(struct elfs *e, struct symbol *sym1)
+static struct symbol *find_twin(struct elf *elf, struct symbol *sym1)
 {
-	struct symbol *name_last = NULL, *scope_last = NULL,
-		      *file_last = NULL, *csum_last = NULL;
-	unsigned int name_orig = 0, name_patched = 0;
-	unsigned int scope_orig = 0, scope_patched = 0;
-	unsigned int file_orig = 0, file_patched = 0;
-	unsigned int csum_orig = 0, csum_patched = 0;
 	struct symbol *sym2, *match = NULL;
+	unsigned int candidates = 0;
+	u32 key;
 
-	/* Count orig candidates */
-	for_each_sym_by_demangled_name(e->orig, sym1->identity.demangled_name, sym2) {
-		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
-		    (!maybe_same_file(sym1, sym2)))
+	if (!has_dwarf_identity(sym1))
+		return NULL;
+	key = identity_hash(&sym1->identity);
+
+	elf_hash_for_each_possible(elf, symbol_identity, sym2, identity_hash, key) {
+		if (sym2->twin || !is_twin_candidate(sym1, sym2))
 			continue;
+		match = sym2;
+		candidates++;
+	}
 
-		/* Level 1: name match (widest filter)  */
-		name_orig++;
+	return candidates == 1 ? match : NULL;
+}
 
-		/* Level 2: scope (scope changes allowed) */
-		if (is_tu_local_sym(sym1) != is_tu_local_sym(sym2))
-			continue;
-		scope_orig++;
+/* Checksums can disambiguate only unchanged symbols. */
+static struct symbol *find_twin_by_identity_and_checksum(struct elf *elf, struct symbol *sym1)
+{
+	struct symbol *sym2, *match = NULL;
+	unsigned int candidates = 0;
+	u32 key;
 
-		/* Level 3: file (scope changes disallowed) */
-		if (!same_file(sym1, sym2))
-			continue;
-		file_orig++;
+	if (!has_dwarf_identity(sym1) || !sym1->csum.checksum)
+		return NULL;
+	key = identity_hash(&sym1->identity);
 
-		/* Level 4: checksum (unchanged symbols) */
-		if (sym1->len != sym2->len || !sym1->csum.checksum ||
+	elf_hash_for_each_possible(elf, symbol_identity, sym2, identity_hash, key) {
+		if (!is_twin_candidate(sym1, sym2))
+			continue;
+		if (sym2->twin || sym1->len != sym2->len ||
 		    sym1->csum.checksum != sym2->csum.checksum)
 			continue;
-		csum_orig++;
+		match = sym2;
+		candidates++;
 	}
 
-	/* Count patched candidates */
-	for_each_sym_by_demangled_name(e->patched, sym1->identity.demangled_name, sym2) {
-		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
-		    !maybe_same_file(sym1, sym2))
-			continue;
-
-		/* Level 1 */
-		name_patched++;
-		name_last = sym2;
+	return candidates == 1 ? match : NULL;
+}
 
-		/* Level 2 */
-		if (is_tu_local_sym(sym1) != is_tu_local_sym(sym2))
-			continue;
-		scope_patched++;
-		scope_last = sym2;
+static int correlate_by_identity(struct elfs *e)
+{
+	struct symbol *sym, *match;
 
-		/* Level 3 */
-		if (!same_file(sym1, sym2))
+	for_each_sym(e->orig, sym) {
+		/* Compound literal names are compiler counters, not stable identities. */
+		if (sym->twin || sym->dont_correlate || is_compound_constant(sym))
 			continue;
-		file_patched++;
-		file_last = sym2;
-
-		/* Level 4 */
-		if (sym1->len != sym2->len || !sym1->csum.checksum ||
-		    sym1->csum.checksum != sym2->csum.checksum)
+		match = find_twin(e->patched, sym);
+		/* Two originals can share the same sole patched candidate. */
+		if (!match || find_twin(e->orig, match) != sym)
 			continue;
-		csum_patched++;
-		csum_last = sym2;
+		sym->twin = match;
+		match->twin = sym;
+		dbg_correlate("%s(): %s -> %s", __func__, sym->name, match->name);
 	}
+	return 0;
+}
 
-	/* Return the widest level that yields a unique (1:1) match */
-	if (name_orig == 1 && name_patched == 1)
-		match = name_last;
-	else if (scope_orig == 1 && scope_patched == 1)
-		match = scope_last;
-	else if (file_orig == 1 && file_patched == 1)
-		match = file_last;
-	else if (csum_orig == 1 && csum_patched == 1)
-		match = csum_last;
-
-	if (!match)
-		return NULL;
+static int extend_same_twins_by_checksum(struct elfs *e)
+{
+	struct symbol *sym, *match;
 
-	if (name_orig != 1 || name_patched != 1)
-		dbg_correlate("find_twin(): %s%s -> %s%s",
-			      sym1->name, is_func_sym(sym1) ? "()" : "",
-			      match->name, is_func_sym(match) ? "()" : "");
+	for_each_sym(e->orig, sym) {
+		if (sym->twin || sym->dont_correlate)
+			continue;
+		match = find_twin_by_identity_and_checksum(e->patched, sym);
+		/* Two originals can share the same sole patched candidate. */
+		if (!match || find_twin_by_identity_and_checksum(e->orig, match) != sym)
+			continue;
+		sym->twin = match;
+		match->twin = sym;
+		dbg_correlate("%s(): %s -> %s", __func__, sym->name, match->name);
+	}
+	return 0;

tools/objtool/klp-diff.c static bool dont_correlate(struct symbol *sym) · line 668

Quoted 10 lines
tools/objtool/klp-diff.c +7 -1
+}
 
-	return match;
+/* Data aliases can reuse the representative's storage without another twin. */
+static struct symbol *reference_twin(struct symbol *sym)
+{
+	if (!sym->twin && is_object_sym(sym))
+		return sym->alias->twin;
+	return sym->twin;
 }

[Severity: High]
How does alias handling work across find_twin_by_identity_and_checksum(), reference_twin(), check_linkage_changes(), and mark_changed_functions() when non-representative alias symbols are present?

In tools/objtool/elf.c (elf_add_symbol()), when multiple symbols share the same section, offset, type, and non-zero length, each earlier symbol has sym->alias set to the last symbol at that offset (the representative symbol, where sym->alias == sym). In tools/objtool/klp-checksum.c (calculate_checksums()), symbols with is_alias_sym(sym) (sym->alias != sym) are skipped, so read_sym_checksums() populates sym->csum.checksum only on the representative symbol and leaves sym->csum.checksum as 0 on all other aliases at that address.

However, correlate_by_identity() correlates symbols regardless of is_alias_sym(sym), which appears to cause several issues:

1. In mark_changed_functions(), orig_sym->csum.checksum != patched_sym->csum.checksum is compared directly instead of via orig_sym->alias->csum.checksum and patched_sym->alias->csum.checksum. If constant merging or symbol ordering changes whether a correlated symbol is the representative (checksum != 0) versus a non-representative alias (checksum == 0) between orig and patched, mark_changed_functions() compares a non-zero checksum against 0 and aborts with ERROR("changed data: %s") on unchanged data (or falsely marks an unchanged function as changed). Conversely, if both sides of a correlated pair are non-representative aliases (checksum == 0 on both sides) while the representative symbol is uncorrelated, mark_changed_functions() compares 0 != 0 and misses actual data modifications.

2. In find_twin_by_identity_and_checksum(), sym1->csum.checksum and sym2->csum.checksum are checked directly instead of sym1->alias->csum.checksum and sym2->alias->csum.checksum, so non-representative aliases always have checksum == 0 and cannot be disambiguated by checksum.

3. In reference_twin(), sym->alias only points one-way from earlier symbols to the representative symbol. If an earlier non-representative alias at the same address was correlated while the representative symbol itself was not correlated, calling reference_twin() on the representative symbol evaluates sym->alias->twin (which is sym->twin == NULL) and fails to find the correlated alias's twin.

4. In check_linkage_changes(), orig_sym->twin and patched_sym->twin are checked directly rather than reference_twin(orig_sym) and reference_twin(patched_sym).

Quoted 86 lines
tools/objtool/klp-diff.c +34 -37
-struct llvm_suffix_pair {
+struct symtab_entry {
 	struct hlist_node hash;
-	const char *orig;
-	const char *patched;
+	const char *file, *name;
+	struct symbol *sym;
+	bool is_unique;
+};
+
+struct symtab_hash {
+	DECLARE_HASHTABLE(entries, 12);
 };
 
-static DECLARE_HASHTABLE(suffix_map, 7);
+static struct symtab_entry *find_symtab_entry(struct symtab_hash *table, struct symbol *sym)
+{
+	const char *file = sym->file ? sym->file->name : "";
+	const char *name = sym->identity.demangled_name;
+	u32 key = jhash_2words(str_hash(file), str_hash(name), 0);
+	struct symtab_entry *entry;
 
-/*
- * Build a mapping of known orig-to-patched LLVM suffixes based on
- * already-correlated symbol pairs.  All promoted symbols from the same TU
- * share the same .llvm.<hash> suffix, so one correlated pair seeds the map
- * for the entire TU.
- */
-static int update_suffix_map(struct elf *elf)
+	hash_for_each_possible(table->entries, entry, hash, key) {
+		if (!strcmp(entry->file, file) && !strcmp(entry->name, name))
+			return entry;
+	}
+	return NULL;
+}
+
+static int build_symtab_hash(struct elf *elf, struct symtab_hash *table)
 {
-	struct llvm_suffix_pair *entry;
+	struct symtab_entry *entry;
 	struct symbol *sym;
+	u32 key;
 
+	hash_init(table->entries);
 	for_each_sym(elf, sym) {
-		const char *s1, *s2;
-		bool found;
-
-		if (!sym->twin)
+		if (!sym->len || is_undef_sym(sym) || sym->dont_correlate ||
+		    is_compound_constant(sym))
 			continue;
-
-		s1 = llvm_suffix(sym->name);
-		s2 = llvm_suffix(sym->twin->name);
-
-		if (!s1 || !s2)
+		/* Already matched symbols still count against uniqueness. */
+		entry = find_symtab_entry(table, sym);
+		if (entry) {
+			entry->is_unique = false;
 			continue;
-
-		found = false;
-		hash_for_each_possible(suffix_map, entry, hash, str_hash(s1)) {
-			if (!strcmp(entry->orig, s1)) {
-				found = true;
-				break;
-			}
 		}
-		if (found)
-			continue;
-
-		entry = calloc(1, sizeof(*entry));
+		entry = malloc(sizeof(*entry));
 		if (!entry) {
-			ERROR_GLIBC("calloc");
+			ERROR_GLIBC("malloc symtab entry");
 			return -1;
 		}
-
-		entry->orig = s1;
-		entry->patched = s2;
-		hash_add(suffix_map, &entry->hash, str_hash(s1));
+		/* Assembly may inherit FILE from the preceding input. */

tools/objtool/klp-diff.c static bool dont_correlate(struct symbol *sym) · line 726

Quoted 10 lines
tools/objtool/klp-diff.c +6 -1
+		entry->file = sym->file ? sym->file->name : "";
+		entry->name = sym->identity.demangled_name;
+		entry->sym = sym;
+		entry->is_unique = true;
+		key = jhash_2words(str_hash(entry->file), str_hash(entry->name), 0);
+		hash_add(table->entries, &entry->hash, key);
 	}
-
 	return 0;
 }

[Severity: Medium]
Should find_symtab_entry() and build_symtab_hash() use sym->file->identity.source instead of sym->file->name?

In tools/objtool/klp-identity.c (prepare_identities()), STT_FILE symbols have sym->identity.source initialized to sym->name and then updated by complete_file_source() in tools/objtool/klp-dwarf.c with the full normalized DWARF CU path.

By reading sym->file->name here instead of sym->file->identity.source, sym->file->identity.source is never read after complete_file_source(), and in multi-TU objects such as vmlinux.o where different directories share an STT_FILE basename (for example, drivers/base/core.c and net/core/core.c), symbols from different translation units collide in symtab_hash under the same basename.

[ ... ]

Quoted 11 lines
tools/objtool/klp-diff.c +4 -4
@@ -744,102 +731,67 @@ static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1)
 {
-	unsigned int idx_orig = 0, idx_patched = 0;
-	unsigned int sym1_pos = 0;
-	struct symbol *sym2, *match = NULL;
+	struct symtab_hash orig_table = {}, patched_table = {};
+	struct symtab_entry *orig_entry, *patched_entry;
+	struct symbol *orig_sym, *patched_sym;
+	int ret = -1;
 
-	for_each_sym_by_demangled_name(e->orig, sym1->identity.demangled_name, sym2) {

tools/objtool/klp-diff.c static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1) · line 744

Quoted 10 lines
tools/objtool/klp-diff.c +7 -2
-		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
-		    !maybe_same_file(sym1, sym2))
+	if (build_symtab_hash(e->orig, &orig_table) ||
+	    build_symtab_hash(e->patched, &patched_table))
+		goto out;
+
+	for_each_sym(e->patched, patched_sym) {
+		if (!patched_sym->len || is_undef_sym(patched_sym) ||
+		    patched_sym->dont_correlate || reference_twin(patched_sym))
 			continue;

[Severity: Medium]
What happens to defined zero-length symbols (where sym->len == 0, such as assembly SYM_INNER_LABEL or SYM_DATA_END_LABEL markers like __bhi_args_end, hv_crash_asm_end, ftrace_caller_end, or empty C arrays) when they are referenced by a changed function?

Previously, find_twin() correlated defined zero-length symbols by name, scope, and file because only Level 4 (checksum) required sym->len > 0. With this commit, is_dwarf_identity_candidate() in tools/objtool/klp-identity.c requires sym->len, and both build_symtab_hash() and validate_symbol_twins() skip !sym->len. (In the follow-up commits "objtool/klp: Extend twins through unchanged function references" and "objtool/klp: Check unmatched data references", extend_twins_by_relocations() only walks relocations in unchanged functions and check_unmatched_reference() also skips !patched_sym->len.)

If a changed function references an existing unexported or local defined zero-length symbol that is not referenced by an unchanged function in the object, reference_twin(patched_sym) remains NULL, klp_reloc_needed() returns false, and clone_reloc() clones a new 0-byte symbol into the livepatch module instead of emitting a KLP relocation to the original symbol.

Quoted 173 lines
tools/objtool/klp-diff.c +70 -82
-		if (is_tu_local_sym(sym1) != is_tu_local_sym(sym2) ||
-		    is_llvm_sym(sym1) != is_llvm_sym(sym2))
+		patched_entry = find_symtab_entry(&patched_table, patched_sym);
+		if (!patched_entry || !patched_entry->is_unique)
 			continue;
-		if (sym1 == sym2)
-			sym1_pos = idx_orig;
-		idx_orig++;
-	}
-
-	for_each_sym_by_demangled_name(e->patched, sym1->identity.demangled_name, sym2) {
-		if (sym2->twin || sym1->type != sym2->type || sym2->dont_correlate ||
-		    !maybe_same_file(sym1, sym2))
+		orig_entry = find_symtab_entry(&orig_table, patched_sym);
+		if (!orig_entry || !orig_entry->is_unique)
 			continue;
-		if (is_tu_local_sym(sym1) != is_tu_local_sym(sym2) ||
-		    is_llvm_sym(sym1) != is_llvm_sym(sym2))
+		orig_sym = orig_entry->sym;
+		if (reference_twin(orig_sym))
 			continue;
-		if (idx_patched == sym1_pos)
-			match = sym2;
-		idx_patched++;
-	}
-
-	if (idx_orig != idx_patched)
-		return NULL;
-
-	dbg_correlate("find_twin_positional(): %s%s -> %s%s",
-	    sym1->name, is_func_sym(sym1) ? "()" : "",
-	    match->name, is_func_sym(match) ? "()" : "");
-
-	return match;
+		if (strcmp(orig_sym->name, patched_sym->name) ||
+		    orig_sym->bind != patched_sym->bind ||
+		    !is_twin_candidate(orig_sym, patched_sym))
+			continue;
+		if (has_dwarf_identity(orig_sym) != has_dwarf_identity(patched_sym)) {
+			WARN("unmatched symbol %s (FILE %s): DWARF identity is missing on the %s side",
+			     patched_sym->name,
+			     *patched_entry->file ? patched_entry->file : "<none>",
+			     has_dwarf_identity(orig_sym) ? "patched" : "original");
+			continue;
+		}
+		if (!known_symtab_fallback(orig_sym) || !known_symtab_fallback(patched_sym))
+			WARN("ELF FILE/name fallback outside known rules: %s (FILE %s, binding %u, DWARF %s), orig[%u] -> patched[%u]",
+			     patched_sym->name,
+			     *patched_entry->file ? patched_entry->file : "<none>",
+			     patched_sym->bind, has_dwarf_identity(patched_sym) ? "yes" : "no",
+			     orig_sym->idx, patched_sym->idx);
+		orig_sym->twin = patched_sym;
+		patched_sym->twin = orig_sym;
+		dbg_correlate("%s(): %s (FILE %s)", __func__,
+			      patched_sym->name, patched_entry->file);
+	}
+	ret = 0;
+out:
+	free_symtab_hash(&orig_table);
+	free_symtab_hash(&patched_table);
+	return ret;
 }
 
 /*
@@ -850,96 +833,61 @@ static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1)
  */
 static int correlate_symbols(struct elfs *e)
 {
-	struct symbol *file1_sym, *file2_sym;
-	struct symbol *sym1, *sym2;
-	bool progress;
-
-	for_each_sym(e->orig, sym1)
-		sym1->dont_correlate = dont_correlate(sym1);
-	for_each_sym(e->patched, sym2)
-		sym2->dont_correlate = dont_correlate(sym2);
-
-	/* Correlate FILE symbols */
-	file1_sym = first_file_symbol(e->orig);
-	file2_sym = first_file_symbol(e->patched);
-
-	for (; ; file1_sym = next_file_symbol(e->orig, file1_sym),
-		 file2_sym = next_file_symbol(e->patched, file2_sym)) {
-
-		if (!file1_sym && file2_sym) {
-			ERROR("FILE symbol mismatch: NULL != %s", file2_sym->name);
-			return -1;
-		}
+	struct symbol *sym;
 
-		if (file1_sym && !file2_sym) {
-			ERROR("FILE symbol mismatch: %s != NULL", file1_sym->name);
-			return -1;
-		}
+	for_each_sym(e->orig, sym)
+		sym->dont_correlate = dont_correlate(sym);
+	for_each_sym(e->patched, sym)
+		sym->dont_correlate = dont_correlate(sym);
 
-		if (!file1_sym)
-			break;
+	if (prepare_identities(e->orig) || prepare_identities(e->patched))
+		return -1;
 
-		if (strcmp(file1_sym->name, file2_sym->name)) {
-			ERROR("FILE symbol mismatch: %s != %s", file1_sym->name, file2_sym->name);
-			return -1;
-		}
+	if (correlate_by_identity(e) ||
+	    extend_same_twins_by_checksum(e) ||
+	    validate_symbol_twins(e))
+		return -1;
 
-		file1_sym->twin = file2_sym;
-		file2_sym->twin = file1_sym;
+	for_each_sym(e->orig, sym) {
+		if (!sym->len || is_compound_constant(sym))
+			continue;
+		if (!reference_twin(sym) && !sym->dont_correlate)
+			WARN("no correlation: %s", sym->name);
 	}
 
+	return 0;
+}
 
-	/*
-	 * Correlate in two phases: loop deterministic levels until no more
-	 * progress, then use positional fallback for the rest.  This prevents
-	 * the nondeterministic positional matching from stealing symbols that
-	 * have deterministic matches.
-	 */
-	hash_init(suffix_map);
-	do {
-		progress = false;
-		for_each_sym(e->orig, sym1) {
-			if (sym1->twin || sym1->dont_correlate)
-				continue;
-			sym2 = find_twin(e, sym1);
-			if (!sym2)
-				continue;
-			sym1->twin = sym2;
-			sym2->twin = sym1;
-			progress = true;
-		}
+static int check_linkage_changes(struct elfs *e)
+{
+	struct symbol *orig_sym, *patched_sym;
 
-		if (update_suffix_map(e->orig))
-			return -1;
+	for_each_sym(e->orig, orig_sym) {
+		if (orig_sym->twin || orig_sym->dont_correlate ||
+		    !has_dwarf_identity(orig_sym) || is_undef_sym(orig_sym) || !orig_sym->len)
+			continue;
 
-		for_each_sym(e->orig, sym1) {
-			if (sym1->twin || sym1->dont_correlate)
+		/*
+		 * Identities distinguish linkage changes, but later name-based
+		 * resolution can bind references to the wrong state. Reject them.
+		 */
+		for_each_sym_by_demangled_name(e->patched, orig_sym->identity.demangled_name,
+					       patched_sym) {
+			if (patched_sym->twin || patched_sym->dont_correlate ||
+			    !has_dwarf_identity(patched_sym) ||
+			    is_undef_sym(patched_sym) || !patched_sym->len)
 				continue;
-			sym2 = find_twin_suffixed(e->patched, sym1);
-			if (!sym2)
+			if (orig_sym->type != patched_sym->type ||
+			    strcmp(orig_sym->identity.scope, patched_sym->identity.scope) ||
+			    orig_sym->identity.external == patched_sym->identity.external)
 				continue;

tools/objtool/klp-diff.c static struct symbol *find_twin_positional(struct elfs *e, struct symbol *sym1) · line 925

Quoted 10 lines
tools/objtool/klp-diff.c +5 -4
-			sym1->twin = sym2;
-			sym2->twin = sym1;
-			progress = true;
+			ERROR("unsupported linkage change (%s, %s): original %s symtab[%u] (external=%d), patched %s symtab[%u] (external=%d)",
+			      orig_sym->identity.scope, orig_sym->identity.demangled_name,
+			      orig_sym->name, orig_sym->idx, orig_sym->identity.external,
+			      patched_sym->name, patched_sym->idx, patched_sym->identity.external);
+			return -1;
 		}
-	} while (progress);

[Severity: Medium]
Does check_linkage_changes() need to verify that orig_sym and patched_sym come from the same translation unit before reporting an unsupported linkage change?

In tools/objtool/klp-dwarf.c (get_die_identity() and needs_identity_source()), global and weak external definitions have identity.source set to "" and identity.scope set to "/". File-scope static definitions also have identity.scope set to "/" (with identity.external == false).

In multi-TU objects such as vmlinux.o or multi-file modules, if one translation unit has an uncorrelated file-scope static symbol (for example, a static helper that was inlined or removed by the patch) and a different translation unit has an uncorrelated global symbol with the same demangled name and type (for example, a newly added global function), both have identity.scope == "/" with opposite identity.external values, causing check_linkage_changes() to abort with an unsupported linkage change error across unrelated translation units.

tools/objtool/klp-diff.c static int mark_changed_functions(struct elfs *e) · line 1065

Quoted 10 lines
tools/objtool/klp-diff.c +2 -1
@@ -1110,7 +1058,8 @@ static int mark_changed_functions(struct elfs *e)
 
 	/* Find added functions and print them */
 	for_each_sym(e->patched, patched_sym) {
-		if (!is_func_sym(patched_sym) || patched_sym->dont_correlate)
+		if (!is_func_sym(patched_sym) || patched_sym->dont_correlate ||
+		    is_undef_sym(patched_sym))
 			continue;
 
 		if (!patched_sym->twin) {

[Severity: High]
When an existing function remains uncorrelated (patched_sym->twin == NULL) because of ambiguity or a missing DWARF identity on one side, doesn't mark_changed_functions() misclassify it as a newly added function?

For STT_FUNC symbols, multiple compiler clones of the same function in a translation unit (such as foo.isra.0 and foo.isra.1, or foo.part.0 and foo.part.1) have distinct ELF symbol names (so validate_dwarf_identity() permits them) but share the same DWARF identity and match compatible_symbol_name(). Likewise, functions missing a DWARF identity on one side are skipped with only a warning in validate_symbol_twins().

When such a function is modified (or when two clones have identical checksums and are not disambiguated by an unchanged caller), patched_sym->twin remains NULL. In mark_changed_functions(), the loop over e->patched treats any !patched_sym->twin function as a newly added function (setting included = 1 while leaving changed = 0), so create_klp_sections() does not emit a klp_func_ext livepatch entry for the modified function and the original function in the running kernel remains unpatched. (The follow-up commit "objtool/klp: Check unmatched data references" only checks is_object_sym(patched_sym) in check_unmatched_reference() and does not check uncorrelated STT_FUNC symbols.)

Quoted 17 lines
tools/objtool/tests/generic/test-thinlto-ambiguity.sh +1 -1
index 34f4f3a58e201..4e0e49a9fc567 100755
@@ -38,7 +38,7 @@ build_thinlto()		# $1 output object, $2 extra flags
 {
 	local t
 	for t in "" -DTU_B -DTU_C; do
-		$THIN_CC -flto=thin -O2 -ffunction-sections -fdata-sections \
+		$THIN_CC -flto=thin -O2 -g -ffunction-sections -fdata-sections \
 			$2 $t -c "$FIXTURES_DIR/thinlto_ambiguity.c" \
 			-o "$workdir/tu$t.o" 2>/dev/null || return 1
 	done
tools/objtool/tests/lib.sh +0 -0
index 8e1aa7614dc68..44ce670c4a6c3 100644

[ ... ]

tools/objtool/tests/lib.sh EOF · line 157

Quoted 9 lines
tools/objtool/tests/lib.sh +1 -1
@@ -150,7 +151,7 @@ EOF
 #					checksum
 #
 # A test overrides it; see tools/objtool/Documentation/klp-write-tests.txt.
-FIXTURE_CFLAGS="-O2 -ffunction-sections -fdata-sections -fno-common \
+FIXTURE_CFLAGS="-O2 -g -ffunction-sections -fdata-sections -fno-common \
 		-fno-asynchronous-unwind-tables"
 
 test_name="$(basename "$0" .sh)"

[Severity: Medium]
As noted in the commit message, test-local-to-global-flip.sh, test-sympos.sh, and test-sympos-vmlinux.sh now fail when running the objtool test suite.

For test-sympos.sh and test-sympos-vmlinux.sh, those tests exercise klp_find_sympos() and .klp.symid disambiguation for duplicate local symbols across translation units (which is still used in clone_reloc_klp() and create_klp_sections()), and only fail because the test scripts simulate two translation units by compiling the same fixture .c file twice with -g, giving both objects the same DWARF CU path. Similarly, test-thinlto-ambiguity.sh compiles thinlto_ambiguity.c three times with -g, giving both promoted helper.llvm.<hash> functions the same DWARF CU path instead of distinct per-TU paths, so at this commit the changed helper fails correlation and is emitted as a new function with no klp_func_ext entry.

Should the multi-TU test scripts compile distinct source paths (or override -fdebug-prefix-map) so sympos and ThinLTO multi-TU disambiguation remain tested, and should test-local-to-global-flip.sh be updated to test the new linkage-change rejection so the test suite passes cleanly?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
#15 lore raw

RFC PATCH v2 8/11 objtool/klp: Include symbol identities in reference checksums

In reply to Longjun Luo

Hashing only the target's suffix-stripped name can hide a reference change between different objects with the same name.

Prepare DWARF identities for checksum calculation and hash the identity path for named references. Keep the existing checksum section format and use suffix-stripped ELF names when no DWARF identity is available.

The three tests rejected by policy in earlier patches remain rejected.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM
---
 tools/objtool/klp-checksum.c | 31 ++++++++++++++++++++++---------
 1 file changed, 22 insertions(+), 9 deletions(-)
tools/objtool/klp-checksum.c +22 -9
index 1a5f2fa89baa..44c0eb784e57 100644
@@ -77,6 +77,12 @@ static bool is_const_reference(struct symbol *sym)
 static int checksum_object(struct objtool_file *file, struct symbol *sym,
 			   unsigned int depth);
 
+/* Use suffix-stripped ELF names for references without DWARF identities. */
+static const char *checksum_name(struct symbol *sym)
+{
+	return sym->identity.path ?: sym->identity.demangled_name;
+}
+
 static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 				struct instruction *insn)
 {
@@ -110,8 +116,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 			__checksum_update_insn(func, insn, buf, len);
 
 			if (call_dest) {
-				__checksum_update_insn(func, insn, call_dest->identity.demangled_name,
-						       strlen(call_dest->identity.demangled_name));
+				__checksum_update_insn(func, insn, checksum_name(call_dest),
+						       strlen(checksum_name(call_dest)));
 
 			} else if (jump_dest) {
 				struct symbol *dest_sym;
@@ -127,8 +133,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 				if (!dest_sym)
 					goto alts;
 
-				__checksum_update_insn(func, insn, dest_sym->identity.demangled_name,
-						       strlen(dest_sym->identity.demangled_name));
+				__checksum_update_insn(func, insn, checksum_name(dest_sym),
+						       strlen(checksum_name(dest_sym)));
 
 				offset = jump_dest->offset - dest_sym->offset;
 				__checksum_update_insn(func, insn, &offset, sizeof(offset));
@@ -170,8 +176,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 		__checksum_update_insn(func, insn, &sym->csum.checksum,
 				       sizeof(sym->csum.checksum));
 	} else {
-		__checksum_update_insn(func, insn, sym->identity.demangled_name,
-				       strlen(sym->identity.demangled_name));
+		__checksum_update_insn(func, insn, checksum_name(sym),
+				       strlen(checksum_name(sym)));
 	}
 	__checksum_update_insn(func, insn, &offset, sizeof(offset));
 
@@ -265,9 +271,9 @@ static int checksum_update_object(struct objtool_file *file, struct symbol *sym,
 						 &target->csum.checksum,
 						 sizeof(target->csum.checksum));
 		} else {
-			__checksum_update_object(sym, sym_offset, "reloc name",
-						 target->identity.demangled_name,
-						 strlen(target->identity.demangled_name));
+			__checksum_update_object(sym, sym_offset, "reloc identity",
+						 checksum_name(target),
+						 strlen(checksum_name(target)));
 		}
 		__checksum_update_object(sym, sym_offset, "reloc addend",
 					 &offset, sizeof(offset));
@@ -387,6 +393,7 @@ static const char * const klp_checksum_usage[] = {
 int cmd_klp_checksum(int argc, const char **argv)
 {
 	struct objtool_file *file;
+	struct symbol *sym;
 	int ret;
 
 	const struct option options[] = {
@@ -411,6 +418,12 @@ int cmd_klp_checksum(int argc, const char **argv)
 	if (ret)
 		goto out;
 
+	for_each_sym(file->elf, sym)
+		sym->dont_correlate = dont_correlate(sym);
+	ret = prepare_identities(file->elf);
+	if (ret)
+		goto out;
+
 	ret = calculate_checksums(file);
 	if (ret)
 		goto out;
Signature · 1 line
2.55.0
#16 lore raw

Re: [RFC PATCH v2 08/11] objtool/klp: Include symbol identities in reference checksums

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [Medium] objtool/klp: incomplete .lto_priv suffix handling in checksum_name() - [Medium] objtool/klp…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [Medium] objtool/klp: incomplete .lto_priv suffix handling in checksum_name()
- [Medium] objtool/klp: alias checksum mismatch in checksum_object() and klp-diff
- [Medium] objtool/klp: broken .rodata and cross-CU matching in fallback_by_name()
- [Medium] objtool/klp: missing alias and TU checks in check_linkage_changes() --

commit 4c8f17799f11215eb7f986c3b3afec4af75ec543
Author: Longjun Luo <[email protected]>

objtool/klp: Include symbol identities in reference checksums

This commit updates livepatch checksum calculation to prepare DWARF identities and hash symbol identity paths for named references instead of relying solely on suffix-stripped ELF names.

tools/objtool/klp-checksum.c static bool is_const_reference(struct symbol *sym) · line 84

Quoted 13 lines
tools/objtool/klp-checksum.c +5 -0
index 1a5f2fa89baa5..44c0eb784e570 100644
@@ -77,6 +77,12 @@ static bool is_const_reference(struct symbol *sym)
 static int checksum_object(struct objtool_file *file, struct symbol *sym,
 			   unsigned int depth);
 
+/* Use suffix-stripped ELF names for references without DWARF identities. */
+static const char *checksum_name(struct symbol *sym)
+{
+	return sym->identity.path ?: sym->identity.demangled_name;
+}

[Severity: Medium]
When building with GCC LTO, GCC appends .lto_priv.<num> suffixes to privatized symbols. In tools/objtool/klp-diff.c, same_compiler_name() calls skip_lto_priv() so that symbols whose .lto_priv.<num> suffix is added or removed between original and patched builds can still correlate:

tools/objtool/klp-diff.c:skip_lto_priv() {
	if (!strstarts(name, ".lto_priv.") || !isdigit((unsigned char)name[10]))
		return name;
	name += 10;
	while (isdigit((unsigned char)*name))
		name++;
	return name;
}

However, demangled_name_len() in tools/objtool/elf.c only strips .llvm.<hash> and trailing .<digits> suffixes:

tools/objtool/elf.c:demangled_name_len() {
	...
	while (len && isdigit(name[len - 1])) {
		p = name + len - 1;
		while (p > name && isdigit(*p))
			p--;

		if (p == name || *p != '.')
			break;
		len = p - name;
	}
	return len;
}

As a result, a symbol like foo.lto_priv.0 has sym->identity.demangled_name truncated to foo.lto_priv instead of foo, which also leaves .lto_priv in sym->identity.path. In addition, matches_name() in tools/objtool/klp-dwarf.c only skips .llvm. and .<digits>, rejecting .lto_priv.<num> when matching ELF names against DWARF DW_AT_name, and find_twin() in tools/objtool/klp-diff.c keys hash lookups and same_identity() on sym->identity.demangled_name before calling is_twin_candidate() -> compatible_symbol_name() -> same_compiler_name() -> skip_lto_priv().

If a referenced symbol gains or loses a .lto_priv.<num> suffix between builds, won't checksum_name() hash different strings and cause false checksum mismatches as well as failed symbol correlation?

tools/objtool/klp-checksum.c static int checksum_update_insn(struct objtool_file *file, struct symbol *func, · line 182

Quoted 10 lines
tools/objtool/klp-checksum.c +2 -2
@@ -170,8 +176,8 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
 		__checksum_update_insn(func, insn, &sym->csum.checksum,
 				       sizeof(sym->csum.checksum));
 	} else {
-		__checksum_update_insn(func, insn, sym->identity.demangled_name,
-				       strlen(sym->identity.demangled_name));
+		__checksum_update_insn(func, insn, checksum_name(sym),
+				       strlen(checksum_name(sym)));
 	}
 	__checksum_update_insn(func, insn, &offset, sizeof(offset));

[Severity: Medium]
In calculate_checksums(), symbols where is_alias_sym(sym) is true are skipped so that only the representative symbol sym->alias gets a checksum in .discard.sym_checksum.

However, in checksum_update_insn() and checksum_update_object(), compound constants are hashed by calling checksum_object() directly on sym without resolving sym->alias or checking whether the checksum was already computed:

tools/objtool/klp-checksum.c:checksum_update_insn() {
	...
	if (is_compound_constant(sym)) {
		if (checksum_object(file, sym, 1))
			return -1;
		__checksum_update_insn(func, insn, &sym->csum.checksum,
				       sizeof(sym->csum.checksum));
	}
}

And on the diff side in tools/objtool/klp-diff.c, read_sym_checksums() only populates csum.checksum on sym->alias (leaving sym->csum.checksum == 0 on aliases), whereas correlate_by_identity() pairs aliased static objects directly via orig_sym->twin = patched_sym, and mark_changed_functions() and find_twin_by_identity_and_checksum() inspect sym->csum.checksum directly rather than sym->alias->csum.checksum:

tools/objtool/klp-diff.c:mark_changed_functions() {
	...
		if (orig_sym->csum.checksum != patched_sym->csum.checksum) {
			if (!is_func_sym(orig_sym)) {
				ERROR("changed data: %s", orig_sym->name);
				return -1;
			}
}

Doesn't this emit duplicate .discard.sym_checksum entries for compound constant aliases, and cause mark_changed_functions() to falsely report changed data when constant merging or symbol table ordering changes which symbol is the representative alias?

tools/objtool/klp-checksum.c int cmd_klp_checksum(int argc, const char **argv) · line 429

Quoted 13 lines
tools/objtool/klp-checksum.c +6 -0
@@ -411,6 +418,12 @@ int cmd_klp_checksum(int argc, const char **argv)
 	if (ret)
 		goto out;
 
+	for_each_sym(file->elf, sym)
+		sym->dont_correlate = dont_correlate(sym);
+	ret = prepare_identities(file->elf);
+	if (ret)
+		goto out;
+
 	ret = calculate_checksums(file);
 	if (ret)
 		goto out;

[Severity: Medium]
Now that cmd_klp_checksum() calls prepare_identities(), get_identity_for_symbol() invokes fallback_by_name() in tools/objtool/klp-dwarf.c when no matching address DIE is found:

tools/objtool/klp-dwarf.c:fallback_by_name() {
	const char *name = sym->pfunc->identity.demangled_name;
	const char *file = sym->file ? basename(sym->file->name) : NULL;
	...
	elf_hash_for_each_possible(elf, dwarf_name, entry, name_hash, str_hash(name)) {
		if (entry->type != sym->type || strcmp(name, entry->name))
			continue;
		...
		if (!entry->cu->assembly && !candidate.external && file &&
		    strcmp(file, basename(candidate.source)) &&
		    !known_file_name_mismatch(file, basename(candidate.source)))
			continue;
}

Could this fail in two cases for .rodata symbols?

First, under Clang, function-local static const objects in .rodata have ELF names of the form <func>.<var>[.<suffix>], so sym->pfunc->identity.demangled_name is <func>.<var>, whereas dwarf_name is indexed by DW_AT_name (<var>), so strcmp(name, entry->name) never matches.

Second, for top-level .rodata symbols in multi-TU objects where two CUs share the same basename, fallback_by_name() does not exclude DIEs that already have concrete addresses (entry->nr_addresses > 0), skips the file check when candidate.external is true, and only compares basename(sym->file->name) against basename(candidate.source) rather than checking sym->file->identity.source. Can a .rodata symbol without a location attribute in one CU match a same-named DIE in another CU and cause complete_file_source() to fail with conflicting DWARF sources?

[Severity: Medium]
Also related to identity handling in tools/objtool/klp-diff.c, check_linkage_changes() checks orig_sym->twin and patched_sym->twin directly when scanning for linkage changes across symbols with the same demangled name:

tools/objtool/klp-diff.c:check_linkage_changes() {
	for_each_sym(e->orig, orig_sym) {
		if (orig_sym->twin || orig_sym->dont_correlate ||
		    !has_dwarf_identity(orig_sym) || is_undef_sym(orig_sym) || !orig_sym->len)
			continue;

		for_each_sym_by_demangled_name(e->patched, orig_sym->identity.demangled_name,
					       patched_sym) {
			if (patched_sym->twin || patched_sym->dont_correlate ||
			    !has_dwarf_identity(patched_sym) ||
			    is_undef_sym(patched_sym) || !patched_sym->len)
				continue;

			if (orig_sym->type != patched_sym->type ||
			    strcmp(orig_sym->identity.scope, patched_sym->identity.scope) ||
			    orig_sym->identity.external == patched_sym->identity.external)
				continue;

			ERROR("unsupported linkage change for %s",
			      orig_sym->identity.demangled_name);
			return -1;
		}
	}
}

Unlike correlate_symbols() and validate_symbol_twins(), which use reference_twin(sym) so data aliases whose representative symbol is correlated are treated as correlated, check_linkage_changes() checks sym->twin directly and does not check whether orig_sym and patched_sym belong to the same translation unit.

In a multi-TU object or an object with correlated data aliases, won't an uncorrelated static symbol in one TU matching the demangled name of an uncorrelated global symbol in a different TU (or an uncorrelated alias of a correlated symbol) trigger a false "unsupported linkage change" error?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#17 lore raw

RFC PATCH v2 9/11 objtool/klp: Extend twins through unchanged function references

In reply to Longjun Luo

Repeated statics and optimized variable pieces can share an identity and checksum, leaving them uncorrelated after identity matching.

For paired functions with equal checksums, compare their relocation entries and extend twins to the referenced symbols. Require matching reference layouts and compatible targets, and reject twin conflicts.

This also checks the correlation against the function checksum: equal checksums can hide a switch between instances sharing one identity, and a conflicting reference must stop patch generation.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM
---
 tools/objtool/klp-diff.c | 132 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 132 insertions(+)
tools/objtool/klp-diff.c +132 -0
index 468b8654b687..e8252e586260 100644
@@ -826,6 +826,8 @@ static int validate_symbol_twins(struct elfs *e)
 	return ret;
 }
 
+static int extend_twins_by_relocations(struct elfs *e);
+
 /*
  * Correlate symbols between the orig and patched objects.  This is a
  * prerequisite for detecting changed functions, as well as for properly
@@ -845,6 +847,7 @@ static int correlate_symbols(struct elfs *e)
 
 	if (correlate_by_identity(e) ||
 	    extend_same_twins_by_checksum(e) ||
+	    extend_twins_by_relocations(e) ||
 	    validate_symbol_twins(e))
 		return -1;
 
@@ -1294,6 +1297,135 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
 	return convert_reloc_secsym_to_sym(elf, reloc);
 }
 
+static int correlate_reloc(struct elfs *e, struct symbol *func,
+			   struct reloc *orig_reloc, struct reloc *patched_reloc)
+{
+	struct symbol *orig_sym, *patched_sym;
+	unsigned long offset = reloc_offset(orig_reloc) - func->offset;
+
+	if (offset != reloc_offset(patched_reloc) - func->twin->offset ||
+	    reloc_type(orig_reloc) != reloc_type(patched_reloc)) {
+		ERROR("%s+%#lx: relocation mismatch in unchanged function", func->name, offset);
+		return -1;
+	}
+
+	if (reloc_type(orig_reloc) == R_NONE ||
+	    is_uncorrelated_section(orig_reloc->sym->sec) ||
+	    is_uncorrelated_section(patched_reloc->sym->sec))
+		return 0;
+
+	/* Section symbols have no twins; resolve the target symbol first. */
+	if (convert_reloc_secsym_to_sym(e->orig, orig_reloc) ||
+	    convert_reloc_secsym_to_sym(e->patched, patched_reloc))
+		return 0;
+
+	orig_sym = orig_reloc->sym;
+	patched_sym = patched_reloc->sym;
+	if (orig_sym->dont_correlate || patched_sym->dont_correlate)
+		return 0;
+
+	if (reloc_addend(orig_reloc) != reloc_addend(patched_reloc)) {
+		ERROR("%s+%#lx: reference offset mismatch: %s[%u]%+lld and %s[%u]%+lld",
+		      func->name, offset,
+		      orig_sym->name, orig_sym->idx, (long long)reloc_addend(orig_reloc),
+		      patched_sym->name, patched_sym->idx, (long long)reloc_addend(patched_reloc));
+		return -1;
+	}
+
+	if (orig_sym->twin == patched_sym && patched_sym->twin == orig_sym)
+		return 0;
+
+	if (orig_sym->twin || patched_sym->twin) {
+		ERROR("%s+%#lx: twin conflict: orig %s[%u]->%s[%u], patched %s[%u]->%s[%u]",
+		      func->name, offset,
+		      orig_sym->name, orig_sym->idx,
+		      orig_sym->twin ? orig_sym->twin->name : "<none>",
+		      orig_sym->twin ? orig_sym->twin->idx : 0,
+		      patched_sym->name, patched_sym->idx,
+		      patched_sym->twin ? patched_sym->twin->name : "<none>",
+		      patched_sym->twin ? patched_sym->twin->idx : 0);
+		return -1;
+	}
+
+	/* Compound target checksums are independent of their defining TU. */
+	if (is_compound_constant(orig_sym) && is_compound_constant(patched_sym)) {
+		if (!orig_sym->csum.checksum || orig_sym->len != patched_sym->len ||
+		    orig_sym->csum.checksum != patched_sym->csum.checksum) {
+			ERROR("%s+%#lx: compound content mismatch in unchanged function",
+			      func->name, offset);
+			return -1;
+		}
+	} else if (!is_twin_candidate(orig_sym, patched_sym)) {
+		ERROR("%s+%#lx: incompatible reference targets: %s[%u] and %s[%u]",
+		      func->name, offset, orig_sym->name, orig_sym->idx,
+		      patched_sym->name, patched_sym->idx);
+		return -1;
+	}
+
+	orig_sym->twin = patched_sym;
+	patched_sym->twin = orig_sym;
+	dbg_correlate("%s(): %s+%#lx: %s[%u] -> %s[%u]",
+		      __func__, func->name, offset,
+		      orig_sym->name, orig_sym->idx, patched_sym->name, patched_sym->idx);
+	return 0;
+}
+
+static struct reloc *get_func_relocs(struct elf *elf, struct symbol *func, unsigned int *nr)
+{
+	struct reloc *first, *reloc;
+
+	first = find_reloc_by_dest_range(elf, func->sec, func->offset, func->len);
+	*nr = 0;
+	for (reloc = first; reloc && reloc_offset(reloc) < func->offset + func->len;
+	     reloc = rsec_next_reloc(func->sec->rsec, reloc))
+		(*nr)++;
+	return first;
+}
+
+/* Use unchanged functions' references to resolve ambiguous symbol identities. */
+static int extend_twin_by_relocations(struct elfs *e, struct symbol *func)
+{
+	struct reloc *orig_relocs, *patched_relocs;
+	unsigned int nr_orig, nr_patched, i;
+
+	orig_relocs = get_func_relocs(e->orig, func, &nr_orig);
+	patched_relocs = get_func_relocs(e->patched, func->twin, &nr_patched);
+	if (nr_orig != nr_patched) {
+		ERROR("%s: relocation count mismatch in unchanged function: %u != %u",
+		      func->name, nr_orig, nr_patched);
+		return -1;
+	}
+
+	for (i = 0; i < nr_orig; i++)
+		if (correlate_reloc(e, func, &orig_relocs[i], &patched_relocs[i]))
+			return -1;
+	return 0;
+}
+
+static bool same_function_checksum(struct symbol *func)
+{
+	/* Cold subfunctions have no separate checksum; use their parent's. */
+	struct symbol *parent = func->pfunc;
+	struct symbol *parent_twin = func->twin->pfunc;
+
+	return parent->twin == parent_twin && parent->csum.checksum &&
+	       parent->csum.checksum == parent_twin->csum.checksum;
+}
+
+static int extend_twins_by_relocations(struct elfs *e)
+{
+	struct symbol *sym;
+
+	for_each_sym(e->orig, sym) {
+		if (!is_func_sym(sym) || sym->dont_correlate || !sym->twin ||
+		    !same_function_checksum(sym))
+			continue;
+		if (extend_twin_by_relocations(e, sym))
+			return -1;
+	}
+	return 0;
+}
+
 /*
  * Check if the original module already has a dependency on dep_mod, i.e. it
  * already references at least one export from that module.
Signature · 1 line
2.55.0
#18 lore raw

Re: [RFC PATCH v2 09/11] objtool/klp: Extend twins through unchanged function references

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider: - [Medium] objtool/klp: single pass in extend_twins_by_relocations() misses functions - [Medium]…

Thank you for your contribution! Sashiko AI review found 6 potential issue(s) to consider:
- [Medium] objtool/klp: single pass in extend_twins_by_relocations() misses functions
- [Medium] objtool/klp: correlate_reloc() pairs SHN_UNDEF symbols without undef checks
- [Medium] objtool/klp: logical OR guards in correlate_reloc() hide target mismatches
- [Medium] objtool/klp: section-symbol lookup mismatch in correlate_reloc() vs checksum
- [Medium] objtool/klp: same_function_checksum() ignores function alias representative
- [Medium] objtool/klp: correlate_reloc() does not canonicalize data alias symbols --

commit 1871820aa5fdf039e6c898d41596c9459d2e3e27
Author: Longjun Luo <[email protected]>

objtool/klp: Extend twins through unchanged function references

This commit extends symbol twin correlation in objtool klp diff by comparing relocation entries of paired functions that share equal checksums and correlating their referenced target symbols.

Quoted 4 lines
tools/objtool/klp-diff.c +0 -0
index 468b8654b6871..e8252e586260d 100644

[ ... ]

Quoted 15 lines
tools/objtool/klp-diff.c +11 -0
@@ -1294,6 +1297,135 @@ static int convert_reloc_sym(struct elf *elf, struct reloc *reloc)
 	return convert_reloc_secsym_to_sym(elf, reloc);
 }
 
+static int correlate_reloc(struct elfs *e, struct symbol *func,
+			   struct reloc *orig_reloc, struct reloc *patched_reloc)
+{
+	struct symbol *orig_sym, *patched_sym;
+	unsigned long offset = reloc_offset(orig_reloc) - func->offset;
+
+	if (offset != reloc_offset(patched_reloc) - func->twin->offset ||
+	    reloc_type(orig_reloc) != reloc_type(patched_reloc)) {
+		ERROR("%s+%#lx: relocation mismatch in unchanged function", func->name, offset);
+		return -1;
+	}

tools/objtool/klp-diff.c static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) · line 1320

Quoted 10 lines
tools/objtool/klp-diff.c +10 -0
+
+	if (reloc_type(orig_reloc) == R_NONE ||
+	    is_uncorrelated_section(orig_reloc->sym->sec) ||
+	    is_uncorrelated_section(patched_reloc->sym->sec))
+		return 0;
+
+	/* Section symbols have no twins; resolve the target symbol first. */
+	if (convert_reloc_secsym_to_sym(e->orig, orig_reloc) ||
+	    convert_reloc_secsym_to_sym(e->patched, patched_reloc))
+		return 0;

[Severity: Medium]
Does convert_reloc_secsym_to_sym() resolve one-past-the-end section-symbol references differently from checksum_update_insn() in tools/objtool/klp-checksum.c?

In tools/objtool/klp-checksum.c:checksum_update_insn(), section-symbol relocations are looked up with find_symbol_containing():

	if (is_sec_sym(sym)) {
		sym = find_symbol_containing(reloc->sym->sec, offset);
		if (!sym)
			goto alts;

		offset -= sym->offset;
	}

For a one-past-the-end reference such as arr + ARRAY_SIZE(arr) at sym->offset + sym->len, find_symbol_containing() returns NULL and skips hashing both the target symbol identity and the offset.

When extend_twins_by_relocations() subsequently treats the function as unchanged, convert_reloc_secsym_to_sym() resolves sym->offset + sym->len via find_symbol_by_offset(sec, 0) or find_symbol_containing_inclusive(). If the referenced array's size or target changed, won't correlate_reloc() abort patch generation with a reference offset or target mismatch error instead of treating the function as changed?

tools/objtool/klp-diff.c static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) · line 1325

Quoted 5 lines
tools/objtool/klp-diff.c +5 -0
+
+	orig_sym = orig_reloc->sym;
+	patched_sym = patched_reloc->sym;
+	if (orig_sym->dont_correlate || patched_sym->dont_correlate)
+		return 0;

[Severity: Medium]
Can these three logical OR guards in correlate_reloc() silently hide asymmetric relocation targets and conversion errors?

First, if only one side of the relocation pair targets an uncorrelated section or has dont_correlate set (for example, if a .rodata section-symbol relocation resolves via find_symbol_containing_inclusive() to a named STT_OBJECT symbol with dont_correlate == false on one side, while remaining an unresolved .rodata section symbol with dont_correlate == true on the other side), returning 0 skips validation and leaves the correlatable symbol unpaired.

Second, using || in:

	if (convert_reloc_secsym_to_sym(e->orig, orig_reloc) ||
	    convert_reloc_secsym_to_sym(e->patched, patched_reloc))
		return 0;

short-circuits converting patched_reloc when orig_reloc returns non-zero, ignores cases where only one side fails to resolve a section symbol, and turns a -1 error return from convert_reloc_secsym_to_sym() into 0.

[Severity: Medium]
Also in correlate_reloc(), what happens when orig_sym or patched_sym is an SHN_UNDEF symbol?

Unlike correlate_by_identity(), extend_same_twins_by_checksum(), and validate_symbol_twins(), correlate_reloc() does not check is_undef_sym(orig_sym) or is_undef_sym(patched_sym), and dont_correlate is false for SHN_UNDEF symbols. In addition, is_twin_candidate() does not reject SHN_UNDEF symbols or mismatches in is_undef_sym():

	/* Don't match zero-length labels with sized symbols. */
	if (!is_undef_sym(sym1) && !is_undef_sym(sym2) &&
	    (!sym1->len != !sym2->len))
		return false;

If an unchanged function references an external SHN_UNDEF symbol and correlate_reloc() sets orig_sym->twin = patched_sym and patched_sym->twin = orig_sym:

1. In klp_reloc_needed(), reference_twin(patched_sym) becomes non-NULL for an unexported undefined symbol, causing klp_reloc_needed() to return true as if the symbol were defined in e->orig, which then fails in clone_reloc_klp() / find_vmlinux_sympos() or emits a bogus KLP relocation.

2. In clone_reloc_klp(), having orig_sym non-NULL for a weak SHN_UNDEF symbol exported by another module skips the !orig_sym check that enforces has_module_dep().

3. If a symbol was SHN_UNDEF on one side and defined on the other, is_twin_candidate() can pair the undefined symbol (checksum 0) with the defined symbol (non-zero checksum), causing mark_changed_functions() to report a false changed data error.

Should correlate_reloc() and is_twin_candidate() reject SHN_UNDEF symbols?

Quoted 31 lines
tools/objtool/klp-diff.c +31 -0
+
+	if (reloc_addend(orig_reloc) != reloc_addend(patched_reloc)) {
+		ERROR("%s+%#lx: reference offset mismatch: %s[%u]%+lld and %s[%u]%+lld",
+		      func->name, offset,
+		      orig_sym->name, orig_sym->idx, (long long)reloc_addend(orig_reloc),
+		      patched_sym->name, patched_sym->idx, (long long)reloc_addend(patched_reloc));
+		return -1;
+	}
+
+	if (orig_sym->twin == patched_sym && patched_sym->twin == orig_sym)
+		return 0;
+
+	if (orig_sym->twin || patched_sym->twin) {
+		ERROR("%s+%#lx: twin conflict: orig %s[%u]->%s[%u], patched %s[%u]->%s[%u]",
+		      func->name, offset,
+		      orig_sym->name, orig_sym->idx,
+		      orig_sym->twin ? orig_sym->twin->name : "<none>",
+		      orig_sym->twin ? orig_sym->twin->idx : 0,
+		      patched_sym->name, patched_sym->idx,
+		      patched_sym->twin ? patched_sym->twin->name : "<none>",
+		      patched_sym->twin ? patched_sym->twin->idx : 0);
+		return -1;
+	}
+
+	/* Compound target checksums are independent of their defining TU. */
+	if (is_compound_constant(orig_sym) && is_compound_constant(patched_sym)) {
+		if (!orig_sym->csum.checksum || orig_sym->len != patched_sym->len ||
+		    orig_sym->csum.checksum != patched_sym->csum.checksum) {
+			ERROR("%s+%#lx: compound content mismatch in unchanged function",
+			      func->name, offset);
+			return -1;

tools/objtool/klp-diff.c static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) · line 1366

Quoted 10 lines
tools/objtool/klp-diff.c +10 -0
+		}
+	} else if (!is_twin_candidate(orig_sym, patched_sym)) {
+		ERROR("%s+%#lx: incompatible reference targets: %s[%u] and %s[%u]",
+		      func->name, offset, orig_sym->name, orig_sym->idx,
+		      patched_sym->name, patched_sym->idx);
+		return -1;
+	}
+
+	orig_sym->twin = patched_sym;
+	patched_sym->twin = orig_sym;

[Severity: Medium]
What happens in correlate_reloc() when a direct symbol relocation targets a non-representative data alias where is_alias_sym(sym) is true?

In tools/objtool/klp-checksum.c:calculate_checksums(), symbols with is_alias_sym(sym) are skipped, so only the representative symbol sym->alias has a non-zero csum.checksum, and reference_twin() expects shared twins for data objects to be stored on sym->alias:

static struct symbol *reference_twin(struct symbol *sym)

{
	if (!sym->twin && is_object_sym(sym))
		return sym->alias->twin;
	return sym->twin;
}

While convert_reloc_secsym_to_sym() resolves section symbols to sym->alias, it is a no-op when orig_reloc or patched_reloc is already a direct symbol relocation to a non-representative alias.

If correlate_reloc() operates on non-representative data aliases directly without canonicalizing STT_OBJECT symbols to sym->alias:

1. Pairing a non-representative alias (csum.checksum == 0) with a representative symbol (csum.checksum != 0) causes mark_changed_functions() to fail with a false changed data error.

2. Pairing two non-representative aliases leaves twin == NULL on both representative sym->alias symbols, so reference_twin(sym->alias) returns NULL and mark_changed_functions() compares 0 == 0 on the aliases instead of checking the data checksum on sym->alias.

3. If another relocation references the representative sym->alias, correlate_reloc() will see a false twin conflict.

[ ... ]

tools/objtool/klp-diff.c static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) · line 1413

Quoted 9 lines
tools/objtool/klp-diff.c +9 -0
+static bool same_function_checksum(struct symbol *func)
+{
+	/* Cold subfunctions have no separate checksum; use their parent's. */
+	struct symbol *parent = func->pfunc;
+	struct symbol *parent_twin = func->twin->pfunc;
+
+	return parent->twin == parent_twin && parent->csum.checksum &&
+	       parent->csum.checksum == parent_twin->csum.checksum;
+}

[Severity: Medium]
In same_function_checksum(), can parent->csum.checksum be 0 when a function has an alias at the same address?

In tools/objtool/elf.c:elf_add_symbol(), when two function symbols share the same section, offset, type, and length, the earlier symbol has sym->alias set to the later symbol while sym->pfunc remains sym. Because calculate_checksums() in tools/objtool/klp-checksum.c skips symbols where is_alias_sym(sym) is true:

	if (is_cold_func(sym) || is_alias_sym(sym) || !sym->len ||
	    !sym->sec || !sym->sec->data)
		continue;

the checksum is only stored on parent->alias->csum.checksum. Won't same_function_checksum() always evaluate to false for non-representative function aliases unless it reads the checksum from parent->alias and parent_twin->alias?

Quoted 4 lines
tools/objtool/klp-diff.c +4 -0
+
+static int extend_twins_by_relocations(struct elfs *e)
+{
+	struct symbol *sym;

tools/objtool/klp-diff.c static int convert_reloc_sym(struct elf *elf, struct reloc *reloc) · line 1427

Quoted 10 lines
tools/objtool/klp-diff.c +10 -0
+
+	for_each_sym(e->orig, sym) {
+		if (!is_func_sym(sym) || sym->dont_correlate || !sym->twin ||
+		    !same_function_checksum(sym))
+			continue;
+		if (extend_twin_by_relocations(e, sym))
+			return -1;
+	}
+	return 0;
+}

[Severity: Medium]
Can a single linear pass in extend_twins_by_relocations() miss functions and cold subfunctions whose twins are assigned later in the loop?

In ELF symbol tables, all STB_LOCAL symbols (including static helpers, compiler clones, and .cold subfunctions, which have csum.checksum == 0 and are not paired by extend_same_twins_by_checksum()) appear before STB_GLOBAL symbols, and static callees often precede their callers.

When for_each_sym(e->orig, sym) visits an unpaired local function or .cold subfunction first, !sym->twin (or !same_function_checksum(sym) if its parent was not yet paired) is true and the loop skips it. When a caller at a later symbol index is processed by extend_twin_by_relocations() -> correlate_reloc(), orig_sym->twin = patched_sym is assigned for that earlier function or .cold subfunction, but extend_twins_by_relocations() never revisits it to correlate its relocations or verify its references against its checksum.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#19 lore raw

RFC PATCH v2 10/11 objtool/klp: Check unmatched data references

In reply to Longjun Luo

An unmatched data reference can still refer to existing state. Treating it as new can give old and patched code separate copies of that state.

For ordinary data, allow new storage only when no original identity candidate exists. For compound literals, reuse a unique unchanged object in both functions' references. Reject ambiguous or changed compounds; without a source identity, additions cannot be distinguished from changes.

Report the referencing function and offset for unresolved ordinary data. Exclude fake special-section symbols created by objtool after correlation.

Validated the available objtool tests with GCC 16.2.1 and Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM
---
 tools/objtool/klp-diff.c | 134 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 134 insertions(+)
tools/objtool/klp-diff.c +134 -0
index e8252e586260..19f5161420cd 100644
@@ -1426,6 +1426,135 @@ static int extend_twins_by_relocations(struct elfs *e)
 	return 0;
 }
 
+static int find_unique_compound_reference(struct elf *elf, struct reloc *relocs,
+					  unsigned int nr, struct symbol *sym,
+					  struct symbol **match)
+{
+	unsigned int i;
+
+	*match = NULL;
+	for (i = 0; i < nr; i++) {
+		struct reloc *reloc = &relocs[i];
+		struct symbol *target;
+
+		if (reloc_type(reloc) == R_NONE || convert_reloc_secsym_to_sym(elf, reloc))
+			continue;
+		target = reloc->sym;
+		if (!is_compound_constant(target) || target->len != sym->len ||
+		    target->csum.checksum != sym->csum.checksum)
+			continue;
+		if (*match && *match != target) {
+			ERROR("unresolved compound reference: %s matches %s[%u] and %s[%u]",
+			      sym->name, (*match)->name, (*match)->idx, target->name, target->idx);
+			return -1;
+		}
+		*match = target;
+	}
+	return 0;
+}
+
+static int resolve_compound_reference(struct elfs *e, struct symbol *sym,
+				      struct reloc *orig_relocs, unsigned int nr_orig,
+				      struct reloc *patched_relocs, unsigned int nr_patched)
+{
+	struct symbol *orig_sym, *patched_sym;
+
+	if (!sym->csum.checksum) {
+		ERROR("%s: missing compound checksum", sym->name);
+		return -1;
+	}
+
+	if (find_unique_compound_reference(e->orig, orig_relocs, nr_orig, sym, &orig_sym))
+		return -1;
+	if (!orig_sym) {
+		/*
+		 * Data changes are unsupported. With no source identity, new and
+		 * changed compound literals cannot be distinguished here.
+		 */
+		ERROR("unresolved compound reference: %s has no unchanged match", sym->name);
+		return -1;
+	}
+
+	if (find_unique_compound_reference(e->patched, patched_relocs, nr_patched,
+					   sym, &patched_sym))
+		return -1;
+	if (patched_sym != sym) {
+		ERROR("unresolved compound reference: %s is not a unique reference in its function",
+		      sym->name);
+		return -1;
+	}
+	if (orig_sym->twin) {
+		ERROR("compound twin conflict: %s[%u] paired with %s[%u]",
+		      orig_sym->name, orig_sym->idx,
+		      orig_sym->twin->name, orig_sym->twin->idx);
+		return -1;
+	}
+	sym->twin = orig_sym;
+	orig_sym->twin = sym;
+	dbg_correlate("%s(): %s[%u] -> %s[%u]", __func__,
+		      orig_sym->name, orig_sym->idx, sym->name, sym->idx);
+	return 0;
+}
+
+/*
+ * Check references to symbols without twins for ambiguity:
+ *
+ * 1. Ordinary data: allow as new only if no original identity candidates exist.
+ *    We assume DWARF identity information is stable across builds with the
+ *    same compiler.
+ * 2. Compound literals (anonymous data with static storage duration):
+ *    - delete: allowed.
+ *    - same: require a unique unchanged match in both functions' references.
+ *    - change: unsupported, to keep old and patched code's data views consistent.
+ *    - new: reject for now; it cannot be distinguished from change.
+ *      Supporting additions requires an explicit mechanism.
+ */
+static int check_unmatched_reference(struct elfs *e, struct reloc *reloc)
+{
+	struct symbol *patched_sym = reloc->sym, *orig_sym, *func;
+	struct reloc *orig_relocs = NULL, *patched_relocs = NULL;
+	unsigned int nr_orig = 0, nr_patched = 0;
+	u32 key;
+
+	if (reference_twin(patched_sym) || patched_sym->dont_correlate ||
+	    !is_object_sym(patched_sym) || is_undef_sym(patched_sym) || !patched_sym->len)
+		return 0;
+
+	if (!is_compound_constant(patched_sym)) {
+		if (!has_dwarf_identity(patched_sym)) {
+			ERROR_FUNC(reloc->sec->base, reloc_offset(reloc),
+				   "unresolved data reference: %s has no DWARF identity",
+				   patched_sym->name);
+			return -1;
+		}
+		key = identity_hash(&patched_sym->identity);
+		/*
+		 * With one old count.N and two patched candidates, the unmatched one
+		 * may still refer to old state, even if the original already has a twin.
+		 */
+		elf_hash_for_each_possible(e->orig, symbol_identity, orig_sym, identity_hash, key) {
+			if (!same_identity(&patched_sym->identity, &orig_sym->identity))
+				continue;
+			ERROR_FUNC(reloc->sec->base, reloc_offset(reloc),
+				   "unresolved data reference: %s (%s, %s, %s), old candidate %s[%u]",
+				   patched_sym->name, patched_sym->identity.source,
+				   patched_sym->identity.scope,
+				   patched_sym->identity.demangled_name,
+				   orig_sym->name, orig_sym->idx);
+			return -1;
+		}
+		return 0;
+	}
+
+	func = find_func_containing(reloc->sec->base, reloc_offset(reloc));
+	if (func && func->included && func->twin && !same_function_checksum(func)) {
+		orig_relocs = get_func_relocs(e->orig, func->twin, &nr_orig);
+		patched_relocs = get_func_relocs(e->patched, func, &nr_patched);
+	}
+	return resolve_compound_reference(e, patched_sym, orig_relocs, nr_orig,
+					  patched_relocs, nr_patched);
+}
+
 /*
  * Check if the original module already has a dependency on dep_mod, i.e. it
  * already references at least one export from that module.
@@ -1629,6 +1758,9 @@ static int clone_reloc(struct elfs *e, struct reloc *patched_reloc,
 	bool klp;
 
 	/* Reject ambiguous data references before treating them as new. */
+	if (check_unmatched_reference(e, patched_reloc))
+		return -1;
+
 	klp = klp_reloc_needed(patched_reloc);
 
 	dbg_clone_reloc(sec, offset, patched_sym, addend, export, klp);
@@ -1781,6 +1913,8 @@ static int create_fake_symbol(struct elf *elf, struct section *sec,
 		return -1;
 
 	sym->fake = 1;
+	/* Special-section entries are copied with the patched code. */
+	sym->dont_correlate = 1;
 	return 0;
 }
 
Signature · 1 line
2.55.0
#20 lore raw

RFC PATCH v2 11/11 objtool/klp: Test symbol identity and state preservation

In reply to Longjun Luo

Exercise local state across source changes, inlining, compiler renaming and LTO. Check that patches reference the original objects and allocate only new state. Cover aliases, assembly, metadata, ICF, DWARF pieces, shrunk storage, reduced information and saved DWP files. Cover separate cold-fragment identities left by weak overrides.

Check rejection of ambiguous repeated statics, conflicting references and changed data. Complete compound-reference tests and add same-named target switches for the identity-based reference checksum.

Skip the saved-DWP test when the linked libdw lacks DWP support.

The full suite has 68 passes with GCC 16.2.1 and 75 with Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM
---
 .../tests/generic/fixtures/identity/alias.c   | 25 ++++++
 .../generic/fixtures/identity/block_data.c    | 22 ++++++
 .../generic/fixtures/identity/branch_hint.c   | 28 +++++++
 .../generic/fixtures/identity/cold_strong.c   | 13 +++
 .../generic/fixtures/identity/cold_user.c     | 15 ++++
 .../generic/fixtures/identity/cold_weak.c     | 10 +++
 .../generic/fixtures/identity/consumer.c      | 30 +++++++
 .../tests/generic/fixtures/identity/entries.c | 10 +++
 .../generic/fixtures/identity/full_left.c     | 16 ++++
 .../generic/fixtures/identity/full_right.c    |  6 ++
 .../generic/fixtures/identity/icf_left/same.c | 16 ++++
 .../fixtures/identity/icf_right/same.c        | 16 ++++
 .../fixtures/identity/inline_helpers.c        | 27 +++++++
 .../tests/generic/fixtures/identity/lookup.c  | 13 +++
 .../generic/fixtures/identity/metadata.c      | 24 ++++++
 .../generic/fixtures/identity/move_left.c     |  9 +++
 .../generic/fixtures/identity/move_right.c    |  8 ++
 .../generic/fixtures/identity/new/same.c      |  6 ++
 .../generic/fixtures/identity/new_driver.c    | 15 ++++
 .../generic/fixtures/identity/new_provider.c  |  4 +
 .../generic/fixtures/identity/old/same.c      |  6 ++
 .../tests/generic/fixtures/identity/once.h    | 16 ++++
 .../generic/fixtures/identity/once_inline.c   | 28 +++++++
 .../generic/fixtures/identity/once_insert.c   | 21 +++++
 .../tests/generic/fixtures/identity/pieces.c  | 28 +++++++
 .../fixtures/identity/promotion_bounds_a.c    | 12 +++
 .../fixtures/identity/promotion_bounds_b.c    | 12 +++
 .../fixtures/identity/promotion_bounds_d.c    | 12 +++
 .../generic/fixtures/identity/provider.c      |  9 +++
 .../generic/fixtures/identity/readonly.c      | 30 +++++++
 .../tests/generic/fixtures/identity/record.h  |  8 ++
 .../tests/generic/fixtures/identity/rx.c      |  4 +
 .../tests/generic/fixtures/identity/shrink.c  | 19 +++++
 .../generic/fixtures/identity/source_driver.c | 11 +++
 .../fixtures/identity/static_replace.c        | 29 +++++++
 .../generic/fixtures/identity/static_scope.c  | 31 ++++++++
 .../fixtures/identity/static_scope_reorder.c  | 31 ++++++++
 .../tests/generic/fixtures/identity/tx.c      |  4 +
 .../generic/test-checksum-identity-source.sh  | 24 ++++++
 .../tests/generic/test-checksum-identity.sh   | 32 ++++++++
 .../tests/generic/test-identity-alias.sh      | 26 ++++++
 .../tests/generic/test-identity-block-data.sh | 12 +++
 .../generic/test-identity-branch-hint.sh      | 18 +++++
 .../tests/generic/test-identity-compound.sh   | 24 +++++-
 .../tests/generic/test-identity-dwarf.sh      | 34 ++++++++
 .../tests/generic/test-identity-full-lto.sh   | 30 +++++++
 .../tests/generic/test-identity-icf.sh        | 23 ++++++
 .../generic/test-identity-inline-helpers.sh   | 25 ++++++
 .../generic/test-identity-inline-state.sh     | 21 +++++
 .../tests/generic/test-identity-metadata.sh   | 20 +++++
 .../tests/generic/test-identity-new-tu.sh     | 24 ++++++
 .../generic/test-identity-once-insert.sh      | 19 +++++
 .../tests/generic/test-identity-pieces.sh     | 27 +++++++
 .../generic/test-identity-readonly-address.sh | 23 ++++++
 .../tests/generic/test-identity-shrink.sh     | 19 +++++
 .../tests/generic/test-identity-source.sh     | 29 +++++++
 .../tests/generic/test-identity-split.sh      | 34 ++++++++
 .../generic/test-identity-static-replace.sh   | 20 +++++
 .../generic/test-identity-static-scope.sh     | 35 ++++++++
 .../test-identity-thinlto-name-drift.sh       | 79 +++++++++++++++++++
 .../test-identity-thinlto-promotion.sh        | 26 ++++++
 .../tests/generic/test-identity-weak-cold.sh  | 35 ++++++++
 .../tests/x86/fixtures/identity_assembly.S    | 33 ++++++++
 .../tests/x86/fixtures/warn_reference.c       | 36 +++++++++
 .../tests/x86/test-identity-assembly.sh       | 19 +++++
 .../objtool/tests/x86/test-warn-reference.sh  | 21 +++++
 66 files changed, 1390 insertions(+), 2 deletions(-)
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/alias.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/block_data.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/branch_hint.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_strong.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_user.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/cold_weak.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/consumer.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/entries.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/full_left.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/full_right.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/icf_left/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/icf_right/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/inline_helpers.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/lookup.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/metadata.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/move_left.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/move_right.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new_driver.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/new_provider.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/old/same.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once_inline.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/once_insert.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/pieces.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_a.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_b.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/promotion_bounds_d.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/provider.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/readonly.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/record.h
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/rx.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/shrink.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/source_driver.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_replace.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_scope.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/static_scope_reorder.c
 create mode 100644 tools/objtool/tests/generic/fixtures/identity/tx.c
 create mode 100755 tools/objtool/tests/generic/test-checksum-identity-source.sh
 create mode 100755 tools/objtool/tests/generic/test-checksum-identity.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-alias.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-block-data.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-branch-hint.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-dwarf.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-full-lto.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-icf.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-inline-helpers.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-inline-state.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-metadata.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-new-tu.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-once-insert.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-pieces.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-readonly-address.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-shrink.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-source.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-split.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-static-replace.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-static-scope.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-name-drift.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-thinlto-promotion.sh
 create mode 100755 tools/objtool/tests/generic/test-identity-weak-cold.sh
 create mode 100644 tools/objtool/tests/x86/fixtures/identity_assembly.S
 create mode 100644 tools/objtool/tests/x86/fixtures/warn_reference.c
 create mode 100755 tools/objtool/tests/x86/test-identity-assembly.sh
 create mode 100755 tools/objtool/tests/x86/test-warn-reference.sh
tools/objtool/tests/generic/fixtures/identity/alias.c new file +25 -0
new file mode 100644
index 000000000000..ab1f67d29d01
@@ -0,0 +1,25 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+#if defined(PATCHED) && defined(CHANGE_DATA)
+long state[2] = { 12, 22 };
+#else
+long state[2] = { 11, 22 };
+#endif
+extern long state_alias[2] __attribute__((alias("state")));
+
+NOIPA long *target(unsigned int index)
+{
+#ifdef PATCHED
+	if (index >= 2)
+		return (void *)0;
+#endif
+	return &state_alias[index];
+}
+
+NOIPA long *unchanged(void)
+{
+	return state;
+}
tools/objtool/tests/generic/fixtures/identity/block_data.c new file +22 -0
new file mode 100644
index 000000000000..bb64ba670d3e
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+NOIPA int target(int which)
+{
+	if (which) {
+		static volatile int budget = 5;
+
+		return --budget;
+	}
+	{
+#ifdef PATCHED
+		static volatile int budget = 10;
+#else
+		static volatile int budget = 5;
+#endif
+
+		return --budget;
+	}
+}
tools/objtool/tests/generic/fixtures/identity/branch_hint.c new file +28 -0
new file mode 100644
index 000000000000..a605426047cb
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+extern void consume(int value);
+
+#ifdef PATCHED
+#define FIRST_LIKELY 1
+#define SECOND_LIKELY 0
+#else
+#define FIRST_LIKELY 0
+#define SECOND_LIKELY 1
+#endif
+
+NOIPA void target(int first, int second)
+{
+	if (__builtin_expect(first, FIRST_LIKELY)) {
+		static volatile int count;
+
+		consume(count++);
+	}
+	consume(0);
+	if (__builtin_expect(second, SECOND_LIKELY)) {
+		static volatile int count;
+
+		consume(count++);
+	}
+}
tools/objtool/tests/generic/fixtures/identity/cold_strong.c new file +13 -0
new file mode 100644
index 000000000000..d2049c417499
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+extern void strong_slow_path(int value) __attribute__((cold));
+
+__attribute__((noinline)) int foo(int value)
+{
+#if defined(PATCHED) && defined(CHANGE_STRONG)
+	value++;
+#endif
+	if (__builtin_expect(value < 0, 0))
+		strong_slow_path(value);
+	return value + 2;
+}
tools/objtool/tests/generic/fixtures/identity/cold_user.c new file +15 -0
new file mode 100644
index 000000000000..c88d9aff1a97
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+int foo(int value);
+
+int user(int value)
+{
+#ifdef PATCHED
+	return foo(value) + 1;
+#else
+	return foo(value);
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/cold_weak.c new file +10 -0
new file mode 100644
index 000000000000..15981d530be5
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+extern void weak_slow_path(int value) __attribute__((cold));
+
+__attribute__((weak, noinline)) int foo(int value)
+{
+	if (__builtin_expect(value < 0, 0))
+		weak_slow_path(value);
+	return value + 1;
+}
tools/objtool/tests/generic/fixtures/identity/consumer.c new file +30 -0
new file mode 100644
index 000000000000..4aad0e941bc3
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+#ifdef PATCHED
+static NOIPA int helper(int uid) { return uid == 0 ? 0 : -1; }
+#else
+int helper(int uid);
+#endif
+#ifdef DATA
+#ifdef PATCHED
+static volatile int limit = 1500;
+#else
+extern volatile int limit;
+#endif
+#endif
+#ifdef TABLE
+struct operations { int (*check)(int); };
+struct operations ops = { .check = helper };
+#endif
+NOIPA int caller(int uid)
+{
+#ifdef DATA
+	return uid <= limit ? 0 : -90;
+#elif defined(TABLE)
+	return ops.check(uid);
+#else
+	return helper(uid);
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/entries.c new file +10 -0
new file mode 100644
index 000000000000..53e809957c1c
@@ -0,0 +1,10 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+unsigned int tx(unsigned int n);
+unsigned int rx(unsigned int n);
+unsigned int entry_tx(unsigned int n) { return tx(n); }
+#ifdef PATCHED
+unsigned int entry_rx(unsigned int n) { return rx(n); }
+#endif
tools/objtool/tests/generic/fixtures/identity/full_left.c new file +16 -0
new file mode 100644
index 000000000000..1a75df588712
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+static volatile int state;
+#ifdef PATCHED
+static NOIPA int helper(void) { return ++state; }
+#endif
+NOIPA int left_fn(void)
+{
+#ifdef PATCHED
+	return helper();
+#else
+	return ++state;
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/full_right.c new file +6 -0
new file mode 100644
index 000000000000..17583384cf32
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+static volatile int state;
+static NOIPA int helper(void) { return ++state; }
+NOIPA int right_fn(void) { return helper(); }
tools/objtool/tests/generic/fixtures/identity/icf_left/same.c new file +16 -0
new file mode 100644
index 000000000000..25312a0ee0c0
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+extern long process(long value);
+
+static long base(long value)
+{
+	return process(value) + process(value + 7);
+}
+
+static long helper(long value)
+{
+	return process(value) + process(value + 7);
+}
+
+long (*left_base)(long) = base;
+long (*left_helper)(long) = helper;
tools/objtool/tests/generic/fixtures/identity/icf_right/same.c new file +16 -0
new file mode 100644
index 000000000000..e4357fe8b475
@@ -0,0 +1,16 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+extern long process(long value);
+
+static long base(long value)
+{
+	return process(value) + process(value + 9);
+}
+
+static long helper(long value)
+{
+	return process(value) + process(value + 9);
+}
+
+long (*right_base)(long) = base;
+long (*right_helper)(long) = helper;
tools/objtool/tests/generic/fixtures/identity/inline_helpers.c new file +27 -0
new file mode 100644
index 000000000000..41ee7326f8a1
@@ -0,0 +1,27 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+#include "once.h"
+MODINFO;
+
+static volatile long rx_seed, tx_seed;
+
+static inline __attribute__((always_inline)) long initialize_rx(long value)
+{
+	ONCE_INIT(rx_seed, 1);
+	return value ^ rx_seed;
+}
+
+static inline __attribute__((always_inline)) long initialize_tx(long value)
+{
+	ONCE_INIT(tx_seed, 2);
+	return value ^ tx_seed;
+}
+
+NOIPA long target(long value)
+{
+#ifdef PATCHED
+	value = initialize_tx(value);
+#endif
+	return initialize_rx(value);
+}
tools/objtool/tests/generic/fixtures/identity/lookup.c new file +13 -0
new file mode 100644
index 000000000000..b72d8d01815d
@@ -0,0 +1,13 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+NOIPA long target(long value)
+{
+#ifdef PATCHED
+	return value + 2;
+#else
+	return value + 1;
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/metadata.c new file +24 -0
new file mode 100644
index 000000000000..53f6785b2593
@@ -0,0 +1,24 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+extern void *get_symbol(const char *name);
+
+#define SYMBOL_GET(name) ({ \
+	static const char __notrim[] __attribute__((used, section(".no_trim_symbol"))) = #name; \
+	get_symbol(#name); \
+})
+#define TRACEPOINT_CHECK(name) \
+	static const char __trace_check_##name[] \
+		__attribute__((used, section("__tracepoint_check"))) = #name
+
+NOIPA void *target(void)
+{
+	TRACEPOINT_CHECK(start);
+	SYMBOL_GET(first);
+#ifdef PATCHED
+	TRACEPOINT_CHECK(finish);
+	SYMBOL_GET(extra);
+#endif
+	return SYMBOL_GET(second);
+}
tools/objtool/tests/generic/fixtures/identity/move_left.c new file +9 -0
new file mode 100644
index 000000000000..7d3b03bc87e5
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+static NOIPA int helper(int n) { return n <= 1500 ? 0 : -90; }
+NOIPA int keep_left(int n) { return helper(n); }
+#ifndef PATCHED
+NOIPA int caller(int n) { return helper(n); }
+#endif
tools/objtool/tests/generic/fixtures/identity/move_right.c new file +8 -0
new file mode 100644
index 000000000000..9a5f82a7d9ba
@@ -0,0 +1,8 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+static NOIPA int helper(int n) { return n <= 4096 ? 0 : -90; }
+NOIPA int keep_right(int n) { return helper(n); }
+#ifdef PATCHED
+NOIPA int caller(int n) { return helper(n); }
+#endif
tools/objtool/tests/generic/fixtures/identity/new/same.c new file +6 -0
new file mode 100644
index 000000000000..54550f20fe79
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "../common.h"
+static volatile int bytes;
+static NOIPA int helper(int n) { return bytes += n; }
+NOIPA int account_new(int n) { return helper(n); }
tools/objtool/tests/generic/fixtures/identity/new_driver.c new file +15 -0
new file mode 100644
index 000000000000..c06a1ac27609
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+#ifdef PATCHED
+int clamp(int value);
+#endif
+NOIPA int target(int value)
+{
+#ifdef PATCHED
+	return clamp(value);
+#else
+	return value;
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/new_provider.c new file +4 -0
new file mode 100644
index 000000000000..69c1b3d0b6a7
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+NOIPA int clamp(int value) { return value < 0 ? 0 : value; }
tools/objtool/tests/generic/fixtures/identity/old/same.c new file +6 -0
new file mode 100644
index 000000000000..e03f14f16214
@@ -0,0 +1,6 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "../common.h"
+static volatile int bytes;
+static NOIPA int helper(int n) { return bytes += n; }
+NOIPA int account_old(int n) { return helper(n); }
tools/objtool/tests/generic/fixtures/identity/once.h new file +16 -0
new file mode 100644
index 000000000000..c31b2911ce3e
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#ifndef KLP_TEST_ONCE_H
+#define KLP_TEST_ONCE_H
+
+extern long generate(long seed);
+
+#define ONCE_INIT(state, seed) do { \
+	static volatile int done; \
+	if (!done) { \
+		done = 1; \
+		(state) = generate(seed); \
+	} \
+} while (0)
+
+#endif
tools/objtool/tests/generic/fixtures/identity/once_inline.c new file +28 -0
new file mode 100644
index 000000000000..607f81ca4dd1
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+#include "once.h"
+MODINFO;
+
+static volatile long first, second;
+
+static inline __attribute__((always_inline)) long initialize(long value)
+{
+	ONCE_INIT(first, 1);
+	ONCE_INIT(second, 2);
+	return value ^ first ^ second;
+}
+
+NOIPA long unchanged(long value)
+{
+	return initialize(value);
+}
+
+NOIPA long target(long value)
+{
+#ifdef PATCHED
+	return initialize(value + 1);
+#else
+	return initialize(value);
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/once_insert.c new file +21 -0
new file mode 100644
index 000000000000..62f4215210ae
@@ -0,0 +1,21 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+#include "once.h"
+MODINFO;
+
+static volatile long first, second, extra;
+
+NOIPA long target(long value)
+{
+	ONCE_INIT(first, 1);
+#ifdef PATCHED
+	ONCE_INIT(extra, 3);
+#endif
+	ONCE_INIT(second, 2);
+#ifdef PATCHED
+	return value ^ first ^ second ^ extra;
+#else
+	return value ^ first ^ second;
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/pieces.c new file +28 -0
new file mode 100644
index 000000000000..4cd3ffbacbae
@@ -0,0 +1,28 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+static struct { long first, second; } state;
+
+NOIPA void set(long first, long second)
+{
+	state.first = first;
+	state.second = second;
+}
+
+NOIPA long sum(void)
+{
+	return state.first + state.second;
+}
+
+NOIPA long target(void)
+{
+#if defined(PATCHED) && defined(SWITCH_FIELD)
+	return state.second;
+#elif defined(PATCHED)
+	return state.first + 1;
+#else
+	return state.first;
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/promotion_bounds_a.c new file +12 -0
new file mode 100644
index 000000000000..74eb8ac0bfc3
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+static volatile int state[4];
+
+int a_get(unsigned int index)
+{
+#ifdef PATCHED
+	if (index >= 4)
+		return 0;
+#endif
+	return state[index];
+}
tools/objtool/tests/generic/fixtures/identity/promotion_bounds_b.c new file +12 -0
new file mode 100644
index 000000000000..2b96569ce6cd
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+int a_get(unsigned int index);
+int d_get(unsigned int index);
+
+int target(unsigned int index)
+{
+	return a_get(index) + d_get(index);
+}
tools/objtool/tests/generic/fixtures/identity/promotion_bounds_d.c new file +12 -0
new file mode 100644
index 000000000000..2e2a2691df38
@@ -0,0 +1,12 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+static volatile int state[4];
+
+int d_get(unsigned int index)
+{
+#ifdef PATCHED
+	return state[index & 3];
+#else
+	return state[index];
+#endif
+}
tools/objtool/tests/generic/fixtures/identity/provider.c new file +9 -0
new file mode 100644
index 000000000000..412c5cb67341
@@ -0,0 +1,9 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+NOIPA int helper(int uid)
+{
+	return uid == 0 || uid == 1000 ? 0 : -1;
+}
+
+volatile int limit = 4096;
tools/objtool/tests/generic/fixtures/identity/readonly.c new file +30 -0
new file mode 100644
index 000000000000..f145d3344d60
@@ -0,0 +1,30 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+struct protocol { int kind; };
+NOIPA const struct protocol *tcp_type(int valid)
+{
+#if defined(PATCHED) && defined(CHANGE_VALUE)
+	static const struct protocol type = { 4 };
+#else
+	static const struct protocol type = { 1 };
+#endif
+#ifdef PATCHED
+	if (!valid)
+		return (void *)0;
+#endif
+	return &type;
+}
+NOIPA const struct protocol *udp_type(int valid)
+{
+	static const struct protocol type = { 1 };
+	return &type;
+}
+#ifdef PATCHED
+NOIPA const struct protocol *unix_type(int valid)
+{
+	static const struct protocol type = { 1 };
+	return &type;
+}
+#endif
tools/objtool/tests/generic/fixtures/identity/record.h new file +8 -0
new file mode 100644
index 000000000000..5af00486c5f4
@@ -0,0 +1,8 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+static inline unsigned int record(unsigned int n)
+{
+	static volatile unsigned int state;
+
+	return state += n;
+}
tools/objtool/tests/generic/fixtures/identity/rx.c new file +4 -0
new file mode 100644
index 000000000000..5d9871527b08
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "record.h"
+unsigned int rx(unsigned int n) { return record(n); }
tools/objtool/tests/generic/fixtures/identity/shrink.c new file +19 -0
new file mode 100644
index 000000000000..1a43964f719b
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+extern void initialize(void);
+extern void finish(void);
+
+NOIPA void target(void)
+{
+	static int done;
+
+	if (!done) {
+		done = 1;
+		initialize();
+#ifdef PATCHED
+		finish();
+#endif
+	}
+}
tools/objtool/tests/generic/fixtures/identity/source_driver.c new file +11 -0
new file mode 100644
index 000000000000..0ebcb01ced02
@@ -0,0 +1,11 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+#ifdef PATCHED
+int account_new(int n);
+NOIPA int account(int n) { return account_new(n); }
+#else
+int account_old(int n);
+NOIPA int account(int n) { return account_old(n); }
+#endif
tools/objtool/tests/generic/fixtures/identity/static_replace.c new file +29 -0
new file mode 100644
index 000000000000..5745738d5cbd
@@ -0,0 +1,29 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+#ifndef PATCHED
+NOIPA long retired(long value)
+{
+	static volatile long count;
+
+	return count += value;
+}
+#endif
+
+NOIPA long unchanged(long value)
+{
+	static volatile long count;
+
+	return count += value;
+}
+
+#ifdef PATCHED
+NOIPA long added(long value)
+{
+	static volatile long count;
+
+	return count += value;
+}
+#endif
tools/objtool/tests/generic/fixtures/identity/static_scope.c new file +31 -0
new file mode 100644
index 000000000000..a467f24f1a14
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+NOIPA int tx(int n)
+{
+	static volatile int bytes;
+
+#ifdef PATCHED
+	if (n < 0)
+		return 0;
+#endif
+	return bytes += n;
+}
+
+NOIPA int rx(int n)
+{
+	static volatile int bytes;
+
+	return bytes += n;
+}
+
+#ifdef PATCHED
+NOIPA int extra(int n)
+{
+	static volatile int bytes;
+
+	return bytes += n;
+}
+#endif
tools/objtool/tests/generic/fixtures/identity/static_scope_reorder.c new file +31 -0
new file mode 100644
index 000000000000..a451e632a6cb
@@ -0,0 +1,31 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "common.h"
+MODINFO;
+
+#ifdef PATCHED
+NOIPA int rx(int n)
+{
+	static volatile int bytes;
+
+	if (n < 0)
+		return 0;
+	return bytes += n;
+}
+#endif
+
+NOIPA int tx(int n)
+{
+	static volatile int bytes;
+
+	return bytes += n;
+}
+
+#ifndef PATCHED
+NOIPA int rx(int n)
+{
+	static volatile int bytes;
+
+	return bytes += n;
+}
+#endif
tools/objtool/tests/generic/fixtures/identity/tx.c new file +4 -0
new file mode 100644
index 000000000000..d536c3c63549
@@ -0,0 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+#include "record.h"
+unsigned int tx(unsigned int n) { return record(n); }
tools/objtool/tests/generic/test-checksum-identity-source.sh new file +24 -0
new file mode 100755
index 000000000000..b320a57c6a76
@@ -0,0 +1,24 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Moving an external caller changes which TU-local helper it calls.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+modes=(none)
+case "$($CC --version | head -1)" in *clang*) modes+=(thin) ;; esac
+
+for mode in "${modes[@]}"; do
+	build_identity_pair "$mode" "" identity/move_left.c identity/move_right.c
+	[ "$(in_symbols orig.o | awk '$4 == "FUNC" && $8 ~ /^helper($|.llvm.)/' | wc -l)" = 2 ] ||
+		fail "fixture lacks two TU-local helpers"
+	assert_checksum_differs caller
+	assert_checksum_matches keep_left
+	assert_checksum_matches keep_right
+	run_diff
+	assert_patched caller
+	assert_not_patched keep_left
+	assert_not_patched keep_right
+done
+
+pass "moving a caller between TUs detects the changed local target"
tools/objtool/tests/generic/test-checksum-identity.sh new file +32 -0
new file mode 100755
index 000000000000..cc8a70bf31d2
@@ -0,0 +1,32 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Checksums for calls, data references and function-pointer tables must
+# distinguish same-named static and external targets.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+modes=(none)
+case "$($CC --version | head -1)" in *clang*) modes+=(thin) ;; esac
+
+for mode in "${modes[@]}"; do
+	build_identity_pair "$mode" "" identity/provider.c identity/consumer.c
+	[ "$(in_symbols patched.o |
+		awk '$4 == "FUNC" && $8 ~ /^helper($|.llvm.)/' | wc -l)" = 2 ] ||
+		fail "fixture lacks the local and external helpers"
+	assert_checksum_differs caller
+	run_diff
+	assert_patched caller
+
+	build_identity_pair "$mode" -DTABLE identity/provider.c identity/consumer.c
+	assert_checksum_differs ops
+	run_diff 255
+	assert_diff_log 'changed data: ops'
+
+	build_identity_pair "$mode" -DDATA identity/provider.c identity/consumer.c
+	assert_checksum_differs caller
+	run_diff
+	assert_patched caller
+done
+
+pass "call, data and function-pointer targets use the full identity"
tools/objtool/tests/generic/test-identity-alias.sh new file +26 -0
new file mode 100755
index 000000000000..84a3829b00e8
@@ -0,0 +1,26 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Access through an alias must retain the original shared storage.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/alias.c
+assert_input_symbol state
+assert_input_symbol state_alias
+
+run_diff
+assert_patched target
+assert_not_patched unchanged
+assert_no_symbol state
+assert_no_symbol state_alias
+run_post_link
+identity_relocs out.o .klp.rela.orig..text.target |
+	awk '$5 ~ /^\.klp\.sym\.orig\.state(_alias)?,/' | grep -q . ||
+	fail "target does not reference original state"
+
+build_pair identity/alias.c -DCHANGE_DATA
+run_diff 255
+assert_diff_log 'changed data: state'
+
+pass "data aliases reuse the original object and reject changed storage"
tools/objtool/tests/generic/test-identity-block-data.sh new file +12 -0
new file mode 100755
index 000000000000..439b2fe7cc61
@@ -0,0 +1,12 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Changing a block-local counter's initial value remains a data change.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/block_data.c
+run_diff 255
+assert_diff_log 'changed data:.*budget'
+
+pass "changed block-local data is rejected"
tools/objtool/tests/generic/test-identity-branch-hint.sh new file +18 -0
new file mode 100755
index 000000000000..68a49aa12e96
@@ -0,0 +1,18 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Branch hints can reorder symbols for two existing block-local counters.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/branch_hint.c
+for obj in orig.o patched.o; do
+	nr="$(in_symbols "$obj" | awk '
+		$4 == "OBJECT" && $8 ~ /(^|\.)count(\.[0-9]+)?$/ { n++ } END { print n+0 }')"
+	[ "$nr" = 2 ] || fail "expected two counters in $obj"
+done
+
+run_diff 255
+assert_diff_log 'unresolved data reference:.*count'
+
+pass "branch hint changes report ambiguous counters"
tools/objtool/tests/generic/test-identity-compound.sh +22 -2
index 76b16616b32c..fc58d2fad17a 100755
@@ -1,13 +1,33 @@
 #!/bin/bash
 # SPDX-License-Identifier: GPL-2.0
 # Copyright (C) 2026 Longjun Luo <[email protected]>
-# Array contents and callbacks contribute to the referring function's checksum.
+# A changed function keeps the address of an unchanged anonymous table.
 . "$(dirname "$0")/../identity-lib.sh"
 
 setup
+for flags in "" -DWRITABLE; do
+	build_pair identity/compound.c -DCHANGE_FUNCTION $flags
+	old="$(identity_object_for orig.o target)"
+	[ -n "$old" ] || fail "missing compound reference"
+
+	run_diff
+	assert_patched target
+	assert_not_patched unchanged
+	run_post_link
+	assert_original_reference target "$old"
+	assert_no_symbol "$old"
+done
+
+# Array contents and callbacks contribute to the referring function's checksum.
 for change in CHANGE_LEAF CHANGE_TARGET; do
 	build_pair identity/compound.c "-D$change"
 	assert_checksum_differs target
+	run_diff 255
+	assert_diff_log 'unresolved compound reference:.*has no unchanged match'
 done
 
-pass "compound contents contribute to reference checksums"
+build_pair identity/compound.c -DCHANGE_FUNCTION -DAMBIGUOUS
+run_diff 255
+assert_diff_log 'unresolved compound reference:.*matches .* and '
+
+pass "compound references retain old storage or report ambiguity"
tools/objtool/tests/generic/test-identity-dwarf.sh new file +34 -0
new file mode 100755
index 000000000000..2b18110f243b
@@ -0,0 +1,34 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Kernel DWARF 4/5 and reduced debug information describe the same static state.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+reduced=(-fno-var-tracking)
+case "$($CC --version | head -1)" in
+*clang*) ;;
+*) reduced+=(-femit-struct-debug-baseonly) ;;
+esac
+
+for version in 4 5; do
+	for mode in full reduced; do
+		flags=("-gdwarf-$version")
+		[ "$mode" = reduced ] && flags+=("${reduced[@]}")
+		build_pair identity/once_inline.c "${flags[@]}"
+		states="$(in_symbols orig.o | awk '
+			$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { print $8 }')"
+		[ "$(echo "$states" | wc -w)" = 2 ] || fail "expected two initialization flags"
+
+		run_diff
+		assert_patched target
+		assert_not_patched unchanged
+		run_post_link
+		for state in $states; do
+			assert_original_reference target "$state"
+			assert_no_symbol "$state"
+		done
+	done
+done
+
+pass "full and reduced DWARF preserve initialization state"
tools/objtool/tests/generic/test-identity-full-lto.sh new file +30 -0
new file mode 100755
index 000000000000..08aa67a444a3
@@ -0,0 +1,30 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Full LTO merges FILE entries; a new helper must not replace an existing TU's helper.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_identity_pair full "" identity/full_left.c identity/full_right.c
+
+[ "$(in_symbols orig.o | awk '$4 == "FUNC" && $8 ~ /^helper(\.[0-9]+)?$/' | wc -l)" = 1 ] &&
+[ "$(in_symbols patched.o | awk '$4 == "FUNC" && $8 ~ /^helper(\.[0-9]+)?$/' | wc -l)" = 2 ] ||
+	probe_skip "Full LTO did not emit two same-base helper names"
+left_helper="$(identity_relocs patched.o .rela.text.left_fn |
+	awk '{ sub(/^\.text\./, "", $5); print $5 }')"
+right_helper="$(identity_relocs patched.o .rela.text.right_fn |
+	awk '{ sub(/^\.text\./, "", $5); print $5 }')"
+[ -n "$left_helper" ] && [ -n "$right_helper" ] || fail "missing helper references"
+left_state="$(identity_object_for orig.o left_fn)"
+[ -n "$left_state" ] || fail "missing left state reference"
+
+run_diff
+assert_patched left_fn
+assert_not_patched right_fn
+assert_patched "$left_helper"
+assert_not_patched "$right_helper"
+assert_reloc_sym .text.left_fn "$left_helper"
+run_post_link
+assert_original_reference "$left_helper" "$left_state"
+
+pass "Full LTO helpers retain their defining TUs"
tools/objtool/tests/generic/test-identity-icf.sh new file +23 -0
new file mode 100755
index 000000000000..bad6acccd0c1
@@ -0,0 +1,23 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Same-named ICF wrappers in different CUs must remain separate functions.
+. "$(dirname "$0")/../identity-lib.sh"
+
+gcc_only "GCC IPA-ICF emits wrappers without code ranges in DWARF"
+setup
+build_identity_pair none -fipa-icf \
+	identity/icf_left/same.c identity/icf_right/same.c identity/lookup.c
+for obj in orig.o patched.o; do
+	nr="$(in_symbols "$obj" | awk '$4 == "FUNC" && $8 == "helper" { n++ } END { print n+0 }')"
+	[ "$nr" = 2 ] || fail "expected two helper functions in $obj"
+done
+
+run_diff
+assert_patched target
+assert_not_patched helper
+assert_not_patched base
+assert_checksum_matches left_helper
+assert_checksum_matches right_helper
+
+pass "same FILE basenames do not confuse ICF wrapper identities"
tools/objtool/tests/generic/test-identity-inline-helpers.sh new file +25 -0
new file mode 100755
index 000000000000..21641c8dc975
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A new inline initializer gets its own flag; the existing initializer keeps its state.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/inline_helpers.c
+old="$(in_symbols orig.o | awk '$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { print $8 }')"
+[ "$(echo "$old" | wc -w)" = 1 ] || fail "expected one original initialization flag"
+nr="$(in_symbols patched.o |
+	awk '$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { n++ } END { print n+0 }')"
+[ "$nr" = 2 ] ||
+	fail "expected two patched initialization flags"
+
+run_diff
+assert_patched target
+new="$(out_symbols |
+	awk '$4 == "OBJECT" && $7 != "UND" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { print $8 }')"
+[ "$(echo "$new" | wc -w)" = 1 ] || fail "expected only the new initializer's flag to be copied"
+assert_reloc_sym .text.target "$new"
+run_post_link
+assert_original_reference target "$old"
+
+pass "inline helpers retain separate initialization state without an unchanged caller"
tools/objtool/tests/generic/test-identity-inline-state.sh new file +21 -0
new file mode 100755
index 000000000000..2cff1d3c0645
@@ -0,0 +1,21 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Both callers of an inline initializer must keep using its original flags.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/once_inline.c
+states="$(in_symbols orig.o | awk '$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { print $8 }')"
+[ "$(echo "$states" | wc -w)" = 2 ] || fail "expected two initialization flags"
+
+run_diff
+assert_patched target
+assert_not_patched unchanged
+run_post_link
+for state in $states; do
+	assert_original_reference target "$state"
+	assert_no_symbol "$state"
+done
+
+pass "changed caller retains both initialization flags"
tools/objtool/tests/generic/test-identity-metadata.sh new file +20 -0
new file mode 100755
index 000000000000..fe575d710c56
@@ -0,0 +1,20 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# symbol_get and tracepoint checks leave build-time markers that the linker discards.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/metadata.c
+assert_input_section .no_trim_symbol
+assert_input_section __tracepoint_check
+
+run_diff
+assert_patched target
+assert_no_section .no_trim_symbol
+assert_no_section __tracepoint_check
+if diff_log | grep -qE 'no correlation:.*(__notrim|__trace_check_)'; then
+	fail "build-time markers reported as unmatched state"
+fi
+
+pass "build-time markers do not enter state correlation"
tools/objtool/tests/generic/test-identity-new-tu.sh new file +24 -0
new file mode 100755
index 000000000000..9675af836b46
@@ -0,0 +1,24 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A patch may extract validation into a newly added source file.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/new_driver.c
+build_one identity/new_provider.c provider.o
+mv "$workdir/patched.o" "$workdir/driver.o"
+partial_link "$workdir/patched.o" "$workdir/driver.o" "$workdir/provider.o" ||
+	fail "partial link failed"
+
+[ "$(in_symbols patched.o | awk '$4 == "FILE"' | wc -l)" \
+	-gt "$(in_symbols orig.o | awk '$4 == "FILE"' | wc -l)" ] ||
+	fail "fixture did not add a FILE entry"
+
+run_diff
+assert_patched target
+assert_patched clamp
+assert_diff_log 'changed function: target'
+assert_diff_log 'new function: clamp'
+
+pass "new translation unit does not block existing symbol correlation"
tools/objtool/tests/generic/test-identity-once-insert.sh new file +19 -0
new file mode 100755
index 000000000000..f1accb98d1e5
@@ -0,0 +1,19 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Inserting an initializer must not reset the two existing once flags.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/once_insert.c
+for spec in "orig.o 2" "patched.o 3"; do
+	set -- $spec
+	nr="$(in_symbols "$1" | awk '
+		$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { n++ } END { print n+0 }')"
+	[ "$nr" = "$2" ] || fail "expected $2 initialization flags in $1"
+done
+
+run_diff 255
+assert_diff_log 'unresolved data reference:.*done'
+
+pass "ambiguous initialization flags are rejected"
tools/objtool/tests/generic/test-identity-pieces.sh new file +27 -0
new file mode 100755
index 000000000000..c42a98ad0ef1
@@ -0,0 +1,27 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# GlobalOpt can split a struct into equal-sized objects with the same DWARF name.
+. "$(dirname "$0")/../identity-lib.sh"
+
+clang_only "Clang GlobalOpt splits this state into separate objects"
+setup
+build_pair identity/pieces.c
+nr="$(in_symbols orig.o | awk '$4 == "OBJECT" && $8 ~ /^state\.[0-9]+$/ { n++ } END { print n+0 }')"
+[ "$nr" = 2 ] || probe_skip "compiler did not split state into two objects"
+old="$(identity_object_for orig.o target)"
+[ -n "$old" ] || fail "missing state reference"
+
+run_diff
+assert_patched target
+assert_not_patched set
+assert_not_patched sum
+run_post_link
+assert_original_reference target "$old"
+assert_no_symbol "$old"
+
+build_pair identity/pieces.c -DSWITCH_FIELD
+run_diff 255
+assert_diff_log 'target.*twin conflict:'
+
+pass "unchanged references preserve pieces and conflicting field switches are rejected"
tools/objtool/tests/generic/test-identity-readonly-address.sh new file +23 -0
new file mode 100755
index 000000000000..18c49856833b
@@ -0,0 +1,23 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Read-only descriptors still need their original pointer identity.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/readonly.c
+old="$(identity_object_for orig.o tcp_type)"
+[ -n "$old" ] || fail "missing TCP descriptor reference"
+
+run_diff
+assert_patched tcp_type
+assert_patched unix_type
+assert_not_patched udp_type
+run_post_link
+assert_original_reference tcp_type "$old"
+
+build_pair identity/readonly.c -DCHANGE_VALUE
+run_diff 255
+assert_diff_log 'changed data:'
+
+pass "changed function retains the original read-only descriptor address"
tools/objtool/tests/generic/test-identity-shrink.sh new file +19 -0
new file mode 100755
index 000000000000..d9f007fc56f3
@@ -0,0 +1,19 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A compressed once flag keeps its state when the initialization code changes.
+. "$(dirname "$0")/../identity-lib.sh"
+
+clang_only "Clang shrinks the int flag to one byte"
+setup
+build_pair identity/shrink.c
+old="$(in_symbols orig.o | awk '$4 == "OBJECT" && $3 == 1 && $8 ~ /\.done$/ { print $8 }')"
+[ -n "$old" ] || probe_skip "compiler did not shrink the once flag"
+
+run_diff
+assert_patched target
+run_post_link
+assert_original_reference target "$old"
+assert_no_symbol "$old"
+
+pass "compressed once flag retains its original storage"
tools/objtool/tests/generic/test-identity-source.sh new file +29 -0
new file mode 100755
index 000000000000..bbb2e729571f
@@ -0,0 +1,29 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# old/same.c and new/same.c share the FILE name "same.c"; static state
+# from the new file must still be treated as new.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/source_driver.c
+build_one identity/old/same.c old.o
+build_one identity/new/same.c new.o
+mv "$workdir/orig.o" "$workdir/driver-orig.o"
+mv "$workdir/patched.o" "$workdir/driver-patched.o"
+partial_link "$workdir/orig.o" "$workdir/driver-orig.o" "$workdir/old.o" ||
+	fail "original link failed"
+partial_link "$workdir/patched.o" "$workdir/driver-patched.o" "$workdir/new.o" ||
+	fail "patched link failed"
+for obj in orig.o patched.o; do
+	in_symbols "$obj" | awk '$4 == "FILE" && $8 == "same.c"' | grep -q . ||
+		fail "missing same.c FILE"
+done
+
+run_diff
+assert_patched account
+assert_patched helper
+assert_no_klp_sym bytes
+assert_section .bss.bytes
+
+pass "private state from a different TU is new even with the same basename"
tools/objtool/tests/generic/test-identity-split.sh new file +34 -0
new file mode 100755
index 000000000000..0af15583fe08
@@ -0,0 +1,34 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# klp-build saves the original DWOs in a DWP before the patched build overwrites them.
+. "$(dirname "$0")/../identity-lib.sh"
+
+DWP="${DWP:-llvm-dwp}"
+command -v "$DWP" >/dev/null || probe_skip "split DWARF requires llvm-dwp (or set DWP)"
+setup
+# libdw 0.191 added DWP lookup and dwarf_cu_dwp_section_info().
+libdw="$(ldd "$OBJTOOL" 2>/dev/null | awk '$1 == "libdw.so.1" { print $3 }')"
+if [ -n "$libdw" ] && $READELF --dyn-syms -W "$libdw" > "$workdir/libdw.symbols"; then
+	grep -q 'dwarf_cu_dwp_section_info' "$workdir/libdw.symbols" ||
+		probe_skip "saved DWP files require libdw >= 0.191"
+fi
+# CONFIG_DEBUG_INFO_SPLIT with CONFIG_DEBUG_INFO_DWARF4.
+build_pair identity/once_inline.c -gdwarf-4 -gsplit-dwarf
+states="$(in_symbols orig.o | awk '$4 == "OBJECT" && $8 ~ /(^|\.)done(\.[0-9]+)?$/ { print $8 }')"
+[ "$(echo "$states" | wc -w)" = 2 ] || fail "expected two initialization flags"
+for side in orig patched; do
+	"$DWP" -e "$workdir/$side.o" -o "$workdir/$side.o.dwp" || fail "DWP packaging failed"
+	mv "$workdir/$side.dwo" "$workdir/$side.dwo.saved" || fail "missing $side.dwo"
+done
+
+run_diff
+assert_patched target
+assert_not_patched unchanged
+run_post_link
+for state in $states; do
+	assert_original_reference target "$state"
+	assert_no_symbol "$state"
+done
+
+pass "saved DWP files retain the original initialization state"
tools/objtool/tests/generic/test-identity-static-replace.sh new file +20 -0
new file mode 100755
index 000000000000..987555f925a7
@@ -0,0 +1,20 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A new function's counter must not share state with an unchanged function.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity/static_replace.c
+new="$(identity_object_for patched.o added)"
+[ -n "$new" ] || fail "missing new counter reference"
+
+run_diff
+assert_patched added
+assert_not_patched unchanged
+assert_not_patched retired
+assert_section ".bss.$new"
+assert_reloc_sym .text.added "$new"
+assert_no_klp_sym "$new"
+
+pass "replacement function gets its own new counter"
tools/objtool/tests/generic/test-identity-static-scope.sh new file +35 -0
new file mode 100755
index 000000000000..fc0372100886
@@ -0,0 +1,35 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Reordering functions or adding a same-named static must retain old state.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+
+build_pair identity/static_scope.c
+tx_state="$(identity_object_for orig.o tx)"
+extra_state="$(identity_object_for patched.o extra)"
+[ -n "$tx_state" ] && [ -n "$extra_state" ] || fail "missing static reference"
+
+run_diff
+assert_patched tx
+assert_not_patched rx
+assert_patched extra
+assert_section ".bss.$extra_state"
+assert_reloc_sym .text.extra "$extra_state"
+run_post_link
+assert_original_reference tx "$tx_state"
+
+build_pair identity/static_scope_reorder.c
+rx_state="$(identity_object_for orig.o rx)"
+[ -n "$rx_state" ] || fail "missing RX static reference"
+
+run_diff
+assert_patched rx
+assert_not_patched tx
+out_symbols | awk '$4 == "OBJECT" && $7 != "UND" && $8 ~ /bytes/' | grep -q . &&
+	fail "existing static state was copied"
+run_post_link
+assert_original_reference rx "$rx_state"
+
+pass "named scopes preserve state through renumbering and additions"
tools/objtool/tests/generic/test-identity-thinlto-name-drift.sh new file +79 -0

79 lines not on this page · raw message

tools/objtool/tests/generic/test-identity-thinlto-promotion.sh new file +26 -0
new file mode 100755
index 000000000000..f4e9418817a0
@@ -0,0 +1,26 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A new caller makes ThinLTO import rx() and promote rx.c's copy of
+# record()'s static state; the patch must keep using the original state.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_identity_pair thin "" identity/tx.c identity/rx.c identity/entries.c
+
+in_symbols orig.o | awk '$4 == "OBJECT" && $5 == "LOCAL" && $8 == "record.state"' |
+	grep -q . || probe_skip "RX state was not local before importing"
+[ "$(in_symbols patched.o |
+	awk '$4 == "OBJECT" && $5 == "GLOBAL" && $8 ~ /^record.state.llvm./' | wc -l)" = 2 ] ||
+	probe_skip "ThinLTO did not promote both state instances"
+rx_state="$(identity_object_for orig.o rx)"
+[ -n "$rx_state" ] || fail "missing RX static reference"
+
+run_diff
+assert_patched entry_rx
+out_symbols | awk '$4 == "OBJECT" && $7 != "UND" && $8 ~ /record.state/' | grep -q . &&
+	fail "promoted state was allocated again"
+run_post_link
+assert_original_reference entry_rx "$rx_state"
+
+pass "header state retains its defining TU across promotion"
tools/objtool/tests/generic/test-identity-weak-cold.sh new file +35 -0
new file mode 100755
index 000000000000..a21c522d5702
@@ -0,0 +1,35 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Overridden weak functions can leave cold fragments with the strong function's name.
+. "$(dirname "$0")/../identity-lib.sh"
+
+gcc_only "GCC splits functions into cold fragments"
+setup
+split_flag=-freorder-blocks-and-partition
+cc_supports "$split_flag" || split_flag=
+
+for flags in "" "-DCHANGE_STRONG"; do
+	build_identity_pair none "$split_flag $flags" \
+		identity/cold_weak.c identity/cold_strong.c identity/cold_user.c
+	for obj in orig.o patched.o; do
+		nr="$(in_symbols "$obj" | awk '
+			$4 == "FUNC" && $8 ~ /^foo\.cold(\.[0-9]+)?$/ { n++ }
+			END { print n+0 }')"
+		[ "$nr" = 2 ] || probe_skip "compiler did not emit both cold fragments"
+	done
+
+	run_diff
+	assert_patched user
+	if [ -n "$flags" ]; then
+		assert_patched foo
+		out_symbols | awk '$4 == "FUNC" && $7 != "UND" && $8 ~ /^foo\.cold(\.[0-9]+)?$/' |
+			grep -q . || fail "strong cold fragment was not copied"
+		assert_symbol strong_slow_path
+		assert_no_symbol weak_slow_path
+	else
+		assert_not_patched foo
+	fi
+done
+
+pass "weak and strong cold fragments retain separate source identities"
tools/objtool/tests/x86/fixtures/identity_assembly.S new file +33 -0
new file mode 100644
index 000000000000..eb2c1ca9d71f
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+	.file "identity_assembly.S"
+	.section .modinfo,"a"
+	.asciz "name=vmlinux"
+
+	.section .data.asm_state,"aw"
+	.type asm_state, @object
+asm_state:
+	.quad 11
+	.size asm_state, .-asm_state
+
+	.section .text.asm_unchanged,"ax"
+	.globl asm_unchanged
+	.type asm_unchanged, @function
+asm_unchanged:
+	mov asm_state(%rip), %rax
+	ret
+	.size asm_unchanged, .-asm_unchanged
+
+	.section .text.asm_target,"ax"
+	.globl asm_target
+	.type asm_target, @function
+asm_target:
+	mov asm_state(%rip), %rax
+#ifdef PATCHED
+	add $2, %rax
+#else
+	add $1, %rax
+#endif
+	ret
+	.size asm_target, .-asm_target
+	.section .note.GNU-stack,"",@progbits
tools/objtool/tests/x86/fixtures/warn_reference.c new file +36 -0
new file mode 100644
index 000000000000..1d5ffda88c75
@@ -0,0 +1,36 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <[email protected]> */
+static const char modinfo[] __attribute__((used, section(".modinfo"))) = "name=vmlinux";
+extern void report(void *entry);
+
+/* WARN passes its own bug-table entry to the reporting code. */
+#define WARN_ENTRY() do { \
+	void *entry; \
+	asm volatile("1: nop\n\t" \
+		     ".pushsection __bug_table,\"aw\"\n\t" \
+		     "2:\n\t" \
+		     ".pushsection .discard.annotate_data,\"M\",@progbits,8\n\t" \
+		     ".long 2b - ., 1\n\t" \
+		     ".popsection\n\t" \
+		     ".long 1b - .\n\t" \
+		     ".fill 8,1,0\n\t" \
+		     ".popsection\n\t" \
+		     "leaq 2b(%%rip), %0" : "=r" (entry)); \
+	report(entry); \
+} while (0)
+
+int other(int value)
+{
+	WARN_ENTRY();
+	return value;
+}
+
+int target(int value)
+{
+	WARN_ENTRY();
+#ifdef PATCHED
+	return value + 2;
+#else
+	return value + 1;
+#endif
+}
tools/objtool/tests/x86/test-identity-assembly.sh new file +19 -0
new file mode 100755
index 000000000000..4aee7ee7f279
@@ -0,0 +1,19 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# Assembly functions without their own DIEs still reuse existing data.
+. "$(dirname "$0")/../identity-lib.sh"
+
+setup
+build_pair identity_assembly.S
+assert_input_symbol asm_state
+assert_input_symbol asm_target
+
+run_diff
+assert_patched asm_target
+assert_not_patched asm_unchanged
+assert_no_symbol asm_state
+run_post_link
+assert_original_reference asm_target asm_state
+
+pass "assembly function retains the original data reference"
tools/objtool/tests/x86/test-warn-reference.sh new file +21 -0
new file mode 100755
index 000000000000..48c8042e0cbd
@@ -0,0 +1,21 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <[email protected]>
+# A patched WARN site needs the bug-table entry it passes to the reporter.
+. "$(dirname "$0")/../lib.sh"
+
+setup
+build_pair warn_reference.c
+assert_input_section __bug_table
+in_relocs patched.o | grep -q 'R_X86_64_PC32.*__bug_table' ||
+	fail "missing function reference to __bug_table"
+
+run_diff
+assert_patched target
+assert_not_patched other
+assert_section __bug_table
+assert_reloc_sym __bug_table target
+assert_no_reloc_sym __bug_table other
+assert_reloc_count __bug_table 1
+
+pass "patched WARN site retains its own bug-table entry"
Signature · 1 line
2.55.0
#21 lore raw

Re: [RFC PATCH v2 11/11] objtool/klp: Test symbol identity and state preservation

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] objtool/klp: test suite left failing and sympos coverage lost --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] objtool/klp: test suite left failing and sympos coverage lost
--

commit 81f49775a536febbdc9ffe953af763d00e57323f
Author: Longjun Luo <[email protected]>

objtool/klp: Test symbol identity and state preservation

This commit adds tests and fixtures to exercise symbol identity and local state preservation across source changes, inlining, compiler renaming, and LTO in objtool klp.

Quoted 12 lines

objtool/klp: Test symbol identity and state preservation

Exercise local state across source changes, inlining, compiler renaming and LTO. Check that patches reference the original objects and allocate only new state. Cover aliases, assembly, metadata, ICF, DWARF pieces, shrunk storage, reduced information and saved DWP files. Cover separate cold-fragment identities left by weak overrides.

Check rejection of ambiguous repeated statics, conflicting references and changed data. Complete compound-reference tests and add same-named target switches for the identity-based reference checksum.

Quoted 8 lines

Skip the saved-DWP test when the linked libdw lacks DWP support.

The full suite has 68 passes with GCC 16.2.1 and 75 with Clang 22.1.8. Only local-to-global-flip, sympos and sympos-vmlinux fail, reflecting the linkage and duplicate-identity policies.

Assisted-by LLM

[Severity: Medium]
Does leaving test-local-to-global-flip.sh, test-sympos.sh, and test-sympos-vmlinux.sh failing cause the test suite runner in tools/objtool/tests/run-tests.sh to always exit non-zero, while also losing all test coverage for sympos calculation?

In tools/objtool/tests/run-tests.sh, the runner exits non-zero whenever any test fails without xfail:

    [ "$fail" = 0 ] && [ "$xpass" = 0 ]

In tools/objtool/tests/generic/test-sympos.sh and tools/objtool/tests/generic/test-sympos-vmlinux.sh, the scripts compile a single source file twice to produce two translation units with a same-named static variable (dup_counter):

tools/objtool/tests/generic/test-sympos.sh:
build_one sympos_dup.c "a$p.o" -DFUNC_NAME=use_a $p # shellcheck disable=SC2086
build_one sympos_dup.c "b$p.o" -DFUNC_NAME=use_b -DNO_MODINFO $p

tools/objtool/tests/generic/test-sympos-vmlinux.sh:
build_one sympos_vmlinux.c orig_a.o -DFUNC_NAME=use_a -DVARSEC='".data.zzz"'
build_one sympos_vmlinux.c patched_a.o -DFUNC_NAME=use_a -DVARSEC='".data.zzz"' -DPATCHED
build_one sympos_vmlinux.c b.o -DFUNC_NAME=use_b -DVARSEC='".data.aaa"' -DNO_MODINFO

Because both objects are compiled from the exact same source file path, they share an identical DWARF CU path, causing validate_dwarf_identity() in tools/objtool/klp-identity.c to abort during klp checksum with a duplicate symbol identity error before klp diff ever runs. As a result, klp_find_sympos() and .klp.symid vmlinux address-order resolution are never reached or tested.

Similarly, in tools/objtool/tests/generic/test-local-to-global-flip.sh:

run_diff

assert_diff_log 'changed function: caller'

the test still expects klp diff to succeed and correlate symbols across linkage changes, whereas check_linkage_changes() in tools/objtool/klp-diff.c now rejects linkage flips with exit status 255.

Should test-sympos.sh and test-sympos-vmlinux.sh compile separate source files so their DWARF CU paths are distinct and sympos resolution is still exercised, and should test-local-to-global-flip.sh be updated to assert the linkage rejection so the test suite passes cleanly?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
maintainer
#22 lore raw
In reply to Longjun Luo
Quoted 28 lines · Longjun Luo

Responses to v1 Review (Guide)
------------------------------

Thanks to Josh for reviewing v1. v2 responds as follows:

1. CONFIG_DEBUG_INFO and split/reduced DWARF: as suggested, klp-build now requires CONFIG_DEBUG_INFO and uses Kbuild's debug flags (Patch 6). Reduced and split DWARF are supported (Patch 5). See "Compatibility Limits and Build Requirements".

2. Repeated macro statics and the one-line test: building on the referencing-function idea, unchanged-function references resolve repeated statics (Patch 9). The tree-wide test reduces v1's no correlation warnings from 682/764 to zero (GCC/Clang). See "Evaluation and Performance Overhead".

3. Upgrading no correlation to an error and explicit annotations:
following this direction, v2 rejects ambiguous data references and allows verified new data (Patch 10). See Case D for the state-inheritance boundary, its kernel-wide impact, and proposed annotations.

4. Referencing functions, ThinLTO naming flags, and Full LTO: in ELF, the referencing function only approximates the defining scope, and STT_FILE and .llvm.<hash> suffixes only approximate the defining TU. Definition identity distinguishes inlined helpers' state and survives LTO renaming and STT_FILE merging; see Cases A-B.

Quoted 4 lines

5. Toolchain reliability and performance: v2 cross-checks DWARF against ELF and measures checksum/diff costs, including a separate memory prototype. See "Design Overview", "Evaluation and Performance Overhead", and "Open Questions and Next Steps".

Going forward, please reply directly to the review comments (including sashiko reviews), as threaded emails are much easier to follow/discuss than a summary like the above.

Signature · 1 line
Josh
#23 lore raw

Understood, I'll follow up inline on those threads directly.

I sent v2 out as a new series mainly because it covers the next layer of the design: v1 introduced the (source, scope, name) identity, and v2 builds on that to do the actual symbol matching using checksums and reference context.

Longjun

maintainer
#24 lore raw
In reply to Longjun Luo

On Wed, Nov 04, 2026 at 09:31:46PM +0800, Longjun Luo wrote: > Open Questions and Next Steps

Quoted 32 lines

-----------------------------

This is a substantial change: 1,755 of the 4,010 added lines are tests, and about 1,300 implement the DWARF reader and identity layer. Before preparing a merge-ready series, I would like explicit maintainer guidance on how to stage its integration. My proposed sequence is:

- Submit Patches 1-3 separately with their regression tests.
- Prepare the identity representation, DWARF reader, and ELF cross-checks as a foundation that can be reviewed separately from twin matching.
- Integrate the matching changes in Patches 7-10 together: reference extension pairs repeated statics, and the reference checks reject remaining ambiguous data references.
- Submit the memory optimizations separately and pursue explicit state annotations in a separate RFC.

This RFC retains upstream expectations for the three policy tests to make the behavior changes visible. For the merge-ready series, I will update their assertions to reflect the agreed policy and move Patch 11's tests alongside the changes they cover.

I plan to add triple-based DWARF correctness tests in upstream compilers, using known source definitions and optimization variants to check coverage, non-conflict, and attribution accuracy (with clones, pieces, anonymous scopes, and folding checked according to their respective semantics) without locking in a specific DIE layout or suffix scheme. My earlier bug reports and fixes on IPA-ICF, weak-override attribution, and assembly CU selection [2-6] provide concrete test inputs, and the same attribution model applies to consumers such as AutoFDO [7].

I would appreciate feedback on four questions:

Quoted 11 lines

1. Would maintainers prefer to integrate the identity representation and reader first, then evolve twin matching in a follow-up series, or keep them together for integration?
2. Is constraining candidates by the identity triple and extending twins via checksums and unchanged-function references the right foundation for correlation, and how should the final ELF fallback be bounded?
3. Is the handling boundary in Case D reasonable, and should explicit state annotations (such as KLP_NEW_STATIC() / KLP_DEL_STATIC()) be pursued in a separate RFC?
4. How should we structure upstream compiler tests around the identity triple to protect both DWARF producers and livepatch consumers?

There are a LOT of words in this cover letter. The whole thing is a bit overwhelming to try to review, much less answer these questions. Please don't use LLM-generated text, as it can be very difficult to follow.

Also, I noticed the cover letter has several back-references to v1, adding to my confusion. The cover letter should be self-contained, such that a reviewer can easily understand what the patch series does without first studying v1.

DWARF is the most controversial and complex part of this series. Before going there, we should look at whatever can be improved *without* relying on DWARF. That will then make it much easier to evaluate what additional benefits can be provided by DWARF later on.

klp-build is very much a pragmatic tool, and most likely will never be able to handle all possible AI-imagined edge cases. It should always be used with caution, with someone (or something) double checking its results at the end.

I suggest splitting up the patch series, focusing on fixing the more realistic and tractable issues first. Maybe even one issue at a time. Human reviewer attention is spread very thin right now, please make legibility a top priority.

Signature · 1 line
Josh
#25 lore raw

I understand the concern about readability. Since English is not my native language, I drafted the content myself and used AI to help with the English writing before reviewing and sending it out. I'll keep the information density in mind going forward.

Just to clarify the background: this design comes from my practical work on userspace livepatch, and v1/v2 were meant to present the two layers of that design as a whole. While dealing with LTO in practice, I ran into many recurring correlation issues, so I tried to abstract and model the problem systematically rather than hunting for corner cases, and I brought that modeling process into this review.

As noted in the RFC, this patchset is intended to validate the feasibility of the approach. I wanted feedback on the overall design before deciding whether to take it further.

Thanks for taking the time to review and share your thoughts.

Longjun

maintainer
#26 lore raw
Quoted 12 lines · Longjun Luo

I understand the concern about readability. Since English is not my native language, I drafted the content myself and used AI to help with the English writing before reviewing and sending it out. I'll keep the information density in mind going forward.

Just to clarify the background: this design comes from my practical work on userspace livepatch, and v1/v2 were meant to present the two layers of that design as a whole. While dealing with LTO in practice, I ran into many recurring correlation issues, so I tried to abstract and model the problem systematically rather than hunting for corner cases, and I brought that modeling process into this review.

Quoted 5 lines

As noted in the RFC, this patchset is intended to validate the feasibility of the approach. I wanted feedback on the overall design before deciding whether to take it further.

Thanks for taking the time to review and share your thoughts.

Thanks, that makes sense, but for upstreaming I think we will need to take a more iterative approach, even if it ends up causing some intermediate patches to be obsoleted by later ones.

Signature · 1 line
Josh
#27 lore raw
Quoted 3 lines

Thanks, that makes sense, but for upstreaming I think we will need to take a more iterative approach, even if it ends up causing some intermediate patches to be obsoleted by later ones.

Agreed. I actually got into livepatch through kpatch — I guess I got a bit nervous and wrote the RFCs like papers.

I'll follow up case by case within the current design. For cases klp-build leaves unsupported for now, I think documenting the limits would help patch authors and AI tools know what to check.

The two RFCs can stay as background on another approach, for cross-checking and future discussion.

Longjun