From: Yonghong Song <yonghong.song@linux.dev>
To: Alan Maguire <alan.maguire@oracle.com>,
Arnaldo Carvalho de Melo <arnaldo.melo@gmail.com>,
dwarves@vger.kernel.org
Cc: Alexei Starovoitov <ast@kernel.org>,
Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org, kernel-team@fb.com
Subject: [PATCH dwarves v4 02/11] dwarf_loader: Prescan all parameters with expected registers
Date: Wed, 25 Mar 2026 18:31:54 -0700 [thread overview]
Message-ID: <20260326013154.2903247-1-yonghong.song@linux.dev> (raw)
In-Reply-To: <20260326013144.2901265-1-yonghong.song@linux.dev>
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 <yonghong.song@linux.dev>
---
dwarf_loader.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/dwarf_loader.c b/dwarf_loader.c
index c569002..412a73e 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);
@@ -2432,6 +2499,7 @@ static struct tag *die__create_new_function(Dwarf_Die *die, struct cu *cu, struc
if (function != NULL) {
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.52.0
next prev parent reply other threads:[~2026-03-26 1:32 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-03-26 1:31 [PATCH dwarves v4 00/11] pahole: Encode true signatures in kernel BTF Yonghong Song
2026-03-26 1:31 ` [PATCH dwarves v4 01/11] dwarf_loader: Reduce parameter checking with clang DW_AT_calling_convention attr Yonghong Song
2026-03-30 8:31 ` Alan Maguire
2026-03-26 1:31 ` Yonghong Song [this message]
2026-03-26 1:31 ` [PATCH dwarves v4 03/11] dwarf_loader: Handle signatures with dead arguments Yonghong Song
2026-03-30 10:13 ` Alan Maguire
2026-03-26 1:32 ` [PATCH dwarves v4 04/11] dwarf_loader: Refactor initial ret -1 to be macro PARM_DEFAULT_FAIL Yonghong Song
2026-03-26 1:32 ` [PATCH dwarves v4 05/11] dwarf_laoder: Handle locations with DW_OP_fbreg Yonghong Song
2026-03-26 1:32 ` [PATCH dwarves v4 06/11] dwarf_loader: Change exprlen checking condition in parameter__reg() Yonghong Song
2026-03-26 1:32 ` [PATCH dwarves v4 07/11] dwarf_loader: Detect optimized parameters with locations having constant values Yonghong Song
2026-03-26 1:32 ` [PATCH dwarves v4 08/11] dwarf_loader: Check whether two-reg parameter actually use two regs or not Yonghong Song
2026-03-26 1:32 ` [PATCH dwarves v4 09/11] dwarf_loader: Handle expression lists Yonghong Song
2026-03-31 8:04 ` Alan Maguire
2026-03-26 1:33 ` [PATCH dwarves v4 10/11] btf_encoder: Handle optimized parameter properly Yonghong Song
2026-03-26 1:33 ` [PATCH dwarves v4 11/11] tests: Add a few clang true signature tests Yonghong Song
2026-03-27 16:02 ` [PATCH dwarves v4 00/11] pahole: Encode true signatures in kernel BTF Alan Maguire
2026-03-27 19:38 ` Yonghong Song
2026-03-30 9:56 ` Alan Maguire
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260326013154.2903247-1-yonghong.song@linux.dev \
--to=yonghong.song@linux.dev \
--cc=alan.maguire@oracle.com \
--cc=andrii@kernel.org \
--cc=arnaldo.melo@gmail.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=dwarves@vger.kernel.org \
--cc=kernel-team@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox