From patchwork Fri Nov 8 18:05:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 13868816 Received: from 66-220-155-179.mail-mxout.facebook.com (66-220-155-179.mail-mxout.facebook.com [66.220.155.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FD9C233D6E for ; Fri, 8 Nov 2024 18:05:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=66.220.155.179 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731089136; cv=none; b=VKGiacLyTH5+D/6XNf0xk4EI9bKrVqflgKzim6GQTGMdx56cVIhL/kiJQmsKHRKcs5X7NcidVtPp147RjfimRML+6wWqVbOvy2EaeqUgaJeulmwE9Mrbpp6+mUIKARezxhdmiwpWxBAsEy8gs8UYpmeaN7ATfjwnx2LLgWfPxZY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731089136; c=relaxed/simple; bh=cuiy5GYjVBQIbA1G3tU1eosWyRaIYEfuH4DMlYdEvYQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=G4Oyfr0MRpv7Cv6pJ5vQBWDcjxNspgOgG3AuYxiCkjaOIe88KZx0QEkgSp/8z2mVl6/Lw4yaxE1ofvdo9SRFaoeK3vsy2eW0BWNvjyQOWjBzcmZB7vVxDOEV2tCRk+xv0pNea+P7vRjYPyvC/ruKuiFKqOyJxeHMsnAUyVVNoWE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev; spf=fail smtp.mailfrom=linux.dev; arc=none smtp.client-ip=66.220.155.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=linux.dev Received: by devbig309.ftw3.facebook.com (Postfix, from userid 128203) id CBE97ADDC53F; Fri, 8 Nov 2024 10:05:19 -0800 (PST) From: Yonghong Song To: Alan Maguire , Arnaldo Carvalho de Melo , dwarves@vger.kernel.org Cc: Alexei Starovoitov , Andrii Nakryiko , bpf@vger.kernel.org, Daniel Borkmann , kernel-team@fb.com Subject: [PATCH dwarves 2/3] dwarf_loader: Refactor function check_dwarf_locations() Date: Fri, 8 Nov 2024 10:05:19 -0800 Message-ID: <20241108180519.1198396-1-yonghong.song@linux.dev> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20241108180508.1196431-1-yonghong.song@linux.dev> References: <20241108180508.1196431-1-yonghong.song@linux.dev> Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The newly-added function check_dwarf_locations() supports two variants of implementations: elfutils "version < 157" and "version >= 157". The "version < 157" supports one single location (dwarf_getlocation()) and "version >= 157" supports location lists (dwarf_getlocations()). Currently, even for dwarf_getlocations(), only the first location and its first expression is checked. In the subsequent patch, all locations and all expressions may be changed. So this patch refactors the code such that two different implementations (based on elfutils versions) are clearly separated. This makes subsequent change cleaner. No functional change. Signed-off-by: Yonghong Song --- dwarf_loader.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/dwarf_loader.c b/dwarf_loader.c index 4bb9096..e0b8c11 100644 --- a/dwarf_loader.c +++ b/dwarf_loader.c @@ -1160,22 +1160,47 @@ static struct template_parameter_pack *template_parameter_pack__new(Dwarf_Die *d static bool check_dwarf_locations(Dwarf_Attribute *attr, struct parameter *parm, struct cu *cu, int param_idx) { + int expected_reg = cu->register_params[param_idx]; Dwarf_Addr base, start, end; struct location loc; + Dwarf_Op *expr; + if (!parm->has_loc) + return false; + +#if _ELFUTILS_PREREQ(0, 157) /* dwarf_getlocations() handles location lists; here we are * only interested in the first expr. */ - if (parm->has_loc && -#if _ELFUTILS_PREREQ(0, 157) - dwarf_getlocations(attr, 0, &base, &start, &end, + if (dwarf_getlocations(attr, 0, &base, &start, &end, &loc.expr, &loc.exprlen) > 0 && + loc.exprlen != 0) { + expr = loc.expr; + + switch (expr->atom) { + case DW_OP_reg0 ... DW_OP_reg31: + /* mark parameters that use an unexpected + * register to hold a parameter; these will + * be problematic for users of BTF as they + * violate expectations about register + * contents. + */ + if (expected_reg >= 0 && expected_reg != expr->atom) + parm->unexpected_reg = 1; + break; + default: + parm->optimized = 1; + break; + } + + return true; + } + + return false; #else - dwarf_getlocation(attr, &loc.expr, &loc.exprlen) == 0 && -#endif + if (dwarf_getlocation(attr, &loc.expr, &loc.exprlen) == 0 && loc.exprlen != 0) { - int expected_reg = cu->register_params[param_idx]; - Dwarf_Op *expr = loc.expr; + expr = loc.expr; switch (expr->atom) { case DW_OP_reg0 ... DW_OP_reg31: @@ -1197,6 +1222,7 @@ static bool check_dwarf_locations(Dwarf_Attribute *attr, struct parameter *parm, } return false; +#endif } static struct parameter *parameter__new(Dwarf_Die *die, struct cu *cu,