[PATCH v12 05/19] perf annotate-x86: Record immediates in a 64-bit field

From: Arnaldo Carvalho de Melo

Date: Mon Nov 23 2026 - 17:01:04 EST


From: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>

An x86 immediate is parsed into the int offset shared with
displacements, so a constant wider than 32 bits is truncated to garbage
that the register tracking then records as its value.

Add a s64 imm_value to struct annotated_op_loc, fill it in
extract_op_location_x86() with strtoull() and read it in the three
places of update_insn_state_x86() that take an immediate as a constant.
The displacement and the ARM64 and x86 %gs: immediates, which fit in
32 bits, stay in offset, and pointer offset tracking is unchanged.

Signed-off-by: Arnaldo Carvalho de Melo <acme@xxxxxxxxxx>
---
tools/perf/util/annotate-arch/annotate-x86.c | 13 ++++++++-----
tools/perf/util/annotate.h | 6 ++++--
2 files changed, 12 insertions(+), 7 deletions(-)

diff --git a/tools/perf/util/annotate-arch/annotate-x86.c b/tools/perf/util/annotate-arch/annotate-x86.c
index a975c39c3d6ce008..94c45806029ee670 100644
--- a/tools/perf/util/annotate-arch/annotate-x86.c
+++ b/tools/perf/util/annotate-arch/annotate-x86.c
@@ -341,7 +341,7 @@ static void update_insn_state_x86(struct type_state *state,
tsr->lifetime_end = 0;

if (src->imm)
- imm_value = src->offset;
+ imm_value = src->imm_value;
else if (has_reg_type(state, src->reg1) &&
state->regs[src->reg1].kind == TSR_KIND_CONST)
imm_value = state->regs[src->reg1].imm_value;
@@ -408,7 +408,7 @@ static void update_insn_state_x86(struct type_state *state,
tsr->lifetime_end = 0;

if (src->imm)
- imm_value = src->offset;
+ imm_value = src->imm_value;
else if (has_reg_type(state, src->reg1) &&
state->regs[src->reg1].kind == TSR_KIND_CONST)
imm_value = state->regs[src->reg1].imm_value;
@@ -589,7 +589,7 @@ static void update_insn_state_x86(struct type_state *state,

if (src->imm) {
tsr->kind = TSR_KIND_CONST;
- tsr->imm_value = (s64)src->offset;
+ tsr->imm_value = src->imm_value;
tsr->offset = 0;
tsr->ok = true;

@@ -967,8 +967,11 @@ static void extract_op_location_x86(const struct arch *arch,
if (*s == arch->objdump.register_char) {
op_loc->reg1 = arch__dwarf_regnum(arch, s);
} else if (*s == arch->objdump.imm_char) {
- op_loc->offset = strtol(s + 1, &p, 0);
- if (p && p != s + 1)
+ const char *start = s + 1;
+ u64 imm = strtoull(start, &p, 0);
+
+ op_loc->imm_value = imm;
+ if (p && p != start)
op_loc->imm = true;
} else if (*s == '-' || strstarts(s, "0x")) {
u64 offset = strtoull(s, NULL, 0);
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
index 58adf7f84146be5f..71a616b5ed1e1358 100644
--- a/tools/perf/util/annotate.h
+++ b/tools/perf/util/annotate.h
@@ -530,7 +530,8 @@ int arch__dwarf_regnum(const struct arch *arch, const char *str);
* struct annotated_op_loc - Location info of instruction operand
* @reg1: First register in the operand
* @reg2: Second register in the operand
- * @offset: Memory access offset in the operand
+ * @imm_value: Immediate value of an x86 operand
+ * @offset: Memory access offset, or an ARM64 or x86 %gs: immediate
* @segment: Segment selector register
* @addr_mode: Addressing mode, only valid if @mem_ref is true
* @extend_type: Operand extension type (enum annotated_ext_type)
@@ -538,11 +539,12 @@ int arch__dwarf_regnum(const struct arch *arch, const char *str);
* @amount: Number of bits to shift
* @mem_ref: Whether the operand accesses memory
* @multi_regs: Whether the second register is used
- * @imm: Whether the operand is an immediate value (in offset)
+ * @imm: Whether the operand is an immediate value
*/
struct annotated_op_loc {
int reg1;
int reg2;
+ s64 imm_value;
int offset;
u8 segment;
u8 addr_mode;
--
2.56.0