[PATCH dwarves v5 02/11] dwarf_loader: Prescan all parameters with expected registers

Yonghong Song <[email protected]> Sat, 23 May 2026 09:57:23 -0700
Newsgroups org.kernel.vger.dwarves,org.kernel.vger.bpf
Message-ID <[email protected]>
Find expected registers for each parameter so the current
parameter can check the next one to decide what type should be
used. In some cases, based on dwarf locations, a particular
parameter can be optimized. But the compiler may not really
optimize it. In such cases, the original parameter type should
be preserved in order to match the next parameter register.

The following are two examples, all from arm64.
Example 1:
  $ cat t.c
  struct t { long f1; long f2; };
  __attribute__((noinline)) static long foo(struct t a, struct t b, int i)
  {
          return a.f1 + b.f1 + b.f2;
  }
  struct t p1, p2;
  int i;
  int main()
  {
          return (int)foo(p1, p2, i);
  }
  $ clang -O2 -g t.c
  $ llvm-dwarfdump a.out
  ...
  0x00000041:   DW_TAG_subprogram
                  DW_AT_calling_convention        (DW_CC_nocall)
                  DW_AT_type      (0x0000008f "long")
                  ...

  0x00000051:     DW_TAG_formal_parameter
                    DW_AT_location        (indexed (0x0) loclist = 0x00000014:
                       [0x0000000000000740, 0x0000000000000748): DW_OP_reg0 W0, DW_OP_piece 0x8)
                    DW_AT_name    ("a")
                    DW_AT_type    (0x00000077 "t")
                    ...

  0x0000005a:     DW_TAG_formal_parameter
                    DW_AT_location        (indexed (0x1) loclist = 0x0000001c:
                       [0x0000000000000740, 0x000000000000074c): DW_OP_reg2 W2, DW_OP_piece 0x8, DW_OP_reg3 W3, DW_OP_piece 0x8)
                    DW_AT_name    ("b")
                    DW_AT_type    (0x00000077 "t")
                    ...

  0x00000063:     DW_TAG_formal_parameter
                    DW_AT_name    ("i")
                    DW_AT_type    (0x00000027 "int")
                    ...

  0x0000006b:     NULL

In the above, parameter 'a' actually only uses the first 8 byte value, so looks like
it can be optimized. But since the second parameter starts with register W2, it makes
sense to keep the first parameter original type to ensure correct ABI.

Another example from vmlinux dwarf:

  0x0533fd03:   DW_TAG_subprogram
                  DW_AT_calling_convention        (DW_CC_nocall)
                  DW_AT_type      (0x05334dc7 "int")
                  ...

  0x0533fd15:     DW_TAG_formal_parameter
                    DW_AT_name    ("str")
                    DW_AT_type    (0x05335918 "char *")
                    ...

  0x0533fd1f:     DW_TAG_formal_parameter
                    DW_AT_location        (indexed (0x3b) loclist = 0x00eb9d83:
                       [0xffff80008419f2e0, 0xffff80008419f324): DW_OP_reg1 W1
                       [0xffff80008419f324, 0xffff80008419f47c): DW_OP_reg19 W19
                       [0xffff80008419f47c, 0xffff80008419f494): DW_OP_entry_value(DW_OP_reg1 W1), DW_OP_stack_value
                       [0xffff80008419f494, 0xffff80008419f498): DW_OP_reg19 W19)
                    DW_AT_name    ("used")
                    DW_AT_type    (0x05334dc7 "int")
                    ...

In the above, since the second argument has register W1, it makes sense to
keep the type of the first argument to ensure correct ABI.

Without prescan, the above two cases will be rejected for btf due to mismatched
expected registers.

Signed-off-by: Yonghong Song <[email protected]>
---
 dwarf_loader.c | 71 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 70 insertions(+), 1 deletion(-)

diff --git a/dwarf_loader.c b/dwarf_loader.c
index 0bc4fc4..f0ac699 100644
--- a/dwarf_loader.c
+++ b/dwarf_loader.c
@@ -1190,10 +1190,40 @@ static ptrdiff_t __dwarf_getlocations(Dwarf_Attribute *attr,
 	return ret;
 }
 
+/* Max 20 register parameters, considering some parameters may be optimized out.  */
+#define	MAX_PRESCAN_PARAMS	20
+
 struct func_info {
 	bool signature_changed;
+	int nr_params;
+	int param_start_regs[MAX_PRESCAN_PARAMS];
 };
 
+/* Get the first DW_OP_X (should be a register) from a parameter's DW_AT_location. */
+static int parameter__peek_first_reg(Dwarf_Die *die)
+{
+	Dwarf_Attribute attr;
+	if (dwarf_attr(die, DW_AT_location, &attr) == NULL)
+		return -1;
+
+	Dwarf_Addr base, start, end;
+	Dwarf_Op *expr;
+	size_t exprlen;
+	ptrdiff_t offset = 0;
+
+	pthread_mutex_lock(&libdw__lock);
+	offset = __dwarf_getlocations(&attr, offset, &base, &start, &end, &expr, &exprlen);
+	pthread_mutex_unlock(&libdw__lock);
+
+	if (offset <= 0 || exprlen == 0)
+		return -1;
+
+	if (expr[0].atom >= DW_OP_reg0 && expr[0].atom <= DW_OP_reg31)
+		return expr[0].atom;
+
+	return -1;
+}
+
 /* For DW_AT_location 'attr':
  * - if first location is DW_OP_regXX with expected number, return the register;
  *   otherwise save the register for later return
@@ -2425,6 +2455,43 @@ out_enomem:
 	return -ENOMEM;
 }
 
+/* Pre-scan all formal parameters to collect their starting registers.
+ * This allows look-ahead when processing parameters sequentially, so that
+ * a parameter can check the next parameter's register to determine if the
+ * ABI register layout is preserved despite partial optimization.
+ * For example, for a function like below:
+ *  struct t { long f1; long f2; };
+ *  __attribute__((noinline)) static long foo(struct t a, struct t b)
+ *  {
+ *      return a.f1 + b.f1 + b.f2;
+ *  }
+ * If dwarf has parameter 'a' at aarch64 register W0, and 'b' at register W2,
+ * even compiler could optimize 'a' to 'a.f1'. To conform to ABI, the
+ * parameter 'a' will keep 'struct t' type.
+ */
+static void func_info__prescan_params(struct func_info *info, Dwarf_Die *die)
+{
+	Dwarf_Die child;
+	int idx = 0;
+
+	if (!info->signature_changed)
+		return;
+
+	if (!dwarf_haschildren(die) || dwarf_child(die, &child) != 0)
+		return;
+
+	do {
+		if (dwarf_tag(&child) != DW_TAG_formal_parameter)
+			continue;
+		if (idx >= MAX_PRESCAN_PARAMS)
+			break;
+		info->param_start_regs[idx] = parameter__peek_first_reg(&child);
+		idx++;
+	} while (dwarf_siblingof(&child, &child) == 0);
+
+	info->nr_params = idx;
+}
+
 static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu, struct conf_load *conf)
 {
 	struct function *function = function__new(die, cu, conf);
@@ -2434,8 +2501,10 @@ static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu, struc
 		/* For clang, we determine if function signature changes via DW_AT_calling_convention
 		 * set to DW_CC_nocall.
 		 */
-		if (cu->producer_clang)
+		if (cu->producer_clang) {
 			info.signature_changed = function__signature_changed(function, die);
+			func_info__prescan_params(&info, die);
+		}
 
 		if (die__process_function(die, &function->proto, &function->lexblock, cu, conf, &info) != 0) {
 			function__delete(function, cu);
-- 
2.53.0-Meta