diff --git a/tools/objtool/include/objtool/klp.h b/tools/objtool/include/objtool/klp.h
index c57775d78c71..90468e141124 100644
--- a/tools/objtool/include/objtool/klp.h
+++ b/tools/objtool/include/objtool/klp.h
@@ -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);
diff --git a/tools/objtool/klp-checksum.c b/tools/objtool/klp-checksum.c
index efaf083c03a1..2a515c7e2bf6 100644
--- a/tools/objtool/klp-checksum.c
+++ b/tools/objtool/klp-checksum.c
@@ -11,6 +11,8 @@
#include <objtool/warn.h>
#include <objtool/checksum.h>
+#define COMPOUND_MAX_DEPTH 3
+
static int checksum_debug_init(struct objtool_file *file)
{
char *dup, *s;@@ -67,6 +69,14 @@ static bool is_anonymous_const_data(struct symbol *sym)
(is_sec_sym(sym) || strstarts(sym->name, ".LC"));
}
+static bool is_const_reference(struct symbol *sym)
+{
+ return is_string_sec(sym->sec) || is_anonymous_const_data(sym);
+}
+
+static int checksum_object(struct objtool_file *file, struct symbol *sym,
+ unsigned int depth);
+
static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
struct instruction *insn)
{@@ -74,7 +84,9 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
struct alternative *alt;
unsigned long offset;
struct symbol *sym;
+ const void *data;
static bool in_alt;
+ ssize_t size;
if (insn->fake)
return 0;
@@ -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;@@ -145,14 +156,6 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
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)@@ -161,8 +164,15 @@ static int checksum_update_insn(struct objtool_file *file, struct symbol *func,
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)) {
+ 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;
}
if (is_sec_sym(target)) {
target = find_symbol_containing(reloc->sym->sec, offset);
- if (!target)
+ if (!target) {
+ if (is_compound_constant(sym)) {
+ ERROR("%s+%#lx: unresolved compound constant reference",
+ sym->name, sym_offset);
+ return -1;
+ }
continue;
+ }
offset -= target->offset;
}
- __checksum_update_object(sym, sym_offset, "reloc name",
- target->demangled_name,
- strlen(target->demangled_name));
+ if (is_compound_constant(target)) {
+ if (checksum_object(file, target, depth + 1))
+ return -1;
+ __checksum_update_object(sym, sym_offset, "reloc compound",
+ &target->csum.checksum,
+ sizeof(target->csum.checksum));
+ } else {
+ __checksum_update_object(sym, sym_offset, "reloc name",
+ target->demangled_name,
+ strlen(target->demangled_name));
+ }
__checksum_update_object(sym, sym_offset, "reloc addend",
&offset, sizeof(offset));
}
return 0;
}
+/* Count only consecutive compound constants; named targets use their identity. */
+static int checksum_object(struct objtool_file *file, struct symbol *sym,
+ unsigned int depth)
+{
+ if (depth > COMPOUND_MAX_DEPTH) {
+ ERROR("%s: compound constant nesting exceeds %u levels",
+ sym->name, COMPOUND_MAX_DEPTH);
+ return -1;
+ }
+ if (sym->csum.state) {
+ ERROR("%s: cyclic compound constant reference", sym->name);
+ return -1;
+ }
+
+ checksum_init(sym);
+ if (checksum_update_object(file, sym, depth))
+ return -1;
+ checksum_finish(sym);
+ return 0;
+}
+
int calculate_checksums(struct objtool_file *file)
{
struct instruction *insn;@@ -272,10 +324,8 @@ int calculate_checksums(struct objtool_file *file)
checksum_finish(sym);
} else if (is_object_sym(sym)) {
- checksum_init(sym);
- if (checksum_update_object(file, sym))
+ if (checksum_object(file, sym, is_compound_constant(sym) ? 1 : 0))
return -1;
- checksum_finish(sym);
}
}diff --git a/tools/objtool/klp-diff.c b/tools/objtool/klp-diff.c
index 6321f691adbb..0391f91a8d31 100644
--- a/tools/objtool/klp-diff.c
+++ b/tools/objtool/klp-diff.c
@@ -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.diff --git a/tools/objtool/tests/generic/fixtures/identity/compound.c b/tools/objtool/tests/generic/fixtures/identity/compound.c
new file mode 100644
index 000000000000..bf607bdd6093
--- /dev/null
+++ b/tools/objtool/tests/generic/fixtures/identity/compound.c
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (C) 2026 Longjun Luo <luolongjuna@gmail.com> */
+#include "common.h"
+MODINFO;
+extern void observe(int value);
+
+NOIPA int read_value(int value)
+{
+ return value + 1;
+}
+
+NOIPA int other_value(int value)
+{
+ return value + 2;
+}
+
+#ifdef WRITABLE
+#define CONST
+#else
+#define CONST const
+#endif
+
+struct config {
+ CONST unsigned int *values;
+ const char *label;
+ int (*read)(int value);
+};
+
+#if defined(PATCHED) && defined(CHANGE_LEAF)
+#define FIRST 12
+#else
+#define FIRST 11
+#endif
+#if defined(PATCHED) && defined(CHANGE_TARGET)
+#define READ other_value
+#else
+#define READ read_value
+#endif
+
+static CONST struct config *const first = &(CONST struct config) {
+ (CONST unsigned int[]) { FIRST, 22, 33, 44, 55, 66, 77, 88 }, "table", READ
+};
+static CONST struct config *const second = &(CONST struct config) {
+ (CONST unsigned int[]) { 11, 22, 33, 44, 55, 66, 77, 88 }, "table", read_value
+};
+
+NOIPA CONST struct config *target(int value)
+{
+#if defined(PATCHED) && defined(CHANGE_FUNCTION)
+ if (value < 0)
+ return (void *)0;
+#endif
+ observe(value);
+#ifdef AMBIGUOUS
+ return value ? first : second;
+#else
+ return first;
+#endif
+}
+
+NOIPA CONST struct config *unchanged(int value)
+{
+ observe(value);
+ return second;
+}diff --git a/tools/objtool/tests/generic/test-identity-compound.sh b/tools/objtool/tests/generic/test-identity-compound.sh
new file mode 100755
index 000000000000..76b16616b32c
--- /dev/null
+++ b/tools/objtool/tests/generic/test-identity-compound.sh
@@ -0,0 +1,13 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2026 Longjun Luo <luolongjuna@gmail.com>
+# 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"
--
2.55.0