From nobody Wed Nov 27 16:50:05 2024 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) (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 C799B212D19 for ; Tue, 8 Oct 2024 18:38:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412723; cv=none; b=fbbwCgIpEj/81+8QUvJDq2uDpNJjlNdv9rHHHPgNfcf1XbjBe/TeIfSEB2xpwiRxktPG4n9bWlkhWaWqDX8ATM9vDMxJsyAq6gp8FsF+SZKcbeUW5guCovLV7awNys52xmCyHusqJyZ7gkoeETqwCoFtsqhibepWGY5pNID65nc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412723; c=relaxed/simple; bh=h+mo20l2kp3w3blfl82mawj1OgqJdUYD/CSEIbBqKE0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MJzQ83nFhtRQNl4letZzwrWc1iZ35vsBy7g7RsYyhyDCnBTRWqSIRXCWflH+tM9Xpa2al/Gsddcb2mRjUHAJJGpUMgD3Pea7ah0JGNuQ1nNcwHKTVytqXPZLVErUxk7rpFy63M2OCIjo4YVDS9KETpkWkbGTftu6niwvduwhCjI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uLZigLEx; arc=none smtp.client-ip=209.85.219.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uLZigLEx" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e24a31ad88aso6833700276.1 for ; Tue, 08 Oct 2024 11:38:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412719; x=1729017519; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=BdmcYDJf4HzMC3FDJRvMlqu57C7hVA9DJ7ZvQtm/XmM=; b=uLZigLExR9xkzIbZwGhQVPRh/DlnebPhY0cz5X2Kn4LCnmznQEBeOwW3DvJkFlKOWL qbnXdW7Sshv+ihTfyxV9ykKsmFfLhoYycngiRDRNwaNYqxuUOSMst6g2pkev0m7cNeYe 0v0soc+fLRU5SxhsUm2cideFbxOch9tmqmhl4/cdBsp+jo9yB2mXcfeho76aOj4G5NoT 5soWl5S4mYcTUzRSKUyVh7Dnt3UOrD3WUXQOSR8bK6lcHMZlFKiin39qnknONRqqzr+C 1rLQLdKNLLNxL67GWGxL3gMbMr9eHFJzWttFtPC6yMuWtpMpSUAL9bSNfRB7+qiqDnlS pkPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412719; x=1729017519; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=BdmcYDJf4HzMC3FDJRvMlqu57C7hVA9DJ7ZvQtm/XmM=; b=ORlBultYlFUCGHanU98xbttVr0Zp3SoHxigjEswUXhfyp4X2CDSrBFL4rTh1ojoLAq tT9vj7qeK2R5ESvtQK5TsgAfhzKBW26a/zsBAs9F71hfwi/8cC3QtHL4kZJDLjZvRIfb Zw9veElKXY1iWI6bpy3AVmsqFQgSkcrn38QQvevCOqllavIPtUOgbVhSUr729qNT3b9R J6ZClaWy/fpj2vb27L/bEdyStQ5fezzufauEjI49+rko/QGHTFatNfqBlLrP9VgFtvDf sHEC6SIhwjnxDi3U4D4Cgn2/sI6NqJE7TmZcCvcK8l6TRCMBR8aYNLuhvLoKB3g2Geok wZAg== X-Forwarded-Encrypted: i=1; AJvYcCUqz5Q4L/3mNZix+NZuT/tq5bOVydyEPA6n7twk5IogY0990jIQL+XYzAlfROLyRPYhzy3JqzpSGTQ6OnM=@vger.kernel.org X-Gm-Message-State: AOJu0YwX6W38T662fUK7zH01geBBR/ej+T2LZhytnC06XJHX3Kyfpaap 6A+yaYZJUNSOpup0hD2EQB44qh/0MbWguF80BEFhGdRelmhDd9EAS80HNW0cVlDA2K99b+TyGnx bfIDDYDhHLKiuz3fmJsRkIZEEJg== X-Google-Smtp-Source: AGHT+IEbZK12V8Aiv3LB4P7W05XqgfKMsBp1EQB7oECpU7RR7Lppavsy+sVmq4BJ9oKg1wyunCBraLakHQCNMOatGIw= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a5b:c52:0:b0:e16:68fb:f261 with SMTP id 3f1490d57ef6-e28fe46633amr33276.5.1728412718807; Tue, 08 Oct 2024 11:38:38 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:25 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=9998; i=samitolvanen@google.com; h=from:subject; bh=h+mo20l2kp3w3blfl82mawj1OgqJdUYD/CSEIbBqKE0=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNQpX3sfW/nFfYflG4pXNIsO64MMzNXqa8/P4SnbfE uWVvWnYUcrCIMbBICumyNLydfXW3d+dUl99LpKAmcPKBDKEgYtTACbiHcjI8Ku+915THPtEW24D JpbfCXUTZ2ibHeGa4nflj9qpNt+d/Qz/nVwOHCl4w/Vzgs/N9312LhN5TW9MuOp8qnvefMf59Vw svAA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-22-samitolvanen@google.com> Subject: [PATCH v4 01/19] scripts: move genksyms crc32 implementation to a common include From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To avoid duplication between host programs, move the crc32 code to a shared header file. Suggested-by: Petr Pavlu Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- scripts/genksyms/genksyms.c | 77 +----------------------------- scripts/include/crc32.h | 93 +++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 76 deletions(-) create mode 100644 scripts/include/crc32.h diff --git a/scripts/genksyms/genksyms.c b/scripts/genksyms/genksyms.c index f3901c55df23..2885bbcb9eec 100644 --- a/scripts/genksyms/genksyms.c +++ b/scripts/genksyms/genksyms.c @@ -18,6 +18,7 @@ #include #include =20 +#include #include "genksyms.h" /*----------------------------------------------------------------------*/ =20 @@ -58,82 +59,6 @@ static struct string_list *mk_node(const char *string); static void print_location(void); static void print_type_name(enum symbol_type type, const char *name); =20 -/*----------------------------------------------------------------------*/ - -static const unsigned int crctab32[] =3D { - 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, - 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, - 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, - 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU, - 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U, - 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, - 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, - 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, - 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, - 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU, - 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U, - 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, - 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, - 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, - 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, - 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U, - 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU, - 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, - 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, - 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, - 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, - 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U, - 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U, - 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, - 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, - 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, - 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U, - 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U, - 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U, - 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, - 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, - 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, - 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, - 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU, - 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU, - 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, - 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, - 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, - 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, - 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U, - 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU, - 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, - 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, - 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, - 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, - 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U, - 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U, - 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, - 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, - 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, - 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, - 0x2d02ef8dU -}; - -static unsigned long partial_crc32_one(unsigned char c, unsigned long crc) -{ - return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); -} - -static unsigned long partial_crc32(const char *s, unsigned long crc) -{ - while (*s) - crc =3D partial_crc32_one(*s++, crc); - return crc; -} - -static unsigned long crc32(const char *s) -{ - return partial_crc32(s, 0xffffffff) ^ 0xffffffff; -} - -/*----------------------------------------------------------------------*/ - static enum symbol_type map_to_ns(enum symbol_type t) { switch (t) { diff --git a/scripts/include/crc32.h b/scripts/include/crc32.h new file mode 100644 index 000000000000..06eedd273717 --- /dev/null +++ b/scripts/include/crc32.h @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* + * CRC32 implementation. + * + * Moved from scripts/genksyms/genksyms.c, which has the following + * notice: + * + * Generate kernel symbol version hashes. + * Copyright 1996, 1997 Linux International. + * + * New implementation contributed by Richard Henderson + * Based on original work by Bjorn Ekwall + * + * This file was part of the Linux modutils 2.4.22: moved back into the + * kernel sources by Rusty Russell/Kai Germaschewski. + */ + +#ifndef __CRC32_H +#define __CRC32_H + +static const unsigned int crctab32[] =3D { + 0x00000000U, 0x77073096U, 0xee0e612cU, 0x990951baU, 0x076dc419U, + 0x706af48fU, 0xe963a535U, 0x9e6495a3U, 0x0edb8832U, 0x79dcb8a4U, + 0xe0d5e91eU, 0x97d2d988U, 0x09b64c2bU, 0x7eb17cbdU, 0xe7b82d07U, + 0x90bf1d91U, 0x1db71064U, 0x6ab020f2U, 0xf3b97148U, 0x84be41deU, + 0x1adad47dU, 0x6ddde4ebU, 0xf4d4b551U, 0x83d385c7U, 0x136c9856U, + 0x646ba8c0U, 0xfd62f97aU, 0x8a65c9ecU, 0x14015c4fU, 0x63066cd9U, + 0xfa0f3d63U, 0x8d080df5U, 0x3b6e20c8U, 0x4c69105eU, 0xd56041e4U, + 0xa2677172U, 0x3c03e4d1U, 0x4b04d447U, 0xd20d85fdU, 0xa50ab56bU, + 0x35b5a8faU, 0x42b2986cU, 0xdbbbc9d6U, 0xacbcf940U, 0x32d86ce3U, + 0x45df5c75U, 0xdcd60dcfU, 0xabd13d59U, 0x26d930acU, 0x51de003aU, + 0xc8d75180U, 0xbfd06116U, 0x21b4f4b5U, 0x56b3c423U, 0xcfba9599U, + 0xb8bda50fU, 0x2802b89eU, 0x5f058808U, 0xc60cd9b2U, 0xb10be924U, + 0x2f6f7c87U, 0x58684c11U, 0xc1611dabU, 0xb6662d3dU, 0x76dc4190U, + 0x01db7106U, 0x98d220bcU, 0xefd5102aU, 0x71b18589U, 0x06b6b51fU, + 0x9fbfe4a5U, 0xe8b8d433U, 0x7807c9a2U, 0x0f00f934U, 0x9609a88eU, + 0xe10e9818U, 0x7f6a0dbbU, 0x086d3d2dU, 0x91646c97U, 0xe6635c01U, + 0x6b6b51f4U, 0x1c6c6162U, 0x856530d8U, 0xf262004eU, 0x6c0695edU, + 0x1b01a57bU, 0x8208f4c1U, 0xf50fc457U, 0x65b0d9c6U, 0x12b7e950U, + 0x8bbeb8eaU, 0xfcb9887cU, 0x62dd1ddfU, 0x15da2d49U, 0x8cd37cf3U, + 0xfbd44c65U, 0x4db26158U, 0x3ab551ceU, 0xa3bc0074U, 0xd4bb30e2U, + 0x4adfa541U, 0x3dd895d7U, 0xa4d1c46dU, 0xd3d6f4fbU, 0x4369e96aU, + 0x346ed9fcU, 0xad678846U, 0xda60b8d0U, 0x44042d73U, 0x33031de5U, + 0xaa0a4c5fU, 0xdd0d7cc9U, 0x5005713cU, 0x270241aaU, 0xbe0b1010U, + 0xc90c2086U, 0x5768b525U, 0x206f85b3U, 0xb966d409U, 0xce61e49fU, + 0x5edef90eU, 0x29d9c998U, 0xb0d09822U, 0xc7d7a8b4U, 0x59b33d17U, + 0x2eb40d81U, 0xb7bd5c3bU, 0xc0ba6cadU, 0xedb88320U, 0x9abfb3b6U, + 0x03b6e20cU, 0x74b1d29aU, 0xead54739U, 0x9dd277afU, 0x04db2615U, + 0x73dc1683U, 0xe3630b12U, 0x94643b84U, 0x0d6d6a3eU, 0x7a6a5aa8U, + 0xe40ecf0bU, 0x9309ff9dU, 0x0a00ae27U, 0x7d079eb1U, 0xf00f9344U, + 0x8708a3d2U, 0x1e01f268U, 0x6906c2feU, 0xf762575dU, 0x806567cbU, + 0x196c3671U, 0x6e6b06e7U, 0xfed41b76U, 0x89d32be0U, 0x10da7a5aU, + 0x67dd4accU, 0xf9b9df6fU, 0x8ebeeff9U, 0x17b7be43U, 0x60b08ed5U, + 0xd6d6a3e8U, 0xa1d1937eU, 0x38d8c2c4U, 0x4fdff252U, 0xd1bb67f1U, + 0xa6bc5767U, 0x3fb506ddU, 0x48b2364bU, 0xd80d2bdaU, 0xaf0a1b4cU, + 0x36034af6U, 0x41047a60U, 0xdf60efc3U, 0xa867df55U, 0x316e8eefU, + 0x4669be79U, 0xcb61b38cU, 0xbc66831aU, 0x256fd2a0U, 0x5268e236U, + 0xcc0c7795U, 0xbb0b4703U, 0x220216b9U, 0x5505262fU, 0xc5ba3bbeU, + 0xb2bd0b28U, 0x2bb45a92U, 0x5cb36a04U, 0xc2d7ffa7U, 0xb5d0cf31U, + 0x2cd99e8bU, 0x5bdeae1dU, 0x9b64c2b0U, 0xec63f226U, 0x756aa39cU, + 0x026d930aU, 0x9c0906a9U, 0xeb0e363fU, 0x72076785U, 0x05005713U, + 0x95bf4a82U, 0xe2b87a14U, 0x7bb12baeU, 0x0cb61b38U, 0x92d28e9bU, + 0xe5d5be0dU, 0x7cdcefb7U, 0x0bdbdf21U, 0x86d3d2d4U, 0xf1d4e242U, + 0x68ddb3f8U, 0x1fda836eU, 0x81be16cdU, 0xf6b9265bU, 0x6fb077e1U, + 0x18b74777U, 0x88085ae6U, 0xff0f6a70U, 0x66063bcaU, 0x11010b5cU, + 0x8f659effU, 0xf862ae69U, 0x616bffd3U, 0x166ccf45U, 0xa00ae278U, + 0xd70dd2eeU, 0x4e048354U, 0x3903b3c2U, 0xa7672661U, 0xd06016f7U, + 0x4969474dU, 0x3e6e77dbU, 0xaed16a4aU, 0xd9d65adcU, 0x40df0b66U, + 0x37d83bf0U, 0xa9bcae53U, 0xdebb9ec5U, 0x47b2cf7fU, 0x30b5ffe9U, + 0xbdbdf21cU, 0xcabac28aU, 0x53b39330U, 0x24b4a3a6U, 0xbad03605U, + 0xcdd70693U, 0x54de5729U, 0x23d967bfU, 0xb3667a2eU, 0xc4614ab8U, + 0x5d681b02U, 0x2a6f2b94U, 0xb40bbe37U, 0xc30c8ea1U, 0x5a05df1bU, + 0x2d02ef8dU +}; + +static inline unsigned long partial_crc32_one(unsigned char c, unsigned lo= ng crc) +{ + return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); +} + +static inline unsigned long partial_crc32(const char *s, unsigned long crc) +{ + while (*s) + crc =3D partial_crc32_one(*s++, crc); + return crc; +} + +static inline unsigned long crc32(const char *s) +{ + return partial_crc32(s, 0xffffffff) ^ 0xffffffff; +} + +#endif /* __CRC32_H */ --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) (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 6EC8C212D3B for ; Tue, 8 Oct 2024 18:38:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412723; cv=none; b=aJNKwPOx0tFTym8YFVwVfzNzDh916UHupalb8DHh8+thGgU5fDjUkLLi0GIERXFfeN6Rx+a0Abg74E5ZiVK17NLC5Xt+rf5g05LHNOE2nRMAfh/S5EHXQ7PRZ7Py6qzvIjgFOLvgyypWpKudtzK8e8fZIiQBU6asqXwVgyRxdjs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412723; c=relaxed/simple; bh=eo+R2sBJnv+m6zmCr9UjsnOOCyqB0aRtDd5rSDkYDD8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Ws8i056kKUB1PbaDcny06OmG9KvOX8l32n0Hi6mC3D//hG402FMjnUPDO1OOJFOmTVpQoXo+I6ydMJAR6j1lvHTGk96SSDFRaQIHl3fAHEBjqlDEoMwN4UbM7pjoHkSbywwCcHyo8QrDJ3iVRhFLl/ZGbdOJCuD4hxD/EVGDxvs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=oXFGw465; arc=none smtp.client-ip=209.85.219.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="oXFGw465" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e28edea9af6so1615353276.3 for ; Tue, 08 Oct 2024 11:38:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412720; x=1729017520; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4mOYxux7phJ57C24xHjYVDdst1lbeaQ8YtQCEcOZURM=; b=oXFGw465rCCjoDAJe7fD2FTtbcrK69haMWE5FlItCPj3jTK+SYAktRsI0A/rbkNkJl 9B1dl/vwRE3BpPWAn0m8foOqNfTuVsgTw9k3lyctRjuxW6RHTV9f3ic8EypIErt37dXa 1Q8m7J+YQWEzTDJAOpwrdvOUd5GOSl3Jim/eV+IFcEtUSyPDehSR/XA2GMMTr5OIcjfJ SaIJJaNb7oQluWKoYZKWalyWWs9zb6h6gTRoQ73PEGtG2EJQ8Rtt8vKc/36SqaI2a8Qk kD+2VctcFLWbWPsWUqTGhMz+53zDbBw/bSsEVONbaIZYEfgC7A4JNXNghVJFUNUj9eFH PS5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412720; x=1729017520; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4mOYxux7phJ57C24xHjYVDdst1lbeaQ8YtQCEcOZURM=; b=Z8cKrfd0s3ldZKPpzIginbcuon9OchmqO2CLr/2gdrDAPU+iW5MKlwDyMeoep9n9mv vKUV02GzDxsF2wJVpdXZYSWbKaBzRusfAoeRT7jqMp6EEvO2vBZirM/UhO9MqoJzDHe5 J/xmMpKKn6ADtKCRRWtEtwaVyWXc6iqFdA90V5MTtiBWmHcx3BaraIJa9vlegXNKtcuU ifvJIqyjBz4tyVR22VlpcfO/CreO54WFMabdxij0cU2hM/SYgPRRbJVHn2GQ/fKKokcc Is7RXWRRgtF1+wzGxkV37hfNQE0VczWVDCUQhoQQckGlPAmkS/cg5lu+3EF7f07BKzzF tOOg== X-Forwarded-Encrypted: i=1; AJvYcCVoBoPKbMpCdQT+JJtmCPkBeeUNydcJ965kpnw1Q7GQEwHPFOQ+IgmClJVjUkeYYzNeuWDgrzusgi7icCc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz+i1RMPGh5zNErqEZC0tI6YE95vmsVx+oPaXUFD8oOu99Mc6At M+pFIUHjSJqrSr1h31Nq7pqPRfwrzgh3yjRFPUpno3csU2/xuN5Gks6+T67XJAtBv4bZtrPTcBI /R6l4VkVj3n52Mke1Qx8Ls3k+4A== X-Google-Smtp-Source: AGHT+IEjOmD/ny7LxZkSR1lU2BjEtLi/IZV0CgcW3UIR7JYU+gd4v9F3fU7wjm6TlX6DjkneDCKcmkDU89MzYFcPdVc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a25:ed0b:0:b0:e17:8e73:866c with SMTP id 3f1490d57ef6-e28fe421ddfmr75276.10.1728412720467; Tue, 08 Oct 2024 11:38:40 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:26 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=14725; i=samitolvanen@google.com; h=from:subject; bh=eo+R2sBJnv+m6zmCr9UjsnOOCyqB0aRtDd5rSDkYDD8=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNYpCQUr7DucuZTJd98ams+OZfaBd/Qbhlun7PznzS j6Nls3vKGVhEONgkBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABPxMWZkmD/513W7o68D836d ERBunB2iyiLJvPlF9FUFljUPNb+7/2b4n9Pzq6Mn2bwjSMtQreqrzebiK9UnDQ3OKtcWucx+0vu LCQA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-23-samitolvanen@google.com> Subject: [PATCH v4 02/19] tools: Add gendwarfksyms From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a basic DWARF parser, which uses libdw to traverse the debugging information in an object file and looks for functions and variables. In follow-up patches, this will be expanded to produce symbol versions for CONFIG_MODVERSIONS from DWARF. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- kernel/module/Kconfig | 8 ++ scripts/Makefile | 1 + scripts/gendwarfksyms/.gitignore | 2 + scripts/gendwarfksyms/Makefile | 8 ++ scripts/gendwarfksyms/dwarf.c | 166 ++++++++++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 124 +++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 97 +++++++++++++++ scripts/gendwarfksyms/symbols.c | 83 +++++++++++++ 8 files changed, 489 insertions(+) create mode 100644 scripts/gendwarfksyms/.gitignore create mode 100644 scripts/gendwarfksyms/Makefile create mode 100644 scripts/gendwarfksyms/dwarf.c create mode 100644 scripts/gendwarfksyms/gendwarfksyms.c create mode 100644 scripts/gendwarfksyms/gendwarfksyms.h create mode 100644 scripts/gendwarfksyms/symbols.c diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index 7c6588148d42..f9e5f82fa88b 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -169,6 +169,14 @@ config MODVERSIONS make them incompatible with the kernel you are running. If unsure, say N. =20 +config GENDWARFKSYMS + bool + depends on DEBUG_INFO + # Requires full debugging information, split DWARF not supported. + depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT + # Requires ELF object files. + depends on !LTO + config ASM_MODVERSIONS bool default HAVE_ASM_MODVERSIONS && MODVERSIONS diff --git a/scripts/Makefile b/scripts/Makefile index 6bcda4b9d054..d7fec46d38c0 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -54,6 +54,7 @@ targets +=3D module.lds =20 subdir-$(CONFIG_GCC_PLUGINS) +=3D gcc-plugins subdir-$(CONFIG_MODVERSIONS) +=3D genksyms +subdir-$(CONFIG_GENDWARFKSYMS) +=3D gendwarfksyms subdir-$(CONFIG_SECURITY_SELINUX) +=3D selinux subdir-$(CONFIG_SECURITY_IPE) +=3D ipe =20 diff --git a/scripts/gendwarfksyms/.gitignore b/scripts/gendwarfksyms/.giti= gnore new file mode 100644 index 000000000000..0927f8d3cd96 --- /dev/null +++ b/scripts/gendwarfksyms/.gitignore @@ -0,0 +1,2 @@ +# SPDX-License-Identifier: GPL-2.0 +/gendwarfksyms diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile new file mode 100644 index 000000000000..9f8fec4fd39b --- /dev/null +++ b/scripts/gendwarfksyms/Makefile @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: GPL-2.0 +hostprogs-always-y +=3D gendwarfksyms + +gendwarfksyms-objs +=3D gendwarfksyms.o +gendwarfksyms-objs +=3D dwarf.o +gendwarfksyms-objs +=3D symbols.o + +HOSTLDLIBS_gendwarfksyms :=3D -ldw -lelf diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c new file mode 100644 index 000000000000..81df3e2ad3ae --- /dev/null +++ b/scripts/gendwarfksyms/dwarf.c @@ -0,0 +1,166 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *v= alue) +{ + Dwarf_Attribute da; + + /* dwarf_formref_die returns a pointer instead of an error value. */ + return dwarf_attr(die, id, &da) && dwarf_formref_die(&da, value); +} + +#define DEFINE_GET_STRING_ATTR(attr) \ + static const char *get_##attr##_attr(Dwarf_Die *die) \ + { \ + Dwarf_Attribute da; \ + if (dwarf_attr(die, DW_AT_##attr, &da)) \ + return dwarf_formstring(&da); \ + return NULL; \ + } + +DEFINE_GET_STRING_ATTR(name) +DEFINE_GET_STRING_ATTR(linkage_name) + +static const char *get_symbol_name(Dwarf_Die *die) +{ + const char *name; + + /* rustc uses DW_AT_linkage_name for exported symbols */ + name =3D get_linkage_name_attr(die); + if (!name) + name =3D get_name_attr(die); + + return name; +} + +static bool match_export_symbol(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die *source =3D die; + Dwarf_Die origin; + + /* If the DIE has an abstract origin, use it for type information. */ + if (get_ref_die_attr(die, DW_AT_abstract_origin, &origin)) + source =3D &origin; + + state->sym =3D symbol_get(get_symbol_name(die)); + + /* Look up using the origin name if there are no matches. */ + if (!state->sym && source !=3D die) + state->sym =3D symbol_get(get_symbol_name(source)); + + state->die =3D *source; + return !!state->sym; +} + +/* + * Type string processing + */ +static void process(const char *s) +{ + s =3D s ?: ""; + + if (dump_dies) + fputs(s, stderr); +} + +bool match_all(Dwarf_Die *die) +{ + return true; +} + +int process_die_container(struct state *state, Dwarf_Die *die, + die_callback_t func, die_match_callback_t match) +{ + Dwarf_Die current; + int res; + + res =3D checkp(dwarf_child(die, ¤t)); + while (!res) { + if (match(¤t)) { + /* <0 =3D error, 0 =3D continue, >0 =3D stop */ + res =3D checkp(func(state, ¤t)); + if (res) + return res; + } + + res =3D checkp(dwarf_siblingof(¤t, ¤t)); + } + + return 0; +} + +/* + * Exported symbol processing + */ +static void process_symbol(struct state *state, Dwarf_Die *die, + die_callback_t process_func) +{ + debug("%s", state->sym->name); + check(process_func(state, die)); + if (dump_dies) + fputs("\n", stderr); +} + +static int __process_subprogram(struct state *state, Dwarf_Die *die) +{ + process("subprogram"); + return 0; +} + +static void process_subprogram(struct state *state, Dwarf_Die *die) +{ + process_symbol(state, die, __process_subprogram); +} + +static int __process_variable(struct state *state, Dwarf_Die *die) +{ + process("variable "); + return 0; +} + +static void process_variable(struct state *state, Dwarf_Die *die) +{ + process_symbol(state, die, __process_variable); +} + +static int process_exported_symbols(struct state *unused, Dwarf_Die *die) +{ + int tag =3D dwarf_tag(die); + + switch (tag) { + /* Possible containers of exported symbols */ + case DW_TAG_namespace: + case DW_TAG_class_type: + case DW_TAG_structure_type: + return check(process_die_container( + NULL, die, process_exported_symbols, match_all)); + + /* Possible exported symbols */ + case DW_TAG_subprogram: + case DW_TAG_variable: { + struct state state; + + if (!match_export_symbol(&state, die)) + return 0; + + if (tag =3D=3D DW_TAG_subprogram) + process_subprogram(&state, &state.die); + else + process_variable(&state, &state.die); + + return 0; + } + default: + return 0; + } +} + +void process_cu(Dwarf_Die *cudie) +{ + check(process_die_container(NULL, cudie, process_exported_symbols, + match_all)); +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c new file mode 100644 index 000000000000..1a9be8fa18c8 --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -0,0 +1,124 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include "gendwarfksyms.h" + +/* + * Options + */ + +/* Print debugging information to stderr */ +int debug; +/* Dump DIE contents */ +int dump_dies; + +static void usage(void) +{ + fputs("Usage: gendwarfksyms [options] elf-object-file ... < symbol-list\n= \n" + "Options:\n" + " -d, --debug Print debugging information\n" + " --dump-dies Dump DWARF DIE contents\n" + " -h, --help Print this message\n" + "\n", + stderr); +} + +static int process_module(Dwfl_Module *mod, void **userdata, const char *n= ame, + Dwarf_Addr base, void *arg) +{ + Dwarf_Addr dwbias; + Dwarf_Die cudie; + Dwarf_CU *cu =3D NULL; + Dwarf *dbg; + int res; + + debug("%s", name); + dbg =3D dwfl_module_getdwarf(mod, &dwbias); + + do { + res =3D dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL); + if (res < 0) + error("dwarf_get_units failed: no debugging information?"); + if (res =3D=3D 1) + break; /* No more units */ + + process_cu(&cudie); + } while (cu); + + return DWARF_CB_OK; +} + +static const Dwfl_Callbacks callbacks =3D { + .section_address =3D dwfl_offline_section_address, + .find_debuginfo =3D dwfl_standard_find_debuginfo, +}; + +int main(int argc, char **argv) +{ + unsigned int n; + int opt; + + struct option opts[] =3D { { "debug", 0, NULL, 'd' }, + { "dump-dies", 0, &dump_dies, 1 }, + { "help", 0, NULL, 'h' }, + { 0, 0, NULL, 0 } }; + + while ((opt =3D getopt_long(argc, argv, "dh", opts, NULL)) !=3D EOF) { + switch (opt) { + case 0: + break; + case 'd': + debug =3D 1; + break; + case 'h': + usage(); + return 0; + default: + usage(); + return 1; + } + } + + if (optind >=3D argc) { + usage(); + error("no input files?"); + } + + symbol_read_exports(stdin); + + for (n =3D optind; n < argc; n++) { + Dwfl *dwfl; + int fd; + + fd =3D open(argv[n], O_RDONLY); + if (fd =3D=3D -1) + error("open failed for '%s': %s", argv[n], + strerror(errno)); + + dwfl =3D dwfl_begin(&callbacks); + if (!dwfl) + error("dwfl_begin failed for '%s': %s", argv[n], + dwarf_errmsg(-1)); + + if (!dwfl_report_offline(dwfl, argv[n], argv[n], fd)) + error("dwfl_report_offline failed for '%s': %s", + argv[n], dwarf_errmsg(-1)); + + dwfl_report_end(dwfl, NULL, NULL); + + if (dwfl_getmodules(dwfl, &process_module, NULL, 0)) + error("dwfl_getmodules failed for '%s'", argv[n]); + + dwfl_end(dwfl); + } + + return 0; +} diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h new file mode 100644 index 000000000000..1a10d18f178e --- /dev/null +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -0,0 +1,97 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +#ifndef __GENDWARFKSYMS_H +#define __GENDWARFKSYMS_H + +/* + * Options -- in gendwarfksyms.c + */ +extern int debug; +extern int dump_dies; + +/* + * Output helpers + */ +#define __PREFIX "gendwarfksyms: " +#define __println(prefix, format, ...) \ + fprintf(stderr, prefix __PREFIX "%s: " format "\n", __func__, \ + ##__VA_ARGS__) + +#define debug(format, ...) \ + do { \ + if (debug) \ + __println("", format, ##__VA_ARGS__); \ + } while (0) + +#define warn(format, ...) __println("warning: ", format, ##__VA_ARGS__) +#define error(format, ...) \ + do { \ + __println("error: ", format, ##__VA_ARGS__); \ + exit(1); \ + } while (0) + +/* + * Error handling helpers + */ +#define __check(expr, test) \ + ({ \ + int __res =3D expr; \ + if (test) \ + error("`%s` failed: %d", #expr, __res); \ + __res; \ + }) + +/* Error =3D=3D non-zero values */ +#define check(expr) __check(expr, __res) +/* Error =3D=3D negative values */ +#define checkp(expr) __check(expr, __res < 0) + +/* + * symbols.c + */ + +struct symbol { + const char *name; + struct hlist_node name_hash; +}; + +typedef void (*symbol_callback_t)(struct symbol *, void *arg); + +void symbol_read_exports(FILE *file); +struct symbol *symbol_get(const char *name); + +/* + * dwarf.c + */ + +struct state { + struct symbol *sym; + Dwarf_Die die; +}; + +typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); +typedef bool (*die_match_callback_t)(Dwarf_Die *die); +bool match_all(Dwarf_Die *die); + +int process_die_container(struct state *state, Dwarf_Die *die, + die_callback_t func, die_match_callback_t match); + +void process_cu(Dwarf_Die *cudie); + +#endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c new file mode 100644 index 000000000000..4df685deb9e0 --- /dev/null +++ b/scripts/gendwarfksyms/symbols.c @@ -0,0 +1,83 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +#define SYMBOL_HASH_BITS 15 +static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); + +static unsigned int for_each(const char *name, symbol_callback_t func, + void *data) +{ + struct hlist_node *tmp; + struct symbol *match; + + if (!name || !*name) + return 0; + + hash_for_each_possible_safe(symbol_names, match, tmp, name_hash, + hash_str(name)) { + if (strcmp(match->name, name)) + continue; + + if (func) + func(match, data); + + return 1; + } + + return 0; +} + +static bool is_exported(const char *name) +{ + return for_each(name, NULL, NULL) > 0; +} + +void symbol_read_exports(FILE *file) +{ + struct symbol *sym; + char *line =3D NULL; + char *name =3D NULL; + size_t size =3D 0; + int nsym =3D 0; + + while (getline(&line, &size, file) > 0) { + if (sscanf(line, "%ms\n", &name) !=3D 1) + error("malformed input line: %s", line); + + if (is_exported(name)) { + /* Ignore duplicates */ + free(name); + continue; + } + + sym =3D xcalloc(1, sizeof(struct symbol)); + sym->name =3D name; + + hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); + ++nsym; + + debug("%s", sym->name); + } + + free(line); + debug("%d exported symbols", nsym); +} + +static void get_symbol(struct symbol *sym, void *arg) +{ + struct symbol **res =3D arg; + + *res =3D sym; +} + +struct symbol *symbol_get(const char *name) +{ + struct symbol *sym =3D NULL; + + for_each(name, get_symbol, &sym); + return sym; +} --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 31923213ED0 for ; Tue, 8 Oct 2024 18:38:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412725; cv=none; b=BDCOFjmwZ+vuccFoPj2KdXExQC9ODB65ABPZ+C+wJYGA8FNRhGEk0XK0kVwK8CgB4OJUwHDkMwtHFRyV2amDMEOQ6sPRqZWAIxVXLnRRwq+hC2TP3uNp6ev7n8bR14HiEOcdC7V5DEXa2v35N+QDPfFdfdtZOOPHgJ1iischibs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412725; c=relaxed/simple; bh=0lGBHoCLiG9Acxs3AlKgo1tCJjq8TaMCY9M43AccgSk=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=D2Lfv/+QdRC7LHoWE9aKJlIeBv1VcSRc0NLpDHF/8INAlJ2W7n/cjD9mkzSCdbrXx96NLEX022m7XZTqSuKxg9nkJf6lVJEOWSMriaSFxzAL23p16Z6ZkzQzr+feTb7IESGkDj9eoViO6EEd//Sp5hdPjVdE1NoT7L+H+lUmiZU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=gl0dWXs/; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="gl0dWXs/" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-20b6144cc2aso839885ad.0 for ; Tue, 08 Oct 2024 11:38:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412722; x=1729017522; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=++q9xYQNjqMI7NCqb6FQjA9b6KXYKSoI9fCqXlx+keI=; b=gl0dWXs/3aauN9UMH5AvMq33tgPWj+QKWenzFJLVPDN0QVu4kV0d8aB4qW6JnU3htz mpcSqvM16yn0pp0Ol+NyJp0FxUuwfQ52Wmbj9kNndJK8ki6UVjFa+o3WH0LHTBoy7lRf /R8tEzITFUuAuzpvf0BKdmBxmilxtE5Pc+EJ8YCsyT6OsNlxivglGUXq708w7Cd6uIT1 RgaZXC8Hm2pomWEYEqc/eozrPVnqii6OE4G3oGQ6NKc2fN29XkYHQB40irV8rsOEHXPS LpYcNqul8QFN4PxARxIyszLYWVQrp0LQ/SmEfeU4iyk7yH5D6/ZlVrczihau5S9Hq2pu SG5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412722; x=1729017522; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=++q9xYQNjqMI7NCqb6FQjA9b6KXYKSoI9fCqXlx+keI=; b=PgUTty1fkkd87nzCzK2MVuovVgUMCgu3inMsdxbd/s2ym7B46ietn+516J1y1k8ehe ocu+CtqG/8MJ8F8ue6U83KatuADPhrp0ed+1QaFw8sSKYY9WMxNcJvnrH83aFBTq8tkz s89f3C5gazXFnaHsrnNVHEELWRqL3cKjD2G1N2tYThUEX8TTF68tshX0brMNcra7jRbl 1hW3tK1kupTLBfloFaTMK7RppnWHcyGJRjFymewTXF/OHIcNSG/kbKhvGlEDPwn7+j7T vc3basFga/VEvUNy1HuHOclslGdH6txksxOQO7Fl9x70FcGF1GB8OlCvf6sjTpvB1Ik6 vBDA== X-Forwarded-Encrypted: i=1; AJvYcCXzjSM2TNPdpeKJQepasjlNXqu3UAKMm/SEnpz+W+BL9cfZVydrDOFQ6kj5ranujl0ibTCh3OZcA2pwlgc=@vger.kernel.org X-Gm-Message-State: AOJu0Yz3a98BnCji5OKrz/H4jCjZJ48CiOxDpHc7Wv7pIUXwHuWzf9Pz uKrslVcgvRtc8WMLPCCmVwNdl609Hv+9V64kgFq0eU+dpHJOYT6Yx2oR2E4mIqkfocIVHIcd+SG 6Pjq4iPYjg6NyrBFkm9sTfuG8Xw== X-Google-Smtp-Source: AGHT+IHgBcTNp6rWi0Ni7hSopmcFbaWgQhJOHl/IF8aJ1tnShtxifRPktwBmOod2lusXthZJBtQICU4F3TBg9cRCiKU= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:903:2343:b0:20b:6a57:bf36 with SMTP id d9443c01a7336-20c63183cd8mr1145ad.2.1728412722170; Tue, 08 Oct 2024 11:38:42 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:27 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=6809; i=samitolvanen@google.com; h=from:subject; bh=0lGBHoCLiG9Acxs3AlKgo1tCJjq8TaMCY9M43AccgSk=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNYp7m6wiFleV8NwQ3t/xhCHs8l7JB38F+NaEpOVNN f73b3ZbRykLgxgHg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZgIw15GhjX3d7zcMIFR0PCh TR5bq9ybRx0XrTQW/Ht+bdVNrscJIk4M/yzsDz1y71Xslft6pOX9Iu98W+aNLf4n1dOqBJ5rzOO IYAcA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-24-samitolvanen@google.com> Subject: [PATCH v4 03/19] gendwarfksyms: Add address matching From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler may choose not to emit type information in DWARF for all aliases, but it's possible for each alias to be exported separately. To ensure we find type information for the aliases as well, read {section, address} tuples from the symbol table and match symbols also by address. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- scripts/gendwarfksyms/gendwarfksyms.c | 2 + scripts/gendwarfksyms/gendwarfksyms.h | 13 +++ scripts/gendwarfksyms/symbols.c | 148 ++++++++++++++++++++++++++ 3 files changed, 163 insertions(+) diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index 1a9be8fa18c8..6fb12f9f6023 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -103,6 +103,8 @@ int main(int argc, char **argv) error("open failed for '%s': %s", argv[n], strerror(errno)); =20 + symbol_read_symtab(fd); + dwfl =3D dwfl_begin(&callbacks); if (!dwfl) error("dwfl_begin failed for '%s': %s", argv[n], diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 1a10d18f178e..a058647e2361 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -66,14 +66,27 @@ extern int dump_dies; * symbols.c */ =20 +static inline unsigned int addr_hash(uintptr_t addr) +{ + return hash_ptr((const void *)addr); +} + +struct symbol_addr { + uint32_t section; + Elf64_Addr address; +}; + struct symbol { const char *name; + struct symbol_addr addr; + struct hlist_node addr_hash; struct hlist_node name_hash; }; =20 typedef void (*symbol_callback_t)(struct symbol *, void *arg); =20 void symbol_read_exports(FILE *file); +void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); =20 /* diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c index 4df685deb9e0..6cb99b8769ea 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -6,8 +6,39 @@ #include "gendwarfksyms.h" =20 #define SYMBOL_HASH_BITS 15 + +/* struct symbol_addr -> struct symbol */ +static HASHTABLE_DEFINE(symbol_addrs, 1 << SYMBOL_HASH_BITS); +/* name -> struct symbol */ static HASHTABLE_DEFINE(symbol_names, 1 << SYMBOL_HASH_BITS); =20 +static inline unsigned int symbol_addr_hash(const struct symbol_addr *addr) +{ + return hash_32(addr->section ^ addr_hash(addr->address)); +} + +static unsigned int __for_each_addr(struct symbol *sym, symbol_callback_t = func, + void *data) +{ + struct hlist_node *tmp; + struct symbol *match =3D NULL; + unsigned int processed =3D 0; + + hash_for_each_possible_safe(symbol_addrs, match, tmp, addr_hash, + symbol_addr_hash(&sym->addr)) { + if (match =3D=3D sym) + continue; /* Already processed */ + + if (match->addr.section =3D=3D sym->addr.section && + match->addr.address =3D=3D sym->addr.address) { + func(match, data); + ++processed; + } + } + + return processed; +} + static unsigned int for_each(const char *name, symbol_callback_t func, void *data) { @@ -22,9 +53,13 @@ static unsigned int for_each(const char *name, symbol_ca= llback_t func, if (strcmp(match->name, name)) continue; =20 + /* Call func for the match, and all address matches */ if (func) func(match, data); =20 + if (match->addr.section !=3D SHN_UNDEF) + return __for_each_addr(match, func, data) + 1; + return 1; } =20 @@ -56,6 +91,7 @@ void symbol_read_exports(FILE *file) =20 sym =3D xcalloc(1, sizeof(struct symbol)); sym->name =3D name; + sym->addr.section =3D SHN_UNDEF; =20 hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); ++nsym; @@ -81,3 +117,115 @@ struct symbol *symbol_get(const char *name) for_each(name, get_symbol, &sym); return sym; } + +typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, + Elf32_Word xndx, void *arg); + +static void elf_for_each_global(int fd, elf_symbol_callback_t func, void *= arg) +{ + size_t sym_size; + GElf_Shdr shdr_mem; + GElf_Shdr *shdr; + Elf_Data *xndx_data =3D NULL; + Elf_Scn *scn; + Elf *elf; + + if (elf_version(EV_CURRENT) !=3D EV_CURRENT) + error("elf_version failed: %s", elf_errmsg(-1)); + + elf =3D elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) + error("elf_begin failed: %s", elf_errmsg(-1)); + + scn =3D elf_nextscn(elf, NULL); + + while (scn) { + shdr =3D gelf_getshdr(scn, &shdr_mem); + + if (shdr && shdr->sh_type =3D=3D SHT_SYMTAB_SHNDX) { + xndx_data =3D elf_getdata(scn, NULL); + break; + } + + scn =3D elf_nextscn(elf, scn); + } + + sym_size =3D gelf_fsize(elf, ELF_T_SYM, 1, EV_CURRENT); + scn =3D elf_nextscn(elf, NULL); + + while (scn) { + shdr =3D gelf_getshdr(scn, &shdr_mem); + + if (shdr && shdr->sh_type =3D=3D SHT_SYMTAB) { + Elf_Data *data =3D elf_getdata(scn, NULL); + unsigned int nsyms; + unsigned int n; + + if (shdr->sh_entsize !=3D sym_size) + error("expected sh_entsize (%lu) to be %zu", + shdr->sh_entsize, sym_size); + + nsyms =3D shdr->sh_size / shdr->sh_entsize; + + for (n =3D 1; n < nsyms; ++n) { + const char *name =3D NULL; + Elf32_Word xndx =3D 0; + GElf_Sym sym_mem; + GElf_Sym *sym; + + sym =3D gelf_getsymshndx(data, xndx_data, n, + &sym_mem, &xndx); + + if (!sym || + GELF_ST_BIND(sym->st_info) =3D=3D STB_LOCAL) + continue; + + if (sym->st_shndx !=3D SHN_XINDEX) + xndx =3D sym->st_shndx; + + name =3D elf_strptr(elf, shdr->sh_link, + sym->st_name); + + /* Skip empty symbol names */ + if (name && *name) + func(name, sym, xndx, arg); + } + } + + scn =3D elf_nextscn(elf, scn); + } + + check(elf_end(elf)); +} + +static void set_symbol_addr(struct symbol *sym, void *arg) +{ + struct symbol_addr *addr =3D arg; + + if (sym->addr.section =3D=3D SHN_UNDEF) { + sym->addr =3D *addr; + hash_add(symbol_addrs, &sym->addr_hash, + symbol_addr_hash(&sym->addr)); + + debug("%s -> { %u, %lx }", sym->name, sym->addr.section, + sym->addr.address); + } else if (sym->addr.section !=3D addr->section || + sym->addr.address !=3D addr->address) { + warn("multiple addresses for symbol %s?", sym->name); + } +} + +static void elf_set_symbol_addr(const char *name, GElf_Sym *sym, + Elf32_Word xndx, void *arg) +{ + struct symbol_addr addr =3D { .section =3D xndx, .address =3D sym->st_val= ue }; + + /* Set addresses for exported symbols */ + if (addr.section !=3D SHN_UNDEF) + for_each(name, set_symbol_addr, &addr); +} + +void symbol_read_symtab(int fd) +{ + elf_for_each_global(fd, elf_set_symbol_addr, NULL); +} --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 CF15D213EF8 for ; Tue, 8 Oct 2024 18:38:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412727; cv=none; b=eSJA3gJuUTbXS8ZyNq7BDrcilgOKSLWORvgXGQHAoQsCSmNOERVhzRMO+llzYDLVfIa7dNBsxcMAOZZQ5KX6O2WhsA0KvRUqkOckLH5xL+pqlS8nEMtl1hHz6Irq5MjXPXiuwIUxAtAg8F267eiOqswXL1TFnnh3XyOfjZaWzyM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412727; c=relaxed/simple; bh=L8gZOiNafqMVcV01G0lHB854teeObYaONcRZ1bZVn3Y=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=pLa8tcpjbz+FcZATcff+RhO5Cbs4vQIIhW+aqoq57DaxvIWqCIoHp+xOXB4/sIxFgLwQf0FNsSsz0q+jh1xW+/atBHnAK2PEdwkFUOQUO2ROPrt+Ov1Cg2qUK7KDSN3DCB1cFxyNu7n+v0SIMlp7TW+JKghDxx6RG1eIDbEdWJk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=uXWk762Y; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="uXWk762Y" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-20bb491189aso77152235ad.3 for ; Tue, 08 Oct 2024 11:38:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412724; x=1729017524; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MkH5Ih0xNj3RgiCeSysh2t3eMUNLsppdxz7owK02bHI=; b=uXWk762YSg4lcvdVkuzlgrGPC5GlWv8rBj4h77aGjQ0q+v+Q3Xnhu6aSQu5P5covQ9 Gs7aBvo42FE5AdlzwfahL+SKvNhy4iKccBdDpq8K8Uos5DqbytuGc9SGSXHgvIgfmJzf M5520n1VR28Q9amiMPEV+JyadIDuqmjoXbEi3SqmjgNr7cnpP/IgkY0V4kNMFXlbITRR HQvG3+E6mZlrPR5NbKih6aEDcz/eT2zrNOa1zYSlOFOuV4VKwzu0Fg8ymOO7QAORX/vw LNKu4lo2pNW0KHLkM8gY+CRMa/BIq1rQKq94eFPQHiprNd8GTpz6xjT3GhZ1LkX+aVzE +tZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412724; x=1729017524; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MkH5Ih0xNj3RgiCeSysh2t3eMUNLsppdxz7owK02bHI=; b=JCxN0Yk6T6lkk5nMXnDTMBhujXN2V1sNtFgqZQy3GVzodkoermNnM+6oREMtjq72MY B6BcUFAnwGxV8fgeqmMeLG9POiNJIQ28xPbgoho/SAD2G9/1+6seo+gqJ5XCHZVJdfX0 RmEcNqcur9kcmFYqJvzCQnytqQ7YUC37Q5Khruce7xzyUFMiWzeSESSmkoVdx0lh1+99 NOH2PABCY2iTW05Q2XvHWd1IWGlFpKssNJmSLzuVP4wgMXnjRJilZXVL2E8c7KOxtunB c8XrDRb8NVPBUfDYUIr2blvK1PalvMAPVVFB4PqYylxDaJyPwGEBeSUZuDvDMVwa716a nxBw== X-Forwarded-Encrypted: i=1; AJvYcCWkgedVC1/LkVxI8sb7jOT7FYSe7yPkJO1Of3fZp4CK9wKMQVJlQe64awOhtsQFim88F9rCrCJ0pE6vVYc=@vger.kernel.org X-Gm-Message-State: AOJu0YyahynSMmduwAPZ2PP8pD0VOA6bAyIX4jsqqbYLOYXjp/O4z1g1 cfMjaD3ucxYvVEZ/lekHE58vLkoL5eY7eR8EFQnajIDfyV57GN08zSeMkCT48BvaUhkiAaLq28J TlLPgH6h4wmQLJIkSJIrDJOaMXA== X-Google-Smtp-Source: AGHT+IFmEULt/jO3vqjILkmVX45b4prHh+Y47lGGy6+F3tp14h2lmNPgyIt35LgeXNCkncMExOSiIlEpJkHiZLFfzWc= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:c404:b0:20b:b7b2:b6ef with SMTP id d9443c01a7336-20bfe0628c0mr4719855ad.4.1728412724086; Tue, 08 Oct 2024 11:38:44 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:28 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5424; i=samitolvanen@google.com; h=from:subject; bh=L8gZOiNafqMVcV01G0lHB854teeObYaONcRZ1bZVn3Y=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNUprA7ZfPDfpwdpNx/Mfb/FK6tu1cJ+r7LQ5lt2HY h/fO1ad21HKwiDGwSArpsjS8nX11t3fnVJffS6SgJnDygQyhIGLUwAm0vOO4Z/x1+Z1mozzFI9G c/IVlXFm31H9zN7MPeNG+q3H5pn7Fm1kZLj64GhzhfvR7mQFIxbuoL/6Hh+3VfxN/XJh88WuZ3P tmVkA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-25-samitolvanen@google.com> Subject: [PATCH v4 04/19] gendwarfksyms: Expand base_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Start making gendwarfksyms more useful by adding support for expanding DW_TAG_base_type types and basic DWARF attributes. Example: $ echo loops_per_jiffy | \ scripts/gendwarfksyms/gendwarfksyms \ --debug --dump-dies vmlinux.o ... gendwarfksyms: process_symbol: loops_per_jiffy variable base_type unsigned long byte_size(8) encoding(7) ... Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 159 ++++++++++++++++++++++++++++++++++ 1 file changed, 159 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 81df3e2ad3ae..35fd1dfeeadc 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -3,8 +3,20 @@ * Copyright (C) 2024 Google LLC */ =20 +#include #include "gendwarfksyms.h" =20 +#define DEFINE_GET_ATTR(attr, type) \ + static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \ + type *value) \ + { \ + Dwarf_Attribute da; \ + return dwarf_attr(die, id, &da) && \ + !dwarf_form##attr(&da, value); \ + } + +DEFINE_GET_ATTR(udata, Dwarf_Word) + static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *v= alue) { Dwarf_Attribute da; @@ -67,6 +79,109 @@ static void process(const char *s) fputs(s, stderr); } =20 +#define MAX_FMT_BUFFER_SIZE 128 + +static void process_fmt(const char *fmt, ...) +{ + char buf[MAX_FMT_BUFFER_SIZE]; + va_list args; + + va_start(args, fmt); + + if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >=3D sizeof(buf)) + error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); + + process(buf); + va_end(args); +} + +#define MAX_FQN_SIZE 64 + +/* Get a fully qualified name from DWARF scopes */ +static char *get_fqn(Dwarf_Die *die) +{ + const char *list[MAX_FQN_SIZE]; + Dwarf_Die *scopes =3D NULL; + bool has_name =3D false; + char *fqn =3D NULL; + char *p; + int count =3D 0; + int len =3D 0; + int res; + int i; + + res =3D checkp(dwarf_getscopes_die(die, &scopes)); + if (!res) { + list[count] =3D get_name_attr(die); + + if (!list[count]) + return NULL; + + len +=3D strlen(list[count]); + count++; + + goto done; + } + + for (i =3D res - 1; i >=3D 0 && count < MAX_FQN_SIZE; i--) { + if (dwarf_tag(&scopes[i]) =3D=3D DW_TAG_compile_unit) + continue; + + list[count] =3D get_name_attr(&scopes[i]); + + if (list[count]) { + has_name =3D true; + } else { + list[count] =3D ""; + has_name =3D false; + } + + len +=3D strlen(list[count]); + count++; + + if (i > 0) { + list[count++] =3D "::"; + len +=3D 2; + } + } + + free(scopes); + + if (count =3D=3D MAX_FQN_SIZE) + warn("increase MAX_FQN_SIZE: reached the maximum"); + + /* Consider the DIE unnamed if the last scope doesn't have a name */ + if (!has_name) + return NULL; +done: + fqn =3D xmalloc(len + 1); + *fqn =3D '\0'; + + p =3D fqn; + for (i =3D 0; i < count; i++) + p =3D stpcpy(p, list[i]); + + return fqn; +} + +static void process_fqn(Dwarf_Die *die) +{ + process(" "); + process(get_fqn(die) ?: ""); +} + +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) = \ + static void process_##attribute##_attr(Dwarf_Die *die) \ + { \ + Dwarf_Word value; \ + if (get_udata_attr(die, DW_AT_##attribute, &value)) \ + process_fmt(" " #attribute "(%" PRIu64 ")", value); \ + } + +DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) +DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) +DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) + bool match_all(Dwarf_Die *die) { return true; @@ -93,6 +208,49 @@ int process_die_container(struct state *state, Dwarf_Di= e *die, return 0; } =20 +static int process_type(struct state *state, Dwarf_Die *die); + +static void process_type_attr(struct state *state, Dwarf_Die *die) +{ + Dwarf_Die type; + + if (get_ref_die_attr(die, DW_AT_type, &type)) { + check(process_type(state, &type)); + return; + } + + /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ + process("base_type void"); +} + +static void process_base_type(struct state *state, Dwarf_Die *die) +{ + process("base_type"); + process_fqn(die); + process_byte_size_attr(die); + process_encoding_attr(die); + process_alignment_attr(die); +} + +#define PROCESS_TYPE(type) \ + case DW_TAG_##type##_type: \ + process_##type##_type(state, die); \ + break; + +static int process_type(struct state *state, Dwarf_Die *die) +{ + int tag =3D dwarf_tag(die); + + switch (tag) { + PROCESS_TYPE(base) + default: + debug("unimplemented type: %x", tag); + break; + } + + return 0; +} + /* * Exported symbol processing */ @@ -119,6 +277,7 @@ static void process_subprogram(struct state *state, Dwa= rf_Die *die) static int __process_variable(struct state *state, Dwarf_Die *die) { process("variable "); + process_type_attr(state, die); return 0; } =20 --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 4BB80215019 for ; Tue, 8 Oct 2024 18:38:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412728; cv=none; b=mEpxLCX621sQQX1BYLmbHOlqIaXGscu+Cfrw7LxZEJEsiwIUa+8s6bDimwgfWpEm3pYWUb74hmHvhEHbH7sSS0UMq1/UDdhZ5on8vomZwxwaYpa22xZ6fOpcTUeDoHyKNgYPChMSKzt/sa71lRq6bymVzlmJSVTWLc8ynL9ht9Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412728; c=relaxed/simple; bh=OG4jYRmeq2OxCAm7eaoPN2WlrBwptnMx6cqOEj+7Jj4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=di/MhXc41AuDCV3gl9MF7DeZDvoFRkzYUWNz4dHYW8Gw8+o4LxKJmdRt/6MOEjxtJqPAOvjAjkcrwGyc0M8aRoE2V7bMZLf//K8Q3rofWv/SuG7O7GrpR2VXLlkCmXlG+yu+el1W9u0lFDzRQrp6wSG2tw296veSQGW5cRsLpQg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Z2vpL4uh; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Z2vpL4uh" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71dff575924so3420947b3a.1 for ; Tue, 08 Oct 2024 11:38:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412726; x=1729017526; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=7WWJPZVdMIN3hgJWOmwaRZdFog5vZGW0/0gQg5D/7SY=; b=Z2vpL4uhgPtXm8cQJtq2OsQygYJ0RzjdRJTN9HyWd/dHqgN7F29kuV3nl/5XR7++3p ZPRSfaulwUMmbHkiQ/CKlkpl1A+trrYC/zxTVSpvZgEpg3wUwY5cRvEgjLfNHjHkyWw1 YHkOSbYav0eX3ywwr0mR3NanT9JdAK9KhzPORvZAQlrauZv3/ITZlJEAjxV6mRMXhRkg /bKojQHPH5Lb5MgkhsmhSVv99OQg5sSoXNCNKv8MJ2WyyWPz1dqG7aJgLj/qqD0riZPG vTSZjJPCJvSXnbGYfaRmbX2YGcWVwh3tAtDl4pPmYdVAU2tsxZtp8qQMKNC4oaJ0+qjC KytA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412726; x=1729017526; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=7WWJPZVdMIN3hgJWOmwaRZdFog5vZGW0/0gQg5D/7SY=; b=ZetJ351Zh6H/mPVsJqH0CgvECJoYCZT/D5kx+8D4iKnF94f9ZCvP/ugZfcVxb0Ujs1 czutLcWqW2GzipJyjgQMculqq8fjiEEMaODOLdHLR2UCsdlXTAo4eDtV6hw5a+v1LAed udVoiQzIYYMe0qe+BEFk+mGp+kGiL3jhayf5DVUmxYU6erxJJDBz2a//51gnrQ6OMadS GXOPW7ojDG2pOLUFsHgbNdoza591HoLetBBwOVwgKdTog/eazOfnVCNH3Yhtv4AsVy0r GHNn9TTvAy9CD2TO5AdwCAJdOpiip1pKj+05q7wnZUQXjLl1XjyvF3q+jk2s9xcZ0Y2Y fYGQ== X-Forwarded-Encrypted: i=1; AJvYcCWN/D6DP5LRMQB09ryYQHqAdh2F9ed2kE3oFIVwmbUJ0M/ax+B8tNDWpZoJiIhG/ktXIiHmOWcuZbXVuOY=@vger.kernel.org X-Gm-Message-State: AOJu0YxeNhdp/tEZEA6zwoJpfWucig43+UnxgoUTCoMYnCaM9oJoV2Gu LcObjkRu9QrLH9BYegYvykmrUHmCV4QrUo5bQ+GYblrslM9aWG5c4xnw80reWVUi2tpA2yKUxor jjSihw36w5QO4E367jSgeKtY8LA== X-Google-Smtp-Source: AGHT+IF09EZVczMWwtjJOc7d6ItMnCmijZ0LPQsAGC2WSz8PkLB/F7Oyetu8/jxLAqyGEmkt0LnxSPe4C2FIparC/WI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6a00:9446:b0:71e:1e8:e337 with SMTP id d2e1a72fcca58-71e01e8eb14mr26928b3a.4.1728412725541; Tue, 08 Oct 2024 11:38:45 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:29 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=15470; i=samitolvanen@google.com; h=from:subject; bh=OG4jYRmeq2OxCAm7eaoPN2WlrBwptnMx6cqOEj+7Jj4=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNUoKSyOUXMX7Jf9XMVUvntoloen66P8ypf3vZlbKb GMQLFXtKGVhEONgkBVTZGn5unrr7u9Oqa8+F0nAzGFlAhnCwMUpABOZyMbI8CzD/+m8+FVCJ33j 9scFpdiyas6LCIveav4ktPdefHxIJ8P/nKvfLh65z9r/3/mgXsoaGbVgno1b/+Qevp+a1m3Z86u JCQA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-26-samitolvanen@google.com> Subject: [PATCH v4 05/19] gendwarfksyms: Add a cache for processed DIEs From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Basic types in DWARF repeat frequently and traversing the DIEs using libdw is relatively slow. Add a simple hashtable based cache for the processed DIEs. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 143 ++++++++++++++++++++++++++ scripts/gendwarfksyms/dwarf.c | 136 +++++++++++++++++------- scripts/gendwarfksyms/gendwarfksyms.c | 6 ++ scripts/gendwarfksyms/gendwarfksyms.h | 63 +++++++++++- 5 files changed, 308 insertions(+), 41 deletions(-) create mode 100644 scripts/gendwarfksyms/die.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 9f8fec4fd39b..c0d4ce50fc27 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -2,6 +2,7 @@ hostprogs-always-y +=3D gendwarfksyms =20 gendwarfksyms-objs +=3D gendwarfksyms.o +gendwarfksyms-objs +=3D die.o gendwarfksyms-objs +=3D dwarf.o gendwarfksyms-objs +=3D symbols.o =20 diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c new file mode 100644 index 000000000000..28d89fce89fc --- /dev/null +++ b/scripts/gendwarfksyms/die.c @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include +#include "gendwarfksyms.h" + +#define DIE_HASH_BITS 20 + +/* {die->addr, state} -> struct die * */ +static HASHTABLE_DEFINE(die_map, 1 << DIE_HASH_BITS); + +static unsigned int map_hits; +static unsigned int map_misses; + +static inline unsigned int die_hash(uintptr_t addr, enum die_state state) +{ + return hash_32(addr_hash(addr) ^ (unsigned int)state); +} + +static void init_die(struct die *cd) +{ + cd->state =3D DIE_INCOMPLETE; + cd->fqn =3D NULL; + cd->tag =3D -1; + cd->addr =3D 0; + INIT_LIST_HEAD(&cd->fragments); +} + +static struct die *create_die(Dwarf_Die *die, enum die_state state) +{ + struct die *cd; + + cd =3D xmalloc(sizeof(struct die)); + init_die(cd); + cd->addr =3D (uintptr_t)die->addr; + + hash_add(die_map, &cd->hash, die_hash(cd->addr, state)); + return cd; +} + +int __die_map_get(uintptr_t addr, enum die_state state, struct die **res) +{ + struct die *cd; + + hash_for_each_possible(die_map, cd, hash, die_hash(addr, state)) { + if (cd->addr =3D=3D addr && cd->state =3D=3D state) { + *res =3D cd; + return 0; + } + } + + return -1; +} + +struct die *die_map_get(Dwarf_Die *die, enum die_state state) +{ + struct die *cd; + + if (__die_map_get((uintptr_t)die->addr, state, &cd) =3D=3D 0) { + map_hits++; + return cd; + } + + map_misses++; + return create_die(die, state); +} + +static void reset_die(struct die *cd) +{ + struct die_fragment *tmp; + struct die_fragment *df; + + list_for_each_entry_safe(df, tmp, &cd->fragments, list) { + if (df->type =3D=3D FRAGMENT_STRING) + free(df->data.str); + free(df); + } + + if (cd->fqn && *cd->fqn) + free(cd->fqn); + init_die(cd); +} + +void die_map_free(void) +{ + struct hlist_node *tmp; + unsigned int stats[DIE_LAST + 1]; + struct die *cd; + int i; + + memset(stats, 0, sizeof(stats)); + + hash_for_each_safe(die_map, cd, tmp, hash) { + stats[cd->state]++; + reset_die(cd); + free(cd); + } + hash_init(die_map); + + if (map_hits + map_misses > 0) + debug("hits %u, misses %u (hit rate %.02f%%)", map_hits, + map_misses, + (100.0f * map_hits) / (map_hits + map_misses)); + + for (i =3D 0; i <=3D DIE_LAST; i++) + debug("%s: %u entries", die_state_name(i), stats[i]); +} + +static struct die_fragment *append_item(struct die *cd) +{ + struct die_fragment *df; + + df =3D xmalloc(sizeof(struct die_fragment)); + df->type =3D FRAGMENT_EMPTY; + list_add_tail(&df->list, &cd->fragments); + return df; +} + +void die_map_add_string(struct die *cd, const char *str) +{ + struct die_fragment *df; + + if (!cd) + return; + + df =3D append_item(cd); + df->data.str =3D xstrdup(str); + df->type =3D FRAGMENT_STRING; +} + +void die_map_add_die(struct die *cd, struct die *child) +{ + struct die_fragment *df; + + if (!cd) + return; + + df =3D append_item(cd); + df->data.addr =3D child->addr; + df->type =3D FRAGMENT_DIE; +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 35fd1dfeeadc..c2cd4743515e 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -71,17 +71,19 @@ static bool match_export_symbol(struct state *state, Dw= arf_Die *die) /* * Type string processing */ -static void process(const char *s) +static void process(struct die *cache, const char *s) { s =3D s ?: ""; =20 if (dump_dies) fputs(s, stderr); + + die_map_add_string(cache, s); } =20 #define MAX_FMT_BUFFER_SIZE 128 =20 -static void process_fmt(const char *fmt, ...) +static void process_fmt(struct die *cache, const char *fmt, ...) { char buf[MAX_FMT_BUFFER_SIZE]; va_list args; @@ -91,7 +93,7 @@ static void process_fmt(const char *fmt, ...) if (checkp(vsnprintf(buf, sizeof(buf), fmt, args)) >=3D sizeof(buf)) error("vsnprintf overflow: increase MAX_FMT_BUFFER_SIZE"); =20 - process(buf); + process(cache, buf); va_end(args); } =20 @@ -164,18 +166,28 @@ static char *get_fqn(Dwarf_Die *die) return fqn; } =20 -static void process_fqn(Dwarf_Die *die) +static void update_fqn(struct die *cache, Dwarf_Die *die) +{ + if (!cache->fqn) + cache->fqn =3D get_fqn(die) ?: ""; +} + +static void process_fqn(struct die *cache, Dwarf_Die *die) { - process(" "); - process(get_fqn(die) ?: ""); + update_fqn(cache, die); + if (*cache->fqn) + process(cache, " "); + process(cache, cache->fqn); } =20 -#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) = \ - static void process_##attribute##_attr(Dwarf_Die *die) \ - { \ - Dwarf_Word value; \ - if (get_udata_attr(die, DW_AT_##attribute, &value)) \ - process_fmt(" " #attribute "(%" PRIu64 ")", value); \ +#define DEFINE_PROCESS_UDATA_ATTRIBUTE(attribute) = \ + static void process_##attribute##_attr(struct die *cache, \ + Dwarf_Die *die) \ + { \ + Dwarf_Word value; \ + if (get_udata_attr(die, DW_AT_##attribute, &value)) \ + process_fmt(cache, " " #attribute "(%" PRIu64 ")", \ + value); \ } =20 DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) @@ -187,8 +199,9 @@ bool match_all(Dwarf_Die *die) return true; } =20 -int process_die_container(struct state *state, Dwarf_Die *die, - die_callback_t func, die_match_callback_t match) +int process_die_container(struct state *state, struct die *cache, + Dwarf_Die *die, die_callback_t func, + die_match_callback_t match) { Dwarf_Die current; int res; @@ -197,7 +210,7 @@ int process_die_container(struct state *state, Dwarf_Di= e *die, while (!res) { if (match(¤t)) { /* <0 =3D error, 0 =3D continue, >0 =3D stop */ - res =3D checkp(func(state, ¤t)); + res =3D checkp(func(state, cache, ¤t)); if (res) return res; } @@ -208,39 +221,78 @@ int process_die_container(struct state *state, Dwarf_= Die *die, return 0; } =20 -static int process_type(struct state *state, Dwarf_Die *die); +static int process_type(struct state *state, struct die *parent, + Dwarf_Die *die); =20 -static void process_type_attr(struct state *state, Dwarf_Die *die) +static void process_type_attr(struct state *state, struct die *cache, + Dwarf_Die *die) { Dwarf_Die type; =20 if (get_ref_die_attr(die, DW_AT_type, &type)) { - check(process_type(state, &type)); + check(process_type(state, cache, &type)); return; } =20 /* Compilers can omit DW_AT_type -- print out 'void' to clarify */ - process("base_type void"); + process(cache, "base_type void"); +} + +static void process_base_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process(cache, "base_type"); + process_fqn(cache, die); + process_byte_size_attr(cache, die); + process_encoding_attr(cache, die); + process_alignment_attr(cache, die); } =20 -static void process_base_type(struct state *state, Dwarf_Die *die) +static void process_cached(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("base_type"); - process_fqn(die); - process_byte_size_attr(die); - process_encoding_attr(die); - process_alignment_attr(die); + struct die_fragment *df; + Dwarf_Die child; + + list_for_each_entry(df, &cache->fragments, list) { + switch (df->type) { + case FRAGMENT_STRING: + process(NULL, df->data.str); + break; + case FRAGMENT_DIE: + if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), + (void *)df->data.addr, &child)) + error("dwarf_die_addr_die failed"); + check(process_type(state, NULL, &child)); + break; + default: + error("empty die_fragment"); + } + } } =20 -#define PROCESS_TYPE(type) \ - case DW_TAG_##type##_type: \ - process_##type##_type(state, die); \ +#define PROCESS_TYPE(type) \ + case DW_TAG_##type##_type: \ + process_##type##_type(state, cache, die); \ break; =20 -static int process_type(struct state *state, Dwarf_Die *die) +static int process_type(struct state *state, struct die *parent, Dwarf_Die= *die) { + struct die *cache; int tag =3D dwarf_tag(die); =20 + /* + * If we have the DIE already cached, use it instead of walking + * through DWARF. + */ + cache =3D die_map_get(die, DIE_COMPLETE); + + if (cache->state =3D=3D DIE_COMPLETE) { + process_cached(state, cache, die); + die_map_add_die(parent, cache); + return 0; + } + switch (tag) { PROCESS_TYPE(base) default: @@ -248,6 +300,11 @@ static int process_type(struct state *state, Dwarf_Die= *die) break; } =20 + /* Update cache state and append to the parent (if any) */ + cache->tag =3D tag; + cache->state =3D DIE_COMPLETE; + die_map_add_die(parent, cache); + return 0; } =20 @@ -258,14 +315,15 @@ static void process_symbol(struct state *state, Dwarf= _Die *die, die_callback_t process_func) { debug("%s", state->sym->name); - check(process_func(state, die)); + check(process_func(state, NULL, die)); if (dump_dies) fputs("\n", stderr); } =20 -static int __process_subprogram(struct state *state, Dwarf_Die *die) +static int __process_subprogram(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("subprogram"); + process(cache, "subprogram"); return 0; } =20 @@ -274,10 +332,11 @@ static void process_subprogram(struct state *state, D= warf_Die *die) process_symbol(state, die, __process_subprogram); } =20 -static int __process_variable(struct state *state, Dwarf_Die *die) +static int __process_variable(struct state *state, struct die *cache, + Dwarf_Die *die) { - process("variable "); - process_type_attr(state, die); + process(cache, "variable "); + process_type_attr(state, cache, die); return 0; } =20 @@ -286,7 +345,8 @@ static void process_variable(struct state *state, Dwarf= _Die *die) process_symbol(state, die, __process_variable); } =20 -static int process_exported_symbols(struct state *unused, Dwarf_Die *die) +static int process_exported_symbols(struct state *unused, struct die *cach= e, + Dwarf_Die *die) { int tag =3D dwarf_tag(die); =20 @@ -296,7 +356,7 @@ static int process_exported_symbols(struct state *unuse= d, Dwarf_Die *die) case DW_TAG_class_type: case DW_TAG_structure_type: return check(process_die_container( - NULL, die, process_exported_symbols, match_all)); + NULL, cache, die, process_exported_symbols, match_all)); =20 /* Possible exported symbols */ case DW_TAG_subprogram: @@ -320,6 +380,6 @@ static int process_exported_symbols(struct state *unuse= d, Dwarf_Die *die) =20 void process_cu(Dwarf_Die *cudie) { - check(process_die_container(NULL, cudie, process_exported_symbols, + check(process_die_container(NULL, NULL, cudie, process_exported_symbols, match_all)); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index 6fb12f9f6023..310cc9257d6e 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -43,6 +43,10 @@ static int process_module(Dwfl_Module *mod, void **userd= ata, const char *name, debug("%s", name); dbg =3D dwfl_module_getdwarf(mod, &dwbias); =20 + /* + * Look for exported symbols in each CU, follow the DIE tree, and add + * the entries to die_map. + */ do { res =3D dwarf_get_units(dbg, cu, &cu, NULL, NULL, &cudie, NULL); if (res < 0) @@ -53,6 +57,8 @@ static int process_module(Dwfl_Module *mod, void **userda= ta, const char *name, process_cu(&cudie); } while (cu); =20 + die_map_free(); + return DWARF_CB_OK; } =20 diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index a058647e2361..da0db7a0c093 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -89,6 +89,61 @@ void symbol_read_exports(FILE *file); void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); =20 +/* + * die.c + */ + +enum die_state { + DIE_INCOMPLETE, + DIE_COMPLETE, + DIE_LAST =3D DIE_COMPLETE +}; + +enum die_fragment_type { + FRAGMENT_EMPTY, + FRAGMENT_STRING, + FRAGMENT_DIE +}; + +struct die_fragment { + enum die_fragment_type type; + union { + char *str; + uintptr_t addr; + } data; + struct list_head list; +}; + +#define CASE_CONST_TO_STR(name) \ + case name: \ + return #name; + +static inline const char *die_state_name(enum die_state state) +{ + switch (state) { + CASE_CONST_TO_STR(DIE_INCOMPLETE) + CASE_CONST_TO_STR(DIE_COMPLETE) + } + + error("unexpected die_state: %d", state); +} + +struct die { + enum die_state state; + char *fqn; + int tag; + uintptr_t addr; + struct list_head fragments; + struct hlist_node hash; +}; + +int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); +struct die *die_map_get(Dwarf_Die *die, enum die_state state); +void die_map_add_string(struct die *pd, const char *str); +void die_map_add_linebreak(struct die *pd, int linebreak); +void die_map_add_die(struct die *pd, struct die *child); +void die_map_free(void); + /* * dwarf.c */ @@ -98,12 +153,14 @@ struct state { Dwarf_Die die; }; =20 -typedef int (*die_callback_t)(struct state *state, Dwarf_Die *die); +typedef int (*die_callback_t)(struct state *state, struct die *cache, + Dwarf_Die *die); typedef bool (*die_match_callback_t)(Dwarf_Die *die); bool match_all(Dwarf_Die *die); =20 -int process_die_container(struct state *state, Dwarf_Die *die, - die_callback_t func, die_match_callback_t match); +int process_die_container(struct state *state, struct die *cache, + Dwarf_Die *die, die_callback_t func, + die_match_callback_t match); =20 void process_cu(Dwarf_Die *cudie); =20 --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) (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 2E1BB1E00A7 for ; Tue, 8 Oct 2024 18:38:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412730; cv=none; b=eW9IKPJHVtT48Au2xpSqd7A4APyhDGjKmy+flB+xAz5CV6ffpFSJpDuaVzd4SBoscL9JF+TQum7PrsyXGAiYmeNSAo13i1Ww5bYl8cb67aWbsb2sm8tiF4cRMLsQOkuxrmoNPFTkyfnOQ6yxzGQz5BlzxeSXSetGqeKFECQuYGk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412730; c=relaxed/simple; bh=xDuML6o4fEs5aHjVcwYcLw3N1kJ6VZ4/BAbuR+YQWeY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=bbuXejCjfzOXVFshSs5FzQHyfvJ+4a9J2nZx5mc8c6qpIrxqm2EX3cO6Lam1bV4kfm7BXRDyKUvqGni+sAsuMpRWFE8KnZermlqLlxQM9AWd1HBfgbfPTtCacgJTa+APBS35XVWQpbSg0fjNaOhX0Ee2ypvRW8jzxf1bA2e1UJA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=16uoj4yf; arc=none smtp.client-ip=209.85.128.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="16uoj4yf" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e30cf0cf1bso30445527b3.0 for ; Tue, 08 Oct 2024 11:38:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412727; x=1729017527; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=UGsxbbl6RhT99UW/xnh7OxspW7cFwqbMTXbe6iYf0Zo=; b=16uoj4yfiL15ocaro/cowrQDye58eBwfLAeIeXIyo5vLXLn4gMNo+4qsU4lo+cMgRa fWlyeJrr642xUu4yM+Dea9Nvm0bEyX5b8M9GhP75UZwoHEwc2MhBQZ1cZ91yqxN7Qk8G DDG8739lajB7c2MiDIprKC3Ror45Xq93Jry4NeOO18nf6910ps6t3uHj8phSbmuyz5O4 5FTl8Z7bZk14TexrVssJFqHa5dB6jdJ8UWEDPslLMJyTtheGklHIV896Au3Cx0fdq8QV vYSTWsx9JT2c+OlQ5tmn7HGvQILz87iqXXcOIy9IpPMhqwoTcWP6TwsPWyGF87Mufqtt Wpxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412727; x=1729017527; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UGsxbbl6RhT99UW/xnh7OxspW7cFwqbMTXbe6iYf0Zo=; b=wxFkq6Tth1tMQVbV9XB0YQtMHjZ7i++NVnTqKLPxm/m85D2i3qtcLzr5uTL6buEclS /cPGaJhai9i8MPPHZzq2TwdMTCbN0HHzDCCJtBP9dWG0hnvvJLB0f62nWPK2Q3wDcTgc ELT8gDF9tAdtpPAQ22nUFG8sjVh9ATeTVN5MYpJDSZ4S/lYReZpKfygy4YMPov/jNmRq Ycdf8Kwkowr8aGZlyl98zp8hmnv4wHX2zPHCoFmR5l6JNs1oqXylxEKqawLmj59jb4nb p/sGXfrhuD9f9JycZ1qJKp8v9gUh1yreUyrPuhU4TvDVPdKhEcuG0WQ5ucMj09YStRjL 3DJQ== X-Forwarded-Encrypted: i=1; AJvYcCWqWw0Jaw1nuatlmxWOMAu6swUNhukygFbov5XPzg9y/MNS3klFwNymVorZR7GhXPj4DO/YB5plAiVBy7Q=@vger.kernel.org X-Gm-Message-State: AOJu0YyyLQiaKpxkwLwSqrCrAGjm5e4VFSYe8zZcNCm1Bl6PVQYdZvUM rWGZlpPxhg19Naj3J+AVpOE2gO/XHRloqDCz4nPNeN7QnusfInWvEJDWyB+CAjRAUwWgsErJzVC If9XrQJSBzG+bJBhhF9qq0pdvQQ== X-Google-Smtp-Source: AGHT+IEMOzIMrtBNEu9h0hc2HfglEyPOd+SSxvH4vUcRXywwLVPqQRBVQ6gzWW7firXx8TmjMsnjwFcjuXMhNaoHU8w= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:690c:4b08:b0:6e3:189a:ad65 with SMTP id 00721157ae682-6e3224c577cmr29017b3.5.1728412727295; Tue, 08 Oct 2024 11:38:47 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:30 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5468; i=samitolvanen@google.com; h=from:subject; bh=xDuML6o4fEs5aHjVcwYcLw3N1kJ6VZ4/BAbuR+YQWeY=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNcoqp6Jc/7xKmOi01IPncq4k13zG840t8k8ajl5IT rV9KMPUUcrCIMbBICumyNLydfXW3d+dUl99LpKAmcPKBDKEgYtTAG7yZ4a/Ih8FNKJkNrluvTip I5PXYulkBWs+tfWu5W1zLhofMkkVY2R4oyfc0L9nY7C99uQbN1Iqtt9X2FpbMDeSOaJ/Ub9ocCI LAA== X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-27-samitolvanen@google.com> Subject: [PATCH v4 06/19] gendwarfksyms: Expand type modifiers and typedefs From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for expanding DWARF type modifiers, such as pointers, const values etc., and typedefs. These types all have DW_AT_type attribute pointing to the underlying type, and thus produce similar output. Also add linebreaks and indentation to debugging output to make it more readable. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/die.c | 12 +++++ scripts/gendwarfksyms/dwarf.c | 67 +++++++++++++++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 5 ++ 3 files changed, 84 insertions(+) diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index 28d89fce89fc..2829387fd815 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -130,6 +130,18 @@ void die_map_add_string(struct die *cd, const char *st= r) df->type =3D FRAGMENT_STRING; } =20 +void die_map_add_linebreak(struct die *cd, int linebreak) +{ + struct die_fragment *df; + + if (!cd) + return; + + df =3D append_item(cd); + df->data.linebreak =3D linebreak; + df->type =3D FRAGMENT_LINEBREAK; +} + void die_map_add_die(struct die *cd, struct die *child) { struct die_fragment *df; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index c2cd4743515e..1d67ee18a388 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -6,6 +6,17 @@ #include #include "gendwarfksyms.h" =20 +static bool do_linebreak; +static int indentation_level; + +/* Line breaks and indentation for pretty-printing */ +static void process_linebreak(struct die *cache, int n) +{ + indentation_level +=3D n; + do_linebreak =3D true; + die_map_add_linebreak(cache, n); +} + #define DEFINE_GET_ATTR(attr, type) \ static bool get_##attr##_attr(Dwarf_Die *die, unsigned int id, \ type *value) \ @@ -75,6 +86,12 @@ static void process(struct die *cache, const char *s) { s =3D s ?: ""; =20 + if (dump_dies && do_linebreak) { + fputs("\n", stderr); + for (int i =3D 0; i < indentation_level; i++) + fputs(" ", stderr); + do_linebreak =3D false; + } if (dump_dies) fputs(s, stderr); =20 @@ -238,6 +255,40 @@ static void process_type_attr(struct state *state, str= uct die *cache, process(cache, "base_type void"); } =20 +/* Container types with DW_AT_type */ +static void __process_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + process(cache, type); + process_fqn(cache, die); + process(cache, " {"); + process_linebreak(cache, 1); + process_type_attr(state, cache, die); + process_linebreak(cache, -1); + process(cache, "}"); + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); +} + +#define DEFINE_PROCESS_TYPE(type) = \ + static void process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + __process_type(state, cache, die, #type "_type"); \ + } + +DEFINE_PROCESS_TYPE(atomic) +DEFINE_PROCESS_TYPE(const) +DEFINE_PROCESS_TYPE(immutable) +DEFINE_PROCESS_TYPE(packed) +DEFINE_PROCESS_TYPE(pointer) +DEFINE_PROCESS_TYPE(reference) +DEFINE_PROCESS_TYPE(restrict) +DEFINE_PROCESS_TYPE(rvalue_reference) +DEFINE_PROCESS_TYPE(shared) +DEFINE_PROCESS_TYPE(volatile) +DEFINE_PROCESS_TYPE(typedef) + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -259,6 +310,9 @@ static void process_cached(struct state *state, struct = die *cache, case FRAGMENT_STRING: process(NULL, df->data.str); break; + case FRAGMENT_LINEBREAK: + process_linebreak(NULL, df->data.linebreak); + break; case FRAGMENT_DIE: if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), (void *)df->data.addr, &child)) @@ -294,7 +348,20 @@ static int process_type(struct state *state, struct di= e *parent, Dwarf_Die *die) } =20 switch (tag) { + /* Type modifiers */ + PROCESS_TYPE(atomic) + PROCESS_TYPE(const) + PROCESS_TYPE(immutable) + PROCESS_TYPE(packed) + PROCESS_TYPE(pointer) + PROCESS_TYPE(reference) + PROCESS_TYPE(restrict) + PROCESS_TYPE(rvalue_reference) + PROCESS_TYPE(shared) + PROCESS_TYPE(volatile) + /* Other types */ PROCESS_TYPE(base) + PROCESS_TYPE(typedef) default: debug("unimplemented type: %x", tag); break; diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index da0db7a0c093..f2be353c3b0b 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -62,6 +62,9 @@ extern int dump_dies; /* Error =3D=3D negative values */ #define checkp(expr) __check(expr, __res < 0) =20 +/* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_typedef_type DW_TAG_typedef + /* * symbols.c */ @@ -102,6 +105,7 @@ enum die_state { enum die_fragment_type { FRAGMENT_EMPTY, FRAGMENT_STRING, + FRAGMENT_LINEBREAK, FRAGMENT_DIE }; =20 @@ -109,6 +113,7 @@ struct die_fragment { enum die_fragment_type type; union { char *str; + int linebreak; uintptr_t addr; } data; struct list_head list; --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) (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 759AD21644B for ; Tue, 8 Oct 2024 18:38:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412732; cv=none; b=PapXP1s6BRphzs8oqbb0iG33NhD5xrgzN8cE/E9C84BGXLU3Y4E06Cb7JvvLIt7pqj4OMIsVsR9/E1mLpIO14Yg1G5iv87tA+vfzGkN2QgncBbCAEqjqvFk1gXXUX34aEzoXRTLJaZfo0NeD2U+Cw5ZbtJKKCqOPfOpQ3LBEFgc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412732; c=relaxed/simple; bh=9gV7/0tGIQjLqh9Fx/jz8DaRmGCazj09za4CP/pU8js=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=alGWvrShSxJoTaAMb8BDV+gSgxOzz79H9A5Z59aLJubROD32D8RtNQByvNoCIJW4D7vNwy5zlWdjeZj6vngPMPe8I6l71kaMvcHRKadXgtGtVs8MN6DvXhDIcmrfHlP/568tiq98LRQpeQSBsXUbsG+erP7tlRtBErV4xD6+TGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=m5i3hcCt; arc=none smtp.client-ip=209.85.216.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="m5i3hcCt" Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2e08c75943cso5796689a91.3 for ; Tue, 08 Oct 2024 11:38:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412730; x=1729017530; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=L/7cUK46GwJQ6x4qnVErUSuR+LuA3HBRtEZoz9lIYWY=; b=m5i3hcCtMYAryMpppz64EFswcL1RMnQNzcLorVup98gJzMhYkLoZJ/qXomjA8yb4No hJMaOOHup7FA8WMcSCCOAUzfj7IuxcNDSZ42x4DRDPo2dA2BAHh6IUWYjGJJMDt9SCXz YOjzU4q/BzfBJKvEk5GJnpBgVoxNltFwm+2vgTHoN/X5ylGR/yOIjfIUAz5ymlux47kY hEm1dZrsfjLYahAeLpo2jc0DoJbBqoBrfKo0kx1HZfVF6lJEMTZ5831D9E2Nyleo3zy2 VwrzX72//pyz/BTgLjhu1/reL+IL3EcqVD5vxj8zc7AYQiupG4W9Th6lpLT7w9aqt8LQ eESw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412730; x=1729017530; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=L/7cUK46GwJQ6x4qnVErUSuR+LuA3HBRtEZoz9lIYWY=; b=SJVRgpW2dstvj0aoxjt/2kzfcF3MTAMU7CBS6NBBFSQqFBVsJwIwak0JzuTL4/sEan WhZnyLc3LRcGZFDpbpEKc48hEIA4RTkLPXWN0qRDwh38LC+IsJbCfd8K7+2sJ5WK37JW 3VJp99DDc0iu+ttE4d7o4w9rWL86gFruFqjcv+LL6Da4ftH5VhX24HjwCuNxaTHr68Tx Yo4xOgjkXTUJUoa4IdWH8EhabC2XLUDOItWahcbdoFeFaKFzf4Rf+Z9ftH2UQF3JF+Wl iPDyhr2Mc5fOmG+6RIT91XfyerijQ+yy3QjSuHUPrQbGhdgS2VsOdTlAXmMfqiwRPvSR KS+w== X-Forwarded-Encrypted: i=1; AJvYcCWmgZuJhs5ZTx96tRr3JWNrZ4Xq4eYdOkk9QrOWCZ42quYE1XXgjHgk/ddLOoLksqn7LUbEUb5EOd/xy90=@vger.kernel.org X-Gm-Message-State: AOJu0YzihlRKunFsrCj05SPtuTIbiG/3j/G4UnAI9vfSu+s/duWQ7xjn 0GF345CX+U83RRJFlv0MPadVtlcF3FMpBQHKqYvUU1femOfEU4R/eT8eX1lLtqTfKdkANe+s8Qy cyV3lXgo42HcbvzlcALv+3jGy1w== X-Google-Smtp-Source: AGHT+IE6LhNdR5oIpsAclCxTYj23wKJXRH88zQ4Is6y5tWPfBQ0Qtjn64WftOc8Vg35khcoJoJyWDcEjmUP+BUTj5jE= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:90a:750:b0:2e0:b26c:9068 with SMTP id 98e67ed59e1d1-2e1e631af4cmr25866a91.4.1728412728856; Tue, 08 Oct 2024 11:38:48 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:31 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5802; i=samitolvanen@google.com; h=from:subject; bh=9gV7/0tGIQjLqh9Fx/jz8DaRmGCazj09za4CP/pU8js=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNcoSvMe/39Vcwps63dC+evPSRc3T7yikJq3csGuh1 82fj9/N7ihlYRDjYJAVU2Rp+bp66+7vTqmvPhdJwMxhZQIZwsDFKQATOfCXkeEqRx+v4WTbFPO4 jS2GttOndRzO71j/7MP/C0zzj2Rte2PGyHCpeEuY67LXUTuVtF5vLp60pnhz6cMFzVICaw58TJ3 nlMYHAA== X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-28-samitolvanen@google.com> Subject: [PATCH v4 07/19] gendwarfksyms: Expand subroutine_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for expanding DW_TAG_subroutine_type and the parameters in DW_TAG_formal_parameter. Use this to also expand subprograms. Example output with --dump-dies: subprogram ( formal_parameter pointer_type { const_type { base_type char byte_size(1) encoding(6) } } ) -> base_type unsigned long byte_size(8) encoding(7) Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 84 ++++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 4 ++ 2 files changed, 85 insertions(+), 3 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 1d67ee18a388..7e6b477d7c12 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -211,6 +211,15 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) =20 +/* Match functions -- die_match_callback_t */ +#define DEFINE_MATCH(type) \ + static bool match_##type##_type(Dwarf_Die *die) \ + { \ + return dwarf_tag(die) =3D=3D DW_TAG_##type##_type; \ + } + +DEFINE_MATCH(formal_parameter) + bool match_all(Dwarf_Die *die) { return true; @@ -223,19 +232,28 @@ int process_die_container(struct state *state, struct= die *cache, Dwarf_Die current; int res; =20 + /* Track the first item in lists. */ + if (state) + state->first_list_item =3D true; + res =3D checkp(dwarf_child(die, ¤t)); while (!res) { if (match(¤t)) { /* <0 =3D error, 0 =3D continue, >0 =3D stop */ res =3D checkp(func(state, cache, ¤t)); if (res) - return res; + goto out; } =20 res =3D checkp(dwarf_siblingof(¤t, ¤t)); } =20 - return 0; + res =3D 0; +out: + if (state) + state->first_list_item =3D false; + + return res; } =20 static int process_type(struct state *state, struct die *parent, @@ -255,6 +273,40 @@ static void process_type_attr(struct state *state, str= uct die *cache, process(cache, "base_type void"); } =20 +static void process_list_comma(struct state *state, struct die *cache) +{ + if (state->first_list_item) { + state->first_list_item =3D false; + } else { + process(cache, " ,"); + process_linebreak(cache, 0); + } +} + +/* Comma-separated with DW_AT_type */ +static void __process_list_type(struct state *state, struct die *cache, + Dwarf_Die *die, const char *type) +{ + const char *name =3D get_name_attr(die); + + process_list_comma(state, cache); + process(cache, type); + process_type_attr(state, cache, die); + if (name) { + process(cache, " "); + process(cache, name); + } +} + +#define DEFINE_PROCESS_LIST_TYPE(type) = \ + static void process_##type##_type(struct state *state, \ + struct die *cache, Dwarf_Die *die) \ + { \ + __process_list_type(state, cache, die, #type " "); \ + } + +DEFINE_PROCESS_LIST_TYPE(formal_parameter) + /* Container types with DW_AT_type */ static void __process_type(struct state *state, struct die *cache, Dwarf_Die *die, const char *type) @@ -289,6 +341,29 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) =20 +static void __process_subroutine_type(struct state *state, struct die *cac= he, + Dwarf_Die *die, const char *type) +{ + process(cache, type); + process(cache, " ("); + process_linebreak(cache, 1); + /* Parameters */ + check(process_die_container(state, cache, die, process_type, + match_formal_parameter_type)); + process_linebreak(cache, -1); + process(cache, ")"); + process_linebreak(cache, 0); + /* Return type */ + process(cache, "-> "); + process_type_attr(state, cache, die); +} + +static void process_subroutine_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + __process_subroutine_type(state, cache, die, "subroutine_type"); +} + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -359,8 +434,11 @@ static int process_type(struct state *state, struct di= e *parent, Dwarf_Die *die) PROCESS_TYPE(rvalue_reference) PROCESS_TYPE(shared) PROCESS_TYPE(volatile) + /* Subtypes */ + PROCESS_TYPE(formal_parameter) /* Other types */ PROCESS_TYPE(base) + PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) default: debug("unimplemented type: %x", tag); @@ -390,7 +468,7 @@ static void process_symbol(struct state *state, Dwarf_D= ie *die, static int __process_subprogram(struct state *state, struct die *cache, Dwarf_Die *die) { - process(cache, "subprogram"); + __process_subroutine_type(state, cache, die, "subprogram"); return 0; } =20 diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index f2be353c3b0b..f31bfcb0dfe7 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -63,6 +63,7 @@ extern int dump_dies; #define checkp(expr) __check(expr, __res < 0) =20 /* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_formal_parameter_type DW_TAG_formal_parameter #define DW_TAG_typedef_type DW_TAG_typedef =20 /* @@ -156,6 +157,9 @@ void die_map_free(void); struct state { struct symbol *sym; Dwarf_Die die; + + /* List expansion */ + bool first_list_item; }; =20 typedef int (*die_callback_t)(struct state *state, struct die *cache, --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 3E8A2217332 for ; Tue, 8 Oct 2024 18:38:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412733; cv=none; b=sgq6DOVGjawDWLQHwEGMRZf9rLGJZBtjcq5R3h5wy47vmP0B0t/yPJdLHxNL5L2GFivQPdoWZRoE39SLMw9yg3lKfUdDqD5r19OeqCg3LGC6ji1/C1GSnm81VzyiE/2TVLWZvKQ6weXeAvjdOqd6vA+Uf0Gzmr/dF+uwess5H30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412733; c=relaxed/simple; bh=mITPyravUpQ/bJzC6y0bi7OfUc6olmVCmSnsnJKAM7Q=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=K5DFmy64DUe8AxmMBFBdQywl4QxUvfkJvDfXDoj9EYlqd3iuZn4uMWvX633zdjL+shg6GI/nfLU2okKkY6eLzQ6/H0KGDcvrv3gffGN2xxRlOF0SLk1Udpfy9K1fI6duWgIuZVMUq4tiwd62Cwl1TJwuO+jifR/yZKNrARDccjM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=tEISiiuW; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="tEISiiuW" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7d4f9974c64so3693540a12.1 for ; Tue, 08 Oct 2024 11:38:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412732; x=1729017532; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=5IN83f2AWDRsPM4oFeAVwXzABTM6F2NmuC5qwi5gGYs=; b=tEISiiuWIUNOtRULQhl0rjZEdoDCXhCf5jCLqDmupCoaKupmgyG3dOyqH4mZAI+RE3 +baTtLHU+0gYSg/frMz+Vs/ahssg3NsGvgc/j2V5lJY4AwrArckK+y4dEqJfF4H7uuXx apGh0MTIcfJBJ3+6PLJ/eEue7zNrBfQo94aGqgi3rVLhvXphvFSoVbcyVcHo3OEHOXZY NF/dsrLW1iVjd0bm1txpCla6oitZOCzHzn3lslFfDiWaB0zW2HWLvzxLF0LsML8vuEm6 cij2s74ILpPYnmO3U22c7q4y8Zi5yGtTAwckE7nWVye2XOaqxNCM9+ENwtFyI2+Y1WlZ eHBQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412732; x=1729017532; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=5IN83f2AWDRsPM4oFeAVwXzABTM6F2NmuC5qwi5gGYs=; b=DMgD8YiGi6aCsOJ5yPFR35BVS8YlfqjfWegT+zCtyOS4649x5v2nb5DyxLwRWnu8QR Ls6ISnV+orrQXbkwz7aHcgiDdn93ZrhbvCpC+Nk6x3kkj2peOL4kJNoOSPuXG/89dJ6d Up8Po1o4EUqWmtjgNZdhyITdj1DZRjitCqWQnXcewfsbRfU4kqay/0wuL1kZ/53JIvz5 lxLYU3Be6scYJpZP9ycTwpjEiCEtxlOuk7USrI6kk/2drTIMj1IOgdIJdiupoZa/ZZAd 62ImnTUTxFhFaJrstLhrlZTOXiF8zbLDL7IFymQyc6SWY4B7/wq1Q8YdxFK0Zwo8ymKA LLxA== X-Forwarded-Encrypted: i=1; AJvYcCUTi4gK3pLU3BDZF4Sz/gdOrJyB+I9rpwAdnb0eWb0EPQisvrs/bZi2Rt6uPOGkBN31ji/+HWE9QFcfqDY=@vger.kernel.org X-Gm-Message-State: AOJu0YwknGfmiJkJ/7TF9oKbSXGo7/d7TN/vBpMjXI9LxUAA2znu9kG2 HbtUcnt7pqQID2vnn9ZEfyhX/UKtzI5w9U4cLyRallrToIG00hy2LfEuANrPgNXU8LWEuNS+RS5 rmSEllVzQD4IpSXsOuSJR2H3SDQ== X-Google-Smtp-Source: AGHT+IFTrAucXWOZ9cZin4iLnAuYNLU0EypeB1Z89LIS9DMR6Ty9nh8TVw3+ALRwnadMOVxAECwEdv5BsoMa/bUsExI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:6247:0:b0:7d5:e48:4286 with SMTP id 41be03b00d2f7-7ea320e1a64mr17a12.7.1728412731505; Tue, 08 Oct 2024 11:38:51 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:32 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2343; i=samitolvanen@google.com; h=from:subject; bh=mITPyravUpQ/bJzC6y0bi7OfUc6olmVCmSnsnJKAM7Q=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNSotQRLVu8W36TyxnBn03Kh8W8wLl9zpO/6c+5uir zeHSyygo5SFQYyDQVZMkaXl6+qtu787pb76XCQBM4eVCWQIAxenAExk5xJGhgW6sTfmOap9F+2d dj/kZfa9k0X8vx0SbpTtlLaVf3TpRTsjw0IVqdWfXtzz9zknuvFfs55LlIl/QB73x//S4bUObNb L+QA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-29-samitolvanen@google.com> Subject: [PATCH v4 08/19] gendwarfksyms: Expand array_type From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for expanding DW_TAG_array_type, and the subrange type indicating array size. Example source code: const char *s[34]; Output with --dump-dies: variable array_type[34] { pointer_type { const_type { base_type char byte_size(1) encoding(6) } } byte_size(8) } Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 7e6b477d7c12..ade9b3b7b119 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -219,6 +219,7 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) } =20 DEFINE_MATCH(formal_parameter) +DEFINE_MATCH(subrange) =20 bool match_all(Dwarf_Die *die) { @@ -341,6 +342,33 @@ DEFINE_PROCESS_TYPE(shared) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) =20 +static void process_subrange_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Word count =3D 0; + + if (get_udata_attr(die, DW_AT_count, &count)) + process_fmt(cache, "[%" PRIu64 "]", count); + else if (get_udata_attr(die, DW_AT_upper_bound, &count)) + process_fmt(cache, "[%" PRIu64 "]", count + 1); + else + process(cache, "[]"); +} + +static void process_array_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process(cache, "array_type"); + /* Array size */ + check(process_die_container(state, cache, die, process_type, + match_subrange_type)); + process(cache, " {"); + process_linebreak(cache, 1); + process_type_attr(state, cache, die); + process_linebreak(cache, -1); + process(cache, "}"); +} + static void __process_subroutine_type(struct state *state, struct die *cac= he, Dwarf_Die *die, const char *type) { @@ -436,7 +464,9 @@ static int process_type(struct state *state, struct die= *parent, Dwarf_Die *die) PROCESS_TYPE(volatile) /* Subtypes */ PROCESS_TYPE(formal_parameter) + PROCESS_TYPE(subrange) /* Other types */ + PROCESS_TYPE(array) PROCESS_TYPE(base) PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 D07A7217915 for ; Tue, 8 Oct 2024 18:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412735; cv=none; b=fJekuuI0y5ZJL9LdWy0dWSddzafThjqR9QWeBBQciM2ejx+dOl9YfJYXqqLe0JyHhnV6NTx1zOSwi6i3jnEpVJREu+6xlH3N+eD/x05Ospuq69DoHWL3Ln8qTvc2S/m6gSimcZpV8dUwDItNVLPYdebp4UNnI+h3zJihIm4oS94= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412735; c=relaxed/simple; bh=nsMG8BmDS1pPJGvMwlDq7GSzLM5mFSBmZQGjooaZEi4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TfhIb5zAIdwPBzZUVNWgEFFQt/TlMJjXQKIVB67NbqNEXfpx6J+K5WBML5mmRT3fQUq8HsrJqdT4AInjwsshPgc0NGQopPJAZCTt1/+YJSDL9Y1cxus7UpA/niIRderpslhBYRRZXm3qbsmyXLHy2sNodoqZI1JemmYbmA6JycE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=AQF1f6N5; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="AQF1f6N5" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71dfeda9ac0so2709456b3a.1 for ; Tue, 08 Oct 2024 11:38:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412733; x=1729017533; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/MFnwrF1jxR+zPWj5478XfhkmmcZPfEIKtDxJzvFfWw=; b=AQF1f6N51hyyD2yZOflFQQQKlILAfF2mcAFnnR5q+blSvC+CtLB4jLHQezmEHQvw2p /NuyEhcQwY+2O3KX3O6mMF558kjdNQ/QJw7sv90UP4FLKKEdq9ASqgamz4/BYdacgNSz Gf59KALc6ffYt+X7s/hyocRzFlcn5zHiRSv1F+CT0PyPcTQdzIzRa11sl2NXOpG6Ev8X ghHmYi1BWY1fBXONqyD80GZsiLELPtcZD6Z3pDNSQw+F3uJEF6OU79lBaKAsr2f79cJo HJn3uV+EYUldPfTdYiD5hucbg9gx/LPXe+/XtjXRbQjdYx/J1LcuI84wYxeDZLVBXXHt Kggg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412733; x=1729017533; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/MFnwrF1jxR+zPWj5478XfhkmmcZPfEIKtDxJzvFfWw=; b=myepu5e+8yv9+tPJZcOGDFzZh0QrBMv1NrGD6cR/QPUlo9oAwN8qSHAsnT/rHSJmm6 buznXd7hs3SqtIaltYULgG4KxH8LJSKOceqE5c/VoymsFXus7C+Hag+SPwLBVEX4SaD5 4OwvoL2TPxoWyVoz8/qgq2s/zELAbPh8vCg+/uWGtx2Jyq3wm5wpAld7IBINU84FAZ2+ HwUjyd1qWT+TG/0eqkAuhbSAMP/7JrJvoo7+QB0kATX+hTiuLdTb/AMxekSGaxIGMhR7 zbITXgIZHsTaQXGSpOrJsLa4Jpa3ai2YNWzvl3IgnJAhTKtXYk+AkdPq+/z76Z8RqEMT D8ww== X-Forwarded-Encrypted: i=1; AJvYcCU6ItwoziXRV6/XkgSH9bQbi4qTPGV7fV2xyKRxdj3L710G1HxodRPys3Fs0439NXRAYL/p7SJwuHvFSAU=@vger.kernel.org X-Gm-Message-State: AOJu0YxZ+FndELTpNr6233rXAPncGVNsFfOdkAbWf8vo85Obqt2u8rDN qSe5mSkHb359sEY3RfwT/ukudgjYJnMwvBqbni91D7fe+ANgfqBn9HNvAi0ICfDQ1JAuTnlLdZY 7D88YIKLtysy7o9bmTrKaTmwYjA== X-Google-Smtp-Source: AGHT+IHJsowxjbX+q1XfBlv8DyC6GRIA7D0odEsehk64lT4TxW8E1rLvEKkG9Ou4X9QXrXLXjNVPLkveiP2yBFqXmBI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6a00:6518:b0:71d:ff10:7c4 with SMTP id d2e1a72fcca58-71dff100856mr14799b3a.4.1728412733086; Tue, 08 Oct 2024 11:38:53 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:33 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=8477; i=samitolvanen@google.com; h=from:subject; bh=nsMG8BmDS1pPJGvMwlDq7GSzLM5mFSBmZQGjooaZEi4=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNSp3d55mXv3+uoLTLy2PafNtt909/PLvzGn6C5f86 RaVk/ll3lHKwiDGwSArpsjS8nX11t3fnVJffS6SgJnDygQyhIGLUwAmcrmMkeHL9BmRuwSF3Hnk zms1ueUenGQR+kJtqvDzR/ZO8xbErtnK8L9+vh2Li63m6qBzod66G6OT1okxunU9dMvSMHd+Kj/ pGR8A X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-30-samitolvanen@google.com> Subject: [PATCH v4 09/19] gendwarfksyms: Expand structure types From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Recursively expand DWARF structure types, i.e. structs, unions, and enums. Also include relevant DWARF attributes in type strings to encode structure layout, for example. Example output with --dump-dies: subprogram ( formal_parameter structure_type &str { member pointer_type { base_type u8 byte_size(1) encoding(7) } data_ptr data_member_location(0) , member base_type usize byte_size(8) encoding(7) length data_member_lo= cation(8) } byte_size(16) alignment(8) msg ) -> base_type void Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 138 +++++++++++++++++++++++++- scripts/gendwarfksyms/gendwarfksyms.h | 5 + 2 files changed, 141 insertions(+), 2 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index ade9b3b7b119..f5cebbdcc212 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -207,9 +207,14 @@ static void process_fqn(struct die *cache, Dwarf_Die *= die) value); \ } =20 +DEFINE_PROCESS_UDATA_ATTRIBUTE(accessibility) DEFINE_PROCESS_UDATA_ATTRIBUTE(alignment) +DEFINE_PROCESS_UDATA_ATTRIBUTE(bit_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(byte_size) DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) +DEFINE_PROCESS_UDATA_ATTRIBUTE(data_bit_offset) +DEFINE_PROCESS_UDATA_ATTRIBUTE(data_member_location) +DEFINE_PROCESS_UDATA_ATTRIBUTE(discr_value) =20 /* Match functions -- die_match_callback_t */ #define DEFINE_MATCH(type) \ @@ -218,7 +223,9 @@ DEFINE_PROCESS_UDATA_ATTRIBUTE(encoding) return dwarf_tag(die) =3D=3D DW_TAG_##type##_type; \ } =20 +DEFINE_MATCH(enumerator) DEFINE_MATCH(formal_parameter) +DEFINE_MATCH(member) DEFINE_MATCH(subrange) =20 bool match_all(Dwarf_Die *die) @@ -297,6 +304,10 @@ static void __process_list_type(struct state *state, s= truct die *cache, process(cache, " "); process(cache, name); } + process_accessibility_attr(cache, die); + process_bit_size_attr(cache, die); + process_data_bit_offset_attr(cache, die); + process_data_member_location_attr(cache, die); } =20 #define DEFINE_PROCESS_LIST_TYPE(type) = \ @@ -307,6 +318,7 @@ static void __process_list_type(struct state *state, st= ruct die *cache, } =20 DEFINE_PROCESS_LIST_TYPE(formal_parameter) +DEFINE_PROCESS_LIST_TYPE(member) =20 /* Container types with DW_AT_type */ static void __process_type(struct state *state, struct die *cache, @@ -339,6 +351,7 @@ DEFINE_PROCESS_TYPE(reference) DEFINE_PROCESS_TYPE(restrict) DEFINE_PROCESS_TYPE(rvalue_reference) DEFINE_PROCESS_TYPE(shared) +DEFINE_PROCESS_TYPE(template_type_parameter) DEFINE_PROCESS_TYPE(volatile) DEFINE_PROCESS_TYPE(typedef) =20 @@ -392,6 +405,107 @@ static void process_subroutine_type(struct state *sta= te, struct die *cache, __process_subroutine_type(state, cache, die, "subroutine_type"); } =20 +static void process_variant_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + process_list_comma(state, cache); + process(cache, "variant {"); + process_linebreak(cache, 1); + check(process_die_container(state, cache, die, process_type, + match_member_type)); + process_linebreak(cache, -1); + process(cache, "}"); + process_discr_value_attr(cache, die); +} + +static void process_variant_part_type(struct state *state, struct die *cac= he, + Dwarf_Die *die) +{ + process_list_comma(state, cache); + process(cache, "variant_part {"); + process_linebreak(cache, 1); + check(process_die_container(state, cache, die, process_type, + match_all)); + process_linebreak(cache, -1); + process(cache, "}"); +} + +static int ___process_structure_type(struct state *state, struct die *cach= e, + Dwarf_Die *die) +{ + switch (dwarf_tag(die)) { + case DW_TAG_member: + case DW_TAG_variant_part: + return check(process_type(state, cache, die)); + case DW_TAG_class_type: + case DW_TAG_enumeration_type: + case DW_TAG_structure_type: + case DW_TAG_template_type_parameter: + case DW_TAG_union_type: + case DW_TAG_subprogram: + /* Skip non-member types, including member functions */ + return 0; + default: + error("unexpected structure_type child: %x", dwarf_tag(die)); + } +} + +static void __process_structure_type(struct state *state, struct die *cach= e, + Dwarf_Die *die, const char *type, + die_callback_t process_func, + die_match_callback_t match_func) +{ + process(cache, type); + process_fqn(cache, die); + process(cache, " {"); + process_linebreak(cache, 1); + + check(process_die_container(state, cache, die, process_func, + match_func)); + + process_linebreak(cache, -1); + process(cache, "}"); + + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); +} + +#define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ + static void process_##structure##_type( \ + struct state *state, struct die *cache, Dwarf_Die *die) \ + { \ + __process_structure_type(state, cache, die, \ + #structure "_type", \ + ___process_structure_type, \ + match_all); \ + } + +DEFINE_PROCESS_STRUCTURE_TYPE(class) +DEFINE_PROCESS_STRUCTURE_TYPE(structure) +DEFINE_PROCESS_STRUCTURE_TYPE(union) + +static void process_enumerator_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Word value; + + process_list_comma(state, cache); + process(cache, "enumerator"); + process_fqn(cache, die); + + if (get_udata_attr(die, DW_AT_const_value, &value)) { + process(cache, " =3D "); + process_fmt(cache, "%" PRIu64, value); + } +} + +static void process_enumeration_type(struct state *state, struct die *cach= e, + Dwarf_Die *die) +{ + __process_structure_type(state, cache, die, "enumeration_type", + process_type, match_enumerator_type); +} + static void process_base_type(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -402,6 +516,16 @@ static void process_base_type(struct state *state, str= uct die *cache, process_alignment_attr(cache, die); } =20 +static void process_unspecified_type(struct state *state, struct die *cach= e, + Dwarf_Die *die) +{ + /* + * These can be emitted for stand-elone assembly code, which means we + * might run into them in vmlinux.o. + */ + process(cache, "unspecified_type"); +} + static void process_cached(struct state *state, struct die *cache, Dwarf_Die *die) { @@ -462,17 +586,27 @@ static int process_type(struct state *state, struct d= ie *parent, Dwarf_Die *die) PROCESS_TYPE(rvalue_reference) PROCESS_TYPE(shared) PROCESS_TYPE(volatile) + /* Container types */ + PROCESS_TYPE(class) + PROCESS_TYPE(structure) + PROCESS_TYPE(union) + PROCESS_TYPE(enumeration) /* Subtypes */ + PROCESS_TYPE(enumerator) PROCESS_TYPE(formal_parameter) + PROCESS_TYPE(member) PROCESS_TYPE(subrange) + PROCESS_TYPE(template_type_parameter) + PROCESS_TYPE(variant) + PROCESS_TYPE(variant_part) /* Other types */ PROCESS_TYPE(array) PROCESS_TYPE(base) PROCESS_TYPE(subroutine) PROCESS_TYPE(typedef) + PROCESS_TYPE(unspecified) default: - debug("unimplemented type: %x", tag); - break; + error("unexpected type: %x", tag); } =20 /* Update cache state and append to the parent (if any) */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index f31bfcb0dfe7..f317de5b0653 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -63,8 +63,13 @@ extern int dump_dies; #define checkp(expr) __check(expr, __res < 0) =20 /* Consistent aliases (DW_TAG__type) for DWARF tags */ +#define DW_TAG_enumerator_type DW_TAG_enumerator #define DW_TAG_formal_parameter_type DW_TAG_formal_parameter +#define DW_TAG_member_type DW_TAG_member +#define DW_TAG_template_type_parameter_type DW_TAG_template_type_parameter #define DW_TAG_typedef_type DW_TAG_typedef +#define DW_TAG_variant_part_type DW_TAG_variant_part +#define DW_TAG_variant_type DW_TAG_variant =20 /* * symbols.c --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) (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 A3C4C2185A1 for ; Tue, 8 Oct 2024 18:38:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412737; cv=none; b=g3jmZXVEqqWRXNBDWzCla9zwqISCUqujUaZO+gXLA8gZKoTcKcHY4K+KchnwF0VR0hYz2aPqnMdLr9Vdmvh2Ypkpgogu5FjPMx8Q89FFtf7PljnLacXOjEiQuSVMYFxjRMgnjql/+UILqupQb4hjnY9QrjTatdgBivYU3ReXupo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412737; c=relaxed/simple; bh=Gpj7JEsBTlGFIdj3OdN4lasC11o2stRy6hap+Dbg4ts=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XuTrgKKxNAhIJiZJDiRoEyLAiaOv7JRvxFzL4M9LTGoksRJTZ4IlD3RO7MPrTzeO/TZloqYq0DLhq01RhSwpgj23LNkCCOxyQd+8x8Mypf2o8gS3SaUUNMkHvcY8UgZqoX8xi1gBcO3FcRibIlNjtuJu2em9T8uSGHr3EWTNbo0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=muF1cMxq; arc=none smtp.client-ip=209.85.128.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="muF1cMxq" Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-6e230808455so71397797b3.0 for ; Tue, 08 Oct 2024 11:38:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412735; x=1729017535; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=VMEPli1l6MBGDJ/OEAGe+Qz5+9aaFjquQDP4ULAQ6b8=; b=muF1cMxqwwYioSUSmLVTLnYOV4dZGNXKiX/iNm7KfMqvBqVH+LS5L5TSAcCOq+MLpo uA24nWDv3MWKhB10ufahLB4FdzQ5tu9Wo7wWIarRJkJg4DkgH7QI3ZZ+RFtvz2etbGE1 U9403p5sbU7ocs339rE37NIHObvujrXKC+dEoruIlglGou8rk1qFXEHYbZ8zl7AYIjNp dGfzQWlGwXRvSeFqnosz+m+Ri+bf2TvlMCj0hCEQ/LyJyH80Edj7oMuwduFdWMeIAhPc IOzhqgS7P5swd5nNXf+ows0/KksJS+HFkogTwWNfgm9stiNsjRWwNRHAICNa6pAT/Qoq s2hQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412735; x=1729017535; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=VMEPli1l6MBGDJ/OEAGe+Qz5+9aaFjquQDP4ULAQ6b8=; b=D+Byji4KCbeEo3bpFnS7jhpQYeyI7AU4gQtQZpCqtdkJjG9rKJ+H9rm05lgeiubXNW DG18GrR8hndLifrTnSqHlDcUaKWEJLNhWxRK+PJQm2Q3C22k+uGl7B3aZXZRuADbWq/+ g+pa5CVJ57joZLuhGvqtbHrwCnzOjgBZPrlmcx0ux7Eo8GMyTh0gf/WxqTCJNwDG8dFs agQbwFLetphXbJGyNWKoYsf61DQbVt2NIRGz/QfZkWArvRaY9H2liOv36KXmb8gWrVCd EWoINH+LmEAztq08oVi7koSFH0Og2Ky9EuyVWKpbYFL/lUnuePbYD1+ZkqOmG8op7RaG w3lQ== X-Forwarded-Encrypted: i=1; AJvYcCVKyItivoooM9LM0CxeOmTfI98K0KvS1i75/Sc0SkjErfPfNi+FkDsDBL46mHdKFuAhZsWm90pq5Ocagg0=@vger.kernel.org X-Gm-Message-State: AOJu0YzrUoP18rV6tCd4X9SahXnEYwDyMOervubawruuUNP/zYrVSm93 DWI1abSipzz7EX5SDU2En9747apnhroDnhyQhZXvC+9k65D+Bn00OpyB+rMYBPOVw1rOz2bUmcU MQIfzZCTZVEfy+f/650faw4aHgA== X-Google-Smtp-Source: AGHT+IGv62YsZO0lr1qcYP2143ECESrSbnTFI5nvZIA9vuAPPCeUKCv++zuF+N3AbUfwBBFTZQCNE/wVbuxsksfPTiw= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a5b:a4c:0:b0:e28:f454:7de5 with SMTP id 3f1490d57ef6-e28fe52740fmr6276.6.1728412734731; Tue, 08 Oct 2024 11:38:54 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:34 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=10495; i=samitolvanen@google.com; h=from:subject; bh=Gpj7JEsBTlGFIdj3OdN4lasC11o2stRy6hap+Dbg4ts=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNao/W2x8M+8fMKiK//jsfeebtA7NPfIbQq+dY2z5+ pbp67HZHaUsDGIcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAimqUM/5M+TurdaC4u7xkm +urkv0fbJGsZtFpjUz5+8HyWdTZQiI2R4TRvjqF2pH9BYMi2+4d3L2K2/nxuX3BNv9akJ/We02u vMwMA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-31-samitolvanen@google.com> Subject: [PATCH v4 10/19] gendwarfksyms: Limit structure expansion From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Expand each structure type only once per exported symbol. This is necessary to support self-referential structures, which would otherwise result in infinite recursion, but is still sufficient for catching ABI changes. For pointers, limit structure expansion after the first pointer in the symbol type. This should be plenty for detecting ABI differences, but it stops us from pulling in half the kernel for types that contain pointers to large kernel data structures, like task_struct, for example. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/cache.c | 44 +++++++++++ scripts/gendwarfksyms/dwarf.c | 109 +++++++++++++++++++++++--- scripts/gendwarfksyms/gendwarfksyms.h | 37 +++++++++ 4 files changed, 182 insertions(+), 9 deletions(-) create mode 100644 scripts/gendwarfksyms/cache.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c0d4ce50fc27..c06145d84df8 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -2,6 +2,7 @@ hostprogs-always-y +=3D gendwarfksyms =20 gendwarfksyms-objs +=3D gendwarfksyms.o +gendwarfksyms-objs +=3D cache.o gendwarfksyms-objs +=3D die.o gendwarfksyms-objs +=3D dwarf.o gendwarfksyms-objs +=3D symbols.o diff --git a/scripts/gendwarfksyms/cache.c b/scripts/gendwarfksyms/cache.c new file mode 100644 index 000000000000..2f1517133a20 --- /dev/null +++ b/scripts/gendwarfksyms/cache.c @@ -0,0 +1,44 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +struct expanded { + uintptr_t addr; + struct hlist_node hash; +}; + +void __cache_mark_expanded(struct expansion_cache *ec, uintptr_t addr) +{ + struct expanded *es; + + es =3D xmalloc(sizeof(struct expanded)); + es->addr =3D addr; + hash_add(ec->cache, &es->hash, addr_hash(addr)); +} + +bool __cache_was_expanded(struct expansion_cache *ec, uintptr_t addr) +{ + struct expanded *es; + + hash_for_each_possible(ec->cache, es, hash, addr_hash(addr)) { + if (es->addr =3D=3D addr) + return true; + } + + return false; +} + +void cache_clear_expanded(struct expansion_cache *ec) +{ + struct hlist_node *tmp; + struct expanded *es; + + hash_for_each_safe(ec->cache, es, tmp, hash) { + free(es); + } + + hash_init(ec->cache); +} diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index f5cebbdcc212..51dd8e82f9e7 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -26,6 +26,7 @@ static void process_linebreak(struct die *cache, int n) !dwarf_form##attr(&da, value); \ } =20 +DEFINE_GET_ATTR(flag, bool) DEFINE_GET_ATTR(udata, Dwarf_Word) =20 static bool get_ref_die_attr(Dwarf_Die *die, unsigned int id, Dwarf_Die *v= alue) @@ -79,6 +80,13 @@ static bool match_export_symbol(struct state *state, Dwa= rf_Die *die) return !!state->sym; } =20 +static bool is_declaration(Dwarf_Die *die) +{ + bool value; + + return get_flag_attr(die, DW_AT_declaration, &value) && value; +} + /* * Type string processing */ @@ -455,19 +463,28 @@ static void __process_structure_type(struct state *st= ate, struct die *cache, die_callback_t process_func, die_match_callback_t match_func) { + bool is_decl; + process(cache, type); process_fqn(cache, die); process(cache, " {"); process_linebreak(cache, 1); =20 - check(process_die_container(state, cache, die, process_func, - match_func)); + is_decl =3D is_declaration(die); + + if (!is_decl && state->expand.expand) { + cache_mark_expanded(&state->expansion_cache, die->addr); + check(process_die_container(state, cache, die, process_func, + match_func)); + } =20 process_linebreak(cache, -1); process(cache, "}"); =20 - process_byte_size_attr(cache, die); - process_alignment_attr(cache, die); + if (!is_decl && state->expand.expand) { + process_byte_size_attr(cache, die); + process_alignment_attr(cache, die); + } } =20 #define DEFINE_PROCESS_STRUCTURE_TYPE(structure) \ @@ -520,7 +537,7 @@ static void process_unspecified_type(struct state *stat= e, struct die *cache, Dwarf_Die *die) { /* - * These can be emitted for stand-elone assembly code, which means we + * These can be emitted for stand-alone assembly code, which means we * might run into them in vmlinux.o. */ process(cache, "unspecified_type"); @@ -552,6 +569,42 @@ static void process_cached(struct state *state, struct= die *cache, } } =20 +static void state_init(struct state *state) +{ + state->expand.expand =3D true; + state->expand.ptr_depth =3D 0; + state->expand.ptr_expansion_depth =3D 0; + hash_init(state->expansion_cache.cache); +} + +static void expansion_state_restore(struct expansion_state *state, + struct expansion_state *saved) +{ + state->expand =3D saved->expand; + state->ptr_depth =3D saved->ptr_depth; + state->ptr_expansion_depth =3D saved->ptr_expansion_depth; +} + +static void expansion_state_save(struct expansion_state *state, + struct expansion_state *saved) +{ + expansion_state_restore(saved, state); +} + +static bool is_pointer_type(int tag) +{ + return tag =3D=3D DW_TAG_pointer_type || tag =3D=3D DW_TAG_reference_type; +} + +static bool is_expanded_type(int tag) +{ + return tag =3D=3D DW_TAG_class_type || tag =3D=3D DW_TAG_structure_type || + tag =3D=3D DW_TAG_union_type || tag =3D=3D DW_TAG_enumeration_type; +} + +/* The maximum depth for expanding structures in pointers */ +#define MAX_POINTER_EXPANSION_DEPTH 2 + #define PROCESS_TYPE(type) \ case DW_TAG_##type##_type: \ process_##type##_type(state, cache, die); \ @@ -559,18 +612,52 @@ static void process_cached(struct state *state, struc= t die *cache, =20 static int process_type(struct state *state, struct die *parent, Dwarf_Die= *die) { + enum die_state want_state =3D DIE_COMPLETE; struct die *cache; + struct expansion_state saved; int tag =3D dwarf_tag(die); =20 + expansion_state_save(&state->expand, &saved); + + /* + * Structures and enumeration types are expanded only once per + * exported symbol. This is sufficient for detecting ABI changes + * within the structure. + * + * We fully expand the first pointer reference in the exported + * symbol, but limit the expansion of further pointer references + * to at most MAX_POINTER_EXPANSION_DEPTH levels. + */ + if (is_pointer_type(tag)) + state->expand.ptr_depth++; + + if (state->expand.ptr_depth > 0 && is_expanded_type(tag)) { + if (state->expand.ptr_expansion_depth >=3D + MAX_POINTER_EXPANSION_DEPTH || + cache_was_expanded(&state->expansion_cache, die->addr)) + state->expand.expand =3D false; + + if (state->expand.expand) + state->expand.ptr_expansion_depth++; + } + /* - * If we have the DIE already cached, use it instead of walking + * If we have want_state already cached, use it instead of walking * through DWARF. */ - cache =3D die_map_get(die, DIE_COMPLETE); + if (!state->expand.expand && is_expanded_type(tag)) + want_state =3D DIE_UNEXPANDED; + + cache =3D die_map_get(die, want_state); + + if (cache->state =3D=3D want_state) { + if (want_state =3D=3D DIE_COMPLETE && is_expanded_type(tag)) + cache_mark_expanded(&state->expansion_cache, die->addr); =20 - if (cache->state =3D=3D DIE_COMPLETE) { process_cached(state, cache, die); die_map_add_die(parent, cache); + + expansion_state_restore(&state->expand, &saved); return 0; } =20 @@ -611,9 +698,10 @@ static int process_type(struct state *state, struct di= e *parent, Dwarf_Die *die) =20 /* Update cache state and append to the parent (if any) */ cache->tag =3D tag; - cache->state =3D DIE_COMPLETE; + cache->state =3D want_state; die_map_add_die(parent, cache); =20 + expansion_state_restore(&state->expand, &saved); return 0; } =20 @@ -675,11 +763,14 @@ static int process_exported_symbols(struct state *unu= sed, struct die *cache, if (!match_export_symbol(&state, die)) return 0; =20 + state_init(&state); + if (tag =3D=3D DW_TAG_subprogram) process_subprogram(&state, &state.die); else process_variable(&state, &state.die); =20 + cache_clear_expanded(&state.expansion_cache); return 0; } default: diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index f317de5b0653..6147859ae2af 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -104,6 +104,7 @@ struct symbol *symbol_get(const char *name); =20 enum die_state { DIE_INCOMPLETE, + DIE_UNEXPANDED, DIE_COMPLETE, DIE_LAST =3D DIE_COMPLETE }; @@ -133,6 +134,7 @@ static inline const char *die_state_name(enum die_state= state) { switch (state) { CASE_CONST_TO_STR(DIE_INCOMPLETE) + CASE_CONST_TO_STR(DIE_UNEXPANDED) CASE_CONST_TO_STR(DIE_COMPLETE) } =20 @@ -155,9 +157,40 @@ void die_map_add_linebreak(struct die *pd, int linebre= ak); void die_map_add_die(struct die *pd, struct die *child); void die_map_free(void); =20 +/* + * cache.c + */ + +#define EXPANSION_CACHE_HASH_BITS 11 + +/* A cache for addresses we've already seen. */ +struct expansion_cache { + HASHTABLE_DECLARE(cache, 1 << EXPANSION_CACHE_HASH_BITS); +}; + +void __cache_mark_expanded(struct expansion_cache *ec, uintptr_t addr); +bool __cache_was_expanded(struct expansion_cache *ec, uintptr_t addr); + +static inline void cache_mark_expanded(struct expansion_cache *ec, void *a= ddr) +{ + __cache_mark_expanded(ec, (uintptr_t)addr); +} + +static inline bool cache_was_expanded(struct expansion_cache *ec, void *ad= dr) +{ + return __cache_was_expanded(ec, (uintptr_t)addr); +} + +void cache_clear_expanded(struct expansion_cache *ec); + /* * dwarf.c */ +struct expansion_state { + bool expand; + unsigned int ptr_depth; + unsigned int ptr_expansion_depth; +}; =20 struct state { struct symbol *sym; @@ -165,6 +198,10 @@ struct state { =20 /* List expansion */ bool first_list_item; + + /* Structure expansion */ + struct expansion_state expand; + struct expansion_cache expansion_cache; }; =20 typedef int (*die_callback_t)(struct state *state, struct die *cache, --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 34459218D68 for ; Tue, 8 Oct 2024 18:38:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412738; cv=none; b=Qd3rCnSqr0ehjQ3F+FYtfS9NkxWWSqNMtgeHCgbooThCOLMxyiSgVnDSx6iWJGwuO+5peISj1AQ0m42PRbn5M4mwDrR5yg80Kkk615cYc3GlYiMtGbYDhZeLlHYWDqVrS9YeVuhzyj25h2EubAZ1xxXoMt3Ei475/fdRDshg/p0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412738; c=relaxed/simple; bh=5W4NnaWBh21tEB8jC/516T8HqqLQetbKzxXxh7N0Wcc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lrxHnH/cSq8VuKzjTYs4WXvzdO6XiFqt2ZYfCMcTA2YSOGQ0m7SLmIzIt8+YZOFKF6VJIAF8daEpuRsfXCYBZtrwfl40OZlJHxFKvuOmyfuhF31SgtcyDeeHveFyuUdkuL7HOvqBgVXIaO1IvtpcG/aJ7ZxDuhKarYFtqu7RSOM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=a6jBdezt; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="a6jBdezt" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71df2e5e4beso3576290b3a.0 for ; Tue, 08 Oct 2024 11:38:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412737; x=1729017537; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GC9IxdNYQ5s89h9KldC7D7ygNlj7/6kTvjxiY5iu2+k=; b=a6jBdezt+DeVXi4+xF7mLDuCh96hDFA4K4s1ZWMV5JLKJtNsKT2+lXcz9wOmq9BFz4 AO6U+TwynFAyXVUzp/91ihr4TeYDOLQqRDLG6stwJvEdnLCgyBoD33/J+BBq2QunIRGo +l5b+PJHYScKIgxzK6nInk2HFUbBg3vAcKgyYY+H3RzriKRZjLo5sw/UguRZP6nEk5tY cdgUoh8ZB/UFwJI/N+42mmZgRkGm3Rs37QmsSWYWthhnUHjP5S9K+rtW2M3dsdNzk3Uo N8miAVxi/B4It2LMJyMSUteukJGIZYxqKytIIJbAdyRmhmCgFJDpooj/UERKgE8NKMPc nQzg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412737; x=1729017537; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=GC9IxdNYQ5s89h9KldC7D7ygNlj7/6kTvjxiY5iu2+k=; b=TECsHUgwFHiXdrzRH7akcilPIuJKT882LFDPZ/IVP70MfIPvxODTWWMzIPcBfWKTlJ gPbJPOpNPA8Ts2/MSZrpgJdIZ5POGI9m6NoxB5tAnBpHZSdwA3gn7340j/uenJlUDDbj E7YytLMFCLZjy9C+G7O+1KOibkb9zuspwbh/acWcHVlq4LeW6J7kAg3NB7RMCdbFGKpS OQS/e+zv/gWMhkPQ6e5eJ8aH2/KZxP20rS98x6RthQ8/b64bfPp00IjvRdE17z65Afxo dCaubp8rA3cD0r5evDJOfkDWP1H5kzTPUQdC6GI7w2s40Ws+vmV3FAOFWwDYGmLA7Zpq wcsg== X-Forwarded-Encrypted: i=1; AJvYcCWyBmK5GwN2qYYiJs7b9XdqLJ3IVKKrbvkpjupxi2YsSfwGWNRf2yMazQ41YV7vTvZF0IoPnLBhwid1dkQ=@vger.kernel.org X-Gm-Message-State: AOJu0YzVKnrvruYu0GD3WtoqIPi7xWyqmQGxNPDGsGZ9B77e/wq6kVdn 78bHxMajB/CGUDe2niOwq7C1DeIqUunRKDuuYTBJ9Ee0oiKi8wz3gpea8vMLnzsB0hrDE1Y32Y7 HLw0j7wW8sCU6s1HxTAKrSmfsOQ== X-Google-Smtp-Source: AGHT+IEZ7EStY5c6my6qpz+1/bN8XdjA2O/ROJGFeFD06XOmiiKtukLKzGwRHycXHMhZArMgvL1TdNWRER0WY9E7H1s= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6a00:318f:b0:71d:f1f9:b979 with SMTP id d2e1a72fcca58-71df1f9ba82mr20092b3a.3.1728412736553; Tue, 08 Oct 2024 11:38:56 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:35 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5132; i=samitolvanen@google.com; h=from:subject; bh=5W4NnaWBh21tEB8jC/516T8HqqLQetbKzxXxh7N0Wcc=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNaob3u/aK5s4gWfLNos9ky8VnFticU58fdA0of0Wk 3RNL0pv6ihlYRDjYJAVU2Rp+bp66+7vTqmvPhdJwMxhZQIZwsDFKQATMfnFyDDPnjvsjp5s2ss7 P1OiXZ8abiws1T2nN6v6TvTp9/6PJX4yMnQvYl7If+10SVim0XLXn54LXxk79Z1lLJzUl/mwWaB Ojw8A X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-32-samitolvanen@google.com> Subject: [PATCH v4 11/19] gendwarfksyms: Add die_map debugging From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Debugging the DWARF processing can be somewhat challenging, so add more detailed debugging output for die_map operations. Add the --dump-die-map flag, which adds color coded tags to the output for die_map changes. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 15 +++++++++++++++ scripts/gendwarfksyms/gendwarfksyms.c | 7 +++++++ scripts/gendwarfksyms/gendwarfksyms.h | 13 +++++++++++++ 3 files changed, 35 insertions(+) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 51dd8e82f9e7..d67cac16f8fb 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -103,6 +103,8 @@ static void process(struct die *cache, const char *s) if (dump_dies) fputs(s, stderr); =20 + if (cache) + die_debug_r("cache %p string '%s'", cache, s); die_map_add_string(cache, s); } =20 @@ -552,6 +554,8 @@ static void process_cached(struct state *state, struct = die *cache, list_for_each_entry(df, &cache->fragments, list) { switch (df->type) { case FRAGMENT_STRING: + die_debug_b("cache %p STRING '%s'", cache, + df->data.str); process(NULL, df->data.str); break; case FRAGMENT_LINEBREAK: @@ -561,6 +565,8 @@ static void process_cached(struct state *state, struct = die *cache, if (!dwarf_die_addr_die(dwarf_cu_getdwarf(die->cu), (void *)df->data.addr, &child)) error("dwarf_die_addr_die failed"); + die_debug_b("cache %p DIE addr %" PRIxPTR " tag %x", + cache, df->data.addr, dwarf_tag(&child)); check(process_type(state, NULL, &child)); break; default: @@ -651,6 +657,9 @@ static int process_type(struct state *state, struct die= *parent, Dwarf_Die *die) cache =3D die_map_get(die, want_state); =20 if (cache->state =3D=3D want_state) { + die_debug_g("cached addr %p tag %x -- %s", die->addr, tag, + die_state_name(cache->state)); + if (want_state =3D=3D DIE_COMPLETE && is_expanded_type(tag)) cache_mark_expanded(&state->expansion_cache, die->addr); =20 @@ -661,6 +670,9 @@ static int process_type(struct state *state, struct die= *parent, Dwarf_Die *die) return 0; } =20 + die_debug_g("addr %p tag %x -- %s -> %s", die->addr, tag, + die_state_name(cache->state), die_state_name(want_state)); + switch (tag) { /* Type modifiers */ PROCESS_TYPE(atomic) @@ -696,6 +708,9 @@ static int process_type(struct state *state, struct die= *parent, Dwarf_Die *die) error("unexpected type: %x", tag); } =20 + die_debug_r("parent %p cache %p die addr %p tag %x", parent, cache, + die->addr, tag); + /* Update cache state and append to the parent (if any) */ cache->tag =3D tag; cache->state =3D want_state; diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index 310cc9257d6e..d40692a703bb 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -19,6 +19,8 @@ int debug; /* Dump DIE contents */ int dump_dies; +/* Print debugging information about die_map changes */ +int dump_die_map; =20 static void usage(void) { @@ -26,6 +28,7 @@ static void usage(void) "Options:\n" " -d, --debug Print debugging information\n" " --dump-dies Dump DWARF DIE contents\n" + " --dump-die-map Print debugging information about die_map c= hanges\n" " -h, --help Print this message\n" "\n", stderr); @@ -74,6 +77,7 @@ int main(int argc, char **argv) =20 struct option opts[] =3D { { "debug", 0, NULL, 'd' }, { "dump-dies", 0, &dump_dies, 1 }, + { "dump-die-map", 0, &dump_die_map, 1 }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; =20 @@ -93,6 +97,9 @@ int main(int argc, char **argv) } } =20 + if (dump_die_map) + dump_dies =3D 1; + if (optind >=3D argc) { usage(); error("no input files?"); diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 6147859ae2af..126916e85ee0 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -24,6 +24,7 @@ */ extern int debug; extern int dump_dies; +extern int dump_die_map; =20 /* * Output helpers @@ -46,6 +47,18 @@ extern int dump_dies; exit(1); \ } while (0) =20 +#define __die_debug(color, format, ...) \ + do { \ + if (dump_dies && dump_die_map) \ + fprintf(stderr, \ + "\033[" #color "m<" format ">\033[39m", \ + __VA_ARGS__); \ + } while (0) + +#define die_debug_r(format, ...) __die_debug(91, format, __VA_ARGS__) +#define die_debug_g(format, ...) __die_debug(92, format, __VA_ARGS__) +#define die_debug_b(format, ...) __die_debug(94, format, __VA_ARGS__) + /* * Error handling helpers */ --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 08481218D85 for ; Tue, 8 Oct 2024 18:38:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412741; cv=none; b=nAC25acKkOTx8hRnxG0PsCjBTU/q4p8HrP9YlJWcQ5ZKzT8R89U+7mjU0mm8Vrm6ws48PyZD8/mLt0IlWCOSlqolTuUuyh8u+G+cwSQeRloivHDjrrHUne3WrafZbpp597HLYvldMlSB96jYUu5/S8A0yI4xw/xhh7D3lC3/Avo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412741; c=relaxed/simple; bh=WNdwr6FXXcOGrX2avVQt1Y+NFwDP0zHMyaavnvUEfAg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=tcdttFo16bqZgOGDosa/LL+IRYGhYUBfvFRu7elih7GlLOo0qMARnCq8ehZosa1uSc7UypMo3Kau5PXm6RU6AbAn7ZgCkSbPYICfkivzeS2k66iDdzBKRO0lBz28GaMn5bxqHar+u3tiAKPEY5NUGtCm+aYWscb3jWWZwnGq3RE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SfV4wVX/; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SfV4wVX/" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-7163489149fso6729028a12.3 for ; Tue, 08 Oct 2024 11:38:59 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412738; x=1729017538; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2ffgWMN3vEs3wT1TWnqR+RAUC1XrtaiPVhWpjo7YySw=; b=SfV4wVX/N0Q6oaxg6tzOvbcYZqDGua7TZTiZXS73krexbR94goujzyAKY50bM8osKU JbULU3KMzH1/I8nNlrT6qVuot3b+k/wcyiUjs2TEjsqB99saP9gVmiVFevqIPkX9GcOy iuAYm7FY+JjMhZth9Vdl2kff53SkT3JEhc4tp12ODrtexdqL4iV15CXGs+NKvaFGc0/+ thQiB8qXtKmSafGGevC9j7FT/e/u8gw0LA415cyf1FrIw4gJHqhkX4dO24cJAnMcdzIC boC882K0Q97eOJAgiJPC5DuGXpkp7Jk7vLu9WJzx7sFejKGJYPc8sZgisdb2TIG56ycY hocQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412738; x=1729017538; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2ffgWMN3vEs3wT1TWnqR+RAUC1XrtaiPVhWpjo7YySw=; b=hFdc0ixljlgZFyOKtsFs58JpH0G6FdgaZ03aNs2YuUOknBL1nZqxv7SKm9G8+75Y01 z1QfdlemqCKjaMtZjZNEJJHqDWUuhMjUEy7b7pnp9Jjbr6ulxwvUiLoeIGrVtw2gNlVh 7gH0wJThFP2bcsSnMYAoBkWx9Ned1rj/3PzI6FseWaoYNZPCqePJywzAIPmcc+pJEWsr JUuATILe0G6GU8vEhKi8hXct//imuFgwoNBSd0DDdGHd1mzCmHRTNEzyXVaITHBKQTWW rLT87j3nz5ApBd1ElGv1N3QboNqZPHko2oVrYPZ3GU/l9661g56SMeKeh9z8EUz+CwiW xaUg== X-Forwarded-Encrypted: i=1; AJvYcCUFHt09RX2tGZBLyftwMwcgxx2FprscCRAZSc9VwDfEnn9lh8boOjqowEput1xdka5la9n1XF6uX735Q+k=@vger.kernel.org X-Gm-Message-State: AOJu0YzBlnT9RtJtStEv3mFNfUEWh6SZq9AlDDFeyl1J+71VZ84zCtXz d8tYw3nsoe00DjEVcVNZWMOtzGagrmeznV+fikEVfKKsMLjgW5OYVHoMIeVOrmqMtb+fVawUUbS fgbfUvid2pN4DNW19PSdnYPe0uw== X-Google-Smtp-Source: AGHT+IGpH1OS7XZaUX7qEGX7PXRQXiX5HDeeeftlVvk1mCMaWtg4rTz+mdM/ycZGVMdx69DGWTMfil1gX/3MCoCVjz0= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:b25b:0:b0:75d:16f9:c075 with SMTP id 41be03b00d2f7-7ea320f35d9mr14a12.9.1728412738277; Tue, 08 Oct 2024 11:38:58 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:36 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=16977; i=samitolvanen@google.com; h=from:subject; bh=WNdwr6FXXcOGrX2avVQt1Y+NFwDP0zHMyaavnvUEfAg=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNWptIbZnPFxa9woHvFpqFjvVaVOTo+O0/WerMyLOz 99gvOdaRykLgxgHg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZhIhijD/8iJ22tn3n05bUdW nM2hzgC+w2rLfgT9Dy+zmCWc+zlsQy8jw7TLUwRzbJac4JO7lDdLfo5/N2sDp+NRlVdbFL9vVtt ZywcA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-33-samitolvanen@google.com> Subject: [PATCH v4 12/19] gendwarfksyms: Add symtypes output From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for producing genksyms-style symtypes files. Process die_map to find the longest expansions for each type, and use symtypes references in type definitions. The basic file format is similar to genksyms, with two notable exceptions: 1. Type names with spaces (common with Rust) in references are wrapped in single quotes. E.g.: s#'core::result::Result' 2. The actual type definition is the simple parsed DWARF format we output with --dump-dies, not the preprocessed C-style format genksyms produces. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/die.c | 11 + scripts/gendwarfksyms/dwarf.c | 1 + scripts/gendwarfksyms/gendwarfksyms.c | 36 ++- scripts/gendwarfksyms/gendwarfksyms.h | 19 ++ scripts/gendwarfksyms/symbols.c | 4 +- scripts/gendwarfksyms/types.c | 359 ++++++++++++++++++++++++++ 7 files changed, 428 insertions(+), 3 deletions(-) create mode 100644 scripts/gendwarfksyms/types.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index c06145d84df8..6540282dc746 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -6,5 +6,6 @@ gendwarfksyms-objs +=3D cache.o gendwarfksyms-objs +=3D die.o gendwarfksyms-objs +=3D dwarf.o gendwarfksyms-objs +=3D symbols.o +gendwarfksyms-objs +=3D types.o =20 HOSTLDLIBS_gendwarfksyms :=3D -ldw -lelf diff --git a/scripts/gendwarfksyms/die.c b/scripts/gendwarfksyms/die.c index 2829387fd815..df1ca3a032bb 100644 --- a/scripts/gendwarfksyms/die.c +++ b/scripts/gendwarfksyms/die.c @@ -22,6 +22,7 @@ static inline unsigned int die_hash(uintptr_t addr, enum = die_state state) static void init_die(struct die *cd) { cd->state =3D DIE_INCOMPLETE; + cd->mapped =3D false; cd->fqn =3D NULL; cd->tag =3D -1; cd->addr =3D 0; @@ -83,6 +84,16 @@ static void reset_die(struct die *cd) init_die(cd); } =20 +void die_map_for_each(die_map_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct die *cd; + + hash_for_each_safe(die_map, cd, tmp, hash) { + func(cd, arg); + } +} + void die_map_free(void) { struct hlist_node *tmp; diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index d67cac16f8fb..e1a9e9061b1d 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -728,6 +728,7 @@ static void process_symbol(struct state *state, Dwarf_D= ie *die, { debug("%s", state->sym->name); check(process_func(state, NULL, die)); + state->sym->state =3D SYMBOL_MAPPED; if (dump_dies) fputs("\n", stderr); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index d40692a703bb..24c87523fc3a 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -21,6 +21,11 @@ int debug; int dump_dies; /* Print debugging information about die_map changes */ int dump_die_map; +/* Print out type strings (i.e. type_map) */ +int dump_types; +/* Write a symtypes file */ +int symtypes; +static const char *symtypes_file; =20 static void usage(void) { @@ -29,6 +34,8 @@ static void usage(void) " -d, --debug Print debugging information\n" " --dump-dies Dump DWARF DIE contents\n" " --dump-die-map Print debugging information about die_map c= hanges\n" + " --dump-types Dump type strings\n" + " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", stderr); @@ -41,6 +48,7 @@ static int process_module(Dwfl_Module *mod, void **userda= ta, const char *name, Dwarf_Die cudie; Dwarf_CU *cu =3D NULL; Dwarf *dbg; + FILE *symfile =3D arg; int res; =20 debug("%s", name); @@ -60,6 +68,10 @@ static int process_module(Dwfl_Module *mod, void **userd= ata, const char *name, process_cu(&cudie); } while (cu); =20 + /* + * Use die_map to expand type strings and write them to `symfile`. + */ + generate_symtypes(symfile); die_map_free(); =20 return DWARF_CB_OK; @@ -72,22 +84,29 @@ static const Dwfl_Callbacks callbacks =3D { =20 int main(int argc, char **argv) { + FILE *symfile =3D NULL; unsigned int n; int opt; =20 struct option opts[] =3D { { "debug", 0, NULL, 'd' }, { "dump-dies", 0, &dump_dies, 1 }, { "dump-die-map", 0, &dump_die_map, 1 }, + { "dump-types", 0, &dump_types, 1 }, + { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; =20 - while ((opt =3D getopt_long(argc, argv, "dh", opts, NULL)) !=3D EOF) { + while ((opt =3D getopt_long(argc, argv, "dT:h", opts, NULL)) !=3D EOF) { switch (opt) { case 0: break; case 'd': debug =3D 1; break; + case 'T': + symtypes =3D 1; + symtypes_file =3D optarg; + break; case 'h': usage(); return 0; @@ -107,6 +126,16 @@ int main(int argc, char **argv) =20 symbol_read_exports(stdin); =20 + if (symtypes_file) { + symfile =3D fopen(symtypes_file, "w"); + + if (!symfile) { + error("fopen failed for '%s': %s", symtypes_file, + strerror(errno)); + return -1; + } + } + for (n =3D optind; n < argc; n++) { Dwfl *dwfl; int fd; @@ -129,11 +158,14 @@ int main(int argc, char **argv) =20 dwfl_report_end(dwfl, NULL, NULL); =20 - if (dwfl_getmodules(dwfl, &process_module, NULL, 0)) + if (dwfl_getmodules(dwfl, &process_module, symfile, 0)) error("dwfl_getmodules failed for '%s'", argv[n]); =20 dwfl_end(dwfl); } =20 + if (symfile) + check(fclose(symfile)); + return 0; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 126916e85ee0..e47b5e967520 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -25,6 +25,8 @@ extern int debug; extern int dump_dies; extern int dump_die_map; +extern int dump_types; +extern int symtypes; =20 /* * Output helpers @@ -93,6 +95,11 @@ static inline unsigned int addr_hash(uintptr_t addr) return hash_ptr((const void *)addr); } =20 +enum symbol_state { + SYMBOL_UNPROCESSED, + SYMBOL_MAPPED, +}; + struct symbol_addr { uint32_t section; Elf64_Addr address; @@ -103,6 +110,8 @@ struct symbol { struct symbol_addr addr; struct hlist_node addr_hash; struct hlist_node name_hash; + enum symbol_state state; + uintptr_t die_addr; }; =20 typedef void (*symbol_callback_t)(struct symbol *, void *arg); @@ -156,6 +165,7 @@ static inline const char *die_state_name(enum die_state= state) =20 struct die { enum die_state state; + bool mapped; char *fqn; int tag; uintptr_t addr; @@ -163,10 +173,13 @@ struct die { struct hlist_node hash; }; =20 +typedef void (*die_map_callback_t)(struct die *, void *arg); + int __die_map_get(uintptr_t addr, enum die_state state, struct die **res); struct die *die_map_get(Dwarf_Die *die, enum die_state state); void die_map_add_string(struct die *pd, const char *str); void die_map_add_linebreak(struct die *pd, int linebreak); +void die_map_for_each(die_map_callback_t func, void *arg); void die_map_add_die(struct die *pd, struct die *child); void die_map_free(void); =20 @@ -228,4 +241,10 @@ int process_die_container(struct state *state, struct = die *cache, =20 void process_cu(Dwarf_Die *cudie); =20 +/* + * types.c + */ + +void generate_symtypes(FILE *file); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c index 6cb99b8769ea..e414257333e5 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -92,6 +92,7 @@ void symbol_read_exports(FILE *file) sym =3D xcalloc(1, sizeof(struct symbol)); sym->name =3D name; sym->addr.section =3D SHN_UNDEF; + sym->state =3D SYMBOL_UNPROCESSED; =20 hash_add(symbol_names, &sym->name_hash, hash_str(sym->name)); ++nsym; @@ -107,7 +108,8 @@ static void get_symbol(struct symbol *sym, void *arg) { struct symbol **res =3D arg; =20 - *res =3D sym; + if (sym->state =3D=3D SYMBOL_UNPROCESSED) + *res =3D sym; } =20 struct symbol *symbol_get(const char *name) diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c new file mode 100644 index 000000000000..c05811299eba --- /dev/null +++ b/scripts/gendwarfksyms/types.c @@ -0,0 +1,359 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +static struct expansion_cache expansion_cache; + +/* + * A simple linked list of shared or owned strings to avoid copying strings + * around when not necessary. + */ +struct type_list_entry { + const char *str; + void *owned; + struct list_head list; +}; + +static void type_list_free(struct list_head *list) +{ + struct type_list_entry *entry; + struct type_list_entry *tmp; + + list_for_each_entry_safe(entry, tmp, list, list) { + if (entry->owned) + free(entry->owned); + free(entry); + } + + INIT_LIST_HEAD(list); +} + +static int type_list_append(struct list_head *list, const char *s, void *o= wned) +{ + struct type_list_entry *entry; + + if (!s) + return 0; + + entry =3D xmalloc(sizeof(struct type_list_entry)); + entry->str =3D s; + entry->owned =3D owned; + list_add_tail(&entry->list, list); + + return strlen(entry->str); +} + +static void type_list_write(struct list_head *list, FILE *file) +{ + struct type_list_entry *entry; + + list_for_each_entry(entry, list, list) { + if (entry->str) + checkp(fputs(entry->str, file)); + } +} + +/* + * An expanded type string in symtypes format. + */ +struct type_expansion { + char *name; + size_t len; + struct list_head expanded; + struct hlist_node hash; +}; + +static void type_expansion_init(struct type_expansion *type) +{ + type->name =3D NULL; + type->len =3D 0; + INIT_LIST_HEAD(&type->expanded); +} + +static inline void type_expansion_free(struct type_expansion *type) +{ + free(type->name); + type->name =3D NULL; + type->len =3D 0; + type_list_free(&type->expanded); +} + +static void type_expansion_append(struct type_expansion *type, const char = *s, + void *owned) +{ + type->len +=3D type_list_append(&type->expanded, s, owned); +} + +/* + * type_map -- the longest expansions for each type. + * + * const char *name -> struct type_expansion * + */ +#define TYPE_HASH_BITS 16 +static HASHTABLE_DEFINE(type_map, 1 << TYPE_HASH_BITS); + +static int type_map_get(const char *name, struct type_expansion **res) +{ + struct type_expansion *e; + + hash_for_each_possible(type_map, e, hash, hash_str(name)) { + if (!strcmp(name, e->name)) { + *res =3D e; + return 0; + } + } + + return -1; +} + +static void type_map_add(const char *name, struct type_expansion *type) +{ + struct type_expansion *e; + + if (type_map_get(name, &e)) { + e =3D xmalloc(sizeof(struct type_expansion)); + type_expansion_init(e); + e->name =3D xstrdup(name); + + hash_add(type_map, &e->hash, hash_str(e->name)); + + if (dump_types) + debug("adding %s", e->name); + } else { + /* Use the longest available expansion */ + if (type->len <=3D e->len) + return; + + type_list_free(&e->expanded); + + if (dump_types) + debug("replacing %s", e->name); + } + + /* Take ownership of type->expanded */ + list_replace_init(&type->expanded, &e->expanded); + e->len =3D type->len; + + if (dump_types) { + checkp(fputs(e->name, stderr)); + checkp(fputs(" ", stderr)); + type_list_write(&e->expanded, stderr); + checkp(fputs("\n", stderr)); + } +} + +static void type_map_write(FILE *file) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + if (!file) + return; + + hash_for_each_safe(type_map, e, tmp, hash) { + checkp(fputs(e->name, file)); + checkp(fputs(" ", file)); + type_list_write(&e->expanded, file); + checkp(fputs("\n", file)); + } +} + +static void type_map_free(void) +{ + struct type_expansion *e; + struct hlist_node *tmp; + + hash_for_each_safe(type_map, e, tmp, hash) { + type_expansion_free(e); + free(e); + } + + hash_init(type_map); +} + +/* + * Type reference format: #, where prefix: + * s -> structure + * u -> union + * e -> enum + * t -> typedef + * + * Names with spaces are additionally wrapped in single quotes. + */ +static char get_type_prefix(int tag) +{ + switch (tag) { + case DW_TAG_class_type: + case DW_TAG_structure_type: + return 's'; + case DW_TAG_union_type: + return 'u'; + case DW_TAG_enumeration_type: + return 'e'; + case DW_TAG_typedef_type: + return 't'; + default: + return 0; + } +} + +static char *get_type_name(struct die *cache) +{ + const char *quote; + char prefix; + char *name; + + if (cache->state =3D=3D DIE_INCOMPLETE) { + warn("found incomplete cache entry: %p", cache); + return NULL; + } + if (!cache->fqn || !*cache->fqn) + return NULL; + + prefix =3D get_type_prefix(cache->tag); + if (!prefix) + return NULL; + + /* Wrap names with spaces in single quotes */ + quote =3D strstr(cache->fqn, " ") ? "'" : ""; + + /* #\0 */ + if (asprintf(&name, "%c#%s%s%s", prefix, quote, cache->fqn, quote) < 0) + error("asprintf failed for '%s'", cache->fqn); + + return name; +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive); + +static void type_expand_child(struct die *cache, struct type_expansion *ty= pe, + bool recursive) +{ + struct type_expansion child; + char *name; + + name =3D get_type_name(cache); + if (!name) { + __type_expand(cache, type, recursive); + return; + } + + if (recursive && !__cache_was_expanded(&expansion_cache, cache->addr)) { + __cache_mark_expanded(&expansion_cache, cache->addr); + type_expansion_init(&child); + __type_expand(cache, &child, true); + type_map_add(name, &child); + type_expansion_free(&child); + } + + type_expansion_append(type, name, name); +} + +static void __type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + struct die_fragment *df; + struct die *child; + + list_for_each_entry(df, &cache->fragments, list) { + switch (df->type) { + case FRAGMENT_STRING: + type_expansion_append(type, df->data.str, NULL); + break; + case FRAGMENT_DIE: + /* Use a complete die_map expansion if available */ + if (__die_map_get(df->data.addr, DIE_COMPLETE, + &child) && + __die_map_get(df->data.addr, DIE_UNEXPANDED, + &child)) + error("unknown child: %" PRIxPTR, + df->data.addr); + + type_expand_child(child, type, recursive); + break; + case FRAGMENT_LINEBREAK: + /* + * Keep whitespace in the symtypes format, but avoid + * repeated spaces. + */ + if (list_is_last(&df->list, &cache->fragments) || + list_next_entry(df, list)->type !=3D + FRAGMENT_LINEBREAK) + type_expansion_append(type, " ", NULL); + break; + default: + error("empty die_fragment in %p", cache); + } + } +} + +static void type_expand(struct die *cache, struct type_expansion *type, + bool recursive) +{ + type_expansion_init(type); + __type_expand(cache, type, recursive); + cache_clear_expanded(&expansion_cache); +} + +static void expand_type(struct die *cache, void *arg) +{ + struct type_expansion type; + char *name; + + if (cache->mapped) + return; + + cache->mapped =3D true; + + /* + * Skip unexpanded die_map entries if there's a complete + * expansion available for this DIE. + */ + if (cache->state =3D=3D DIE_UNEXPANDED && + !__die_map_get(cache->addr, DIE_COMPLETE, &cache)) { + if (cache->mapped) + return; + + cache->mapped =3D true; + } + + name =3D get_type_name(cache); + if (!name) + return; + + debug("%s", name); + type_expand(cache, &type, true); + type_map_add(name, &type); + + type_expansion_free(&type); + free(name); +} + +void generate_symtypes(FILE *file) +{ + hash_init(expansion_cache.cache); + + /* + * die_map processing: + * + * 1. die_map contains all types referenced in exported symbol + * signatures, but can contain duplicates just like the original + * DWARF, and some references may not be fully expanded depending + * on how far we processed the DIE tree for that specific symbol. + * + * For each die_map entry, find the longest available expansion, + * and add it to type_map. + */ + die_map_for_each(expand_type, NULL); + + /* + * 2. If a symtypes file is requested, write type_map contents to + * the file. + */ + type_map_write(file); + type_map_free(); +} --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 177CF21949A for ; Tue, 8 Oct 2024 18:39:00 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412743; cv=none; b=XtSbHzavNaG3pqQkZ00LfuEvlY+vxpQMwz+A6apyKGXI7adeOUJ2BFU9HRDx+jBqEXxdtbHxxE+OFY60P4G+p/rSydlbnUUimUcFmkWIivBF6+fseM9A/rYWBSQf7rqoR17R6FoLkyp3gwb3S2g30DRf5SaaJnsPAWbI67Ikucg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412743; c=relaxed/simple; bh=ggZ4/DsRPSrpuHedARJ3vOs4yFiMuttLjnqlXUuH4Sg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WTYUlO9TJBUgBu/O2F7SM5lOEs4ytCbn8HLtJ97V4wN8PkV+SF9cvvRMJmXSFsUB31LRDlCqz/Gb7BsfFsp6b0X5CD2nbVxGIUFNk7OymceMT2zcHOmVtdDe7cMm1bVT0Pm39kX8PEOu4HShBtzYHhh8mlj7OPuB2jjRMUxfd7Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=dTBxEdbR; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="dTBxEdbR" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-206f405f453so59424155ad.1 for ; Tue, 08 Oct 2024 11:39:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412740; x=1729017540; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Nefy7NEWSpvj+5H4Mo7PbP31xdhZEeAqjsaFmsVuQug=; b=dTBxEdbROSeJhNwCsRZi5PDrdLKORQrQc0JTIXOMQ4KOhU0D7OtEVkg8UPKG0FImiZ kuZ7Cgm/r1//gfgFDb1UvTrDRk59P7afV9SbQ+XoF2m5iwJ9Qu5ux4QPz4npZ7FbCT0K dThSrAiS3euInUD7USVal6TgvKUlN2CcevUw31N55XD1dL9fEt8rVC0waFGuhxghGNaV uvelxwCvHTfijR0pa5M9TVhqtITGD1AzTJzHkWOz58VKyP9d83juRwk7UGTuB6lJLqcD 7v/q0HsQfW8bf8eWAY56S3xqEZPjPuPRF+jt0AYcdT6XgYC52rHKiQtWaXwomsqbQ4VB ejgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412740; x=1729017540; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Nefy7NEWSpvj+5H4Mo7PbP31xdhZEeAqjsaFmsVuQug=; b=IJVZMaJLV+YHdEzC2AaDfxGoM+AKDm4pyKymimWh/RQfZBf+qP7IihpRkuOmPS5OjQ 4FhYsMdIhfC3Hggrdhwn+cSyPkjrLY9OHBCj2SVkpy+yA68pgHKQybTVTUtInGA6zkIa z0Syx9m/vc4YzgJyg0G/tlluPAp1lref/KEJ433+S69sYimxK5Ls4NJM4S0LyHB5lHkA L1huaDC9u8jbpJQAS77BRPwZL20hofHLYONz7sZ+Hn4gEZ1LVX6s4XcvOXzRivDfRnoX Z4uk2R06wbRtBQiBs6M5BYXwkXB74IRCWUw5bQZkfrBrJwaNS13dzXqrn2t5UpNsio3T xlAw== X-Forwarded-Encrypted: i=1; AJvYcCWaqo6Ct6bxE3EcMs8+L0RRO7+vy8Mlc5CB38DMMGrUxJwP0KYcIO3El7MBhP7HQBYy5Fc/8W1kLTPjZog=@vger.kernel.org X-Gm-Message-State: AOJu0YyPunO8E6jH0lKMFnxXFiVUjufnnJSNwQdKPBraGcYhLyWLo8IU ZvXJcImAoFUUzzZrq6Mfe20cCqDU46ru3zRxym3pFtDP0E4jTkxC38dJNpwMHfpb3zZIuxcDLFW LZ1hsMo4tBbeCXnPqalW88uiQ8g== X-Google-Smtp-Source: AGHT+IHWia1k91cGN4oEK/yiR+e72pBcRsNAu1yAS81UX+urdreGkjRTH0CHzQagtakq50cpEXNkaw2c2Lem6dzWf3Q= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:f681:b0:20b:96b6:9fd1 with SMTP id d9443c01a7336-20bff200770mr3180595ad.11.1728412740090; Tue, 08 Oct 2024 11:39:00 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:37 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=12525; i=samitolvanen@google.com; h=from:subject; bh=ggZ4/DsRPSrpuHedARJ3vOs4yFiMuttLjnqlXUuH4Sg=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNWp5dzoUmYy3mmdW3p/9tqvgLkebM2u3WI/sB/+JJ kpnrBQ7SlkYxDgYZMUUWVq+rt66+7tT6qvPRRIwc1iZQIYwcHEKwESC0xh+s93Y8+zZs6CrB1pM P7zWvX95bkXhTZ2zGdn71CP0AlvvnWb4X6Lque6CySXJH5N7Z4Z03MnaeTfXw6XET1Z9n/VSTcN kZgA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-34-samitolvanen@google.com> Subject: [PATCH v4 13/19] gendwarfksyms: Add symbol versioning From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Calculate symbol versions from the fully expanded type strings in type_map, and output the versions in a genksyms-compatible format. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 25 +++++- scripts/gendwarfksyms/gendwarfksyms.c | 11 ++- scripts/gendwarfksyms/gendwarfksyms.h | 13 ++- scripts/gendwarfksyms/symbols.c | 59 +++++++++++++ scripts/gendwarfksyms/types.c | 122 +++++++++++++++++++++++++- 5 files changed, 222 insertions(+), 8 deletions(-) diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index e1a9e9061b1d..a47a3a0f7a69 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -723,12 +723,33 @@ static int process_type(struct state *state, struct d= ie *parent, Dwarf_Die *die) /* * Exported symbol processing */ +static struct die *get_symbol_cache(struct state *state, Dwarf_Die *die) +{ + struct die *cache; + + cache =3D die_map_get(die, DIE_SYMBOL); + + if (cache->state !=3D DIE_INCOMPLETE) + return NULL; /* We already processed a symbol for this DIE */ + + cache->tag =3D dwarf_tag(die); + return cache; +} + static void process_symbol(struct state *state, Dwarf_Die *die, die_callback_t process_func) { + struct die *cache; + + symbol_set_die(state->sym, die); + + cache =3D get_symbol_cache(state, die); + if (!cache) + return; + debug("%s", state->sym->name); - check(process_func(state, NULL, die)); - state->sym->state =3D SYMBOL_MAPPED; + check(process_func(state, cache, die)); + cache->state =3D DIE_SYMBOL; if (dump_dies) fputs("\n", stderr); } diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index 24c87523fc3a..e90d909d259b 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -23,6 +23,8 @@ int dump_dies; int dump_die_map; /* Print out type strings (i.e. type_map) */ int dump_types; +/* Print out expanded type strings used for symbol versions */ +int dump_versions; /* Write a symtypes file */ int symtypes; static const char *symtypes_file; @@ -35,6 +37,7 @@ static void usage(void) " --dump-dies Dump DWARF DIE contents\n" " --dump-die-map Print debugging information about die_map c= hanges\n" " --dump-types Dump type strings\n" + " --dump-versions Dump expanded type strings used for symbol = versions\n" " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", @@ -69,9 +72,10 @@ static int process_module(Dwfl_Module *mod, void **userd= ata, const char *name, } while (cu); =20 /* - * Use die_map to expand type strings and write them to `symfile`. + * Use die_map to expand type strings, write them to `symfile`, and + * calculate symbol versions. */ - generate_symtypes(symfile); + generate_symtypes_and_versions(symfile); die_map_free(); =20 return DWARF_CB_OK; @@ -92,6 +96,7 @@ int main(int argc, char **argv) { "dump-dies", 0, &dump_dies, 1 }, { "dump-die-map", 0, &dump_die_map, 1 }, { "dump-types", 0, &dump_types, 1 }, + { "dump-versions", 0, &dump_versions, 1 }, { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; @@ -167,5 +172,7 @@ int main(int argc, char **argv) if (symfile) check(fclose(symfile)); =20 + symbol_print_versions(); + return 0; } diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index e47b5e967520..814f53ef799e 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -26,6 +26,7 @@ extern int debug; extern int dump_dies; extern int dump_die_map; extern int dump_types; +extern int dump_versions; extern int symtypes; =20 /* @@ -98,6 +99,7 @@ static inline unsigned int addr_hash(uintptr_t addr) enum symbol_state { SYMBOL_UNPROCESSED, SYMBOL_MAPPED, + SYMBOL_PROCESSED }; =20 struct symbol_addr { @@ -112,6 +114,7 @@ struct symbol { struct hlist_node name_hash; enum symbol_state state; uintptr_t die_addr; + unsigned long crc; }; =20 typedef void (*symbol_callback_t)(struct symbol *, void *arg); @@ -119,6 +122,10 @@ typedef void (*symbol_callback_t)(struct symbol *, voi= d *arg); void symbol_read_exports(FILE *file); void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); +void symbol_set_die(struct symbol *sym, Dwarf_Die *die); +void symbol_set_crc(struct symbol *sym, unsigned long crc); +void symbol_for_each(symbol_callback_t func, void *arg); +void symbol_print_versions(void); =20 /* * die.c @@ -128,7 +135,8 @@ enum die_state { DIE_INCOMPLETE, DIE_UNEXPANDED, DIE_COMPLETE, - DIE_LAST =3D DIE_COMPLETE + DIE_SYMBOL, + DIE_LAST =3D DIE_SYMBOL }; =20 enum die_fragment_type { @@ -158,6 +166,7 @@ static inline const char *die_state_name(enum die_state= state) CASE_CONST_TO_STR(DIE_INCOMPLETE) CASE_CONST_TO_STR(DIE_UNEXPANDED) CASE_CONST_TO_STR(DIE_COMPLETE) + CASE_CONST_TO_STR(DIE_SYMBOL) } =20 error("unexpected die_state: %d", state); @@ -245,6 +254,6 @@ void process_cu(Dwarf_Die *cudie); * types.c */ =20 -void generate_symtypes(FILE *file); +void generate_symtypes_and_versions(FILE *file); =20 #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c index e414257333e5..e17f11a02f6e 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -66,6 +66,36 @@ static unsigned int for_each(const char *name, symbol_ca= llback_t func, return 0; } =20 +static void set_crc(struct symbol *sym, void *data) +{ + unsigned long *crc =3D data; + + if (sym->state =3D=3D SYMBOL_PROCESSED && sym->crc !=3D *crc) + warn("overriding version for symbol %s (crc %lx vs. %lx)", + sym->name, sym->crc, *crc); + + sym->state =3D SYMBOL_PROCESSED; + sym->crc =3D *crc; +} + +void symbol_set_crc(struct symbol *sym, unsigned long crc) +{ + if (for_each(sym->name, set_crc, &crc) =3D=3D 0) + error("no matching symbols: '%s'", sym->name); +} + +static void set_die(struct symbol *sym, void *data) +{ + sym->die_addr =3D (uintptr_t)((Dwarf_Die *)data)->addr; + sym->state =3D SYMBOL_MAPPED; +} + +void symbol_set_die(struct symbol *sym, Dwarf_Die *die) +{ + if (for_each(sym->name, set_die, die) =3D=3D 0) + error("no matching symbols: '%s'", sym->name); +} + static bool is_exported(const char *name) { return for_each(name, NULL, NULL) > 0; @@ -120,6 +150,16 @@ struct symbol *symbol_get(const char *name) return sym; } =20 +void symbol_for_each(symbol_callback_t func, void *arg) +{ + struct hlist_node *tmp; + struct symbol *sym; + + hash_for_each_safe(symbol_names, sym, tmp, name_hash) { + func(sym, arg); + } +} + typedef void (*elf_symbol_callback_t)(const char *name, GElf_Sym *sym, Elf32_Word xndx, void *arg); =20 @@ -231,3 +271,22 @@ void symbol_read_symtab(int fd) { elf_for_each_global(fd, elf_set_symbol_addr, NULL); } + +void symbol_print_versions(void) +{ + struct hlist_node *tmp; + struct symbol *sym; + + hash_for_each_safe(symbol_names, sym, tmp, name_hash) { + if (sym->state !=3D SYMBOL_PROCESSED) + warn("no information for symbol %s", sym->name); + + printf("#SYMVER %s 0x%08lx\n", sym->name, sym->crc); + + free((void *)sym->name); + free(sym); + } + + hash_init(symbol_addrs); + hash_init(symbol_names); +} diff --git a/scripts/gendwarfksyms/types.c b/scripts/gendwarfksyms/types.c index c05811299eba..08886063363c 100644 --- a/scripts/gendwarfksyms/types.c +++ b/scripts/gendwarfksyms/types.c @@ -3,6 +3,7 @@ * Copyright (C) 2024 Google LLC */ =20 +#include #include "gendwarfksyms.h" =20 static struct expansion_cache expansion_cache; @@ -174,6 +175,33 @@ static void type_map_free(void) hash_init(type_map); } =20 +/* + * CRC for a type, with an optional fully expanded type string for + * debugging. + */ +struct version { + struct type_expansion type; + unsigned long crc; +}; + +static void version_init(struct version *version) +{ + version->crc =3D 0xffffffff; + type_expansion_init(&version->type); +} + +static void version_free(struct version *version) +{ + type_expansion_free(&version->type); +} + +static void version_add(struct version *version, const char *s) +{ + version->crc =3D partial_crc32(s, version->crc); + if (dump_versions) + type_expansion_append(&version->type, s, NULL); +} + /* * Type reference format: #, where prefix: * s -> structure @@ -183,6 +211,12 @@ static void type_map_free(void) * * Names with spaces are additionally wrapped in single quotes. */ +static inline bool is_type_prefix(const char *s) +{ + return (s[0] =3D=3D 's' || s[0] =3D=3D 'u' || s[0] =3D=3D 'e' || s[0] =3D= =3D 't') && + s[1] =3D=3D '#'; +} + static char get_type_prefix(int tag) { switch (tag) { @@ -210,6 +244,8 @@ static char *get_type_name(struct die *cache) warn("found incomplete cache entry: %p", cache); return NULL; } + if (cache->state =3D=3D DIE_SYMBOL) + return NULL; if (!cache->fqn || !*cache->fqn) return NULL; =20 @@ -227,6 +263,39 @@ static char *get_type_name(struct die *cache) return name; } =20 +static void __calculate_version(struct version *version, struct list_head = *list) +{ + struct type_list_entry *entry; + struct type_expansion *e; + + /* Calculate a CRC over an expanded type string */ + list_for_each_entry(entry, list, list) { + if (is_type_prefix(entry->str)) { + check(type_map_get(entry->str, &e)); + + /* + * It's sufficient to expand each type reference just + * once to detect changes. + */ + if (cache_was_expanded(&expansion_cache, e)) { + version_add(version, entry->str); + } else { + cache_mark_expanded(&expansion_cache, e); + __calculate_version(version, &e->expanded); + } + } else { + version_add(version, entry->str); + } + } +} + +static void calculate_version(struct version *version, struct list_head *l= ist) +{ + version_init(version); + __calculate_version(version, list); + cache_clear_expanded(&expansion_cache); +} + static void __type_expand(struct die *cache, struct type_expansion *type, bool recursive); =20 @@ -333,7 +402,49 @@ static void expand_type(struct die *cache, void *arg) free(name); } =20 -void generate_symtypes(FILE *file) +static void expand_symbol(struct symbol *sym, void *arg) +{ + struct type_expansion type; + struct version version; + struct die *cache; + + /* + * No need to expand again unless we want a symtypes file entry + * for the symbol. Note that this means `sym` has the same address + * as another symbol that was already processed. + */ + if (!symtypes && sym->state =3D=3D SYMBOL_PROCESSED) + return; + + if (__die_map_get(sym->die_addr, DIE_SYMBOL, &cache)) + return; /* We'll warn about missing CRCs later. */ + + type_expand(cache, &type, false); + + /* If the symbol already has a version, don't calculate it again. */ + if (sym->state !=3D SYMBOL_PROCESSED) { + calculate_version(&version, &type.expanded); + symbol_set_crc(sym, version.crc); + debug("%s =3D %lx", sym->name, version.crc); + + if (dump_versions) { + checkp(fputs(sym->name, stderr)); + checkp(fputs(" ", stderr)); + type_list_write(&version.type.expanded, stderr); + checkp(fputs("\n", stderr)); + } + + version_free(&version); + } + + /* These aren't needed in type_map unless we want a symtypes file. */ + if (symtypes) + type_map_add(sym->name, &type); + + type_expansion_free(&type); +} + +void generate_symtypes_and_versions(FILE *file) { hash_init(expansion_cache.cache); =20 @@ -351,7 +462,14 @@ void generate_symtypes(FILE *file) die_map_for_each(expand_type, NULL); =20 /* - * 2. If a symtypes file is requested, write type_map contents to + * 2. For each exported symbol, expand the die_map type, and use + * type_map expansions to calculate a symbol version from the + * fully expanded type string. + */ + symbol_for_each(expand_symbol, NULL); + + /* + * 3. If a symtypes file is requested, write type_map contents to * the file. */ type_map_write(file); --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (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 85E402194BE for ; Tue, 8 Oct 2024 18:39:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412745; cv=none; b=bSl7tIFB/Xxu3mrP0XdDvzGuNqSNh8hHu7BuuJPCpK76+WBYaNzXoFDoQivoimg1hDaJ+aumzS+RHtOg0U1ho5kcCdQfYptQiKrXX8S5uGVIXbIWgR7oPn0FIwgg2k+p2hyrVqn1PPOAIB9ncpWL5xGDsK4BlniyQK0kSHLSo5E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412745; c=relaxed/simple; bh=YOOugzM5mt+RGuDHuB7UYTtvmIzVOO1B8SD7j0f4lu8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=QQqgoOmmMNrLAaZtandOz8N4jU1sZVLYj/BwVD0GwkKNx7hk/++bU3TSEPyGx7tRiHURa+RxTahtLFJXOHRut8iPMXJ2/Bm10QQJrRxGIR6+PhtJjkNXaRnrpWfixnPr9/bRjetwqTnl5WO3IIn3BAy+CpzbnijBSGv0Xy96iaU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QyTa3zAj; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QyTa3zAj" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-71dfd33fe78so3670813b3a.1 for ; Tue, 08 Oct 2024 11:39:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412742; x=1729017542; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=tGTnU6hyS0+XSlLsxEi6enqtqzcVag7ZErzUexQjRPI=; b=QyTa3zAjdY0G42EVku5suugCy91FGhCj1RISbdmDarGRojMA7Int5OcOHVwgbLfMum b2jQsP9UqtK31SdGC6uCYGGpBOCBD8i4nRQRf7RqQyg05FMJA/MJ2Y5mpUsrArx1KxPj uqXZvUUqduF1VrU4CGU2Lsr4kIsawJ3R4ERGS0qKCPyrrHs1tSaC3FY2OHl3CI0CQiyr u5i+bUqCuvNEDWeU8ZBhcpNM2V0YApyL9nnMR3okm902kt87HjqZa1JaotOF5fNQnBxQ y1CWZhdXPsroPsfReCUlHIx5PWPDMV2vq7Ir1upp7fbUNvrfQC/W3fdBzN70hbG0JjJl iNTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412742; x=1729017542; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=tGTnU6hyS0+XSlLsxEi6enqtqzcVag7ZErzUexQjRPI=; b=U2x6cnnXUv1zj6+WIosGUE7EXAJ09N2bIV+h0/fHcrXXmKrQ9mB7ANvZMu/vT55tM/ 5wGH0aVOFB4yVukrXajD1FzKhklX3wQgjKvfVo7mrkLAK/sAb6x/mBi4mtRoeW/5ZNCY Btkj3DFy/sbel1ef5mi7nqkw9rc+aAB8QZBmJ2+kb+8kBKZjK18K+uh+0zhK8svildkQ TjuJ/7th2eGec1vKoi3XNnjjtSvHx2CbMFH1rIFYNhnYxlvB+je4FTDGqiVP9sWhSGgm Haqw11N1woIndxH/OnzdRgHZGp7a63rZLkkpQEaLnrnT4/EPoAflIuuevBY9RDLNdH4J 0olQ== X-Forwarded-Encrypted: i=1; AJvYcCX+BSTZcjgeLL0zwG4L+IN6lvslsjWBgSlD/cIj+L2Y16HnFogpGRO271tXmw3DHD97MY+54UTpJVGBNyQ=@vger.kernel.org X-Gm-Message-State: AOJu0Yyfa99XhgC+OB7espxIhFxqgtQ4ezRNtPX2zRW3k9JPTKHebDbr zEPxt3vOBKtFrWs1JCXfgKxxliXsVp02kPnUeFHFHZhRwI22frMHEJ5wFiFXD4GZ76ECOmhyZUi 8VP6ylD57ngE34jdU4BALVQAB7g== X-Google-Smtp-Source: AGHT+IGNnB1cloqV7aXucCU01PgaJb0uEyG5KMs/AGIEMrZ0/YCZFoa3riLo9tKZ/4pPLjQbxX595RxM/JhD2/CfCrI= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:aa7:9193:0:b0:71d:f4ee:9034 with SMTP id d2e1a72fcca58-71df4ee913bmr26528b3a.0.1728412741790; Tue, 08 Oct 2024 11:39:01 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:38 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=18038; i=samitolvanen@google.com; h=from:subject; bh=YOOugzM5mt+RGuDHuB7UYTtvmIzVOO1B8SD7j0f4lu8=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNeqXZ27tvvxr+r7TplFmsanCvPc6nBlEzSr2Tv928 4fVEoG9HaUsDGIcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAisysZGa6FCruL3FW4Yin+ xi5v1so/VekZvB+lNkQe5VxxfO9R13pGho/XYruPTvz7pya9i7Hjq8cRpsSclcx3yk9eKlsaqmQ cww8A X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-35-samitolvanen@google.com> Subject: [PATCH v4 14/19] gendwarfksyms: Add support for kABI rules From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Distributions that want to maintain a stable kABI need the ability to make ABI compatible changes to kernel without affecting symbol versions, either because of LTS updates or backports. With genksyms, developers would typically hide these changes from version calculation with #ifndef __GENKSYMS__, which would result in the symbol version not changing even though the actual type has changed. When we process precompiled object files, this isn't an option. To support this use case, add a --stable command line flag that gates kABI stability features that are not needed in mainline kernels, but can be useful for distributions, and add support for kABI rules, which can be used to restrict gendwarfksyms output. The rules are specified as a set of null-terminated strings stored in the .discard.gendwarfksyms.kabi_rules section. Each rule consists of four strings as follows: "version\0type\0target\0value" The version string ensures the structure can be changed in a backwards compatible way. The type string indicates the type of the rule, and target and value strings contain rule-specific data. Initially support two simple rules: 1. Declaration-only structures A structure declaration can change into a full definition when additional includes are pulled in to the TU, which changes the versions of any symbol that references the struct. Add support for defining declaration-only structs whose definition is not expanded during versioning. 2. Ignored enum fields It's possible to add new enum fields without changing the ABI, but as the fields are included in symbol versioning, this would change the versions. Add support for ignoring specific fields. Add examples for using the rules under the examples/ directory. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- scripts/gendwarfksyms/Makefile | 1 + scripts/gendwarfksyms/dwarf.c | 19 +- scripts/gendwarfksyms/examples/kabi.h | 61 ++++++ scripts/gendwarfksyms/examples/kabi_rules.c | 56 +++++ scripts/gendwarfksyms/gendwarfksyms.c | 11 +- scripts/gendwarfksyms/gendwarfksyms.h | 57 ++++++ scripts/gendwarfksyms/kabi.c | 214 ++++++++++++++++++++ 7 files changed, 415 insertions(+), 4 deletions(-) create mode 100644 scripts/gendwarfksyms/examples/kabi.h create mode 100644 scripts/gendwarfksyms/examples/kabi_rules.c create mode 100644 scripts/gendwarfksyms/kabi.c diff --git a/scripts/gendwarfksyms/Makefile b/scripts/gendwarfksyms/Makefile index 6540282dc746..27258c31e839 100644 --- a/scripts/gendwarfksyms/Makefile +++ b/scripts/gendwarfksyms/Makefile @@ -5,6 +5,7 @@ gendwarfksyms-objs +=3D gendwarfksyms.o gendwarfksyms-objs +=3D cache.o gendwarfksyms-objs +=3D die.o gendwarfksyms-objs +=3D dwarf.o +gendwarfksyms-objs +=3D kabi.o gendwarfksyms-objs +=3D symbols.o gendwarfksyms-objs +=3D types.o =20 diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index a47a3a0f7a69..b15f1a5db452 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -80,11 +80,12 @@ static bool match_export_symbol(struct state *state, Dw= arf_Die *die) return !!state->sym; } =20 -static bool is_declaration(Dwarf_Die *die) +static bool is_declaration(struct die *cache, Dwarf_Die *die) { bool value; =20 - return get_flag_attr(die, DW_AT_declaration, &value) && value; + return (get_flag_attr(die, DW_AT_declaration, &value) && value) || + kabi_is_struct_declonly(cache->fqn); } =20 /* @@ -472,10 +473,11 @@ static void __process_structure_type(struct state *st= ate, struct die *cache, process(cache, " {"); process_linebreak(cache, 1); =20 - is_decl =3D is_declaration(die); + is_decl =3D is_declaration(cache, die); =20 if (!is_decl && state->expand.expand) { cache_mark_expanded(&state->expansion_cache, die->addr); + state->expand.current_fqn =3D cache->fqn; check(process_die_container(state, cache, die, process_func, match_func)); } @@ -508,6 +510,15 @@ static void process_enumerator_type(struct state *stat= e, struct die *cache, { Dwarf_Word value; =20 + if (stable) { + /* Get the fqn before we process anything */ + update_fqn(cache, die); + + if (kabi_is_enumerator_ignored(state->expand.current_fqn, + cache->fqn)) + return; + } + process_list_comma(state, cache); process(cache, "enumerator"); process_fqn(cache, die); @@ -580,6 +591,7 @@ static void state_init(struct state *state) state->expand.expand =3D true; state->expand.ptr_depth =3D 0; state->expand.ptr_expansion_depth =3D 0; + state->expand.current_fqn =3D NULL; hash_init(state->expansion_cache.cache); } =20 @@ -589,6 +601,7 @@ static void expansion_state_restore(struct expansion_st= ate *state, state->expand =3D saved->expand; state->ptr_depth =3D saved->ptr_depth; state->ptr_expansion_depth =3D saved->ptr_expansion_depth; + state->current_fqn =3D saved->current_fqn; } =20 static void expansion_state_save(struct expansion_state *state, diff --git a/scripts/gendwarfksyms/examples/kabi.h b/scripts/gendwarfksyms/= examples/kabi.h new file mode 100644 index 000000000000..c53e8d4a7d2e --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi.h @@ -0,0 +1,61 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * Copyright (C) 2024 Google LLC + * + * Example macros for maintaining kABI stability. + * + * This file is based on android_kabi.h, which has the following notice: + * + * Heavily influenced by rh_kabi.h which came from the RHEL/CENTOS kernel + * and was: + * Copyright (c) 2014 Don Zickus + * Copyright (c) 2015-2018 Jiri Benc + * Copyright (c) 2015 Sabrina Dubroca, Hannes Frederic Sowa + * Copyright (c) 2016-2018 Prarit Bhargava + * Copyright (c) 2017 Paolo Abeni, Larry Woodman + */ + +#ifndef __KABI_H__ +#define __KABI_H__ + +/* Kernel macros for userspace testing. */ +#ifndef __aligned +#define __aligned(x) __attribute__((__aligned__(x))) +#endif +#ifndef __used +#define __used __attribute__((__used__)) +#endif +#ifndef __section +#define __section(section) __attribute__((__section__(section))) +#endif +#ifndef __PASTE +#define ___PASTE(a, b) a##b +#define __PASTE(a, b) ___PASTE(a, b) +#endif +#ifndef __stringify +#define __stringify_1(x...) #x +#define __stringify(x...) __stringify_1(x) +#endif + +#define __KABI_RULE(hint, target, value) \ + static const char __PASTE(__gendwarfksyms_rule_, \ + __COUNTER__)[] __used __aligned(1) \ + __section(".discard.gendwarfksyms.kabi_rules") =3D \ + "1\0" #hint "\0" #target "\0" #value + +/* + * KABI_USE_ARRAY(fqn) + * Treat the struct fqn as a declaration, i.e. even if a definition + * is available, don't expand the contents. + */ +#define KABI_STRUCT_DECLONLY(fqn) __KABI_RULE(struct_declonly, fqn, ;) + +/* + * KABI_ENUMERATOR_IGNORE(fqn, field) + * When expanding enum fqn, skip the provided field. This makes it + * possible to hide added enum fields from versioning. + */ +#define KABI_ENUMERATOR_IGNORE(fqn, field) \ + __KABI_RULE(enumerator_ignore, fqn, field) + +#endif /* __KABI_H__ */ diff --git a/scripts/gendwarfksyms/examples/kabi_rules.c b/scripts/gendwarf= ksyms/examples/kabi_rules.c new file mode 100644 index 000000000000..446818e67d80 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_rules.c @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + * + * Examples for kABI rules with --stable. + */ + +/* + * The comments below each example contain the expected gendwarfksyms + * output which can be verified using LLVM's FileCheck tool: + * + * https://llvm.org/docs/CommandGuide/FileCheck.html + * + * RUN: gcc -g -c examples/kabi_rules.c -o examples/kabi_rules.o + * + * Verify --stable output: + * + * RUN: echo -e "ex0\nex1" | \ + * RUN: ./gendwarfksyms --stable --dump-dies \ + * RUN: examples/kabi_rules.o 2>&1 >/dev/null | \ + * RUN: FileCheck examples/kabi_rules.c --check-prefix=3DSTABLE + */ + +#include "kabi.h" + +struct s { + int a; +}; + +KABI_STRUCT_DECLONLY(s); + +struct s e0; + +/* + * STABLE: variable structure_type s { + * STABLE-NEXT: } + */ + +enum e { + A, + B, + C, + D, +}; + +KABI_ENUMERATOR_IGNORE(e, B); +KABI_ENUMERATOR_IGNORE(e, C); + +enum e e1; + +/* + * STABLE: variable enumeration_type e { + * STABLE-NEXT: enumerator A =3D 0 , + * STABLE-NEXT: enumerator D =3D 3 + * STABLE-NEXT: } byte_size(4) + */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.c b/scripts/gendwarfksyms/= gendwarfksyms.c index e90d909d259b..21abf1c98366 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.c +++ b/scripts/gendwarfksyms/gendwarfksyms.c @@ -25,6 +25,8 @@ int dump_die_map; int dump_types; /* Print out expanded type strings used for symbol versions */ int dump_versions; +/* Support kABI stability features */ +int stable; /* Write a symtypes file */ int symtypes; static const char *symtypes_file; @@ -38,6 +40,7 @@ static void usage(void) " --dump-die-map Print debugging information about die_map c= hanges\n" " --dump-types Dump type strings\n" " --dump-versions Dump expanded type strings used for symbol = versions\n" + " -s, --stable Support kABI stability features\n" " -T, --symtypes file Write a symtypes file\n" " -h, --help Print this message\n" "\n", @@ -97,17 +100,21 @@ int main(int argc, char **argv) { "dump-die-map", 0, &dump_die_map, 1 }, { "dump-types", 0, &dump_types, 1 }, { "dump-versions", 0, &dump_versions, 1 }, + { "stable", 0, NULL, 's' }, { "symtypes", 1, NULL, 'T' }, { "help", 0, NULL, 'h' }, { 0, 0, NULL, 0 } }; =20 - while ((opt =3D getopt_long(argc, argv, "dT:h", opts, NULL)) !=3D EOF) { + while ((opt =3D getopt_long(argc, argv, "dsT:h", opts, NULL)) !=3D EOF) { switch (opt) { case 0: break; case 'd': debug =3D 1; break; + case 's': + stable =3D 1; + break; case 'T': symtypes =3D 1; symtypes_file =3D optarg; @@ -151,6 +158,7 @@ int main(int argc, char **argv) strerror(errno)); =20 symbol_read_symtab(fd); + kabi_read_rules(fd); =20 dwfl =3D dwfl_begin(&callbacks); if (!dwfl) @@ -167,6 +175,7 @@ int main(int argc, char **argv) error("dwfl_getmodules failed for '%s'", argv[n]); =20 dwfl_end(dwfl); + kabi_free(); } =20 if (symfile) diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 814f53ef799e..f32ad4389b58 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -27,6 +27,7 @@ extern int dump_dies; extern int dump_die_map; extern int dump_types; extern int dump_versions; +extern int stable; extern int symtypes; =20 /* @@ -225,6 +226,7 @@ struct expansion_state { bool expand; unsigned int ptr_depth; unsigned int ptr_expansion_depth; + const char *current_fqn; }; =20 struct state { @@ -256,4 +258,59 @@ void process_cu(Dwarf_Die *cudie); =20 void generate_symtypes_and_versions(FILE *file); =20 +/* + * kabi.c + */ + +#define KABI_RULE_SECTION ".discard.gendwarfksyms.kabi_rules" +#define KABI_RULE_VERSION "1" + +/* + * The rule section consists of four null-terminated strings per + * entry: + * + * 1. version + * Entry format version. Must match KABI_RULE_VERSION. + * + * 2. type + * Type of the kABI rule. Must be one of the tags defined below. + * + * 3. target + * Rule-dependent target, typically the fully qualified name of + * the target DIE. + * + * 4. value + * Rule-dependent value. + */ +#define KABI_RULE_MIN_ENTRY_SIZE \ + (/* version\0 */ 2 + /* type\0 */ 2 + /* target\0" */ 2 + \ + /* value\0 */ 2) +#define KABI_RULE_EMPTY_VALUE ";" + +/* + * Rule: struct_declonly + * - For the struct in the target field, treat it as a declaration + * only even if a definition is available. + */ +#define KABI_RULE_TAG_STRUCT_DECLONLY "struct_declonly" + +/* + * Rule: enumerator_ignore + * - For the enum in the target field, ignore the named enumerator + * in the value field. + */ +#define KABI_RULE_TAG_ENUMERATOR_IGNORE "enumerator_ignore" + +enum kabi_rule_type { + KABI_RULE_TYPE_UNKNOWN, + KABI_RULE_TYPE_STRUCT_DECLONLY, + KABI_RULE_TYPE_ENUMERATOR_IGNORE, +}; + +bool kabi_is_enumerator_ignored(const char *fqn, const char *field); +bool kabi_is_struct_declonly(const char *fqn); + +void kabi_read_rules(int fd); +void kabi_free(void); + #endif /* __GENDWARFKSYMS_H */ diff --git a/scripts/gendwarfksyms/kabi.c b/scripts/gendwarfksyms/kabi.c new file mode 100644 index 000000000000..a5414382782c --- /dev/null +++ b/scripts/gendwarfksyms/kabi.c @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + */ + +#include "gendwarfksyms.h" + +#define RULE_HASH_BITS 10 + +struct rule { + enum kabi_rule_type type; + const char *target; + const char *value; + struct hlist_node hash; +}; + +/* { type, target, value } -> struct rule */ +static HASHTABLE_DEFINE(rules, 1 << RULE_HASH_BITS); + +static inline unsigned int rule_hash(enum kabi_rule_type type, + const char *target, const char *value) +{ + return hash_32(type) ^ hash_str(target) ^ hash_str(value); +} + +static inline unsigned int __rule_hash(const struct rule *rule) +{ + return rule_hash(rule->type, rule->target, rule->value); +} + +static inline const char *get_rule_field(const char **pos, ssize_t *left) +{ + const char *start =3D *pos; + size_t len; + + if (*left <=3D 1) + error("unexpected end of kABI rules"); + + len =3D strnlen(start, *left); + if (!len) + error("empty kABI rule field"); + + len +=3D 1; + *pos +=3D len; + *left -=3D len; + + return start; +} + +void kabi_read_rules(int fd) +{ + GElf_Shdr shdr_mem; + GElf_Shdr *shdr; + Elf_Data *rule_data =3D NULL; + Elf_Scn *scn; + Elf *elf; + size_t shstrndx; + const char *rule_str; + ssize_t left; + int i; + + const struct { + enum kabi_rule_type type; + const char *tag; + } rule_types[] =3D { + { + .type =3D KABI_RULE_TYPE_STRUCT_DECLONLY, + .tag =3D KABI_RULE_TAG_STRUCT_DECLONLY, + }, + { + .type =3D KABI_RULE_TYPE_ENUMERATOR_IGNORE, + .tag =3D KABI_RULE_TAG_ENUMERATOR_IGNORE, + }, + }; + + if (!stable) + return; + + if (elf_version(EV_CURRENT) !=3D EV_CURRENT) + error("elf_version failed: %s", elf_errmsg(-1)); + + elf =3D elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (!elf) + error("elf_begin failed: %s", elf_errmsg(-1)); + + if (elf_getshdrstrndx(elf, &shstrndx) < 0) + error("elf_getshdrstrndx failed: %s", elf_errmsg(-1)); + + scn =3D elf_nextscn(elf, NULL); + + while (scn) { + shdr =3D gelf_getshdr(scn, &shdr_mem); + if (shdr) { + const char *sname =3D + elf_strptr(elf, shstrndx, shdr->sh_name); + + if (sname && !strcmp(sname, KABI_RULE_SECTION)) { + rule_data =3D elf_getdata(scn, NULL); + break; + } + } + + scn =3D elf_nextscn(elf, scn); + } + + if (!rule_data) { + debug("kABI rules not found"); + return; + } + + rule_str =3D rule_data->d_buf; + left =3D shdr->sh_size; + + if (left < KABI_RULE_MIN_ENTRY_SIZE) + error("kABI rule section too small: %zd bytes", left); + + if (rule_str[left - 1] !=3D '\0') + error("kABI rules are not null-terminated"); + + while (left > KABI_RULE_MIN_ENTRY_SIZE) { + enum kabi_rule_type type =3D KABI_RULE_TYPE_UNKNOWN; + const char *field; + struct rule *rule; + + /* version */ + field =3D get_rule_field(&rule_str, &left); + + if (strcmp(field, KABI_RULE_VERSION)) + error("unsupported kABI rule version: '%s'", field); + + /* type */ + field =3D get_rule_field(&rule_str, &left); + + for (i =3D 0; i < ARRAY_SIZE(rule_types); i++) { + if (!strcmp(field, rule_types[i].tag)) { + type =3D rule_types[i].type; + break; + } + } + + if (type =3D=3D KABI_RULE_TYPE_UNKNOWN) + error("unsupported kABI rule type: '%s'", field); + + rule =3D xmalloc(sizeof(struct rule)); + + rule->type =3D type; + rule->target =3D xstrdup(get_rule_field(&rule_str, &left)); + rule->value =3D xstrdup(get_rule_field(&rule_str, &left)); + + hash_add(rules, &rule->hash, __rule_hash(rule)); + + debug("kABI rule: type: '%s', target: '%s', value: '%s'", field, + rule->target, rule->value); + } + + if (left > 0) + warn("unexpected data at the end of the kABI rules section"); + + check(elf_end(elf)); +} + +bool kabi_is_struct_declonly(const char *fqn) +{ + struct rule *rule; + + if (!stable) + return false; + if (!fqn || !*fqn) + return false; + + hash_for_each_possible(rules, rule, hash, + rule_hash(KABI_RULE_TYPE_STRUCT_DECLONLY, fqn, + KABI_RULE_EMPTY_VALUE)) { + if (rule->type =3D=3D KABI_RULE_TYPE_STRUCT_DECLONLY && + !strcmp(fqn, rule->target)) + return true; + } + + return false; +} + +bool kabi_is_enumerator_ignored(const char *fqn, const char *field) +{ + struct rule *rule; + + if (!stable) + return false; + if (!fqn || !*fqn || !field || !*field) + return false; + + hash_for_each_possible(rules, rule, hash, + rule_hash(KABI_RULE_TYPE_ENUMERATOR_IGNORE, fqn, + field)) { + if (rule->type =3D=3D KABI_RULE_TYPE_ENUMERATOR_IGNORE && + !strcmp(fqn, rule->target) && !strcmp(field, rule->value)) + return true; + } + + return false; +} + +void kabi_free(void) +{ + struct hlist_node *tmp; + struct rule *rule; + + hash_for_each_safe(rules, rule, tmp, hash) { + free((void *)rule->target); + free((void *)rule->value); + free(rule); + } + + hash_init(rules); +} --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 1E706219490 for ; Tue, 8 Oct 2024 18:39:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412746; cv=none; b=cYVATgizsIpLCiMc4SS9/CYMgcX57C1GKZ2qiK4tPb+7sexCMzpPRjZhCZIJHgd8afgqq0FHlD8VRPbFj0NtvRqSPpsrh1wcX45EDgEhGqLDILHeWqac6Y4tZjjhvGUBo4vHBHYLGsa8JFhRobT2J0Lh/OztgUcaFinD+O7E3Zg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412746; c=relaxed/simple; bh=7iEYxuvKURfMToy43kzDsHMlqvEBi9vmALynl4jTXAQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=uIpnfFkxPY5agSH2J/ozAWcvFCdUZII4QOH1l/YCLkKRWvlwN1kTfe77cpzZOkwfw6W2Okc8RT3W4tUf+8m7DTSfUMqU2RHxNoNSkeZV3bWagczijDFe6Kr23zDQWS044rWqcHxyBVjfUoUAQMMSOSvMQ9Wcx2SrKQd5MdFPsXk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=lL3kOrhC; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="lL3kOrhC" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-778702b9f8fso72520a12.1 for ; Tue, 08 Oct 2024 11:39:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412743; x=1729017543; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=+iE7he/HA4afHI612c8+0S81xp1mlY4zazLunRi9hUU=; b=lL3kOrhCw5EeUZi421L+9ZuilG4tEz7A0aKRq0axFbMDJQu/uMUuBKcnoeKCHdiTk9 V2I6pCzeTmyfpVC9KwFqLt4nGU4Um5/+zefhJSlmNrKshSIDkMmfxw3BBidFBRqGP1Jx OkZgjJmcCUZrPACSW6SLFPHVQU86PAV5PFSXVfIuAW0uFblszzQ547uJoRE9waZ6qq69 qGkiTRktaI5eoRA6RUEIyDoxZYHAWh2m2/0GTzzEMdCubzDXPzTHIUCZ/pvgX6tiOON7 W577MVgCvl7U7rc4H9UuNClq9ZmBBYopoXzauAoxGNA5Wu1NRNK6EF5JD3x6kLIOWgau pB6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412743; x=1729017543; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=+iE7he/HA4afHI612c8+0S81xp1mlY4zazLunRi9hUU=; b=EjFeNg9kJtCThQPdnFDOf8FtE3VC5N1TPld9CwXKjhq7YN+pF7iFQm23hch5PPfuCf PfvvmBOz+c6m8eYLjXLMch29Eb7h01qz3z2bzYwZLKGsQi8iKfeh3NkfRvPCEIsVqys/ SIuzi9MFbBLrDuARo2hgyeU4NLAV/x+Oo5O9Z+Yf798iwtUcDqKxD75zqTnUJ2riLqLE Ye63UzM5/IXQ7MNUMkwrhq92LUlng5CORNmEDeIdynB2Hd1iW6VN9UDbbfI3C9UR7hx/ L3oUu69T19FV0MfwKOmlT8dyU1cBQ7/OiY+UtNwOZ5+gi5YbLOaQOYNGJI9hw9uM+fxL b6/Q== X-Forwarded-Encrypted: i=1; AJvYcCXHTgGg5oZZMcrtxjbiLiLNbIxNvkWWvHjZIz025jk638kIvSroxHGvwgmLTbt0Ft9qUwBw0k3jPVMwTMs=@vger.kernel.org X-Gm-Message-State: AOJu0YzH9YbMRi/HG/G633k2rAbDb7gM1PONYSibEHvbXH79E/cnAbkZ Bi1rLlneGjhPRx6a+64+S1hV4JPVQp0Z8qZe4BYBwg7V3EHHWoHpt6mG9dVvYvZEWBPRYEScu+M 5lW1OQnIGH37fwT+cq5V3JHXFbw== X-Google-Smtp-Source: AGHT+IHwGeXoJD1mx9Vh0FV73kalHfWeTum5WmVbipykukNbrjgsnIwkT3YHMOQ9wV9o6FLYvp4WNl8Zvn3W1VZOieQ= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a63:3e89:0:b0:7db:54a0:cf3b with SMTP id 41be03b00d2f7-7ea1f6aaafcmr11321a12.0.1728412743392; Tue, 08 Oct 2024 11:39:03 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:39 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=23511; i=samitolvanen@google.com; h=from:subject; bh=7iEYxuvKURfMToy43kzDsHMlqvEBi9vmALynl4jTXAQ=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNeps1a0/zk/e8Hdp2eFNfEtnKr6rtv37Itdb5y670 atXLfM/dpSyMIhxMMiKKbK0fF29dfd3p9RXn4skYOawMoEMYeDiFICJqG1g+J/vqjKlgOXC/p8X Gs9y7Tq7blNSh7rtIe2d53lehq1fHfyQ4X98UPqk/5r3m8/vMFBR/rnP3WmbEOcWx507n4Ryr3T /o8UHAA== X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-36-samitolvanen@google.com> Subject: [PATCH v4 15/19] gendwarfksyms: Add support for reserved and ignored fields From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Distributions that want to maintain a stable kABI need the ability to make ABI compatible changes to kernel data structures without affecting symbol versions, either because of LTS updates or backports. With genksyms, developers would typically hide these changes from version calculation with #ifndef __GENKSYMS__, which would result in the symbol version not changing even though the actual type has changed. When we process precompiled object files, this isn't an option. Change union processing to recognize field name prefixes that allow the user to ignore the union completely during symbol versioning with a __kabi_ignored prefix in a field name, or to replace the type of a placeholder field using a __kabi_reserved field name prefix. For example, assume we want to add a new field to an existing alignment hole in a data structure, and ignore the new field when calculating symbol versions: struct struct1 { int a; /* a 4-byte alignment hole */ unsigned long b; }; To add `int n` to the alignment hole, we can add a union that includes a __kabi_ignored field that causes gendwarfksyms to ignore the entire union: struct struct1 { int a; union { char __kabi_ignored_0; int n; }; unsigned long b; }; With --stable, both structs produce the same symbol version. Alternatively, when a distribution expects future modification to a data structure, they can explicitly add reserved fields: struct struct2 { long a; long __kabi_reserved_0; /* reserved for future use */ }; To take the field into use, we can again replace it with a union, with one of the fields keeping the __kabi_reserved name prefix to indicate the original type: struct struct2 { long a; union { long __kabi_reserved_0; struct { int b; int v; }; }; Here gendwarfksyms --stable replaces the union with the type of the placeholder field when calculating versions. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 202 +++++++++++++++++++++- scripts/gendwarfksyms/examples/kabi.h | 80 +++++++++ scripts/gendwarfksyms/examples/kabi_ex0.c | 86 +++++++++ scripts/gendwarfksyms/examples/kabi_ex1.c | 89 ++++++++++ scripts/gendwarfksyms/examples/kabi_ex2.c | 98 +++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 29 ++++ 6 files changed, 583 insertions(+), 1 deletion(-) create mode 100644 scripts/gendwarfksyms/examples/kabi_ex0.c create mode 100644 scripts/gendwarfksyms/examples/kabi_ex1.c create mode 100644 scripts/gendwarfksyms/examples/kabi_ex2.c diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index b15f1a5db452..72e24140b6e3 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -308,6 +308,9 @@ static void __process_list_type(struct state *state, st= ruct die *cache, { const char *name =3D get_name_attr(die); =20 + if (stable && is_kabi_prefix(name)) + name =3D NULL; + process_list_comma(state, cache); process(cache, type); process_type_attr(state, cache, die); @@ -441,11 +444,193 @@ static void process_variant_part_type(struct state *= state, struct die *cache, process(cache, "}"); } =20 +static int get_kabi_status(Dwarf_Die *die) +{ + const char *name =3D get_name_attr(die); + + if (is_kabi_prefix(name)) { + name +=3D KABI_PREFIX_LEN; + + if (!strncmp(name, KABI_RESERVED_PREFIX, + KABI_RESERVED_PREFIX_LEN)) + return KABI_RESERVED; + if (!strncmp(name, KABI_IGNORED_PREFIX, + KABI_IGNORED_PREFIX_LEN)) + return KABI_IGNORED; + } + + return KABI_NORMAL; +} + +static int check_struct_member_kabi_status(struct state *state, + struct die *__unused, Dwarf_Die *die) +{ + int res; + + if (dwarf_tag(die) !=3D DW_TAG_member_type) + error("expected a member"); + + /* + * If the union member is a struct, expect the __kabi field to + * be the first member of the structure, i.e..: + * + * union { + * type new_member; + * struct { + * type __kabi_field; + * } + * }; + */ + res =3D get_kabi_status(die); + + if (res =3D=3D KABI_RESERVED && + !get_ref_die_attr(die, DW_AT_type, &state->kabi.placeholder)) + error("structure member missing a type?"); + + return res; +} + +static int check_union_member_kabi_status(struct state *state, + struct die *__unused, Dwarf_Die *die) +{ + Dwarf_Die type; + int res; + + if (dwarf_tag(die) !=3D DW_TAG_member_type) + error("expected a member"); + + if (!get_ref_die_attr(die, DW_AT_type, &type)) + error("union member missing a type?"); + + /* + * We expect a union with two members. Check if either of them + * has a __kabi name prefix, i.e.: + * + * union { + * ... + * type memberN; // <- type, N =3D {0,1} + * ... + * }; + * + * The member can also be a structure type, in which case we'll + * check the first structure member. + * + * In any case, stop processing after we've seen two members. + */ + res =3D get_kabi_status(die); + + if (res =3D=3D KABI_RESERVED) + state->kabi.placeholder =3D type; + if (res !=3D KABI_NORMAL) + return res; + + if (dwarf_tag(&type) =3D=3D DW_TAG_structure_type) + res =3D checkp(process_die_container( + state, NULL, &type, check_struct_member_kabi_status, + match_member_type)); + + if (res <=3D KABI_NORMAL && ++state->kabi.members < 2) + return 0; /* Continue */ + + return res; +} + +static int get_union_kabi_status(Dwarf_Die *die, Dwarf_Die *placeholder) +{ + struct state state; + int res; + + if (!stable) + return KABI_NORMAL; + + /* + * To maintain a stable kABI, distributions may choose to reserve + * space in structs for later use by adding placeholder members, + * for example: + * + * struct s { + * u32 a; + * // an 8-byte placeholder for future use + * u64 __kabi_reserved_0; + * }; + * + * When the reserved member is taken into use, the type change + * would normally cause the symbol version to change as well, but + * if the replacement uses the following convention, gendwarfksyms + * continues to use the placeholder type for versioning instead, + * thus maintaining the same symbol version: + * + * struct s { + * u32 a; + * union { + * // placeholder replaced with a new member `b` + * struct t b; + * struct { + * // the placeholder type that is still + * // used for versioning + * u64 __kabi_reserved_0; + * }; + * }; + * }; + * + * I.e., as long as the replaced member is in a union, and the + * placeholder has a __kabi_reserved name prefix, we'll continue + * to use the placeholder type (here u64) for version calculation + * instead of the union type. + * + * It's also possible to ignore new members from versioning if + * they've been added to alignment holes, for example, by + * including them in a union with another member that uses the + * __kabi_ignored name prefix: + * + * struct s { + * u32 a; + * // an alignment hole is used to add `n` + * union { + * u32 n; + * // hide the entire union member from versioning + * u8 __kabi_ignored_0; + * }; + * u64 b; + * }; + * + * Note that the user of this feature is responsible for ensuring + * that the structure actually remains ABI compatible. + */ + state.kabi.members =3D 0; + + res =3D checkp(process_die_container(&state, NULL, die, + check_union_member_kabi_status, + match_member_type)); + + if (placeholder && res =3D=3D KABI_RESERVED) + *placeholder =3D state.kabi.placeholder; + + return res; +} + +static bool is_kabi_ignored(Dwarf_Die *die) +{ + Dwarf_Die type; + + if (!stable) + return false; + + if (!get_ref_die_attr(die, DW_AT_type, &type)) + error("member missing a type?"); + + return dwarf_tag(&type) =3D=3D DW_TAG_union_type && + checkp(get_union_kabi_status(&type, NULL)) =3D=3D KABI_IGNORED; +} + static int ___process_structure_type(struct state *state, struct die *cach= e, Dwarf_Die *die) { switch (dwarf_tag(die)) { case DW_TAG_member: + if (is_kabi_ignored(die)) + return 0; + return check(process_type(state, cache, die)); case DW_TAG_variant_part: return check(process_type(state, cache, die)); case DW_TAG_class_type: @@ -503,7 +688,22 @@ static void __process_structure_type(struct state *sta= te, struct die *cache, =20 DEFINE_PROCESS_STRUCTURE_TYPE(class) DEFINE_PROCESS_STRUCTURE_TYPE(structure) -DEFINE_PROCESS_STRUCTURE_TYPE(union) + +static void process_union_type(struct state *state, struct die *cache, + Dwarf_Die *die) +{ + Dwarf_Die placeholder; + + int res =3D checkp(get_union_kabi_status(die, &placeholder)); + + if (res =3D=3D KABI_RESERVED) + check(process_type(state, cache, &placeholder)); + if (res > KABI_NORMAL) + return; + + __process_structure_type(state, cache, die, "union_type", + ___process_structure_type, match_all); +} =20 static void process_enumerator_type(struct state *state, struct die *cache, Dwarf_Die *die) diff --git a/scripts/gendwarfksyms/examples/kabi.h b/scripts/gendwarfksyms/= examples/kabi.h index c53e8d4a7d2e..ec99c2fb9e96 100644 --- a/scripts/gendwarfksyms/examples/kabi.h +++ b/scripts/gendwarfksyms/examples/kabi.h @@ -43,6 +43,28 @@ __section(".discard.gendwarfksyms.kabi_rules") =3D \ "1\0" #hint "\0" #target "\0" #value =20 +#define __KABI_NORMAL_SIZE_ALIGN(_orig, _new) = \ + union { = \ + _Static_assert( = \ + sizeof(struct { _new; }) <=3D sizeof(struct { _orig; }), \ + __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ + _new) " is larger than " __stringify(_orig)); \ + _Static_assert( = \ + __alignof__(struct { _new; }) <=3D \ + __alignof__(struct { _orig; }), \ + __FILE__ ":" __stringify(__LINE__) ": " __stringify( \ + _orig) " is not aligned the same as " __stringify(_new)); \ + } + +#define __KABI_REPLACE(_orig, _new) \ + union { \ + _new; \ + struct { \ + _orig; \ + }; \ + __KABI_NORMAL_SIZE_ALIGN(_orig, _new); \ + } + /* * KABI_USE_ARRAY(fqn) * Treat the struct fqn as a declaration, i.e. even if a definition @@ -58,4 +80,62 @@ #define KABI_ENUMERATOR_IGNORE(fqn, field) \ __KABI_RULE(enumerator_ignore, fqn, field) =20 +/* + * KABI_RESERVE + * Reserve some "padding" in a structure for use by LTS backports. + * This normally placed at the end of a structure. + * number: the "number" of the padding variable in the structure. Start= with + * 1 and go up. + */ +#define KABI_RESERVE(n) unsigned long __kabi_reserved##n + +/* + * KABI_RESERVE_ARRAY + * Same as _BACKPORT_RESERVE but allocates an array with the specified + * size in bytes. + */ +#define KABI_RESERVE_ARRAY(n, s) \ + unsigned char __aligned(8) __kabi_reserved##n[s] + +/* + * KABI_IGNORE + * Add a new field that's ignored in versioning. + */ +#define KABI_IGNORE(n, _new) \ + union { \ + _new; \ + unsigned char __kabi_ignored##n; \ + } + +/* + * KABI_USE(number, _new) + * Use a previous padding entry that was defined with KABI_RESERVE + * number: the previous "number" of the padding variable + * _new: the variable to use now instead of the padding variable + */ +#define KABI_USE(number, _new) __KABI_REPLACE(KABI_RESERVE(number), _new) + +/* + * KABI_USE2(number, _new1, _new2) + * Use a previous padding entry that was defined with KABI_RESERVE for + * two new variables that fit into 64 bits. This is good for when you d= o not + * want to "burn" a 64bit padding variable for a smaller variable size i= f not + * needed. + */ +#define KABI_USE2(number, _new1, _new2) \ + __KABI_REPLACE( \ + KABI_RESERVE(number), struct { \ + _new1; \ + _new2; \ + }) +/* + * KABI_USE_ARRAY(number, bytes, _new) + * Use a previous padding entry that was defined with KABI_RESERVE_ARRAY + * number: the previous "number" of the padding variable + * bytes: the size in bytes reserved for the array + * _new: the variable to use now instead of the padding variable + */ +#define KABI_USE_ARRAY(number, bytes, _new) \ + __KABI_REPLACE(KABI_RESERVE_ARRAY(number, bytes), _new) + #endif /* __KABI_H__ */ diff --git a/scripts/gendwarfksyms/examples/kabi_ex0.c b/scripts/gendwarfks= yms/examples/kabi_ex0.c new file mode 100644 index 000000000000..934324cba837 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_ex0.c @@ -0,0 +1,86 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * kabi_ex0.c + * + * Copyright (C) 2024 Google LLC + * + * Reserved and ignored data structure field examples with --stable. + */ + +/* + * The comments below each example contain the expected gendwarfksyms + * output, which can be verified using LLVM's FileCheck tool: + * + * https://llvm.org/docs/CommandGuide/FileCheck.html + * + * $ gcc -g -c examples/kabi_ex0.c examples/kabi_ex0.o + * + * Verify --stable output: + * + * $ echo -e "ex0a\nex0b\nex0c" | \ + * ./gendwarfksyms --stable --dump-dies \ + * examples/kabi_ex0.o 2>&1 >/dev/null | \ + * FileCheck examples/kabi_ex0.c --check-prefix=3DSTABLE + * + * Verify that symbol versions match with --stable: + * + * $ echo -e "ex0a\nex0b\nex0c" | \ + * ./gendwarfksyms --stable examples/kabi_ex0.o | \ + * sort | \ + * FileCheck examples/kabi_ex0.c --check-prefix=3DVERSION + */ + +#include "kabi.h" + +/* + * Example 0: Reserved fields. + */ + +struct { + int a; + KABI_RESERVE(0); + KABI_RESERVE(1); +} ex0a; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG:long unsigned int|unsigned long= ]] byte_size(8) encoding(7) data_member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data= _member_location(16) + * STABLE-NEXT: } byte_size(24) + * + * VERSION-DAG: #SYMVER ex0a 0x[[#%.08x,EX0:]] + */ + +struct { + int a; + KABI_RESERVE(0); + KABI_USE2(1, int b, int c); +} ex0b; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data= _member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data= _member_location(16) + * + * STABLE-NEXT: } byte_size(24) + * + * VERSION-DAG: #SYMVER ex0b 0x[[#%.08x,EX0]] + */ + +struct { + int a; + KABI_USE(0, void *p); + KABI_USE2(1, int b, int c); +} ex0c; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data= _member_location(8) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) data= _member_location(16) + * STABLE-NEXT: } byte_size(24) + * + * VERSION-DAG: #SYMVER ex0c 0x[[#%.08x,EX0]] + */ diff --git a/scripts/gendwarfksyms/examples/kabi_ex1.c b/scripts/gendwarfks= yms/examples/kabi_ex1.c new file mode 100644 index 000000000000..7bc34bc7dec8 --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_ex1.c @@ -0,0 +1,89 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * kabi_ex1.c + * + * Copyright (C) 2024 Google LLC + * + * Reserved and ignored data structure field examples with --stable. + */ + +/* + * The comments below each example contain the expected gendwarfksyms + * output, which can be verified using LLVM's FileCheck tool: + * + * https://llvm.org/docs/CommandGuide/FileCheck.html + * + * $ gcc -g -c examples/kabi_ex1.c examples/kabi_ex1.o + * + * Verify --stable output: + * + * $ echo -e "ex1a\nex1b\nex1c" | \ + * ./gendwarfksyms --stable --dump-dies \ + * examples/kabi_ex1.o 2>&1 >/dev/null | \ + * FileCheck examples/kabi_ex1.c --check-prefix=3DSTABLE + * + * Verify that symbol versions match with --stable: + * + * $ echo -e "ex1a\nex1b\nex1c" | \ + * ./gendwarfksyms --stable examples/kabi_ex1.o | \ + * sort | \ + * FileCheck examples/kabi_ex1.c --check-prefix=3DVERSION + */ + +#include "kabi.h" + +/* + * Example 1: A reserved array. + */ + +struct { + unsigned int a; + KABI_RESERVE_ARRAY(0, 64); +} ex1a; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a= data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + * + * VERSION-DAG: #SYMVER ex1a 0x[[#%.08x,EX1:]] + */ + +struct { + unsigned int a; + KABI_USE_ARRAY( + 0, 64, struct { + void *p; + KABI_RESERVE_ARRAY(1, 56); + }); +} ex1b; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a= data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + * + * VERSION-DAG: #SYMVER ex1b 0x[[#%.08x,EX1]] + */ + +struct { + unsigned int a; + KABI_USE_ARRAY(0, 64, void *p[8]); +} ex1c; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type unsigned int byte_size(4) encoding(7) a= data_member_location(0) , + * STABLE-NEXT: member array_type[64] { + * STABLE-NEXT: base_type unsigned char byte_size(1) encoding(8) + * STABLE-NEXT: } data_member_location(8) + * STABLE-NEXT: } byte_size(72) + * + * VERSION-DAG: #SYMVER ex1c 0x[[#%.08x,EX1]] + */ diff --git a/scripts/gendwarfksyms/examples/kabi_ex2.c b/scripts/gendwarfks= yms/examples/kabi_ex2.c new file mode 100644 index 000000000000..947ea5675b4f --- /dev/null +++ b/scripts/gendwarfksyms/examples/kabi_ex2.c @@ -0,0 +1,98 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * kabi_ex2.c + * + * Copyright (C) 2024 Google LLC + * + * Reserved and ignored data structure field examples with --stable. + */ + +/* + * The comments below each example contain the expected gendwarfksyms + * output, which can be verified using LLVM's FileCheck tool: + * + * https://llvm.org/docs/CommandGuide/FileCheck.html + * + * $ gcc -g -c examples/kabi_ex2.c examples/kabi_ex2.o + * + * Verify --stable output: + * + * $ echo -e "ex2a\nex2b\nex2c" | \ + * ./gendwarfksyms --stable --dump-dies \ + * examples/kabi_ex2.o 2>&1 >/dev/null | \ + * FileCheck examples/kabi_ex2.c --check-prefix=3DSTABLE + * + * Verify that symbol versions match with --stable: + * + * $ echo -e "ex2a\nex2b\nex2c" | \ + * ./gendwarfksyms --stable examples/kabi_ex2.o | \ + * sort | \ + * FileCheck examples/kabi_ex2.c --check-prefix=3DVERSION + */ + +#include "kabi.h" + +/* + * Example 2: An ignored field added to an alignment hole. + */ + +struct { + int a; + unsigned long b; + int c; + unsigned long d; +} ex2a; + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG:long unsigned int|unsigned long= ]] byte_size(8) encoding(7) b data_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_mem= ber_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d da= ta_member_location(24) + * STABLE-NEXT: } byte_size(32) + * + * VERSION-DAG: #SYMVER ex2a 0x[[#%.08x,EX2:]] + */ + +struct { + int a; + KABI_IGNORE(0, unsigned int n); + unsigned long b; + int c; + unsigned long d; +} ex2b; + +_Static_assert(sizeof(ex2a) =3D=3D sizeof(ex2b), "ex2a size doesn't match = ex2b"); + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) b da= ta_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_mem= ber_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d da= ta_member_location(24) + * STABLE-NEXT: } byte_size(32) + * + * VERSION-DAG: #SYMVER ex2b 0x[[#%.08x,EX2]] + */ + +struct { + int a; + KABI_IGNORE(0, unsigned int n); + unsigned long b; + int c; + KABI_IGNORE(1, unsigned int m); + unsigned long d; +} ex2c; + +_Static_assert(sizeof(ex2a) =3D=3D sizeof(ex2c), "ex2a size doesn't match = ex2c"); + +/* + * STABLE: variable structure_type { + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) a data_mem= ber_location(0) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) b da= ta_member_location(8) + * STABLE-NEXT: member base_type int byte_size(4) encoding(5) c data_mem= ber_location(16) , + * STABLE-NEXT: member base_type [[ULONG]] byte_size(8) encoding(7) d da= ta_member_location(24) + * STABLE-NEXT: } byte_size(32) + * + * VERSION-DAG: #SYMVER ex2c 0x[[#%.08x,EX2]] + */ diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index f32ad4389b58..1cff868bacdb 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -222,6 +222,20 @@ void cache_clear_expanded(struct expansion_cache *ec); /* * dwarf.c */ + +/* See dwarf.c:get_union_kabi_status */ +#define KABI_PREFIX "__kabi_" +#define KABI_PREFIX_LEN (sizeof(KABI_PREFIX) - 1) +#define KABI_RESERVED_PREFIX "reserved" +#define KABI_RESERVED_PREFIX_LEN (sizeof(KABI_RESERVED_PREFIX) - 1) +#define KABI_IGNORED_PREFIX "ignored" +#define KABI_IGNORED_PREFIX_LEN (sizeof(KABI_IGNORED_PREFIX) - 1) + +static inline bool is_kabi_prefix(const char *name) +{ + return name && !strncmp(name, KABI_PREFIX, KABI_PREFIX_LEN); +} + struct expansion_state { bool expand; unsigned int ptr_depth; @@ -229,6 +243,18 @@ struct expansion_state { const char *current_fqn; }; =20 +enum kabi_status { + /* >0 to stop DIE processing */ + KABI_NORMAL =3D 1, + KABI_RESERVED, + KABI_IGNORED, +}; + +struct kabi_state { + int members; + Dwarf_Die placeholder; +}; + struct state { struct symbol *sym; Dwarf_Die die; @@ -239,6 +265,9 @@ struct state { /* Structure expansion */ struct expansion_state expand; struct expansion_cache expansion_cache; + + /* Reserved or ignored members */ + struct kabi_state kabi; }; =20 typedef int (*die_callback_t)(struct state *state, struct die *cache, --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 D633D219CB2 for ; Tue, 8 Oct 2024 18:39:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412747; cv=none; b=B1k6BIaB4jUThPr/TtTe5t5JZJs7XLTbvdLYUSn2XM6Ah/wBrAWSdZ36fI/zKK4WBgmdbTRO59lW3MV2nirL++P2CZYngropN04Fntjp9gFSmrOwmody7cf4Bm0FaeKTY1R2rtBmjBXqoTA96G02yNJEWHyROlTfvvjI9oAXOAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412747; c=relaxed/simple; bh=VV8RyqkjEIMTguBleBQTWmqEEQ7vvL9UFQFoUvhgx/0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=aNmT4LfhSa061bhTjdPo8Rw2udaQktgfYru2YZlDy92HyCw2ULaS4lOikPsitpahH5PJmsVl9/rITcJbIA0qMANKJgPJb9O3ih6i3fXqAT8m9aY2oxJIft4VhR5d+b3vcRdrZuq7LjQcwPYE+MssRqRFBJXha+SgaqIknMC3ELc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=xmkz5eY8; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="xmkz5eY8" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-204e310e050so72778295ad.0 for ; Tue, 08 Oct 2024 11:39:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412745; x=1729017545; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=F2AT3S/FTITMJSaLZ4Kx7JT1knp4Ak6sBD54GdNIWds=; b=xmkz5eY8UTo+gSkm0eiDxWAVK26IGTAB4ktHou4NfhXfCfnye82wpS4SWQ0n2u85pu xafwjvXPMz/o0r7CTiLpWjpnHPzCY1+eRZvZlSWUqQLWtJYKQzThdvAalMVcs8R53G12 GeakyFFRRvs+tUjwb3BaJXoN+V7+4gR909mewhGDArwzEtawfuPrp7GNMG8zAeemlTOW NjCf5bOfwahZBpD/axb78kGA96huXRwshWK9Yvs1Ed+p4jbf//wykOEIRjJTTXSYVM9c zUJhSTLKjnb7pFHYjwL5Pqbac5m7rjmRmqWcu/FfjThMuczvHrv4g7z9HytvwcGoBCif 1dNA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412745; x=1729017545; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=F2AT3S/FTITMJSaLZ4Kx7JT1knp4Ak6sBD54GdNIWds=; b=X60OW5bpXp8Vluj8w906diD27yWBcbfRrUzH8YgQYxfglmSnNeiniO7GLbBmcuDlge s+K46J3Hr206bZVxcKoUCCEhhkmFal3bh6G/ZdJ8HQ/YVHe4YMAPkHIYvjCk+O+hMNk4 Ex0W2It8Nvl/4zoVz1eY7QM6tLIEXMH0OJAcPb7KZZWPf/uo3NvcbJ3uoVslOy9ghiHO /RQ+WbLasa5Xr0QxIlZ+QFlgrnOdjCEExbJuL1nnNP4GvxquKQyTliFPr8iQhqPM7hUd JDBXC4SxUP1EWpqCwOzXMCM2VwET2Qpr4A87GIchgI04wrnO+AMMZOq4R+xyWKUtVhQt gbRA== X-Forwarded-Encrypted: i=1; AJvYcCWVInm6DQOEjnlzveTE2mqHJH3SUmPaqOtHQDAn+IFZFO6rcHqNwmY/KPUIeQesCNl/DlPfsVHhq/VQJiY=@vger.kernel.org X-Gm-Message-State: AOJu0YzqwoTOPBygqFrakRbjz+ENgguosjruSc2KmZb8/WQg9uV9hpGJ ZFtN3JYieO2alF1F12ac9MGAhCgs9U2JGLK0U+zz71gE56/tDJciIQBd9trk99CxLQGOFtNTUWn j4kZAwOGeTQILhdIF26Jb/vFuBQ== X-Google-Smtp-Source: AGHT+IH/V3y+LllgTJ+TrmYeQuG6YRVBzDdigeY0ii8eUR2fzMKLzUvah/mNCHKm20b3RlFtV114GTPZgDEOUTW3M10= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:ea0a:b0:20b:982e:73e7 with SMTP id d9443c01a7336-20bff227791mr6270115ad.11.1728412745223; Tue, 08 Oct 2024 11:39:05 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:40 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=7859; i=samitolvanen@google.com; h=from:subject; bh=VV8RyqkjEIMTguBleBQTWmqEEQ7vvL9UFQFoUvhgx/0=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNRp+KiGd814eEu+bdY+zb/3Ja0cmdb0+/Cp5qxGvT NrqupupHaUsDGIcDLJiiiwtX1dv3f3dKfXV5yIJmDmsTCBDGLg4BWAizQ2MDG0xwUrZ7tVr3s/h /23/9cnG8o9b7Jasa9v70an36f+LSvYM/0zeRLkzHlCUO5Xal5i3fYV5wiaRLzusfy9J2W+ksLr pEBcA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-37-samitolvanen@google.com> Subject: [PATCH v4 16/19] gendwarfksyms: Add support for symbol type pointers From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The compiler may choose not to emit type information in DWARF for external symbols. Clang, for example, does this for symbols not defined in the current TU. To provide a way to work around this issue, add support for __gendwarfksyms_ptr_ pointers that force the compiler to emit the necessary type information in DWARF also for the missing symbols. Example usage: #define GENDWARFKSYMS_PTR(sym) \ static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ __section(".discard.gendwarfksyms") =3D &sym; extern int external_symbol(void); GENDWARFKSYMS_PTR(external_symbol); Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa Reviewed-by: Petr Pavlu --- scripts/gendwarfksyms/dwarf.c | 55 +++++++++++++++++++++- scripts/gendwarfksyms/examples/symbolptr.c | 33 +++++++++++++ scripts/gendwarfksyms/gendwarfksyms.h | 7 +++ scripts/gendwarfksyms/symbols.c | 27 +++++++++++ 4 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 scripts/gendwarfksyms/examples/symbolptr.c diff --git a/scripts/gendwarfksyms/dwarf.c b/scripts/gendwarfksyms/dwarf.c index 72e24140b6e3..0112b5e8fbf5 100644 --- a/scripts/gendwarfksyms/dwarf.c +++ b/scripts/gendwarfksyms/dwarf.c @@ -992,6 +992,31 @@ static void process_variable(struct state *state, Dwar= f_Die *die) process_symbol(state, die, __process_variable); } =20 +static void save_symbol_ptr(struct state *state) +{ + Dwarf_Die ptr_type; + Dwarf_Die type; + + if (!get_ref_die_attr(&state->die, DW_AT_type, &ptr_type) || + dwarf_tag(&ptr_type) !=3D DW_TAG_pointer_type) + error("%s must be a pointer type!", + get_symbol_name(&state->die)); + + if (!get_ref_die_attr(&ptr_type, DW_AT_type, &type)) + error("%s pointer missing a type attribute?", + get_symbol_name(&state->die)); + + /* + * Save the symbol pointer DIE in case the actual symbol is + * missing from the DWARF. Clang, for example, intentionally + * omits external symbols from the debugging information. + */ + if (dwarf_tag(&type) =3D=3D DW_TAG_subroutine_type) + symbol_set_ptr(state->sym, &type); + else + symbol_set_ptr(state->sym, &ptr_type); +} + static int process_exported_symbols(struct state *unused, struct die *cach= e, Dwarf_Die *die) { @@ -1015,7 +1040,9 @@ static int process_exported_symbols(struct state *unu= sed, struct die *cache, =20 state_init(&state); =20 - if (tag =3D=3D DW_TAG_subprogram) + if (is_symbol_ptr(get_symbol_name(&state.die))) + save_symbol_ptr(&state); + else if (tag =3D=3D DW_TAG_subprogram) process_subprogram(&state, &state.die); else process_variable(&state, &state.die); @@ -1028,8 +1055,34 @@ static int process_exported_symbols(struct state *un= used, struct die *cache, } } =20 +static void process_symbol_ptr(struct symbol *sym, void *arg) +{ + struct state state; + Dwarf *dwarf =3D arg; + + if (sym->state !=3D SYMBOL_UNPROCESSED || !sym->ptr_die_addr) + return; + + debug("%s", sym->name); + state_init(&state); + state.sym =3D sym; + + if (!dwarf_die_addr_die(dwarf, (void *)sym->ptr_die_addr, &state.die)) + error("dwarf_die_addr_die failed for symbol ptr: '%s'", + sym->name); + + if (dwarf_tag(&state.die) =3D=3D DW_TAG_subroutine_type) + process_subprogram(&state, &state.die); + else + process_variable(&state, &state.die); + + cache_clear_expanded(&state.expansion_cache); +} + void process_cu(Dwarf_Die *cudie) { check(process_die_container(NULL, NULL, cudie, process_exported_symbols, match_all)); + + symbol_for_each(process_symbol_ptr, dwarf_cu_getdwarf(cudie->cu)); } diff --git a/scripts/gendwarfksyms/examples/symbolptr.c b/scripts/gendwarfk= syms/examples/symbolptr.c new file mode 100644 index 000000000000..b7b97cd39769 --- /dev/null +++ b/scripts/gendwarfksyms/examples/symbolptr.c @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Copyright (C) 2024 Google LLC + * + * Example for symbol pointers. When compiled with Clang, gendwarfkyms + * uses a symbol pointer for `f`. + * + * $ clang -g -c examples/symbolptr.c examples/symbolptr.o + * $ echo -e "f\ng\np" | ./gendwarfksyms -d examples/symbolptr.o + */ + +/* Kernel macros for userspace testing. */ +#ifndef __used +#define __used __attribute__((__used__)) +#endif +#ifndef __section +#define __section(section) __attribute__((__section__(section))) +#endif + +#define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") =3D &sym; + +extern void f(unsigned int arg); +void g(int *arg); +void g(int *arg) {} + +struct s; +extern struct s *p; + +__GENDWARFKSYMS_EXPORT(f); +__GENDWARFKSYMS_EXPORT(g); +__GENDWARFKSYMS_EXPORT(p); diff --git a/scripts/gendwarfksyms/gendwarfksyms.h b/scripts/gendwarfksyms/= gendwarfksyms.h index 1cff868bacdb..28315b38762f 100644 --- a/scripts/gendwarfksyms/gendwarfksyms.h +++ b/scripts/gendwarfksyms/gendwarfksyms.h @@ -92,6 +92,10 @@ extern int symtypes; * symbols.c */ =20 +/* See symbols.c:is_symbol_ptr */ +#define SYMBOL_PTR_PREFIX "__gendwarfksyms_ptr_" +#define SYMBOL_PTR_PREFIX_LEN (sizeof(SYMBOL_PTR_PREFIX) - 1) + static inline unsigned int addr_hash(uintptr_t addr) { return hash_ptr((const void *)addr); @@ -115,14 +119,17 @@ struct symbol { struct hlist_node name_hash; enum symbol_state state; uintptr_t die_addr; + uintptr_t ptr_die_addr; unsigned long crc; }; =20 typedef void (*symbol_callback_t)(struct symbol *, void *arg); =20 +bool is_symbol_ptr(const char *name); void symbol_read_exports(FILE *file); void symbol_read_symtab(int fd); struct symbol *symbol_get(const char *name); +void symbol_set_ptr(struct symbol *sym, Dwarf_Die *ptr); void symbol_set_die(struct symbol *sym, Dwarf_Die *die); void symbol_set_crc(struct symbol *sym, unsigned long crc); void symbol_for_each(symbol_callback_t func, void *arg); diff --git a/scripts/gendwarfksyms/symbols.c b/scripts/gendwarfksyms/symbol= s.c index e17f11a02f6e..ca6a17bd009c 100644 --- a/scripts/gendwarfksyms/symbols.c +++ b/scripts/gendwarfksyms/symbols.c @@ -39,6 +39,20 @@ static unsigned int __for_each_addr(struct symbol *sym, = symbol_callback_t func, return processed; } =20 +/* + * For symbols without debugging information (e.g. symbols defined in other + * TUs), we also match __gendwarfksyms_ptr_ symbols, which the + * kernel uses to ensure type information is present in the TU that exports + * the symbol. A __gendwarfksyms_ptr pointer must have the same type as the + * exported symbol, e.g.: + * + * typeof(symname) *__gendwarf_ptr_symname =3D &symname; + */ +bool is_symbol_ptr(const char *name) +{ + return name && !strncmp(name, SYMBOL_PTR_PREFIX, SYMBOL_PTR_PREFIX_LEN); +} + static unsigned int for_each(const char *name, symbol_callback_t func, void *data) { @@ -47,6 +61,8 @@ static unsigned int for_each(const char *name, symbol_cal= lback_t func, =20 if (!name || !*name) return 0; + if (is_symbol_ptr(name)) + name +=3D SYMBOL_PTR_PREFIX_LEN; =20 hash_for_each_possible_safe(symbol_names, match, tmp, name_hash, hash_str(name)) { @@ -84,6 +100,17 @@ void symbol_set_crc(struct symbol *sym, unsigned long c= rc) error("no matching symbols: '%s'", sym->name); } =20 +static void set_ptr(struct symbol *sym, void *data) +{ + sym->ptr_die_addr =3D (uintptr_t)((Dwarf_Die *)data)->addr; +} + +void symbol_set_ptr(struct symbol *sym, Dwarf_Die *ptr) +{ + if (for_each(sym->name, set_ptr, ptr) =3D=3D 0) + error("no matching symbols: '%s'", sym->name); +} + static void set_die(struct symbol *sym, void *data) { sym->die_addr =3D (uintptr_t)((Dwarf_Die *)data)->addr; --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (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 784CF21A706 for ; Tue, 8 Oct 2024 18:39:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412750; cv=none; b=ERRr8UwQj97lMkgyNOCooU20TZjYw8GpuaksAapeTK5vIGivrjCmXUoMN5YSuLY8o2NLiimOf2cvpiRlcYYCQDB18Qyzb43QFcHeChkVBM1oXXm8Mt63j/nCRGNRe/crPl8yTRQmUXeqQ9tQ4pkg/H643HiDVSKQIoCZpwC0yPw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412750; c=relaxed/simple; bh=63FTcQQsHnD0ujy7ldzffjez+nFzb+CbbWB4scUUBT4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ZCHxjMPYP8rIeEUiXhFishSI3yFH1MTdZd374ykv4XxD8xcowLpiMs3wKizCD7wYJui7dME7peTyenC7q+RD0h7AO40lBcIf+3wLVWmseJYUn6uoRUsIG/tzkd+s06ZP4lzoUC49wjXPCG/QU+PlKShGY8n9OaukcAcy1wizqqw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=p8u5e+YE; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="p8u5e+YE" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-20c5c43ecf0so7304335ad.2 for ; Tue, 08 Oct 2024 11:39:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412748; x=1729017548; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=A4gdQj2kSzdcnDtZR+VagbT7f5UDOVpvc3nQ1NtsNEc=; b=p8u5e+YEr7Ld6yPVAF2ecfv/onOgI4H7LFYqq8rLWWNVtF21zjGzLzZzOP9qRE7afc fXxomKdHlO0tzm53bC+9HNJY8g3T1ZN6j4MFRtBH9Zxm8LUJB1I/Ua4GDCr9vuFLJ542 Kk0bs0rEFeS2LUGVdqy+B319gXLfeM6hTZUjVwGpCmzP0+Q+QKRLa2ZwE/2OQUQen4vN m/1bEU9yE43A5oDcKZEPyWtGx6szqmg4eYTBt2IH9AfkCY8fLuRYdALYtDvNsavc4zQx nS9LI9IlZXZ5/zeYHWfzhKUsSX4aD/oqhtOIpyiuFvqNy2i5XNskwixz3l/axGOve7fo YffA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412748; x=1729017548; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=A4gdQj2kSzdcnDtZR+VagbT7f5UDOVpvc3nQ1NtsNEc=; b=j2aHiPpnEaxhtONauQc5SkLlOdp5e+PegIzgutqNSTPZK9LM5ykM6Nc1q2W3Lm+wNj VTgqXnfoEG3aizCumI6G7wIriYw+IyFxO/C70+CVxYiKZKCKBejO9VoNfW5GnPKBJSyr dHuWHTCVmGMV/Xjq8bnSwvdh0iW/RQEaSM0P++ZOO3vM3eZcwH1EPLx4fGoZB+U+kwuN xVCUNvTuTiJrBwSUXt7J7j0iz6ZqRIYT0nj/Hh2IQsChVXp6bKYg+elBZQcm/c+nyB2k BBd6RXWGUxpH1EyFy3C3vLQ39OkyYXsMzPfDrEL1LH0A/cz+ZSWchcPc8W/cvHKSiN2T yjBA== X-Forwarded-Encrypted: i=1; AJvYcCUpu18+45Pq7hS02Lwjy56igTKjGdlTpFdMYxYYpKXHFdVzW0fqYi2nTpzUspBZXbucXcAlI1h50J4b1IA=@vger.kernel.org X-Gm-Message-State: AOJu0YxQBfW6VG2luJ85QycBPJtoRy6WvBLYGOPxSO9BlsGSzGzHPsl1 xBnt2dTzU4fG1Kv75pJpsCEHP+/kb80wboYbdHiP0fJs8KNyvKxPo6036mLW/dbMWwlABQITfH5 wSZiTLoTzOaqKYPbdggF+qU2Hwg== X-Google-Smtp-Source: AGHT+IH4uDld0Ae+/fhGpS4rG8AsXui7zPcfmITo1sskAJxITEdUVNeC4NRhZvQwVt5ry3RgjuLHg8OC5hd+mt50H8Y= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a17:902:d491:b0:20b:984d:7000 with SMTP id d9443c01a7336-20bfd971431mr1757795ad.0.1728412746828; Tue, 08 Oct 2024 11:39:06 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:41 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=2048; i=samitolvanen@google.com; h=from:subject; bh=63FTcQQsHnD0ujy7ldzffjez+nFzb+CbbWB4scUUBT4=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNRpG/111u5W0hZqm1vFmmz/T2LbvG0O6QZZc0RW9h ILTJ2U6SlkYxDgYZMUUWVq+rt66+7tT6qvPRRIwc1iZQIYwcHEKwEQWfGFkOHiaqenqT3WXl0vZ +GRnuU789WO3+Yd7qalh/16/amCZrsrwV9qfp/aZoGsNo1j4m0elC4Q553GkOlgop/W/nZI70/M GHwA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-38-samitolvanen@google.com> Subject: [PATCH v4 17/19] export: Add __gendwarfksyms_ptr_ references to exported symbols From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" With gendwarfksyms, we need each TU where the EXPORT_SYMBOL() macro is used to also contain DWARF type information for the symbols it exports. However, as a TU can also export external symbols and compilers may choose not to emit debugging information for symbols not defined in the current TU, the missing types will result in missing symbol versions. Stand-alone assembly code also doesn't contain type information for exported symbols, so we need to compile a temporary object file with asm-prototypes.h instead, and similarly need to ensure the DWARF in the temporary object file contains the necessary types. To always emit type information for external exports, add explicit __gendwarfksyms_ptr_ references to them in EXPORT_SYMBOL(). gendwarfksyms will use the type information for __gendwarfksyms_ptr_* if needed. Discard the pointers from the final binary to avoid further bloat. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- include/linux/export.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/include/linux/export.h b/include/linux/export.h index 0bbd02fd351d..cf71d3202e5b 100644 --- a/include/linux/export.h +++ b/include/linux/export.h @@ -52,9 +52,24 @@ =20 #else =20 +#ifdef CONFIG_GENDWARFKSYMS +/* + * With CONFIG_GENDWARFKSYMS, ensure the compiler emits debugging + * information for all exported symbols, including those defined in + * different TUs, by adding a __gendwarfksyms_ptr_ pointer + * that's discarded during the final link. + */ +#define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") =3D &sym; +#else +#define __GENDWARFKSYMS_EXPORT(sym) +#endif + #define __EXPORT_SYMBOL(sym, license, ns) \ extern typeof(sym) sym; \ __ADDRESSABLE(sym) \ + __GENDWARFKSYMS_EXPORT(sym) \ asm(__stringify(___EXPORT_SYMBOL(sym, license, ns))) =20 #endif --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pg1-f202.google.com (mail-pg1-f202.google.com [209.85.215.202]) (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 0B63D213ED7 for ; Tue, 8 Oct 2024 18:39:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412752; cv=none; b=TnDFXTR+S5bj+O+oGol8aSFcUoJNF60dHnlfGHdoX3IWwxS9rcxtZKs9vxwFsPuclwLmejQlQICgDhZXuoh8y7uTKMUWe1ZGMf/5HdH8OBsHlsMFceCb0bpkd4b3Jx0Csr2kBXWfbBYlIBK2eEZ7LUj3gOjsArLrtQvNxvBpLjo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412752; c=relaxed/simple; bh=cu13kRY3+mJ5cZSxocHr5Yb/1rJZvzr4RQDiYdBp4Ok=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=qlRySq4Tei7TlHSSB8Slz7e6EmOSrLjysIDtgSIWjza91lhAtodc9JY7CLAaXYGuhkgH04C///X0AxIAZK2AlubGFAhY14A9iznJjt09lrYFnQQFRjrQsJjR/fF3oyyWiRw8Ydhb5oaCwiH3RcbVvv5QS1/rXkgG11Yd4gv46Vs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=f5epyucS; arc=none smtp.client-ip=209.85.215.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="f5epyucS" Received: by mail-pg1-f202.google.com with SMTP id 41be03b00d2f7-70ac9630e3aso5116686a12.1 for ; Tue, 08 Oct 2024 11:39:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412750; x=1729017550; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=ZaLa12BRozVfGYk63WojrW4fg6j3chZ7iM5FcwVR/BA=; b=f5epyucS+JMK/n0rZdTd3t1ea+qMaw4ky80HzghouWdnaSLprCO2CfGmNM5o90wwxZ CjnWqtFsszQK5mGqk8Xbv6Bk5Vkscmg9dipR88dM14s0kBYGIQkUPfId5U7NOzoBO0HF iEdhDFC0Dqjp6RvpLJ+CrKfD3Y33PyQBYbZ1j3aLt3wy3kTl8PyrQTzzzPnfDXYoQleX 4RZwyNIibp7trm/qrIlueNuKbGxjAYcuvkLb2dZsDENwI+NICaNnjIG8vSGZGeShoaHI mAHcc9cguN0NecVU0Ljx5KVWYCT0Zde1D7xJiIuj1Y6VFv81TNlkr8QLA7ujPNI7K6IL 6dMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412750; x=1729017550; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=ZaLa12BRozVfGYk63WojrW4fg6j3chZ7iM5FcwVR/BA=; b=Duk+C0KIcglS29oTv3ecAM+90s+vcoVZ2DZpXgxYIbLmS7V1iaNu+Wp/wcMCprX73L QEFeL24m3wGcXd3eXIQD7WwkaZHICJZL2RUuSL0xh7J0EMJIGh8JZKXHqt0ZtIEUfrnu 3e7gRPcj9hsUkl0cgK8SE+dGEzkxYsAXxi9vqikHmjXcILLe5vWL9pqGmXDNN7kkB//o Q0NDF7F0DJCh6i0YvotOCfvHLrcQHS7U4QhsIcV+SEyThzFj3/KxCF/jVOaAFJm5jQIv mDyY/35F0VzrXGeh0ef3aUfG/Dw+ejdkMExhbaNjmjNulL+G+p5LhkFP7joC+L4dYSHo 2pIg== X-Forwarded-Encrypted: i=1; AJvYcCVahXDOEOVjnndm47ajtac7Cin5CvFrdhDbJG+4PuuYsm8PCKFV/PfCgieyzXxJnJompoaNSZ0HUUhsW4M=@vger.kernel.org X-Gm-Message-State: AOJu0Yz8aahDv2IT+pF4/E4lFkMyGutb/zJVNriCxWdWrG06B6dX4lbA d2kR5tPc8G9JF4qS0xUmWLebrLfkyxj+Ub02GWyvCVHZtOSmlaXvaPsYFBCqMPIYwIcUx5DAn2x TlZeR0NR043NT9WYz2iBQPxxvdA== X-Google-Smtp-Source: AGHT+IE1ARsuZR5MICYuQmn5iAsWmKU1pOr38cH5bK+78z1447fX+K+DtRTHFCEh7OVqUA+XdHcAISnHPXeFVjGnjgs= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a65:6886:0:b0:7e6:b6fb:b115 with SMTP id 41be03b00d2f7-7ea32071cb3mr39a12.1.1728412749298; Tue, 08 Oct 2024 11:39:09 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:42 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=5204; i=samitolvanen@google.com; h=from:subject; bh=cu13kRY3+mJ5cZSxocHr5Yb/1rJZvzr4RQDiYdBp4Ok=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNZp8M81P/XzpMEt8kXySwOLzX2ZLdotUP123Sc3/v 7SbXpZxRykLgxgHg6yYIkvL19Vbd393Sn31uUgCZg4rE8gQBi5OAZgIuzLDP8UTt/Omhgg/cNPk KU+Jc7P+3HJEPolDMHvB2w12bvNFzzIyPMmYaeRs7mYZvOt4SdHt7Qbt0r1+l5/PrC3o8PZcWcP EBAA= X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-39-samitolvanen@google.com> Subject: [PATCH v4 18/19] kbuild: Add gendwarfksyms as an alternative to genksyms From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When MODVERSIONS is enabled, allow selecting gendwarfksyms as the implementation, but default to genksyms. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- kernel/module/Kconfig | 25 ++++++++++++++++++++++++- scripts/Makefile | 2 +- scripts/Makefile.build | 39 +++++++++++++++++++++++++++++++-------- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/kernel/module/Kconfig b/kernel/module/Kconfig index f9e5f82fa88b..e6b2427e5c19 100644 --- a/kernel/module/Kconfig +++ b/kernel/module/Kconfig @@ -169,13 +169,36 @@ config MODVERSIONS make them incompatible with the kernel you are running. If unsure, say N. =20 +choice + prompt "Module versioning implementation" + depends on MODVERSIONS + default GENKSYMS + help + Select the tool used to calculate symbol versions for modules. + + If unsure, select GENKSYMS. + +config GENKSYMS + bool "genksyms (from source code)" + help + Calculate symbol versions from pre-processed source code using + genksyms. + + If unsure, say Y. + config GENDWARFKSYMS - bool + bool "gendwarfksyms (from debugging information)" depends on DEBUG_INFO # Requires full debugging information, split DWARF not supported. depends on !DEBUG_INFO_REDUCED && !DEBUG_INFO_SPLIT # Requires ELF object files. depends on !LTO + help + Calculate symbol versions from DWARF debugging information using + gendwarfksyms. Requires DEBUG_INFO to be enabled. + + If unsure, say N. +endchoice =20 config ASM_MODVERSIONS bool diff --git a/scripts/Makefile b/scripts/Makefile index d7fec46d38c0..8533f4498885 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -53,7 +53,7 @@ hostprogs +=3D unifdef targets +=3D module.lds =20 subdir-$(CONFIG_GCC_PLUGINS) +=3D gcc-plugins -subdir-$(CONFIG_MODVERSIONS) +=3D genksyms +subdir-$(CONFIG_GENKSYMS) +=3D genksyms subdir-$(CONFIG_GENDWARFKSYMS) +=3D gendwarfksyms subdir-$(CONFIG_SECURITY_SELINUX) +=3D selinux subdir-$(CONFIG_SECURITY_IPE) +=3D ipe diff --git a/scripts/Makefile.build b/scripts/Makefile.build index 8f423a1faf50..ae13afb71123 100644 --- a/scripts/Makefile.build +++ b/scripts/Makefile.build @@ -107,18 +107,28 @@ cmd_cpp_i_c =3D $(CPP) $(c_flags) -o $@ $< $(obj)/%.i: $(obj)/%.c FORCE $(call if_changed_dep,cpp_i_c) =20 +gendwarfksyms :=3D scripts/gendwarfksyms/gendwarfksyms +getexportsymbols =3D $(NM) $(1) | sed -n 's/.* __export_symbol_\(.*\)/$(2)= /p' + genksyms =3D scripts/genksyms/genksyms \ $(if $(1), -T $(2)) \ $(if $(KBUILD_PRESERVE), -p) \ -r $(or $(wildcard $(2:.symtypes=3D.symref)), /dev/null) =20 # These mirror gensymtypes_S and co below, keep them in synch. +ifdef CONFIG_GENDWARFKSYMS +symtypes_dep_c =3D $(obj)/%.o +cmd_gensymtypes_c =3D $(if $(skip_gendwarfksyms),, \ + $(call getexportsymbols,$(2:.symtypes=3D.o),\1) | \ + $(gendwarfksyms) $(2:.symtypes=3D.o) $(if $(1), --symtypes $(2))) +else cmd_gensymtypes_c =3D $(CPP) -D__GENKSYMS__ $(c_flags) $< | $(genksyms) +endif # CONFIG_GENDWARFKSYMS =20 quiet_cmd_cc_symtypes_c =3D SYM $(quiet_modtag) $@ cmd_cc_symtypes_c =3D $(call cmd_gensymtypes_c,true,$@) >/dev/null =20 -$(obj)/%.symtypes : $(obj)/%.c FORCE +$(obj)/%.symtypes : $(obj)/%.c $(symtypes_dep_c) FORCE $(call cmd,cc_symtypes_c) =20 # LLVM assembly @@ -314,19 +324,32 @@ $(obj)/%.ll: $(obj)/%.rs FORCE # This is convoluted. The .S file must first be preprocessed to run guards= and # expand names, then the resulting exports must be constructed into plain # EXPORT_SYMBOL(symbol); to build our dummy C file, and that gets preproce= ssed -# to make the genksyms input. +# to make the genksyms input or compiled into an object for gendwarfksyms. # # These mirror gensymtypes_c and co above, keep them in synch. -cmd_gensymtypes_S =3D = \ - { echo "\#include " ; = \ - echo "\#include " ; = \ - $(NM) $@ | sed -n 's/.* __export_symbol_\(.*\)/EXPORT_SYMBOL(\1);/p' = ; } | \ - $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) +getasmexports =3D \ + { echo "\#include " ; \ + echo "\#include " ; \ + echo "\#include " ; \ + $(call getexportsymbols,$(2:.symtypes=3D.o),EXPORT_SYMBOL(\1);) ; } + +ifdef CONFIG_GENDWARFKSYMS +cmd_gensymtypes_S =3D \ + $(getasmexports) | \ + $(CC) $(c_flags) -c -o $(2:.symtypes=3D.gendwarfksyms.o) -xc -; \ + $(call getexportsymbols,$(2:.symtypes=3D.o),\1) | \ + $(gendwarfksyms) $(2:.symtypes=3D.gendwarfksyms.o) \ + $(if $(1), --symtypes $(2)) +else +cmd_gensymtypes_S =3D \ + $(getasmexports) | \ + $(CPP) -D__GENKSYMS__ $(c_flags) -xc - | $(genksyms) +endif # CONFIG_GENDWARFKSYMS =20 quiet_cmd_cc_symtypes_S =3D SYM $(quiet_modtag) $@ cmd_cc_symtypes_S =3D $(call cmd_gensymtypes_S,true,$@) >/dev/null =20 -$(obj)/%.symtypes : $(obj)/%.S FORCE +$(obj)/%.symtypes : $(obj)/%.S $(obj)/%.o FORCE $(call cmd,cc_symtypes_S) =20 =20 --=20 2.47.0.rc0.187.ge670bccf7e-goog From nobody Wed Nov 27 16:50:05 2024 Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) (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 562D421B45C for ; Tue, 8 Oct 2024 18:39:12 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412755; cv=none; b=JtN0c4+KtSUHcmOMIHPyUq2QaWeyZSj2lZUB1bCjcMLxXzv/ioShh+/wjrKqg41S7G79psTFmH+VBBZo11QPf1DasGV7bJ17q16v1hFKpc4Bdgwqh4a8U0qgrJsZMVE0G0mLJGwVkbIYbtyjOHdoh2DVIoT6d1UdmuB7kc1/FCU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1728412755; c=relaxed/simple; bh=/LE4RdA8pusjopTkowR5788fIrd8xfB9an7aJ5zBUNQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ltOVMCMJKi0Evv1U8wXd3F+Jyd/k2BWUtTqnJBtZDAMI+qD/R8Dt8IFyGBNTj6nxNJsCHaBYUtvubbZdK8pNUC4XFdCngJmIZ+Sfwph+bBZojorka6YTEKUx26PLeBTbZjWc7bc48S4a3gl5aVwy4PW+K1YKSH41CI6QaBIs7wk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=hH5a3woj; arc=none smtp.client-ip=209.85.210.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--samitolvanen.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="hH5a3woj" Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-71dfeda9ac0so2709793b3a.1 for ; Tue, 08 Oct 2024 11:39:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1728412752; x=1729017552; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=noTpT7TMNCGwQMuY8x/MMQ5qdP9Xs2RZaKSa35+PR7Y=; b=hH5a3wojngBcRIdzmlRlFu34+BKL354cKJ+V2MZoyAEbwfJjoLQqpHMEPsCz3TwH4E 5Eu1ct3zgN8T3pJ435wmICFPNNhmO7hDgLybZNY8Xd4Cn0TVQqk51/o9hfvmVZqxQ032 c07yj3jWavMLGKLtl7KpqFBuV4PXz2PKhJ37X1UkhPWZLlSOiS2s7w0fNPFvdbHQbc+D XTBm0cfdNZoRZdoUmzdbXW/8IsJpWNE7QR9XJXcGSRYzyFriVMuVdo71FVch4MCzTfnO 8KgneeSDazNEcT56iAGeiO56NhuhvuxWN1368oPq3dntnAiqS4D4ZvZW4zezFhc9YJHq zxyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1728412752; x=1729017552; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=noTpT7TMNCGwQMuY8x/MMQ5qdP9Xs2RZaKSa35+PR7Y=; b=GYUtsDO008suvpe4f5SAobHsbOhh1A/OnUb+ZuKlfIobDjDoo0FgvQmGkcSjT77+i1 3rZKZOk7mNS6kj4vchqUYG13lRrkduBQJUupm4V1xrs45l0ypZYHKZuZLOaL4IkOgq+y t5cATTayrRtKVYFdzY33PDOKXNmyO5plduCD5SvYQSrULYPdwFeiBCnmlImvxA9qtRhk 4UI67jpQwr5uosha1m0rYPmEzYp8S3F6iEeJRqqObmld13MQ1sJAAN6Z8GwrWfi2BBw/ CKA6r0zAPImc/cCUwSEhsoVkcOVwhtkAPI2VVcOnvm4FG5Y1t6361tWu/YfA24RBWA+p Q64w== X-Forwarded-Encrypted: i=1; AJvYcCWC8FVnvpXrBSwbl4vWHW9Ef5vtDrGCtn7OamoDnEfV/oQ4xtZ8l01YQzC6Iq1sS0+GJqdESDgxhyRaJT0=@vger.kernel.org X-Gm-Message-State: AOJu0YyE97pkzVa2wMvd90JWIIxtwBXygJP27mC9QcVCJei2jb6pSogb pSzVoMtKhNq25BcUzonoP+vbW0abMT+147oC4gHSSjtKFgpA1XSWpY9gkjr7lfRMxn5RTy5Q2X7 VHtlNnlzSNn07O6CAaWprBgHQCw== X-Google-Smtp-Source: AGHT+IGtpxdMd1s0GqDjj3A1x/xHJTGLLYvXcWGscZ8OXRzRdLj3KYX1d6a0ORk0NdpZ6U/bTRE1O+oJekLFTGg+vgM= X-Received: from samitolvanen.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:4f92]) (user=samitolvanen job=sendgmr) by 2002:a05:6a00:7687:b0:71d:fb06:e79b with SMTP id d2e1a72fcca58-71dfb06ea3amr18395b3a.0.1728412751653; Tue, 08 Oct 2024 11:39:11 -0700 (PDT) Date: Tue, 8 Oct 2024 18:38:43 +0000 In-Reply-To: <20241008183823.36676-21-samitolvanen@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20241008183823.36676-21-samitolvanen@google.com> X-Developer-Key: i=samitolvanen@google.com; a=openpgp; fpr=35CCFB63B283D6D3AEB783944CB5F6848BBC56EE X-Developer-Signature: v=1; a=openpgp-sha256; l=11978; i=samitolvanen@google.com; h=from:subject; bh=/LE4RdA8pusjopTkowR5788fIrd8xfB9an7aJ5zBUNQ=; b=owGbwMvMwCEWxa662nLh8irG02pJDOmsNZrX723LiT3Rfdb16vnXSd/UjhoyJxac3rNv7RWde VtjFixc21HKwiDGwSArpsjS8nX11t3fnVJffS6SgJnDygQyhIGLUwAmcsiO4X/wtdATVcs3/4xP LlU7sqXt4avYOUX+58+KnQnf9EDMbXMhw/8y+/sWFWYex+9EvxMNLdOIiK1TXVNpdetpId9lLa9 1LFwA X-Mailer: git-send-email 2.47.0.rc0.187.ge670bccf7e-goog Message-ID: <20241008183823.36676-40-samitolvanen@google.com> Subject: [PATCH v4 19/19] Documentation/kbuild: Add DWARF module versioning From: Sami Tolvanen To: Masahiro Yamada , Luis Chamberlain , Miguel Ojeda , Greg Kroah-Hartman Cc: Matthew Maurer , Alex Gaynor , Gary Guo , Petr Pavlu , Daniel Gomez , Neal Gompa , Hector Martin , Janne Grunau , Miroslav Benes , Asahi Linux , Sedat Dilek , linux-kbuild@vger.kernel.org, linux-kernel@vger.kernel.org, linux-modules@vger.kernel.org, rust-for-linux@vger.kernel.org, Sami Tolvanen Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add documentation for gendwarfksyms changes, and the kABI stability features that can be useful for distributions even though they're not used in mainline kernels. Signed-off-by: Sami Tolvanen Acked-by: Neal Gompa --- Documentation/kbuild/gendwarfksyms.rst | 274 +++++++++++++++++++++++++ Documentation/kbuild/index.rst | 1 + 2 files changed, 275 insertions(+) create mode 100644 Documentation/kbuild/gendwarfksyms.rst diff --git a/Documentation/kbuild/gendwarfksyms.rst b/Documentation/kbuild/= gendwarfksyms.rst new file mode 100644 index 000000000000..4b89743d2a88 --- /dev/null +++ b/Documentation/kbuild/gendwarfksyms.rst @@ -0,0 +1,274 @@ +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D +DWARF module versioning +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +1. Introduction +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +When CONFIG_MODVERSIONS is enabled, symbol versions for modules +are typically calculated from preprocessed source code using the +**genksyms** tool. However, this is incompatible with languages such +as Rust, where the source code has insufficient information about +the resulting ABI. With CONFIG_GENDWARFKSYMS (and CONFIG_DEBUG_INFO) +selected, **gendwarfksyms** is used instead to calculate symbol versions +from the DWARF debugging information, which contains the necessary +details about the final module ABI. + +1.1. Usage +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +gendwarfksyms accepts a list of object files on the command line, and a +list of symbol names (one per line) in standard input:: + + Usage: gendwarfksyms [options] elf-object-file ... < symbol-list + + Options: + -d, --debug Print debugging information + --dump-dies Dump DWARF DIE contents + --dump-die-map Print debugging information about die_map c= hanges + --dump-types Dump type strings + --dump-versions Dump expanded type strings used for symbol = versions + -s, --stable Support kABI stability features + -T, --symtypes file Write a symtypes file + -h, --help Print this message + + +2. Type information availability +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D + +While symbols are typically exported in the same translation unit (TU) +where they're defined, it's also perfectly fine for a TU to export +external symbols. For example, this is done when calculating symbol +versions for exports in stand-alone assembly code. + +To ensure the compiler emits the necessary DWARF type information in the +TU where symbols are actually exported, gendwarfksyms adds a pointer +to exported symbols in the `EXPORT_SYMBOL()` macro using the following +macro:: + + #define __GENDWARFKSYMS_EXPORT(sym) \ + static typeof(sym) *__gendwarfksyms_ptr_##sym __used \ + __section(".discard.gendwarfksyms") =3D &sym; + + +When a symbol pointer is found in DWARF, gendwarfksyms can use its +type for calculating symbol versions even if the symbol is defined +elsewhere. The name of the symbol pointer is expected to start with +`__gendwarfksyms_ptr_`, followed by the name of the exported symbol. + +3. Symtypes output format +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +Similarly to genksyms, gendwarfksyms supports writing a symtypes file +for each processed object that contain types for exported symbols and +each referenced type that was used in calculating symbol versions. These +files can be useful when trying to determine what exactly caused symbol +versions to change between builds. + +Matching the existing format, the first column of each line contains +either a type reference or a symbol name. Type references have a +one-letter prefix followed by "#" and the name of the type. Four +reference types are supported:: + + e# =3D enum + s# =3D struct + t# =3D typedef + u# =3D union + +Type names with spaces in them are wrapped in single quotes, e.g.:: + + s#'core::result::Result' + +The rest of the line contains a type string. Unlike with genksyms that +produces C-style type strings, gendwarfksyms uses the same simple parsed +DWARF format produced by **--dump-dies**, but with type references +instead of fully expanded strings. + +4. Maintaining a stable kABI +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D + +Distribution maintainers often need the ability to make ABI compatible +changes to kernel data structures due to LTS updates or backports. Using +the traditional `#ifndef __GENKSYMS__` to hide these changes from symbol +versioning won't work when processing object files. To support this +use case, gendwarfksyms provides kABI stability features designed to +hide changes that won't affect the ABI when calculating versions. These +features are all gated behind the **--stable** command line flag and are +not used in the mainline kernel. + +Examples for using these features are provided in the +**scripts/gendwarfksyms/examples** directory, including helper macros +for source code annotation. Note that as these features are only used to +transform the inputs for symbol versioning, the user is responsible for +ensuring that their changes actually won't break the ABI. + +4.1. kABI rules +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +kABI rules allow distributions to fine-tune certain parts +of gendwarfksyms output and thus control how symbol +versions are calculated. These rules are defined in the +`.discard.gendwarfksyms.kabi_rules` section of the object file and +consist of simple null-terminated strings with the following structure:: + + version\0type\0target\0value\0 + +This string sequence is repeated as many times as needed to express all +the rules. The fields are as follows: + +- `version`: Ensures backward compatibility for future changes to the + structure. Currently expected to be "1". +- `type`: Indicates the type of rule being applied. +- `target`: Specifies the target of the rule, typically the fully + qualified name of the DWARF Debugging Information Entry (DIE). +- `value`: Provides rule-specific data. + +The following helper macro, for example, can be used to specify rules +in the source code:: + + #define __KABI_RULE(hint, target, value) \ + static const char __PASTE(__gendwarfksyms_rule_, \ + __COUNTER__)[] __used __aligned(1) \ + __section(".discard.gendwarfksyms.kabi_rules") =3D \ + "1\0" #hint "\0" #target "\0" #value + + +Currently, only the rules discussed in this section are supported, but +the format is extensible enough to allow further rules to be added as +need arises. + +4.1.1. Managing structure visibility +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +A structure declaration can change into a full definition when +additional includes are pulled into the translation unit. This changes +the versions of any symbol that references the structure even if the ABI +remains unchanged. As it may not be possible to drop includes without +breaking the build, the `struct_declonly` rule can be used to specify a +data structure as declaration-only, even if the debugging information +contains the full definition. + +The rule fields are expected to be as follows: + +- `type`: "struct_declonly" +- `target`: The fully qualified name of the target data structure + (as shown in **--dump-dies** output). +- `value`: This field is ignored and is expected to have the value ";". + +Using the `__KABI_RULE` macro, this rule can be defined as:: + + #define KABI_STRUCT_DECLONLY(fqn) \ + __KABI_RULE(struct_declonly, fqn, ;) + +Example usage:: + + struct s { + /* definition */ + }; + + KABI_STRUCT_DECLONLY(s); + +4.1.2. Adding enumerators +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D + +For enums, all enumerators and their values are included in calculating +symbol versions, which becomes a problem if we later need to add more +enumerators without changing symbol versions. The `enumerator_ignore` +rule allows us to hide named enumerators from the input. + +The rule fields are expected to be as follows: + +- `type`: "enumerator_ignore" +- `target`: The fully qualified name of the target enum + (as shown in **--dump-dies** output). +- `value`: The name of the enumerator to ignore. + +Using the `__KABI_RULE` macro, this rule can be defined as:: + + #define KABI_ENUMERATOR_IGNORE(fqn, field) \ + __KABI_RULE(enumerator_ignore, fqn, field) + +Example usage:: + + enum e { + A, B, C, D, + }; + + KABI_ENUMERATOR_IGNORE(e, B); + KABI_ENUMERATOR_IGNORE(e, C); + + +4.3. Adding structure members +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D + +Perhaps the most common ABI compatible changeis adding a member to a +kernel data structure. When changes to a structure are anticipated, +distribution maintainers can pre-emptively reserve space in the +structure and take it into use later without breaking the ABI. If +changes are needed to data structures without reserved space, existing +alignment holes can potentially be used instead. While kABI rules could +be added for these type of changes, using unions is typically a more +natural method. This section describes gendwarfksyms support for using +reserved space in data structures and hiding members that don't change +the ABI when calculating symbol versions. + +4.3.1. Reserving space and replacing members +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +To reserve space in a struct, adding a member of any type with a name +that starts with `__kabi_` will result in the name being left out of +symbol versioning:: + + struct s { + long a; + long __kabi_reserved_0; /* reserved for future use */ + }; + +The space reserved by this member can be later taken into use by +wrapping it into a union, which includes the original type and the +replacement struct member:: + + struct s { + long a; + union { + long __kabi_reserved_0; /* original type */ + struct b b; /* replaced field */ + }; + }; + +As long as the reserved member's name in the union starts with +`__kabi_reserved_`, the original type will be used for symbol +versioning and rest of the union is ignored. The examples include +`KABI_(RESERVE|USE)*` macros that help simplify the process and also +ensure the replacement member's size won't exceed the reserved space. + +4.3.2. Hiding members +=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D + +Predicting which structures will require changes during the support +timeframe isn't always possible, in which case one might have to resort +to placing new members into existing alignment holes:: + + struct s { + int a; + /* a 4-byte alignment hole */ + unsigned long b; + }; + + +While this won't change the size of the data structure, one needs to +be able to hide the added members from symbol versioning. Similarly +to reserved fields, this can be accomplished by wrapping the added +member to a union where one of the fields has a name starting with +`__kabi_ignored`:: + + struct s { + int a; + union { + char __kabi_ignored_0; + int n; + }; + unsigned long b; + }; + +With **--stable**, both versions produce the same symbol version. diff --git a/Documentation/kbuild/index.rst b/Documentation/kbuild/index.rst index cee2f99f734b..e82af05cd652 100644 --- a/Documentation/kbuild/index.rst +++ b/Documentation/kbuild/index.rst @@ -21,6 +21,7 @@ Kernel Build System reproducible-builds gcc-plugins llvm + gendwarfksyms =20 .. only:: subproject and html =20 --=20 2.47.0.rc0.187.ge670bccf7e-goog