From patchwork Thu Dec 16 20:19:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682471 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7696FC433FE for ; Thu, 16 Dec 2021 20:20:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241319AbhLPUUG (ORCPT ); Thu, 16 Dec 2021 15:20:06 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:33392 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbhLPUUE (ORCPT ); Thu, 16 Dec 2021 15:20:04 -0500 Received: from pps.filterd (m0246629.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGInWvN005091; Thu, 16 Dec 2021 20:19:52 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=fdoVdxf6gDSe9Z0aQq/l4ni0Hbq7nNmEB5szE8POLEI=; b=tGSIWMPxA8f1Vjx3c7FdfO6uLc+gB7q4ak3lUrKdpz/zbanLIP9mpLjAEGLrVe8r6Kph a7EHVVP0LRm1rXh2UEnvhaUxi2VKVqjlNiZrT6ryidZJqwEfSw7y6iPcSr7kIeZh9w9a 4QUzb+OUNpq2ZcnbFyGfIxIPjNbwv0e4B/6hsbNXTFzXxrHXHet7a8A+FchzyWgKpuTE cch1ghzlR2F9tu5iU0Fb99jsOi/ZR6rVVQYC9D0oP1at3BIWnALUITpyysODaC40Pm3k nIiyzqheAgfmDqMbzL8zLwgBUQRGz96eDEdqBgB7etr2LWYW9VfMVsU/U241ro3W9qpo fg== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3cyknrkvau-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:52 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGD96123962; Thu, 16 Dec 2021 20:19:50 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2106.outbound.protection.outlook.com [104.47.70.106]) by aserp3030.oracle.com with ESMTP id 3cyjuae09p-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:50 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=WPwZFC5V4tCnZEHI4IcLX4fosKYOyUHr/tODay9pc1BpRIGATBkWbzof8Y9kPjAP2WA8OWb0rRJcI6+m7fHEXJJE1X29ZdqqoRqfAtd9cKH+iLN1p9/hIudMZj4B1ekYM38Uw85ty6RULCYHezZxzieZYlpudokCKvww8mm8YenFOhkLMl/uD1Z4+2ghkv38W5l95XH9c+A325KUOWI88L2Weybfmme68Aj/y7JJKSfqQEvhP86bUmQ2jQXSeUYE6Gsv0Vu5sxb6qnOjJzuXiyZdmHYzVtPSgJkDcKS3vR6iin9w1pRZm70o/83ZqeRruiopQO31ppZS5pK4h5vzVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=fdoVdxf6gDSe9Z0aQq/l4ni0Hbq7nNmEB5szE8POLEI=; b=hlMgCBqP/oJI85P1CxuhzTMQIdMjWIHAcYzzsoKRBKxwiQWQzG3bB1dGu3mUt7llhzxK1/31EnlEWGFDl4ER2EzY+V3Fp65dhsjDLKBqvbs1uoEFrQb1zGBH2ZKwLKvm5RM49EWZtfA2vxI+GIwabG6IP3AM+GF9uLdJki2A3A+zTsplQ+kMKheUf6mJxrUEjHA9XRohZOeCgCkb/p8c57mgKnnhqhkPQHgauE3CWczo6BnoaTnj/gSx/Vp4O1I9jjh0nwL6N5MIcVTSgBthf/KDaALhaH04rGE1nAi7huc5ZXJIY3EctzGX55tabL+5pkqHb3xGKA4nPdXaGLiH2A== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=fdoVdxf6gDSe9Z0aQq/l4ni0Hbq7nNmEB5szE8POLEI=; b=nzza5DNwuztVuGfMAvp9anzwWkZ8Z6w5NKkQo1T2d5oqSPItKz2Gh3lVsCt1bufqOimrvWwc1kFEqbEUMda4hzBsICWmEiJ/RrAFz8IE/4J2MFA0Iq/jiCgcsIYxb+NEFqynnm58TycL466j0cea3XYCcH0WQuf7l4FeWGbE8uo= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:48 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:48 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 1/7] kbuild: bring back tristate.conf Date: Thu, 16 Dec 2021 20:19:13 +0000 Message-Id: <20211216201919.234994-2-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e9ec893e-00f2-46de-590a-08d9c0d168b7 X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:3826; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: Jkf4OqrbhvRv4cAR65cY8gjko2kA9ujF+jmrFW2o0BVmwsqOlBgN83J6AWeRFgl1RKWrX3T8iw4CauimELGvYlNyEtddPGkltqMv6KvlxLdA3Cnw8VSXIhQXLAe0Yhl5D/Yn88GTT0J8e0JrH/nkeQGU9yNbVYRSCOTHpRlg90vGdY7lnnvyiPP5b+ct5FTX2yDzThlfWH53VR8UUVL8Vt2tuc4cSGL/PXj+IEqCnujdC5Uq4PXvlYb6k6LrVExRptFlt1PtYuGmKGmbqsyBeesoMDnfkdMcFJ2JBe5hTYv3Y9Edv5bwyZsMSRc5gHV1v5uZe3lfgoCYGjXwmQ/LVbhZZe8h3P0vm2E8Vd4lQGOGdyXLgVJBhqgblKmgK7/d5bE4PEddnlRrp7ftt7dMVyNAIizd5Z76JfvGtwM5jNiVtxD0Chde+elTuZ3580iSRyWqcGSUO7NFVN/7KcbAG520/XewxW2Ab5zSouUI5KYc0N/9jME7g0izB0+GRC8NXY9gYmniPfR+5Dibg4DdDAvypLIerPF+RIKIN8+fhHJ1rzAPuOcWyZ5P2wOFofxh128HgNFha1MxW3JEk0N+U7RizrcvNAA6m5fqE7eeZKHzRK4aAkJsC0kc3UNnwBk+rU8iknxhUAun16SHgGT3ig== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: tGkqpck4vik8X7QNbOvS7JUZw9impNjqBqQQ0JMFTZ0zTNPROBVsLgrLuadyZXooHZcnPuYt5sqTr2zpBr27FGamR/w/OBm2zJS9tYaje6O0ny5NkqsNVVKdSf8AT4AD42YckPs+e8qL3qOLAUyHKg90eOvjUKJHV2ArOgKwTlL6P1WWtObj++6WkDUzeNih3oKel24paQWCemr8C2wrxiuhJXxghVMpYAdTo861rIG6+eDmJjh6xtEfQwDGfj5agv+N5Vj72S8NecqQRl4PFlU4fZNd/EdzD2x6n96O4dMthIc+z8tl/9xyla0g92nm2R4HAmHk455IYSeb9UNByLLGFKqde/XueUogqBT6BNvRn4I4O1y7IcNp8pBk5MZP+0ReYVzr5HRDKPUYAJT5jTdexN+Vk8fgCBNBn3vK/4PFkuv99cQLUEA1QMnl9I6hfrz7BfClMr3dfrKaL98iyzY0qYTF6ncs0tNt4/Z9yQBsK5p/LpAuxgKOy7chc+pbjaRHm5injxr8fe7u0m+zopWjbzHbWgu3c+hH9nnn5N96B7WNvIXMfhD4gbzgbKXUUjp3sGVbmF4CJxUr2fGde4Ldg0sCvePKmTmlw4PSVzb4Xelm7eHF/XQ+pS0vOdwA7wEhL7X0Di2TEDqZ/gs506A+XKlYx9PPugTKn0QkPCaXlaExizRkiT54m7Ems0FrLNXnCJNs7wEJRtDIiN0JrqTw82T44/U7o/WYNrGtbTBQiySQY4w18QTCcAYoFw81QQN88uxT3MO9TR9oRULNKeu/COlQj6ksOYB9Fs2ohXyERNwVileunWd1N51VUh8llqdyEWS0bM//NaMU28VDNDGN7UmtoHeRXVfgoXvU/OjxipAYD1IVzorEgtJSV5lMQa2KoJ+ZQMOR5ZCwqPrDpDaZm0jqYt44E14Q7q5weiEBhImHWC5U322BzXei46ju54UkgNUG0je5CpXEOQ8AVAXeygiTfkGYb2TWta0NnVFCqtJ9+Qhj0DkPXQgz3vhtxDiOy7UbsRDL46IjggIUy/Dmj0wfA1GQsYdP1MmHsyEEKuffwQo9F74mvHCh3WparrHmXsONPgQcGBa0fje0gjADlT9p5Z+5C0NdLIUQPajqb25G+po96SHSz2umByzUhY0kD4iRWeZBUDxakcihSnOT3/WTvvm9OGYpDj9Hojd78at7pxu+8ms7kAcjRracq23PQKWLXi0ny5P7JRyuqOqrjZY8+Q91SVdkC+V4jKA3+rR+QArKRuVfXOmUXkSqtMNWpnofJuhGwz5KUkAu0734TrdCDAHovwcm4cS+iHemdXppCcOsiNO32WxHRo36ou9aVBCYtnUz3vJem6Lye9XpgsKyHAJBLQeRakaqAxWUmtP9V23p6E50QO4jXpGYfp00CRZyM34hvyKquZVVsWWNXEfN33XllxlSdKg3hMTtls+GYBb44ddmcY7tnhp7JjdDVX4fasPGr1KQoDUbihvLqYax4Dc3s/kHw/YPr7vpSAjSbmHDLvLWbgAcM4K9872ZhqVqQWp/xQJb95pdYulyrbPRhEjbrBCzLQD8pc6DIhRsT6Vz3Hj+R6NVlhEEr6HkXO9ApSCZzAomVsb9L0f2LylPnHxqMrHXa0EdDrx9z98chg4QXmOm5MWj2f+d X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e9ec893e-00f2-46de-590a-08d9c0d168b7 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:48.8273 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: 8EEF/Eru4kQ5FwCu0ExttJC2jYwSHmG2KpyIi5b4CKp07gqxmK763zd30eZ5+MKSX7CuntPBrDso9dgYH63+xg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 adultscore=0 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: MpVCiALNpVDKyUzrV0fT_EymCDgnYy9H X-Proofpoint-GUID: MpVCiALNpVDKyUzrV0fT_EymCDgnYy9H Precedence: bulk List-ID: tristate.conf was dropped because it is not needed to build a modules.builtin (although dropping it introduces a few false positives into modules.builtin support), and doing so avoids one round of recursion through the build tree to build it. But kallmodsyms support requires building a mapping from object file name to built-in module name for all builtin modules: this seems to me impossible to accomplish without parsing all makefiles under the influence of tristate.conf, since the makefiles are the only place this mapping is recorded. So bring it back for this purpose. (Thanks to the refactoring in the 5.16 timeframe, this is basically a reimplementation of commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29 rather than a simple reversion.) Signed-off-by: Nick Alcock --- Notes: v7: rewrite in terms of the new confdata refactoring Documentation/kbuild/kconfig.rst | 5 +++++ Makefile | 2 +- scripts/kconfig/confdata.c | 36 +++++++++++++++++++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/Documentation/kbuild/kconfig.rst b/Documentation/kbuild/kconfig.rst index 5967c79c3baa..e2c78760d442 100644 --- a/Documentation/kbuild/kconfig.rst +++ b/Documentation/kbuild/kconfig.rst @@ -162,6 +162,11 @@ KCONFIG_AUTOCONFIG This environment variable can be set to specify the path & name of the "auto.conf" file. Its default value is "include/config/auto.conf". +KCONFIG_TRISTATE +---------------- +This environment variable can be set to specify the path & name of the +"tristate.conf" file. Its default value is "include/config/tristate.conf". + KCONFIG_AUTOHEADER ------------------ This environment variable can be set to specify the path & name of the diff --git a/Makefile b/Makefile index 765115c99655..879066d1af4b 100644 --- a/Makefile +++ b/Makefile @@ -727,7 +727,7 @@ $(KCONFIG_CONFIG): # # Do not use $(call cmd,...) here. That would suppress prompts from syncconfig, # so you cannot notice that Kconfig is waiting for the user input. -%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h: $(KCONFIG_CONFIG) +%/config/auto.conf %/config/auto.conf.cmd %/generated/autoconf.h %/tristate.conf: $(KCONFIG_CONFIG) $(Q)$(kecho) " SYNC $@" $(Q)$(MAKE) -f $(srctree)/Makefile syncconfig else # !may-sync-config diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c index 42bc56ee238c..1d30faff6984 100644 --- a/scripts/kconfig/confdata.c +++ b/scripts/kconfig/confdata.c @@ -216,6 +216,13 @@ static const char *conf_get_autoheader_name(void) return name ? name : "include/generated/autoconf.h"; } +static const char *conf_get_tristate_name(void) +{ + char *name = getenv("KCONFIG_TRISTATE"); + + return name ? name : "include/config/tristate.conf"; +} + static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) { char *p2; @@ -666,7 +673,8 @@ static char *escape_string_value(const char *in) enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE }; static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, - bool escape_string) + bool escape_string, bool upcase_string, + bool tristate_only) { const char *val; char *escaped = NULL; @@ -674,6 +682,9 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, if (sym->type == S_UNKNOWN) return; + if (sym->type != S_TRISTATE && tristate_only) + return; + val = sym_get_string_value(sym); if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) && @@ -688,24 +699,32 @@ static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n, val = escaped; } - fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); + if (upcase_string) + fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*val)); + else + fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val); free(escaped); } static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym) { - __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true); + __print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true, false, false); } static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym) { - __print_symbol(fp, sym, OUTPUT_N_NONE, true); + __print_symbol(fp, sym, OUTPUT_N_NONE, true, false, false); +} + +static void print_symbol_for_tristate(FILE *fp, struct symbol *sym) +{ + __print_symbol(fp, sym, OUTPUT_N_NONE, true, true, true); } void print_symbol_for_listconfig(struct symbol *sym) { - __print_symbol(stdout, sym, OUTPUT_N, true); + __print_symbol(stdout, sym, OUTPUT_N, true, false, false); } static void print_symbol_for_c(FILE *fp, struct symbol *sym) @@ -1129,6 +1148,13 @@ int conf_write_autoconf(int overwrite) if (ret) return ret; + + ret = __conf_write_autoconf(conf_get_tristate_name(), + print_symbol_for_tristate, + &comment_style_pound); + if (ret) + return ret; + /* * Create include/config/auto.conf. This must be the last step because * Kbuild has a dependency on auto.conf and this marks the successful From patchwork Thu Dec 16 20:19:14 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682465 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8395C433EF for ; Thu, 16 Dec 2021 20:20:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241290AbhLPUT7 (ORCPT ); Thu, 16 Dec 2021 15:19:59 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:4256 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229521AbhLPUT7 (ORCPT ); Thu, 16 Dec 2021 15:19:59 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn6ie016283; Thu, 16 Dec 2021 20:19:53 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=b14iBLRWU0nVcSXUIqFX1pJwnsPw3Y8nCAGtD4diYDA=; b=R1JgLXaAMtiPLAsENRvOXieP3GMhD2jBlbznxIdVnHqx8tDs7Qvc53v7H4Isk/CH66fs H9hHgxdwsOlD3qSCsRSC5YsO0/wdO7JiW61AO2aig6VD1zj6lutqEq6HDuwyET5j+clw vS9mSub7OkblnYt/YDQqZY4T7HLh493OZyCphs5MSOPKoZbERZcdN6tjVPLdqmKVLeLR hphhC0ZekumBTBPW6B6OolzpZ/kZqvft0qDaequqELNzWSKIWKvfmktpEIc4WrnY6FE2 lIVI9xJ3tccEGvjlCcdYOeZ70zMqnlVXg3/iYQb2N7XcoLYOmIGnsy18Stj00D33W9or IA== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3cykmbky5y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:53 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGbqn047602; Thu, 16 Dec 2021 20:19:52 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2101.outbound.protection.outlook.com [104.47.70.101]) by aserp3020.oracle.com with ESMTP id 3cxmrdxb5q-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:52 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=QFiLX40OocrEOmzPrqJhGJgdrU7CkNojY3It8qO/f7I0pDMSy10d8vUVSHj4Zyr0xDCWOw6JgpiNWxQMUeR9rq7JBbjP0yB9ZoIiNGsf93B/nIen9JGJlPg2+1f5EbPskc4UUwpZe4wSHuyyIwExJTnj6daz3k584bNWf/x5dXVemxS81LjCcTXxsbWIIOGvzVhSsV8Z3kaPVLOsmw7yQ9giFteacqX5ufD5NRPDwybuQ836E+nz/dcaD/Z1Udr6+6ryTGdNYLS2FLJZAIJJJ8O+q3wk/LmBQ00kBxbKZAUnCD76ssBhRaXTLAO/JUIOFt/ZNoIqgUztbOj4kjKN9Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=b14iBLRWU0nVcSXUIqFX1pJwnsPw3Y8nCAGtD4diYDA=; b=iVtQ9l5v7rIDC3N2rAoKQ5wJcvD+1EvSfigJWdqPX/MhlH8UBECn3lKcYj/s9NXgrPNcadW//yO1jx/r+eLM4RN7xaFcOi+OqVH1RT+DrfVKI1/GNvMC+HYFx/MazJYiXJIQHhPDV7WeY/ni9btxPHW5K/pqNifiPDXEhWSfUuI0VAiHVrRP01T8EzlRXskkQA1K0MUdIhXp5skz1olGqcEnz3FjunQFQLomR9li8eyXQsXRD8XJA3tVwsrTZeWkz7t4VkmY7Alu+OIW8/yxjoF1CTCy+ETTG1gyDmeqxPak9FjYeqKXm6quNhjNQgL58a/xqj2/8ejfPE2eF85O3g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=b14iBLRWU0nVcSXUIqFX1pJwnsPw3Y8nCAGtD4diYDA=; b=oLaC5Rx2jpocxfyoz7PgAELBvHJqG4igH0DGKCJQsJOqtQQ9sNZx/n9pb0qY6pMnsqMKzI76KVM6P1oQP43lT0BtgoS3CQy9c11VkW0M9sWmx77Vhh2VqEQhHB2aU76z198tId5p3cCcJsmcRHQWGFQ9MBG1foEjyOfoEdyrJWM= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:50 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:50 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 2/7] kbuild: add modules_thick.builtin Date: Thu, 16 Dec 2021 20:19:14 +0000 Message-Id: <20211216201919.234994-3-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c3ac7cb4-bdb0-455a-5595-08d9c0d169a1 X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6108; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: o0v6+HychBpRunCBxU4xBijDgQLK7bzJ44oJ8572wlsKBT+tpbZioWc/sdmUq20B7SqxQkWtBTZGXou5TouMi2yBK+u2YgdgBj1d0SlgMYxbnpOPykd35zij/CZqCIZ1szU2x6YA2cHwrRVW5Sb1tojuFBjT1APpYK4ZuD3KV8z80/gyQrdQfDY5aZU0EX6IgSUdyowWJp2lby8dZ0fCC99kmUJOr78d7BD1VplwHY/lfpL/eUr3rz7Or5RYxU29M5e9RduHV6WqxMY2LJqur1LQNzSiU6YJFJ5wKsUw5Rhk9aUn1S6fZZ8ql/WYfXlPkI65aYDrFu25vJPE5+SICgsGVa1vervMmmA6pJi49i/+ZemM3RKmRcHUzmXzRYmsE7eyJ3M5cbbW9WadjmTdoQH6fufkV0T2mMPItCaDoH4Sf/w7LfU2zmsPAlkfLCyiYYTCjeo6kkT3wmXH/8ePllp3gqpgX6huhYu2LlbZuxdM6z+Xm4wODtBKT0bKgQtt94AiC2/dIMcGl9pcF9/SJ2bUUopqurr0qavvNO2O6vAynVXARAanjxW5sRYvkOpuj7Tpn8SjCmM8iyUshX3jbV8s6xdwDQbProW/yXbjdyLFadId8vqe6ZO/wpXfpwotzGgaXqrAYMdFIsp4KjDJslO/hQuvZtAwNBjicw/XFOcQoLlNw3uCCxfwvMpn/uxXxALyUqymtviIhBSr3wTVgg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(30864003)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007)(2004002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: XL43zFdmKUCo+2WlYCVIdNmnhPQIipWAAKxl1QCeG1pCsr43dhuj2hsPPgPXsGegqUYOL4PNUACt2MvJSFsuyiZSY/H6xs2Pnat/81ZrYPkioOYCMEwKVlV538elUSLPfvnZRRU+hctwPHC2lUFRz/ZaCkyAwMwEbinYSDchlVtag8056h/slMkSkvop6C+3aDft/oiPXGb0nxozqyQnDZH5uBqv9jGZeUzJBP359I6/XendbCA+tCec8NbG1GuYX+nm0NbIAF7+hpM5Qa9KrWulyE/NK2C6Wvqsjg//gr7rSPGcvsTr+dgTD0rLccCW3KGW/GG55qy5NQoHfutUSWDmtVdzxz1Z6BvNfMRpP4u4Ex8s5GQMzCO10dYP7oj5TUv361uFjhhWdldFSeQhSWv4P6ktM+By/AR24s/xovoYNm2mrm4ZAHp/0W31S4QtySlATZJsY/rOo71e8+frdYxFq1ztkWjfW2OGl9LxJzN0YGxY74HBv6p0cEj9S2taDFu0NfAj2dStJoYgxvT3lJnpUiC2AYj4mS60OyYR6iexAact1l6SacRBK4ytGDiFcRvkS2L+RzzA8a1VcjoYougyxgGl5nXVbgp8HVHuk9Tx9OL1zqbbv0RGKsl0EskyAyVzFL96gFdE5MSOiyXmLGBruUiwcWZG7AlHB7nAkshJQwPnia0O4Wc/V/C8k5ucURKb2pQIIFsW7d/Gn+69ZMWwo7bHKAQahJ9P5sBf1yPTsbLTNOCV3OgSLOL2rWA8H1n7MeQI+sRwuURXhzUJQCg4Z4yqPrD67PMLhTghXeSdKbJQGe5RGT3Lai1llvVfkGbc1vpNQ6zI0hhnHLbCAfub4W/+msXpMkdQCevOsv9J9wqI9cjXK4rw/wOk4OjwPOxcw2VksTSZpmSB0Gybl+vYIwQKjNn+7g6/359VQKlFdj7iuiDBOuL/LjQ8edWxl4yvM3TKLMblD/F2qow7E0SUkYIxUrHWBVEwg4Z+MIWyKZMQkb1YYkfce1QIyHgMno7wrni16Xa2I5tYkkl5LuWRN2xRk+wp/B4i+UYWG8vtRUktBdUkC8jP2fbCBh6zLWS8bSS4pDutM4jr/wBVcLlwpLr2imT3G2JVh+FNK5SS7f0xb/6GaIrUWaJ1y5uXwfoiPfXkNoGqK/8BPLXP8ziZPj/oHIabz2xrvctWkLfaekcXG9DgNagODIPLsEK5KcAuMFopwK794dv1MH3gDfvb5Ugum6NC92U5XI2dCHYR24orj1/6c0bwrTirOasqj994UQ36PYZw6+9QcKGoXVPrNLnNOz/UcknaZ5q1S/BZblvvd9BYpJkR3Nv2VlTXgawY34/rEV4UMTatuEP+Vzj8nbPAoGYm80J9kiSLlVKYri4Al2ukTzIYP1zUbIwaE0+BuK8m5HJ6ZVVrx7IR+X0KqgiOtqgbcfC1wxGPdb0AM9+WQVWkFmrzhSFQMCbU5jaxhVIo/VZ+rDPlQ6MDomFudFwjFL0H4E+tdWWyLN86U060VVLfd7+H0nAX+KnjM2TGMcHtk8yl34ZW9XpOVKdrlDBz7bHLfd3WxydjfkhDInvwJYDVcXvjScZ51Oq80r4OI3GE5iLSb687FK1tEF1RpVXBOt+mlCuTCEMioiUSIqMh8BOqWEdwLohszuoC X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c3ac7cb4-bdb0-455a-5595-08d9c0d169a1 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:50.3608 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: x+Ur41n092RHBWS0+Cf9wRjDOfTqRtTvts4Ol5EngPCgOE8jXHqNof1GYClUmrN7lzlv560yYGTzn6lnjjh59A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: YS6tnhHrqPi_Qy53VYpt8bxI8qUj51mu X-Proofpoint-GUID: YS6tnhHrqPi_Qy53VYpt8bxI8qUj51mu Precedence: bulk List-ID: This is similar to modules.builtin, and constructed in a similar way to the way that used to be built before commit 8b41fc4454e36fbfdbb23f940d023d4dece2de29, via tristate.conf inclusion and recursive concatenation up the tree. Unlike modules.builtin, modules_thick.builtin givs the names of the object files that make up modules that are comprised of more than one object file, using a syntax similar to that of makefiles, e.g.: crypto/crypto.o: crypto/api.o crypto/cipher.o crypto/compress.o crypto/memneq.o crypto/crypto_algapi.o: crypto/algapi.o crypto/proc.o crypto/scatterwalk.o crypto/aead.o: crypto/geniv.o: (where the latter two are single-file modules). An upcoming commit will use this mapping to populate /proc/kallmodsyms. A parser is included that yields a stram of (module, objfile name[]) mappings: it's a bit baroque, but then parsing text files in C is quite painful, and I'd rather put the complexity in here than in its callers. The parser is not built in this commit, nor does it have any callers yet; nor is any rule added that causes modules_thick.builtin to actually be constructed. (Again, see a later commit for that.) I am not wedded to the approach used to construct this file, but I don't see any other way to do it despite spending a week or so trying to tie it into Kbuild without using a separate Makefile.modbuiltin: unlike the names of builtin modules (which are also recorded in the source files themseves via MODULE_*() macros) the mapping from object file name to built-in module name is not recorded anywhere but in the makefiles themselves, so we have to at least reparse them with something to indicate the builtin-ness of each module (i.e., tristate.conf) if we are to figure out which modules are built-in and which are not. Signed-off-by: Nick Alcock --- .gitignore | 1 + Documentation/dontdiff | 1 + Makefile | 19 +++- scripts/Kbuild.include | 6 ++ scripts/Makefile.modbuiltin | 56 ++++++++++ scripts/modules_thick.c | 200 ++++++++++++++++++++++++++++++++++++ scripts/modules_thick.h | 48 +++++++++ 7 files changed, 330 insertions(+), 1 deletion(-) create mode 100644 scripts/Makefile.modbuiltin create mode 100644 scripts/modules_thick.c create mode 100644 scripts/modules_thick.h diff --git a/.gitignore b/.gitignore index 7afd412dadd2..b49cd96f587a 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ *.zst Module.symvers modules.order +modules_thick.builtin # # Top-level generic files diff --git a/Documentation/dontdiff b/Documentation/dontdiff index 910b30a2a7d9..6a78988547d0 100644 --- a/Documentation/dontdiff +++ b/Documentation/dontdiff @@ -183,6 +183,7 @@ modules.builtin modules.builtin.modinfo modules.nsdeps modules.order +modules_thick.builtin modversions.h* nconf nconf-cfg diff --git a/Makefile b/Makefile index 879066d1af4b..f94b5f05e9f5 100644 --- a/Makefile +++ b/Makefile @@ -1485,6 +1485,23 @@ __modinst_pre: endif # CONFIG_MODULES +# modules_thick.builtin maps from kernel modules (or rather the object file +# names they would have had had they not been built in) to their constituent +# object files: we can use this to determine which modules any given object +# file is part of. (We cannot eliminate the slight redundancy here without +# double-expansion.) + +modthickbuiltin-files := $(addsuffix /modules_thick.builtin, $(build-dirs)) + +modules_thick.builtin: $(modthickbuiltin-files) + $(Q)$(AWK) '!x[$$0]++' $(addsuffix /$@, $(build-dirs)) > $@ + +# tristate.conf is not included from this Makefile. Add it as a prerequisite +# here to make it self-healing in case somebody accidentally removes it. +$(modthickbuiltin-files): include/config/tristate.conf + $(Q)$(MAKE) $(modbuiltin)=$(patsubst %/modules_thick.builtin,%,$@) builtin-file=modules_thick.builtin + + ### # Cleaning is done on three levels. # make clean Delete most generated files @@ -1864,7 +1881,7 @@ clean: $(clean-dirs) -o -name '*.lex.c' -o -name '*.tab.[ch]' \ -o -name '*.asn1.[ch]' \ -o -name '*.symtypes' -o -name 'modules.order' \ - -o -name '.tmp_*.o.*' \ + -o -name '.tmp_*.o.*' -o -name modules_thick.builtin \ -o -name '*.c.[012]*.*' \ -o -name '*.ll' \ -o -name '*.gcno' \ diff --git a/scripts/Kbuild.include b/scripts/Kbuild.include index cdec22088423..ef68635831b0 100644 --- a/scripts/Kbuild.include +++ b/scripts/Kbuild.include @@ -74,6 +74,12 @@ endef # $(Q)$(MAKE) $(build)=dir build := -f $(srctree)/scripts/Makefile.build obj +### +# Shorthand for $(Q)$(MAKE) -f scripts/Makefile.modbuiltin obj= +# Usage: +# $(Q)$(MAKE) $(modbuiltin)=dir +modbuiltin := -f $(srctree)/scripts/Makefile.modbuiltin obj + ### # Shorthand for $(Q)$(MAKE) -f scripts/Makefile.dtbinst obj= # Usage: diff --git a/scripts/Makefile.modbuiltin b/scripts/Makefile.modbuiltin new file mode 100644 index 000000000000..a27b692ea795 --- /dev/null +++ b/scripts/Makefile.modbuiltin @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0 +# ========================================================================== +# Generating modules_thick.builtin +# ========================================================================== + +src := $(obj) + +PHONY := __modbuiltin +__modbuiltin: + +include include/config/auto.conf +# tristate.conf sets tristate variables to uppercase 'Y' or 'M' +# That way, we get the list of built-in modules in obj-Y +include include/config/tristate.conf + +include scripts/Kbuild.include + +ifdef building_out_of_srctree +# Create output directory if not already present +_dummy := $(shell [ -d $(obj) ] || mkdir -p $(obj)) +endif + +# The filename Kbuild has precedence over Makefile +kbuild-dir := $(if $(filter /%,$(src)),$(src),$(srctree)/$(src)) +kbuild-file := $(if $(wildcard $(kbuild-dir)/Kbuild),$(kbuild-dir)/Kbuild,$(kbuild-dir)/Makefile) +include $(kbuild-file) + +include scripts/Makefile.lib + +modthickbuiltin-subdirs := $(patsubst %,%/modules_thick.builtin, $(subdir-ym)) +modthickbuiltin-target := $(obj)/modules_thick.builtin + +__modbuiltin: $(obj)/$(builtin-file) $(subdir-ym) + @: + +$(modthickbuiltin-target): $(subdir-ym) FORCE + $(Q) rm -f $@ + $(Q) $(foreach mod-o, $(filter %.o,$(obj-Y)),\ + printf "%s:" $(addprefix $(obj)/,$(mod-o)) >> $@; \ + printf " %s" $(sort $(strip $(addprefix $(obj)/,$($(mod-o:.o=-objs)) \ + $($(mod-o:.o=-y)) $($(mod-o:.o=-Y))))) >> $@; \ + printf "\n" >> $@; ) \ + cat /dev/null $(modthickbuiltin-subdirs) >> $@; + +PHONY += FORCE + +FORCE: + +# Descending +# --------------------------------------------------------------------------- + +PHONY += $(subdir-ym) +$(subdir-ym): + $(Q)$(MAKE) $(modbuiltin)=$@ builtin-file=$(builtin-file) + +.PHONY: $(PHONY) diff --git a/scripts/modules_thick.c b/scripts/modules_thick.c new file mode 100644 index 000000000000..9a15e99c1330 --- /dev/null +++ b/scripts/modules_thick.c @@ -0,0 +1,200 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#include +#include +#include +#include + +#include "modules_thick.h" + +/* + * Read a modules_thick.builtin file and translate it into a stream of + * name / module-name pairs. + */ + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file) +{ + struct modules_thick_iter *i; + + i = calloc(1, sizeof(struct modules_thick_iter)); + if (i == NULL) + return NULL; + + i->f = fopen(modules_thick_file, "r"); + + if (i->f == NULL) { + fprintf(stderr, "Cannot open builtin module file %s: %s\n", + modules_thick_file, strerror(errno)); + return NULL; + } + + return i; +} + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name) +{ + size_t npaths = 1; + char **module_paths; + char *last_slash; + char *last_dot; + char *trailing_linefeed; + char *object_name = i->line; + char *dash; + int composite = 0; + + /* + * Read in all module entries, computing the suffixless, pathless name + * of the module and building the next arrayful of object file names for + * return. + * + * Modules can consist of multiple files: in this case, the portion + * before the colon is the path to the module (as before): the portion + * after the colon is a space-separated list of files that should be + * considered part of this module. In this case, the portion before the + * name is an "object file" that does not actually exist: it is merged + * into built-in.a without ever being written out. + * + * All module names have - translated to _, to match what is done to the + * names of the same things when built as modules. + */ + + /* + * Reinvocation of exhausted iterator. Return NULL, once. + */ +retry: + if (getline(&i->line, &i->line_size, i->f) < 0) { + if (ferror(i->f)) { + fprintf(stderr, "Error reading from modules_thick file:" + " %s\n", strerror(errno)); + exit(1); + } + rewind(i->f); + return NULL; + } + + if (i->line[0] == '\0') + goto retry; + + /* + * Slice the line in two at the colon, if any. If there is anything + * past the ': ', this is a composite module. (We allow for no colon + * for robustness, even though one should always be present.) + */ + if (strchr(i->line, ':') != NULL) { + char *name_start; + + object_name = strchr(i->line, ':'); + *object_name = '\0'; + object_name++; + name_start = object_name + strspn(object_name, " \n"); + if (*name_start != '\0') { + composite = 1; + object_name = name_start; + } + } + + /* + * Figure out the module name. + */ + last_slash = strrchr(i->line, '/'); + last_slash = (!last_slash) ? i->line : + last_slash + 1; + free(*module_name); + *module_name = strdup(last_slash); + dash = *module_name; + + while (dash != NULL) { + dash = strchr(dash, '-'); + if (dash != NULL) + *dash = '_'; + } + + last_dot = strrchr(*module_name, '.'); + if (last_dot != NULL) + *last_dot = '\0'; + + trailing_linefeed = strchr(object_name, '\n'); + if (trailing_linefeed != NULL) + *trailing_linefeed = '\0'; + + /* + * Multifile separator? Object file names explicitly stated: + * slice them up and shuffle them in. + * + * The array size may be an overestimate if any object file + * names start or end with spaces (very unlikely) but cannot be + * an underestimate. (Check for it anyway.) + */ + if (composite) { + char *one_object; + + for (npaths = 0, one_object = object_name; + one_object != NULL; + npaths++, one_object = strchr(one_object + 1, ' ')); + } + + module_paths = malloc((npaths + 1) * sizeof(char *)); + if (!module_paths) { + fprintf(stderr, "%s: out of memory on module %s\n", __func__, + *module_name); + exit(1); + } + + if (composite) { + char *one_object; + size_t i = 0; + + while ((one_object = strsep(&object_name, " ")) != NULL) { + if (i >= npaths) { + fprintf(stderr, "%s: num_objs overflow on module " + "%s: this is a bug.\n", __func__, + *module_name); + exit(1); + } + + module_paths[i++] = one_object; + } + } else + module_paths[0] = i->line; /* untransformed module name */ + + module_paths[npaths] = NULL; + + return module_paths; +} + +/* + * Free an iterator. Can be called while iteration is underway, so even + * state that is freed at the end of iteration must be freed here too. + */ +void +modules_thick_iter_free(struct modules_thick_iter *i) +{ + if (i == NULL) + return; + fclose(i->f); + free(i->line); + free(i); +} diff --git a/scripts/modules_thick.h b/scripts/modules_thick.h new file mode 100644 index 000000000000..f5edcaf9550c --- /dev/null +++ b/scripts/modules_thick.h @@ -0,0 +1,48 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* + * A simple modules_thick reader. + * + * (C) 2014, 2021 Oracle, Inc. All rights reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + */ + +#ifndef _LINUX_MODULES_THICK_H +#define _LINUX_MODULES_THICK_H + +#include +#include + +/* + * modules_thick.builtin iteration state. + */ +struct modules_thick_iter { + FILE *f; + char *line; + size_t line_size; +}; + +/* + * Construct a modules_thick.builtin iterator. + */ +struct modules_thick_iter * +modules_thick_iter_new(const char *modules_thick_file); + +/* + * Iterate, returning a new null-terminated array of object file names, and a + * new dynamically-allocated module name. (The module name passed in is freed.) + * + * The array of object file names should be freed by the caller: the strings it + * points to are owned by the iterator, and should not be freed. + */ + +char ** __attribute__((__nonnull__)) +modules_thick_iter_next(struct modules_thick_iter *i, char **module_name); + +void +modules_thick_iter_free(struct modules_thick_iter *i); + +#endif From patchwork Thu Dec 16 20:19:15 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682473 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9021DC433FE for ; Thu, 16 Dec 2021 20:20:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241333AbhLPUUI (ORCPT ); Thu, 16 Dec 2021 15:20:08 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:34186 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241313AbhLPUUE (ORCPT ); Thu, 16 Dec 2021 15:20:04 -0500 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn4X2025843; Thu, 16 Dec 2021 20:19:54 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=cydgqgIbAFFV9frwO6tRQMlczTwcZCygPS8wi7Rls1I=; b=R7vAt5bCq0ajFoiNKOxrPR9I1UP3gcHsYLtAiMWabqpwbhQIM0A4ZatKhVoH9r9CweGD 8o++YK9pFS4kRvTFG3HjkRvFNV7OZhjTRtiEfDf/WHOL9f44d67JW4bENMyAIKz76Z77 4E3onRcHEuVd81J+Yj1xbTUU+wZfcUtMbNCaQbTUje6w7KnfbVPyqzJZvG75wjbCWTB1 egNojAXmcsQGwuZOER+UBipBtS92yCS1c2pNEVTKWI8drzHmzC6jo955+lU4NSJ3bWi0 V2tYMHZ5RqWAzcAXHsMwRZQW9fob9MBnSb92uNBAstRnHd59qK+jbayzM3quEUoZdrx/ 3A== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3cyknp3y7a-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:54 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGbqo047602; Thu, 16 Dec 2021 20:19:53 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2101.outbound.protection.outlook.com [104.47.70.101]) by aserp3020.oracle.com with ESMTP id 3cxmrdxb5q-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:53 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=TDerjdmeykT8JP0H1iYtTVewHS0D6jfedC6fL66NDMnxAjUMTxhtI5AsGv5XcG7+y379aJ8k5fDYwcJIGDGX27mSdur7kYQB8PM3QIDTgx4vu1Sw2bAfG82sdF7nrOUf1kg3qFmOxyn0CwZ3hRMpyJGaz9IsEknOLtKfmnXkjW//oTweYz2cnPDUr0VCjQvOfy4B1swhWOLIwaYR5XNFRH77lOyWQJ1I3xuYBB5yaWB38qAyrjeYb1x6wxhgI2lKws+J5SxlY4FtfJXk4WfEjOYBv6cH7ZwFoh+uP1P2+HirmC0SVQ4uqpkwg8gUA7azsJoD4wa9Qnro1+Otc2+3qA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=cydgqgIbAFFV9frwO6tRQMlczTwcZCygPS8wi7Rls1I=; b=D/FXoK0PLmZ9j2xqf6Ezp2BoqEacHNCtgrmdt9/lkfSidhMvISAmuXOC+ze0kUBT7hYEaawhmG5U7ABd0/WtpunbJ9eukdD3upXAbi/tOGR0F2XkNFQ+bZkjY1YHJt4c9HEdwi2rcn522Dbaq2aPwBV7cM3VAfoxJIDwCbwIwh6KdlTSZqKRCzlWtd6rT5vOeaQVgGHrrwv6SU1tr2Jp2ChriuYlJSD+PYBTr5Wpcy7nf9nfOzG9UrK57urJrvW8/MZ3ytuB8fqlNmzVgIdVD9wfYFVb+U55sq7BVsjJ8sFd8fX6HGohaG55/3G4AfIJSL1fbknT4/dNsXX+Rqwm6w== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=cydgqgIbAFFV9frwO6tRQMlczTwcZCygPS8wi7Rls1I=; b=rNetWIjE2gvF1yn7N4MvA2OI9PpsbhzSN0oIQz/jG9wLzaGDd8ybIMu/HeYl7p1Oh2xKikI0/cvZXlii5IoB0Ue/u9hCPxgwLpArNGw+LjtwuMuWvctlh+a63B/U4c+GzrrUppdHm/3G3WKwt4tH8TeF+TYEg60s9vSxa5TK12g= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:52 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:52 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 3/7] kbuild: generate an address ranges map at vmlinux link time Date: Thu, 16 Dec 2021 20:19:15 +0000 Message-Id: <20211216201919.234994-4-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 9aadf8dc-6965-4e18-08d7-08d9c0d16a97 X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:9508; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: GUgVsZK/vBV2sERPyNMGMzmPiIF3ZYNpOqsPqdIKb4s8GQTbw1eP5+0r1qvQVmA85wIin2ZFUnqNDBlYQoXXiW77MWvswEYLuwTzXqMzQ1sWU8c4fPPofxgYLxm/TQZpC74a8ClZmWtjpeErQiyuV9FA7Dvv2Uw22Xa3Orjx6MIwQ3GW/DGKXjqnRltSPRnWmEo9S7w8ST157l7WXY+yblTvm/eL/IG24Taw11CZ5WxbohM8Ekw+dFqaumpBxoAHSVkCtmh7gB/UVImhVKrF9lluV0buR9vp/WgbAkdfmK9JU8cyH7UQgAdzHJIGGMiN24z7f3BocroVRgCkmUucxrtxc5w2n6khcjFhhNbkq9cxt5rRofOIrnxjCR9PmHBYfKEEj7A4nEdGHv8okxlfXCZP18kxisXeMKo4LP3XMbaLSR+oeDO8lmxgCBuYSQDx/bEh8G3nx0sJOvviBGXNWII0PmB3E8UYgQF5u4DHzu9MEk/L7V4NiOFniu8l6vwpnEwGEyIfhblPn4qCA+Fvgny65G7NyHclPpNBRP+QIlbS5lw2wlEKnnFJ9mu1YYIYnxJoLQ5vuQQxgwXBRigTOBzsX9Lif6JSJ9s23pbku9ByBc7d1Zhs9mwLSSl4INJ28W8U9myx9YIp0n/Fq7nX7A== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: LJbmgfFy3g+Wy9BaTshUrmmemTcdn1qRj9YQtAqrSE69S8Myjq+V4R72mpFRu+D9QdtJKW3AE+h/XDzD58cPsH6B73DlNw/WFnx+Wfna0PVP1AdWAkB6YIz0kQziMM48WvpHOcP1XXb2imgK/Rezi0o/Md6NrlIG3ZeT0nxuVaavL0TZ6ENY9pLwrqYkhIG4gi91C4csPuivsv5UqvkN0QWyhTSCe6mlIbLEIrAWX1bt5F7R5y7RoceXY4Uv6LEtBm8gOQ86r7BKQbDm4bZsGSr/ZR3/lEPw2vBWgv2i56B77efs87hOzbvAIVVCIN+FsYuaQZmI/ONxcImp55ki9sCaaxGCuXtGSWp2lqr+nVRuBpcEm75eog5m78hOlKiZ9XmLiv3yS+MF7irKK29CKYg0A0Wz1S4y6QqNi6Uv+efWHsBoDqlA+j7f5SyZZKWmkkxAYr3SzqBt9mEUKv4Pv9vjVnfcSDnVEOFjVnx8tv9oZP6fA064wq/DVQ/HVu3ZZWKQ9lXMTuMU+lIr9h3/YPiCVJB1JfzIi/dEXB/o6WP/O7i3F4a6YCjh/38T9QPP0S5aboLey/WN3cUgUCccyrKDkiTJeErOnqE5YIM3HUYVYownhodSARW60dSAiLVjNcqxwOR2sA/OPhRlFNfL50ZQIy1XUem3xSI7BB7UyIoLW31d1SYQNtRlbMIb6Nm2sQqxeAGVZWo+GSnwZnlvrYINTocS+pofqgnyY5kJuo0KvN0ju3dOD0q38roj+yvpmnxku/k8kmVnjCXUnNZwNY2bM8rbgxvT+b3d72ipWBT5w9jXlpYCwnGoviVQUzi/CC3PX0EjpHTCl05+NZzUN+TRk5qmwfdCL4NjojgiZl1xHea/kqn+Y/5RSDPV3Sp/NhFNuloG/9YNfjH9HTxr65vPDq3zcB/oaewJTfiziaKUUj4j+cwfHQxrAnZEwzWA90cZCkuqydaPagfaH4XfsAkuS1tD6WpFd54Z1RAnZU3rYmpqcjcb8+wMxKQ8wEEcgvQjyl34X1jOo0eKzyHj27QVivll0nip1bu88zswtr4qAeDI5Ek58xvh+0waNn9KRXzDNywwuRhe3mBwCQXNdKG4RrSkxEtDtuPH4aCRbAgsG/VoTitEMYuh64Q6beuFGXlCnswOKSRTxgKfSVobSzOND/KMpQ056/9paNB3Oa+Q5Yupc1npR0bq+ydtK0LNinG3Yph/mSu9Ty2RlOaUS6hlrMvAj/nKbw0z1y4MMO2ailblxY14nZmARJplBUvY01m8sQTZqnt+6npWOqB20yEsxFEqQNaJ0/aEQctcV4qu87pDYTcL2ZPaJDIlVgLgtRggAao/0eevj/7LRPR6//sSs8Nn5EGNXOdIkzmTRKLscorstVfAPyajb7gA5wsjfj7AJRWXgYi9k2qv6UmMZe4G884Dt6tQow6Yu0DJbscsJVQaEpF+qVvLiWicGaWhDXsW5+exI4FQj5tYzuBZHFR/R3v4wrLxpWQfJq5MXeBA7gV8VecokjxMR0+P0yhowSR/XJZFbaUDuOPisaZiwMjow+bN50PCrdefELxrLtJTzowOb90wrRDshR4nB5Bcl1KqCbJPw/n5GV0u6S9+JICSbzxHE8qOWcN0oUioh+latos75Uei6M9nVoo2Yc5d X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 9aadf8dc-6965-4e18-08d7-08d9c0d16a97 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:51.9710 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: piu5SQb01mKjY0WATqx3Mk7X71d6tmVqfvJdXFPHT1bnwLAoMqFJqYWGFpsE0XOpnzWMac3JcypDPO0KKZ4jdQ== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: dA9hO4T1qMXn3hRMLQzWwAc8t9ujXhFn X-Proofpoint-GUID: dA9hO4T1qMXn3hRMLQzWwAc8t9ujXhFn Precedence: bulk List-ID: This emits a new file, .tmp_vmlinux.ranges, which maps address range/size pairs in vmlinux to the object files which make them up, e.g., in part: 0x0000000000000000 0x30 arch/x86/kernel/cpu/common.o 0x0000000000001000 0x1000 arch/x86/events/intel/ds.o 0x0000000000002000 0x4000 arch/x86/kernel/irq_64.o 0x0000000000006000 0x5000 arch/x86/kernel/process.o 0x000000000000b000 0x1000 arch/x86/kernel/cpu/common.o 0x000000000000c000 0x5000 arch/x86/mm/cpu_entry_area.o 0x0000000000011000 0x10 arch/x86/kernel/espfix_64.o 0x0000000000011010 0x2 arch/x86/kernel/cpu/common.o [...] In my simple tests this seems to work with clang too, but if I'm not sure how stable the format of clang's linker mapfiles is: if it turns out not to work in some versions, the mapfile-massaging awk script added here might need some adjustment. Signed-off-by: Nick Alcock --- Notes: v6: use ${wl} where appropriate to avoid failure on UML scripts/link-vmlinux.sh | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 5cdd9bc5c385..5301f3e77116 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -196,7 +196,7 @@ vmlinux_link() ${ld} ${ldflags} -o ${output} \ ${wl}--whole-archive ${objs} ${wl}--no-whole-archive \ ${wl}--start-group ${libs} ${wl}--end-group \ - $@ ${ldlibs} + ${wl}-Map=.tmp_vmlinux.map $@ ${ldlibs} } # generate .BTF typeinfo from DWARF debuginfo @@ -239,6 +239,19 @@ kallsyms() { local kallsymopt; + # read the linker map to identify ranges of addresses: + # - for each *.o file, report address, size, pathname + # - most such lines will have four fields + # - but sometimes there is a line break after the first field + # - start reading at "Linker script and memory map" + # - stop reading at ".brk" + ${AWK} ' + /\.o$/ && start==1 { print $(NF-2), $(NF-1), $NF } + /^Linker script and memory map/ { start = 1 } + /^\.brk/ { exit(0) } + ' .tmp_vmlinux.map | sort > .tmp_vmlinux.ranges + + # get kallsyms options if [ -n "${CONFIG_KALLSYMS_ALL}" ]; then kallsymopt="${kallsymopt} --all-symbols" fi From patchwork Thu Dec 16 20:19:16 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682467 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 345FFC433F5 for ; Thu, 16 Dec 2021 20:20:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241300AbhLPUUE (ORCPT ); Thu, 16 Dec 2021 15:20:04 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:10158 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241292AbhLPUUC (ORCPT ); Thu, 16 Dec 2021 15:20:02 -0500 Received: from pps.filterd (m0246631.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn9Va016304; Thu, 16 Dec 2021 20:19:56 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=dR1G9fVMyzEOkA0HmWbq33C33988tKKKcOR/SzF72ws=; b=sQ/V2bfZH62nv5FaekYQwoJPpXU+9otQpuLtynhyU51b0SD3rUDmhEmHMS9aIGgzeFS1 Sf/ZjkAewG4AMT9fzeJHROHp0uHQAdbZvH3nAezlHBuZHtDWQAQF6j1JJ8XHEOtmOLbp RmT9TL1UvyS5R7xR8BE9cIise8F+R+C0zaeTWugh8I0QJH0I17uSpLdJFHw61RhzTx1z R9//u4z4sER2Dl48dZwvdsz7S2MH1lb5hqYMi1A39J+djw12eQlHkNUg6izLkqY8ANGB PYyFhOAbvXZvt4uhTUTFWKXoE/AWbO02dulIdbBqcbR5GQgAV36TfGk9E8U8WfIoZWdc IQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3cykmbky63-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:56 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGCtG123797; Thu, 16 Dec 2021 20:19:55 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2107.outbound.protection.outlook.com [104.47.70.107]) by aserp3030.oracle.com with ESMTP id 3cyjuae0cc-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:55 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=GxgBKHT4JF2WIOJlR5S+JREE+qvcPW3bO64ofot3Ol3EAtw6Irh07fYF5QM9WE4FWXa9tPiYzRTHK6amfqrhsOZ9g7sh/HD2y0aLTP3whYPZkBgww+fPKXJ8sHpKqwOTzan2Lt1HLvQ/oathUt/Wha2wfXzAvES2/KSphv0KfBqxbZKjfCJqR6P3Q7GP7CXLMYkrAteQjg3BzBwFumOfiwQMpFB8ucCUesCP9Dh2MoKr6VAwZmwHlNDUyks63RJLS8hXEnRqagGgCwz6T2WaIFJJlEiCeX4H5pQWwbTdRxZcSIfMIFyqnLSvIcu2irMTGqf4921po4oiRmwzSDzGpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=dR1G9fVMyzEOkA0HmWbq33C33988tKKKcOR/SzF72ws=; b=U4PSCTHA8hy+1BA1jm6hDXW2Q9Cm0VQTHpzw+WpY+ymhzffSO8cO9/HgrVqmzAk2kEJ2EIOx3lUDykH41HZv23uaNAeMc/4/sPOnu2HfRb6SXSCLALjOQLRZIghaSDAB9ojlMCcgVg7dydZu3OvpSUmmsEFohGewyw2orPtTeg00QtPmLbhAKLYmSebR9beFsGgl4HLGZR0ORqOFn9xF61M8Ywz0FHx23LFCjCj8SLdL4AW6xL0jIwM0JP/78V5U3R9VCpuhN56m4FYobO1/8hFAIFNO2PwvsQqoP0xJtvRwYN+w+tAwNztPM6tcHwr8v1WNipPD8yPuukXB8pa+kg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=dR1G9fVMyzEOkA0HmWbq33C33988tKKKcOR/SzF72ws=; b=jdqN9BDPMIMF0x7NziZfzfhCrlXuI3RIjHvE6RZCJAPjXZnU6dQoriNZ/WiyU2iBq+h1UE4yIYOo0ST+8+yYBd3oYCA5Bm9arH48KmtbNX4YIyeG3q9QeZY95PFTNqiTkK88V2K0dpNiquXVeWYae/4+/9IIG2OkADFP34NBJy8= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:53 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:53 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 4/7] kallsyms: introduce sections needed to map symbols to built-in modules Date: Thu, 16 Dec 2021 20:19:16 +0000 Message-Id: <20211216201919.234994-5-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: e6d14516-04b5-4291-374f-08d9c0d16b83 X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4714; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: uWM5v8zvoLAPwgwJtvzZ32/QVHaqlTyXMRoRx3kRh3ekoIMuFNuYQcXazF6etIniIPtpzm8kaWas6ep6EqneS4butp76reSIjzjzxScXtWNGv/1q9UlYMiKU79XoY/NqWEAArVGvZO0pyu80gAQkrUTNR2RXTsz+H3/Gvoc9Wf4ZeqmjRvLIfiY2LOxgiYKB23I/PC5xVaSik4M/9G61UKPF0Jh7CUpxADVw6596cq0SVZmGT7WiO9hzgMvcXukKJ4+hYeOKzEDAdthaRiqdbAkXvxCSOtRQ7f460A1g1l4rUaDxBn/kCx5QKx6h/mX3gw6Zkyvvk5iR8ra2g67N2eVQXSsus6BpVhdOPT4R2qpiIGIZaRlJrhsLuquPBKUSU2Ltvb6/4DmTNV3w2PykfIMRfwkHAFTIZL+X7dZW2ugjZZYcbvkJw+ZvN0tPXev5xR5Ma2LzOx/WC4JHuGDlG4QbzMcdciS3mxXicGuX3llhiIfOZBnMU+i4ibb5gWm3I2Jzm3DLXOsKjuHAi55I0lgsXN7gNP1UQiZ4MWHyKyZu91GCBKFNQkWXe1yemM8t4vxeBoQmzBF2M+cNkcNg/gy6/yrCamz2Td8wGR58YCpSe7mZqP6qtdBljU/16bt1zycrD7qgYctA4Lna0NLgmg== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(30864003)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: 16LdWjqXOXAROACXLRD93amdWzW/GMcB3TcpVKUAC6ShxG0lF/wdpk+8q7BIaerRhZuwBvEe7QPdJOeYv2u5JXYRBUT5rYLwQ2Aivh/PFoEgpAL4bsOZLTk+TtDANvU+mu1Qnun2F0KH677NLTvWunGLcWWD0KfHdpC/45BZMX3iJuh79gyg3+M4nipfGXxqqLeLARHGd2hC6rQi0gJ85fPZMMNQ5kvUlI4hIrGbhr2ttywwtUjAX24b+tX8baRJidEFxFaS6LiFnLQ8kNCX5WatwtGpZzmqoyUEv15VBaeeAxY7FMj1O4CA7U8Ooz4klFPMhJccCCwgPHQCmfgnihHMQqBDQB+lDnLzoNuyDgD4tR/iZ+SBqr8renQkAo8wG5ygW2AfQ2k30iAZrb/kE56NM+92yxU/Y3gxdzZLunSoMoNCAP4aJKbw+IuEyPEOIKncb3fQaom2yqctgnfV9S6yqidpvQ3e3poA90QkVqGY42YZ2xe38qisaqDM/CoUefwizS0nUhBDP3/OWb7VMlCpqMplpekJXeFAmQRjeWx6MUtGQC5EkztDUhkEXdlibSCg7p67iWaQyu3zgMupUK6upjnmBgVAqyTKFaHWMsE/taZEfUSxJUh0HHyD2xUhRppBZOGE8K7mKs/AD/mAdnuFTdV2rWQ437/hcM12U4v8IrzGYyiF8fxPlQ4JMBp3SE9uGWoMQWUGaeOJzMKn64TYINiZikJchQ/dzcS+30XekFyeXITg3aCb5VQOsD0PIQNjydWSQ4GpicUwRBv62zU5MDOsHjipW5RRY8oOKdsxo7FysZA0VAjaKk+0PO+cX7rfEfEKa+KqhOGj++nTFvv0MXrdK0m8GH5YOwQeIums0obyI/2roy7K2apvofpC+PKp9Vo39o/TEEGo1xWKhdzdZ8HtE6ZxgCq27Jb60f7fNy2swzT6vdkUlxWc8RyDzPjewjEUoG2a1jz6+H6sK3sPnSkIAuspRPlhZiwelqtQkwyJEi4hVbxjuKWc3GWiKvuxkNFKT7BjeSe6C7y9Gw+x3Xr+IlADKlWveEuGmuJ6OiEi5wFKmfPq2/8cpA8M7grpSHHwU9rmyDbA8R0qzTJF9X8P9yxB3cwroG4cyU8xLLP8AjD62bIHw/DtcbuP05se8nZowdAGihF3U0WTkMFKFi317AtHMWdzBXKVDiGObyHgo5XmhYuDsFNIOwmGowQ6G1+DOFiP9Xb8PPxTTHqoHNWmTXvOz0lRiqQ4bVHGIFpAjO56zmlTaIHA78Z/qEgx9hMthF3BchVo8buI6eGblgAZM6UMej+xgwYgpB41kbbJ3e7wJn5TSybwhWumJPZbmwu5jHrFElfImJju2WoCReIqr9M+9RR13uPuTnnGAjdorx5h/+JShEBfL5woiK3QfVc3TvIJAiUKa91vxBrmJpuAAGuA521JvFw+jrIV99ox7c+E7vOEIQ2/hDysk57AL+TA322h0+PJYHXa3RvNTA+fXi39odAoIkgxIgRzOlknn8lK0QIgQlDgv7gGFERByLDXFXtt97cU0qutAxuVOtI54COmw6uWbNf7YA18dOKD4uS5GI0KdBh7Has96lQpPcKJ5DkVZRi7mlGNRzWhcpWJdS4ns1h2qiMpxrHYbBVd3Ts+o8i4k6utJ1sN X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: e6d14516-04b5-4291-374f-08d9c0d16b83 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:53.5344 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: wkHbs1zi0VojEQ4Gz7es9fOvD7tkzIl9HxcFrRoAr177ebEMw8PPEC7nND+DuBgyFCFUsvVI5J94+wbv0SpqKg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 adultscore=0 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: 5w6S-oRD5C4nTq5EM6E1PCENf5BjOhTi X-Proofpoint-GUID: 5w6S-oRD5C4nTq5EM6E1PCENf5BjOhTi Precedence: bulk List-ID: The mapping consists of three new symbols, computed by integrating the information in the (just-added) .tmp_vmlinux.ranges and modules_thick.builtin: taken together, they map address ranges (corresponding to object files on the input) to the names of zero or more modules containing those address ranges. - kallsyms_module_addresses/kallsyms_module_offsets encodes the address/offset of each object file (derived from the linker map), in exactly the same way as kallsyms_addresses/kallsyms_offsets does for symbols. There is no size: instead, the object files are assumed to tile the address space. (This is slightly more space-efficient than using a size). Non-text-section addresses are skipped: for now, all the users of this interface only need module/non-module information for instruction pointer addresses, not absolute-addressed symbols and the like. This restriction can easily be lifted in future. (Regarding the name: right now the entries correspond pretty closely to object files, so we could call the section kallsyms_objfiles or something, but the optimizer added in the next commit will change this.) - kallsyms_module_names encodes the name of each module in a modified form of strtab: notably, if an object file appears in *multiple* modules, all of which are built in, this is encoded via a zero byte, a one-byte module count, then a series of that many null-terminated strings. As a special case, the table starts with a single zero byte which does *not* represent the start of a multi-module list. - kallsyms_modules connects the two, encoding a table associated 1:1 with kallsyms_module_addresses / kallsyms_module_offsets, pointing at an offset in kallsyms_module_names describing which module (or modules, for a multi-module list) the code occupying this address range is part of. If an address range is part of no module (always built-in) it points at 0 (the null byte at the start of the kallsyms_module_names list). There is no optimization yet: kallsyms_modules and kallsyms_module_names will almost certainly contain many duplicate entries, and kallsyms_module_{addresses,offsets} may contain consecutive entries that point to the same place. The size hit is fairly substantial as a result, though still much less than a naive implementation mapping each symbol to a module name would be: 50KiB or so. Signed-off-by: Nick Alcock --- Makefile | 2 +- init/Kconfig | 8 + scripts/Makefile | 6 + scripts/kallsyms.c | 366 +++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 371 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index f94b5f05e9f5..321955ab96b7 100644 --- a/Makefile +++ b/Makefile @@ -1157,7 +1157,7 @@ cmd_link-vmlinux = \ $(CONFIG_SHELL) $< "$(LD)" "$(KBUILD_LDFLAGS)" "$(LDFLAGS_vmlinux)"; \ $(if $(ARCH_POSTLINK), $(MAKE) -f $(ARCH_POSTLINK) $@, true) -vmlinux: scripts/link-vmlinux.sh autoksyms_recursive $(vmlinux-deps) FORCE +vmlinux: scripts/link-vmlinux.sh autoksyms_recursive $(vmlinux-deps) modules_thick.builtin FORCE +$(call if_changed_dep,link-vmlinux) targets := vmlinux diff --git a/init/Kconfig b/init/Kconfig index 4b7bac10c72d..2df1bd34a63e 100644 --- a/init/Kconfig +++ b/init/Kconfig @@ -1531,6 +1531,14 @@ config POSIX_TIMERS If unsure say y. +config KALLMODSYMS + default y + bool "Enable support for /proc/kallmodsyms" if EXPERT + depends on KALLSYMS + help + This option enables the /proc/kallmodsyms file, which maps symbols + to addresses and their associated modules. + config PRINTK default y bool "Enable support for printk" if EXPERT diff --git a/scripts/Makefile b/scripts/Makefile index 9adb6d247818..d677995d3f31 100644 --- a/scripts/Makefile +++ b/scripts/Makefile @@ -32,6 +32,12 @@ HOSTCFLAGS_sorttable.o += -DUNWINDER_ORC_ENABLED HOSTLDLIBS_sorttable = -lpthread endif +kallsyms-objs := kallsyms.o + +ifdef CONFIG_KALLMODSYMS +kallsyms-objs += modules_thick.o +endif + # The following programs are only built on demand hostprogs += unifdef diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 54ad86d13784..8f87b724d0fa 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,10 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: nm -n vmlinux | scripts/kallsyms [--all-symbols] > symbols.S + * Usage: nm -n vmlinux + * | scripts/kallsyms [--all-symbols] [--absolute-percpu] + * [--base-relative] [--builtin=modules_thick.builtin] + * > symbols.S * * Table compression uses all the unused char codes on the symbols and * maps these to the most used substrings (tokens). For instance, it might @@ -24,6 +27,10 @@ #include #include #include +#include +#include "modules_thick.h" + +#include "../include/generated/autoconf.h" #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) @@ -67,11 +74,116 @@ static int token_profit[0x10000]; static unsigned char best_table[256][2]; static unsigned char best_table_len[256]; +#ifdef CONFIG_KALLMODSYMS +static unsigned int strhash(const char *s) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + + for (; *s; s++) + hash = (hash ^ *s) * 0x01000193; + return hash; +} + +#define OBJ2MOD_BITS 10 +#define OBJ2MOD_N (1 << OBJ2MOD_BITS) +#define OBJ2MOD_MASK (OBJ2MOD_N - 1) +struct obj2mod_elem { + char *obj; + char *mods; /* sorted module name strtab */ + size_t nmods; /* number of modules in "mods" */ + size_t mods_size; /* size of all mods together */ + int mod_offset; /* offset in .kallsyms_module_names */ + struct obj2mod_elem *obj2mod_next; +}; + +/* + * Map from object files to obj2mod entries (a unique mapping). + */ + +static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static size_t num_objfiles; + +/* + * An ordered list of address ranges and the objfile that occupies that range. + */ +struct addrmap_entry { + unsigned long long addr; + struct obj2mod_elem *objfile; +}; +static struct addrmap_entry *addrmap; +static int addrmap_num, addrmap_alloced; + +static void obj2mod_init(void) +{ + memset(obj2mod, 0, sizeof(obj2mod)); +} + +static struct obj2mod_elem *obj2mod_get(const char *obj) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (strcmp(elem->obj, obj) == 0) + return elem; + } + return NULL; +} + +/* + * Note that a given object file is found in some module, interning it in the + * obj2mod hash. Should not be called more than once for any given (module, + * object) pair. + */ +static void obj2mod_add(char *obj, char *mod) +{ + int i = strhash(obj) & OBJ2MOD_MASK; + struct obj2mod_elem *elem; + + elem = obj2mod_get(obj); + if (!elem) { + elem = malloc(sizeof(struct obj2mod_elem)); + if (!elem) + goto oom; + memset(elem, 0, sizeof(struct obj2mod_elem)); + elem->obj = strdup(obj); + if (!elem->obj) + goto oom; + elem->mods = strdup(mod); + if (!elem->mods) + goto oom; + + elem->obj2mod_next = obj2mod[i]; + obj2mod[i] = elem; + num_objfiles++; + } else { + elem->mods = realloc(elem->mods, elem->mods_size + + strlen(mod) + 1); + if (!elem->mods) + goto oom; + strcpy(elem->mods + elem->mods_size, mod); + } + + elem->mods_size += strlen(mod) + 1; + elem->nmods++; + if (elem->nmods > 255) { + fprintf(stderr, "kallsyms: %s: too many modules associated with this object file\n", + obj); + exit(EXIT_FAILURE); + } + return; +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} +#endif /* CONFIG_KALLMODSYMS */ static void usage(void) { - fprintf(stderr, "Usage: kallsyms [--all-symbols] " - "[--base-relative] < in.map > out.S\n"); + fprintf(stderr, "Usage: kallsyms [--all-symbols] [--absolute-percpu] " + "[--base-relative] [--builtin=modules_thick.builtin] " + "< nm_vmlinux.out > symbols.S\n"); exit(1); } @@ -95,10 +207,16 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_offsets", "kallsyms_relative_base", "kallsyms_num_syms", + "kallsyms_num_modules", "kallsyms_names", "kallsyms_markers", "kallsyms_token_table", "kallsyms_token_index", + "kallsyms_module_offsets", + "kallsyms_module_addresses", + "kallsyms_modules", + "kallsyms_module_names", + "kallsyms_module_names_len", /* Exclude linker generated symbols which vary between passes */ "_SDA_BASE_", /* ppc */ "_SDA2_BASE_", /* ppc */ @@ -246,8 +364,8 @@ static struct sym_entry *read_symbol(FILE *in) return sym; } -static int symbol_in_range(const struct sym_entry *s, - const struct addr_range *ranges, int entries) +static int addr_in_range(unsigned long long addr, + const struct addr_range *ranges, int entries) { size_t i; const struct addr_range *ar; @@ -255,7 +373,7 @@ static int symbol_in_range(const struct sym_entry *s, for (i = 0; i < entries; ++i) { ar = &ranges[i]; - if (s->addr >= ar->start && s->addr <= ar->end) + if (addr >= ar->start && addr <= ar->end) return 1; } @@ -269,8 +387,8 @@ static int symbol_valid(const struct sym_entry *s) /* if --all-symbols is not specified, then symbols outside the text * and inittext sections are discarded */ if (!all_symbols) { - if (symbol_in_range(s, text_ranges, - ARRAY_SIZE(text_ranges)) == 0) + if (addr_in_range(s->addr, text_ranges, + ARRAY_SIZE(text_ranges)) == 0) return 0; /* Corner case. Discard any symbols with the same value as * _etext _einittext; they can move between pass 1 and 2 when @@ -352,6 +470,121 @@ static void output_address(unsigned long long addr) printf("\tPTR\t_text - %#llx\n", _text - addr); } +#ifdef CONFIG_KALLMODSYMS +/* Output the .kallmodsyms_modules symbol content. */ +static void output_kallmodsyms_modules(void) +{ + struct obj2mod_elem *elem; + size_t offset = 1; + size_t i; + + /* + * Traverse and emit, updating mod_offset accordingly. + * Emit a single \0 at the start, to encode non-modular objfiles. + */ + output_label("kallsyms_module_names"); + printf("\t.byte\t0\n"); + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; + elem = elem->obj2mod_next) { + const char *onemod; + size_t i; + + elem->mod_offset = offset; + onemod = elem->mods; + + /* + * Technically this is a waste of space: we could just + * as well implement multimodule entries by pointing one + * byte further back, to the trailing \0 of the previous + * entry, but doing it this way makes it more obvious + * when an entry is a multimodule entry. + */ + if (elem->nmods != 1) { + printf("\t.byte\t0\n"); + printf("\t.byte\t%zi\n", elem->nmods); + offset += 2; + } + + for (i = elem->nmods; i > 0; i--) { + printf("\t.asciz\t\"%s\"\n", onemod); + offset += strlen(onemod) + 1; + onemod += strlen(onemod) + 1; + } + } + } + printf("\n"); + output_label("kallsyms_module_names_len"); + printf("\t.long\t%zi\n", offset); +} + +static void output_kallmodsyms_objfiles(void) +{ + size_t i = 0; + size_t emitted_offsets = 0; + size_t emitted_objfiles = 0; + + if (base_relative) + output_label("kallsyms_module_offsets"); + else + output_label("kallsyms_module_addresses"); + + for (i = 0; i < addrmap_num; i++) { + long long offset; + int overflow; + + if (base_relative) { + if (!absolute_percpu) { + offset = addrmap[i].addr - relative_base; + overflow = (offset < 0 || offset > UINT_MAX); + } else { + offset = relative_base - addrmap[i].addr - 1; + overflow = (offset < INT_MIN || offset >= 0); + } + if (overflow) { + fprintf(stderr, "kallsyms failure: " + "objfile %s at address %#llx out of range in relative mode\n", + addrmap[i].objfile ? addrmap[i].objfile->obj : + "in always-built-in object", table[i]->addr); + exit(EXIT_FAILURE); + } + printf("\t.long\t0x%x\n", (int)offset); + } else + printf("\tPTR\t%#llx\n", addrmap[i].addr); + emitted_offsets++; + } + + output_label("kallsyms_modules"); + + for (i = 0; i < addrmap_num; i++) { + struct obj2mod_elem *elem = addrmap[i].objfile; + /* + * Address range cites no object file: point at 0, the built-in + * module. + */ + if (addrmap[i].objfile == NULL) { + printf("\t.long\t0x0\n"); + emitted_objfiles++; + continue; + } + + /* + * Zero offset is the initial \0, there to catch uninitialized + * obj2mod entries, and is forbidden. + */ + assert (elem->mod_offset != 0); + + printf("\t.long\t0x%x\n", elem->mod_offset); + emitted_objfiles++; + } + + assert (emitted_offsets == emitted_objfiles); + output_label("kallsyms_num_modules"); + printf("\t.long\t%zi\n", emitted_objfiles); + printf("\n"); +} +#endif /* CONFIG_KALLMODSYMS */ + /* uncompress a compressed symbol. When this function is called, the best table * might still be compressed itself, so the function needs to be recursive */ static int expand_symbol(const unsigned char *data, int len, char *result) @@ -451,6 +684,11 @@ static void write_src(void) printf("\n"); } +#ifdef CONFIG_KALLMODSYMS + output_kallmodsyms_modules(); + output_kallmodsyms_objfiles(); +#endif + output_label("kallsyms_num_syms"); printf("\t.long\t%u\n", table_cnt); printf("\n"); @@ -735,7 +973,7 @@ static void make_percpus_absolute(void) unsigned int i; for (i = 0; i < table_cnt; i++) - if (symbol_in_range(table[i], &percpu_range, 1)) { + if (addr_in_range(table[i]->addr, &percpu_range, 1)) { /* * Keep the 'A' override for percpu symbols to * ensure consistent behavior compared to older @@ -762,17 +1000,124 @@ static void record_relative_base(void) } } +#ifdef CONFIG_KALLMODSYMS +/* + * Read the linker map. + */ +static void read_linker_map(void) +{ + unsigned long long addr, size; + char obj[PATH_MAX+1]; + FILE *f = fopen(".tmp_vmlinux.ranges", "r"); + + if (!f) { + fprintf(stderr, "Cannot open '.tmp_vmlinux.ranges'.\n"); + exit(1); + } + + addrmap_num = 0; + addrmap_alloced = 4096; + addrmap = malloc(sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + + /* + * For each address range, add to addrmap the address and the objfile + * entry to which the range maps. Only add entries relating to text + * ranges. (We assume that the text ranges are tightly packed, because + * in any reasonable object file format they will be, so we can ignore + * the size.) + * + * Ranges that do not correspond to a built-in module, but to an + * always-built-in object file, have no obj2mod_elem and point at NULL + * instead. + */ + + while (fscanf(f, "%llx %llx %s\n", &addr, &size, obj) == 3) { + struct obj2mod_elem *elem = obj2mod_get(obj); + + if (addr == 0 || size == 0 || + !addr_in_range(addr, text_ranges, ARRAY_SIZE(text_ranges))) + continue; + + if (addrmap_num >= addrmap_alloced) { + addrmap_alloced *= 2; + addrmap = realloc(addrmap, + sizeof(*addrmap) * addrmap_alloced); + if (!addrmap) + goto oom; + } + + addrmap[addrmap_num].addr = addr; + addrmap[addrmap_num].objfile = elem; + addrmap_num++; + } + fclose(f); + return; + +oom: + fprintf(stderr, "kallsyms: out of memory\n"); + exit(1); +} + +/* + * Read "modules_thick.builtin" (the list of built-in modules). Construct the + * obj2mod hash to track objfile -> module mappings. Read ".tmp_vmlinux.ranges" + * (the linker map) and build addrmap[], which maps address ranges to built-in + * module names (using obj2mod). + */ +static void read_modules(const char *modules_builtin) +{ + struct modules_thick_iter *i; + char *module_name = NULL; + char **module_paths; + + obj2mod_init(); + /* + * Iterate over all modules in modules_thick.builtin and add each. + */ + i = modules_thick_iter_new(modules_builtin); + if (i == NULL) { + fprintf(stderr, "Cannot iterate over builtin modules.\n"); + exit(1); + } + + while ((module_paths = modules_thick_iter_next(i, &module_name))) { + char **walk = module_paths; + while (*walk) { + obj2mod_add(*walk, module_name); + walk++; + } + free(module_paths); + } + + free(module_name); + modules_thick_iter_free(i); + + /* + * Read linker map. + */ + read_linker_map(); +} +#else +static void read_modules(const char *unused) {} +#endif /* CONFIG_KALLMODSYMS */ + int main(int argc, char **argv) { + const char *modules_builtin = "modules_thick.builtin"; + if (argc >= 2) { int i; for (i = 1; i < argc; i++) { - if(strcmp(argv[i], "--all-symbols") == 0) + if (strcmp(argv[i], "--all-symbols") == 0) all_symbols = 1; else if (strcmp(argv[i], "--absolute-percpu") == 0) absolute_percpu = 1; else if (strcmp(argv[i], "--base-relative") == 0) base_relative = 1; + else if (strncmp(argv[i], "--builtin=", 10) == 0) + modules_builtin = &argv[i][10]; else usage(); } @@ -780,6 +1125,7 @@ int main(int argc, char **argv) usage(); read_map(stdin); + read_modules(modules_builtin); shrink_table(); if (absolute_percpu) make_percpus_absolute(); From patchwork Thu Dec 16 20:19:17 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682469 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 281A0C433EF for ; Thu, 16 Dec 2021 20:20:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241316AbhLPUUF (ORCPT ); Thu, 16 Dec 2021 15:20:05 -0500 Received: from mx0b-00069f02.pphosted.com ([205.220.177.32]:12940 "EHLO mx0b-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241303AbhLPUUD (ORCPT ); Thu, 16 Dec 2021 15:20:03 -0500 Received: from pps.filterd (m0246632.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn5m2025358; Thu, 16 Dec 2021 20:19:58 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=gApXByGo7Nnq5Bknpj7YS6Xp4dORLUVG4M1mJfLQd3k=; b=VM2gldADzwu7T4e9Ph+8BZ+9Nmt38GwTPBm71GDFWKgjp+HFJwkXRZHMP/V6HAeJQ5D2 3pHcWuzsG/X40lftjvIDaJ+rnsNinavLi3BNTK00lZDblgWiYYkd/a6NJXvDf7KIqvd2 b9ju+rZ8v1yRVQBN5yJL4M9Tv9GiLZ4w1ttOPNC8NYNO+IOFWPofFDFAc8wtIYcH5o3u AJ/ocfrvgMrAnod17mI2d6CeOv8TIZjzn1/uWGNING6XAuVllSHIP4eKzhKKuz1h38nB srJYP6d7HU5mrNFNX2aqSeK0/xLBnPNuWTIM/2v4RlqJqoZwS6pKf2UhKKSFn+1WAHXH BQ== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3cyknc3xn2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:58 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGCTa123775; Thu, 16 Dec 2021 20:19:57 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2106.outbound.protection.outlook.com [104.47.70.106]) by aserp3030.oracle.com with ESMTP id 3cyjuae0cy-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:57 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=UxrWOyW5Rw0uP0HtL/2yxUUPcQrxehRRujQ9ZprW2M7M7oLU9YFR+ZGViCCCtnSzg3qc7CCBmL4ZvLl+gsGgOYhLqGhe32zK1PXZO0Azak2Z2qVG9w18JUMldQND941I5s4ydYA+QRwLggUgws6mcqmIAmUZQRdkx9nBVqnTGoBh4BevdXLceOpdWWADIPA4JpYYrd3AxedE8C8A39CgGuHccXEeNBQK8XiK4eP18wKbvXh027J8k+jyFYhUhNbt6ji67wfUQmtFdY1Rfn3TIg7ZQkUBFNOwivRinYKx63PUj0l3rpFg8h2EgIm9Bxiw/us/ETZg6mzG9R1VEfbC0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=gApXByGo7Nnq5Bknpj7YS6Xp4dORLUVG4M1mJfLQd3k=; b=jbU5LyF4FgwiCxQ422iuRTKLiOJYpu2EvCyVFpIOPKEIbUYMgu+RwPrm4GeRYHHWF12oewq7WLo+9nnyydQWssuYBTZuF5T+0TpfD8Q1kXfMruELoz/uZuEuVPqd7DNxF8ozwaZlYMxMBjSWsPsuASYVf7bLRwyjpEnOvR7/igNOgjBA+oPIFaWY2ujBiWmMmIglRF2w9ptkQd4jCeZ+aPP7WnZJxNikalkLH68AUYHUPNHNRRYpFHdaLcuhuVLVOQ2MBuR1Kw/Mhafngu4jj2jaVxMMHFAVMstJrhqXJssYDf2v+lBbOZe9GU0Bx3Ez9pSV7zqszeS/YwPvd5v7xw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=gApXByGo7Nnq5Bknpj7YS6Xp4dORLUVG4M1mJfLQd3k=; b=iD8K/+2otGUD3+9m14enCzux5w4dmdIB6hrF8KsyQzh94p0yomlzKolQfoYnoek6JN+rZkTphEPsV/7cSzJYcRK9svcVkYmJX26MNlzRxVGymDl+0ZnF8flEBxcoJ5S/zb/Ewn95xyklJtBeOlOERH3NlFVvxY0x8uquu3HSAVo= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:55 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:55 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 5/7] kallsyms: optimize .kallsyms_modules* Date: Thu, 16 Dec 2021 20:19:17 +0000 Message-Id: <20211216201919.234994-6-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: c88bfce1-e04a-49d4-337f-08d9c0d16c6f X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:6430; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: RclG2suHsharImX3CCL5ujXf+ANQfpLqyWn7IJSGHxi9UGhbywlKO2aIS9o2elpk7EODKzwZqt0wwhUSGPBnGgcWWZymbhqcTLsl7p73+XhATOnaU+IDdFFfmT7JV1avQBOiVCCQhF/VxwbcS0usqvQLhleiR9fK13/iTVU+YycyY/M85+jguR4OoIb+JmU+u7Ef7vr3DuiPeT4rXnt2jUVH5FaGih1j2eFxk3G4YPy9OGGYWRWEFFL7C+w8tm5QvkoRkzJGHgQZafQ/gvJi7kfwRpQKzrwZpPg511e74VGoEFht7l67xggOjAsYwW95cg3IOXUMNzln9BuwilH38b9bGnfBpszbGrU9EBWwZ7mcc0fzqQBnIaDN6jjq7kLEckxk39z9BYsAc4S1dQpVAAuhDfm+pPNj88ni/gPXvVAWq1UBLhMOZJHicxvneppo1KPTVHZpNPhTtaPrU/jgNOzvJDbYDkx9Z92AkJAENLhcf4XBQcrCG8B/YkuHArAEd0x1G6aePCX2WfrgCaIZ+q75/WCvVAaZzF0nhZJD0/Za2mQaqbAQ4VAaT2ngSRbUY89Ix1hqWfEDOtPLwz/yErPpo8ncJf7K5RVQSj3gJNfBzx12y+x1yt8X9hVf+GzpFewqQl0FEKv5d/WjjvE8ReW7TJvNRjLSpESLROrpPEiaG0noPeZI3BCvE9Pw0twcqGnqEp4+0cvw/sjGjsILjA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(30864003)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007)(142923001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: Gw/FMCJtgLCYlGMAUYx80AgatWDDKh/bBqxMmDqbbDn7Isp8C6qSNMY+a7xktxToQSZwdZXlkJJU+P35j23IYj5ngJN+y+vKIu4rvvJOjIv0SMXsA9Fre728JXXNHW4br0gkVhmglcFXeVA9nu2Dwhb191TBDzOM5PsZ3b3XdSZnfmW1Unnenud5KgndxsrExH8AREZesCSUGBb0nDJMDnaaYbfx+7/ihu+WBf1IMyNJuFOoLYxpjZ7f98BUe6MkRHdkZ3zQTO5gjK0Pvoq2N6/dbAiHk2pbR7ufWSHaqy3epXOLaoXTGe0gs4fUJZqJ8rJF4KfKmqfPshNy0rA7n+U9zSWsdNqkxjY0uB4zWRrY4RXyL0vvS/b8+wiPd0zvhuqG2v58DHdXbBdliNljCaFsvA9Bh36RFezMNcRNKVCpINPb4SaYcCm9fenhmWo5iTpaWJKGdc8YRnUxlZxgVZ3SX8LFCoHX0dzQlQ5BsSuDcKEwddMdo0ZdxYPqmxfIZ2wqv5DQ3KcYxHT1fKAg6gTvp6MTDRUmK980Yt1RM3wUb4LwijBKiYXCFYTWsTHr8MNLfZ5bgagAdA+a2bhwKHEGJsNbiI4Ygs6M7xcLaJjmAzYtnH2LpX5rLLloNyHdZrv4zKKTAt5BysZvYs0IhlNbeDB6lI0Fn9yIpeXQSObzgqLbq4TOI9m5czCBwKngxZPL5jUP21OvrDcr/g+5cj1PWu6NkhxpuyQed4LUZDhNlQGy9R3U2kJGrGRKqRXJodp4TmdkODCEAbzxHe3tTf2oRSw+rOqe4GhHunpCNngxpt7HOgIdC1gcb6jUftQBJWI4U01Js5PtMNWnAhmwZbrBQZNbOfteo0WYMnPYh47B8sdDqgdLsWDfmLxenzegLsrtQ3YRPNrdI4rLe+Y6Zz0SccQwx6CytvJ/r+++2iK/6kTV44+C87BAagueKDqxDfNmy7etHa2BYWknMtLfWCyhkI3RF0ETBh7GyeVXuf+5rjrdYAwwu6N1Qj0BMf6RmUA/ExfRL0vRnpnL/OtwygoaZFBua5ze/FWCXlHn3sCHVxk3cEqA89SEhlnoVaangzgddgY/XFmqzgRKvAoQT5rcez9nCStQCc01puL6G8Dr++ukEyhKptYbIF2R0CrP6JBcfrfhUgF970OKgxRBPGH2rBOtztJ9J3iU0u0benvJTVYh6CjVnzCZifvfmbgs9iUA8Si3uO0XKRl8G1Uerb3HK0QFWPxUTk+m+9jdAuyJThQgWZMMKqjyZvYq2NrCiQXUMx4Qh6RU4attyZP7ukik5W+M8gfui+WkSPL7oXcuL2kFCIPYjixHMMJVxaH7+2X2FqP+L13rFPsdSj06aES3H1M7BlNdlNSkPyFE4XfW4rEeiHU0lXkNiLB8fLpjvd0YCw52jaCPjgiSMg7HpVPZWmwBJZlOmp7qGGRVxApyZeDy8/6ynNZi2BYVXsqhqruQ0FiDy4eqMKTB39hyggnBAcEBhqDEVehCLc0GZ/ckjQRTWumNqTJLdY8zqOV3uOhY2hk+fEIvAavowqg6i/9iMMiu3kxblhulZPW6PGWO0ZN4hVyDBV/AMkr6HOaVf0iGeiyYMEkWRTqNjbrolJs9fV6ZN0Ha7G+3O5EwL9bfTXkbmNq0IkBSAZlMEUwB X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: c88bfce1-e04a-49d4-337f-08d9c0d16c6f X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:55.0665 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: /fsiuxjOoHy4q3ePVxVai9giVSYWudQp1MvnjBGyhd/u04LK8aUpVaRu/S/FCD8BX/tv/CO/t+tgC+Gy5iagdg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 adultscore=0 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: BEWF7JwLln2qZz8_1WZPzaN2uR9Jj7dv X-Proofpoint-GUID: BEWF7JwLln2qZz8_1WZPzaN2uR9Jj7dv Precedence: bulk List-ID: These symbols are terribly inefficiently stored at the moment. Add a simple optimizer which fuses obj2mod_elem entries and uses this to implement three cheap optimizations: - duplicate names are eliminated from .kallsyms_module_names. - entries in .kallsyms_modules which point at single-file modules which also appear in a multi-module list are redirected to point inside that list, and the single-file entry is dropped from .kallsyms_module_names. Thus, modules which contain some object files shared with other modules and some object files exclusive to them do not double up the module name. (There might still be some duplication between multiple multi-module lists, but this is an extremely marginal size effect, and resolving it would require an extra layer of lookup tables which would be even more complex, and incompressible to boot). - Entries in .kallsyms_modules that would contain the same value after the above optimizations are fused together, along with their corresponding .kallsyms_module_addresses/offsets entries. Due to this fusion process, and because object files can be split apart into multiple parts by the linker for hot/cold partitioning and the like, entries in .kallsyms_module_addresses/offsets no longer correspond 1:1 to object files, but more to some contiguous range of addresses which are guaranteed to belong to a single built-in module, but which may well stretch over multiple object files. The optimizer's time complexity is O(log n) in the number of objfiles at most (and probably much lower), so, given the relatively low number of objfiles, its runtime overhead is in the noise. Optimization reduces the overhead of the kallmodsyms tables by about 7500 items, dropping the .tmp_kallsyms2.o object file size by about 33KiB, leaving it 8672 bytes larger than before: a gain of .4%. The vmlinux size is not yet affected because the variables are not used and are eliminated by the linker: but if they were used (after the next commit), the size impact of all of this on the final kernel is minimal: in my testing, the kallsyms2.S file went from 14107772 to 14137245 bytes, a gain of 29743 bytes, or 0.16%: vmlinux gained 10824 bytes, a gain of .017%, and the compressed vmlinux only 7552 bytes, a gain of .08%: though the latter two values are very configuration-dependent, they seem likely to scale roughly with the kernel they are part of. Signed-off-by: Nick Alcock --- scripts/kallsyms.c | 267 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 258 insertions(+), 9 deletions(-) diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 8f87b724d0fa..93fdf0dcf587 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -85,6 +85,17 @@ static unsigned int strhash(const char *s) return hash; } +static unsigned int memhash(char *s, size_t len) +{ + /* fnv32 hash */ + unsigned int hash = 2166136261U; + size_t i; + + for (i = 0; i < len; i++) + hash = (hash ^ *(s + i)) * 0x01000193; + return hash; +} + #define OBJ2MOD_BITS 10 #define OBJ2MOD_N (1 << OBJ2MOD_BITS) #define OBJ2MOD_MASK (OBJ2MOD_N - 1) @@ -94,14 +105,24 @@ struct obj2mod_elem { size_t nmods; /* number of modules in "mods" */ size_t mods_size; /* size of all mods together */ int mod_offset; /* offset in .kallsyms_module_names */ + /* + * If set at emission time, this points at another obj2mod entry that + * contains the module name we need (possibly at a slightly later + * offset, if the entry is for an objfile that appears in many modules). + */ + struct obj2mod_elem *xref; struct obj2mod_elem *obj2mod_next; + struct obj2mod_elem *mod2obj_next; }; /* - * Map from object files to obj2mod entries (a unique mapping). + * Map from object files to obj2mod entries (a unique mapping), and vice versa + * (not unique, but entries for objfiles in more than one module in this hash + * are ignored). */ static struct obj2mod_elem *obj2mod[OBJ2MOD_N]; +static struct obj2mod_elem *mod2obj[OBJ2MOD_N]; static size_t num_objfiles; /* @@ -143,6 +164,8 @@ static void obj2mod_add(char *obj, char *mod) elem = obj2mod_get(obj); if (!elem) { + int j = strhash(mod) & OBJ2MOD_MASK; + elem = malloc(sizeof(struct obj2mod_elem)); if (!elem) goto oom; @@ -156,8 +179,15 @@ static void obj2mod_add(char *obj, char *mod) elem->obj2mod_next = obj2mod[i]; obj2mod[i] = elem; + elem->mod2obj_next = mod2obj[j]; + mod2obj[j] = elem; num_objfiles++; } else { + /* + * TU appears in multiple modules. mod2obj for this entry will + * be ignored from now on, except insofar as it is needed to + * maintain the hash chain. + */ elem->mods = realloc(elem->mods, elem->mods_size + strlen(mod) + 1); if (!elem->mods) @@ -177,6 +207,164 @@ static void obj2mod_add(char *obj, char *mod) fprintf(stderr, "kallsyms: out of memory\n"); exit(1); } + +/* + * Used inside optimize_obj2mod to identify duplicate module entries. + */ +struct obj2mod_modhash_elem { + struct obj2mod_elem *elem; + unsigned int modhash; /* hash value of this entry */ +}; + +static int qstrcmp(const void *a, const void *b) +{ + return strcmp((const char *) a, (const char *) b); +} + +static int qmodhash(const void *a, const void *b) +{ + const struct obj2mod_modhash_elem *el_a = a; + const struct obj2mod_modhash_elem *el_b = b; + if (el_a->modhash < el_b->modhash) + return -1; + else if (el_a->modhash > el_b->modhash) + return 1; + return 0; +} + +/* + * Associate all TUs in obj2mod which refer to the same module with a single + * obj2mod entry for emission, preferring to point into the module list in a + * multi-module objfile. + */ +static void optimize_obj2mod(void) +{ + size_t i; + size_t n = 0; + struct obj2mod_elem *elem; + struct obj2mod_elem *dedup; + /* An array of all obj2mod_elems, later sorted by hashval. */ + struct obj2mod_modhash_elem *uniq; + struct obj2mod_modhash_elem *last; + + /* + * Canonicalize all module lists by sorting them, then compute their + * hash values. + */ + uniq = malloc(sizeof(struct obj2mod_modhash_elem) * num_objfiles); + if (uniq == NULL) + goto oom; + + for (i = 0; i < OBJ2MOD_N; i++) { + for (elem = obj2mod[i]; elem; elem = elem->obj2mod_next) { + if (elem->nmods >= 2) { + char **sorter; + char *walk; + char *tmp_mods; + size_t j; + + tmp_mods = malloc(elem->mods_size); + sorter = malloc(sizeof(char *) * elem->nmods); + if (sorter == NULL || tmp_mods == NULL) + goto oom; + memcpy(tmp_mods, elem->mods, elem->mods_size); + + for (j = 0, walk = tmp_mods; j < elem->nmods; + j++) { + sorter[j] = walk; + walk += strlen(walk) + 1; + } + qsort(sorter, elem->nmods, sizeof (char *), + qstrcmp); + for (j = 0, walk = elem->mods; j < elem->nmods; + j++) { + strcpy(walk, sorter[j]); + walk += strlen(walk) + 1; + } + free(tmp_mods); + free(sorter); + } + + uniq[n].elem = elem; + uniq[n].modhash = memhash(elem->mods, elem->mods_size); + n++; + } + } + + qsort (uniq, num_objfiles, sizeof (struct obj2mod_modhash_elem), + qmodhash); + + /* + * Work over multimodule entries. These must be emitted into + * .kallsyms_module_names as a unit, but we can still optimize by + * reusing some other identical entry. Single-file modules are amenable + * to the same optimization, but we avoid doing it for now so that we + * can prefer to point them directly inside a multimodule entry. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + const char *onemod; + size_t j; + + if (uniq[i].elem->nmods < 2) + continue; + + /* Duplicate multimodule. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i].modhash) { + uniq[i].elem->xref = last->elem; + continue; + } + + /* + * Single-module entries relating to modules also emitted as + * part of this multimodule entry can refer to it: later, we + * will hunt down the right specific module name within this + * multimodule entry and point directly to it. + */ + onemod = uniq[i].elem->mods; + for (j = uniq[i].elem->nmods; j > 0; j--) { + int h = strhash(onemod) & OBJ2MOD_MASK; + + for (dedup = mod2obj[h]; dedup; + dedup = dedup->mod2obj_next) { + if (dedup->nmods > 1) + continue; + + if (strcmp(dedup->mods, onemod) != 0) + continue; + dedup->xref = uniq[i].elem; + assert (uniq[i].elem->xref == NULL); + } + onemod += strlen(onemod) + 1; + } + + last = &uniq[i]; + } + + /* + * Now traverse all single-module entries, xreffing every one that + * relates to a given module to the first one we saw that refers to that + * module. + */ + for (i = 0, last = NULL; i < num_objfiles; i++) { + if (uniq[i].elem->nmods > 1) + continue; + + if (uniq[i].elem->xref != NULL) + continue; + + /* Duplicate module name. Reuse the first we saw. */ + if (last != NULL && last->modhash == uniq[i].modhash) { + uniq[i].elem->xref = last->elem; + assert (last->elem->xref == NULL); + continue; + } + last = &uniq[i]; + } + return; +oom: + fprintf(stderr, "kallsyms: out of memory optimizing module list\n"); + exit(EXIT_FAILURE); +} #endif /* CONFIG_KALLMODSYMS */ static void usage(void) @@ -479,7 +667,7 @@ static void output_kallmodsyms_modules(void) size_t i; /* - * Traverse and emit, updating mod_offset accordingly. + * Traverse and emit, chasing xref and updating mod_offset accordingly. * Emit a single \0 at the start, to encode non-modular objfiles. */ output_label("kallsyms_module_names"); @@ -489,9 +677,15 @@ static void output_kallmodsyms_modules(void) elem = elem->obj2mod_next) { const char *onemod; size_t i; + struct obj2mod_elem *out_elem = elem; - elem->mod_offset = offset; - onemod = elem->mods; + if (elem->xref) + out_elem = elem->xref; + if (out_elem->mod_offset != 0) + continue; /* Already emitted. */ + + out_elem->mod_offset = offset; + onemod = out_elem->mods; /* * Technically this is a waste of space: we could just @@ -500,13 +694,13 @@ static void output_kallmodsyms_modules(void) * entry, but doing it this way makes it more obvious * when an entry is a multimodule entry. */ - if (elem->nmods != 1) { + if (out_elem->nmods != 1) { printf("\t.byte\t0\n"); - printf("\t.byte\t%zi\n", elem->nmods); + printf("\t.byte\t%zi\n", out_elem->nmods); offset += 2; } - for (i = elem->nmods; i > 0; i--) { + for (i = out_elem->nmods; i > 0; i--) { printf("\t.asciz\t\"%s\"\n", onemod); offset += strlen(onemod) + 1; onemod += strlen(onemod) + 1; @@ -533,6 +727,13 @@ static void output_kallmodsyms_objfiles(void) long long offset; int overflow; + /* + * Fuse consecutive address ranges citing the same object file + * into one. + */ + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + if (base_relative) { if (!absolute_percpu) { offset = addrmap[i].addr - relative_base; @@ -558,6 +759,13 @@ static void output_kallmodsyms_objfiles(void) for (i = 0; i < addrmap_num; i++) { struct obj2mod_elem *elem = addrmap[i].objfile; + int orig_nmods; + const char *orig_modname; + int mod_offset; + + if (i > 0 && addrmap[i-1].objfile == addrmap[i].objfile) + continue; + /* * Address range cites no object file: point at 0, the built-in * module. @@ -568,13 +776,53 @@ static void output_kallmodsyms_objfiles(void) continue; } + orig_nmods = elem->nmods; + orig_modname = elem->mods; + + /* + * Chase down xrefs, if need be. There can only be one layer of + * these: from single-module entry to other single-module entry, + * or from single- or multi-module entry to another multi-module + * entry. Single -> single and multi -> multi always points at + * the start of the xref target, so its offset can be used as is. + */ + if (elem->xref) + elem = elem->xref; + + if (elem->nmods == 1 || orig_nmods > 1) + mod_offset = elem->mod_offset; + else { + /* + * If this is a reference from a single-module entry to + * a multi-module entry, hunt down the offset to this + * specific module's name (which is guaranteed to be + * present: see optimize_obj2mod). + */ + + size_t j = elem->nmods; + const char *onemod = elem->mods; + mod_offset = elem->mod_offset; + + for (; j > 0; j--) { + if (strcmp(orig_modname, onemod) == 0) + break; + onemod += strlen(onemod) + 1; + } + assert (j > 0); + /* + * +2 to skip the null byte and count at the start of + * the multimodule entry. + */ + mod_offset += onemod - elem->mods + 2; + } + /* * Zero offset is the initial \0, there to catch uninitialized * obj2mod entries, and is forbidden. */ - assert (elem->mod_offset != 0); + assert (mod_offset != 0); - printf("\t.long\t0x%x\n", elem->mod_offset); + printf("\t.long\t0x%x\n", mod_offset); emitted_objfiles++; } @@ -1093,6 +1341,7 @@ static void read_modules(const char *modules_builtin) free(module_name); modules_thick_iter_free(i); + optimize_obj2mod(); /* * Read linker map. From patchwork Thu Dec 16 20:19:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682475 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id AF155C433EF for ; Thu, 16 Dec 2021 20:20:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241321AbhLPUUL (ORCPT ); Thu, 16 Dec 2021 15:20:11 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:36148 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241315AbhLPUUF (ORCPT ); Thu, 16 Dec 2021 15:20:05 -0500 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn54P025850; Thu, 16 Dec 2021 20:20:00 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=q0GBE8rbSHLO6USczfw1PFrUGrmQ8mGv7BbNVlM11+A=; b=hFWktOkJs8R9lSYEdFHDgIOvVxyAh+J5Zz3WFS0lPjEQxv231eFuGu2JFiCTJmpYH0Wx bTkBFxJH+OD/OtvPU1DiRiptisExmbxpjDNo3BzNa+1xDdr+YRB7sxQWYqLiK7wadG3u gDW/IOO0LU4+h41jabx2KUMXZKt6jNeTkbQXBEFrXBrzAh8oUtYpeWf+gvoVSXixHaG0 D0LubNXzDYWdieSCwhVZOMW2Hsnu3Vl3EUJqJDa1IO/tTMzxxuu4svVWmyJ70bJaOMkN SVAreXgojD9fPfIDZhg1pSupS7J6wVZPCrgeqG0s0mFpMZU50suONCVbbwjwZvbttZlG fA== Received: from aserp3030.oracle.com (aserp3030.oracle.com [141.146.126.71]) by mx0b-00069f02.pphosted.com with ESMTP id 3cyknp3y7n-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:59 +0000 Received: from pps.filterd (aserp3030.oracle.com [127.0.0.1]) by aserp3030.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGCTc123775; Thu, 16 Dec 2021 20:19:58 GMT Received: from nam10-bn7-obe.outbound.protection.outlook.com (mail-bn7nam10lp2106.outbound.protection.outlook.com [104.47.70.106]) by aserp3030.oracle.com with ESMTP id 3cyjuae0cy-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:19:58 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=kQQ6cIdhYZMN05a8zusCZ52T6i3FXdtBq6+/B7Eaf3uRM5dyu4HiyBrEZ/Ui59Odc6LLdK54wQrBRaz4B2IrZJN3txt01cGtIdB9it3uFWJk9mMVxULqxrMmIPSr1Gn6kuZ912vpkhKU+pZ07X3dcHxkZ6hytWPZmA6VhVXjuewydsoK8HzQGw5FgE1n1N9a11xJd9Ei6cE2c6byBVvVXCnkhSf6ZEmVUv6acvIo2X7HFZFjChjMU1s6xjzkgvLU+V433I7hcMa3DXTWlSs3SP03xhfZhHOg3YA2FvJLPAj/Qsk8CETGwkHuM/oSQWshQULvRV+65pPDMN7SsyzdEg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=q0GBE8rbSHLO6USczfw1PFrUGrmQ8mGv7BbNVlM11+A=; b=WZkY8tjf63JAmXNTCY6Umgbj6Bqkq3c/4s3rgFzH1uMEi9FqdvOM+7hmOusZ4WhgqdlDlz5Vz/bm+OKrBgEzATfc/cSpOfxECgqYwBRotI92NWUMf58kb7IhubbEAiP14/ccwzOVARsbzsVDCiqGZwSLdJ7pX76dhdZWgYnqutX+NJp/3qdD+OPvndLlDin38tF6Il//a6o8G/5giLYu/e1vFlxBh9L/hlYpiUI7oRdE6vphRvkzklnb8s7qXea0e9DVT9GKulyDPUkGVeEJXgGVeerZdmTJaaFOHg4c1rovWHpL0B5cAhJoSWZTPM7tTN8KgwOZf43mZRn3RVeF9g== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=q0GBE8rbSHLO6USczfw1PFrUGrmQ8mGv7BbNVlM11+A=; b=IDnxlQ6WhSMv7v3Qagvbj72ToSnLf4WetT0W8fdQ5Fm+mRhDPD7oWrEsXFJk7LfyILRuuBBeHGkXomGUvEnmuulCVPvW9cRICo1HRELUEnmnyYNvjXRFXm24Kg0rM65ldjuwPV5/iIoVI7kZSNO96rOarqCjmM+4z/ALkmUGLNA= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SN6PR10MB2541.namprd10.prod.outlook.com (2603:10b6:805:44::28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.17; Thu, 16 Dec 2021 20:19:56 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:56 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 6/7] kallsyms: add /proc/kallmodsyms Date: Thu, 16 Dec 2021 20:19:18 +0000 Message-Id: <20211216201919.234994-7-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 7f14610a-cfb6-44d0-4636-08d9c0d16d5b X-MS-TrafficTypeDiagnostic: SN6PR10MB2541:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5516; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: J9FIsUhcXgWGNXN5UVyt6NUqpoTUP1rHh3JEG9ckabIt3WN9daPz4wgLfd1sxoGhmXtqT1+weN3ljWdHp54qaO6wAwVmt0G5cnDlonP9RbJSzt4OooeCJBCQanGOtwYOrp967qSd0zjsZLn88AIhZlW9EsdnvPFX4Q+JJFUvRK1hHAxT0LOTEH9VxyAhJZy365kvYs/VGLUPEE3y252t6gdDGfr8Hznp+4hugyq+SUbwBm6DXDfauKE5+QnqpD9Q21wSJbAyaxwqQwh4bijcyTIbb5R8ha7QL4WrXnfXnOi0H7LD9Rykpwjqj7kmaYL/Xs3okEC3cLumKSj0wSTAnXWTjsGxknJPKoEQmLhfRYDXjMsv+3kJ37XzpMVrIG/c0q9zhfoQZq3IGWf2bGCrrtYOcpjoGKQe5azTmHYRqhW2mD4pHVCYSOI/DgABzbCPlITJir/B8iwzpf8fzeSa/atNOm3ZIGZrNdzv5oJGRB2JoEJNyJjjFABaimBnsffKqK5NUQT7u+DKFmyuTRn2Uea8a5Db6BSdsxhzTR+aG5pjjDIL1I+yRxXETtseVAG7nn9dZZ4Bp1Kik+LG1f0S6dpOuK4wSkY2Nw4PW8BDvxlBpLjYwIPx8bvtRCdnQ5TXWVslnHCaWo6gvLtqh7Pj3Q== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(30864003)(83380400001)(52116002)(38100700002)(86362001)(44832011)(36756003)(5660300002)(1076003)(2616005)(6506007)(508600001)(6666004)(66946007)(66556008)(66476007)(186003)(8676002)(316002)(107886003)(6486002)(2906002)(8936002)(4326008)(6512007);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: EDrVF/dHt2lwAynQAs0K/Dshy7yo3gjvXP7lrnvPBtJCR0V4T85lr6ojbtZ/dwAEi2ysyyLn41e8UlxoHI9a1eejtFcxRV7FaLff8Wh9c0K8YiqyabNa8sk6E7V0gA9TrCY9C2Lb2uYjI5L+PgGCrFgke6+WKiYHnt9v0QUFMXvabaCGb2Knj/8AA/+hTrYogeRaK/IO2rt8PGYy4PBzxaUgT2SIPQlAb7PJOA7OSS//22FBfFSokDaUeScuDJvZly1YAwcAFJKfjeKbE+/blLEVIfFByAf9o0WobCPLDnV8eHhe0lx7mKjy0iRVp/tGIHr7DsnTbiTm+0PtIKhNWVcKe10nDVwNuAqej81Aqlzz8bjYUIZIyjYCgkRXXWyE5FmAiisVp+mk7SsPEZ1DQn3vXFSXZYO2BYxishOTDhfIebKwf8ri99UNdvQOKd/N9JTHKXNTmYBfIipdE9ZgHlblpXhfSHLfHtfIgy8XmlpoLpV/nMfCq4E7LdZM/Pa2wZW1eeyKcGI6Bho0sAOD9vwPqYC/MR6KljHx+8BrjCkPxWUy2V6OAEirTfB1eBtLxv5K4oTyy/hMirr8r3j4Yx8HBTS5usj5Orm7tehyZ9Lq68vrDe5NvXIG9vY7rgFUSOEQRr9TrGjxU5kQ8bu6AeohG2Oldx2KGaFK5nW15FJSRZcVTbBgDNEzOCfT0GQ2AzG5sEwYUtnA3ZHwFIvMMPMOdvfFVK32Qv10XyAhC0DLyp3S4XZz71xywE4E8vHXVdcEwKSPphS+8mMRY25Ql2nzRqS7qj0mypZKbyvou2SkqeE1R4TrlRzEefZ8irs201h+gPwTZtdKyyv7NXbDOjEJvDuGrRTHZsU9h172fHwtHK+qLY+kmfqYpa+oafretMxT2WTBZrTl9XVtQeOnl5SoI3x/RCNIjoCwc5duRSUFF+SnUyRnWqAegh9/rhyrpeERZQ+wiNFLHHRp0108zTgSeO6PkRD01WSU3SuMU54Bh1uZ3OUwhPrb04bJJRuBnQRziSOid8wkbhcVUakrrg+GrEmKK866TjDDBF97BHnWagp/LI0PTvu2lyaYNQRRJBH4aW8iussMNbOWYo/5f8jMrYQdwyiw22BvHEbVo/1juFwJKZ5yqEuSX8pBDU2ka/9Lt/goQ1sxl3A7KbMWDyG8v3yp/s3pwWZc44rUn88LRcyoCjimZ6iMZzwT8YB6Dui6XPEqK3ELZBJvYRV5noQZS2rb4EWelesz/foQmRjJM2p7GuwYs3rNCxlCiS3xEyobfqG9FyU1GjlUpzYQEhXaz2RKWHdPWFkYknmRaljU/bXecdVAS5LifO9vcP2+2EGIx5Lk8BHf9ZZhxGhjlmNpwrpBbXmyEbI8HvMijrVoAigmkWxYfP3o1hA68r/5OL+qDoGTTysIlfZcOCYEtAxYTxdTgeHgpAO52j+C8supOnHfDekErs49+9jtoBpZLWySc80UWrae6/l0Fudzka9RCWlF8Iz5XgvroftzIUXI4hz2W3pbi2CIinme84tgVRhpKJVPuBp5siyyo2P+AeiDNynB/2WcB94At3gqjq0Vy4Xlt/qc1B8kPIlJimk1Sq9LBHlpmDxEHGYmjttLAsq+oNTp+3LxPDSiQcVZKyB64KUzRbjUGPAMASbU3P4K X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 7f14610a-cfb6-44d0-4636-08d9c0d16d5b X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:56.7219 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: yJomzvGvRzKrZ37poC0Q5l/PDHWku0rBNW2zkTRX3S+Y3HciZpkay8tVnqMTu2izHIBWp3i3vQX6kr2Rflhnqw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SN6PR10MB2541 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxlogscore=999 phishscore=0 spamscore=0 mlxscore=0 adultscore=0 bulkscore=0 suspectscore=0 malwarescore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: yNDcpvEZ5VgBVjb54KsuTiJ5fqXJfuf_ X-Proofpoint-GUID: yNDcpvEZ5VgBVjb54KsuTiJ5fqXJfuf_ Precedence: bulk List-ID: Use the tables added in the previous commits to introduce a new /proc/kallmodsyms, in which [module names] are also given for things that *could* have been modular had they not been built in to the kernel. So symbols that are part of, say, ext4 are reported as [ext4] even if ext4 happens to be buiilt in to the kernel in this configuration. Symbols that are part of multiple modules at the same time are shown with [multiple] [module names]: consumers will have to be ready to handle such lines. Also, kernel symbols for built-in modules will be sorted by size, as usual for the core kernel, so will probably appear interspersed with other symbols that are part of different modules and non-modular always-built-in symbols, which, as usual, have no square-bracketed module denotation. This differs from /proc/kallsyms, where all symbols associated with a module will always appear in a group (and randomly ordered). The result looks like this: ffffffff8b013d20 t pt_buffer_setup_aux ffffffff8b014130 T intel_pt_interrupt ffffffff8b014250 T cpu_emergency_stop_pt ffffffff8b014280 t rapl_pmu_event_init [intel_rapl_perf] ffffffff8b0143c0 t rapl_event_update [intel_rapl_perf] ffffffff8b014480 t rapl_pmu_event_read [intel_rapl_perf] ffffffff8b014490 t rapl_cpu_offline [intel_rapl_perf] ffffffff8b014540 t __rapl_event_show [intel_rapl_perf] ffffffff8b014570 t rapl_pmu_event_stop [intel_rapl_perf] This is emitted even if intel_rapl_perf is built into the kernel (but, obviously, not if it's not in the .config at all, or is in a module that is not loaded). Further down, we see what happens when object files are reused by multiple modules, all of which are built in to the kernel: ffffffffa22b3aa0 t handle_timestamp [liquidio] ffffffffa22b3b50 t free_netbuf [liquidio] ffffffffa22b3ba0 t liquidio_ptp_settime [liquidio] ffffffffa22b3c30 t liquidio_ptp_adjfreq [liquidio] [...] ffffffffa22b9490 t lio_vf_rep_create [liquidio] ffffffffa22b96a0 t lio_vf_rep_destroy [liquidio] ffffffffa22b9810 t lio_vf_rep_modinit [liquidio] ffffffffa22b9830 t lio_vf_rep_modexit [liquidio] ffffffffa22b9850 t lio_ethtool_get_channels [liquidio] [liquidio_vf] ffffffffa22b9930 t lio_ethtool_get_ringparam [liquidio] [liquidio_vf] ffffffffa22b99d0 t lio_get_msglevel [liquidio] [liquidio_vf] ffffffffa22b99f0 t lio_vf_set_msglevel [liquidio] [liquidio_vf] ffffffffa22b9a10 t lio_get_pauseparam [liquidio] [liquidio_vf] ffffffffa22b9a40 t lio_get_ethtool_stats [liquidio] [liquidio_vf] ffffffffa22ba180 t lio_vf_get_ethtool_stats [liquidio] [liquidio_vf] ffffffffa22ba4f0 t lio_get_regs_len [liquidio] [liquidio_vf] ffffffffa22ba530 t lio_get_priv_flags [liquidio] [liquidio_vf] ffffffffa22ba550 t lio_set_priv_flags [liquidio] [liquidio_vf] ffffffffa22ba580 t lio_set_fecparam [liquidio] [liquidio_vf] ffffffffa22ba5f0 t lio_get_fecparam [liquidio] [liquidio_vf] [...] ffffffffa22cbd10 t liquidio_set_mac [liquidio_vf] ffffffffa22cbe90 t handle_timestamp [liquidio_vf] ffffffffa22cbf40 t free_netbuf [liquidio_vf] ffffffffa22cbf90 t octnet_link_status_change [liquidio_vf] ffffffffa22cbfc0 t liquidio_vxlan_port_command.constprop.0 [liquidio_vf] Like /proc/kallsyms, the output is driven by address, so keeps the curious property of /proc/kallsyms that symbols (like free_netbuf above) may appear repeatedly with different addresses: but now, unlike in /proc/kallsyms, we can see that those symbols appear repeatedly because they are *different symbols* that ultimately belong to different modules, all of which are built in to the kernel. As with /proc/kallsyms, non-root usage produces addresses that are all zero. I am not wedded to the name or format of /proc/kallmodsyms, but felt it best to split it out of /proc/kallsyms to avoid breaking existing kallsyms parsers. Another possible syntax might be to use {curly brackets} or something to denote built-in modules: it might be possible to drop /proc/kallmodsyms and make /proc/kallsyms emit things in this format. (Equally, now kallmodsyms data uses very little space, the CONFIG_KALLMODSYMS config option might be something people don't want to bother with.) Internally, this uses a new kallsyms_builtin_module_address() almost identical to kallsyms_sym_address() to get the address corresponding to a given .kallsyms_modules index, and a new get_builtin_module_idx quite similar to get_symbol_pos to determine the index in the .kallsyms_modules array that relates to a given address. Save a little time by exploiting the fact that all callers will only ever traverse this list from start to end by allowing them to pass in the previous index returned from this function as a hint: thus very few bsearches are actually needed. (In theory this could change to just walk straight down kallsyms_module_addresses/offsets and not bother bsearching at all, but doing it this way is hardly any slower and much more robust.) The display process is complicated a little by the weird format of the .kallsyms_module_names table: we have to look for multimodule entries and print them as space-separated lists of module names. Signed-off-by: Nick Alcock --- kernel/kallsyms.c | 242 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 227 insertions(+), 15 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 3011bc33a5ba..c81610ffc4ba 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -48,8 +48,18 @@ __section(".rodata") __attribute__((weak)); extern const unsigned long kallsyms_relative_base __section(".rodata") __attribute__((weak)); +extern const unsigned long kallsyms_num_modules +__section(".rodata") __attribute__((weak)); + +extern const unsigned long kallsyms_module_names_len +__section(".rodata") __attribute__((weak)); + extern const char kallsyms_token_table[] __weak; extern const u16 kallsyms_token_index[] __weak; +extern const unsigned long kallsyms_module_addresses[] __weak; +extern const int kallsyms_module_offsets[] __weak; +extern const u32 kallsyms_modules[] __weak; +extern const char kallsyms_module_names[] __weak; extern const unsigned int kallsyms_markers[] __weak; @@ -205,6 +215,25 @@ static bool cleanup_symbol_name(char *s) return false; } +#ifdef CONFIG_KALLMODSYMS +static unsigned long kallsyms_builtin_module_address(int idx) +{ + if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) + return kallsyms_module_addresses[idx]; + + /* values are unsigned offsets if --absolute-percpu is not in effect */ + if (!IS_ENABLED(CONFIG_KALLSYMS_ABSOLUTE_PERCPU)) + return kallsyms_relative_base + (u32)kallsyms_module_offsets[idx]; + + /* ...otherwise, positive offsets are absolute values */ + if (kallsyms_module_offsets[idx] >= 0) + return kallsyms_module_offsets[idx]; + + /* ...and negative offsets are relative to kallsyms_relative_base - 1 */ + return kallsyms_relative_base - 1 - kallsyms_module_offsets[idx]; +} +#endif + /* Lookup the address for this symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name) { @@ -308,6 +337,54 @@ static unsigned long get_symbol_pos(unsigned long addr, return low; } +/* + * The caller passes in an address, and we return an index to the corresponding + * builtin module index in .kallsyms_modules, or (unsigned long) -1 if none + * match. + * + * The hint_idx, if set, is a hint as to the possible return value, to handle + * the common case in which consecutive runs of addresses relate to the same + * index. + */ +#ifdef CONFIG_KALLMODSYMS +static unsigned long get_builtin_module_idx(unsigned long addr, unsigned long hint_idx) +{ + unsigned long low, high, mid; + + if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) + BUG_ON(!kallsyms_module_addresses); + else + BUG_ON(!kallsyms_module_offsets); + + /* + * Do a binary search on the sorted kallsyms_modules array. The last + * entry in this array indicates the end of the text section, not an + * object file. + */ + low = 0; + high = kallsyms_num_modules - 1; + + if (hint_idx > low && hint_idx < (high - 1) && + addr >= kallsyms_builtin_module_address(hint_idx) && + addr < kallsyms_builtin_module_address(hint_idx + 1)) + return hint_idx; + + if (addr >= kallsyms_builtin_module_address(low) + && addr < kallsyms_builtin_module_address(high)) { + while (high - low > 1) { + mid = low + (high - low) / 2; + if (kallsyms_builtin_module_address(mid) <= addr) + low = mid; + else + high = mid; + } + return low; + } + + return (unsigned long) -1; +} +#endif + /* * Lookup an address but don't bother to find any names. */ @@ -579,6 +656,8 @@ struct kallsym_iter { char type; char name[KSYM_NAME_LEN]; char module_name[MODULE_NAME_LEN]; + const char *builtin_module_names; + unsigned long hint_builtin_module_idx; int exported; int show_value; }; @@ -609,6 +688,8 @@ static int get_ksymbol_mod(struct kallsym_iter *iter) &iter->value, &iter->type, iter->name, iter->module_name, &iter->exported); + iter->builtin_module_names = NULL; + if (ret < 0) { iter->pos_mod_end = iter->pos; return 0; @@ -628,6 +709,8 @@ static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter) &iter->value, &iter->type, iter->name, iter->module_name, &iter->exported); + iter->builtin_module_names = NULL; + if (ret < 0) { iter->pos_ftrace_mod_end = iter->pos; return 0; @@ -642,6 +725,7 @@ static int get_ksymbol_bpf(struct kallsym_iter *iter) strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN); iter->exported = 0; + iter->builtin_module_names = NULL; ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end, &iter->value, &iter->type, iter->name); @@ -662,23 +746,53 @@ static int get_ksymbol_kprobe(struct kallsym_iter *iter) { strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN); iter->exported = 0; + iter->builtin_module_names = NULL; return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end, &iter->value, &iter->type, iter->name) < 0 ? 0 : 1; } /* Returns space to next name. */ -static unsigned long get_ksymbol_core(struct kallsym_iter *iter) +static unsigned long get_ksymbol_core(struct kallsym_iter *iter, int kallmodsyms) { unsigned off = iter->nameoff; - iter->module_name[0] = '\0'; + iter->exported = 0; iter->value = kallsyms_sym_address(iter->pos); iter->type = kallsyms_get_symbol_type(off); + iter->module_name[0] = '\0'; + iter->builtin_module_names = NULL; + off = kallsyms_expand_symbol(off, iter->name, ARRAY_SIZE(iter->name)); +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + unsigned long mod_idx = (unsigned long) -1; + + if (kallsyms_module_offsets) + mod_idx = + get_builtin_module_idx(iter->value, + iter->hint_builtin_module_idx); + /* + * This is a built-in module iff the tables of built-in modules + * (address->module name mappings) and module names are known, + * and if the address was found there, and if the corresponding + * module index is nonzero. All other cases mean off the end of + * the binary or in a non-modular range in between one or more + * modules. (Also guard against a corrupt kallsyms_objfiles + * array pointing off the end of kallsyms_modules.) + */ + if (kallsyms_modules != NULL && kallsyms_module_names != NULL && + mod_idx != (unsigned long) -1 && + kallsyms_modules[mod_idx] != 0 && + kallsyms_modules[mod_idx] < kallsyms_module_names_len) + iter->builtin_module_names = + &kallsyms_module_names[kallsyms_modules[mod_idx]]; + iter->hint_builtin_module_idx = mod_idx; + } +#endif return off - iter->nameoff; } @@ -724,7 +838,7 @@ static int update_iter_mod(struct kallsym_iter *iter, loff_t pos) } /* Returns false if pos at or past end of file. */ -static int update_iter(struct kallsym_iter *iter, loff_t pos) +static int update_iter(struct kallsym_iter *iter, loff_t pos, int kallmodsyms) { /* Module symbols can be accessed randomly. */ if (pos >= kallsyms_num_syms) @@ -734,7 +848,7 @@ static int update_iter(struct kallsym_iter *iter, loff_t pos) if (pos != iter->pos) reset_iter(iter, pos); - iter->nameoff += get_ksymbol_core(iter); + iter->nameoff += get_ksymbol_core(iter, kallmodsyms); iter->pos++; return 1; @@ -744,14 +858,14 @@ static void *s_next(struct seq_file *m, void *p, loff_t *pos) { (*pos)++; - if (!update_iter(m->private, *pos)) + if (!update_iter(m->private, *pos, 0)) return NULL; return p; } static void *s_start(struct seq_file *m, loff_t *pos) { - if (!update_iter(m->private, *pos)) + if (!update_iter(m->private, *pos, 0)) return NULL; return m->private; } @@ -760,7 +874,7 @@ static void s_stop(struct seq_file *m, void *p) { } -static int s_show(struct seq_file *m, void *p) +static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) { void *value; struct kallsym_iter *iter = m->private; @@ -771,23 +885,67 @@ static int s_show(struct seq_file *m, void *p) value = iter->show_value ? (void *)iter->value : NULL; - if (iter->module_name[0]) { + /* + * Real module, or built-in module and /proc/kallsyms being shown. + */ + if (iter->module_name[0] != '\0' || + (iter->builtin_module_names != NULL && kallmodsyms != 0)) { char type; /* - * Label it "global" if it is exported, - * "local" if not exported. + * Label it "global" if it is exported, "local" if not exported. */ type = iter->exported ? toupper(iter->type) : tolower(iter->type); - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, iter->module_name); +#ifdef CONFIG_KALLMODSYMS + if (kallmodsyms) { + /* + * /proc/kallmodsyms, built as a module. + */ + if (iter->builtin_module_names == NULL) + seq_printf(m, "%px %c %s\t[%s]\n", value, + type, iter->name, + iter->module_name); + /* + * /proc/kallmodsyms, single-module symbol. + */ + else if (*iter->builtin_module_names != '\0') + seq_printf(m, "%px %c %s\t[%s]\n", value, + type, iter->name, + iter->builtin_module_names); + /* + * /proc/kallmodsyms, multimodule symbol. Formatted + * as \0MODULE_COUNTmodule-1\0module-2\0, where + * MODULE_COUNT is a single byte, 2 or higher. + */ + else { + size_t i = *(char *)(iter->builtin_module_names + 1); + const char *walk = iter->builtin_module_names + 2; + + seq_printf(m, "%px %c %s\t[%s]", value, + type, iter->name, walk); + + while (--i > 0) { + walk += strlen(walk) + 1; + seq_printf (m, " [%s]", walk); + } + seq_printf(m, "\n"); + } + } else /* !kallmodsyms */ +#endif /* CONFIG_KALLMODSYMS */ + seq_printf(m, "%px %c %s\t[%s]\n", value, + type, iter->name, iter->module_name); } else seq_printf(m, "%px %c %s\n", value, iter->type, iter->name); return 0; } +static int s_show(struct seq_file *m, void *p) +{ + return s_show_internal(m, p, 0); +} + static const struct seq_operations kallsyms_op = { .start = s_start, .next = s_next, @@ -795,6 +953,35 @@ static const struct seq_operations kallsyms_op = { .show = s_show }; +#ifdef CONFIG_KALLMODSYMS +static int s_mod_show(struct seq_file *m, void *p) +{ + return s_show_internal(m, p, 1); +} +static void *s_mod_next(struct seq_file *m, void *p, loff_t *pos) +{ + (*pos)++; + + if (!update_iter(m->private, *pos, 1)) + return NULL; + return p; +} + +static void *s_mod_start(struct seq_file *m, loff_t *pos) +{ + if (!update_iter(m->private, *pos, 1)) + return NULL; + return m->private; +} + +static const struct seq_operations kallmodsyms_op = { + .start = s_mod_start, + .next = s_mod_next, + .stop = s_stop, + .show = s_mod_show +}; +#endif + static inline int kallsyms_for_perf(void) { #ifdef CONFIG_PERF_EVENTS @@ -830,7 +1017,8 @@ bool kallsyms_show_value(const struct cred *cred) } } -static int kallsyms_open(struct inode *inode, struct file *file) +static int kallsyms_open_internal(struct inode *inode, struct file *file, + const struct seq_operations *ops) { /* * We keep iterator in m->private, since normal case is to @@ -838,7 +1026,7 @@ static int kallsyms_open(struct inode *inode, struct file *file) * using get_symbol_offset for every symbol. */ struct kallsym_iter *iter; - iter = __seq_open_private(file, &kallsyms_op, sizeof(*iter)); + iter = __seq_open_private(file, ops, sizeof(*iter)); if (!iter) return -ENOMEM; reset_iter(iter, 0); @@ -851,6 +1039,18 @@ static int kallsyms_open(struct inode *inode, struct file *file) return 0; } +static int kallsyms_open(struct inode *inode, struct file *file) +{ + return kallsyms_open_internal(inode, file, &kallsyms_op); +} + +#ifdef CONFIG_KALLMODSYMS +static int kallmodsyms_open(struct inode *inode, struct file *file) +{ + return kallsyms_open_internal(inode, file, &kallmodsyms_op); +} +#endif + #ifdef CONFIG_KGDB_KDB const char *kdb_walk_kallsyms(loff_t *pos) { @@ -861,7 +1061,7 @@ const char *kdb_walk_kallsyms(loff_t *pos) reset_iter(&kdb_walk_kallsyms_iter, 0); } while (1) { - if (!update_iter(&kdb_walk_kallsyms_iter, *pos)) + if (!update_iter(&kdb_walk_kallsyms_iter, *pos, 0)) return NULL; ++*pos; /* Some debugging symbols have no name. Ignore them. */ @@ -878,9 +1078,21 @@ static const struct proc_ops kallsyms_proc_ops = { .proc_release = seq_release_private, }; +#ifdef CONFIG_KALLMODSYMS +static const struct proc_ops kallmodsyms_proc_ops = { + .proc_open = kallmodsyms_open, + .proc_read = seq_read, + .proc_lseek = seq_lseek, + .proc_release = seq_release_private, +}; +#endif + static int __init kallsyms_init(void) { proc_create("kallsyms", 0444, NULL, &kallsyms_proc_ops); +#ifdef CONFIG_KALLMODSYMS + proc_create("kallmodsyms", 0444, NULL, &kallmodsyms_proc_ops); +#endif return 0; } device_initcall(kallsyms_init); From patchwork Thu Dec 16 20:19:19 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nick Alcock X-Patchwork-Id: 12682477 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 134EEC433FE for ; Thu, 16 Dec 2021 20:21:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241307AbhLPUVH (ORCPT ); Thu, 16 Dec 2021 15:21:07 -0500 Received: from mx0a-00069f02.pphosted.com ([205.220.165.32]:41420 "EHLO mx0a-00069f02.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241292AbhLPUVG (ORCPT ); Thu, 16 Dec 2021 15:21:06 -0500 Received: from pps.filterd (m0246627.ppops.net [127.0.0.1]) by mx0b-00069f02.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 1BGIn5Ha025867; Thu, 16 Dec 2021 20:20:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : content-transfer-encoding : content-type : mime-version; s=corp-2021-07-09; bh=lPARvt/9VwVgaz1rJeHwdtNSm/OQoGk8u3eBi4Tr6jQ=; b=Zf8spPy1GwYrkXl6Mf4BP4gSv/q/KOh28feFfQDWUQlH6ZEsbo1BAR3W2xNMqXH5YqiC 0Va7nZf1eUVczK7GF+dzvpKNU4b03kvryQmI4YVnSMHNmFqgmkrlE25RNUfxg2gPc4Lw k7aHNHisVX977Jlx2WluCRGxOMrO+O8j3m3CH61KV8DYZgE2TidIwYWNAAng4LKfSjrR klkOvuJIs44Rr0ueN6G4Dxpp1g/INx/9lZRPDPaKRtg6gJPwaZ6vjzA6vq/5rNmwWSi9 4pA+QPHBeuTqcTwCyM7FnoCgeP2580lbjncY9WSd39mz81LJfSS0RXWuAt42ShpVo1ok qg== Received: from aserp3020.oracle.com (aserp3020.oracle.com [141.146.126.70]) by mx0b-00069f02.pphosted.com with ESMTP id 3cyknp3y7s-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:20:01 +0000 Received: from pps.filterd (aserp3020.oracle.com [127.0.0.1]) by aserp3020.oracle.com (8.16.1.2/8.16.1.2) with SMTP id 1BGKGcsi047804; Thu, 16 Dec 2021 20:20:00 GMT Received: from nam12-bn8-obe.outbound.protection.outlook.com (mail-bn8nam12lp2169.outbound.protection.outlook.com [104.47.55.169]) by aserp3020.oracle.com with ESMTP id 3cxmrdxba4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Thu, 16 Dec 2021 20:20:00 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=fW80iVaYjZZ15h+eybg11PDAfCXQ4GNuYXf544kazoWNOPjp5d630g8zH7X7AmDPF+f8JqquPGLG7SJEBWNrjpOAJw+Zkhw8moScBgEvlplwZItB9yoAqjk6R3J6V7OAAgOfC6eVb2+wx2yqv3oaKYYUuKRE9tk/E+Q6/rxcOJzlus/Hc6gW6Duee4y0wdlkGy7FdJNbXHrKvmBWhwZQljY9/rUmfBIqoXn0Fc36kZyhS+P03Hq4ev09jx+1YOJpyfLJLohBP7gCVD9gE5k0xPzD3yV6oJSFUq3keobnTtD+8o9DZW9yaNHAQD0XoyUrSoyVr0McFae3SUd/BEqpPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=lPARvt/9VwVgaz1rJeHwdtNSm/OQoGk8u3eBi4Tr6jQ=; b=Y4YKtXp0e+lUzuKBYZbCWkEwS+dN8XMnhmW09iEj9GNovbva3BvUPXzkwQIP/TGDKpYgWPCmSGjzb0fcIrelTPcjtnx6lApdyWU56OJiGw0B6evRz25nyHqWrvkZr1wGRrHZ4cdy4GN1zSPwvsBhXJK7LXEfJ9TgS1ZYF86MUGSgoiaLupSwaPgKzfblRlC6fcvM1Z+eNKwZ2fCywLNqNS/YA+fDwLY/PSfnUv1kcbn9Vdre4Imv6E3d4PZ6xvwiZVXVBV1RlCJ05uOkzjH+aeKLWWdRWyP09idFJ96r2k41isz1DpjO7tt+HGwfvrQ/i8R5eKTa9gv7t0Chfk9o0Q== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=oracle.com; dmarc=pass action=none header.from=oracle.com; dkim=pass header.d=oracle.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=oracle.onmicrosoft.com; s=selector2-oracle-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=lPARvt/9VwVgaz1rJeHwdtNSm/OQoGk8u3eBi4Tr6jQ=; b=s22L3wYDNbnBPUdVPndgI+2HEFiUbb7vKe8/s1+VlR6qYjAAGjmebQB2BfZmG5kLCIniMWRRyjucTgXi+iqRu8dE8BwTVTwHKBLXnqxVBJIbHtquDc7jAXV5K4KSu4WzjuqL68iYFIU9hdYmbZvI3d9JUPs0uX036oYSgcIqtcM= Received: from SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) by SA1PR10MB5867.namprd10.prod.outlook.com (2603:10b6:806:22b::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4778.11; Thu, 16 Dec 2021 20:19:58 +0000 Received: from SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e]) by SA2PR10MB4715.namprd10.prod.outlook.com ([fe80::c873:a8b4:278e:939e%7]) with mapi id 15.20.4801.015; Thu, 16 Dec 2021 20:19:58 +0000 From: Nick Alcock To: jeyu@kernel.org, masahiroy@kernel.org Cc: linux-modules@vger.kernel.org, linux-kernel@vger.kernel.org, arnd@arndb.de, akpm@linux-foundation.org, eugene.loh@oracle.com, kris.van.hees@oracle.com Subject: [PATCH v7 7/7] kallsyms: add reliable symbol size info Date: Thu, 16 Dec 2021 20:19:19 +0000 Message-Id: <20211216201919.234994-8-nick.alcock@oracle.com> X-Mailer: git-send-email 2.34.0.258.gc900572c39 In-Reply-To: <20211216201919.234994-1-nick.alcock@oracle.com> References: <20211216201919.234994-1-nick.alcock@oracle.com> X-ClientProxiedBy: LO4P123CA0095.GBRP123.PROD.OUTLOOK.COM (2603:10a6:600:191::10) To SA2PR10MB4715.namprd10.prod.outlook.com (2603:10b6:806:fb::10) MIME-Version: 1.0 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 17d3d733-7d47-4541-6e36-08d9c0d16e53 X-MS-TrafficTypeDiagnostic: SA1PR10MB5867:EE_ X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:4303; X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: dgOVhNwrNEr3MUD7P4PZE2Yl3EROWA4x7SFe9D9PKaDewZFjXGUVbJg/QWpQaPraYIzbNUWM9kAJ5GcpFNephT65OktICXcH5PedlX2r6629HZHZplsNJq7w/ry2hVBIa7IunTm8WPVkXynXFDUpSEXlpO2cr6bMIGLshgpRrCYrm0o4CtR7UHhvFrBngslCcY9wNvenblIritRbK/q41A3C/hBeJeqqGD8g9zTOiGKtiliesLiwt8KKmYWqRKLLYvnJKWQbOGJu+YbGWWI9TkHV0w8NvuMzXOaS9da3V8p9DsBO/Dru5//fSwdYBq6BoXvCKGdqBYKNpBK4haTTUO78ePxsPhMrjWoxdwpz9Oe4F8Aktn7IYKtdZ/B8Q/EnNRKGPrbZN/z/94OEdQ+RT2SJOlISz9Ejortn4o+b9NTxbjuC8UDpgcxrEJOL5YTOZQBR7t+rUe5XI0BJywqzCOEQINuC73K7p0Pv50snuzDNYBN7gNtxqiiSBzlAoGvJC9saGAygbBWnCTD2W8GItFZAnIQBi8dqXKidpFM46StwbKRmWh4elG+KOu1z9OlMNjMjTRLFHyG7opOJ0r8bqYw12UvusHJbvikqZ8EZPGjg1rZjWxMacBRCMHqd1JLZ8RvQA7ITMscgdloBuBw9GQ== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:SA2PR10MB4715.namprd10.prod.outlook.com;PTR:;CAT:NONE;SFS:(366004)(30864003)(5660300002)(2906002)(38100700002)(6512007)(36756003)(4326008)(6486002)(6666004)(107886003)(66946007)(2616005)(186003)(44832011)(8936002)(6506007)(66556008)(83380400001)(1076003)(8676002)(66476007)(316002)(52116002)(508600001)(86362001);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: +q5UvA8UGH1XaKYgmnaddhHbzNnkdEI0w0kcpHiF56wKk1A39VWnMo6JqXPUpxFCdPIt8RNYNH4cfe7WsbXRy0N6RlA00Oxf/4dhy9DDkMvIF3QyNSd5uiXU4yV76C76Hc9fzyFb6oYMnpxVpS0vEdX54MUraaenRZuuOtBKuwmn1ZQJhWUw/438lre7dC64GtcjfJwO9XnqpGyJKDXc8J5ZnxOAoDhDv71+iphXqpQnrYqRskYLzXa6GrDUmwdNi00gYw3H8aU5pN7Jzkoy6IjXkufNm3QBeWTmh46TpEkF82k0Y6G3oOX5mvjQBCVdcsWNqg02nEXg+5AKMS/zq0zgDC/RqDpm0sXT3mMa/oJLBR3OtIN+4cZ4HpcS7MIwWWisIe0n3zNVzi3zYhKOUZ1IOyJ8kSOBdhNeWKR0rSYZhykIhCRZlZpXjJoXoLOWAaBnEES/j0gvajQvkpzTH6ion4rOGzneUy54cwU0v4ZToT+NDZlTKnjctN6IaFk367yHFhZsDjsBZOuuL31ZbdqsCTgenoBHDfF+kG4lDrcd+kxqZZd4I10noPxZZwc0NtojIqjgPFnb8+ar+ku2fmERGIpBQbAPdZmDwKGu6ZEQQ3s+XC3ITa9UDo470zz26mhqfRrfMajkCJj3zxMrMc9cMERB+nqDmQrD8wfXIWEsSDJSy/8zAxM6lz9FCoYLMK11FMYzlnUCo1MBtBulU0GcR40KP9wpKWJ8wnyHTN4x+7BycUuL6Ce40xNA75bZ8f8xl/pZVVXt7B3Uua9vprqmysFkBAJ/WdNFCF9tXkIGLKyUVgL8WmmZQHmP62goGLAXtnh1lmm1buQankMQBtNBKe+DKPS+SLzl9sl3jfAbY6ydkM39nfi2Vqc2b9WCYvLasEZDG2F5z3ew5chewvbVjF/ut+vASOlsrhWyIi9n6ALic3yBGByTjN+OkIeE48yfupREXhYTxGpbDhAY2KcpnH15tylrB8OxZnVapFzS/RFDzZWFCFcedsTx2A7KJtz/RIi4tUgqg52OsjqaHAsvQER+xrVfo0bULFYpFecT+ys/qQaSeKF8Qv3H3s5fOcH/uQUGXprqO1qPBlG3l/StKqtycFoz3ySjHv8yf1fI8L+5Pykk6IhnS11T+Xjl7neJl5CAfo5yC1XHeYOsYMnxGOSWnhqayJiP6dShOwGaZBYxmg4w3UGW5qsYyPOQzXkJzuWzz2wo4RC/xHctKCLl9jLL+447mRK0C3hK1v9ZbERChqQzANFwXn951OrKFqmbRE1ENlEyJgJHvUj4zcGFRp/UCYXh72xll8bWTv13gd+vMDbuSG1LLp0R2O0Ru7qgUZPGwVSdh/Oj9TIiylLgyfeCyEeldGOsfBl/aVDm+Din8or+r6N5ij09IFg3l7o57PsfpA3+Bq1ld5Nxv24y9Q4e1tBFJidWqZnyvcBkWMGfID6YykfgDVSvdq/Yuz380b6B/0qAxJoQbBe/PDU++Et2iuTqy9ZjEx5EFoOA9bXMY2MF+1O/qvmo2ISJghRuhIdu6WfGYP9fH+fcQSBieTpamQqD0nWM1gzuaVt5wkdCpe4iouiY1QZVBHp7Y+YsoWRSUNaXIkSg6+BvcmcLT8ZhQUzo56zCsINcj4eOkfX2C3Ui/rAzKvbUNqQ9 X-OriginatorOrg: oracle.com X-MS-Exchange-CrossTenant-Network-Message-Id: 17d3d733-7d47-4541-6e36-08d9c0d16e53 X-MS-Exchange-CrossTenant-AuthSource: SA2PR10MB4715.namprd10.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 16 Dec 2021 20:19:58.2389 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 4e2c6054-71cb-48f1-bd6c-3a9705aca71b X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: Sz2z6X/Kxj1Y6q+4dxJ6octggtBWLCM7hUpIg8LaG1LQ3pb/L/gBPFcIUM7H7rY8wRxyZaJGSMufA+zqInXbWg== X-MS-Exchange-Transport-CrossTenantHeadersStamped: SA1PR10MB5867 X-Proofpoint-Virus-Version: vendor=nai engine=6300 definitions=10200 signatures=668683 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 mlxscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 suspectscore=0 adultscore=0 malwarescore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2110150000 definitions=main-2112160110 X-Proofpoint-ORIG-GUID: f3slKgY2I2Vy8ozzozhc4lj8JMGu3ZCz X-Proofpoint-GUID: f3slKgY2I2Vy8ozzozhc4lj8JMGu3ZCz Precedence: bulk List-ID: The existing mechanisms in get_symbol_pos to determine the end of a symbol is an inaccurate heuristic. By passing nm -S output into scripts/kallsyms.c and writing the symbol sizes to a new .kallsyms_sizes section, we can get accurate sizes and sort the symbols accordingly, reliably sorting zero-size symbols first (on the grounds that they are usually e.g. section markers, and other symbols at the same address are conceptually contained within them and should be sorted after them), then larger symbols before smaller ones (so that overlapping symbols print the containing symbol first, before its containees). We can also use this to improve aliased symbol detection. Emit the size info as an extra column in /proc/kallmodsyms (since its format is not yet set in stone), and export it to iterator consumers. The notable downside of this is that the new .kallsyms_sizes is pretty big: a PTR per symbol, so vmlinux.o grows by almost a megabyte, though it compresses pretty well, so bzImage grows by only a megabyte. I'm not sure how to reduce this (perhaps using an array with elements sized to be no larger than needed for the contents, so that almost always two-byte entries would do? except that in my test kernel two symbols are bigger than this: sme_workarea, at 400K, and __log_buf, at 100K: the latter seems often likely to be larger than 64K). A simple scheme to reduce this would be to split the sizes array into several arrays with differently-sized elements, and run-length-compress away the zero bytes -- but that's not implemented yet, and might never be if people think the whole idea of this is pointless. In the absence of a way to shrink things, this should probably be hidden behind a new config symbol if exposed at all, and kallmodsyms just shows zero sizes if it's configured out (but this is enough of an RFC that that's not yet done: possibly the benefits of this are too marginal to be worth it, even if they do let kall(mod)syms consumers distinguish symbols from padding, which was previously impossible). Signed-off-by: Nick Alcock Signed-off-by: Eugene Loh Reported-by: kernel test robot --- include/linux/module.h | 7 ++-- kernel/kallsyms.c | 74 ++++++++++++++++++++++------------------- kernel/module.c | 4 ++- scripts/kallsyms.c | 29 +++++++++++++--- scripts/link-vmlinux.sh | 7 +++- 5 files changed, 77 insertions(+), 44 deletions(-) diff --git a/include/linux/module.h b/include/linux/module.h index c9f1200b2312..b58f2de48957 100644 --- a/include/linux/module.h +++ b/include/linux/module.h @@ -590,7 +590,8 @@ struct module *find_module(const char *name); /* Returns 0 and fills in value, defined and namebuf, or -ERANGE if symnum out of range. */ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, - char *name, char *module_name, int *exported); + char *name, char *module_name, unsigned long *size, + int *exported); /* Look for this name: can be of form module:name. */ unsigned long module_kallsyms_lookup_name(const char *name); @@ -768,8 +769,8 @@ static inline int lookup_module_symbol_attrs(unsigned long addr, unsigned long * } static inline int module_get_kallsym(unsigned int symnum, unsigned long *value, - char *type, char *name, - char *module_name, int *exported) + char *type, char *name, char *module_name, + unsigned long *size, int *exported) { return -ERANGE; } diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index c81610ffc4ba..e234c659dfe9 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -36,6 +36,7 @@ */ extern const unsigned long kallsyms_addresses[] __weak; extern const int kallsyms_offsets[] __weak; +extern const unsigned long kallsyms_sizes[] __weak; extern const u8 kallsyms_names[] __weak; /* @@ -277,12 +278,24 @@ int kallsyms_on_each_symbol(int (*fn)(void *, const char *, struct module *, } #endif /* CONFIG_LIVEPATCH */ +/* + * The caller passes in an address, and we return an index to the symbol -- + * potentially also size and offset information. + * But an address might map to multiple symbols because: + * - some symbols might have zero size + * - some symbols might be aliases of one another + * - some symbols might span (encompass) others + * The symbols should already be ordered so that, for a particular address, + * we first have the zero-size ones, then the biggest, then the smallest. + * So we find the index by: + * - finding the last symbol with the target address + * - backing the index up so long as both the address and size are unchanged + */ static unsigned long get_symbol_pos(unsigned long addr, unsigned long *symbolsize, unsigned long *offset) { - unsigned long symbol_start = 0, symbol_end = 0; - unsigned long i, low, high, mid; + unsigned long low, high, mid; /* This kernel should never had been booted. */ if (!IS_ENABLED(CONFIG_KALLSYMS_BASE_RELATIVE)) @@ -303,36 +316,17 @@ static unsigned long get_symbol_pos(unsigned long addr, } /* - * Search for the first aliased symbol. Aliased - * symbols are symbols with the same address. + * Search for the first aliased symbol. */ - while (low && kallsyms_sym_address(low-1) == kallsyms_sym_address(low)) + while (low + && kallsyms_sym_address(low-1) == kallsyms_sym_address(low) + && kallsyms_sizes[low-1] == kallsyms_sizes[low]) --low; - symbol_start = kallsyms_sym_address(low); - - /* Search for next non-aliased symbol. */ - for (i = low + 1; i < kallsyms_num_syms; i++) { - if (kallsyms_sym_address(i) > symbol_start) { - symbol_end = kallsyms_sym_address(i); - break; - } - } - - /* If we found no next symbol, we use the end of the section. */ - if (!symbol_end) { - if (is_kernel_inittext(addr)) - symbol_end = (unsigned long)_einittext; - else if (IS_ENABLED(CONFIG_KALLSYMS_ALL)) - symbol_end = (unsigned long)_end; - else - symbol_end = (unsigned long)_etext; - } - if (symbolsize) - *symbolsize = symbol_end - symbol_start; + *symbolsize = kallsyms_sizes[low]; if (offset) - *offset = addr - symbol_start; + *offset = addr - kallsyms_sym_address(low); return low; } @@ -653,6 +647,7 @@ struct kallsym_iter { loff_t pos_bpf_end; unsigned long value; unsigned int nameoff; /* If iterating in core kernel symbols. */ + unsigned long size; char type; char name[KSYM_NAME_LEN]; char module_name[MODULE_NAME_LEN]; @@ -687,7 +682,7 @@ static int get_ksymbol_mod(struct kallsym_iter *iter) int ret = module_get_kallsym(iter->pos - iter->pos_arch_end, &iter->value, &iter->type, iter->name, iter->module_name, - &iter->exported); + &iter->size, &iter->exported); iter->builtin_module_names = NULL; if (ret < 0) { @@ -760,6 +755,7 @@ static unsigned long get_ksymbol_core(struct kallsym_iter *iter, int kallmodsyms iter->exported = 0; iter->value = kallsyms_sym_address(iter->pos); + iter->size = kallsyms_sizes[iter->pos]; iter->type = kallsyms_get_symbol_type(off); iter->module_name[0] = '\0'; @@ -878,12 +874,14 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) { void *value; struct kallsym_iter *iter = m->private; + unsigned long size; /* Some debugging symbols have no name. Ignore them. */ if (!iter->name[0]) return 0; value = iter->show_value ? (void *)iter->value : NULL; + size = iter->show_value ? iter->size : 0; /* * Real module, or built-in module and /proc/kallsyms being shown. @@ -903,15 +901,15 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) * /proc/kallmodsyms, built as a module. */ if (iter->builtin_module_names == NULL) - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, + seq_printf(m, "%px %lx %c %s\t[%s]\n", value, + size, type, iter->name, iter->module_name); /* * /proc/kallmodsyms, single-module symbol. */ else if (*iter->builtin_module_names != '\0') - seq_printf(m, "%px %c %s\t[%s]\n", value, - type, iter->name, + seq_printf(m, "%px %lx %c %s\t[%s]\n", value, + size, type, iter->name, iter->builtin_module_names); /* * /proc/kallmodsyms, multimodule symbol. Formatted @@ -922,8 +920,8 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) size_t i = *(char *)(iter->builtin_module_names + 1); const char *walk = iter->builtin_module_names + 2; - seq_printf(m, "%px %c %s\t[%s]", value, - type, iter->name, walk); + seq_printf(m, "%px %lx %c %s\t[%s]", value, + size, type, iter->name, walk); while (--i > 0) { walk += strlen(walk) + 1; @@ -935,7 +933,13 @@ static int s_show_internal(struct seq_file *m, void *p, int kallmodsyms) #endif /* CONFIG_KALLMODSYMS */ seq_printf(m, "%px %c %s\t[%s]\n", value, type, iter->name, iter->module_name); - } else + /* + * Non-modular, /proc/kallmodsyms -> print size. + */ + } else if (kallmodsyms) + seq_printf(m, "%px %lx %c %s\n", value, size, + iter->type, iter->name); + else seq_printf(m, "%px %c %s\n", value, iter->type, iter->name); return 0; diff --git a/kernel/module.c b/kernel/module.c index 84a9141a5e15..311eaa8fd21c 100644 --- a/kernel/module.c +++ b/kernel/module.c @@ -4405,7 +4405,8 @@ int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, } int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, - char *name, char *module_name, int *exported) + char *name, char *module_name, unsigned long *size, + int *exported) { struct module *mod; @@ -4424,6 +4425,7 @@ int module_get_kallsym(unsigned int symnum, unsigned long *value, char *type, strlcpy(name, kallsyms_symbol_name(kallsyms, symnum), KSYM_NAME_LEN); strlcpy(module_name, mod->name, MODULE_NAME_LEN); *exported = is_exported(name, *value, mod); + *size = kallsyms->symtab[symnum].st_size; preempt_enable(); return 0; } diff --git a/scripts/kallsyms.c b/scripts/kallsyms.c index 93fdf0dcf587..fcb1d706809c 100644 --- a/scripts/kallsyms.c +++ b/scripts/kallsyms.c @@ -5,7 +5,7 @@ * This software may be used and distributed according to the terms * of the GNU General Public License, incorporated herein by reference. * - * Usage: nm -n vmlinux + * Usage: nm -n -S vmlinux * | scripts/kallsyms [--all-symbols] [--absolute-percpu] * [--base-relative] [--builtin=modules_thick.builtin] * > symbols.S @@ -38,6 +38,7 @@ struct sym_entry { unsigned long long addr; + unsigned long long size; unsigned int len; unsigned int start_pos; unsigned int percpu_absolute; @@ -394,6 +395,7 @@ static bool is_ignored_symbol(const char *name, char type) "kallsyms_addresses", "kallsyms_offsets", "kallsyms_relative_base", + "kallsyms_sizes", "kallsyms_num_syms", "kallsyms_num_modules", "kallsyms_names", @@ -507,10 +509,11 @@ static struct sym_entry *read_symbol(FILE *in) unsigned long long addr; unsigned int len; struct sym_entry *sym; - int rc; + int rc = 0; + unsigned long long size; - rc = fscanf(in, "%llx %c %499s\n", &addr, &type, name); - if (rc != 3) { + rc = fscanf(in, "%llx %llx %c %499s\n", &addr, &size, &type, name); + if (rc != 4) { if (rc != EOF && fgets(name, 500, in) == NULL) fprintf(stderr, "Read error or end of file.\n"); return NULL; @@ -548,6 +551,7 @@ static struct sym_entry *read_symbol(FILE *in) sym->sym[0] = type; strcpy(sym_name(sym), name); sym->percpu_absolute = 0; + sym->size = size; return sym; } @@ -932,6 +936,11 @@ static void write_src(void) printf("\n"); } + output_label("kallsyms_sizes"); + for (i = 0; i < table_cnt; i++) + printf("\tPTR\t%#llx\n", table[i]->size); + printf("\n"); + #ifdef CONFIG_KALLMODSYMS output_kallmodsyms_modules(); output_kallmodsyms_objfiles(); @@ -1189,6 +1198,18 @@ static int compare_symbols(const void *a, const void *b) if (sa->addr < sb->addr) return -1; + /* zero-size markers before nonzero-size symbols */ + if (sa->size > 0 && sb->size == 0) + return 1; + if (sa->size == 0 && sb->size > 0) + return -1; + + /* sort by size (large size preceding symbols it encompasses) */ + if (sa->size < sb->size) + return 1; + if (sa->size > sb->size) + return -1; + /* sort by "weakness" type */ wa = (sa->sym[0] == 'w') || (sa->sym[0] == 'W'); wb = (sb->sym[0] == 'w') || (sb->sym[0] == 'W'); diff --git a/scripts/link-vmlinux.sh b/scripts/link-vmlinux.sh index 5301f3e77116..55815937399b 100755 --- a/scripts/link-vmlinux.sh +++ b/scripts/link-vmlinux.sh @@ -265,7 +265,12 @@ kallsyms() fi info KSYMS ${2} - ${NM} -n ${1} | scripts/kallsyms ${kallsymopt} > ${2} + # "nm -S" does not print symbol size when size is 0 + # Therefore use awk to regularize the data: + # - when there are only three fields, add an explicit "0" + # - when there are already four fields, pass through as is + ${NM} -n -S ${1} | ${AWK} 'NF==3 {print $1, 0, $2, $3}; NF==4' | \ + scripts/kallsyms ${kallsymopt} > ${2} } # Perform one step in kallsyms generation, including temporary linking of